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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations