/** * Transforms a LinearRing. The transformation of a LinearRing may result in a coordinate sequence * which does not form a structurally valid ring (i.e. a degnerate ring of 3 or fewer points). In * this case a LineString is returned. Subclasses may wish to override this method and check for * this situation (e.g. a subclass may choose to eliminate degenerate linear rings) * * @param geom the ring to simplify * @param parent the parent geometry * @return a LinearRing if the transformation resulted in a structurally valid ring * @return a LineString if the transformation caused the LinearRing to collapse to 3 or fewer * points */ protected Geometry transformLinearRing(LinearRing geom, Geometry parent) { CoordinateSequence seq = transformCoordinates(geom.getCoordinateSequence(), geom); if (seq == null) return factory.createLinearRing((CoordinateSequence) null); int seqSize = seq.size(); // ensure a valid LinearRing if (seqSize > 0 && seqSize < 4 && !preserveType) return factory.createLineString(seq); return factory.createLinearRing(seq); }
Polygon createPolygon(List list) { LinearRing shell = gf.createLinearRing(coordseq((List) ensureSize(list, 1).get(0))); LinearRing[] holes = list.size() > 1 ? new LinearRing[list.size() - 1] : null; for (int i = 1; i < list.size(); i++) { holes[i - 1] = gf.createLinearRing(coordseq((List) list.get(i))); } return gf.createPolygon(shell, holes); }
/** We must be able to transform specialized collections to GeometryCollectionValues */ @Test public void testGeometryCollectionConversions() throws Exception { Value val = ValueFactory.createValue( gf.createMultiPoint( new Coordinate[] {new Coordinate(0, 0, 0), new Coordinate(1, 2, 3)})); assertTrue(val instanceof DefaultMultiPointValue); Value val2 = val.toType(Type.GEOMETRYCOLLECTION); assertTrue(val2 instanceof DefaultGeometryCollectionValue); // Let's be sure it's not a GeometryCollection just thanks to the inheritance assertFalse(val2 instanceof DefaultMultiPointValue); val = ValueFactory.createValue( gf.createMultiLineString( new LineString[] { gf.createLineString( new Coordinate[] {new Coordinate(0, 0, 0), new Coordinate(4, 2, 3)}), gf.createLineString( new Coordinate[] {new Coordinate(5, 6, 9), new Coordinate(5, 7, 1)}) })); assertTrue(val instanceof DefaultMultiLineStringValue); val2 = val.toType(Type.GEOMETRYCOLLECTION); assertTrue(val2 instanceof DefaultGeometryCollectionValue); // Let's be sure it's not a GeometryCollection just thanks to the inheritance assertFalse(val2 instanceof DefaultMultiPointValue); val = ValueFactory.createValue( gf.createMultiPolygon( new Polygon[] { gf.createPolygon( gf.createLinearRing( new Coordinate[] { new Coordinate(0, 3, 0), new Coordinate(9, 0, 0), new Coordinate(8, 7, 0), new Coordinate(0, 3, 0) }), null), gf.createPolygon( gf.createLinearRing( new Coordinate[] { new Coordinate(10, 3, 0), new Coordinate(9, 0, 0), new Coordinate(8, 7, 0), new Coordinate(10, 3, 0) }), null) })); assertTrue(val instanceof DefaultMultiPolygonValue); val2 = val.toType(Type.GEOMETRYCOLLECTION); assertTrue(val2 instanceof DefaultGeometryCollectionValue); // Let's be sure it's not a GeometryCollection just thanks to the inheritance assertFalse(val2 instanceof DefaultMultiPointValue); }
/** decimates JTS geometries. */ public final Geometry decimate(Geometry geom) { GeometryFactory gFac = new GeometryFactory(geom.getPrecisionModel(), geom.getSRID()); if (spanx == -1) return geom; if (geom instanceof MultiPoint) { // TODO check geometry and if its bbox is too small turn it into a 1 // point geom return geom; } if (geom instanceof GeometryCollection) { // TODO check geometry and if its bbox is too small turn it into a // 1-2 point geom // takes a bit of work because the geometry will need to be // recreated. GeometryCollection collection = (GeometryCollection) geom; Geometry[] result = new Geometry[collection.getDimension()]; final int numGeometries = collection.getNumGeometries(); for (int i = 0; i < numGeometries; i++) { result[i] = decimate(collection.getGeometryN(i)); } return gFac.createGeometryCollection(result); } else if (geom instanceof LineString) { LineString line = (LineString) geom; CoordinateSequence seq = (CoordinateSequence) line.getCoordinateSequence(); LiteCoordinateSequence lseq = new LiteCoordinateSequence(seq.toCoordinateArray()); if (decimateOnEnvelope(line, lseq)) { if (lseq.size() >= 2) return gFac.createLineString(lseq); } if (lseq.size() >= 2) return gFac.createLineString(decimate(lseq)); return null; } else if (geom instanceof Polygon) { Polygon line = (Polygon) geom; Coordinate[] exterior = decimate(line.getExteriorRing()).getCoordinates(); forceClosed(exterior); if (exterior.length > 3) { LinearRing ring = gFac.createLinearRing(exterior); final int numRings = line.getNumInteriorRing(); List<LinearRing> rings = new ArrayList<LinearRing>(); for (int i = 0; i < numRings; i++) { Coordinate[] interior = decimate(line.getInteriorRingN(i)).getCoordinates(); forceClosed(interior); if (interior.length > 3) rings.add(gFac.createLinearRing(interior)); } return gFac.createPolygon(ring, rings.toArray(new LinearRing[] {})); } return null; } return geom; }
/** * Test created to check that we effectively retrieve a good representation of empty * multipolygons. indeed, a NullPointerException used to happen... * * @throws Exception */ @Test public void testGeometryCollectionStringRepresentation() throws Exception { GeometryCollection mp = gf.createMultiPolygon(new Polygon[] {}); Value val = ValueFactory.createValue(mp); String str = val.toString(); assertEquals(str, "MULTIPOLYGON EMPTY"); Polygon poly = gf.createPolygon(gf.createLinearRing(new Coordinate[] {}), new LinearRing[] {}); assertTrue(poly.isEmpty()); mp = gf.createMultiPolygon( new Polygon[] { poly, }); val = ValueFactory.createValue(mp); str = val.toString(); assertNotNull(str); Polygon polyBis = gf.createPolygon( gf.createLinearRing( new Coordinate[] { new Coordinate(0, 0, 0), new Coordinate(1, 1, 0), new Coordinate(3, 4, 0), new Coordinate(0, 0, 0), }), new LinearRing[] {}); mp = gf.createMultiPolygon(new Polygon[] {poly, polyBis}); val = ValueFactory.createValue(mp); str = val.toString(); assertNotNull(str); GeometryCollection coll = gf.createGeometryCollection( new Geometry[] { gf.createPolygon(gf.createLinearRing(new Coordinate[] {}), new LinearRing[] {}), gf.createPolygon( gf.createLinearRing( new Coordinate[] { new Coordinate(0, 0, 0), new Coordinate(1, 1, 0), new Coordinate(3, 4, 0), new Coordinate(0, 0, 0), }), new LinearRing[] {}) }); mp = gf.createGeometryCollection(new Geometry[] {poly, coll, polyBis}); val = ValueFactory.createValue(mp); str = val.toString(); assertNotNull(str); }
/** * Convert a org.geojson.Xxxx geometry to a JTS geometry. Only support Point, Polygon, * MultiPolygon, LineString and MultiLineString for now. * * @param geoJsonGeom * @return The equivalent JTS geometry. * @throws UnsupportedGeometryException */ public static Geometry convertGeoJsonToJtsGeometry(GeoJsonObject geoJsonGeom) throws UnsupportedGeometryException { if (geoJsonGeom instanceof org.geojson.Point) { org.geojson.Point geoJsonPoint = (org.geojson.Point) geoJsonGeom; return gf.createPoint( new Coordinate( geoJsonPoint.getCoordinates().getLongitude(), geoJsonPoint.getCoordinates().getLatitude())); } else if (geoJsonGeom instanceof org.geojson.Polygon) { org.geojson.Polygon geoJsonPolygon = (org.geojson.Polygon) geoJsonGeom; LinearRing shell = gf.createLinearRing(convertPath(geoJsonPolygon.getExteriorRing())); LinearRing[] holes = new LinearRing[geoJsonPolygon.getInteriorRings().size()]; int i = 0; for (List<LngLatAlt> hole : geoJsonPolygon.getInteriorRings()) { holes[i++] = gf.createLinearRing(convertPath(hole)); } return gf.createPolygon(shell, holes); } else if (geoJsonGeom instanceof org.geojson.MultiPolygon) { org.geojson.MultiPolygon geoJsonMultiPolygon = (org.geojson.MultiPolygon) geoJsonGeom; Polygon[] jtsPolygons = new Polygon[geoJsonMultiPolygon.getCoordinates().size()]; int i = 0; for (List<List<LngLatAlt>> geoJsonRings : geoJsonMultiPolygon.getCoordinates()) { org.geojson.Polygon geoJsonPoly = new org.geojson.Polygon(); for (List<LngLatAlt> geoJsonRing : geoJsonRings) geoJsonPoly.add(geoJsonRing); jtsPolygons[i++] = (Polygon) convertGeoJsonToJtsGeometry(geoJsonPoly); } return gf.createMultiPolygon(jtsPolygons); } else if (geoJsonGeom instanceof org.geojson.LineString) { org.geojson.LineString geoJsonLineString = (org.geojson.LineString) geoJsonGeom; return gf.createLineString(convertPath(geoJsonLineString.getCoordinates())); } else if (geoJsonGeom instanceof org.geojson.MultiLineString) { org.geojson.MultiLineString geoJsonMultiLineString = (org.geojson.MultiLineString) geoJsonGeom; LineString[] jtsLineStrings = new LineString[geoJsonMultiLineString.getCoordinates().size()]; int i = 0; for (List<LngLatAlt> geoJsonPath : geoJsonMultiLineString.getCoordinates()) { org.geojson.LineString geoJsonLineString = new org.geojson.LineString(geoJsonPath.toArray(new LngLatAlt[geoJsonPath.size()])); jtsLineStrings[i++] = (LineString) convertGeoJsonToJtsGeometry(geoJsonLineString); } return gf.createMultiLineString(jtsLineStrings); } throw new UnsupportedGeometryException(geoJsonGeom.getClass().toString()); }
protected Polygon decodePolygonCoordinates(JsonNode coordinates, GeometryFactory fac) throws GeoJSONException { if (!coordinates.isArray()) { throw new GeoJSONException("expected array"); } if (coordinates.size() < 1) { throw new GeoJSONException("missing polygon shell"); } LinearRing shell = fac.createLinearRing(decodeCoordinates(coordinates.get(0))); LinearRing[] holes = new LinearRing[coordinates.size() - 1]; for (int i = 1; i < coordinates.size(); ++i) { holes[i - 1] = fac.createLinearRing(decodeCoordinates(coordinates.get(i))); } return fac.createPolygon(shell, holes); }
protected Polygon toPolygon(ReferencedEnvelope env2) { ReferencedEnvelope env = env2; if (env == null) env = new ReferencedEnvelope(-180, 180, -90, 90, DefaultGeographicCRS.WGS84); AttributeDescriptor att = mapper.getSchema().getDescriptor(mapper.getBounds()); CoordinateReferenceSystem crs = null; if (att instanceof GeometryDescriptor) crs = ((GeometryDescriptor) att).getCoordinateReferenceSystem(); if (crs == null) crs = DefaultGeographicCRS.WGS84; GeometryFactory factory = new GeometryFactory(); try { env = env.transform(crs, true); } catch (Exception e) { IssuesActivator.log("", e); // $NON-NLS-1$ } return factory.createPolygon( factory.createLinearRing( new Coordinate[] { new Coordinate(env.getMinX(), env.getMinY()), new Coordinate(env.getMaxX(), env.getMinY()), new Coordinate(env.getMaxX(), env.getMaxY()), new Coordinate(env.getMinX(), env.getMaxY()), new Coordinate(env.getMinX(), env.getMinY()), }), new LinearRing[0]); }
private SimpleFeature createPolyFeature( int startx, int starty, int width, int height, String name, CoordinateReferenceSystem crs, GeometryFactory geomFac) throws Exception { Coordinate[] c = new Coordinate[] { new Coordinate(startx, starty), new Coordinate(startx + width, starty), new Coordinate(startx + width, starty + height), new Coordinate(startx, starty), }; LinearRing line = geomFac.createLinearRing(c); Polygon poly = geomFac.createPolygon(line, null); SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); if (crs != null) builder.add("polygon", poly.getClass(), crs); else builder.add("centre", line.getClass()); builder.add("name", String.class); builder.setName(Rendering2DTest.POLYGON); SimpleFeatureType type = builder.buildFeatureType(); return SimpleFeatureBuilder.build(type, new Object[] {poly, name}, null); }
private LinearRing[] parseInteriorRings(JsonNode arrayOfRings) { LinearRing rings[] = new LinearRing[arrayOfRings.size() - 1]; for (int i = 1; i < arrayOfRings.size(); ++i) { rings[i - 1] = gf.createLinearRing(parseLineString(arrayOfRings.get(i))); } return rings; }
private void updateGeometry() { final List<Geometry> geoms = new ArrayList<>(); if (coords.size() == 1) { // single point final Geometry geom = GEOMETRY_FACTORY.createPoint(coords.get(0)); JTS.setCRS(geom, map.getCanvas().getObjectiveCRS2D()); geoms.add(geom); } else if (coords.size() == 2) { // line final Geometry geom = GEOMETRY_FACTORY.createLineString(coords.toArray(new Coordinate[coords.size()])); JTS.setCRS(geom, map.getCanvas().getObjectiveCRS2D()); geoms.add(geom); } else if (coords.size() > 2) { // polygon final Coordinate[] ringCoords = coords.toArray(new Coordinate[coords.size() + 1]); ringCoords[coords.size()] = coords.get(0); final LinearRing ring = GEOMETRY_FACTORY.createLinearRing(ringCoords); final Geometry geom = GEOMETRY_FACTORY.createPolygon(ring, new LinearRing[0]); JTS.setCRS(geom, map.getCanvas().getObjectiveCRS2D()); geoms.add(geom); } layer.getGeometries().setAll(geoms); if (geoms.isEmpty()) { uiArea.setText("-"); } else { uiArea.setText( NumberFormat.getNumberInstance() .format( MeasureUtilities.calculateArea( geoms.get(0), map.getCanvas().getObjectiveCRS2D(), uiUnit.getValue()))); } }
@Test public void testLenght() throws NoSuchIdentifierException, ProcessException { GeometryFactory fact = new GeometryFactory(); // Inputs first final LinearRing ring = fact.createLinearRing( new Coordinate[] { new Coordinate(0.0, 0.0), new Coordinate(0.0, 10.0), new Coordinate(5.0, 10.0), new Coordinate(5.0, 0.0), new Coordinate(0.0, 0.0) }); final Geometry geom1 = fact.createPolygon(ring, null); // Process final ProcessDescriptor desc = ProcessFinder.getProcessDescriptor("jts", "lenght"); final ParameterValueGroup in = desc.getInputDescriptor().createValue(); in.parameter("geom").setValue(geom1); final org.geotoolkit.process.Process proc = desc.createProcess(in); // result final Double result = (Double) proc.call().parameter("result").getValue(); final Double expected = geom1.getLength(); assertTrue(expected.equals(result)); }
@Test public void test3DGeoms() throws Exception { Coordinate[] coords2D = new Coordinate[] { new Coordinate(10, 10, 10), new Coordinate(40, 10, 10), new Coordinate(40, 40, 10), new Coordinate(10, 40, 10), new Coordinate(10, 10, 10), }; Coordinate[] coords3D = new Coordinate[] { new Coordinate(10, 10), new Coordinate(40, 10), new Coordinate(40, 40), new Coordinate(10, 40), new Coordinate(10, 10), }; Value p1 = ValueFactory.createValue(gf.createPoint(new Coordinate(10, 10, 10))); Value p2 = ValueFactory.createValue(gf.createPoint(new Coordinate(10, 10))); checkDifferent(p1, p2); LineString l1 = gf.createLineString(coords2D); p1 = ValueFactory.createValue(l1); LineString l2 = gf.createLineString(coords3D); p2 = ValueFactory.createValue(l2); checkDifferent(p1, p2); p1 = ValueFactory.createValue(gf.createMultiPoint(coords2D)); p2 = ValueFactory.createValue(gf.createMultiPoint(coords3D)); checkDifferent(p1, p2); Polygon pol1 = gf.createPolygon(gf.createLinearRing(coords2D), null); p1 = ValueFactory.createValue(pol1); Polygon pol2 = gf.createPolygon(gf.createLinearRing(coords3D), null); p2 = ValueFactory.createValue(pol2); checkDifferent(p1, p2); p1 = ValueFactory.createValue(gf.createMultiLineString(new LineString[] {l1})); p2 = ValueFactory.createValue(gf.createMultiLineString(new LineString[] {l2})); checkDifferent(p1, p2); p1 = ValueFactory.createValue(gf.createMultiPolygon(new Polygon[] {pol1})); p2 = ValueFactory.createValue(gf.createMultiPolygon(new Polygon[] {pol2})); checkDifferent(p1, p2); }
// public void testEquals2() throws Exception { // Geometry lineString = reader.read("LINESTRING(0 0, 0 50, 50 50, 50 0, 0 0)"); // Geometry geometryCollection = reader.read("GEOMETRYCOLLECTION ( LINESTRING(0 0 , 0 50), " // + "LINESTRING(0 50 , 50 50), " // + "LINESTRING(50 50, 50 0 ), " // + "LINESTRING(50 0 , 0 0 ) // )"); // assertTrue(lineString.equals(geometryCollection)); // } public void testEqualsExactForLinearRings() throws Exception { LinearRing x = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(0, 0), new Coordinate(100, 0), new Coordinate(100, 100), new Coordinate(0, 0) }); LinearRing somethingExactlyEqual = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(0, 0), new Coordinate(100, 0), new Coordinate(100, 100), new Coordinate(0, 0) }); LinearRing somethingNotEqualButSameClass = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(0, 0), new Coordinate(100, 0), new Coordinate(100, 555), new Coordinate(0, 0) }); LinearRing sameClassButEmpty = geometryFactory.createLinearRing((CoordinateSequence) null); LinearRing anotherSameClassButEmpty = geometryFactory.createLinearRing((CoordinateSequence) null); CollectionFactory collectionFactory = new CollectionFactory() { public Geometry createCollection(Geometry[] geometries) { return geometryFactory.createMultiLineString( GeometryFactory.toLineStringArray(Arrays.asList(geometries))); } }; doTestEqualsExact( x, somethingExactlyEqual, somethingNotEqualButSameClass, sameClassButEmpty, anotherSameClassButEmpty, collectionFactory); // LineString somethingEqualButNotExactly = geometryFactory.createLineString(new Coordinate[] // { // new Coordinate(0, 0), new Coordinate(100, 0), new Coordinate(100, 100), // new Coordinate(0, 0) }); // // doTestEqualsExact(x, somethingExactlyEqual, somethingEqualButNotExactly, // somethingNotEqualButSameClass); }
public LinearRing nextLinearRing(final Envelope envelope) { LinearRing result; do { final CoordinateList cl = new CoordinateList(nextLineString(envelope).getCoordinates()); cl.closeRing(); result = gf.createLinearRing(cl.toCoordinateArray()); } while (!result.isValid()); return result; }
@Test public void toEnvelope() { Coordinate[] coords = getPolyCoords(); GeometryFactory gf = new GeometryFactory(); Geometry geom = gf.createPolygon(gf.createLinearRing(coords), null); ReferencedEnvelope refEnv = JTS.toEnvelope(geom); assertTrue(geom.getEnvelopeInternal().equals(refEnv)); }
private static LinearRing getLinearRing3D() { return gf.createLinearRing( new Coordinate[] { new Coordinate(0, 2, 0), new Coordinate(10, 2, 0), new Coordinate(110, 22, 0), new Coordinate(10, 62, 240), new Coordinate(0, 2, 0) }); }
private LinearRing createHoleRing(GeometryFactory factory, int offset) { return factory.createLinearRing( new Coordinate[] { new Coordinate(offset + 5, 7), new Coordinate(offset + 8, 7), new Coordinate(offset + 8, 8), new Coordinate(offset + 5, 8), new Coordinate(offset + 5, 7) }); }
private LinearRing createShellRing(GeometryFactory factory, int offset) { return factory.createLinearRing( new Coordinate[] { new Coordinate(offset + 0, 5), new Coordinate(offset + 10, 5), new Coordinate(offset + 10, 10), new Coordinate(offset + 0, 10), new Coordinate(offset + 0, 5) }); }
public static LinearRing getLinearRing() { return gf.createLinearRing( new Coordinate[] { new Coordinate(0, 0), new Coordinate(10, 0), new Coordinate(110, 0), new Coordinate(10, 240), new Coordinate(0, 0) }); }
public void testHeterogeneous() throws Exception { Node node = createNode( gcol, new ElementInstance[] {point1, point2, line1, ring1, poly1}, new Object[] { gf.createPoint(new Coordinate(0, 0)), gf.createPoint(new Coordinate(1, 1)), gf.createLineString(new Coordinate[] {new Coordinate(0, 0), new Coordinate(1, 1)}), gf.createLinearRing( new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1), new Coordinate(2, 2), new Coordinate(0, 0) }), gf.createPolygon( gf.createLinearRing( new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 1), new Coordinate(2, 2), new Coordinate(0, 0) }), null) }, null, null); GMLGeometryCollectionTypeBinding s = (GMLGeometryCollectionTypeBinding) container.getComponentInstanceOfType(GMLGeometryCollectionTypeBinding.class); GeometryCollection gc = (GeometryCollection) s.parse(gcol, node, null); assertNotNull(gc); assertEquals(gc.getNumGeometries(), 5); assertTrue(gc.getGeometryN(0) instanceof Point); assertTrue(gc.getGeometryN(1) instanceof Point); assertTrue(gc.getGeometryN(2) instanceof LineString); assertTrue(gc.getGeometryN(3) instanceof LinearRing); assertTrue(gc.getGeometryN(4) instanceof Polygon); }
/** * Returns this ring as a {@link LinearRing}, or null if an Exception occurs while creating it * (such as a topology problem). Details of problems are written to standard output. */ public LinearRing getRing() { if (ring != null) return ring; getCoordinates(); if (ringPts.length < 3) System.out.println(ringPts); try { ring = factory.createLinearRing(ringPts); } catch (Exception ex) { System.out.println(ringPts); } return ring; }
public void testUnclosedLinearRing() { try { geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 0), new Coordinate(1, 1), new Coordinate(2, 1) }); assertTrue(false); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); } }
@Test public void createComplexGeometryCollectionTest() throws Exception { Coordinate ptc = new Coordinate(10, 20); Point point = geometryFactory.createPoint(ptc); point.setSRID(4326); Coordinate[] lsc = new Coordinate[8]; lsc[0] = new Coordinate(5.0d, 5.0d); lsc[1] = new Coordinate(6.0d, 5.0d); lsc[2] = new Coordinate(6.0d, 6.0d); lsc[3] = new Coordinate(7.0d, 6.0d); lsc[4] = new Coordinate(7.0d, 7.0d); lsc[5] = new Coordinate(8.0d, 7.0d); lsc[6] = new Coordinate(8.0d, 8.0d); lsc[7] = new Coordinate(9.0d, 9.0d); LineString lineString = geometryFactory.createLineString(lsc); lineString.setSRID(4326); Coordinate[] lrc = new Coordinate[10]; lrc[0] = new Coordinate(7, 7); lrc[1] = new Coordinate(6, 9); lrc[2] = new Coordinate(6, 11); lrc[3] = new Coordinate(7, 12); lrc[4] = new Coordinate(9, 11); lrc[5] = new Coordinate(11, 12); lrc[6] = new Coordinate(13, 11); lrc[7] = new Coordinate(13, 9); lrc[8] = new Coordinate(11, 7); lrc[9] = new Coordinate(7, 7); LinearRing linearRing = geometryFactory.createLinearRing(lrc); linearRing.setSRID(4326); Geometry polygon = reader.read( "POLYGON ((35 10, 10 20, 15 40," + " 45 45, 35 10), (20 30, 35 35, 30 20, 20 30))"); polygon.setSRID(4326); Geometry multiPoint = reader.read("MULTIPOINT ((10 40), (40 30), " + "(20 20), (30 10))"); multiPoint.setSRID(4326); Geometry multiPolygon = reader.read( "MULTIPOLYGON (((40 40, 20 45," + " 45 30, 40 40)), ((20 35, 45 20, 30 5, " + "10 10, 10 30, 20 35), (30 20, 20 25, 20 15, 30 20)))"); multiPolygon.setSRID(4326); GeometryCollection geometryCollection = new GeometryCollection( new Geometry[] {point, linearRing, lineString, polygon, multiPoint, multiPolygon}, geometryFactory); String geometryCollectionGeoJsonString = mapper.writeValueAsString(geometryCollection); logger.info( ":::::::::::::::::::::::GEO_JSON_GEOMETRY_COLLECTION : \n{}\n", geometryCollectionGeoJsonString); org.geojson.GeometryCollection p = mapper.readValue(geometryCollectionGeoJsonString, org.geojson.GeometryCollection.class); mapper.writeValue(new File("./target/GeometryCollectionComplex.json"), p); }
private LinearRing parseLinearRing(final int dimension, CoordinateReferenceSystem crs) throws XmlPullParserException, IOException, NoSuchAuthorityCodeException, FactoryException { parser.require(START_TAG, GML.NAMESPACE, GML.LinearRing.getLocalPart()); crs = crs(crs); Coordinate[] lineCoords = parseLineStringInternal(dimension, crs); parser.require(END_TAG, GML.NAMESPACE, GML.LinearRing.getLocalPart()); LinearRing linearRing = geomFac.createLinearRing(lineCoords); linearRing.setUserData(crs); return linearRing; }
@Test public void testMovePointOnGeometry() throws Exception { GeometryFactory fac = new GeometryFactory(); double[] coords = new double[] {10, 10, 10, 20, 10, 30, 30, 30, 20, 30, 10, 10}; LinearRing ring = fac.createLinearRing(new PackedCoordinateSequenceFactory().create(coords, 2)); Polygon poly = fac.createPolygon(ring, new LinearRing[0]); bb.clear(); geom = bb.addGeometry(poly, "poly").values().iterator().next(); bb.moveCoords(10, 10, 0, 10); }
/** * @param env GeneralEnvelope * @return Polygon object with the same boundary as env */ protected Polygon polyFromEnvelope(GeneralEnvelope env) { GeometryFactory factory = new GeometryFactory(); Coordinate[] coords = new Coordinate[] { new Coordinate(env.getMinimum(0), env.getMinimum(1)), new Coordinate(env.getMinimum(0), env.getMaximum(1)), new Coordinate(env.getMaximum(0), env.getMaximum(1)), new Coordinate(env.getMaximum(0), env.getMinimum(1)), new Coordinate(env.getMinimum(0), env.getMinimum(1)) }; return factory.createPolygon(factory.createLinearRing(coords), new LinearRing[0]); }
@Test public void polygonWithHoles() throws Exception { LinearRing shell = gf.createLinearRing( new Coordinate[] { new Coordinate(102.0, 2.0), new Coordinate(103.0, 2.0), new Coordinate(103.0, 3.0), new Coordinate(102.0, 3.0), new Coordinate(102.0, 2.0) }); LinearRing[] holes = new LinearRing[] { gf.createLinearRing( new Coordinate[] { new Coordinate(100.2, 0.2), new Coordinate(100.8, 0.2), new Coordinate(100.8, 0.8), new Coordinate(100.2, 0.8), new Coordinate(100.2, 0.2) }) }; assertThat( toJson(gf.createPolygon(shell, holes)), equalTo( "{\"type\":\"Polygon\",\"coordinates\":[[[102.0,2.0],[103.0,2.0],[103.0,3.0],[102.0,3.0],[102.0,2.0]],[[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]]}")); }
/** * Returns the polygon surrounding the specified rectangle. Code lifted from ArcGridDataSource * (temporary). */ public static Polygon getPolygon(final Rectangle2D rect, final int srid) { final PrecisionModel pm = new PrecisionModel(); final GeometryFactory gf = new GeometryFactory(pm, srid); final Coordinate[] coord = new Coordinate[] { new Coordinate(rect.getMinX(), rect.getMinY()), new Coordinate(rect.getMaxX(), rect.getMinY()), new Coordinate(rect.getMaxX(), rect.getMaxY()), new Coordinate(rect.getMinX(), rect.getMaxY()), new Coordinate(rect.getMinX(), rect.getMinY()) }; final LinearRing ring = gf.createLinearRing(coord); return new Polygon(ring, null, gf); }
/** * A geometry spanning all longitudes between latitude1 and latitude2. In latlong space this is a * rectangle. On the globe it is the spherical section between two parallel planes (both at right * angles to the axis). */ static Geometry betweenTwoLatitudes(double latitude1, double latitude2) { GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null); LinearRing border = geometryFactory.createLinearRing( new Coordinate[] { new Coordinate(MIN_LONGITUDE, latitude1), new Coordinate(MIN_LONGITUDE, latitude2), new Coordinate(MAX_LONGITUDE, latitude2), new Coordinate(MAX_LONGITUDE, latitude1), new Coordinate(MIN_LONGITUDE, latitude1) }); return border.convexHull(); }