Search in sources :

Example 66 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project pigeon by aseldawy.

the class Break method breakGeom.

private void breakGeom(Geometry geom, Vector<Coordinate[]> segments) {
    if (geom == null)
        return;
    if (geom instanceof LineString) {
        LineString linestring = (LineString) geom;
        Coordinate[] coordinates = linestring.getCoordinates();
        for (int i = 1; i < coordinates.length; i++) {
            Coordinate[] segment = new Coordinate[2];
            segment[0] = new Coordinate(coordinates[i - 1]);
            segment[1] = new Coordinate(coordinates[i]);
            segments.add(segment);
        }
    } else if (geom instanceof Polygon) {
        Polygon polygon = (Polygon) geom;
        breakGeom(polygon.getExteriorRing(), segments);
        for (int n = 0; n < polygon.getNumInteriorRing(); n++) {
            breakGeom(polygon.getInteriorRingN(n), segments);
        }
    } else if (geom instanceof GeometryCollection) {
        GeometryCollection geomCollection = (GeometryCollection) geom;
        for (int n = 0; n < geomCollection.getNumGeometries(); n++) {
            breakGeom(geomCollection.getGeometryN(n), segments);
        }
    } else if (geom instanceof Point) {
    // Skip
    } else {
        throw new RuntimeException("Cannot break geometry of type " + geom.getClass());
    }
}
Also used : GeometryCollection(com.vividsolutions.jts.geom.GeometryCollection) LineString(com.vividsolutions.jts.geom.LineString) Coordinate(com.vividsolutions.jts.geom.Coordinate) Point(com.vividsolutions.jts.geom.Point) Polygon(com.vividsolutions.jts.geom.Polygon) Point(com.vividsolutions.jts.geom.Point)

Example 67 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project Osmand by osmandapp.

the class JtsAdapter method flatFeatureList.

/**
 * <p>Recursively convert a {@link Geometry}, which may be an instance of {@link GeometryCollection} with mixed
 * element types, into a flat list containing only the following {@link Geometry} types:</p>
 * <ul>
 *     <li>{@link Point}</li>
 *     <li>{@link LineString}</li>
 *     <li>{@link Polygon}</li>
 *     <li>{@link MultiPoint}</li>
 *     <li>{@link MultiLineString}</li>
 *     <li>{@link MultiPolygon}</li>
 * </ul>
 * <p>WARNING: Any other Geometry types that were not mentioned in the list above will be discarded!</p>
 * <p>Useful for converting a generic geometry into a list of simple MVT-feature-ready geometries.</p>
 *
 * @param geom geometry to flatten
 * @return list of MVT-feature-ready geometries
 */
public static List<Geometry> flatFeatureList(Geometry geom) {
    final List<Geometry> singleGeoms = new ArrayList<>();
    final Stack<Geometry> geomStack = new Stack<>();
    Geometry nextGeom;
    int nextGeomCount;
    geomStack.push(geom);
    while (!geomStack.isEmpty()) {
        nextGeom = geomStack.pop();
        if (nextGeom instanceof Point || nextGeom instanceof MultiPoint || nextGeom instanceof LineString || nextGeom instanceof MultiLineString || nextGeom instanceof Polygon || nextGeom instanceof MultiPolygon) {
            singleGeoms.add(nextGeom);
        } else if (nextGeom instanceof GeometryCollection) {
            // Push all child geometries
            nextGeomCount = nextGeom.getNumGeometries();
            for (int i = 0; i < nextGeomCount; ++i) {
                geomStack.push(nextGeom.getGeometryN(i));
            }
        }
    }
    return singleGeoms;
}
Also used : MultiPoint(com.vividsolutions.jts.geom.MultiPoint) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) ArrayList(java.util.ArrayList) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) Stack(java.util.Stack) Geometry(com.vividsolutions.jts.geom.Geometry) GeometryCollection(com.vividsolutions.jts.geom.GeometryCollection) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon)

Example 68 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project Osmand by osmandapp.

the class JtsAdapter method toFeature.

/**
 * Create and return a feature from a geometry. Returns null on failure.
 *
 * @param geom flat geometry via {@link #flatFeatureList(Geometry)} that can be translated to a feature
 * @param cursor vector tile cursor position
 * @param layerProps layer properties for tagging features
 * @return new tile feature instance, or null on failure
 */
private static VectorTile.Tile.Feature toFeature(Geometry geom, Vec2d cursor, MvtLayerProps layerProps, IUserDataConverter userDataConverter) {
    // Guard: UNKNOWN Geometry
    final VectorTile.Tile.GeomType mvtGeomType = JtsAdapter.toGeomType(geom);
    if (mvtGeomType == VectorTile.Tile.GeomType.UNKNOWN) {
        return null;
    }
    final VectorTile.Tile.Feature.Builder featureBuilder = VectorTile.Tile.Feature.newBuilder();
    final boolean mvtClosePath = MvtUtil.shouldClosePath(mvtGeomType);
    final List<Integer> mvtGeom = new ArrayList<>();
    featureBuilder.setType(mvtGeomType);
    if (geom instanceof Point || geom instanceof MultiPoint) {
        // Encode as MVT point or multipoint
        mvtGeom.addAll(ptsToGeomCmds(geom, cursor));
    } else if (geom instanceof LineString || geom instanceof MultiLineString) {
        // Encode as MVT linestring or multi-linestring
        for (int i = 0; i < geom.getNumGeometries(); ++i) {
            mvtGeom.addAll(linesToGeomCmds(geom.getGeometryN(i), mvtClosePath, cursor, 1));
        }
    } else if (geom instanceof MultiPolygon || geom instanceof Polygon) {
        // Encode as MVT polygon or multi-polygon
        for (int i = 0; i < geom.getNumGeometries(); ++i) {
            final Polygon nextPoly = (Polygon) geom.getGeometryN(i);
            final List<Integer> nextPolyGeom = new ArrayList<>();
            boolean valid = true;
            // Add exterior ring
            final LineString exteriorRing = nextPoly.getExteriorRing();
            // Area must be non-zero
            final double exteriorArea = CGAlgorithms.signedArea(exteriorRing.getCoordinates());
            if (((int) Math.round(exteriorArea)) == 0) {
                continue;
            }
            // Check CCW Winding (must be positive area)
            if (exteriorArea < 0d) {
                CoordinateArrays.reverse(exteriorRing.getCoordinates());
            }
            nextPolyGeom.addAll(linesToGeomCmds(exteriorRing, mvtClosePath, cursor, 2));
            // Add interior rings
            for (int ringIndex = 0; ringIndex < nextPoly.getNumInteriorRing(); ++ringIndex) {
                final LineString nextInteriorRing = nextPoly.getInteriorRingN(ringIndex);
                // Area must be non-zero
                final double interiorArea = CGAlgorithms.signedArea(nextInteriorRing.getCoordinates());
                if (((int) Math.round(interiorArea)) == 0) {
                    continue;
                }
                // Check CW Winding (must be negative area)
                if (interiorArea > 0d) {
                    CoordinateArrays.reverse(nextInteriorRing.getCoordinates());
                }
                // Interior ring area must be < exterior ring area, or entire geometry is invalid
                if (Math.abs(exteriorArea) <= Math.abs(interiorArea)) {
                    valid = false;
                    break;
                }
                nextPolyGeom.addAll(linesToGeomCmds(nextInteriorRing, mvtClosePath, cursor, 2));
            }
            if (valid) {
                mvtGeom.addAll(nextPolyGeom);
            }
        }
    }
    if (mvtGeom.size() < 1) {
        return null;
    }
    featureBuilder.addAllGeometry(mvtGeom);
    // Feature Properties
    userDataConverter.addTags(geom.getUserData(), layerProps, featureBuilder);
    return featureBuilder.build();
}
Also used : MultiPoint(com.vividsolutions.jts.geom.MultiPoint) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) ArrayList(java.util.ArrayList) VectorTile(net.osmand.binary.VectorTile) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) VectorTile(net.osmand.binary.VectorTile) MultiPolygon(com.vividsolutions.jts.geom.MultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon)

Example 69 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project eol-globi-data by jhpoelen.

the class StudyImporterForRaymond method calculateCentroidOfBBox.

protected static LatLng calculateCentroidOfBBox(double left, double top, double right, double bottom) {
    LatLng latLng;
    if (left == right && top == bottom) {
        latLng = new LatLng(top, left);
    } else {
        Coordinate[] points = { GeoUtil.getCoordinate(top, left), GeoUtil.getCoordinate(top, right), GeoUtil.getCoordinate(bottom, right), GeoUtil.getCoordinate(bottom, left), GeoUtil.getCoordinate(top, left) };
        GeometryFactory geometryFactory = new GeometryFactory();
        Polygon polygon = geometryFactory.createPolygon(points);
        Point centroid = polygon.getCentroid();
        latLng = new LatLng(centroid.getCoordinate().y, centroid.getCoordinate().x);
    }
    return latLng;
}
Also used : GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Coordinate(com.vividsolutions.jts.geom.Coordinate) LatLng(org.eol.globi.geo.LatLng) Point(com.vividsolutions.jts.geom.Point) Polygon(com.vividsolutions.jts.geom.Polygon)

Example 70 with Polygon

use of com.vividsolutions.jts.geom.Polygon in project elasticsearch by elastic.

the class ShapeBuilderTests method testNewPolygon.

public void testNewPolygon() {
    Polygon polygon = ShapeBuilders.newPolygon(new CoordinatesBuilder().coordinate(-45, 30).coordinate(45, 30).coordinate(45, -30).coordinate(-45, -30).coordinate(-45, 30)).toPolygon();
    LineString exterior = polygon.getExteriorRing();
    assertEquals(exterior.getCoordinateN(0), new Coordinate(-45, 30));
    assertEquals(exterior.getCoordinateN(1), new Coordinate(45, 30));
    assertEquals(exterior.getCoordinateN(2), new Coordinate(45, -30));
    assertEquals(exterior.getCoordinateN(3), new Coordinate(-45, -30));
}
Also used : CoordinatesBuilder(org.elasticsearch.common.geo.builders.CoordinatesBuilder) LineString(com.vividsolutions.jts.geom.LineString) ElasticsearchGeoAssertions.assertMultiLineString(org.elasticsearch.test.hamcrest.ElasticsearchGeoAssertions.assertMultiLineString) Coordinate(com.vividsolutions.jts.geom.Coordinate) ElasticsearchGeoAssertions.assertPolygon(org.elasticsearch.test.hamcrest.ElasticsearchGeoAssertions.assertPolygon) ElasticsearchGeoAssertions.assertMultiPolygon(org.elasticsearch.test.hamcrest.ElasticsearchGeoAssertions.assertMultiPolygon) Polygon(com.vividsolutions.jts.geom.Polygon)

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