private static Geometry<C2D> toPolygon(Envelope env) { final PositionSequence<C2D> ps = PositionSequenceBuilders.fixedSized( 4, C2D.class ) .add( env.lowerLeft().getCoordinate( 0 ), env.lowerLeft().getCoordinate( 1 ) ) .add( env.lowerLeft().getCoordinate( 0 ), env.upperRight().getCoordinate( 1 ) ) .add( env.upperRight().getCoordinate( 0 ), env.upperRight().getCoordinate( 1 ) ) .add( env.lowerLeft().getCoordinate( 0 ), env.lowerLeft().getCoordinate( 1 ) ) .toPositionSequence(); return new Polygon<C2D>( ps, CoordinateReferenceSystems.PROJECTED_2D_METER ); }
private static JGeometry convert(Polygon geometry) { int srid = geometry.getSRID(); int dim = geometry.getCoordinateDimension(); double[] points = getPoints(geometry.getPoints()); int[] elemInfo = new int[3 + geometry.getNumInteriorRing() * 3]; int offset = 0; int pointOffset = 1; elemInfo[offset++] = pointOffset; elemInfo[offset++] = 1003; // exterior elemInfo[offset++] = 1; pointOffset += geometry.getExteriorRing().getNumPoints() * dim; for (int i = 0; i < geometry.getNumInteriorRing(); i++) { elemInfo[offset++] = pointOffset; elemInfo[offset++] = 2003; elemInfo[offset++] = 1; pointOffset += geometry.getInteriorRingN(i).getNumPoints() * dim; } int gtype = dim * 1000 + (geometry.isMeasured() ? dim : 0) * 100 + GTYPE_POLYGON; return new JGeometry(gtype, srid, elemInfo, points); }
if (cameraPosition.zoom > 12) { // Your required zoom level to show polygons LatLngBounds latLngBounds = googleMap.getProjection().getVisibleRegion().latLngBounds; for (Polygon polygon : polygons) { for (LatLng latLng : polygon.getPoints()) { if (latLngBounds.contains(latLng)) { polygon.setVisible(true); break; //Needed if only one polygon can be visible at a time, remove if several can be visible at a time } } } }
private <P extends Position> boolean equals (Polygon<P> first, Polygon<P> second){ if (first.getNumInteriorRing() != second.getNumInteriorRing()) return false; if (!equals(first.getExteriorRing(), second.getExteriorRing())) return false; for (int i = 0; i < first.getNumInteriorRing(); i++) { if (!equals(first.getInteriorRingN(i), second.getInteriorRingN(i))) return false; } return true; }
private <P extends Position> int getPolygonSize(Polygon<P> geom) { //to hold the number of linear rings int size = ByteBuffer.UINT_SIZE; //for each linear ring, a UINT holds the number of points size += geom.isEmpty() ? 0 : ByteBuffer.UINT_SIZE * (geom.getNumInteriorRing() + 1); size += getPointByteSize(geom) * geom.getNumPositions(); return size; }
private static org.postgis.Polygon convert(Polygon polygon) { int numRings = polygon.getNumInteriorRing(); org.postgis.LinearRing[] rings = new org.postgis.LinearRing[numRings + 1]; rings[0] = convert(polygon.getExteriorRing()); for (int i = 0; i < numRings; i++) { rings[i+1] = convert(polygon.getInteriorRingN(i)); } org.postgis.Polygon pgPolygon = new org.postgis.Polygon(rings); pgPolygon.setSrid(polygon.getSRID()); return pgPolygon; }
private Polygon decodePolygon(int shapeIdx) { Shape shape = shapes[shapeIdx]; int figureOffset = shape.figureOffset; if (figureOffset <= -1) { return Polygon.createEmpty(); } int figureStopIdx = figures.length - 1; if (shapeIdx < (shapes.length - 1)) { figureStopIdx = shapes[shapeIdx + 1].figureOffset - 1; } List<LinearRing> linearRings = Lists.newArrayList(); for (int i = figureOffset; i <= figureStopIdx; i++) { linearRings.add(new LinearRing(createPoints(i))); } return new Polygon(linearRings.toArray(new LinearRing[0])); }
private void addInteriorRings(Polygon<?> geom, CountingPositionSequenceBuilder<?> coordinates, List<Figure> figures) { for ( int idx = 0; idx < geom.getNumInteriorRing(); idx++ ) { addInteriorRing( geom.getInteriorRingN( idx ), coordinates, figures ); } }
@Test public void polygon() { Polygon polygon = (org.geolatte.geom.Polygon) Wkt.fromWkt("POLYGON (" + "(30 10, 40 40, 20 40, 10 20, 30 10), " + "(20 30, 35 35, 30 20, 20 30))"); JGeometry geo = JGeometryConverter.convert(polygon); double[] extRing = new double[]{30, 10, 40, 40, 20, 40, 10, 20, 30, 10}; double[] intRing = new double[]{20, 30, 35, 35, 30, 20, 20, 30}; JGeometry geo2 = JGeometry.createLinearPolygon(new Object[]{extRing, intRing}, polygon.getCoordinateDimension(), polygon.getSRID()); assertEquals(geo2, geo); }
protected void writeNumRings(Polygon<P> geom, ByteBuffer byteBuffer) { byteBuffer.putUInt(geom.isEmpty() ? 0 : geom.getNumInteriorRing() + 1); }
private void addExteriorRing(Polygon geom, CountingPositionSequenceBuilder coordinates, List<Figure> figures) { LineString shell = geom.getExteriorRing(); int offset = coordinates.getNumAdded(); addPoints( shell, coordinates ); Figure exterior = new Figure( FigureAttribute.ExteriorRing, offset ); figures.add( exterior ); }
private <P extends Position> void addLinearRings(Polygon<P> geometry) { addRing(geometry.getExteriorRing()); for (int i = 0; i < geometry.getNumInteriorRing(); i++) { addDelimiter(); addRing(geometry.getInteriorRingN(i)); } }
private static org.postgis.Polygon convert(Polygon polygon) { int numRings = polygon.getNumInteriorRing(); org.postgis.LinearRing[] rings = new org.postgis.LinearRing[numRings + 1]; rings[0] = convert(polygon.getExteriorRing()); for (int i = 0; i < numRings; i++) { rings[i + 1] = convert(polygon.getInteriorRingN(i)); } org.postgis.Polygon pgPolygon = new org.postgis.Polygon(rings); pgPolygon.setSrid(polygon.getSRID()); return pgPolygon; }
private String convertPolygonToJSON(Polygon p) { List<LatLng> points=p.getPoints(); StringBuilder sb = new StringBuilder("[1,["+points.get(0)); for (int i = 1; i < points.size(); i++) { sb.append(+ ","+points.get(i)); } sb.append("],{\"strokeColor\":\"#FF0000\",\"fillColor\":\"#00FFFF\"}]"); return sb.toString(); }
private Polygon decodePolygon(int shapeIdx) { Shape shape = shapes[shapeIdx]; int figureOffset = shape.figureOffset; if (figureOffset <= -1) { return Polygon.createEmpty(); } int figureStopIdx = figures.length - 1; if (shapeIdx < (shapes.length - 1)) { figureStopIdx = shapes[shapeIdx + 1].figureOffset - 1; } List<LinearRing> linearRings = Lists.newArrayList(); for (int i = figureOffset; i <= figureStopIdx; i++) { linearRings.add(new LinearRing(createPoints(i))); } return new Polygon(linearRings.toArray(new LinearRing[0])); }
private static JGeometry convert(Polygon geometry) { int srid = geometry.getSRID(); int dim = geometry.getCoordinateDimension(); double[] points = getPoints(geometry.getPoints()); int[] elemInfo = new int[3 + geometry.getNumInteriorRing() * 3]; int offset = 0; int pointOffset = 1; elemInfo[offset++] = pointOffset; elemInfo[offset++] = 1003; // exterior elemInfo[offset++] = 1; pointOffset += geometry.getExteriorRing().getNumPoints() * dim; for (int i = 0; i < geometry.getNumInteriorRing(); i++) { elemInfo[offset++] = pointOffset; elemInfo[offset++] = 2003; elemInfo[offset++] = 1; pointOffset += geometry.getInteriorRingN(i).getNumPoints() * dim; } int gtype = dim * 1000 + (geometry.isMeasured() ? dim : 0) * 100 + GTYPE_POLYGON; return new JGeometry(gtype, srid, elemInfo, points); }
private static <P extends Position> Polygon to(org.geolatte.geom.Polygon<P> polygon, GeometryFactory gFact) { LinearRing shell = to(polygon.getExteriorRing(), gFact); LinearRing[] holes = new LinearRing[polygon.getNumInteriorRing()]; for (int i = 0; i < holes.length; i++) { holes[i] = to(polygon.getInteriorRingN(i), gFact); } return gFact.createPolygon(shell, holes); }
/** * Creates an empty {@code Polygon} for a coordinate reference system * * @param crs the coordinate reference system for the created {@code Polygon} * @param <P> the type of {@code Position} * @return an empty {@code Polygon} with the specified coordinate reference system */ public static <P extends Position> Polygon<P> mkEmptyPolygon(CoordinateReferenceSystem<P> crs) { return new Polygon<P>(crs); }
public boolean totalIntersects(Polygon poly, Shape testShape) { List<Point> points = flatDoublesToPoints(poly.getPoints()); boolean inside = true; // If this is false after testing all points, the poly has at least one point outside of the shape. for(Point point : points) { if(!testShape.intersects(point.x, point.y, 1, 1)) { // The 3rd and 4th parameters here are "width" and "height". 1 for a point. inside = false; } } return inside; }