Search in sources :

Example 26 with Polygon

use of com.revolsys.geometry.model.Polygon in project com.revolsys.open by revolsys.

the class ShapefileGeometryHandler method newPolygonGeometryFromParts.

public Geometry newPolygonGeometryFromParts(final GeometryFactory geometryFactory, final List<double[]> parts, final int axisCount) {
    final List<Polygon> polygons = new ArrayList<>();
    final List<LinearRing> currentParts = new ArrayList<>();
    for (final double[] coordinates : parts) {
        final LinearRing ring = geometryFactory.linearRing(axisCount, coordinates);
        final boolean ringClockwise = ring.isClockwise();
        if (ringClockwise) {
            if (!currentParts.isEmpty()) {
                final Polygon polygon = geometryFactory.polygon(currentParts);
                polygons.add(polygon);
                currentParts.clear();
            }
        }
        currentParts.add(ring);
    }
    if (!currentParts.isEmpty()) {
        final Polygon polygon = geometryFactory.polygon(currentParts);
        polygons.add(polygon);
    }
    if (polygons.size() == 1) {
        return polygons.get(0);
    } else {
        return geometryFactory.polygonal(polygons);
    }
}
Also used : ArrayList(java.util.ArrayList) Polygon(com.revolsys.geometry.model.Polygon) LinearRing(com.revolsys.geometry.model.LinearRing)

Example 27 with Polygon

use of com.revolsys.geometry.model.Polygon in project com.revolsys.open by revolsys.

the class ShapefileGeometryUtil method writePolygon.

private List<LineString> writePolygon(final EndianOutput out, final Geometry geometry, final int shapeType, final int headerOverhead, final int wordsPerPoint) throws IOException {
    int vertexCount = 0;
    final List<LineString> rings = new ArrayList<>();
    for (int i = 0; i < geometry.getGeometryCount(); i++) {
        final Geometry part = geometry.getGeometry(i);
        if (part instanceof Polygon) {
            final Polygon polygon = (Polygon) part;
            LineString shell = polygon.getShell();
            shell = shell.toClockwise();
            rings.add(shell);
            vertexCount += shell.getVertexCount();
            final int numHoles = polygon.getHoleCount();
            for (int j = 0; j < numHoles; j++) {
                LineString hole = polygon.getHole(j);
                hole = hole.toCounterClockwise();
                rings.add(hole);
                vertexCount += hole.getVertexCount();
            }
        } else {
            throw new IllegalArgumentException("Expecting " + Polygon.class + " geometry got " + part.getClass());
        }
    }
    final int numParts = rings.size();
    if (this.writeLength) {
        final int recordLength = 22 + headerOverhead + 2 * numParts + wordsPerPoint * vertexCount;
        out.writeInt(recordLength);
    }
    out.writeLEInt(shapeType);
    final BoundingBox envelope = geometry.getBoundingBox();
    writeEnvelope(out, envelope);
    out.writeLEInt(numParts);
    out.writeLEInt(vertexCount);
    int partIndex = 0;
    for (final LineString ring : rings) {
        out.writeLEInt(partIndex);
        partIndex += ring.getVertexCount();
    }
    for (final LineString ring : rings) {
        writeXYCoordinates(out, ring);
    }
    return rings;
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) LineString(com.revolsys.geometry.model.LineString) BoundingBox(com.revolsys.geometry.model.BoundingBox) ArrayList(java.util.ArrayList) Polygon(com.revolsys.geometry.model.Polygon) Point(com.revolsys.geometry.model.Point)

Example 28 with Polygon

use of com.revolsys.geometry.model.Polygon in project com.revolsys.open by revolsys.

the class GeometryTestUtil method polygon.

public static Polygon polygon(final GeometryFactory geometryFactory, final int ringCount, final double delta) {
    final int axisCount = geometryFactory.getAxisCount();
    final List<LinearRing> rings = new ArrayList<>();
    for (int ringIndex = 0; ringIndex < ringCount; ringIndex++) {
        final double[] coordinates = new double[axisCount * 5];
        final double offset = delta / 100 * ringIndex;
        final double size = delta - offset * 2;
        final double[] firstPoint = coordinates(geometryFactory, delta);
        final double x = Doubles.makePrecise(1000000, firstPoint[0] + offset);
        final double y = Doubles.makePrecise(1000000, firstPoint[1] + offset);
        for (int vertexIndex = 0; vertexIndex < 5; vertexIndex++) {
            final double[] point = coordinates(geometryFactory, delta);
            point[0] = x;
            point[1] = y;
            if (vertexIndex == 1) {
                point[1] += size;
            } else if (vertexIndex == 2) {
                point[0] += size;
                point[1] += size;
            } else if (vertexIndex == 3) {
                point[0] += size;
            }
            point[0] = Doubles.makePrecise(1000000, point[0]);
            point[1] = Doubles.makePrecise(1000000, point[1]);
            CoordinatesListUtil.setCoordinates(coordinates, axisCount, vertexIndex, point);
        }
        LinearRing ring = geometryFactory.linearRing(axisCount, coordinates);
        if (ringIndex > 0) {
            ring = ring.reverse();
        }
        rings.add(ring);
    }
    final Polygon polygon = geometryFactory.polygon(rings);
    return polygon;
}
Also used : ArrayList(java.util.ArrayList) LinearRing(com.revolsys.geometry.model.LinearRing) Polygon(com.revolsys.geometry.model.Polygon) Point(com.revolsys.geometry.model.Point)

Example 29 with Polygon

use of com.revolsys.geometry.model.Polygon in project com.revolsys.open by revolsys.

the class PolygonTest method testVertices.

@Test
public void testVertices() {
    final List<Point> allCoordinates = new ArrayList<>();
    allCoordinates.addAll(EXTERIOR_1);
    allCoordinates.addAll(INTERIOR_2);
    final Polygon polygon = WITH_HOLE;
    int i = 0;
    for (final Vertex vertex : polygon.vertices()) {
        final Point point = allCoordinates.get(i);
        Assert.assertEquals(point, vertex);
        i++;
    }
    Assert.assertEquals(new PointDoubleXY(0.0, 0.0), polygon.getVertex(0, 0));
    Assert.assertNull("VertexIndex out of range", polygon.getVertex(0, 6));
    Assert.assertNull("VertexIndex out of range", polygon.getVertex(1, 6));
    Assert.assertNull("RingIndex Negative", polygon.getVertex(-1, 0));
    Assert.assertNull("RingIndex out of range", polygon.getVertex(2, 0));
}
Also used : Vertex(com.revolsys.geometry.model.vertex.Vertex) ArrayList(java.util.ArrayList) Point(com.revolsys.geometry.model.Point) PointDoubleXY(com.revolsys.geometry.model.impl.PointDoubleXY) Polygon(com.revolsys.geometry.model.Polygon) Point(com.revolsys.geometry.model.Point) Test(org.junit.Test)

Example 30 with Polygon

use of com.revolsys.geometry.model.Polygon in project com.revolsys.open by revolsys.

the class CoordinateSequenceExperiment2 method run2.

public void run2(final int nPts) throws IOException {
    final double size = 100.0;
    final double armLen = 50.0;
    final int nArms = 10;
    long startTime = System.currentTimeMillis();
    final Polygon poly = GeometryTestFactory.newSineStar(this.fact, 0.0, 0.0, size, armLen, nArms, nPts);
    final Polygon box = GeometryTestFactory.newSineStar(this.fact, 0.0, size / 2, size, armLen, nArms, nPts);
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    String totalTimeStr = totalTime < 10000 ? totalTime + " ms" : totalTime / 1000.0 + " s";
    // System.out.println("Sine Star Creation Executed in " + totalTimeStr);
    // RobustDeterminant.callCount = 0;
    // System.out.println("n Pts: " + nPts);
    startTime = System.currentTimeMillis();
    poly.intersects(box);
    // poly.intersection(box);
    endTime = System.currentTimeMillis();
    totalTime = endTime - startTime;
    totalTimeStr = totalTime < 10000 ? totalTime + " ms" : totalTime / 1000.0 + " s";
// System.out.println(" signOfDet2x2 calls: " +
// RobustDeterminant.callCount);
// System.out.println(" Executed in " + totalTimeStr);
}
Also used : Polygon(com.revolsys.geometry.model.Polygon)

Aggregations

Polygon (com.revolsys.geometry.model.Polygon)147 Point (com.revolsys.geometry.model.Point)66 LinearRing (com.revolsys.geometry.model.LinearRing)54 LineString (com.revolsys.geometry.model.LineString)39 Geometry (com.revolsys.geometry.model.Geometry)34 ArrayList (java.util.ArrayList)30 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)22 Polygonal (com.revolsys.geometry.model.Polygonal)17 BoundingBox (com.revolsys.geometry.model.BoundingBox)14 Punctual (com.revolsys.geometry.model.Punctual)12 Test (org.junit.Test)12 Lineal (com.revolsys.geometry.model.Lineal)11 BaseCloseable (com.revolsys.io.BaseCloseable)7 List (java.util.List)7 PolygonEditor (com.revolsys.geometry.model.editor.PolygonEditor)6 PointDoubleXY (com.revolsys.geometry.model.impl.PointDoubleXY)6 Vertex (com.revolsys.geometry.model.vertex.Vertex)6 CoordinateSystem (com.revolsys.geometry.cs.CoordinateSystem)3 ProjectedCoordinateSystem (com.revolsys.geometry.cs.ProjectedCoordinateSystem)3 Graphics2D (java.awt.Graphics2D)3