Polygon maxPolygon = null; for (int j = 0; j < multiPolygon.getNumGeometries(); j++) { Polygon polygon = (Polygon) multiPolygon.getGeometryN(j); if (polygon.getNumPoints() > maxPoints) { maxPoints = polygon.getNumPoints();
@Override public Object getProperty(Object object, QName name) throws Exception { if ("patches".equals(name.getLocalPart())) { MultiPolygon multiSurface = (MultiPolygon) object; Polygon[] members = new Polygon[multiSurface.getNumGeometries()]; for (int i = 0; i < members.length; i++) { members[i] = (Polygon) multiSurface.getGeometryN(i); } return members; } return null; } }
@Override public Object getProperty(Object object, QName name) throws Exception { if ("_Surface".equals(name.getLocalPart()) || "AbstractSurface".equals(name.getLocalPart())) { MultiPolygon multiPolygon = (MultiPolygon) object; // this MultiPolygon consists of a single Polygon wrapped in a MultiPolygon: return multiPolygon.getGeometryN(0); } return super.getProperty(object, name); }
private static List<org.locationtech.jts.geom.Polygon> unrollGeometries(MultiPolygon mp) { List<org.locationtech.jts.geom.Polygon> ret = new ArrayList<Polygon>(); for (int i = 0; i < mp.getNumGeometries(); i++) { org.locationtech.jts.geom.Polygon g = (org.locationtech.jts.geom.Polygon) mp.getGeometryN(i); ret.add(g); } return ret; } }
public Object getProperty(Object object, QName name) throws Exception { if (GML.polygonMember.equals(name)) { MultiPolygon multiPolygon = (MultiPolygon) object; Polygon[] members = new Polygon[multiPolygon.getNumGeometries()]; for (int i = 0; i < members.length; i++) { members[i] = (Polygon) multiPolygon.getGeometryN(i); } GML3EncodingUtils.setChildIDs(multiPolygon); return members; } return null; }
public Object getProperty(Object object, QName name) throws Exception { if ("surfaceMember".equals(name.getLocalPart())) { MultiPolygon multiSurface = (MultiPolygon) object; Polygon[] members = new Polygon[multiSurface.getNumGeometries()]; for (int i = 0; i < members.length; i++) { members[i] = (Polygon) multiSurface.getGeometryN(i); } GML3EncodingUtils.setChildIDs(multiSurface); return members; } return null; } }
/** * TODO: Encode more then 1 * * @param line * @param srid */ private static String toSDOGeom(MultiPolygon polygon, int srid) { if (polygon.getNumGeometries() == 1) { return toSDOGeom(polygon.getGeometryN(0), srid); } throw new UnsupportedOperationException("Cannot encode MultiPolygon (yet)"); } /**
/** * Returns the perimeter of a MultiPolygon. * * @param multiPolygon the MultiPolygon for which the perimeter is calculated. * @return Total perimeter of all polygons in the multiPolygon. */ protected double getperimeter(MultiPolygon multiPolygon) { double perimeter = 0.0d; int numberOfGeometries = multiPolygon.getNumGeometries(); for (int i = 0; i < numberOfGeometries; i++) { perimeter += getPerimeter(multiPolygon.getGeometryN(i)); } return perimeter; }
/** * Returns the area of a MultiPolygon. * * @param multiPolygon the MultiPolygon for which the area is calculated. * @return Total area of all polygons in multiPolygon. */ protected double getArea(MultiPolygon multiPolygon) { double area = 0.0d; int numberOfGeometries = multiPolygon.getNumGeometries(); for (int i = 0; i < numberOfGeometries; i++) { area += getArea(multiPolygon.getGeometryN(i)); } return area; }
private double pointToPolygonDist(Point point, MultiPolygon polygon) { boolean inside = false; double minDistSq = Double.POSITIVE_INFINITY; for (int k = 0; k < polygon.getNumGeometries(); k++) { Coordinate[] ring = polygon.getGeometryN(k).getCoordinates(); for (int i = 0, len = ring.length, j = len - 1; i < len; j = i++) { Coordinate a = ring[i]; Coordinate b = ring[j]; if ((a.y > y != b.y > y) && (x < (b.x - a.x) * (y - a.y) / (b.y - a.y) + a.x)) inside = !inside; minDistSq = Math.min(minDistSq, getSegDistSq(x, y, a, b)); } } // Points outside has a negative distance and thus will be weighted down later. return (inside ? 1 : -1) * Math.sqrt(minDistSq); } // get squared distance from a point to a segment
private static void addCoordinates(List list, MultiPolygon polys) { for (int i = 0; i < polys.getNumGeometries(); i++) { Geometry geometryN = polys.getGeometryN(i); if (geometryN != null && !geometryN.isEmpty()) addCoordinates(list, (Polygon) geometryN); } }
public DBObject toObject(MultiPolygon mp) { if (opportunisticMultiGeometryCoversion && mp.getNumGeometries() == 1) { return toObject((Polygon) mp.getGeometryN(0)); } List<Object> l = new BasicDBList(); for (int i = 0; i < mp.getNumGeometries(); i++) { l.add(toList(((Polygon) mp.getGeometryN(i)))); } return BasicDBObjectBuilder.start().add("type", "MultiPolygon").add("coordinates", l).get(); }
/** * Like makeGoodShapePolygon, but applied towards a multi polygon. * * @param mp The MultiPolygon to "niceify". * @return The "nicified" MultiPolygon. */ public static final MultiPolygon makeGoodShapeMultiPolygon(MultiPolygon mp) { MultiPolygon result; Polygon[] ps = new Polygon[mp.getNumGeometries()]; // check each sub-polygon for (int t = 0; t < mp.getNumGeometries(); t++) { ps[t] = makeGoodShapePolygon((Polygon) mp.getGeometryN(t)); } result = mp.getFactory().createMultiPolygon(ps); return result; }
@Override public void encode(MultiPolygon geometry, AttributesImpl atts, GMLWriter handler) throws Exception { handler.startElement(multiPolygon, atts); for (int i = 0; i < geometry.getNumGeometries(); i++) { handler.startElement(polygonMember, null); pe.encode((Polygon) geometry.getGeometryN(i), null, handler); handler.endElement(polygonMember); } handler.endElement(multiPolygon); } }
@Override public void encode(MultiPolygon geometry, AttributesImpl atts, GMLWriter handler, String gmlId) throws Exception { atts = cloneWithGmlId(atts, gmlId); handler.startElement(multiSurface, atts); for (int i = 0; i < geometry.getNumGeometries(); i++) { handler.startElement(surfaceMember, null); pe.encode((Polygon) geometry.getGeometryN(i), null, handler, gmlId + "." + (i + 1)); handler.endElement(surfaceMember); } handler.endElement(multiSurface); } }
private static void addElemInfo( List elemInfoList, MultiPolygon polys, final int STARTING_OFFSET, final int GTYPE) { Polygon poly; int offset = STARTING_OFFSET; int LEN = D(GTYPE) + L(GTYPE); for (int i = 0; i < polys.getNumGeometries(); i++) { poly = (Polygon) polys.getGeometryN(i); if (poly != null && !poly.isEmpty()) { addElemInfo(elemInfoList, poly, offset, GTYPE); if (isRectangle(poly)) { offset += (2 * LEN); } else { offset += (poly.getNumPoints() * LEN); } } } }
public void testMultiSurface() throws Exception { GMLConfiguration gml = new GMLConfiguration(true); Parser p = new Parser(gml); Object multiSurface = p.parse(getClass().getResourceAsStream("multisurface.xml")); assertTrue(multiSurface instanceof MultiPolygon); MultiPolygon mp = (MultiPolygon) multiSurface; assertEquals(1, mp.getNumGeometries()); Polygon poly = (Polygon) mp.getGeometryN(0); assertTrue(poly.getExteriorRing() instanceof CompoundRing); } }
static void encode(Element e, MultiPolygon g, PrintHandler output) throws OperationNotSupportedException, IOException { if ((g == null) || (g.getNumGeometries() <= 0)) { throw new IOException("Bad MultiPolygon Data"); } AttributesImpl ai = getSrsNameAttribute(g); if (e == null) { output.startElement(GMLSchema.NAMESPACE, "MultiPolygon", ai); } else { output.startElement(e.getNamespace(), e.getName(), ai); } for (int i = 0; i < g.getNumGeometries(); i++) { output.startElement(GMLSchema.NAMESPACE, "polygonMember", null); encode(null, (Polygon) g.getGeometryN(i), output); output.endElement(GMLSchema.NAMESPACE, "polygonMember"); } if (e == null) { output.endElement(GMLSchema.NAMESPACE, "MultiPolygon"); } else { output.endElement(e.getNamespace(), e.getName()); } }
public void testParse() throws Exception { GML3MockData.surface(document, document); MultiPolygon surface = (MultiPolygon) parse(); assertNotNull(surface); assertEquals(1, surface.getNumGeometries()); Polygon p = (Polygon) surface.getGeometryN(0); assertEquals(1, p.getNumInteriorRing()); } }
public void test3D() throws Exception { GML3MockData.multiPolygon3D(document, document); MultiPolygon multiPolygon = (MultiPolygon) parse(); assertNotNull(multiPolygon); assertEquals(2, multiPolygon.getNumGeometries()); Polygon polygon = (Polygon) multiPolygon.getGeometryN(0); LineString exterior = polygon.getExteriorRing(); assertTrue(new Coordinate(1d, 2d, 10d).equals3D(exterior.getCoordinateN(0))); LineString interior = polygon.getInteriorRingN(0); assertTrue(new Coordinate(1d, 2d, 10d).equals3D(interior.getCoordinateN(0))); }