Java Code Examples for org.locationtech.jts.geom.Polygon#getNumInteriorRing()

The following examples show how to use org.locationtech.jts.geom.Polygon#getNumInteriorRing() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GeometryUtilities.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates simple arrow polygons in the direction of the coordinates.
 * 
 * @param geometries the geometries (lines and polygons) for which to create the arrows.
 * @return the list of polygon arrows.
 */
public static List<Polygon> createSimpleDirectionArrow( Geometry... geometries ) {
    List<Polygon> polygons = new ArrayList<>();
    for( Geometry geometry : geometries ) {
        for( int i = 0; i < geometry.getNumGeometries(); i++ ) {
            Geometry geometryN = geometry.getGeometryN(i);
            if (geometryN instanceof LineString) {
                LineString line = (LineString) geometryN;
                polygons.addAll(makeArrows(line));
            } else if (geometryN instanceof Polygon) {
                Polygon polygonGeom = (Polygon) geometryN;
                LineString exteriorRing = polygonGeom.getExteriorRing();
                polygons.addAll(makeArrows(exteriorRing));
                int numInteriorRing = polygonGeom.getNumInteriorRing();
                for( int j = 0; j < numInteriorRing; j++ ) {
                    LineString interiorRingN = polygonGeom.getInteriorRingN(j);
                    polygons.addAll(makeArrows(interiorRingN));
                }
            }
        }
    }
    return polygons;
}
 
Example 2
Source File: TWKBWriter.java    From geowave with Apache License 2.0 6 votes vote down vote up
private void writeMultiPolygon(
    final MultiPolygon multiPolygon,
    final PrecisionWriter precision,
    final DataOutput output) throws IOException {
  Varint.writeUnsignedVarInt(multiPolygon.getNumGeometries(), output);
  for (int i = 0; i < multiPolygon.getNumGeometries(); i++) {
    final Polygon polygon = (Polygon) multiPolygon.getGeometryN(i);
    if (polygon.isEmpty()) {
      Varint.writeUnsignedVarInt(0, output);
      continue;
    }
    Varint.writeUnsignedVarInt(polygon.getNumInteriorRing() + 1, output);
    precision.writePointArray(polygon.getExteriorRing().getCoordinates(), output);
    for (int j = 0; j < polygon.getNumInteriorRing(); j++) {
      precision.writePointArray(polygon.getInteriorRingN(j).getCoordinates(), output);
    }
  }
}
 
Example 3
Source File: GeoJSONEncoder.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
protected ArrayNode encodeCoordinates(Polygon geometry) {
    ArrayNode list = jsonFactory.arrayNode();
    list.add(encodeCoordinates(geometry.getExteriorRing()));
    for (int i = 0; i < geometry.getNumInteriorRing(); ++i) {
        list.add(encodeCoordinates(geometry.getInteriorRingN(i)));
    }
    return list;
}
 
Example 4
Source File: GeoUtils.java    From elasticsearch-plugin-geoshape with MIT License 5 votes vote down vote up
public static Polygon removeDuplicateCoordinates(Polygon polygon) {
    LinearRing polygonShell = removeDuplicateCoordinates((LinearRing) polygon.getExteriorRing());
    LinearRing[] holes = new LinearRing[polygon.getNumInteriorRing()];
    for (int i = 0; i < polygon.getNumInteriorRing(); i++) {
        holes[i] = removeDuplicateCoordinates((LinearRing) polygon.getInteriorRingN(i));
    }
    return polygon.getFactory().createPolygon(polygonShell, holes);
}
 
Example 5
Source File: SpatialiteWKBWriter.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
private void writePolygon( Polygon poly, OutStream os ) throws IOException {
    // writeByteOrder(os);
    writeGeometryType(WKBConstants.wkbPolygon, poly, os);
    writeInt(poly.getNumInteriorRing() + 1, os);
    writeCoordinateSequence(poly.getExteriorRing().getCoordinateSequence(), true, os);
    for( int i = 0; i < poly.getNumInteriorRing(); i++ ) {
        writeCoordinateSequence(poly.getInteriorRingN(i).getCoordinateSequence(), true, os);
    }
}
 
Example 6
Source File: MultiPolygonUtils.java    From geofence with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return the number of points of a polygon in the format
 * E+I0+I1+...+In
 * where E is the number of points of the exterior ring and I0..In are
 * the number of points of the Internal rings.
 */
public static String getPoints(final Polygon p)
{
    final StringBuilder sb = new StringBuilder();
    sb.append(p.getExteriorRing().getNumPoints());
    for (int i = 0; i < p.getNumInteriorRing(); i++)
    {
        LineString ir = p.getInteriorRingN(i);
        sb.append('+').append(ir.getNumPoints());
    }

    return sb.toString();
}
 
Example 7
Source File: TWKBWriter.java    From geowave with Apache License 2.0 5 votes vote down vote up
private void writePolygon(
    final Polygon polygon,
    final PrecisionWriter precision,
    final DataOutput output) throws IOException {
  Varint.writeUnsignedVarInt(polygon.getNumInteriorRing() + 1, output);
  precision.writePointArray(polygon.getExteriorRing().getCoordinates(), output);
  for (int i = 0; i < polygon.getNumInteriorRing(); i++) {
    precision.writePointArray(polygon.getInteriorRingN(i).getCoordinates(), output);
  }
}
 
Example 8
Source File: GeometryTransform.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Transforms the given polygon. Can be invoked directly if the type is known at compile-time,
 * or indirectly through a call to the more generic {@link #transform(Geometry)} method.
 *
 * @param  geom  the polygon to transform.
 * @return the transformed polygon.
 * @throws TransformException if an error occurred while transforming the geometry.
 */
public Polygon transform(final Polygon geom) throws TransformException {
    final LinearRing exterior = transform((LinearRing) geom.getExteriorRing());
    final LinearRing[] holes = new LinearRing[geom.getNumInteriorRing()];
    for (int i = 0; i < holes.length; i++) {
        holes[i] = transform((LinearRing) geom.getInteriorRingN(i));
    }
    return geometryFactory.createPolygon(exterior, holes);
}
 
Example 9
Source File: GeoJSONUtils.java    From crate with Apache License 2.0 5 votes vote down vote up
private double[][][] extract(Polygon polygon) {
    int size = polygon.getNumInteriorRing() + 1;
    double[][][] rings = new double[size][][];
    rings[0] = toArray(polygon.getExteriorRing().getCoordinates());
    for (int i = 0; i < size - 1; i++) {
        rings[i + 1] = toArray(polygon.getInteriorRingN(i).getCoordinates());
    }
    return rings;
}
 
Example 10
Source File: ShapeWriter.java    From geopaparazzi with GNU General Public License v3.0 5 votes vote down vote up
private DrawableShape toShape(Polygon p) {
    PolygonShape poly = new PolygonShape();

    appendRing(poly, p.getExteriorRing().getCoordinates());
    for (int j = 0; j < p.getNumInteriorRing(); j++) {
        appendRing(poly, p.getInteriorRingN(j).getCoordinates());
    }

    return poly;
}
 
Example 11
Source File: JtsLineStringIterable.java    From geogson with Apache License 2.0 5 votes vote down vote up
public static JtsLineStringIterable forHolesOf(final Polygon src) {
    return new JtsLineStringIterable(new LineStringProvider() {
        @Override
        public int getNumLineStrings() {
            return src.getNumInteriorRing();
        }

        @Override
        public LineString getLineStringN(int n) {
            return src.getInteriorRingN(n);
        }
    });
}
 
Example 12
Source File: GmlEncoderv321.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a XML Polygon from a SOS Polygon.
 *
 * @param jtsPolygon
 *            SOS Polygon
 * @param xbPolType
 *            XML Polygon
 */
private void createPolygonFromJtsGeometry(Polygon jtsPolygon, PolygonType xbPolType) {
    List<?> jtsPolygons = PolygonExtracter.getPolygons(jtsPolygon);
    String srsName = getSrsName(jtsPolygon);

    for (int i = 0; i < jtsPolygons.size(); i++) {

        Polygon pol = (Polygon) jtsPolygons.get(i);

        AbstractRingPropertyType xbArpt = xbPolType.addNewExterior();
        AbstractRingType xbArt = xbArpt.addNewAbstractRing();

        LinearRingType xbLrt = LinearRingType.Factory.newInstance();

        // Exterior ring
        LineString ring = pol.getExteriorRing();
        DirectPositionListType xbPosList = xbLrt.addNewPosList();

        xbPosList.setSrsName(srsName);
        xbPosList.setStringValue(JTSHelper.getCoordinatesString(ring));
        xbArt.set(xbLrt);

        // Rename element name for output
        XmlCursor cursor = xbArpt.newCursor();
        if (cursor.toChild(GmlConstants.QN_ABSTRACT_RING_32)) {
            cursor.setName(GmlConstants.QN_LINEAR_RING_32);
        }
        cursor.dispose();

        // Interior ring
        int numberOfInteriorRings = pol.getNumInteriorRing();
        for (int ringNumber = 0; ringNumber < numberOfInteriorRings; ringNumber++) {
            xbArpt = xbPolType.addNewInterior();
            xbArt = xbArpt.addNewAbstractRing();

            xbLrt = LinearRingType.Factory.newInstance();

            ring = pol.getInteriorRingN(ringNumber);

            xbPosList = xbLrt.addNewPosList();
            xbPosList.setSrsName(srsName);
            xbPosList.setStringValue(JTSHelper.getCoordinatesString(ring));
            xbArt.set(xbLrt);

            // Rename element name for output
            cursor = xbArpt.newCursor();
            if (cursor.toChild(GmlConstants.QN_ABSTRACT_RING_32)) {
                cursor.setName(GmlConstants.QN_LINEAR_RING_32);
            }
            cursor.dispose();
        }
    }
}
 
Example 13
Source File: GmlEncoderv311.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a XML Polygon from a SOS Polygon.
 *
 * @param jtsPolygon
 *            SOS Polygon
 * @param xbPolType
 *            XML Polygon
 */
private void createPolygonFromJtsGeometry(Polygon jtsPolygon, PolygonType xbPolType) {
    List<?> jtsPolygons = PolygonExtracter.getPolygons(jtsPolygon);
    for (int i = 0; i < jtsPolygons.size(); i++) {

        Polygon pol = (Polygon) jtsPolygons.get(i);

        AbstractRingPropertyType xbArpt = xbPolType.addNewExterior();
        AbstractRingType xbArt = xbArpt.addNewRing();

        LinearRingType xbLrt = LinearRingType.Factory.newInstance(getXmlOptions());

        // Exterior ring
        LineString ring = pol.getExteriorRing();
        String coords = JTSHelper.getCoordinatesString(ring);
        DirectPositionListType xbPosList = xbLrt.addNewPosList();
        xbPosList.setSrsName(getSrsName(jtsPolygon));
        // switch coordinates
        xbPosList.setStringValue(coords);
        xbArt.set(xbLrt);

        // Rename element name for output
        XmlCursor cursor = xbArpt.newCursor();
        if (cursor.toChild(GmlConstants.QN_ABSTRACT_RING)) {
            cursor.setName(GmlConstants.QN_LINEAR_RING);
        }
        cursor.dispose();

        // Interior ring
        int numberOfInteriorRings = pol.getNumInteriorRing();
        for (int ringNumber = 0; ringNumber < numberOfInteriorRings; ringNumber++) {
            xbArpt = xbPolType.addNewInterior();
            xbArt = xbArpt.addNewRing();

            xbLrt = LinearRingType.Factory.newInstance(getXmlOptions());

            ring = pol.getInteriorRingN(ringNumber);

            xbPosList = xbLrt.addNewPosList();
            xbPosList.setSrsName(getSrsName(jtsPolygon));
            xbPosList.setStringValue(JTSHelper.getCoordinatesString(ring));
            xbArt.set(xbLrt);

            // Rename element name for output
            cursor = xbArpt.newCursor();
            if (cursor.toChild(GmlConstants.QN_ABSTRACT_RING)) {
                cursor.setName(GmlConstants.QN_LINEAR_RING);
            }
            cursor.dispose();
        }
    }
}
 
Example 14
Source File: DxfUtils.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
private static String polygon2Dxf( FeatureMate featureMate, String layerName, String elevationAttrName, boolean suffix ) {
    Geometry geometry = featureMate.getGeometry();
    int numGeometries = geometry.getNumGeometries();
    StringBuilder sb = new StringBuilder();
    for( int g = 0; g < numGeometries; g++ ) {
        Polygon geom = (Polygon) geometry.getGeometryN(g);

        Coordinate[] coords = geom.getExteriorRing().getCoordinates();
        sb.append(DxfGroup.toString(0, POLYLINE));
        sb.append(DxfGroup.toString(8, layerName));

        SimpleFeature feature = featureMate.getFeature();
        handleLTYPE(feature, sb);
        handleELEVATION(feature, sb);
        handleTHICKNESS(feature, sb);
        handleColor(feature, sb);

        double elev = Double.NaN;
        if (elevationAttrName != null) {
            Double tmp = featureMate.getAttribute(elevationAttrName, Double.class);
            if (tmp != null) {
                elev = tmp;
            }
        }

        sb.append(DxfGroup.toString(66, 1));
        sb.append(DxfGroup.toString(10, ZERO));
        sb.append(DxfGroup.toString(20, ZERO));
        coords[0].z = elev;
        if (!Double.isNaN(coords[0].z))
            sb.append(DxfGroup.toString(30, ZERO));
        sb.append(DxfGroup.toString(70, 9));
        for( int i = 0; i < coords.length; i++ ) {
            sb.append(DxfGroup.toString(0, VERTEX));
            sb.append(DxfGroup.toString(8, layerName));
            sb.append(DxfGroup.toString(10, coords[i].x, precision));
            sb.append(DxfGroup.toString(20, coords[i].y, precision));
            coords[i].z = elev;
            if (!Double.isNaN(coords[i].z))
                sb.append(DxfGroup.toString(30, coords[i].z, precision));
            sb.append(DxfGroup.toString(70, 32));
        }
        sb.append(DxfGroup.toString(0, SEQEND));
        for( int h = 0; h < geom.getNumInteriorRing(); h++ ) {
            sb.append(DxfGroup.toString(0, POLYLINE));
            if (suffix)
                sb.append(DxfGroup.toString(8, layerName + SUFFIX));
            else
                sb.append(DxfGroup.toString(8, layerName));

            handleLTYPE(feature, sb);
            handleTHICKNESS(feature, sb);
            handleColor(feature, sb);

            sb.append(DxfGroup.toString(66, 1));
            sb.append(DxfGroup.toString(10, ZERO));
            sb.append(DxfGroup.toString(20, ZERO));
            coords[0].z = elev;
            if (!Double.isNaN(coords[0].z))
                sb.append(DxfGroup.toString(30, ZERO));
            sb.append(DxfGroup.toString(70, 9));
            coords = geom.getInteriorRingN(h).getCoordinates();
            for( int i = 0; i < coords.length; i++ ) {
                sb.append(DxfGroup.toString(0, VERTEX));
                if (suffix)
                    sb.append(DxfGroup.toString(8, layerName + SUFFIX));
                else
                    sb.append(DxfGroup.toString(8, layerName));
                sb.append(DxfGroup.toString(10, coords[i].x, precision));
                sb.append(DxfGroup.toString(20, coords[i].y, precision));
                coords[i].z = elev;
                if (!Double.isNaN(coords[i].z))
                    sb.append(DxfGroup.toString(30, coords[i].z, precision));
                sb.append(DxfGroup.toString(70, 32));
            }
            sb.append(DxfGroup.toString(0, SEQEND));
        }
    }
    return sb.toString();
}