use of org.geotoolkit.geometry.jts.JTS in project geotoolkit by Geomatys.
the class GeometryTransformer method get.
/**
* Procede to conversion from GML to JTS.
* @return Created JTS geometry. Never null, but can be an emptry geometry.
* @throws UnconvertibleObjectException If we don't know how to process
* source GML geometry.
*/
@Override
public Geometry get() throws UnconvertibleObjectException {
Geometry geometry;
/*
* SIMPLE CASES
*/
if (source instanceof org.geotoolkit.gml.xml.Point) {
geometry = accumulateAndBuild(coords -> (coords.length > 0 ? GF.createPoint(coords[0]) : GF.createPoint((Coordinate) null)));
} else if (source instanceof org.geotoolkit.gml.xml.LineString) {
geometry = accumulateAndBuild(GF::createLineString);
} else if (source instanceof org.geotoolkit.gml.xml.LinearRing) {
geometry = accumulateAndBuild(GF::createLinearRing);
} else if (source instanceof Curve) {
geometry = convertCurve((Curve) source);
} else if (source instanceof Envelope) {
geometry = convertEnvelope((Envelope) source);
/*
* COMPOSED GEOMETRIES
*/
} else if (source instanceof org.geotoolkit.gml.xml.Ring) {
geometry = convertRing((Ring) source);
} else if (source instanceof org.geotoolkit.gml.xml.Polygon) {
geometry = convertPolygon((org.geotoolkit.gml.xml.Polygon) source);
if (isForceMultiPolygon()) {
Polygon[] polys = { (Polygon) geometry };
final MultiPolygon result = GF.createMultiPolygon(polys);
applyCRS(result);
geometry = result;
}
} else if (source instanceof AbstractSurface) {
if (source instanceof SurfaceType) {
geometry = convertSurface((SurfaceType) source);
} else if (source instanceof org.geotoolkit.gml.xml.v311.SurfaceType) {
geometry = convertSurface((org.geotoolkit.gml.xml.v311.SurfaceType) source);
} else // TODO : complex case
{
throw new IllegalArgumentException("Unsupported geometry type : " + source.getClass());
}
/*
* GEOMETRY COLLECTIONS
*/
} else if (source instanceof org.geotoolkit.gml.xml.MultiPoint) {
geometry = convertMultiPoint((org.geotoolkit.gml.xml.MultiPoint) source);
} else if (source instanceof org.geotoolkit.gml.xml.MultiLineString) {
geometry = convertMultiLineString((org.geotoolkit.gml.xml.MultiLineString) source);
} else if (source instanceof MultiCurve) {
geometry = convertMultiCurve((MultiCurve) source);
} else if (source instanceof org.geotoolkit.gml.xml.MultiPolygon) {
geometry = convertMultiPolygon((org.geotoolkit.gml.xml.MultiPolygon) source);
} else if (source instanceof MultiSurface) {
geometry = convertMultiSurface((MultiSurface) source);
} else if (source instanceof MultiGeometry) {
geometry = convertMultiGeometry((MultiGeometry) source);
} else {
throw new IllegalArgumentException("Unsupported geometry type : " + source.getClass());
}
// store identifier in user map
final String id = source.getId();
if (id != null && !id.isEmpty()) {
Object userData = geometry.getUserData();
Map values;
if (userData instanceof Map) {
values = (Map) userData;
} else if (userData instanceof CoordinateReferenceSystem) {
values = new HashMap();
values.put(org.apache.sis.internal.feature.jts.JTS.CRS_KEY, userData);
} else if (userData == null) {
values = new HashMap();
} else {
throw new IllegalArgumentException("Unexpected user data object : " + userData);
}
values.put("@id", id);
geometry.setUserData(values);
}
return geometry;
}
use of org.geotoolkit.geometry.jts.JTS in project geotoolkit by Geomatys.
the class FeatureExt method prepareGeometryExtractor.
/**
* Try to create an operator to extract primary geometry from features of a specific type. This method offers the
* following advantages:
* <ul>
* <li>When read geometry does not define any CRS, we assign the one extracted from related property type.</li>
* <li>Property/characteristic analysis is done on assembly, to ensure minimal overhead on result function execution.</li>
* </ul>
*
* @param targetType Type of the features that will be passed as input to the resulting function. Cannot be null.
* @return A function for geometry extraction from features whose is or inherits from input type. Never null.
* If we cannot return a valid value, an error will be thrown as specified by {@link #getDefaultGeometry(FeatureType)}.
* @throws RuntimeException See {@link #getDefaultGeometry(FeatureType)}.
*/
public static Function<Feature, Geometry> prepareGeometryExtractor(final FeatureType targetType) {
ensureNonNull("Target type", targetType);
PropertyType geom = getDefaultGeometry(targetType);
// Minor optimisation : directly use geometry attribute in case a link convention has been set.
geom = Features.getLinkTarget(geom).map(name -> targetType.getProperty(name)).orElse(geom);
final AttributeType<?> attr = Features.toAttribute(geom).orElseThrow(() -> new IllegalStateException("Cannot extract geometries when associate type is not an attribute"));
final Class<?> vClass = attr.getValueClass();
if (!Geometry.class.isAssignableFrom(vClass)) {
throw new UnsupportedOperationException("Only JTS geometries are supported for now.");
}
// Name is built from geom, not attr, because attr can be a virtual result property, not present in source type.
// For example, if you've got two numeric attributes x and y, then add a concatenation operation, you've got no
// geometric attribute, but a geometric operation.
final String name = geom.getName().toString();
final CoordinateReferenceSystem crs = AttributeConvention.getCRSCharacteristic(targetType, attr);
if (crs == null) {
return f -> (Geometry) f.getPropertyValue(name);
} else {
return f -> {
final Object value = f.getPropertyValue(name);
if (value == null)
return null;
final Geometry geometry = (Geometry) value;
final CoordinateReferenceSystem currentCrs;
try {
currentCrs = JTS.findCoordinateReferenceSystem(geometry);
} catch (FactoryException e) {
throw new BackingStoreException(e);
}
if (currentCrs == null)
JTS.setCRS(geometry, crs);
return geometry;
};
}
}
Aggregations