use of org.geotoolkit.internal.geojson.binding.GeoJSONFeature in project geotoolkit by Geomatys.
the class ComplexToGeometryConverter method convert.
/**
* {@inheritDoc}
* @return Geometry.
*/
@Override
public Geometry convert(final Data source, final Map<String, Object> params) throws UnconvertibleObjectException {
String dataMimeTypeIdentifier = null;
try {
final List<Object> data = source.getContent();
final String mimeType = source.getMimeType();
if (data.size() != 1)
throw new UnconvertibleObjectException("Invalid data input : Only one geometry expected.");
if (WPSMimeType.APP_GML.val().equalsIgnoreCase(mimeType) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(mimeType) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(mimeType)) {
dataMimeTypeIdentifier = "GML";
Object value = data.get(0);
if (value instanceof JAXBElement) {
value = ((JAXBElement) value).getValue();
}
AbstractGeometry abstractGeo;
if (value instanceof AbstractGeometry) {
abstractGeo = (AbstractGeometry) value;
} else if (value instanceof String) {
abstractGeo = WPSConvertersUtils.readGMLGeometryFromString((String) value);
} else {
throw new UnconvertibleObjectException("Invalid data input content for " + dataMimeTypeIdentifier + " geometry.");
}
return GeometrytoJTS.toJTS(abstractGeo);
} else if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(mimeType)) {
dataMimeTypeIdentifier = "GeoJSON";
final String content = WPSConvertersUtils.geojsonContentAsString(source);
final GeoJSONObject jsonObject = WPSConvertersUtils.readGeoJSONObjectsFromString(content);
if (!(jsonObject instanceof GeoJSONFeature)) {
throw new UnconvertibleObjectException("Expected a GeoJSONGeometry and found a " + jsonObject.getClass().getName());
}
Geometry geom = WPSConvertersUtils.convertGeoJSONGeometryToGeometry(((GeoJSONFeature) jsonObject).getGeometry());
Class expectedClass = (Class) params.get(WPSObjectConverter.TARGET_CLASS);
/*
* Linear ring does not exist in GeoJson spec, so we transform polygon geometry during conversion
*/
if (geom instanceof Polygon && LinearRing.class.equals(expectedClass)) {
CoordinateReferenceSystem crs = JTS.findCoordinateReferenceSystem(geom);
Polygon poly = (Polygon) geom;
LinearRing lr = GF.createLinearRing(poly.getExteriorRing().getCoordinateSequence());
JTS.setCRS(lr, crs);
return lr;
}
return geom;
} else if (WPSMimeType.APP_EWKT.val().equalsIgnoreCase(mimeType)) {
Object value = data.get(0);
if (value instanceof String) {
String wkt = (String) value;
int idx = wkt.indexOf(';');
CoordinateReferenceSystem crs = null;
if (idx > 0) {
try {
int srid = Integer.valueOf(wkt.substring(5, idx));
if (srid > 0) {
crs = CRS.forCode("EPSG:" + srid);
crs = AbstractCRS.castOrCopy(crs).forConvention(AxesConvention.RIGHT_HANDED);
}
} catch (IllegalArgumentException ex) {
throw new UnconvertibleObjectException("Incorrect SRID definition " + wkt);
}
wkt = wkt.substring(idx + 1);
}
final WKTReader reader = new WKTReader();
final Geometry geom;
try {
geom = reader.read(wkt);
} catch (ParseException ex) {
throw new UnconvertibleObjectException("Incorrect WKT definition " + wkt);
}
geom.setUserData(crs);
return geom;
} else {
throw new UnconvertibleObjectException("Expected a WKT String and found a " + value.getClass().getName());
}
} else {
throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + source.getMimeType());
}
} catch (ClassCastException ex) {
throw new UnconvertibleObjectException("Invalid data input : empty " + dataMimeTypeIdentifier + " geometry.", ex);
} catch (FactoryException ex) {
throw new UnconvertibleObjectException("Invalid data input : Cannot convert " + dataMimeTypeIdentifier + " geometry.", ex);
} catch (MalformedURLException ex) {
throw new UnconvertibleObjectException("Unable to read the CRS from the GeoJSONGeometry.", ex);
} catch (IOException ex) {
throw new UnconvertibleObjectException(ex);
}
}
use of org.geotoolkit.internal.geojson.binding.GeoJSONFeature 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);
}
}
Aggregations