Search in sources :

Example 41 with Polygon

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

the class OgrRecordWriter method toOgrGeometry.

protected org.gdal.ogr.Geometry toOgrGeometry(final Geometry geometry, final int geometryType, final int axisCount) {
    final org.gdal.ogr.Geometry ogrGeometry = new org.gdal.ogr.Geometry(geometryType);
    if (!geometry.isEmpty()) {
        switch(geometryType) {
            case 1:
            case 0x80000000 + 1:
                {
                    final Point point = (Point) geometry;
                    final double x = point.getX();
                    final double y = point.getY();
                    if (axisCount == 2) {
                        ogrGeometry.AddPoint(x, y);
                    } else {
                        final double z = point.getZ();
                        ogrGeometry.AddPoint(x, y, z);
                    }
                }
                break;
            case 2:
            case 0x80000000 + 2:
                final LineString line = (LineString) geometry;
                final int vertexCount = line.getVertexCount();
                for (int vertexIndex = 0; vertexIndex < vertexCount; vertexIndex++) {
                    final double x = line.getX(vertexIndex);
                    final double y = line.getY(vertexIndex);
                    if (axisCount == 2) {
                        ogrGeometry.AddPoint(x, y);
                    } else {
                        final double z = line.getZ(vertexIndex);
                        ogrGeometry.AddPoint(x, y, z);
                    }
                }
                break;
            case 3:
            case 0x80000000 + 3:
                for (final LinearRing ring : ((Polygon) geometry).rings()) {
                    final org.gdal.ogr.Geometry ogrRing = toOgrGeometry(ring, 101, axisCount);
                    ogrGeometry.AddGeometry(ogrRing);
                }
                break;
            case 4:
                addParts(ogrGeometry, geometry, 1, axisCount);
                break;
            case 0x80000000 + 4:
                addParts(ogrGeometry, geometry, 0x80000000 + 1, axisCount);
                break;
            case 5:
                addParts(ogrGeometry, geometry, 2, axisCount);
                break;
            case 0x80000000 + 5:
                addParts(ogrGeometry, geometry, 0x80000000 + 2, axisCount);
                break;
            case 6:
                addParts(ogrGeometry, geometry, 3, axisCount);
                break;
            case 0x80000000 + 6:
                addParts(ogrGeometry, geometry, 0x80000000 + 3, axisCount);
                break;
            // return this.geometryFactory.geometry(parts);
            case 101:
                for (final Vertex vertex : geometry.vertices()) {
                    final double x = vertex.getX();
                    final double y = vertex.getY();
                    if (axisCount == 2) {
                        ogrGeometry.AddPoint(x, y);
                    } else {
                        final double z = vertex.getZ();
                        ogrGeometry.AddPoint(x, y, z);
                    }
                }
                break;
            default:
                return null;
        }
    }
    if (axisCount == 2) {
        ogrGeometry.FlattenTo2D();
    }
    return ogrGeometry;
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) Vertex(com.revolsys.geometry.model.vertex.Vertex) LineString(com.revolsys.geometry.model.LineString) Point(com.revolsys.geometry.model.Point) LinearRing(com.revolsys.geometry.model.LinearRing) Polygon(com.revolsys.geometry.model.Polygon) Point(com.revolsys.geometry.model.Point)

Example 42 with Polygon

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

the class DistanceToPoint method computeDistance.

public static void computeDistance(final Geometry geom, final Point point, final PointPairDistance pointDistance) {
    if (geom instanceof LineString) {
        final LineString line = (LineString) geom;
        computeDistance(line, point, pointDistance);
    } else if (geom instanceof Polygon) {
        final Polygon polygon = (Polygon) geom;
        computeDistance(polygon, point, pointDistance);
    } else if (geom.isGeometryCollection()) {
        for (final Geometry part : geom.geometries()) {
            computeDistance(part, point, pointDistance);
        }
    } else {
        // assume geom is Point
        pointDistance.setMinimum(geom.getPoint(), point);
    }
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) LineString(com.revolsys.geometry.model.LineString) Polygon(com.revolsys.geometry.model.Polygon)

Example 43 with Polygon

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

the class MultiPolygonEditor method forEachPolygon.

@Override
public void forEachPolygon(final Consumer<Polygon> action) {
    for (int i = 0; i < getGeometryCount(); i++) {
        final Polygon polygon = getEditor(i);
        action.accept(polygon);
    }
}
Also used : MultiPolygon(com.revolsys.geometry.model.MultiPolygon) Polygon(com.revolsys.geometry.model.Polygon)

Example 44 with Polygon

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

the class WKTReader method readMultiPolygonText.

/**
 *  Creates a <code>MultiPolygon</code> using the next token in the stream.
 *
 *@param  tokenizer        tokenizer over a stream of text in Well-known Text
 *      format. The next tokens must form a &lt;MultiPolygon Text&gt;.
 *@return                  a <code>MultiPolygon</code> specified by the next
 *      token in the stream, or if if the coordinates used to create the
 *      <code>Polygon</code> shells and holes do not form closed linestrings.
 *@throws  IOException     if an I/O error occurs
 *@throws  ParseException  if an unexpected token was encountered
 */
private Polygonal readMultiPolygonText() throws IOException, ParseException {
    String nextToken = getNextEmptyOrOpener();
    if (nextToken.equals(EMPTY)) {
        return this.geometryFactory.polygonal(new Polygon[] {});
    }
    final ArrayList polygons = new ArrayList();
    Polygon polygon = readPolygonText();
    polygons.add(polygon);
    nextToken = getNextCloserOrComma();
    while (nextToken.equals(COMMA)) {
        polygon = readPolygonText();
        polygons.add(polygon);
        nextToken = getNextCloserOrComma();
    }
    final Polygon[] array = new Polygon[polygons.size()];
    return this.geometryFactory.polygonal((Polygon[]) polygons.toArray(array));
}
Also used : ArrayList(java.util.ArrayList) LineString(com.revolsys.geometry.model.LineString) Polygon(com.revolsys.geometry.model.Polygon)

Example 45 with Polygon

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

the class CascadedPolygonUnion method restrictToPolygons.

/**
 * Computes a {@link Polygonal} containing only {@link Polygonal} components.
 * Extracts the {@link Polygon}s from the input
 * and returns them as an appropriate {@link Polygonal} geometry.
 * <p>
 * If the input is already <tt>Polygonal</tt>, it is returned unchanged.
 * <p>
 * A particular use case is to filter out non-polygonal components
 * returned from an overlay operation.
 *
 * @param geometry the geometry to filter
 * @return a Polygonal geometry
 */
private static Polygonal restrictToPolygons(final Geometry geometry) {
    if (geometry instanceof Polygonal) {
        return (Polygonal) geometry;
    } else {
        final List<Polygon> polygons = geometry.getGeometries(Polygon.class);
        if (polygons.size() == 1) {
            return polygons.get(0);
        }
        final GeometryFactory geometryFactory = geometry.getGeometryFactory();
        return geometryFactory.polygonal(polygons);
    }
}
Also used : GeometryFactory(com.revolsys.geometry.model.GeometryFactory) Polygonal(com.revolsys.geometry.model.Polygonal) 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