Search in sources :

Example 1 with MIFStore

use of org.geotoolkit.data.mapinfo.mif.MIFStore 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 2 with MIFStore

use of org.geotoolkit.data.mapinfo.mif.MIFStore 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)

Example 3 with MIFStore

use of org.geotoolkit.data.mapinfo.mif.MIFStore in project geotoolkit by Geomatys.

the class MIFStoreTest method multipleGeometryTypes.

/**
 * Check that we're capable of writing then read back features with
 * different type of geometries (point, polygon, etc.) into a single MIF-MID
 * file.
 */
@Test
public void multipleGeometryTypes() throws Exception {
    final Path tmpFile = Files.createTempFile(tempDir, "multiple", ".mif");
    try (final MIFStore store = new MIFStore(tmpFile.toUri())) {
        final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
        ftb.setName("multi");
        ftb.addAttribute(String.class).setName("otherProperty");
        ftb.addAttribute(Geometry.class).setName("geometry").setCRS(CommonCRS.WGS84.normalizedGeographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
        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();
        final Point point = GF.createPoint(new Coordinate(10.3, 15.7));
        feature1.setPropertyValue("otherProperty", "here is a point");
        feature1.setPropertyValue("geometry", point);
        final Feature feature2 = ft.newInstance();
        final Coordinate startPoint = new Coordinate(-1.6, -5.4);
        Coordinate[] coords = { startPoint, new Coordinate(0, 0), new Coordinate(3.1, 3.4) };
        final MultiLineString line = GF.createMultiLineString(new LineString[] { GF.createLineString(coords) });
        feature2.setPropertyValue("otherProperty", "here is a line");
        feature2.setPropertyValue("geometry", line);
        final Feature feature3 = ft.newInstance();
        coords = new Coordinate[] { startPoint, new Coordinate(-6, -6), new Coordinate(-3.1, -3.4), startPoint };
        final MultiPolygon polygon = GF.createMultiPolygon(new Polygon[] { GF.createPolygon(coords) });
        feature3.setPropertyValue("otherProperty", "here is a polygon");
        feature3.setPropertyValue("geometry", polygon);
        final Feature feature4 = ft.newInstance();
        final Geometry[] geometries = new Geometry[] { polygon, line, GF.createMultiPoint(new Point[] { point }) };
        feature4.setPropertyValue("otherProperty", "here is a collection of geometries.");
        feature4.setPropertyValue("geometry", GF.createGeometryCollection(geometries));
        final List<Feature> expectedFeatures = new ArrayList<>(4);
        expectedFeatures.add(feature1);
        expectedFeatures.add(feature2);
        expectedFeatures.add(feature3);
        expectedFeatures.add(feature4);
        resource.add(expectedFeatures.iterator());
        checkFeatures(expectedFeatures, resource, new FeatureQuery());
    }
}
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) MultiLineString(org.locationtech.jts.geom.MultiLineString) ArrayList(java.util.ArrayList) MIFStore(org.geotoolkit.data.mapinfo.mif.MIFStore) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) Point(org.locationtech.jts.geom.Point) FeatureQuery(org.apache.sis.storage.FeatureQuery) Feature(org.opengis.feature.Feature) Geometry(org.locationtech.jts.geom.Geometry) Coordinate(org.locationtech.jts.geom.Coordinate) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) DefiningFeatureSet(org.geotoolkit.storage.feature.DefiningFeatureSet) Test(org.junit.Test)

Example 4 with MIFStore

use of org.geotoolkit.data.mapinfo.mif.MIFStore in project geotoolkit by Geomatys.

the class MIFStoreTest method testCreate.

/**
 * Test store creation.
 *
 * @throws Exception
 */
@Test
public void testCreate() throws Exception {
    final Path f = Files.createTempFile(tempDir, "test", ".mif");
    final MIFProvider ff = (MIFProvider) DataStores.getProviderById("MIF-MID");
    final Parameters params = Parameters.castOrWrap(ff.getOpenParameters().createValue());
    params.getOrCreate(MIFProvider.PATH).setValue(f.toUri());
    // create new store from scratch
    try (final MIFStore store = (MIFStore) ff.open(params)) {
        assertNotNull(store);
        // 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());
        WritableFeatureSet resource = (WritableFeatureSet) store.components().iterator().next();
        FeatureType ft = resource.getType();
        for (PropertyType desc : featureType.getProperties(true)) {
            if (!(desc instanceof AttributeType))
                // ignore conventions and operations
                continue;
            PropertyType td = ft.getProperty(desc.getName().toString());
            assertNotNull(td);
            assertTrue(((AttributeType) td).getValueClass().isAssignableFrom(((AttributeType) desc).getValueClass()));
        }
        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)));
        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)));
        final List<Feature> expectedFeatures = new ArrayList<>(2);
        expectedFeatures.add(feature1);
        expectedFeatures.add(feature2);
        resource.add(Arrays.asList(feature1, feature2).iterator());
        checkFeatures(expectedFeatures, resource, new FeatureQuery());
    }
}
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) Parameters(org.apache.sis.parameter.Parameters) ArrayList(java.util.ArrayList) MIFStore(org.geotoolkit.data.mapinfo.mif.MIFStore) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) PropertyType(org.opengis.feature.PropertyType) FeatureQuery(org.apache.sis.storage.FeatureQuery) Feature(org.opengis.feature.Feature) MIFProvider(org.geotoolkit.data.mapinfo.mif.MIFProvider) Coordinate(org.locationtech.jts.geom.Coordinate) AttributeType(org.opengis.feature.AttributeType) DefiningFeatureSet(org.geotoolkit.storage.feature.DefiningFeatureSet) Test(org.junit.Test)

Aggregations

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