Search in sources :

Example 1 with DefiningFeatureSet

use of org.geotoolkit.storage.feature.DefiningFeatureSet in project geotoolkit by Geomatys.

the class MIFStore method add.

@Override
public synchronized Resource add(Resource resource) throws DataStoreException {
    final FeatureType type;
    if (resource instanceof DefiningFeatureSet) {
        type = ((DefiningFeatureSet) resource).getType();
    } else if (resource instanceof FeatureSet) {
        type = ((FeatureSet) resource).getType();
    } else {
        throw new DataStoreException("Unexpected resource type");
    }
    try {
        manager.addSchema(type.getName(), type);
    } catch (URISyntaxException | IOException e) {
        throw new DataStoreException("We're unable to add a schema because we can't access source files.", e);
    }
    // clear cache
    components = null;
    return findResource(type.getName().toString());
}
Also used : FeatureType(org.opengis.feature.FeatureType) DataStoreException(org.apache.sis.storage.DataStoreException) FeatureSet(org.apache.sis.storage.FeatureSet) DefiningFeatureSet(org.geotoolkit.storage.feature.DefiningFeatureSet) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) DefiningFeatureSet(org.geotoolkit.storage.feature.DefiningFeatureSet)

Example 2 with DefiningFeatureSet

use of org.geotoolkit.storage.feature.DefiningFeatureSet in project geotoolkit by Geomatys.

the class MIFStoreTest method filterProperties.

/**
 * Check that we can filter data properties at read time (using {@link Query#getPropertyNames() }).
 * @throws Exception
 */
@Test
public void filterProperties() throws Exception {
    final Path tmpFile = Files.createTempFile(tempDir, "filtered", ".mif");
    try (final MIFStore store = new MIFStore(tmpFile.toUri())) {
        // create a feature type
        final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
        ftb.setName("test");
        ftb.addAttribute(Integer.class).setName("integerProp");
        ftb.addAttribute(Double.class).setName("doubleProp");
        ftb.addAttribute(String.class).setName("stringProp");
        ftb.addAttribute(Point.class).setName("geometryProp").setCRS(CommonCRS.WGS84.normalizedGeographic());
        final FeatureType featureType = ftb.build();
        store.add(new DefiningFeatureSet(featureType, null));
        assertEquals(1, store.components().size());
        final WritableFeatureSet resource = (WritableFeatureSet) store.components().iterator().next();
        final FeatureType ft = resource.getType();
        final Feature feature1 = ft.newInstance();
        feature1.setPropertyValue("integerProp", 8);
        feature1.setPropertyValue("doubleProp", 3.12);
        feature1.setPropertyValue("stringProp", "hello");
        feature1.setPropertyValue("geometryProp", GF.createPoint(new Coordinate(10.3, 15.7)));
        final Feature feature2 = ft.newInstance();
        feature2.setPropertyValue("integerProp", -15);
        feature2.setPropertyValue("doubleProp", -7.1);
        feature2.setPropertyValue("stringProp", "world");
        feature2.setPropertyValue("geometryProp", GF.createPoint(new Coordinate(-1.6, -5.4)));
        List<Feature> expectedFeatures = new ArrayList<>(2);
        expectedFeatures.add(feature1);
        expectedFeatures.add(feature2);
        resource.add(expectedFeatures.iterator());
        // filter output
        final FeatureQuery query = new FeatureQuery();
        final String[] filteredProps = new String[] { "stringProp", "integerProp" };
        query.setProjection(new FeatureQuery.NamedExpression(FF.property("stringProp")), new FeatureQuery.NamedExpression(FF.property("integerProp")));
        ftb.getProperty("doubleProp").remove();
        ftb.getProperty("geometryProp").remove();
        // Modify original dataset to contain only properties kept in above query.
        final FeatureType filteredType = ftb.build();
        expectedFeatures = expectedFeatures.stream().map(f -> {
            final Feature result = filteredType.newInstance();
            for (final String prop : filteredProps) {
                result.setPropertyValue(prop, f.getPropertyValue(prop));
            }
            return result;
        }).collect(Collectors.toList());
        checkFeatures(expectedFeatures, resource, query);
    }
}
Also used : Path(java.nio.file.Path) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) WritableFeatureSet(org.apache.sis.storage.WritableFeatureSet) ArrayList(java.util.ArrayList) MIFStore(org.geotoolkit.data.mapinfo.mif.MIFStore) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) FeatureQuery(org.apache.sis.storage.FeatureQuery) Feature(org.opengis.feature.Feature) Coordinate(org.locationtech.jts.geom.Coordinate) DefiningFeatureSet(org.geotoolkit.storage.feature.DefiningFeatureSet) Test(org.junit.Test)

Example 3 with DefiningFeatureSet

use of org.geotoolkit.storage.feature.DefiningFeatureSet in project geotoolkit by Geomatys.

the class WeakListenerTest method testWeakStorageListener.

/**
 * Test no memory leak in weak style listener
 */
@Test
public void testWeakStorageListener() throws DataStoreException {
    FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("test1");
    ftb.addAttribute(Integer.class).setName("att");
    final FeatureType type1 = ftb.build();
    ftb = new FeatureTypeBuilder();
    ftb.setName("test2");
    ftb.addAttribute(Integer.class).setName("att2");
    final FeatureType type2 = ftb.build();
    final AtomicInteger count = new AtomicInteger(0);
    final InMemoryAggregate store = new InMemoryAggregate();
    StoreListener listener = new StoreListener() {

        @Override
        public void eventOccured(StoreEvent event) {
            count.incrementAndGet();
        }
    };
    final StorageListener.Weak ref = new StorageListener.Weak(listener);
    ref.registerSource(store);
    store.add(new DefiningFeatureSet(type1, null));
    assertEquals(1, count.get());
    listener = null;
    System.gc();
    store.add(new DefiningFeatureSet(type2, null));
    // listener should have desapear now, so the event should not have been send
    assertEquals(1, count.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StorageListener(org.geotoolkit.storage.event.StorageListener) InMemoryAggregate(org.geotoolkit.storage.memory.InMemoryAggregate) StoreListener(org.apache.sis.storage.event.StoreListener) StoreEvent(org.apache.sis.storage.event.StoreEvent) DefiningFeatureSet(org.geotoolkit.storage.feature.DefiningFeatureSet) Test(org.junit.Test)

Example 4 with DefiningFeatureSet

use of org.geotoolkit.storage.feature.DefiningFeatureSet in project geotoolkit by Geomatys.

the class MemoryFeatureStoreDemo method main.

public static void main(String[] args) throws DataStoreException {
    Demos.init();
    // create the datastore
    final InMemoryStore store = new InMemoryStore();
    // add a schema in the datastore
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("http://geomatys.com", "test");
    ftb.addAttribute(String.class).setName("type");
    ftb.addAttribute(Point.class).setName("the_geom").setCRS(CommonCRS.WGS84.normalizedGeographic());
    final FeatureType type = ftb.build();
    store.add(new DefiningFeatureSet(type, null));
    // query the featurestore like any other
    for (FeatureSet fs : DataStores.flatten(store, true, FeatureSet.class)) {
        System.out.println(fs.getType());
    }
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) InMemoryStore(org.geotoolkit.storage.memory.InMemoryStore) FeatureSet(org.apache.sis.storage.FeatureSet) DefiningFeatureSet(org.geotoolkit.storage.feature.DefiningFeatureSet) DefiningFeatureSet(org.geotoolkit.storage.feature.DefiningFeatureSet)

Example 5 with DefiningFeatureSet

use of org.geotoolkit.storage.feature.DefiningFeatureSet in project geotoolkit by Geomatys.

the class MifDemo method main.

public static void main(String[] args) throws DataStoreException, NoSuchIdentifierException {
    Demos.init();
    try {
        // First of all, we delete the files we want to write in.
        URL destinationURL = new URL(DESTINATION_MIF);
        File tmpMIF = new File(destinationURL.toURI());
        if (tmpMIF.exists()) {
            tmpMIF.delete();
        }
        File tmpMID = new File(DESTINATION_MID);
        if (tmpMID.exists()) {
            tmpMID.delete();
        }
        // To build a valid MIFFeatureStore, the MIF file URL must be given to the store parameters.
        URL dataLocation = MifDemo.class.getResource("/data/world/HY_WATER_AREA_POLYGON.mif");
        System.out.println(MIFProvider.PARAMETERS_DESCRIPTOR);
        final Parameters parameters = Parameters.castOrWrap(MIFProvider.PARAMETERS_DESCRIPTOR.createValue());
        parameters.getOrCreate(MIFProvider.PATH).setValue(dataLocation.toURI());
        // Initialize the store, and create a session to browse it's data.
        final DataStore store1 = DataStores.open(parameters);
        // Create a mif featureStore for writing operation.
        final Parameters writerParam = Parameters.castOrWrap(MIFProvider.PARAMETERS_DESCRIPTOR.createValue());
        writerParam.getOrCreate(MIFProvider.PATH).setValue(destinationURL.toURI());
        final MIFStore writingStore = new MIFStore(writerParam);
        // Here we get a function to set mid file attributes delimiter. MID file is a sort of CSV, and default
        // delimiter (which is \t) can be changed by user. Here I choose coma.
        writingStore.setDelimiter(',');
        // file. All those types inherit from base type, so we get all attributes associated with the geometry.
        for (FeatureSet fs : DataStores.flatten(store1, true, FeatureSet.class)) {
            final FeatureType fType = fs.getType();
            // Get all features of given type.
            try (Stream<Feature> stream = fs.features(false)) {
                // If the type we got don't get super type, it's the store base type. Just print info.
                if (fType.getSuperTypes().isEmpty()) {
                    Iterator it = stream.iterator();
                    while (it.hasNext()) {
                        System.out.println(it.next());
                    }
                } else {
                    // If we got the geometric data, we write it into new MIF/MID files.
                    // First we must specify we must add a featureType to the store. If no base Type have already been
                    // specified, the given feature type parent will be used. Else, we check that given type is compliant
                    // with stored base type.
                    WritableFeatureSet nr = (WritableFeatureSet) writingStore.add(new DefiningFeatureSet(fType, null));
                    nr.add(stream.iterator());
                }
            }
        }
    } catch (Exception ex) {
        LOGGER.log(Level.SEVERE, "Unexpected exception happened.", ex);
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) WritableFeatureSet(org.apache.sis.storage.WritableFeatureSet) Parameters(org.apache.sis.parameter.Parameters) MIFStore(org.geotoolkit.data.mapinfo.mif.MIFStore) Feature(org.opengis.feature.Feature) URL(java.net.URL) NoSuchIdentifierException(org.opengis.util.NoSuchIdentifierException) DataStoreException(org.apache.sis.storage.DataStoreException) DataStore(org.apache.sis.storage.DataStore) Iterator(java.util.Iterator) FeatureSet(org.apache.sis.storage.FeatureSet) WritableFeatureSet(org.apache.sis.storage.WritableFeatureSet) DefiningFeatureSet(org.geotoolkit.storage.feature.DefiningFeatureSet) File(java.io.File) DefiningFeatureSet(org.geotoolkit.storage.feature.DefiningFeatureSet)

Aggregations

DefiningFeatureSet (org.geotoolkit.storage.feature.DefiningFeatureSet)8 FeatureType (org.opengis.feature.FeatureType)8 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)6 WritableFeatureSet (org.apache.sis.storage.WritableFeatureSet)5 Feature (org.opengis.feature.Feature)5 ArrayList (java.util.ArrayList)4 MIFStore (org.geotoolkit.data.mapinfo.mif.MIFStore)4 Test (org.junit.Test)4 Coordinate (org.locationtech.jts.geom.Coordinate)4 Path (java.nio.file.Path)3 FeatureQuery (org.apache.sis.storage.FeatureQuery)3 FeatureSet (org.apache.sis.storage.FeatureSet)3 LineString (org.locationtech.jts.geom.LineString)3 MultiLineString (org.locationtech.jts.geom.MultiLineString)3 Parameters (org.apache.sis.parameter.Parameters)2 DataStoreException (org.apache.sis.storage.DataStoreException)2 InMemoryStore (org.geotoolkit.storage.memory.InMemoryStore)2 Point (org.locationtech.jts.geom.Point)2 File (java.io.File)1 IOException (java.io.IOException)1