/** * Tests whether this PreparedPolygon intersects a given geometry. * * @param geom the test geometry * @return true if the test geometry intersects */ public boolean intersects(Geometry geom) { /** * Do point-in-poly tests first, since they are cheaper and may result in a quick positive * result. * * <p>If a point of any test components lie in target, result is true */ boolean isInPrepGeomArea = isAnyTestComponentInTarget(geom); if (isInPrepGeomArea) return true; /** If any segments intersect, result is true */ List lineSegStr = SegmentStringUtil.extractSegmentStrings(geom); // only request intersection finder if there are segments (ie NOT for point inputs) if (lineSegStr.size() > 0) { boolean segsIntersect = prepPoly.getIntersectionFinder().intersects(lineSegStr); if (segsIntersect) return true; } /** * If the test has dimension = 2 as well, it is necessary to test for proper inclusion of the * target. Since no segments intersect, it is sufficient to test representative points. */ if (geom.getDimension() == 2) { // TODO: generalize this to handle GeometryCollections boolean isPrepGeomInArea = isAnyTargetComponentInAreaTest(geom, prepPoly.getRepresentativePoints()); if (isPrepGeomInArea) return true; } return false; }
protected Geometry transformPolygon(Polygon geom, Geometry parent) { boolean isAllValidLinearRings = true; Geometry shell = transformLinearRing((LinearRing) geom.getExteriorRing(), geom); if (shell == null || !(shell instanceof LinearRing) || shell.isEmpty()) isAllValidLinearRings = false; // return factory.createPolygon(null, null); ArrayList holes = new ArrayList(); for (int i = 0; i < geom.getNumInteriorRing(); i++) { Geometry hole = transformLinearRing((LinearRing) geom.getInteriorRingN(i), geom); if (hole == null || hole.isEmpty()) { continue; } if (!(hole instanceof LinearRing)) isAllValidLinearRings = false; holes.add(hole); } if (isAllValidLinearRings) return factory.createPolygon( (LinearRing) shell, (LinearRing[]) holes.toArray(new LinearRing[] {})); else { List components = new ArrayList(); if (shell != null) components.add(shell); components.addAll(holes); return factory.buildGeometry(components); } }
private void init(Geometry geom) { List lines = LinearComponentExtracter.getLines(geom); for (Iterator i = lines.iterator(); i.hasNext(); ) { LineString line = (LineString) i.next(); Coordinate[] pts = line.getCoordinates(); addLine(pts); } }
private void addLine(Coordinate[] pts) { SegmentString segStr = new BasicSegmentString(pts, null); List segChains = MonotoneChainBuilder.getChains(segStr.getCoordinates(), segStr); for (Iterator i = segChains.iterator(); i.hasNext(); ) { MonotoneChain mc = (MonotoneChain) i.next(); index.insert(mc.getEnvelope(), mc); } }
/** * Tests whether any component of the test Geometry intersects the interior of the target * geometry. Handles test geometries with both linear and point components. * * @param geom a geometry to test * @return true if any component of the argument intersects the prepared area geometry interior */ protected boolean isAnyTestComponentInTargetInterior(Geometry testGeom) { List coords = ComponentCoordinateExtracter.getCoordinates(testGeom); for (Iterator i = coords.iterator(); i.hasNext(); ) { Coordinate p = (Coordinate) i.next(); int loc = targetPointLocator.locate(p); if (loc == Location.INTERIOR) return true; } return false; }
private static Geometry convertSegStrings(Iterator it) { GeometryFactory fact = new GeometryFactory(); List lines = new ArrayList(); while (it.hasNext()) { SegmentString ss = (SegmentString) it.next(); LineString line = fact.createLineString(ss.getCoordinates()); lines.add(line); } return fact.buildGeometry(lines); }
protected Geometry transformMultiPolygon(MultiPolygon geom, Geometry parent) { List transGeomList = new ArrayList(); for (int i = 0; i < geom.getNumGeometries(); i++) { Geometry transformGeom = transformPolygon((Polygon) geom.getGeometryN(i), geom); if (transformGeom == null) continue; if (transformGeom.isEmpty()) continue; transGeomList.add(transformGeom); } return factory.buildGeometry(transGeomList); }
private Geobuf.Data.Feature makeFeature(GeobufFeature feature, List<String> keys) { Geobuf.Data.Feature.Builder feat = Geobuf.Data.Feature.newBuilder().setGeometry(geomToGeobuf(feature.geometry)); for (Map.Entry<String, Object> e : feature.properties.entrySet()) { // TODO store keys separately from features Geobuf.Data.Value.Builder val = Geobuf.Data.Value.newBuilder(); Object featVal = e.getValue(); if (featVal instanceof String) val.setStringValue((String) featVal); else if (featVal instanceof Boolean) val.setBoolValue((Boolean) featVal); else if (featVal instanceof Integer) { int keyInt = (Integer) featVal; if (keyInt >= 0) val.setPosIntValue(keyInt); else val.setNegIntValue(keyInt); } else if (featVal instanceof Long) { long keyLong = (Long) featVal; if (keyLong >= 0) val.setPosIntValue(keyLong); else val.setNegIntValue(keyLong); } else if (featVal instanceof Double || featVal instanceof Float) val.setDoubleValue(((Number) featVal).doubleValue()); else { // TODO serialize to JSON LOG.warn( "Unable to save object of type {} to geobuf, falling back on toString. Deserialization will not work as expected.", featVal.getClass()); val.setStringValue(featVal.toString()); } int keyIdx = keys.indexOf(e.getKey()); if (keyIdx == -1) { synchronized (keys) { keyIdx = keys.size(); keys.add(e.getKey()); } } // properties is a jagged array of [key index, value index, . . .] feat.addProperties(keyIdx); feat.addProperties(feat.getValuesCount()); feat.addValues(val); } if (feature.id != null) { feat.setId(feature.id); feat.clearIntId(); } else { feat.setIntId(feature.numericId); feat.clearId(); } return feat.build(); }
private static Coordinate[] extractCoordinates(List segs) { Coordinate[] pts = new Coordinate[segs.size() + 1]; LineSegment seg = null; for (int i = 0; i < segs.size(); i++) { seg = (LineSegment) segs.get(i); pts[i] = seg.p0; } // add last point pts[pts.length - 1] = seg.p1; return pts; }
/** * Computes the {@link Polygon} formed by this ring and any contained holes. * * @return the {@link Polygon} formed by this ring and its holes. */ public Polygon getPolygon() { LinearRing[] holeLR = null; if (holes != null) { holeLR = new LinearRing[holes.size()]; for (int i = 0; i < holes.size(); i++) { holeLR[i] = (LinearRing) holes.get(i); } } Polygon poly = factory.createPolygon(ring, holeLR); return poly; }
protected Geometry transformGeometryCollection(GeometryCollection geom, Geometry parent) { List transGeomList = new ArrayList(); for (int i = 0; i < geom.getNumGeometries(); i++) { Geometry transformGeom = transform(geom.getGeometryN(i)); if (transformGeom == null) continue; if (pruneEmptyGeometry && transformGeom.isEmpty()) continue; transGeomList.add(transformGeom); } if (preserveGeometryCollectionType) return factory.createGeometryCollection(GeometryFactory.toGeometryArray(transGeomList)); return factory.buildGeometry(transGeomList); }
/** * Tests whether any representative point of the test Geometry intersects the target geometry. * Only handles test geometries which are Puntal (dimension 0) * * @param geom a Puntal geometry to test * @return true if any point of the argument intersects the prepared geometry */ protected boolean isAnyTestPointInTarget(Geometry testGeom) { /** * This could be optimized by using the segment index on the lineal target. However, it seems * like the L/P case would be pretty rare in practice. */ PointLocator locator = new PointLocator(); List coords = ComponentCoordinateExtracter.getCoordinates(testGeom); for (Iterator i = coords.iterator(); i.hasNext(); ) { Coordinate p = (Coordinate) i.next(); if (locator.intersects(p, prepLine.getGeometry())) return true; } return false; }
/** * Find the innermost enclosing shell EdgeRing containing the argument EdgeRing, if any. The * innermost enclosing ring is the <i>smallest</i> enclosing ring. The algorithm used depends on * the fact that: <br> * ring A contains ring B iff envelope(ring A) contains envelope(ring B) <br> * This routine is only safe to use if the chosen point of the hole is known to be properly * contained in a shell (which is guaranteed to be the case if the hole does not touch its shell) * * @return containing EdgeRing, if there is one or null if no containing EdgeRing is found */ public static EdgeRing findEdgeRingContaining(EdgeRing testEr, List shellList) { LinearRing testRing = testEr.getRing(); Envelope testEnv = testRing.getEnvelopeInternal(); Coordinate testPt = testRing.getCoordinateN(0); EdgeRing minShell = null; Envelope minShellEnv = null; for (Iterator it = shellList.iterator(); it.hasNext(); ) { EdgeRing tryShell = (EdgeRing) it.next(); LinearRing tryShellRing = tryShell.getRing(); Envelope tryShellEnv = tryShellRing.getEnvelopeInternal(); // the hole envelope cannot equal the shell envelope // (also guards against testing rings against themselves) if (tryShellEnv.equals(testEnv)) continue; // hole must be contained in shell if (!tryShellEnv.contains(testEnv)) continue; testPt = CoordinateArrays.ptNotInList(testRing.getCoordinates(), tryShellRing.getCoordinates()); boolean isContained = false; if (CGAlgorithms.isPointInRing(testPt, tryShellRing.getCoordinates())) isContained = true; // check if this new containing ring is smaller than the current minimum ring if (isContained) { if (minShell == null || minShellEnv.contains(tryShellEnv)) { minShell = tryShell; minShellEnv = minShell.getRing().getEnvelopeInternal(); } } } return minShell; }
public void findEdge(List dirEdgeList) { /** * Check all forward DirectedEdges only. This is still general, because each edge has a forward * DirectedEdge. */ for (Iterator i = dirEdgeList.iterator(); i.hasNext(); ) { DirectedEdge de = (DirectedEdge) i.next(); if (!de.isForward()) continue; checkForRightmostCoordinate(de); } /** * If the rightmost point is a node, we need to identify which of the incident edges is * rightmost. */ Assert.isTrue( minIndex != 0 || minCoord.equals(minDe.getCoordinate()), "inconsistency in rightmost processing"); if (minIndex == 0) { findRightmostEdgeAtNode(); } else { findRightmostEdgeAtVertex(); } /** now check that the extreme side is the R side. If not, use the sym instead. */ orientedDe = minDe; int rightmostSide = getRightmostSide(minDe, minIndex); if (rightmostSide == Position.LEFT) { orientedDe = minDe.getSym(); } }
public Geometry buffer(Geometry g, double distance) { PrecisionModel precisionModel = workingPrecisionModel; if (precisionModel == null) precisionModel = g.getPrecisionModel(); // factory must be the same as the one used by the input geomFact = g.getFactory(); OffsetCurveBuilder curveBuilder = new OffsetCurveBuilder(precisionModel, bufParams); OffsetCurveSetBuilder curveSetBuilder = new OffsetCurveSetBuilder(g, distance, curveBuilder); List bufferSegStrList = curveSetBuilder.getCurves(); // short-circuit test if (bufferSegStrList.size() <= 0) { return createEmptyResultGeometry(); } // BufferDebug.runCount++; // String filename = "run" + BufferDebug.runCount + "_curves"; // System.out.println("saving " + filename); // BufferDebug.saveEdges(bufferEdgeList, filename); // DEBUGGING ONLY // WKTWriter wktWriter = new WKTWriter(); // Debug.println("Rings: " + wktWriter.write(convertSegStrings(bufferSegStrList.iterator()))); // wktWriter.setMaxCoordinatesPerLine(10); // System.out.println(wktWriter.writeFormatted(convertSegStrings(bufferSegStrList.iterator()))); computeNodedEdges(bufferSegStrList, precisionModel); graph = new PlanarGraph(new OverlayNodeFactory()); graph.addEdges(edgeList.getEdges()); List subgraphList = createSubgraphs(graph); PolygonBuilder polyBuilder = new PolygonBuilder(geomFact); buildSubgraphs(subgraphList, polyBuilder); List resultPolyList = polyBuilder.getPolygons(); // just in case... if (resultPolyList.size() <= 0) { return createEmptyResultGeometry(); } Geometry resultGeom = geomFact.buildGeometry(resultPolyList); return resultGeom; }
private void countSegs( RayCrossingCounter rcc, Envelope rayEnv, List monoChains, MCSegmentCounter mcSegCounter) { for (Iterator i = monoChains.iterator(); i.hasNext(); ) { MonotoneChain mc = (MonotoneChain) i.next(); mc.select(rayEnv, mcSegCounter); // short-circuit if possible if (rcc.isOnSegment()) return; } }
private List createSubgraphs(PlanarGraph graph) { List subgraphList = new ArrayList(); for (Iterator i = graph.getNodes().iterator(); i.hasNext(); ) { Node node = (Node) i.next(); if (!node.isVisited()) { BufferSubgraph subgraph = new BufferSubgraph(); subgraph.create(node); subgraphList.add(subgraph); } } /** * Sort the subgraphs in descending order of their rightmost coordinate. This ensures that when * the Polygons for the subgraphs are built, subgraphs for shells will have been built before * the subgraphs for any holes they contain. */ Collections.sort(subgraphList, Collections.reverseOrder()); return subgraphList; }
/** * Tests whether any component of the target geometry intersects the test geometry (which must be * an areal geometry) * * @param geom the test geometry * @param repPts the representative points of the target geometry * @return true if any component intersects the areal test geometry */ protected boolean isAnyTargetComponentInAreaTest(Geometry testGeom, List targetRepPts) { PointOnGeometryLocator piaLoc = new SimplePointInAreaLocator(testGeom); for (Iterator i = targetRepPts.iterator(); i.hasNext(); ) { Coordinate p = (Coordinate) i.next(); int loc = piaLoc.locate(p); if (loc != Location.EXTERIOR) return true; } return false; }
/** * Computes the list of coordinates which are contained in this ring. The coordinatea are computed * once only and cached. * * @return an array of the {@link Coordinate}s in this ring */ private Coordinate[] getCoordinates() { if (ringPts == null) { CoordinateList coordList = new CoordinateList(); for (Iterator i = deList.iterator(); i.hasNext(); ) { DirectedEdge de = (DirectedEdge) i.next(); PolygonizeEdge edge = (PolygonizeEdge) de.getEdge(); addEdge(edge.getLine().getCoordinates(), de.getEdgeDirection(), coordList); } ringPts = coordList.toCoordinateArray(); } return ringPts; }
/** * Completes the building of the input subgraphs by depth-labelling them, and adds them to the * PolygonBuilder. The subgraph list must be sorted in rightmost-coordinate order. * * @param subgraphList the subgraphs to build * @param polyBuilder the PolygonBuilder which will build the final polygons */ private void buildSubgraphs(List subgraphList, PolygonBuilder polyBuilder) { List processedGraphs = new ArrayList(); for (Iterator i = subgraphList.iterator(); i.hasNext(); ) { BufferSubgraph subgraph = (BufferSubgraph) i.next(); Coordinate p = subgraph.getRightmostCoordinate(); // int outsideDepth = 0; // if (polyBuilder.containsPoint(p)) // outsideDepth = 1; SubgraphDepthLocater locater = new SubgraphDepthLocater(processedGraphs); int outsideDepth = locater.getDepth(p); // try { subgraph.computeDepth(outsideDepth); // } // catch (RuntimeException ex) { // // debugging only // //subgraph.saveDirEdges(); // throw ex; // } subgraph.findResultEdges(); processedGraphs.add(subgraph); polyBuilder.add(subgraph.getDirectedEdges(), subgraph.getNodes()); } }
/** * Computes the list of envelopes (from 2 to 4, possibily 0 if env covers this) resulting from the * extrusion of env from this. Only in 2D for the moment. Does not return null envelopes. */ public List<Envelope> extrusion(final Envelope env) { List<Envelope> list = new ArrayList(); double x1 = getMinX(); double x2 = getMaxX(); double y1 = getMinY(); double y2 = getMaxY(); double xx1 = env.getMinX(); double xx2 = env.getMaxX(); double yy1 = env.getMinY(); double yy2 = env.getMaxY(); if (x2 >= x1 && yy1 >= y1) { list.add(new Envelope(x1, x2, y1, yy1)); } if (xx1 >= x1 && y2 >= yy1) { list.add(new Envelope(x1, xx1, yy1, y2)); } if (x2 >= xx1 && y2 >= yy2) { list.add(new Envelope(xx1, x2, yy2, y2)); } if (x2 >= xx2 && yy2 >= yy1) { list.add(new Envelope(xx2, x2, yy1, yy2)); } return list; }
/** * This method is called by clients of the {@link SegmentIntersector} class to process * intersections for two segments of the {@link SegmentString}s being intersected. Note that some * clients (such as {@link MonotoneChain}s) may optimize away this call for segment pairs which * they have determined do not intersect (e.g. by an disjoint envelope test). */ public void processIntersections( SegmentString e0, int segIndex0, SegmentString e1, int segIndex1) { // don't bother intersecting a segment with itself if (e0 == e1 && segIndex0 == segIndex1) return; Coordinate p00 = e0.getCoordinates()[segIndex0]; Coordinate p01 = e0.getCoordinates()[segIndex0 + 1]; Coordinate p10 = e1.getCoordinates()[segIndex1]; Coordinate p11 = e1.getCoordinates()[segIndex1 + 1]; li.computeIntersection(p00, p01, p10, p11); // if (li.hasIntersection() && li.isProper()) Debug.println(li); if (li.hasIntersection()) { if (li.isInteriorIntersection()) { for (int intIndex = 0; intIndex < li.getIntersectionNum(); intIndex++) { interiorIntersections.add(li.getIntersection(intIndex)); } ((NodedSegmentString) e0).addIntersections(li, segIndex0, 0); ((NodedSegmentString) e1).addIntersections(li, segIndex1, 1); } } }
/** * Adds a {@link DirectedEdge} which is known to form part of this ring. * * @param de the {@link DirectedEdge} to add. */ public void add(DirectedEdge de) { deList.add(de); }
public void addToResult(LineSegment seg) { resultSegs.add(seg); }
public int getResultSize() { int resultSegsSize = resultSegs.size(); return resultSegsSize == 0 ? 0 : resultSegsSize + 1; }
/** * Write a dbf file with the information from the featureCollection. * * @param featureCollection column data from collection * @param fname name of the dbf file to write to */ void writeDbf(FeatureCollection featureCollection, String fname) throws Exception { DbfFileWriter dbf; FeatureSchema fs; int t; int f; int u; int num; fs = featureCollection.getFeatureSchema(); // -1 because one of the columns is geometry DbfFieldDef[] fields = new DbfFieldDef[fs.getAttributeCount() - 1]; // dbf column type and size f = 0; for (t = 0; t < fs.getAttributeCount(); t++) { AttributeType columnType = fs.getAttributeType(t); String columnName = fs.getAttributeName(t); if (columnType == AttributeType.INTEGER) { fields[f] = new DbfFieldDef(columnName, 'N', 16, 0); f++; } else if (columnType == AttributeType.DOUBLE) { fields[f] = new DbfFieldDef(columnName, 'N', 33, 16); f++; } else if (columnType == AttributeType.STRING) { int maxlength = findMaxStringLength(featureCollection, t); if (maxlength > 255) { throw new Exception( "ShapefileWriter does not support strings longer than 255 characters"); } fields[f] = new DbfFieldDef(columnName, 'C', maxlength, 0); f++; } else if (columnType == AttributeType.DATE) { fields[f] = new DbfFieldDef(columnName, 'D', 8, 0); f++; } else if (columnType == AttributeType.GEOMETRY) { // do nothing - the .shp file handles this } else { throw new Exception("Shapewriter: unsupported AttributeType found in featurecollection."); } } // write header dbf = new DbfFileWriter(fname); dbf.writeHeader(fields, featureCollection.size()); // write rows num = featureCollection.size(); List features = featureCollection.getFeatures(); for (t = 0; t < num; t++) { // System.out.println("dbf: record "+t); Feature feature = (Feature) features.get(t); Vector DBFrow = new Vector(); // make data for each column in this feature (row) for (u = 0; u < fs.getAttributeCount(); u++) { AttributeType columnType = fs.getAttributeType(u); if (columnType == AttributeType.INTEGER) { Object a = feature.getAttribute(u); if (a == null) { DBFrow.add(new Integer(0)); } else { DBFrow.add((Integer) a); } } else if (columnType == AttributeType.DOUBLE) { Object a = feature.getAttribute(u); if (a == null) { DBFrow.add(new Double(0.0)); } else { DBFrow.add((Double) a); } } else if (columnType == AttributeType.DATE) { Object a = feature.getAttribute(u); if (a == null) { DBFrow.add(""); } else { DBFrow.add(DbfFile.DATE_PARSER.format((Date) a)); } } else if (columnType == AttributeType.STRING) { Object a = feature.getAttribute(u); if (a == null) { DBFrow.add(new String("")); } else { // MD 16 jan 03 - added some defensive programming if (a instanceof String) { DBFrow.add(a); } else { DBFrow.add(a.toString()); } } } } dbf.writeRecord(DBFrow); } dbf.close(); }
/** * return a single geometry collection <br> * result.GeometryN(i) = the i-th feature in the FeatureCollection<br> * All the geometry types will be the same type (ie. all polygons) - or they will be set to<br> * NULL geometries<br> * <br> * GeometryN(i) = {Multipoint,Multilinestring, or Multipolygon)<br> * * @param fc feature collection to make homogeneous */ public GeometryCollection makeSHAPEGeometryCollection(FeatureCollection fc) throws Exception { GeometryCollection result; Geometry[] allGeoms = new Geometry[fc.size()]; int geomtype = findBestGeometryType(fc); if (geomtype == 0) { throw new Exception( "Could not determine shapefile type - data is either all GeometryCollections or empty"); } List features = fc.getFeatures(); for (int t = 0; t < features.size(); t++) { Geometry geom; geom = ((Feature) features.get(t)).getGeometry(); switch (geomtype) { case 1: // point if ((geom instanceof Point)) { // good! Point[] p = new Point[1]; p[0] = (Point) geom; allGeoms[t] = new MultiPoint(p, new PrecisionModel(), 0); } else if (geom instanceof MultiPoint) { allGeoms[t] = geom; } else { allGeoms[t] = new MultiPoint(null, new PrecisionModel(), 0); } break; case 2: // line if ((geom instanceof LineString)) { LineString[] l = new LineString[1]; l[0] = (LineString) geom; allGeoms[t] = new MultiLineString(l, new PrecisionModel(), 0); } else if (geom instanceof MultiLineString) { allGeoms[t] = geom; } else { allGeoms[t] = new MultiLineString(null, new PrecisionModel(), 0); } break; case 3: // polygon if (geom instanceof Polygon) { // good! Polygon[] p = new Polygon[1]; p[0] = (Polygon) geom; allGeoms[t] = makeGoodSHAPEMultiPolygon(new MultiPolygon(p, new PrecisionModel(), 0)); } else if (geom instanceof MultiPolygon) { allGeoms[t] = makeGoodSHAPEMultiPolygon((MultiPolygon) geom); } else { allGeoms[t] = new MultiPolygon(null, new PrecisionModel(), 0); } break; } } result = new GeometryCollection(allGeoms, new PrecisionModel(), 0); return result; }
/** * Adds a hole to the polygon formed by this ring. * * @param hole the {@link LinearRing} forming the hole. */ public void addHole(LinearRing hole) { if (holes == null) holes = new ArrayList(); holes.add(hole); }