Search in sources :

Example 1 with JTS

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;
}
Also used : Measure(org.apache.sis.internal.jaxb.gml.Measure) Unit(javax.measure.Unit) CRS(org.apache.sis.referencing.CRS) GeodeticCalculator(org.apache.sis.referencing.GeodeticCalculator) Spliterators(java.util.Spliterators) NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) Coordinate(org.locationtech.jts.geom.Coordinate) MeasureType(org.geotoolkit.gml.xml.v321.MeasureType) PolygonPatchType(org.geotoolkit.gml.xml.v321.PolygonPatchType) Envelope(org.geotoolkit.gml.xml.Envelope) Matcher(java.util.regex.Matcher) SingleCRS(org.opengis.referencing.crs.SingleCRS) GeometryProperty(org.geotoolkit.gml.xml.GeometryProperty) Map(java.util.Map) Ring(org.geotoolkit.gml.xml.Ring) TransformException(org.opengis.referencing.operation.TransformException) MultiLineString(org.locationtech.jts.geom.MultiLineString) AxesConvention(org.apache.sis.referencing.cs.AxesConvention) AbstractCRS(org.apache.sis.referencing.crs.AbstractCRS) Logger(java.util.logging.Logger) MultiSurface(org.geotoolkit.gml.xml.MultiSurface) Objects(java.util.Objects) Cache(org.apache.sis.util.collection.Cache) List(java.util.List) Stream(java.util.stream.Stream) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) Polygon(org.locationtech.jts.geom.Polygon) Optional(java.util.Optional) Geometry(org.locationtech.jts.geom.Geometry) SurfaceType(org.geotoolkit.gml.xml.v321.SurfaceType) Pattern(java.util.regex.Pattern) Spliterator(java.util.Spliterator) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) FactoryException(org.opengis.util.FactoryException) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Coordinates(org.geotoolkit.gml.xml.Coordinates) PointProperty(org.geotoolkit.gml.xml.PointProperty) ArgumentChecks(org.apache.sis.util.ArgumentChecks) DoubleFunction(java.util.function.DoubleFunction) LinearRing(org.locationtech.jts.geom.LinearRing) HashMap(java.util.HashMap) DirectPositionList(org.geotoolkit.gml.xml.DirectPositionList) Function(java.util.function.Function) Supplier(java.util.function.Supplier) CurveProperty(org.geotoolkit.gml.xml.CurveProperty) MultiGeometry(org.geotoolkit.gml.xml.MultiGeometry) ArrayList(java.util.ArrayList) Angle(javax.measure.quantity.Angle) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) Length(javax.measure.quantity.Length) GeneralDirectPosition(org.apache.sis.geometry.GeneralDirectPosition) AbstractCurveSegment(org.geotoolkit.gml.xml.AbstractCurveSegment) WithCoordinates(org.geotoolkit.gml.xml.WithCoordinates) AxisDirections(org.apache.sis.internal.referencing.AxisDirections) PointPropertyType(org.geotoolkit.gml.xml.v321.PointPropertyType) CurveSegmentArrayProperty(org.geotoolkit.gml.xml.CurveSegmentArrayProperty) StreamSupport(java.util.stream.StreamSupport) Point(org.geotoolkit.gml.xml.Point) NoSuchElementException(java.util.NoSuchElementException) DirectPosition(org.opengis.geometry.DirectPosition) Units(org.apache.sis.measure.Units) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Iterator(java.util.Iterator) PolygonProperty(org.geotoolkit.gml.xml.PolygonProperty) JTS(org.geotoolkit.geometry.jts.JTS) AbstractSurface(org.geotoolkit.gml.xml.AbstractSurface) Consumer(java.util.function.Consumer) MultiCurve(org.geotoolkit.gml.xml.MultiCurve) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) LineString(org.locationtech.jts.geom.LineString) AbstractMap(java.util.AbstractMap) ArcByCenterPointType(org.geotoolkit.gml.xml.v321.ArcByCenterPointType) Curve(org.geotoolkit.gml.xml.Curve) AbstractRingProperty(org.geotoolkit.gml.xml.AbstractRingProperty) MultiPoint(org.locationtech.jts.geom.MultiPoint) SurfaceProperty(org.geotoolkit.gml.xml.SurfaceProperty) Collections(java.util.Collections) LineStringProperty(org.geotoolkit.gml.xml.LineStringProperty) AbstractRing(org.geotoolkit.gml.xml.AbstractRing) MultiPoint(org.locationtech.jts.geom.MultiPoint) MultiSurface(org.geotoolkit.gml.xml.MultiSurface) HashMap(java.util.HashMap) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) Envelope(org.geotoolkit.gml.xml.Envelope) MultiCurve(org.geotoolkit.gml.xml.MultiCurve) Ring(org.geotoolkit.gml.xml.Ring) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) MultiCurve(org.geotoolkit.gml.xml.MultiCurve) Curve(org.geotoolkit.gml.xml.Curve) Point(org.geotoolkit.gml.xml.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint) SurfaceType(org.geotoolkit.gml.xml.v321.SurfaceType) AbstractSurface(org.geotoolkit.gml.xml.AbstractSurface) Geometry(org.locationtech.jts.geom.Geometry) MultiGeometry(org.geotoolkit.gml.xml.MultiGeometry) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) MultiGeometry(org.geotoolkit.gml.xml.MultiGeometry) LinearRing(org.locationtech.jts.geom.LinearRing) Map(java.util.Map) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap)

Example 2 with JTS

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;
        };
    }
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) Arrays(java.util.Arrays) Array(java.lang.reflect.Array) URL(java.net.URL) Date(java.util.Date) BiFunction(java.util.function.BiFunction) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) Envelope(org.opengis.geometry.Envelope) DefaultFactories(org.apache.sis.internal.system.DefaultFactories) FeatureType(org.opengis.feature.FeatureType) IdentifiedType(org.opengis.feature.IdentifiedType) Map(java.util.Map) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole) URI(java.net.URI) NAME_KEY(org.apache.sis.feature.AbstractIdentifiedType.NAME_KEY) ParameterValue(org.opengis.parameter.ParameterValue) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) ArgumentChecks.ensureNonNull(org.apache.sis.util.ArgumentChecks.ensureNonNull) Feature(org.opengis.feature.Feature) NameFactory(org.opengis.util.NameFactory) DefaultAttributeType(org.apache.sis.feature.DefaultAttributeType) Geometries(org.apache.sis.internal.feature.Geometries) Parameters(org.apache.sis.parameter.Parameters) Predicate(java.util.function.Predicate) Collection(java.util.Collection) AttributeType(org.opengis.feature.AttributeType) Attribute(org.opengis.feature.Attribute) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Property(org.opengis.feature.Property) FilterUtilities(org.geotoolkit.filter.FilterUtilities) ArrayFeature(org.geotoolkit.internal.feature.ArrayFeature) List(java.util.List) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) Entry(java.util.Map.Entry) GeneralParameterValue(org.opengis.parameter.GeneralParameterValue) Optional(java.util.Optional) Geometry(org.locationtech.jts.geom.Geometry) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) FactoryException(org.opengis.util.FactoryException) Static(org.apache.sis.util.Static) DefaultNameSpace(org.apache.sis.util.iso.DefaultNameSpace) HashMap(java.util.HashMap) FeatureAssociation(org.opengis.feature.FeatureAssociation) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) DefaultFeatureType(org.apache.sis.feature.DefaultFeatureType) Operation(org.opengis.feature.Operation) ParameterDescriptorGroup(org.opengis.parameter.ParameterDescriptorGroup) BackingStoreException(org.apache.sis.util.collection.BackingStoreException) UID(java.rmi.server.UID) FeatureLoop(org.geotoolkit.internal.feature.FeatureLoop) ResourceId(org.opengis.filter.ResourceId) Iterator(java.util.Iterator) JTS(org.geotoolkit.geometry.jts.JTS) Features(org.apache.sis.feature.Features) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) GenericName(org.opengis.util.GenericName) PropertyType(org.opengis.feature.PropertyType) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) AttributeConvention(org.apache.sis.internal.feature.AttributeConvention) ObjectConverters(org.apache.sis.util.ObjectConverters) Collections(java.util.Collections) ParameterDescriptor(org.opengis.parameter.ParameterDescriptor) FactoryException(org.opengis.util.FactoryException) BackingStoreException(org.apache.sis.util.collection.BackingStoreException) PropertyType(org.opengis.feature.PropertyType) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Aggregations

ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Function (java.util.function.Function)2 Logger (java.util.logging.Logger)2 JTS (org.geotoolkit.geometry.jts.JTS)2 Array (java.lang.reflect.Array)1 URI (java.net.URI)1 URL (java.net.URL)1 UID (java.rmi.server.UID)1 AbstractMap (java.util.AbstractMap)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Date (java.util.Date)1 Entry (java.util.Map.Entry)1 NoSuchElementException (java.util.NoSuchElementException)1