Search in sources :

Example 61 with LinearRing

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

the class WKTDatatypeTest method testReadPolygon2.

/**
 * Test of read method, of class WKTReader.
 */
@Test
public void testReadPolygon2() {
    String wktLiteral = "POLYGON ZM((30 10 0 1, 40 40 0 1, 20 40 0 1, 10 20 0 1, 30 10 0 1), (20 30 0 1, 35 35 0 1, 30 20 0 1, 20 30 0 1))";
    LinearRing shell = GEOMETRY_FACTORY.createLinearRing(new CustomCoordinateSequence(CoordinateSequenceDimensions.XYZM, "30 10 0 1, 40 40 0 1, 20 40 0 1, 10 20 0 1, 30 10 0 1"));
    LinearRing[] holes = new LinearRing[] { GEOMETRY_FACTORY.createLinearRing(new CustomCoordinateSequence(CoordinateSequenceDimensions.XYZM, "20 30 0 1, 35 35 0 1, 30 20 0 1, 20 30 0 1")) };
    Geometry geometry = GEOMETRY_FACTORY.createPolygon(shell, holes);
    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) LinearRing(org.locationtech.jts.geom.LinearRing) CustomCoordinateSequence(org.apache.jena.geosparql.implementation.jts.CustomCoordinateSequence) Test(org.junit.Test)

Example 62 with LinearRing

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

the class GMLWriterTest method testWritePolygon2.

@Test
public void testWritePolygon2() {
    LinearRing shell = GEOMETRY_FACTORY.createLinearRing(new CustomCoordinateSequence(CoordinateSequenceDimensions.XY, "30 10, 40 40, 20 40, 10 20, 30 10"));
    LinearRing[] holes = new LinearRing[] { GEOMETRY_FACTORY.createLinearRing(new CustomCoordinateSequence(CoordinateSequenceDimensions.XY, "20 30, 35 35, 30 20, 20 30")) };
    Geometry geometry = GEOMETRY_FACTORY.createPolygon(shell, holes);
    GeometryWrapper geometryWrapper = new GeometryWrapper(geometry, GML_SRS_NAMESPACE, GMLDatatype.URI, new DimensionInfo(2, 2, 0));
    String result = GMLWriter.write(geometryWrapper);
    String expResult = "<gml:Polygon xmlns:gml=\"http://www.opengis.net/ont/gml\" srsName=\"urn:ogc:def:crs:EPSG::27700\"><gml:exterior><gml:LinearRing><gml:posList>30 10 40 40 20 40 10 20 30 10</gml:posList></gml:LinearRing></gml:exterior><gml:interior><gml:LinearRing><gml:posList>20 30 35 35 30 20 20 30</gml:posList></gml:LinearRing></gml:interior></gml:Polygon>";
    assertEquals(expResult.trim(), result.trim());
}
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) LinearRing(org.locationtech.jts.geom.LinearRing) CustomCoordinateSequence(org.apache.jena.geosparql.implementation.jts.CustomCoordinateSequence) Test(org.junit.Test)

Example 63 with LinearRing

use of org.locationtech.jts.geom.LinearRing 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 64 with LinearRing

use of org.locationtech.jts.geom.LinearRing 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 65 with LinearRing

use of org.locationtech.jts.geom.LinearRing in project ddf by codice.

the class GmdTransformer method setMetacardLocationFromBoundingPolygonPoints.

private void setMetacardLocationFromBoundingPolygonPoints(Metacard metacard) {
    try (InputStream inputStream = getSourceInputStream()) {
        DocumentBuilder builder = XML_UTILS.getSecureDocumentBuilder(false);
        Document document = builder.parse(inputStream);
        XPath xPath = XPathFactory.newInstance().newXPath();
        XPathExpression dataExpression = xPath.compile(GmdConstants.BOUNDING_POLYGON_POINT_PATH);
        NodeList dataList = (NodeList) dataExpression.evaluate(document, XPathConstants.NODESET);
        if (dataList.getLength() > 0) {
            List<Coordinate> coordinates = new ArrayList<>();
            for (int i = 0; i < dataList.getLength(); i++) {
                Node node = dataList.item(i);
                String[] coordinateStrings = node.getTextContent().split(" ");
                if (coordinateStrings.length == 2) {
                    Double lat = Double.parseDouble(coordinateStrings[0]);
                    Double lon = Double.parseDouble(coordinateStrings[1]);
                    Coordinate coordinate = new Coordinate(lat, lon);
                    coordinates.add(coordinate);
                }
            }
            if (CollectionUtils.isNotEmpty(coordinates)) {
                // Close the polygon
                coordinates.add(coordinates.get(0));
                LinearRing linearRing = factory.createLinearRing(coordinates.toArray(new Coordinate[coordinates.size()]));
                String wkt = WKT_WRITER_THREAD_LOCAL.get().write(factory.createPolygon(linearRing, null));
                if (wkt != null) {
                    metacard.setAttribute(new AttributeImpl(Core.LOCATION, wkt));
                }
            }
        }
    } catch (NumberFormatException | ParserConfigurationException | IOException | SAXException | XPathExpressionException e) {
        LOGGER.debug("Unable to parse location in XML document.  Metacard location will not be set.", e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) InputStream(java.io.InputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Coordinate(org.locationtech.jts.geom.Coordinate) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) LinearRing(org.locationtech.jts.geom.LinearRing)

Aggregations

LinearRing (org.locationtech.jts.geom.LinearRing)69 Coordinate (org.locationtech.jts.geom.Coordinate)42 Polygon (org.locationtech.jts.geom.Polygon)34 Test (org.junit.Test)27 MultiPolygon (org.locationtech.jts.geom.MultiPolygon)23 LineString (org.locationtech.jts.geom.LineString)15 CustomCoordinateSequence (org.apache.jena.geosparql.implementation.jts.CustomCoordinateSequence)10 Geometry (org.locationtech.jts.geom.Geometry)10 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)10 Point (org.locationtech.jts.geom.Point)8 MultiPoint (org.locationtech.jts.geom.MultiPoint)7 ArrayList (java.util.ArrayList)6 DimensionInfo (org.apache.jena.geosparql.implementation.DimensionInfo)6 GeometryWrapper (org.apache.jena.geosparql.implementation.GeometryWrapper)6 Test (org.junit.jupiter.api.Test)4 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)3 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)3 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)3 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)3 GeometryNotSupportedException (eu.esdihumboldt.hale.io.gml.geometry.GeometryNotSupportedException)3