Search in sources :

Example 96 with FeatureType

use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.

the class GeoJSONWriteTest method writePropertyArrayTest.

@Test
public void writePropertyArrayTest() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    FeatureType validFeatureType = buildPropertyArrayFeatureType("arrayFT", Point.class);
    Point pt = (Point) WKT_READER.read(PROPERTIES.getProperty("point"));
    double[][] array1 = new double[5][5];
    double[][] array2 = new double[5][5];
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            array1[i][j] = i + j;
            array2[i][j] = i - j;
        }
    }
    try (GeoJSONStreamWriter fw = new GeoJSONStreamWriter(baos, validFeatureType, 4)) {
        Feature feature = fw.next();
        feature.setPropertyValue("array", array1);
        feature.setPropertyValue(AttributeConvention.GEOMETRY, pt);
        fw.write();
        feature = fw.next();
        feature.setPropertyValue("array", array2);
        feature.setPropertyValue(AttributeConvention.GEOMETRY, pt);
        fw.write();
    }
    String outputJSON = baos.toString("UTF-8");
    assertNotNull(outputJSON);
    assertFalse(outputJSON.isEmpty());
    String expected = "{\n" + "\"type\":\"FeatureCollection\"\n" + ",\"features\":[\n" + "{\"type\":\"Feature\",\"id\":\"0\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-105.0162,39.5742]},\"properties\":{\"array\":[[0.0,1.0,2.0,3.0,4.0],[1.0,2.0,3.0,4.0,5.0],[2.0,3.0,4.0,5.0,6.0],[3.0,4.0,5.0,6.0,7.0],[4.0,5.0,6.0,7.0,8.0]]}}\n" + ",{\"type\":\"Feature\",\"id\":\"1\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-105.0162,39.5742]},\"properties\":{\"array\":[[0.0,-1.0,-2.0,-3.0,-4.0],[1.0,0.0,-1.0,-2.0,-3.0],[2.0,1.0,0.0,-1.0,-2.0],[3.0,2.0,1.0,0.0,-1.0],[4.0,3.0,2.0,1.0,0.0]]}}\n" + "]}";
    compareJSON(expected, outputJSON);
}
Also used : FeatureType(org.opengis.feature.FeatureType) GeoJSONStreamWriter(org.geotoolkit.storage.geojson.GeoJSONStreamWriter) SimpleInternationalString(org.apache.sis.util.SimpleInternationalString) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 97 with FeatureType

use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.

the class GeoJSONWriteTest method writeSimpleFTTest.

@Test
public void writeSimpleFTTest() throws Exception {
    final Path file = Files.createTempFile("point", ".json");
    final WritableFeatureSet store = new GeoJSONStore(new GeoJSONProvider(), file, 7);
    assertNotNull(store);
    final String typeName = file.getFileName().toString().replace(".json", "");
    // test creating an unvalid feature type
    final FeatureType unvalidFeatureType = buildGeometryFeatureType("test", Point.class);
    try {
        store.updateType(unvalidFeatureType);
        fail();
    } catch (DataStoreException ex) {
    // normal exception
    }
    // test writing and reading a feature
    final FeatureType validFeatureType = buildGeometryFeatureType(typeName, Point.class);
    store.updateType(validFeatureType);
    assertNotNull(store.getType());
    assertTrue(Files.exists(file));
    final Point expectedPoint = GF.createPoint(new Coordinate(-105.01621, 39.57422));
    final Feature feature = store.getType().newInstance();
    feature.setPropertyValue(AttributeConvention.GEOMETRY, expectedPoint);
    feature.setPropertyValue("type", "simple");
    feature.setPropertyValue("time", new Date(0));
    store.add(Arrays.asList(feature).iterator());
    assertTrue(Files.exists(file));
    try (Stream<Feature> stream = store.features(false)) {
        final Iterator<Feature> reader = stream.iterator();
        assertTrue(reader.hasNext());
        final Feature f = reader.next();
        assertEquals("simple", f.getPropertyValue("type"));
        assertEquals(expectedPoint, f.getPropertyValue(AttributeConvention.GEOMETRY));
    }
    Files.deleteIfExists(file);
}
Also used : Path(java.nio.file.Path) WritableFeatureSet(org.apache.sis.storage.WritableFeatureSet) FeatureType(org.opengis.feature.FeatureType) DataStoreException(org.apache.sis.storage.DataStoreException) GeoJSONProvider(org.geotoolkit.storage.geojson.GeoJSONProvider) GeoJSONStore(org.geotoolkit.storage.geojson.GeoJSONStore) SimpleInternationalString(org.apache.sis.util.SimpleInternationalString) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 98 with FeatureType

use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.

the class GeoJSONWriteTest method writeSimpleFTCborTest.

@Test
public void writeSimpleFTCborTest() throws Exception {
    final Path file = Files.createTempFile("point", ".cbor");
    final WritableFeatureSet store = new GeoJSONStore(new GeoJSONProvider(), file, 7);
    assertNotNull(store);
    final String typeName = file.getFileName().toString().replace(".cbor", "");
    // test creating an unvalid feature type
    final FeatureType unvalidFeatureType = buildGeometryFeatureType("test", Point.class);
    try {
        store.updateType(unvalidFeatureType);
        fail();
    } catch (DataStoreException ex) {
    // normal exception
    }
    // test writing and reading a feature
    final FeatureType validFeatureType = buildGeometryFeatureType(typeName, Point.class);
    store.updateType(validFeatureType);
    assertNotNull(store.getType());
    assertTrue(Files.exists(file));
    final Point expectedPoint = GF.createPoint(new Coordinate(-105.01621, 39.57422));
    final Feature feature = store.getType().newInstance();
    feature.setPropertyValue(AttributeConvention.GEOMETRY, expectedPoint);
    feature.setPropertyValue("type", "simple");
    store.add(Arrays.asList(feature).iterator());
    assertTrue(Files.exists(file));
    try (Stream<Feature> stream = store.features(false)) {
        final Iterator<Feature> reader = stream.iterator();
        assertTrue(reader.hasNext());
        final Feature f = reader.next();
        assertEquals("simple", f.getPropertyValue("type"));
        assertEquals(expectedPoint, f.getPropertyValue(AttributeConvention.GEOMETRY));
    }
    Files.deleteIfExists(file);
}
Also used : Path(java.nio.file.Path) WritableFeatureSet(org.apache.sis.storage.WritableFeatureSet) FeatureType(org.opengis.feature.FeatureType) DataStoreException(org.apache.sis.storage.DataStoreException) GeoJSONProvider(org.geotoolkit.storage.geojson.GeoJSONProvider) GeoJSONStore(org.geotoolkit.storage.geojson.GeoJSONStore) SimpleInternationalString(org.apache.sis.util.SimpleInternationalString) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 99 with FeatureType

use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.

the class GeoJSONWriteTest method writeCollectionsTest.

@Test
public void writeCollectionsTest() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    FeatureType validFeatureType = buildGeometryFeatureType("simpleFT", Point.class);
    Point pt = (Point) WKT_READER.read(PROPERTIES.getProperty("point"));
    Link l = new Link("http://test.com", null, null, null);
    List<Link> links = new ArrayList<>();
    links.add(l);
    try (GeoJSONStreamWriter fw = new GeoJSONStreamWriter(baos, validFeatureType, JsonEncoding.UTF8, 4, false)) {
        fw.writeCollection(links, 10, 5);
        Feature feature = fw.next();
        feature.setPropertyValue("type", "feat1");
        feature.setPropertyValue("time", new Date(0));
        feature.setPropertyValue(AttributeConvention.GEOMETRY, pt);
        fw.write();
        feature = fw.next();
        feature.setPropertyValue("type", "feat2");
        feature.setPropertyValue("time", new Date(1));
        feature.setPropertyValue(AttributeConvention.GEOMETRY, pt);
        fw.write();
    }
    String outputJSON = baos.toString("UTF-8");
    assertNotNull(outputJSON);
    assertFalse(outputJSON.isEmpty());
    String expected = "{\n" + "\"type\":\"FeatureCollection\"\n" + ",\"numberMatched\":10\n" + ",\"numberReturned\":5\n" + ",\"links\":[{\"href\":\"http://test.com\"}]\n" + ",\"features\":[\n" + "{\"type\":\"Feature\",\"id\":0,\"geometry\":{\"type\":\"Point\",\"coordinates\":[-105.0162,39.5742]},\"properties\":{\"type\":\"feat1\",\"time\":\"1970-01-01T00:00:00Z\"}}\n" + ",{\"type\":\"Feature\",\"id\":1,\"geometry\":{\"type\":\"Point\",\"coordinates\":[-105.0162,39.5742]},\"properties\":{\"type\":\"feat2\",\"time\":\"1970-01-01T00:00:00.001Z\"}}\n" + "]}";
    compareJSON(expected, outputJSON);
    assertEquals(expected, outputJSON);
}
Also used : FeatureType(org.opengis.feature.FeatureType) GeoJSONStreamWriter(org.geotoolkit.storage.geojson.GeoJSONStreamWriter) SimpleInternationalString(org.apache.sis.util.SimpleInternationalString) Feature(org.opengis.feature.Feature) Link(org.geotoolkit.atom.xml.Link) Test(org.junit.Test)

Example 100 with FeatureType

use of org.opengis.feature.FeatureType in project geotoolkit by Geomatys.

the class GeoJSONWriteTest method writeAbstractGeometryTest.

@Test
public void writeAbstractGeometryTest() throws Exception {
    Path file = Files.createTempFile("geoms", ".json");
    WritableFeatureSet store = new GeoJSONStore(new GeoJSONProvider(), file, 7);
    assertNotNull(store);
    String typeName = file.getFileName().toString().replace(".json", "");
    FeatureType validFeatureType = buildGeometryFeatureType(typeName, Geometry.class);
    store.updateType(validFeatureType);
    assertNotNull(store.getType());
    assertTrue(Files.exists(file));
    Point pt = (Point) WKT_READER.read(PROPERTIES.getProperty("point"));
    MultiPoint mpt = (MultiPoint) WKT_READER.read(PROPERTIES.getProperty("multipoint"));
    LineString line = (LineString) WKT_READER.read(PROPERTIES.getProperty("linestring"));
    MultiLineString mline = (MultiLineString) WKT_READER.read(PROPERTIES.getProperty("multilinestring"));
    Polygon poly = (Polygon) WKT_READER.read(PROPERTIES.getProperty("polygon"));
    MultiPolygon mpoly = (MultiPolygon) WKT_READER.read(PROPERTIES.getProperty("multipolygon"));
    GeometryCollection coll = (GeometryCollection) WKT_READER.read(PROPERTIES.getProperty("geometrycollection"));
    Feature feature = store.getType().newInstance();
    feature.setPropertyValue("type", "Point");
    feature.setPropertyValue(AttributeConvention.GEOMETRY, pt);
    store.add(Arrays.asList(feature).iterator());
    feature = store.getType().newInstance();
    feature.setPropertyValue("type", "MultiPoint");
    feature.setPropertyValue(AttributeConvention.GEOMETRY, mpt);
    store.add(Arrays.asList(feature).iterator());
    feature = store.getType().newInstance();
    feature.setPropertyValue("type", "LineString");
    feature.setPropertyValue(AttributeConvention.GEOMETRY, line);
    store.add(Arrays.asList(feature).iterator());
    feature = store.getType().newInstance();
    feature.setPropertyValue("type", "MultiLineString");
    feature.setPropertyValue(AttributeConvention.GEOMETRY, mline);
    store.add(Arrays.asList(feature).iterator());
    feature = store.getType().newInstance();
    feature.setPropertyValue("type", "Polygon");
    feature.setPropertyValue(AttributeConvention.GEOMETRY, poly);
    store.add(Arrays.asList(feature).iterator());
    feature = store.getType().newInstance();
    feature.setPropertyValue("type", "MultiPolygon");
    feature.setPropertyValue(AttributeConvention.GEOMETRY, mpoly);
    store.add(Arrays.asList(feature).iterator());
    feature = store.getType().newInstance();
    feature.setPropertyValue("type", "GeometryCollection");
    feature.setPropertyValue(AttributeConvention.GEOMETRY, coll);
    store.add(Arrays.asList(feature).iterator());
    assertTrue(Files.exists(file));
    assertEquals(7, store.features(false).count());
    try (Stream<Feature> stream = store.features(false)) {
        Iterator<Feature> ite = stream.iterator();
        while (ite.hasNext()) {
            Feature f = ite.next();
            // System.out.println(f);
            Geometry geom = (Geometry) f.getPropertyValue(AttributeConvention.GEOMETRY);
            if (geom instanceof Point) {
                assertTrue(pt.equalsExact(geom, 0.0000001));
            } else if (geom instanceof MultiPoint) {
                assertTrue(mpt.equalsExact(geom, 0.0000001));
            } else if (geom instanceof LineString) {
                assertTrue(line.equalsExact(geom, 0.0000001));
            } else if (geom instanceof MultiLineString) {
                assertTrue(mline.equalsExact(geom, 0.0000001));
            } else if (geom instanceof Polygon) {
                assertTrue(poly.equalsExact(geom, 0.0000001));
            } else if (geom instanceof MultiPolygon) {
                assertTrue(mpoly.equalsExact(geom, 0.0000001));
            } else if (geom instanceof GeometryCollection) {
                assertTrue(coll.equalsExact(geom, 0.0000001));
            }
        }
    }
    Files.deleteIfExists(file);
}
Also used : Path(java.nio.file.Path) WritableFeatureSet(org.apache.sis.storage.WritableFeatureSet) FeatureType(org.opengis.feature.FeatureType) GeoJSONProvider(org.geotoolkit.storage.geojson.GeoJSONProvider) GeoJSONStore(org.geotoolkit.storage.geojson.GeoJSONStore) SimpleInternationalString(org.apache.sis.util.SimpleInternationalString) Feature(org.opengis.feature.Feature) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) Test(org.junit.Test)

Aggregations

FeatureType (org.opengis.feature.FeatureType)303 Feature (org.opengis.feature.Feature)146 Test (org.junit.Test)122 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)100 GenericName (org.opengis.util.GenericName)70 ArrayList (java.util.ArrayList)65 DataStoreException (org.apache.sis.storage.DataStoreException)56 PropertyType (org.opengis.feature.PropertyType)51 Coordinate (org.locationtech.jts.geom.Coordinate)50 Point (org.locationtech.jts.geom.Point)41 FeatureSet (org.apache.sis.storage.FeatureSet)37 Query (org.geotoolkit.storage.feature.query.Query)36 AttributeType (org.opengis.feature.AttributeType)36 FeatureAssociationRole (org.opengis.feature.FeatureAssociationRole)33 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)32 WritableFeatureSet (org.apache.sis.storage.WritableFeatureSet)31 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)27 Geometry (org.locationtech.jts.geom.Geometry)27 URL (java.net.URL)26 HashMap (java.util.HashMap)26