Search in sources :

Example 41 with Polygon

use of org.locationtech.jts.geom.Polygon in project jena by apache.

the class WKTReader method buildMultiPolygon.

private Geometry buildMultiPolygon(String coordinates) {
    if (coordinates.isEmpty()) {
        return GEOMETRY_FACTORY.createMultiPolygon(new Polygon[0]);
    }
    String trimmed = coordinates.replace(")) ,", ")),");
    String[] multiCoordinates = trimmed.split("\\)\\),");
    Polygon[] polygons = new Polygon[multiCoordinates.length];
    for (int i = 0; i < multiCoordinates.length; i++) {
        polygons[i] = buildPolygon(multiCoordinates[i]);
    }
    return GEOMETRY_FACTORY.createMultiPolygon(polygons);
}
Also used : LineString(org.locationtech.jts.geom.LineString) Polygon(org.locationtech.jts.geom.Polygon)

Example 42 with Polygon

use of org.locationtech.jts.geom.Polygon in project jena by apache.

the class GeometryReverse method reversePolygon.

private static Polygon reversePolygon(Geometry geometry, GeometryFactory factory) {
    Polygon finalGeometry;
    Polygon polygon = (Polygon) geometry;
    if (polygon.getNumInteriorRing() == 0) {
        // There are no interior rings so perform the standard reversal.
        Coordinate[] coordinates = getReversedCoordinates(geometry);
        finalGeometry = factory.createPolygon(coordinates);
    } else {
        LineString exteriorRing = polygon.getExteriorRing();
        Coordinate[] reversedExteriorCoordinates = getReversedCoordinates(exteriorRing);
        LinearRing reversedExteriorRing = factory.createLinearRing(reversedExteriorCoordinates);
        LinearRing[] reversedInteriorRings = new LinearRing[polygon.getNumInteriorRing()];
        for (int i = 0; i < polygon.getNumInteriorRing(); i++) {
            LineString interiorRing = polygon.getInteriorRingN(i);
            Coordinate[] reversedInteriorCoordinates = getReversedCoordinates(interiorRing);
            LinearRing reversedInteriorRing = factory.createLinearRing(reversedInteriorCoordinates);
            reversedInteriorRings[i] = reversedInteriorRing;
        }
        finalGeometry = factory.createPolygon(reversedExteriorRing, reversedInteriorRings);
    }
    return finalGeometry;
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) LineString(org.locationtech.jts.geom.LineString) Polygon(org.locationtech.jts.geom.Polygon) LinearRing(org.locationtech.jts.geom.LinearRing)

Example 43 with Polygon

use of org.locationtech.jts.geom.Polygon in project jena by apache.

the class GeometryReverse method unpackPolygons.

private static Polygon[] unpackPolygons(GeometryCollection geoCollection) {
    GeometryFactory factory = geoCollection.getFactory();
    int count = geoCollection.getNumGeometries();
    Polygon[] polygons = new Polygon[count];
    for (int i = 0; i < count; i++) {
        Geometry geometry = geoCollection.getGeometryN(i);
        Polygon polygon = reversePolygon(geometry, factory);
        polygons[i] = polygon;
    }
    return polygons;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Polygon(org.locationtech.jts.geom.Polygon)

Example 44 with Polygon

use of org.locationtech.jts.geom.Polygon in project jena by apache.

the class GeometryTransformation method transformPolygon.

private static Polygon transformPolygon(Geometry sourceGeometry, MathTransform transform) throws TransformException {
    GeometryFactory geometryFactory = sourceGeometry.getFactory();
    Polygon polygon = (Polygon) sourceGeometry;
    LinearRing exterior = transformLinearRing(polygon.getExteriorRing(), transform);
    int interiorsNumber = polygon.getNumInteriorRing();
    ArrayList<LinearRing> interiors = new ArrayList<>(interiorsNumber);
    for (int i = 0; i < polygon.getNumInteriorRing(); i++) {
        LinearRing interior = transformLinearRing(polygon.getInteriorRingN(i), transform);
        interiors.add(interior);
    }
    return geometryFactory.createPolygon(exterior, interiors.toArray(new LinearRing[interiors.size()]));
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) ArrayList(java.util.ArrayList) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) LinearRing(org.locationtech.jts.geom.LinearRing) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint)

Example 45 with Polygon

use of org.locationtech.jts.geom.Polygon in project jena by apache.

the class GMLReaderTest method testExtractMultiSurface.

/**
 * Test of extract method, of class GMLReader.
 *
 * @throws org.jdom2.JDOMException
 * @throws java.io.IOException
 */
@Test
public void testExtractMultiSurface() throws JDOMException, IOException {
    Polygon[] polygons = new Polygon[2];
    polygons[0] = GEOMETRY_FACTORY.createPolygon(new CustomCoordinateSequence(CoordinateSequenceDimensions.XY, "40 40, 20 45, 45 30, 40 40"));
    polygons[1] = GEOMETRY_FACTORY.createPolygon(new CustomCoordinateSequence(CoordinateSequenceDimensions.XY, "20 35, 10 30, 10 10, 30 5, 45 20, 20 35"));
    Geometry geometry = GEOMETRY_FACTORY.createMultiPolygon(polygons);
    GMLReader expResult = new GMLReader(geometry, 2, SRS_URI.OSGB36_CRS);
    String gmlText = "<gml:MultiSurface xmlns:gml=\"http://www.opengis.net/ont/gml\" srsName=\"http://www.opengis.net/def/crs/EPSG/0/27700\"><gml:surfaceMember><gml:Polygon srsName=\"http://www.opengis.net/def/crs/EPSG/0/27700\"><gml:exterior><gml:LinearRing><gml:posList>40 40 20 45 45 30 40 40</gml:posList></gml:LinearRing></gml:exterior></gml:Polygon></gml:surfaceMember><gml:surfaceMember><gml:Polygon srsName=\"http://www.opengis.net/def/crs/EPSG/0/27700\"><gml:exterior><gml:LinearRing><gml:posList>20 35 10 30 10 10 30 5 45 20 20 35</gml:posList></gml:LinearRing></gml:exterior></gml:Polygon></gml:surfaceMember></gml:MultiSurface>";
    GMLReader result = GMLReader.extract(gmlText);
    // 
    // 
    assertEquals(expResult, result);
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) LineString(org.locationtech.jts.geom.LineString) Polygon(org.locationtech.jts.geom.Polygon) CustomCoordinateSequence(org.apache.jena.geosparql.implementation.jts.CustomCoordinateSequence) Test(org.junit.Test)

Aggregations

Polygon (org.locationtech.jts.geom.Polygon)56 LineString (org.locationtech.jts.geom.LineString)23 MultiPolygon (org.locationtech.jts.geom.MultiPolygon)22 Point (org.locationtech.jts.geom.Point)22 Geometry (org.locationtech.jts.geom.Geometry)20 Coordinate (org.locationtech.jts.geom.Coordinate)16 MultiPoint (org.locationtech.jts.geom.MultiPoint)15 Test (org.junit.Test)13 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)11 LinearRing (org.locationtech.jts.geom.LinearRing)10 CustomCoordinateSequence (org.apache.jena.geosparql.implementation.jts.CustomCoordinateSequence)8 MultiLineString (org.locationtech.jts.geom.MultiLineString)8 ArrayList (java.util.ArrayList)7 Test (org.junit.jupiter.api.Test)6 DimensionInfo (org.apache.jena.geosparql.implementation.DimensionInfo)5 GeometryWrapper (org.apache.jena.geosparql.implementation.GeometryWrapper)5 WKTReader (org.locationtech.jts.io.WKTReader)5 AreaIndex (com.graphhopper.routing.util.AreaIndex)4 CustomArea (com.graphhopper.routing.util.CustomArea)3 GeometryCollection (org.locationtech.jts.geom.GeometryCollection)3