Search in sources :

Example 76 with Polygon

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

the class TestWfs10JTStoGML200Converter method testGMLToPolygonType.

@Test
public void testGMLToPolygonType() throws JAXBException, SAXException, IOException, ParseException, NullPointerException {
    Polygon polygon = (Polygon) getGeometryFromWkt(POLYGON);
    assertThat(polygon == null, is(Boolean.FALSE));
    String polygonGML = Wfs10JTStoGML200Converter.convertGeometryToGML(polygon);
    PolygonType polygonType = (PolygonType) Wfs10JTStoGML200Converter.convertGMLToGeometryType(polygonGML, Wfs10Constants.POLYGON);
    assertThat(null != polygonType, is(Boolean.TRUE));
    assertThat(polygonType.isSetInnerBoundaryIs(), is(Boolean.FALSE));
    assertThat(polygonType.isSetOuterBoundaryIs(), is(Boolean.TRUE));
    LinearRingMemberType linearRingMemberType = polygonType.getOuterBoundaryIs();
    JAXBElement<? extends AbstractGeometryType> geometry = linearRingMemberType.getGeometry();
    LinearRingType linearRingType = (LinearRingType) geometry.getValue();
    String coordinates = linearRingType.getCoordinates().getValue().replaceAll("\n", "").trim();
    assertThat(POLYGON_COORDS.equals(coordinates), is(Boolean.TRUE));
}
Also used : LinearRingType(ogc.schema.opengis.gml.v_2_1_2.LinearRingType) LinearRingMemberType(ogc.schema.opengis.gml.v_2_1_2.LinearRingMemberType) PolygonType(ogc.schema.opengis.gml.v_2_1_2.PolygonType) MultiPolygonType(ogc.schema.opengis.gml.v_2_1_2.MultiPolygonType) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon) Test(org.junit.Test)

Example 77 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project GeoGig by boundlessgeo.

the class GeoGigFeatureSourceTest method testGetFeaturesFilter.

@Test
public void testGetFeaturesFilter() throws Exception {
    SimpleFeatureCollection collection;
    Set<List<Object>> actual;
    Set<List<Object>> expected;
    Filter filter;
    filter = ff.id(Collections.singleton(ff.featureId(RepositoryTestCase.idP2)));
    collection = pointsSource.getFeatures(new Query(pointsName, filter));
    actual = Sets.newHashSet();
    for (SimpleFeature f : toList(collection)) {
        actual.add(f.getAttributes());
    }
    expected = Collections.singleton(((SimpleFeature) points2).getAttributes());
    assertEquals(expected, actual);
    ReferencedEnvelope queryBounds = boundsOf(points1, points2);
    Polygon geometry = JTS.toGeometry(queryBounds);
    filter = ff.intersects(ff.property(pointsType.getGeometryDescriptor().getLocalName()), ff.literal(geometry));
    collection = pointsSource.getFeatures(new Query(pointsName, filter));
    actual = Sets.newHashSet();
    for (SimpleFeature f : toList(collection)) {
        actual.add(f.getAttributes());
    }
    expected = ImmutableSet.of(((SimpleFeature) points1).getAttributes(), ((SimpleFeature) points2).getAttributes());
    assertEquals(expected, actual);
    ReferencedEnvelope transformedQueryBounds;
    CoordinateReferenceSystem queryCrs = CRS.decode("EPSG:3857");
    transformedQueryBounds = queryBounds.transform(queryCrs, true);
    geometry = JTS.toGeometry(transformedQueryBounds);
    geometry.setUserData(queryCrs);
    filter = ff.intersects(ff.property(pointsType.getGeometryDescriptor().getLocalName()), ff.literal(geometry));
    collection = pointsSource.getFeatures(new Query(pointsName, filter));
    actual = Sets.newHashSet();
    for (SimpleFeature f : toList(collection)) {
        actual.add(f.getAttributes());
    }
    expected = ImmutableSet.of(((SimpleFeature) points1).getAttributes(), ((SimpleFeature) points2).getAttributes());
    assertEquals(expected.size(), actual.size());
    assertEquals(expected, actual);
    filter = ECQL.toFilter("sp = 'StringProp2_3' OR ip = 2000");
    collection = linesSource.getFeatures(new Query(linesName, filter));
    actual = Sets.newHashSet();
    for (SimpleFeature f : toList(collection)) {
        actual.add(f.getAttributes());
    }
    expected = ImmutableSet.of(((SimpleFeature) lines2).getAttributes(), ((SimpleFeature) lines3).getAttributes());
    assertEquals(expected, actual);
}
Also used : ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) Query(org.geotools.data.Query) Filter(org.opengis.filter.Filter) List(java.util.List) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Polygon(com.vividsolutions.jts.geom.Polygon) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Test(org.junit.Test)

Example 78 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project GeoGig by boundlessgeo.

the class LCSGeometryDiffImpl method geomToStringOfCoordinates.

private String geomToStringOfCoordinates(Optional<Geometry> opt) {
    if (!opt.isPresent()) {
        return "";
    }
    Function<Coordinate, String> printCoords = new Function<Coordinate, String>() {

        @Override
        @Nullable
        public String apply(@Nullable Coordinate coord) {
            return Double.toString(coord.x) + "," + Double.toString(coord.y);
        }
    };
    StringBuilder sb = new StringBuilder();
    Geometry geom = opt.get();
    sb.append(geom.getGeometryType() + " ");
    int n = geom.getNumGeometries();
    for (int i = 0; i < n; i++) {
        Geometry subgeom = geom.getGeometryN(i);
        if (subgeom instanceof Polygon) {
            Polygon polyg = (Polygon) subgeom;
            Coordinate[] coords = polyg.getExteriorRing().getCoordinates();
            Iterator<String> iter = Iterators.transform(Iterators.forArray(coords), printCoords);
            sb.append(Joiner.on(' ').join(iter));
            for (int j = 0; j < polyg.getNumInteriorRing(); j++) {
                coords = polyg.getInteriorRingN(j).getCoordinates();
                iter = Iterators.transform(Iterators.forArray(coords), printCoords);
                sb.append(" " + INNER_RING_SEPARATOR + " ");
                sb.append(Joiner.on(' ').join(iter));
            }
            if (i < n - 1) {
                sb.append(" " + SUBGEOM_SEPARATOR + " ");
            }
        } else {
            Coordinate[] coords = subgeom.getCoordinates();
            Iterator<String> iter = Iterators.transform(Iterators.forArray(coords), printCoords);
            sb.append(Joiner.on(' ').join(iter));
            sb.append(" " + SUBGEOM_SEPARATOR + " ");
        }
    }
    String s = sb.toString().trim();
    return s;
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) Function(com.google.common.base.Function) Coordinate(com.vividsolutions.jts.geom.Coordinate) Polygon(com.vividsolutions.jts.geom.Polygon) Nullable(javax.annotation.Nullable)

Example 79 with Polygon

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

the class GeoJsonReader method mergePolygons.

private void mergePolygons(ArrayList<Polygon> polygons) {
    if (polygons.size() == 1)
        return;
    for (int i1 = 0; i1 < polygons.size() - 1; ++i1) {
        Polygon p1 = polygons.get(i1);
        for (int i2 = i1 + 1; i2 < polygons.size(); ++i2) {
            Polygon p2 = polygons.get(i2);
            // Geometry.union() seems to not have this optimization (bbox check)
            if (!p1.getEnvelopeInternal().intersects(p2.getEnvelopeInternal()))
                continue;
            Geometry p1p2Union = p1.union(p2);
            // if p1 and p2 wouldn't intersect, p1p2Union would be a GeometryCollection or MultiPolygon
            if (p1p2Union instanceof Polygon) {
                polygons.remove(i2);
                polygons.set(i1, (Polygon) p1p2Union);
                // start again at i1
                --i1;
                break;
            }
        }
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) 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 80 with Polygon

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

the class JTSConst method toPolygons.

public static Geometry toPolygons(List<List<LatLon>> outer, List<List<LatLon>> inner) {
    Map<LinearRing, ArrayList<LinearRing>> shellsWithHoles = toShellsWithHoles(outer, inner);
    Polygon[] polys = new Polygon[shellsWithHoles.size()];
    int i = 0;
    for (Map.Entry<LinearRing, ArrayList<LinearRing>> shellWithHoles : shellsWithHoles.entrySet()) {
        LinearRing shell = shellWithHoles.getKey();
        ArrayList<LinearRing> holesList = shellWithHoles.getValue();
        LinearRing[] holes = null;
        if (holesList != null) {
            holes = holesList.toArray(new LinearRing[holesList.size()]);
        }
        polys[i++] = factory.createPolygon(shell, holes);
    }
    if (polys.length == 1)
        return polys[0];
    else
        return factory.createMultiPolygon(polys);
}
Also used : ArrayList(java.util.ArrayList) LinearRing(com.vividsolutions.jts.geom.LinearRing) Polygon(com.vividsolutions.jts.geom.Polygon) HashMap(java.util.HashMap) Map(java.util.Map) Point(com.vividsolutions.jts.geom.Point)

Aggregations

Polygon (com.vividsolutions.jts.geom.Polygon)114 LinearRing (com.vividsolutions.jts.geom.LinearRing)50 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)34 Test (org.junit.Test)32 Coordinate (com.vividsolutions.jts.geom.Coordinate)30 Point (com.vividsolutions.jts.geom.Point)29 PackedCoordinateSequence (com.vividsolutions.jts.geom.impl.PackedCoordinateSequence)27 ArrayList (java.util.ArrayList)26 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 Geometry (com.vividsolutions.jts.geom.Geometry)22 LineString (com.vividsolutions.jts.geom.LineString)21 List (java.util.List)20 PersistenceManager (javax.jdo.PersistenceManager)15