Search in sources :

Example 16 with Polygon

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

the class WKTDatatypeTest method testReadMultiPolygon.

/**
 * Test of read method, of class WKTReader.
 */
@Test
public void testReadMultiPolygon() {
    String wktLiteral = "MULTIPOLYGON ZM(((40 40 0 1, 20 45 0 1, 45 30 0 1, 40 40 0 1)), ((20 35 0 1, 10 30 0 1, 10 10 0 1, 30 5 0 1, 45 20 0 1, 20 35 0 1), (30 20 0 1, 20 15 0 1, 20 25 0 1, 30 20 0 1)))";
    Polygon[] polygons = new Polygon[2];
    polygons[0] = GEOMETRY_FACTORY.createPolygon(new CustomCoordinateSequence(CoordinateSequenceDimensions.XYZM, "40 40 0 1, 20 45 0 1, 45 30 0 1, 40 40 0 1"));
    LinearRing shell = GEOMETRY_FACTORY.createLinearRing(new CustomCoordinateSequence(CoordinateSequenceDimensions.XYZM, "20 35 0 1, 10 30 0 1, 10 10 0 1, 30 5 0 1, 45 20 0 1, 20 35 0 1"));
    LinearRing[] holes = new LinearRing[] { GEOMETRY_FACTORY.createLinearRing(new CustomCoordinateSequence(CoordinateSequenceDimensions.XYZM, "30 20 0 1, 20 15 0 1, 20 25 0 1, 30 20 0 1")) };
    polygons[1] = GEOMETRY_FACTORY.createPolygon(shell, holes);
    Geometry geometry = GEOMETRY_FACTORY.createMultiPolygon(polygons);
    GeometryWrapper expResult = new GeometryWrapper(geometry, SRS_URI.DEFAULT_WKT_CRS84, WKTDatatype.URI, new DimensionInfo(4, 3, 2));
    GeometryWrapper result = WKT_DATATYPE.read(wktLiteral);
    // 
    // 
    assertEquals(expResult, result);
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) GeometryWrapper(org.apache.jena.geosparql.implementation.GeometryWrapper) DimensionInfo(org.apache.jena.geosparql.implementation.DimensionInfo) LineString(org.locationtech.jts.geom.LineString) Polygon(org.locationtech.jts.geom.Polygon) CustomCoordinateSequence(org.apache.jena.geosparql.implementation.jts.CustomCoordinateSequence) LinearRing(org.locationtech.jts.geom.LinearRing) Test(org.junit.Test)

Example 17 with Polygon

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

the class GeometryReverseTest method testCheckPolygon.

/**
 * Test of check method, of class GeometryReverse.
 *
 * @throws org.opengis.util.FactoryException
 */
@Test
public void testCheckPolygon() throws FactoryException {
    WKTReader reader = new WKTReader();
    try {
        Polygon geometry = (Polygon) reader.read("POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))");
        String srsURI = "http://www.opengis.net/def/crs/EPSG/0/4326";
        Geometry expResult = reader.read("POLYGON ((10 30, 40 40, 40 20, 20 10, 10 30))");
        Geometry result = GeometryReverse.check(geometry, srsURI);
        // 
        // 
        assertEquals(expResult, result);
    } catch (ParseException ex) {
        System.err.println("ParseException: " + ex.getMessage());
    }
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) ParseException(org.locationtech.jts.io.ParseException) WKTReader(org.locationtech.jts.io.WKTReader) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Test(org.junit.Test)

Example 18 with Polygon

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

the class GeometryReverse method reverseGeometry.

/**
 * Reverses coordinate order of the supplied geometry and produces a new
 * geometry.
 *
 * @param geometry
 * @return Geometry in x,y coordinate order.
 */
public static Geometry reverseGeometry(Geometry geometry) {
    if (geometry.isEmpty()) {
        return geometry.copy();
    }
    GeometryFactory factory = geometry.getFactory();
    Geometry finalGeometry;
    Coordinate[] coordinates;
    String type = geometry.getGeometryType();
    switch(type) {
        case "LineString":
            coordinates = getReversedCoordinates(geometry);
            finalGeometry = factory.createLineString(coordinates);
            break;
        case "LinearRing":
            coordinates = getReversedCoordinates(geometry);
            finalGeometry = factory.createLinearRing(coordinates);
            break;
        case "MultiPoint":
            coordinates = getReversedCoordinates(geometry);
            finalGeometry = factory.createMultiPointFromCoords(coordinates);
            break;
        case "Polygon":
            finalGeometry = reversePolygon(geometry, factory);
            break;
        case "Point":
            coordinates = getReversedCoordinates(geometry);
            finalGeometry = factory.createPoint(coordinates[0]);
            break;
        case "MultiPolygon":
            Polygon[] polygons = unpackPolygons((GeometryCollection) geometry);
            finalGeometry = factory.createMultiPolygon(polygons);
            break;
        case "MultiLineString":
            LineString[] lineString = unpackLineStrings((GeometryCollection) geometry);
            finalGeometry = factory.createMultiLineString(lineString);
            break;
        case "GeometryCollection":
            Geometry[] geometries = unpackGeometryCollection((GeometryCollection) geometry);
            finalGeometry = factory.createGeometryCollection(geometries);
            break;
        default:
            finalGeometry = geometry;
            break;
    }
    return finalGeometry;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Coordinate(org.locationtech.jts.geom.Coordinate) LineString(org.locationtech.jts.geom.LineString) LineString(org.locationtech.jts.geom.LineString) Polygon(org.locationtech.jts.geom.Polygon)

Example 19 with Polygon

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

the class GeometryTransformation method transformMultiPolygon.

private static MultiPolygon transformMultiPolygon(Geometry sourceGeometry, MathTransform transform) throws TransformException {
    GeometryFactory geometryFactory = sourceGeometry.getFactory();
    MultiPolygon multiPolygon = (MultiPolygon) sourceGeometry;
    int geometryNumber = multiPolygon.getNumGeometries();
    ArrayList<Polygon> polygons = new ArrayList<>();
    for (int i = 0; i < geometryNumber; i++) {
        Polygon polygon = transformPolygon(multiPolygon.getGeometryN(i), transform);
        polygons.add(polygon);
    }
    return geometryFactory.createMultiPolygon(polygons.toArray(new Polygon[polygons.size()]));
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) ArrayList(java.util.ArrayList) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint)

Example 20 with Polygon

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

the class WKTReader method buildPolygon.

private Polygon buildPolygon(String coordinates) {
    Polygon polygon;
    String[] splitCoordinates = splitCoordinates(coordinates);
    if (splitCoordinates.length == 1) {
        // Polygon without holes.
        CustomCoordinateSequence shellSequence = new CustomCoordinateSequence(dims, clean(coordinates));
        polygon = GEOMETRY_FACTORY.createPolygon(shellSequence);
    } else {
        // Polygon with holes
        String shellCoordinates = splitCoordinates[0];
        CustomCoordinateSequence shellSequence = new CustomCoordinateSequence(dims, clean(shellCoordinates));
        LinearRing shellLinearRing = GEOMETRY_FACTORY.createLinearRing(shellSequence);
        String[] splitHoleCoordinates = Arrays.copyOfRange(splitCoordinates, 1, splitCoordinates.length);
        LinearRing[] holesLinearRing = splitLinearRings(dims, splitHoleCoordinates);
        polygon = GEOMETRY_FACTORY.createPolygon(shellLinearRing, holesLinearRing);
    }
    return polygon;
}
Also used : LineString(org.locationtech.jts.geom.LineString) Polygon(org.locationtech.jts.geom.Polygon) CustomCoordinateSequence(org.apache.jena.geosparql.implementation.jts.CustomCoordinateSequence) LinearRing(org.locationtech.jts.geom.LinearRing)

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