use of org.geotoolkit.internal.geojson.binding.GeoJSONObject 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);
}
}
Aggregations