use of org.geotoolkit.internal.geojson.binding.GeoJSONGeometry in project geotoolkit by Geomatys.
the class GeometryToComplexConverterTest method getGeometry.
/**
* Helper method that read a json file containing a single Geometry
* @param jsonFile file to read
* @return a geometry
* @throws IOException
* @throws FactoryException
*/
private static Geometry getGeometry(Path jsonFile) throws IOException, FactoryException {
final GeoJSONObject geoJsonObject = GeoJSONParser.parse(jsonFile);
if (!(geoJsonObject instanceof GeoJSONGeometry))
fail();
final GeoJSONGeometry jsonGeometry = (GeoJSONGeometry) geoJsonObject;
return WPSConvertersUtils.convertGeoJSONGeometryToGeometry(jsonGeometry);
}
use of org.geotoolkit.internal.geojson.binding.GeoJSONGeometry in project geotoolkit by Geomatys.
the class GeoJSONWriteTest method readingAsWritingPolygonTest.
@Test
public void readingAsWritingPolygonTest() throws IOException {
final String simplePolygonResource = "/org/apache/sis/internal/storage/geojson/simplePolygon.json";
try (final InputStream inputStream = GeoJSONWriteTest.class.getResourceAsStream(simplePolygonResource);
final InputStream controlInputStream = GeoJSONWriteTest.class.getResourceAsStream(simplePolygonResource)) {
final ObjectMapper mapper = new ObjectMapper();
final GeoJSONGeometry json = mapper.readValue(inputStream, GeoJSONGeometry.class);
assertTrue(json instanceof GeoJSONGeometry.GeoJSONPolygon);
final String expectedJSON = IOUtilities.toString(controlInputStream, Charset.forName("UTF-8"));
final String writtenJSON = mapper.writeValueAsString(json);
assertNotNull(writtenJSON);
assertNotNull(expectedJSON);
assertEquals(expectedJSON.trim(), mapper.writeValueAsString(json).trim());
}
}
use of org.geotoolkit.internal.geojson.binding.GeoJSONGeometry in project geotoolkit by Geomatys.
the class GeoJSONReadTest method mixed_arrays.
@Test
public void mixed_arrays() throws Exception {
try (final InputStream resource = GeoJSONReadTest.class.getResourceAsStream("/org/apache/sis/internal/storage/geojson/mixed_arrays.json")) {
if (resource == null)
throw new IllegalStateException("Cannot find a test resource");
final GeoJSONObject readValue = GeoJSONParser.parse(resource);
assertNotNull(readValue);
assertTrue(readValue instanceof GeoJSONFeature);
final GeoJSONFeature feature = (GeoJSONFeature) readValue;
// Ensure no side-effect would break other parts of the feature reading
final GeoJSONGeometry geom = feature.getGeometry();
assertTrue("Read feature should contain a point, but we've read: " + geom, geom instanceof GeoJSONGeometry.GeoJSONPoint);
// Now, we can check our arrays have been well-parsed
final Map<String, Object> properties = feature.getProperties();
assertPropertyIs(new double[] { 2., 3., 4. }, "numberMix1", properties);
assertPropertyIs(new double[] { 2., 3., 4. }, "numberMix2", properties);
assertPropertyIs(new int[] { 42, 51 }, "intArray", properties);
assertPropertyIs(new long[] { 1, 7_000_000_000l }, "longArray", properties);
assertPropertyIs(new Double[] { 2., null, 4. }, "numbersWithNullValues", properties);
assertPropertyIs(new Object[] { null, null }, "onlyNullValues", properties);
assertPropertyIs(new Object[0], "emptyArray", properties);
assertPropertyIs(new Object[] { 2.0, "I'm a text", null }, "arbitraryMix", properties);
}
}
use of org.geotoolkit.internal.geojson.binding.GeoJSONGeometry in project geotoolkit by Geomatys.
the class GeoJSONReader method fillProperty.
/**
* Try to convert value as expected in PropertyType description.
*
* @param prop
* @param value
*/
private void fillProperty(Attribute prop, Object value) throws BackingStoreException {
Object convertValue = null;
try {
if (value instanceof GeoJSONGeometry) {
convertValue = GeoJSONGeometry.toJTS((GeoJSONGeometry) value, null);
} else if (value != null) {
final AttributeType<?> propertyType = prop.getType();
final Class binding = propertyType.getValueClass();
if (value.getClass().isArray() && binding.isArray()) {
int nbdim = 1;
Class base = value.getClass().getComponentType();
while (base.isArray()) {
base = base.getComponentType();
nbdim++;
}
convertValue = rebuildArray(value, base, nbdim);
} else {
convertValue = convert(value, binding);
}
}
} catch (UnconvertibleObjectException e1) {
throw new BackingStoreException(String.format("Inconvertible property %s : %s", prop.getName().tip().toString(), e1.getMessage()), e1);
}
prop.setValue(convertValue);
}
use of org.geotoolkit.internal.geojson.binding.GeoJSONGeometry in project geotoolkit by Geomatys.
the class GeoJSONWriter method writeSingleGeometry.
/**
* Write a GeometryAttribute
*
* @param geom
* @throws IOException
*/
void writeSingleGeometry(Attribute geom) throws IOException {
GeoJSONGeometry jsonGeometry = GeoJSONGeometry.toGeoJSONGeometry((Geometry) geom.getValue());
writeGeoJSONGeometry(jsonGeometry);
}
Aggregations