Search in sources :

Example 6 with AbstractGeometry

use of org.geotoolkit.gml.xml.AbstractGeometry in project geotoolkit by Geomatys.

the class XmlFeatureReader method getFeatureFromFOI.

protected final Feature getFeatureFromFOI(final AnyFeature foi) {
    if (foi instanceof SamplingFeature) {
        final SamplingFeature feature = (SamplingFeature) foi;
        final Feature f = type.newInstance();
        f.setPropertyValue(AttributeConvention.IDENTIFIER, feature.getId());
        final org.opengis.geometry.Geometry isoGeom = feature.getGeometry();
        try {
            final Geometry geom;
            if (isoGeom instanceof AbstractGeometry) {
                geom = GeometrytoJTS.toJTS((AbstractGeometry) isoGeom);
            } else {
                geom = null;
            }
            if (firstCRS && isoGeom != null) {
                // configure crs in the feature type
                final CoordinateReferenceSystem crs = ((AbstractGeometry) isoGeom).getCoordinateReferenceSystem(false);
                type = FeatureTypeExt.createSubType(type, null, crs);
                firstCRS = false;
            }
            f.setPropertyValue(ATT_DESC.toString(), feature.getDescription());
            if (feature.getName() != null) {
                f.setPropertyValue(ATT_NAME.toString(), feature.getName().toString());
            }
            f.setPropertyValue(ATT_POSITION.toString(), geom);
            final List<String> sampleds = new ArrayList<>();
            for (FeatureProperty featProp : feature.getSampledFeatures()) {
                if (featProp.getHref() != null) {
                    sampleds.add(featProp.getHref());
                }
            }
            f.setPropertyValue(ATT_SAMPLED.toString(), sampleds);
            return f;
        } catch (FactoryException ex) {
            LOGGER.log(Level.WARNING, "error while transforming GML geometry to JTS", ex);
        }
    } else {
        LOGGER.warning("unable to find a valid feature of interest in the observation");
    }
    return null;
}
Also used : AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) FactoryException(org.opengis.util.FactoryException) ArrayList(java.util.ArrayList) SamplingFeature(org.geotoolkit.sampling.xml.SamplingFeature) SamplingFeature(org.geotoolkit.sampling.xml.SamplingFeature) AnyFeature(org.opengis.observation.AnyFeature) Feature(org.opengis.feature.Feature) FeatureProperty(org.geotoolkit.gml.xml.FeatureProperty) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) Geometry(org.locationtech.jts.geom.Geometry) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 7 with AbstractGeometry

use of org.geotoolkit.gml.xml.AbstractGeometry in project geotoolkit by Geomatys.

the class GeometryTransformer method convertMultiSurface.

private MultiPolygon convertMultiSurface(final MultiSurface mp) {
    Polygon[] polys;
    try {
        polys = mp.getSurfaceMember().stream().map(SurfaceProperty::getAbstractSurface).map(surface -> {
            if (surface instanceof AbstractGeometry) {
                return (AbstractGeometry) surface;
            }
            throw new UnconvertibleObjectException("We cannot convert a non-geometric object: " + surface.getClass());
        }).map(p -> new GeometryTransformer(p, this).get()).map(Polygon.class::cast).toArray(size -> new Polygon[size]);
    } catch (ClassCastException e) {
        throw new UnconvertibleObjectException("Cannot create a multi-surface, because some of its components are not polygons", e);
    }
    final MultiPolygon result = GF.createMultiPolygon(polys);
    applyCRS(result);
    return result;
}
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) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon)

Example 8 with AbstractGeometry

use of org.geotoolkit.gml.xml.AbstractGeometry in project geotoolkit by Geomatys.

the class JTStoGeometry method toGML.

/**
 * Try to convert a JTS GeometryCollection to a GML AbstractGeometricAggregateType
 *
 * @param jtsGeom {@link GeometryCollection collection}
 * @param crs Coordinate Reference System
 * @return AbstractGeometricAggregateType
 *
 * @throws org.opengis.referencing.NoSuchAuthorityCodeException - if {@link CoordinateReferenceSystem crs} can't be
 * injected into the {@link AbstractGeometry}.
 * @throws org.opengis.util.FactoryException - if {@link CoordinateReferenceSystem crs} can't be injected into the
 * {@link AbstractGeometry}.
 */
public static AbstractGeometricAggregate toGML(final String gmlVersion, final GeometryCollection jtsGeom, final CoordinateReferenceSystem crs) throws NoSuchAuthorityCodeException, FactoryException {
    // Test if it's a 2D Geometry from CRS
    isValideGeometry(crs);
    if (jtsGeom.isEmpty()) {
        throw new IllegalArgumentException("Cannot write empty geometry for unidentified geometry collection type");
    }
    final Class buffer = jtsGeom.getGeometryN(0).getClass();
    // Get the class of the first geometry in the GeometryCollection
    if (!(buffer.isAssignableFrom(Polygon.class) || buffer.isAssignableFrom(Point.class) || buffer.isAssignableFrom(LineString.class))) {
        throw new IllegalArgumentException("Writing GML geometry collection works only for Points, LineStrings and polygons");
    }
    // Verify that we've got a single geometry type
    boolean isSupported = true;
    final List<Geometry> innerGeometries = new ArrayList<>(jtsGeom.getNumGeometries());
    for (int i = 0; i < jtsGeom.getNumGeometries(); i++) {
        final Geometry subGeom = jtsGeom.getGeometryN(i);
        if (!(subGeom.getClass().isAssignableFrom(buffer))) {
            throw new IllegalArgumentException("Writing GML geometry collection containing different geometry types is not supported");
        }
        innerGeometries.add(subGeom);
    }
    final GeometryFactory gf = JTS.getFactory();
    // Convert to a MultiPoint
    if (buffer.equals(Point.class)) {
        final MultiPoint mutlPt = gf.createMultiPoint(innerGeometries.toArray(new Point[innerGeometries.size()]));
        JTS.setCRS(mutlPt, crs);
        return toGML(gmlVersion, mutlPt, crs);
    // Convert to a MultiLineString
    } else if (buffer.equals(LineString.class)) {
        final MultiLineString multLineString = gf.createMultiLineString(innerGeometries.toArray(new LineString[innerGeometries.size()]));
        JTS.setCRS(multLineString, crs);
        return toGML(gmlVersion, multLineString, crs);
    } else if (buffer.equals(Polygon.class)) {
        final MultiPolygon multPoly = gf.createMultiPolygon(innerGeometries.toArray(new Polygon[innerGeometries.size()]));
        JTS.setCRS(multPoly, crs);
        return toGML(gmlVersion, multPoly, crs);
    } else {
        throw new IllegalArgumentException("Writing GML geometry collection works only for Points, LineStrings and polygons");
    }
}
Also used : MultiPoint(org.locationtech.jts.geom.MultiPoint) MultiLineString(org.locationtech.jts.geom.MultiLineString) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) ArrayList(java.util.ArrayList) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) Geometry(org.locationtech.jts.geom.Geometry) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon)

Example 9 with AbstractGeometry

use of org.geotoolkit.gml.xml.AbstractGeometry in project geotoolkit by Geomatys.

the class JTSISOtransformTest method multiPolygonToISOTest.

@Test
public void multiPolygonToISOTest() throws Exception {
    GeometryFactory fact = org.geotoolkit.geometry.jts.JTS.getFactory();
    final Coordinate[] coordinates = new Coordinate[5];
    coordinates[0] = new Coordinate(0, 0);
    coordinates[1] = new Coordinate(0, 1);
    coordinates[2] = new Coordinate(1, 1);
    coordinates[3] = new Coordinate(1, 0);
    coordinates[4] = new Coordinate(0, 0);
    LinearRing linear = org.geotoolkit.geometry.jts.JTS.getFactory().createLinearRing(coordinates);
    Polygon poly = new Polygon(linear, null, fact);
    poly.setSRID(2154);
    final Coordinate[] coordinates2 = new Coordinate[5];
    coordinates2[0] = new Coordinate(10, 10);
    coordinates2[1] = new Coordinate(10, 11);
    coordinates2[2] = new Coordinate(11, 11);
    coordinates2[3] = new Coordinate(11, 10);
    coordinates2[4] = new Coordinate(10, 10);
    LinearRing linear2 = org.geotoolkit.geometry.jts.JTS.getFactory().createLinearRing(coordinates2);
    Polygon poly2 = new Polygon(linear2, null, fact);
    poly2.setSRID(2154);
    Polygon[] polygons = new Polygon[2];
    polygons[0] = poly;
    polygons[1] = poly2;
    MultiPolygon m = new MultiPolygon(polygons, fact);
    m.setSRID(2154);
    AbstractGeometry gml = JTStoGeometry.toGML("3.2.1", m);
    final Geometry geom = GeometrytoJTS.toJTS(gml);
    // System.out.println(geom);
    System.out.println("SRID:" + geom.getSRID());
}
Also used : AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) Geometry(org.locationtech.jts.geom.Geometry) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) Coordinate(org.locationtech.jts.geom.Coordinate) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) LinearRing(org.locationtech.jts.geom.LinearRing) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Test(org.junit.Test)

Example 10 with AbstractGeometry

use of org.geotoolkit.gml.xml.AbstractGeometry in project geotoolkit by Geomatys.

the class JTSISOtransformTest method polygonToISOTest.

@Test
public void polygonToISOTest() throws Exception {
    GeometryFactory fact = org.geotoolkit.geometry.jts.JTS.getFactory();
    final Coordinate[] coordinates = new Coordinate[5];
    coordinates[0] = new Coordinate(0, 0);
    coordinates[1] = new Coordinate(0, 1);
    coordinates[2] = new Coordinate(1, 1);
    coordinates[3] = new Coordinate(1, 0);
    coordinates[4] = new Coordinate(0, 0);
    LinearRing linear = org.geotoolkit.geometry.jts.JTS.getFactory().createLinearRing(coordinates);
    Polygon poly = new Polygon(linear, null, fact);
    poly.setSRID(2154);
    AbstractGeometry gml = JTStoGeometry.toGML("3.2.1", poly);
    final Geometry geom = GeometrytoJTS.toJTS(gml);
    // System.out.println(geom);
    System.out.println("SRID:" + geom.getSRID());
}
Also used : AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) Geometry(org.locationtech.jts.geom.Geometry) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) Coordinate(org.locationtech.jts.geom.Coordinate) LinearRing(org.locationtech.jts.geom.LinearRing) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Test(org.junit.Test)

Aggregations

AbstractGeometry (org.geotoolkit.gml.xml.AbstractGeometry)20 Geometry (org.locationtech.jts.geom.Geometry)14 FactoryException (org.opengis.util.FactoryException)11 JAXBElement (javax.xml.bind.JAXBElement)7 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)7 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 JAXBException (javax.xml.bind.JAXBException)4 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)4 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)4 LineString (org.locationtech.jts.geom.LineString)4 MultiLineString (org.locationtech.jts.geom.MultiLineString)4 MultiPolygon (org.locationtech.jts.geom.MultiPolygon)4 Polygon (org.locationtech.jts.geom.Polygon)4 Map (java.util.Map)3 JTStoGeometry (org.geotoolkit.gml.JTStoGeometry)3 CurveProperty (org.geotoolkit.gml.xml.CurveProperty)3 SurfaceProperty (org.geotoolkit.gml.xml.SurfaceProperty)3 GeoJSONGeometry (org.geotoolkit.internal.geojson.binding.GeoJSONGeometry)3 GeoJSONObject (org.geotoolkit.internal.geojson.binding.GeoJSONObject)3