Search in sources :

Example 1 with JTSGeometryFactory

use of org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSGeometryFactory in project geotoolkit by Geomatys.

the class GeometryUtils method createPolyhedralSurface.

public static PolyhedralSurface createPolyhedralSurface(final DirectPosition[][] patchPoints) {
    // get the crs and factories
    final CoordinateReferenceSystem crs = patchPoints[0][0].getCoordinateReferenceSystem();
    final GeometryFactory geometryFactory = new JTSGeometryFactory(crs);
    // create polygons from each of the arrays of directPositions
    final List polygons = new ArrayList(patchPoints.length);
    for (int i = 0; i < patchPoints.length; i++) {
        final Polygon polygon = createPolygon(patchPoints[i]);
        polygons.add(polygon);
    }
    return geometryFactory.createPolyhedralSurface(polygons);
}
Also used : JTSGeometryFactory(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSGeometryFactory) GeometryFactory(org.opengis.geometry.coordinate.GeometryFactory) JTSGeometryFactory(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSGeometryFactory) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Polygon(org.opengis.geometry.coordinate.Polygon)

Example 2 with JTSGeometryFactory

use of org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSGeometryFactory in project geotoolkit by Geomatys.

the class GeometryUtils method createPolygon.

public static Polygon createPolygon(final DirectPosition[] exteriorRingPoints, final DirectPosition[][] interiorRingsPoints) {
    final CoordinateReferenceSystem crs = exteriorRingPoints[0].getCoordinateReferenceSystem();
    final GeometryFactory geometryFactory = new JTSGeometryFactory(crs);
    final PrimitiveFactory primitiveFactory = new JTSPrimitiveFactory(crs);
    final Ring exteriorRing = createRing(primitiveFactory, exteriorRingPoints);
    List interiorRingList = interiorRingsPoints.length == 0 ? Collections.EMPTY_LIST : new ArrayList(interiorRingsPoints.length);
    for (int i = 0; i < interiorRingsPoints.length; i++) {
        final DirectPosition[] interiorRingPoints = interiorRingsPoints[i];
        interiorRingList.add(createRing(primitiveFactory, interiorRingPoints));
    }
    final SurfaceBoundary surfaceBoundary = primitiveFactory.createSurfaceBoundary(exteriorRing, interiorRingList);
    return geometryFactory.createPolygon(surfaceBoundary);
}
Also used : JTSGeometryFactory(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSGeometryFactory) DirectPosition(org.opengis.geometry.DirectPosition) GeometryFactory(org.opengis.geometry.coordinate.GeometryFactory) JTSGeometryFactory(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSGeometryFactory) SurfaceBoundary(org.opengis.geometry.primitive.SurfaceBoundary) Ring(org.opengis.geometry.primitive.Ring) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) JTSPrimitiveFactory(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.primitive.JTSPrimitiveFactory) JTSPrimitiveFactory(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.primitive.JTSPrimitiveFactory) PrimitiveFactory(org.opengis.geometry.primitive.PrimitiveFactory)

Example 3 with JTSGeometryFactory

use of org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSGeometryFactory in project geotoolkit by Geomatys.

the class GeometryUtils method createCurve.

private static Curve createCurve(final PrimitiveFactory primitiveFactory, final DirectPosition[] points) {
    final GeometryFactory geometryFactory = new JTSGeometryFactory(primitiveFactory.getCoordinateReferenceSystem());
    final List curveSegmentList = Collections.singletonList(createLineString(geometryFactory, points));
    final Curve curve = primitiveFactory.createCurve(curveSegmentList);
    return curve;
}
Also used : JTSGeometryFactory(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSGeometryFactory) GeometryFactory(org.opengis.geometry.coordinate.GeometryFactory) JTSGeometryFactory(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSGeometryFactory) Curve(org.opengis.geometry.primitive.Curve) CompositeCurve(org.opengis.geometry.complex.CompositeCurve) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with JTSGeometryFactory

use of org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSGeometryFactory in project geotoolkit by Geomatys.

the class GeometryUtils method ensureWGS84.

/**
 * Verifies the CRS of the specified {@code DirectPosition} is
 * WGS84, and returns it unmodified if it is.
 * If not, transforms the input into a new DirectPosition
 * with a WGS84 CRS.  Returns it as a LatLonAlt if input was LatLonAlt.
 * @param dp The DirectPosition to examine and transform if necessary
 * @return The original DirectPosition if it was already WGS84,
 * or the transformed DirectPosition.
 */
public static DirectPosition ensureWGS84(DirectPosition dp) {
    CoordinateReferenceSystem crs = dp.getCoordinateReferenceSystem();
    int dim = crs.getCoordinateSystem().getDimension();
    CoordinateReferenceSystem bcrs = crs instanceof ProjectedCRS ? ((ProjectedCRS) crs).getBaseCRS() : crs;
    GeographicCRS wgs84crs = CommonCRS.WGS84.geographic3D();
    // have doubts about following line, was the commented out 2nd clause to condition doing anything - colin
    if (bcrs.equals(wgs84crs)) {
        // || bcrs.equals(CRSUtils.WGS84_PROJ)) {
        return dp;
    }
    // again, what does the follllowing achieve? - colin
    if (bcrs.toWKT().indexOf("WGS84") > -1) {
        return dp;
    }
    if (bcrs instanceof GeographicCRS) {
        if (((GeographicCRS) bcrs).getDatum().equals(wgs84crs.getDatum())) {
            return dp;
        }
    }
    // not going to need CommonFactory.getCoordinateOperationFactory(),
    // can use transform util in org.geotoolkit.referencing.CRS instaed
    // CoordinateReferenceSystem crs2 = dim == 2 ? wgs84crs : CRSUtils.WGS84_PROJ;
    // same equality issues as above
    DirectPosition dp2 = new JTSGeometryFactory(wgs84crs).createDirectPosition();
    try {
        MathTransform transform = CRS.findOperation(crs, wgs84crs, null).getMathTransform();
        transform.transform(dp, dp2);
    } catch (FactoryException fe) {
        LOGGER.log(Level.WARNING, "Could not create CoordinateOperation to convert DirectPosition CRS " + crs.getName() + " to WGS84, using original coordinates", fe);
    // throw new IllegalArgumentException("Unconvertible coordinate CRS");
    } catch (TransformException e) {
        LOGGER.log(Level.WARNING, "Could not transform DirectPosition CRS " + crs.getName() + " to WGS84, using original coordinates", e);
    // throw new IllegalArgumentException("Unconvertible coordinate CRS");
    } catch (MismatchedDimensionException e) {
        // PENDING(NL): There's probably something better we can do here
        // than just throw an exception.  Normally we only care about lat and lon,
        // and if one has altitude and the other doesn't that shouldn't
        // be a showstopper.
        LOGGER.log(Level.WARNING, "Dimension mismatch prevented conversion of DirectPosition CRS " + crs.getName() + " to WGS84, using original coordinates", e);
    // throw new IllegalArgumentException("Unconvertible coordinate CRS");
    }
    return dp2;
// hmm, not sure about following line,
// think the LatLongAlt class was specific to how the polexis code works
// and is not needed here
// boolean wasLatLonAlt = dp instanceof LatLongAlt;
/*
        if (wasLatLonAlt) {
            dp = commonFactory.getGeometryFactory(crs).createDirectPosition();
        }
        */
/*
        CommonFactory commonFactory = FactoryManager.getCommonFactory();
        CoordinateOperationFactory coopFactory = commonFactory.getCoordinateOperationFactory();
        try {
            CoordinateReferenceSystem crs2 = dim == 2 ? wgs84crs : CRSUtils.WGS84_PROJ;
            CoordinateOperation coOp = coopFactory.createOperation(crs, crs2);
            DirectPosition dp2 = commonFactory.getGeometryFactory(crs2).createDirectPosition();
            dp2 = coOp.getMathTransform().transform(dp, dp2);
            if (dp2.getCoordinateReferenceSystem() != null) {
                if (wasLatLonAlt) {
                    dp2 = new LatLonAlt(dp2);
                }
                return dp2;
            } else {
                getLog().warn(
                    "Attempted to convert coordinate CRS, transform method returned DirectPosition with null CRS, using original coordinates",
                    new IllegalArgumentException("Unconvertible coordinate CRS"));
            }
        } catch (FactoryException fe) {
            getLog().warn("Could not create CoordinateOperation to convert DirectPosition CRS "
                + crs.getName() + " to WGS84, using original coordinates", fe);
            //throw new IllegalArgumentException("Unconvertible coordinate CRS");
        } catch (TransformException e) {
            getLog().warn("Could not transform DirectPosition CRS "
                + crs.getName() + " to WGS84, using original coordinates", e);
            //throw new IllegalArgumentException("Unconvertible coordinate CRS");
        } catch (MismatchedDimensionException e) {
            // PENDING(NL): There's probably something better we can do here
            // than just throw an exception.  Normally we only care about lat and lon,
            // and if one has altitude and the other doesn't that shouldn't
            // be a showstopper.
            getLog().warn("Dimension mismatch prevented conversion of DirectPosition CRS "
                + crs.getName() + " to WGS84, using original coordinates", e);
            //throw new IllegalArgumentException("Unconvertible coordinate CRS");
        } catch (RuntimeException e) {
            getLog().warn("Could not convert DirectPosition CRS "
                + crs.getName() + " to WGS84, using original coordinates", e);
            //throw e;
        }
        return dp;*/
}
Also used : DirectPosition(org.opengis.geometry.DirectPosition) JTSGeometryFactory(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSGeometryFactory) ProjectedCRS(org.opengis.referencing.crs.ProjectedCRS) MathTransform(org.opengis.referencing.operation.MathTransform) FactoryException(org.opengis.util.FactoryException) TransformException(org.opengis.referencing.operation.TransformException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeographicCRS(org.opengis.referencing.crs.GeographicCRS) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException)

Example 5 with JTSGeometryFactory

use of org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSGeometryFactory in project geotoolkit by Geomatys.

the class JTSUtils method toISO.

/**
 * Creates a 19107 primitive geometry from the given JTS geometry.
 */
public static Geometry toISO(final org.locationtech.jts.geom.Geometry jtsGeom, CoordinateReferenceSystem crs) {
    if (jtsGeom == null) {
        return null;
    }
    if (crs == null) {
        // try to extract the crs from the srid
        final int srid = jtsGeom.getSRID();
        if (srid != 0) {
            final String strCRS = SRIDGenerator.toSRS(srid, SRIDGenerator.Version.V1);
            try {
                crs = CRS.forCode(strCRS);
            } catch (FactoryException ex) {
                Logger.getLogger("org.geotoolkit.geometry.isoonjts").log(Level.SEVERE, null, ex);
            }
        }
    }
    // TODO use factory finder when primitive factory and geometry factory are ready.
    // FactoryFinder.getPrimitiveFactory(hints);
    final PrimitiveFactory pf = new JTSPrimitiveFactory(crs);
    // FactoryFinder.getGeometryFactory(hints);
    final GeometryFactory gf = new JTSGeometryFactory(crs);
    if (jtsGeom instanceof org.locationtech.jts.geom.Point) {
        org.locationtech.jts.geom.Point candidate = (org.locationtech.jts.geom.Point) jtsGeom;
        DirectPosition dp = pointToDirectPosition(candidate, crs);
        return pf.createPoint(dp);
    } else if (jtsGeom instanceof org.locationtech.jts.geom.LineString) {
        org.locationtech.jts.geom.LineString candidate = (org.locationtech.jts.geom.LineString) jtsGeom;
        LineString ls = gf.createLineString(new ArrayList<Position>());
        PointArray pointList = ls.getControlPoints();
        for (int i = 0, n = candidate.getNumPoints(); i < n; i++) {
            pointList.add(coordinateToDirectPosition(candidate.getCoordinateN(i), crs));
        }
        return (JTSLineString) ls;
    } else if (jtsGeom instanceof org.locationtech.jts.geom.LinearRing) {
        return linearRingToRing((org.locationtech.jts.geom.LinearRing) jtsGeom, crs);
    } else if (jtsGeom instanceof org.locationtech.jts.geom.Polygon) {
        org.locationtech.jts.geom.Polygon jtsPolygon = (org.locationtech.jts.geom.Polygon) jtsGeom;
        Ring externalRing = linearRingToRing((org.locationtech.jts.geom.LinearRing) jtsPolygon.getExteriorRing(), crs);
        ArrayList internalRings = new ArrayList();
        for (int i = 0, n = jtsPolygon.getNumInteriorRing(); i < n; i++) {
            internalRings.add(linearRingToRing((org.locationtech.jts.geom.LinearRing) jtsPolygon.getInteriorRingN(i), crs));
        }
        SurfaceBoundary boundary = pf.createSurfaceBoundary(externalRing, internalRings);
        Polygon polygon = gf.createPolygon(boundary);
        return (JTSPolygon) polygon;
    /*ArrayList<Polygon> patches = new ArrayList<Polygon>();
            patches.add(polygon);
            PolyhedralSurface result = gf.createPolyhedralSurface(patches);
            return result;*/
    } else if (jtsGeom instanceof GeometryCollection) {
        org.locationtech.jts.geom.GeometryCollection jtsCollection = (org.locationtech.jts.geom.GeometryCollection) jtsGeom;
        boolean multiPoint = jtsGeom instanceof MultiPoint;
        boolean multiCurve = jtsGeom instanceof MultiLineString;
        boolean multiSurface = jtsGeom instanceof MultiPolygon;
        // determine it by analyzing its content.
        if (!(multiPoint || multiCurve || multiSurface || jtsGeom.isEmpty())) {
            multiPoint = multiCurve = multiSurface = true;
            for (int i = 0, n = jtsCollection.getNumGeometries(); i < n && (multiPoint || multiCurve || multiSurface); i++) {
                if (!(jtsCollection.getGeometryN(i) instanceof org.locationtech.jts.geom.Point)) {
                    multiPoint = false;
                }
                if (!(jtsCollection.getGeometryN(i) instanceof org.locationtech.jts.geom.LineString)) {
                    multiCurve = false;
                }
                if (!(jtsCollection.getGeometryN(i) instanceof org.locationtech.jts.geom.Polygon)) {
                    multiSurface = false;
                }
            }
        }
        AbstractJTSAggregate result;
        if (multiPoint) {
            result = new JTSMultiPoint(crs);
            Set elements = result.getElements();
            for (int i = 0, n = jtsCollection.getNumGeometries(); i < n; i++) {
                // result.getElements().add(jtsToGo1(jtsCollection.getGeometryN(i), crs));
                elements.add(toISO(jtsCollection.getGeometryN(i), crs));
            }
        } else if (multiCurve) {
            result = new JTSMultiCurve(crs);
            Set elements = result.getElements();
            for (int i = 0, n = jtsCollection.getNumGeometries(); i < n; i++) {
                // result.getElements().add(jtsToGo1(jtsCollection.getGeometryN(i), crs));
                Geometry element = toISO(jtsCollection.getGeometryN(i), crs);
                if (element instanceof JTSLineString) {
                    JTSCurve curve = new JTSCurve(crs);
                    curve.getSegments().add((JTSLineString) element);
                    element = curve;
                }
                elements.add(element);
            }
        } else if (multiSurface) {
            result = new JTSMultiSurface(crs);
            Set elements = result.getElements();
            for (int i = 0, n = jtsCollection.getNumGeometries(); i < n; i++) {
                // result.getElements().add(jtsToGo1(jtsCollection.getGeometryN(i), crs));
                elements.add(toISO(jtsCollection.getGeometryN(i), crs));
            }
        } else {
            result = new JTSMultiPrimitive();
            Set elements = result.getElements();
            for (int i = 0, n = jtsCollection.getNumGeometries(); i < n; i++) {
                // result.getElements().add(jtsToGo1(jtsCollection.getGeometryN(i), crs));
                elements.add(toISO(jtsCollection.getGeometryN(i), crs));
            }
        }
        return result;
    } else {
        throw new IllegalArgumentException("Unsupported geometry type: " + jtsGeom.getGeometryType());
    }
}
Also used : MultiPoint(org.locationtech.jts.geom.MultiPoint) JTSMultiPoint(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.aggregate.JTSMultiPoint) GeneralDirectPosition(org.apache.sis.geometry.GeneralDirectPosition) DirectPosition(org.opengis.geometry.DirectPosition) MultiLineString(org.locationtech.jts.geom.MultiLineString) GeometryFactory(org.opengis.geometry.coordinate.GeometryFactory) JTSGeometryFactory(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSGeometryFactory) Set(java.util.Set) FactoryException(org.opengis.util.FactoryException) JTSMultiPoint(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.aggregate.JTSMultiPoint) ArrayList(java.util.ArrayList) JTSLineString(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSLineString) LineString(org.opengis.geometry.coordinate.LineString) MultiLineString(org.locationtech.jts.geom.MultiLineString) JTSMultiCurve(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.aggregate.JTSMultiCurve) JTSCurve(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.primitive.JTSCurve) JTSLineString(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSLineString) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) JTSPolygon(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSPolygon) Polygon(org.opengis.geometry.coordinate.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) AbstractJTSAggregate(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.aggregate.AbstractJTSAggregate) JTSGeometryFactory(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSGeometryFactory) SurfaceBoundary(org.opengis.geometry.primitive.SurfaceBoundary) JTSMultiSurface(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.aggregate.JTSMultiSurface) MultiPoint(org.locationtech.jts.geom.MultiPoint) JTSMultiPoint(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.aggregate.JTSMultiPoint) JTSPrimitiveFactory(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.primitive.JTSPrimitiveFactory) JTSPrimitiveFactory(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.primitive.JTSPrimitiveFactory) PrimitiveFactory(org.opengis.geometry.primitive.PrimitiveFactory) PointArray(org.opengis.geometry.coordinate.PointArray) MultiPoint(org.locationtech.jts.geom.MultiPoint) JTSMultiPoint(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.aggregate.JTSMultiPoint) JTSMultiPrimitive(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.aggregate.JTSMultiPrimitive) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) Geometry(org.opengis.geometry.Geometry) JTSLineString(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSLineString) LineString(org.opengis.geometry.coordinate.LineString) MultiLineString(org.locationtech.jts.geom.MultiLineString) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Ring(org.opengis.geometry.primitive.Ring)

Aggregations

JTSGeometryFactory (org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSGeometryFactory)12 GeometryFactory (org.opengis.geometry.coordinate.GeometryFactory)7 ArrayList (java.util.ArrayList)6 DirectPosition (org.opengis.geometry.DirectPosition)6 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)5 List (java.util.List)4 JTSPrimitiveFactory (org.geotoolkit.geometry.isoonjts.spatialschema.geometry.primitive.JTSPrimitiveFactory)4 PrimitiveFactory (org.opengis.geometry.primitive.PrimitiveFactory)4 Ring (org.opengis.geometry.primitive.Ring)4 SurfaceBoundary (org.opengis.geometry.primitive.SurfaceBoundary)3 JTSMultiCurve (org.geotoolkit.geometry.isoonjts.spatialschema.geometry.aggregate.JTSMultiCurve)2 JTSMultiPoint (org.geotoolkit.geometry.isoonjts.spatialschema.geometry.aggregate.JTSMultiPoint)2 JTSLineString (org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSLineString)2 JTSCurve (org.geotoolkit.geometry.isoonjts.spatialschema.geometry.primitive.JTSCurve)2 MultiLineString (org.locationtech.jts.geom.MultiLineString)2 MultiPoint (org.locationtech.jts.geom.MultiPoint)2 Geometry (org.opengis.geometry.Geometry)2 LineString (org.opengis.geometry.coordinate.LineString)2 Polygon (org.opengis.geometry.coordinate.Polygon)2 Curve (org.opengis.geometry.primitive.Curve)2