Geometry poly = jsonFeature.getGeometry().getGeometryN(i); if (poly instanceof org.locationtech.jts.geom.Polygon) borders.add(Polygon.create((org.locationtech.jts.geom.Polygon) poly));
private static void writeGeometryCollection(Geometry collection, DynamicSliceOutput output) { output.appendByte(GeometrySerializationType.GEOMETRY_COLLECTION.code()); for (int geometryIndex = 0; geometryIndex < collection.getNumGeometries(); geometryIndex++) { Geometry geometry = collection.getGeometryN(geometryIndex); int startPosition = output.size(); // leave 4 bytes for the shape length output.appendInt(0); writeGeometry(geometry, output); int endPosition = output.size(); int length = endPosition - startPosition - Integer.BYTES; output.getUnderlyingSlice().setInt(startPosition, length); } }
public Geometry getGeometryN(int n) { return geometry.getGeometryN(n); }
int numPoints = geometry.getNumPoints(); for (int i = 0; i < numGeometries; i++) { Polygon polygon = (Polygon) geometry.getGeometryN(i); if (polygon.getNumPoints() > 0) { numParts += polygon.getNumInteriorRing() + 1; int currentPoint = 0; for (int i = 0; i < numGeometries; i++) { Polygon polygon = (Polygon) geometry.getGeometryN(i);
private static void writePolyline(Geometry geometry, SliceOutput output, boolean multitype) { int numParts; int numPoints = geometry.getNumPoints(); if (multitype) { numParts = geometry.getNumGeometries(); output.writeByte(GeometrySerializationType.MULTI_LINE_STRING.code()); } else { numParts = numPoints > 0 ? 1 : 0; output.writeByte(GeometrySerializationType.LINE_STRING.code()); } output.writeInt(EsriShapeType.POLYLINE.code); writeEnvelope(geometry, output); output.writeInt(numParts); output.writeInt(numPoints); int partIndex = 0; for (int i = 0; i < numParts; i++) { output.writeInt(partIndex); partIndex += geometry.getGeometryN(i).getNumPoints(); } writeCoordinates(geometry.getCoordinates(), output); }
/** * Set a synthetic gml:id on each child of a multigeometry. If the multigeometry has no gml:id, * this method has no effect. The synthetic gml:id of each child is constructed from that of the * parent by appending "." and then an integer starting from one for the first child. * * @param multiGeometry parent multigeometry containing the children to be modified */ static void setChildIDs(Geometry multiGeometry) { String id = getID(multiGeometry); if (id != null) { for (int i = 0; i < multiGeometry.getNumGeometries(); i++) { StringBuilder builder = new StringBuilder(id); builder.append("."); // separator builder.append(i + 1); // synthetic gml:id suffix one-based GML2EncodingUtils.setID(multiGeometry.getGeometryN(i), builder.toString()); } } }
public static ReferencedEnvelope reprojectEnvelope( ReferencedEnvelope sourceEnvelope, CoordinateReferenceSystem targetCRS, ReferencedEnvelope targetReferenceEnvelope) throws FactoryException, TransformException { Geometry reprojected = Utils.reprojectEnvelopeToGeometry( sourceEnvelope, targetCRS, targetReferenceEnvelope); if (reprojected == null) { return new ReferencedEnvelope(targetCRS); } else { if (reprojected.getNumGeometries() > 1) { return new ReferencedEnvelope( reprojected.getGeometryN(0).getEnvelopeInternal(), targetCRS); } else { return new ReferencedEnvelope(reprojected.getEnvelopeInternal(), targetCRS); } } }
private static Geometry smoothGeometryCollection( GeometryFactory factory, GeometrySmoother smoother, Geometry geom, double fit) { final int N = geom.getNumGeometries(); Geometry[] smoothed = new Geometry[N]; for (int i = 0; i < N; i++) { smoothed[i] = smooth(geom.getGeometryN(i), fit, factory, smoother); } return factory.createGeometryCollection(smoothed); }
private static Geometry smoothMultiPolygon( GeometryFactory factory, GeometrySmoother smoother, Geometry geom, double fit) { final int N = geom.getNumGeometries(); Polygon[] smoothed = new Polygon[N]; for (int i = 0; i < N; i++) { smoothed[i] = smoother.smooth((Polygon) geom.getGeometryN(i), fit); } return factory.createMultiPolygon(smoothed); }
private static Geometry smoothMultiLineString( GeometryFactory factory, GeometrySmoother smoother, Geometry geom, double fit) { final int N = geom.getNumGeometries(); LineString[] smoothed = new LineString[N]; for (int i = 0; i < N; i++) { smoothed[i] = (LineString) smoothLineString(factory, smoother, geom.getGeometryN(i), fit); } return factory.createMultiLineString(smoothed); }
public Object parse(ElementInstance instance, Node node, Object value) throws Exception { // <element maxOccurs="unbounded" minOccurs="0" ref="gml:curveMember"/> List<Geometry> curveMemberList = node.getChildValues("curveMember"); // <element minOccurs="0" ref="gml:curveMembers"/> Geometry curveMembers = (Geometry) node.getChildValue("curveMembers"); List<LineString> lineStrings = new ArrayList<LineString>(); if (curveMemberList != null) { for (Geometry curveMember : curveMemberList) { for (int i = 0; i < curveMember.getNumGeometries(); i++) { LineString lineString = (LineString) curveMember.getGeometryN(i); lineStrings.add(lineString); } } } if (curveMembers != null) { for (int i = 0; i < curveMembers.getNumGeometries(); i++) { LineString lineString = (LineString) curveMembers.getGeometryN(i); lineStrings.add(lineString); } } return gf.createMultiLineString(GeometryFactory.toLineStringArray(lineStrings)); }
/** * Adds the geometries into the collection by recursively splitting apart geometry collections, * so that geoms will contains only simple geometries. * * @param geoms * @param geometry * @param geomType * @return the geometry type that all geometries added to the collection conform to. Worst case * it's going to be Geometry.class */ private Class accumulate(List<Geometry> geoms, Geometry geometry, Class geomType) { Class gtype = null; for (int i = 0; i < geometry.getNumGeometries(); i++) { Geometry g = geometry.getGeometryN(i); if (g instanceof GeometryCollection) { gtype = accumulate(geoms, g, geomType); } else { if (renderingEnvelope.intersects(g.getEnvelopeInternal())) { geoms.add(g); gtype = g.getClass(); } } if (gtype == null) { gtype = g.getClass(); } else if (geomType != null && !g.getClass().equals(geomType)) { gtype = Geometry.class; } } return gtype; }
LineString gg = (LineString) g.getGeometryN(t); lines.add(gg); int count = 0; for (int t = 0; t < g.getNumGeometries(); t++) { count += accumulateLineStrings(g.getGeometryN(t), lines);
protected void encodeMembers(Geometry geometry, GMLWriter handler, String gmlId) throws SAXException, Exception { for (int i = 0; i < geometry.getNumGeometries(); i++) { handler.startElement(member, null); LineString line = (LineString) geometry.getGeometryN(i); String childId = gmlId + "." + (i + 1); if (curveEncoding && line instanceof CurvedGeometry) { ce.encode(line, null, handler, childId); } else if (line instanceof LinearRing) { lre.encode(line, null, handler, childId); } else { lse.encode(line, null, handler, childId); } handler.endElement(member); } } }
@DescribeProcess(title = "Split Polygon", description = "Splits a polygon by a linestring") @DescribeResult(description = "The collection of split polygons") public static Geometry splitPolygon( @DescribeParameter(name = "polygon", description = "Polygon to split") Geometry polygon, @DescribeParameter(name = "line", description = "Linestring to split by") LineString line) { Geometry nodedLinework = polygon.getBoundary().union(line); Geometry polys = polygonize(nodedLinework); // Only keep polygons which are inside the input List<Polygon> output = new ArrayList<Polygon>(); for (int i = 0; i < polys.getNumGeometries(); i++) { Polygon candpoly = (Polygon) polys.getGeometryN(i); // use interior point to test for inclusion in parent if (polygon.contains(candpoly.getInteriorPoint())) { output.add(candpoly); } } return polygon.getFactory() .createGeometryCollection(GeometryFactory.toGeometryArray(output)); }
public void testEncode() throws Exception { Geometry geometry = GML3MockData.multiLineString(); GML3EncodingUtils.setID(geometry, "geometry"); Document dom = encode(geometry, GML.MultiLineString); // print(dom); assertEquals("geometry", getID(dom.getDocumentElement())); assertEquals(2, dom.getElementsByTagNameNS(GML.NAMESPACE, "lineStringMember").getLength()); NodeList children = dom.getElementsByTagNameNS(GML.NAMESPACE, GML.LineString.getLocalPart()); assertEquals(2, children.getLength()); assertEquals("geometry.1", getID(children.item(0))); assertEquals("geometry.2", getID(children.item(1))); checkDimension(dom, GML.MultiLineString.getLocalPart(), 2); checkDimension(dom, GML.LineString.getLocalPart(), 2); checkPosListOrdinates(dom, 2 * geometry.getGeometryN(0).getNumPoints()); } }
@Test public void testUTMDatelineWrapping() throws Exception { CoordinateReferenceSystem crs = CRS.decode("EPSG:32601", true); ReferencedEnvelope re = new ReferencedEnvelope(300000, 409800, 5890200, 6000000, crs); MathTransform mt = CRS.findMathTransform(crs, WGS84); Geometry geom = JTS.toGeometry(re); ReferencedEnvelope targetReferenceEnvelope = new ReferencedEnvelope(-180, 180, -90, 90, WGS84); ProjectionHandler ph = ProjectionHandlerFinder.getHandler(targetReferenceEnvelope, crs, true); Geometry preProcessed = ph.preProcess(geom); Geometry transformed = JTS.transform(preProcessed, mt); Geometry postProcessed = ph.postProcess(mt.inverse(), transformed); // sits across the dateline and it's "small" (used to cover the entire planet) Envelope ppEnvelope = postProcessed.getGeometryN(0).getEnvelopeInternal(); assertTrue(ppEnvelope.contains(180, 54)); // the original width is 109km, at this latitude one degree of longitude is only 65km assertEquals(1.7, ppEnvelope.getWidth(), 0.1); }
/** Given {@code geoms} which has already been checked for being in world * bounds, return the minimal longitude range of the bounding box. */ protected Rectangle computeGeoBBox(Geometry geoms) { final Envelope env = geoms.getEnvelopeInternal();//for minY & maxY (simple) if (ctx.isGeo() && env.getWidth() > 180 && geoms.getNumGeometries() > 1) { // This is ShapeCollection's bbox algorithm BBoxCalculator bboxCalc = new BBoxCalculator(ctx); for (int i = 0; i < geoms.getNumGeometries(); i++ ) { Envelope envI = geoms.getGeometryN(i).getEnvelopeInternal(); bboxCalc.expandXRange(envI.getMinX(), envI.getMaxX()); if (bboxCalc.doesXWorldWrap()) break; // can't grow any bigger } return new RectangleImpl(bboxCalc.getMinX(), bboxCalc.getMaxX(), env.getMinY(), env.getMaxY(), ctx); } else { return new RectangleImpl(env.getMinX(), env.getMaxX(), env.getMinY(), env.getMaxY(), ctx); } }
assertTrue(geoms[0] == smoothed.getGeometryN(0)); Geometry g = smoothed.getGeometryN(1); assertTrue(g instanceof LineString); g = smoothed.getGeometryN(2); assertTrue(g instanceof Polygon);
assertEquals(numGeometries, 3); for (int i = 0; i < numGeometries; i++) { assertTrue(preProcessed.getGeometryN(i) instanceof Polygon);