Search in sources :

Example 16 with LinearRing

use of com.vividsolutions.jts.geom.LinearRing in project ddf by codice.

the class GmdTransformer method setLocationFromPolygon.

private void setLocationFromPolygon(String boundingPolygon, MetacardImpl metacard) {
    try {
        String[] stringArray = boundingPolygon.split(" ");
        List<Coordinate> coordinates = new ArrayList<>();
        for (int i = 0; i < stringArray.length - 1; i += 2) {
            coordinates.add(new Coordinate(Double.parseDouble(stringArray[i]), Double.parseDouble(stringArray[i + 1])));
        }
        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(Core.LOCATION, wkt);
        }
    } catch (NumberFormatException nfe) {
        LOGGER.debug("Unable to parse double in {}. Metacard location will not be set.", boundingPolygon);
    }
}
Also used : Coordinate(com.vividsolutions.jts.geom.Coordinate) ArrayList(java.util.ArrayList) LinearRing(com.vividsolutions.jts.geom.LinearRing)

Example 17 with LinearRing

use of com.vividsolutions.jts.geom.LinearRing in project ddf by codice.

the class Wfs20JTStoGML321Converter method convertToPolygonType.

public static PolygonType convertToPolygonType(Polygon polygon, String srsName) {
    PolygonType polygonType = GML320_OBJECT_FACTORY.createPolygonType();
    //exterior
    LineString lineString = polygon.getExteriorRing();
    LinearRing linearRing = lineString.getFactory().createLinearRing(lineString.getCoordinateSequence());
    RingType ringType = convertToRingType(linearRing, srsName);
    JAXBElement<RingType> ringTypeJAXBElement = GML320_OBJECT_FACTORY.createRing(ringType);
    AbstractRingPropertyType abstractRingPropertyType = GML320_OBJECT_FACTORY.createAbstractRingPropertyType();
    abstractRingPropertyType.setAbstractRing(ringTypeJAXBElement);
    polygonType.setExterior(abstractRingPropertyType);
    //interiors
    for (int i = 0; i < polygon.getNumInteriorRing(); i++) {
        LineString interiorRingN = polygon.getInteriorRingN(i);
        LinearRing linearRing1 = interiorRingN.getFactory().createLinearRing(interiorRingN.getCoordinateSequence());
        RingType ringType1 = convertToRingType(linearRing1, srsName);
        JAXBElement<RingType> ringTypeJAXBElement1 = GML320_OBJECT_FACTORY.createRing(ringType1);
        AbstractRingPropertyType abstractRingPropertyType1 = GML320_OBJECT_FACTORY.createAbstractRingPropertyType();
        abstractRingPropertyType1.setAbstractRing(ringTypeJAXBElement1);
        polygonType.getInterior().add(abstractRingPropertyType1);
    }
    polygonType.setSrsName(srsName);
    return polygonType;
}
Also used : LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) AbstractRingPropertyType(net.opengis.gml.v_3_2_1.AbstractRingPropertyType) PolygonType(net.opengis.gml.v_3_2_1.PolygonType) LinearRing(com.vividsolutions.jts.geom.LinearRing) RingType(net.opengis.gml.v_3_2_1.RingType) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint)

Example 18 with LinearRing

use of com.vividsolutions.jts.geom.LinearRing in project StreetComplete by westnordost.

the class JTSConst method toShellsWithHoles.

private static Map<LinearRing, ArrayList<LinearRing>> toShellsWithHoles(List<List<LatLon>> outerPolygons, List<List<LatLon>> innerPolygons) {
    // outer -> List of inner
    Map<LinearRing, ArrayList<LinearRing>> shellsWithHoles = new HashMap<>();
    for (List<LatLon> outer : outerPolygons) {
        LinearRing shell = factory.createLinearRing(toCoordinates(outer));
        Geometry outerGeom = factory.createPolygon(shell);
        shellsWithHoles.put(shell, null);
        Iterator<List<LatLon>> it = innerPolygons.iterator();
        while (it.hasNext()) {
            List<LatLon> inner = it.next();
            LinearRing hole = factory.createLinearRing(toCoordinates(inner));
            Geometry holeGeom = factory.createPolygon(hole);
            if (outerGeom.contains(holeGeom)) {
                if (shellsWithHoles.get(shell) == null) {
                    shellsWithHoles.put(shell, new ArrayList<>());
                }
                shellsWithHoles.get(shell).add(hole);
                it.remove();
            }
        }
    }
    return shellsWithHoles;
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) ElementGeometry(de.westnordost.streetcomplete.data.osm.ElementGeometry) LatLon(de.westnordost.osmapi.map.data.LatLon) OsmLatLon(de.westnordost.osmapi.map.data.OsmLatLon) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LinearRing(com.vividsolutions.jts.geom.LinearRing)

Example 19 with LinearRing

use of com.vividsolutions.jts.geom.LinearRing in project StreetComplete by westnordost.

the class GeoJsonReader method createPolygon.

private Polygon createPolygon(JSONArray coords) throws JSONException {
    LinearRing[] linearRings = new LinearRing[coords.length()];
    for (int i = 0; i < coords.length(); i++) {
        linearRings[i] = createLinearRing(coords.getJSONArray(i));
    }
    LinearRing shell = linearRings[0];
    ArrayList<LinearRing> inner = new ArrayList<>();
    if (linearRings.length > 1) {
        LinearRing[] innerArray = new LinearRing[linearRings.length - 1];
        System.arraycopy(linearRings, 1, innerArray, 0, linearRings.length - 1);
        inner.addAll(Arrays.asList(innerArray));
        /* JTS imposes a restriction on a polygon that GeoJSON does not: that the linear rings
			   that define the holes may not touch each other in more than one point. So, we need to
			   merge inner linear rings that touch each other in a line together */
        mergeHoles(inner);
    }
    Polygon polygon = factory.createPolygon(shell, inner.toArray(new LinearRing[] {}));
    /* in JTS, outer shells are clockwise but in GeoJSON it is specified to be the other way
		   round. This reader is forgiving: It does not care about the direction, it will just
		   reorder if necessary (part of normalize) */
    polygon.normalize();
    return polygon;
}
Also used : ArrayList(java.util.ArrayList) LinearRing(com.vividsolutions.jts.geom.LinearRing) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint)

Example 20 with LinearRing

use of com.vividsolutions.jts.geom.LinearRing in project StreetComplete by westnordost.

the class GeoJsonReader method mergeHoles.

private void mergeHoles(ArrayList<LinearRing> rings) {
    if (rings.size() == 1)
        return;
    // need to be converted to polygons and back because linearring is a lineal data structure,
    // we want to merge by area
    ArrayList<Polygon> polygons = new ArrayList<>(rings.size());
    for (LinearRing ring : rings) {
        polygons.add(factory.createPolygon(ring));
    }
    mergePolygons(polygons);
    // something was merged. Convert polygons back to rings
    if (polygons.size() != rings.size()) {
        rings.clear();
        for (Polygon polygon : polygons) {
            rings.add((LinearRing) polygon.getExteriorRing());
        }
    }
}
Also used : ArrayList(java.util.ArrayList) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon) LinearRing(com.vividsolutions.jts.geom.LinearRing)

Aggregations

LinearRing (com.vividsolutions.jts.geom.LinearRing)101 Polygon (com.vividsolutions.jts.geom.Polygon)72 Coordinate (com.vividsolutions.jts.geom.Coordinate)52 Test (org.junit.Test)42 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)28 PackedCoordinateSequence (com.vividsolutions.jts.geom.impl.PackedCoordinateSequence)27 RdfToRyaConversions.convertStatement (org.apache.rya.api.resolver.RdfToRyaConversions.convertStatement)24 Resource (org.openrdf.model.Resource)24 Statement (org.openrdf.model.Statement)24 URI (org.openrdf.model.URI)24 Value (org.openrdf.model.Value)24 ValueFactory (org.openrdf.model.ValueFactory)24 ContextStatementImpl (org.openrdf.model.impl.ContextStatementImpl)24 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)24 ArrayList (java.util.ArrayList)22 StatementConstraints (org.apache.rya.indexing.StatementConstraints)12 LineString (com.vividsolutions.jts.geom.LineString)10 Point (com.vividsolutions.jts.geom.Point)10 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)9 Geometry (com.vividsolutions.jts.geom.Geometry)8