use of org.geotoolkit.internal.geojson.binding.GeoJSONGeometry in project geotoolkit by Geomatys.
the class GeoJSONWriter method writeSingleGeometry.
/**
* Write a JTS Geometry
*
* @param geom
* @throws IOException
*/
void writeSingleGeometry(Geometry geom) throws IOException {
GeoJSONGeometry jsonGeometry = GeoJSONGeometry.toGeoJSONGeometry(geom);
writeGeoJSONGeometry(jsonGeometry);
}
use of org.geotoolkit.internal.geojson.binding.GeoJSONGeometry in project geotoolkit by Geomatys.
the class GeometryToComplexConverter method convert.
/**
* {@inheritDoc}
*/
@Override
public Data convert(final Geometry source, final Map<String, Object> params) throws UnconvertibleObjectException {
if (source == null) {
throw new UnconvertibleObjectException("The output data should be defined.");
} else if (params == null) {
throw new UnconvertibleObjectException("Not enough information about data format");
}
final Object tmpMime = params.get(MIME);
final String mime;
if (tmpMime instanceof String) {
mime = (String) tmpMime;
} else {
throw new UnconvertibleObjectException("No valid mime type given. We cannot determine output image format");
}
final Data complex = new Data();
complex.setMimeType(mime);
final Object tmpEncoding = params.get(ENCODING);
if (tmpEncoding instanceof String) {
complex.setEncoding((String) tmpEncoding);
}
final Object tmpSchema = params.get(SCHEMA);
if (tmpSchema instanceof String) {
complex.setSchema((String) tmpSchema);
}
Object tmpGmlVersion = params.get(GMLVERSION);
final String gmlVersion;
if (tmpGmlVersion instanceof String) {
gmlVersion = (String) tmpGmlVersion;
} else {
gmlVersion = "3.2.1";
}
final String mimeType = complex.getMimeType();
if (WPSMimeType.APP_GML.val().equalsIgnoreCase(mimeType) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(mimeType) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(mimeType)) {
try {
final AbstractGeometry gmlGeom = JTStoGeometry.toGML(gmlVersion, source);
complex.getContent().add(gmlGeom);
} catch (NoSuchAuthorityCodeException ex) {
throw new UnconvertibleObjectException(ex);
} catch (FactoryException ex) {
throw new UnconvertibleObjectException(ex);
}
} else if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(mimeType)) {
GeoJSONGeometry jsonGeometry = GeoJSONGeometry.toGeoJSONGeometry(source);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
GeoJSONStreamWriter.writeSingleGeometry(baos, WPSConvertersUtils.convertGeoJSONGeometryToGeometry(jsonGeometry), JsonEncoding.UTF8, WPSConvertersUtils.FRACTION_DIGITS, true);
WPSConvertersUtils.addCDATAToComplex(baos.toString("UTF-8"), complex);
} catch (UnsupportedEncodingException e) {
throw new UnconvertibleObjectException("Can't convert output stream into String.", e);
} catch (IOException ex) {
throw new UnconvertibleObjectException(ex);
} catch (FactoryException ex) {
throw new UnconvertibleObjectException("Couldn't decode a CRS.", ex);
}
} else if (WPSMimeType.APP_EWKT.val().equalsIgnoreCase(mimeType)) {
Geometry geom = source;
int srid = 0;
try {
CoordinateReferenceSystem crs = Geometries.wrap(geom).get().getCoordinateReferenceSystem();
if (crs != null) {
final IdentifiedObjectFinder finder = IdentifiedObjects.newFinder("EPSG");
finder.setIgnoringAxes(true);
final CoordinateReferenceSystem epsgcrs = (CoordinateReferenceSystem) finder.findSingleton(crs);
if (epsgcrs != null) {
srid = IdentifiedObjects.lookupEPSG(epsgcrs);
// force geometry in longitude first
final CoordinateReferenceSystem crs2 = ((AbstractCRS) crs).forConvention(AxesConvention.RIGHT_HANDED);
if (crs2 != crs) {
geom = org.apache.sis.internal.feature.jts.JTS.transform(geom, crs2);
}
}
}
} catch (FactoryException | MismatchedDimensionException | TransformException ex) {
throw new UnconvertibleObjectException(ex.getMessage(), ex);
}
String wkt = geom.toText();
if (srid > 0) {
wkt = "SRID=" + srid + ";" + wkt;
}
complex.getContent().add(wkt);
} else {
throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + complex.getMimeType());
}
return complex;
}
use of org.geotoolkit.internal.geojson.binding.GeoJSONGeometry 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