use of org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONGeometryCollection in project geotoolkit by Geomatys.
the class GeoJSONWriter method writeGeoJSONGeometry.
/**
* Write a GeoJSONGeometry
*
* @param jsonGeometry
* @throws IOException
*/
private void writeGeoJSONGeometry(GeoJSONGeometry jsonGeometry) throws IOException {
writer.writeStartObject();
writer.writeStringField(TYPE, jsonGeometry.getType());
if (jsonGeometry instanceof GeoJSONGeometryCollection) {
List<GeoJSONGeometry> geometries = ((GeoJSONGeometryCollection) jsonGeometry).getGeometries();
// "geometries" : [
writer.writeArrayFieldStart(GEOMETRIES);
for (GeoJSONGeometry geometry : geometries) {
writeGeoJSONGeometry(geometry);
}
// "]"
writer.writeEndArray();
} else {
// "coordinates" : [
writer.writeArrayFieldStart(COORDINATES);
if (jsonGeometry instanceof GeoJSONPoint) {
writeArray(((GeoJSONPoint) jsonGeometry).getCoordinates());
} else if (jsonGeometry instanceof GeoJSONLineString) {
writeArray(((GeoJSONLineString) jsonGeometry).getCoordinates());
} else if (jsonGeometry instanceof GeoJSONPolygon) {
writeArray(((GeoJSONPolygon) jsonGeometry).getCoordinates());
} else if (jsonGeometry instanceof GeoJSONMultiPoint) {
writeArray(((GeoJSONMultiPoint) jsonGeometry).getCoordinates());
} else if (jsonGeometry instanceof GeoJSONMultiLineString) {
writeArray(((GeoJSONMultiLineString) jsonGeometry).getCoordinates());
} else if (jsonGeometry instanceof GeoJSONMultiPolygon) {
writeArray(((GeoJSONMultiPolygon) jsonGeometry).getCoordinates());
} else {
throw new IllegalArgumentException("Unsupported geometry type : " + jsonGeometry);
}
// "]"
writer.writeEndArray();
}
writer.writeEndObject();
}
use of org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONGeometryCollection in project geotoolkit by Geomatys.
the class ComplexToGeometryArrayConverter method convert.
/**
* {@inheritDoc}
* @return Geometry array.
*/
@Override
public Geometry[] convert(final Data source, final Map<String, Object> params) throws UnconvertibleObjectException {
String dataMimeTypeIdentifier = null;
try {
final List<Object> data = source.getContent();
if (!data.isEmpty()) {
if (WPSMimeType.APP_GML.val().equalsIgnoreCase(source.getMimeType()) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(source.getMimeType()) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(source.getMimeType())) {
dataMimeTypeIdentifier = "GML";
final List<Geometry> geoms = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
Object val = data.get(i);
if (val instanceof JAXBElement) {
val = ((JAXBElement) val).getValue();
}
if (val instanceof AbstractGeometry) {
geoms.add(GeometrytoJTS.toJTS((AbstractGeometry) val));
} else {
throw new UnconvertibleObjectException("Unknown geometry type at index: " + i);
}
}
return geoms.toArray(new Geometry[geoms.size()]);
} else // array is passed as a GeoJSON FeatureCollection
if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(source.getMimeType())) {
dataMimeTypeIdentifier = "GeoJSON";
final String content = WPSConvertersUtils.geojsonContentAsString(source);
final GeoJSONObject jsonObject = WPSConvertersUtils.readGeoJSONObjectsFromString(content);
// Check that we have a FeatureCollection
if (!(jsonObject instanceof GeoJSONGeometryCollection))
throw new UnconvertibleObjectException("Expected a feature collection. Found : " + jsonObject.getClass().getName());
// Check collection size, if it's empty raise an exception
final GeoJSONGeometryCollection geometryCollection = (GeoJSONGeometryCollection) jsonObject;
int collectionSize = geometryCollection.getGeometries().size();
if (collectionSize == 0)
throw new UnconvertibleObjectException("Empty feature collection.");
// Fill the Geometry array
final Geometry[] geometryArray = new Geometry[collectionSize];
Iterator<GeoJSONGeometry> iterator = geometryCollection.getGeometries().iterator();
int index = 0;
while (iterator.hasNext()) {
final GeoJSONGeometry jsonGeometry = iterator.next();
// GeometryUtils.toJTS(jsonGeometry, crs);
geometryArray[index] = WPSConvertersUtils.convertGeoJSONGeometryToGeometry(jsonGeometry);
index++;
}
return geometryArray;
} else
throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + source.getMimeType());
} else {
throw new UnconvertibleObjectException("Invalid data input : Empty geometry list.");
}
} catch (FactoryException ex) {
throw new UnconvertibleObjectException("Invalid data input : Cannot convert " + dataMimeTypeIdentifier + " geometry.", ex);
} catch (MalformedURLException ex) {
throw new UnconvertibleObjectException("Unknown CRS code or unable to read the CRS from a geometry", ex);
} catch (IOException ex) {
throw new UnconvertibleObjectException(ex);
}
}
Aggregations