use of org.locationtech.jts.geom.Polygon in project jena by apache.
the class WKTDatatypeTest method testReadMultiPolygon.
/**
* Test of read method, of class WKTReader.
*/
@Test
public void testReadMultiPolygon() {
String wktLiteral = "MULTIPOLYGON ZM(((40 40 0 1, 20 45 0 1, 45 30 0 1, 40 40 0 1)), ((20 35 0 1, 10 30 0 1, 10 10 0 1, 30 5 0 1, 45 20 0 1, 20 35 0 1), (30 20 0 1, 20 15 0 1, 20 25 0 1, 30 20 0 1)))";
Polygon[] polygons = new Polygon[2];
polygons[0] = GEOMETRY_FACTORY.createPolygon(new CustomCoordinateSequence(CoordinateSequenceDimensions.XYZM, "40 40 0 1, 20 45 0 1, 45 30 0 1, 40 40 0 1"));
LinearRing shell = GEOMETRY_FACTORY.createLinearRing(new CustomCoordinateSequence(CoordinateSequenceDimensions.XYZM, "20 35 0 1, 10 30 0 1, 10 10 0 1, 30 5 0 1, 45 20 0 1, 20 35 0 1"));
LinearRing[] holes = new LinearRing[] { GEOMETRY_FACTORY.createLinearRing(new CustomCoordinateSequence(CoordinateSequenceDimensions.XYZM, "30 20 0 1, 20 15 0 1, 20 25 0 1, 30 20 0 1")) };
polygons[1] = GEOMETRY_FACTORY.createPolygon(shell, holes);
Geometry geometry = GEOMETRY_FACTORY.createMultiPolygon(polygons);
GeometryWrapper expResult = new GeometryWrapper(geometry, SRS_URI.DEFAULT_WKT_CRS84, WKTDatatype.URI, new DimensionInfo(4, 3, 2));
GeometryWrapper result = WKT_DATATYPE.read(wktLiteral);
//
//
assertEquals(expResult, result);
}
use of org.locationtech.jts.geom.Polygon in project jena by apache.
the class GeometryReverseTest method testCheckPolygon.
/**
* Test of check method, of class GeometryReverse.
*
* @throws org.opengis.util.FactoryException
*/
@Test
public void testCheckPolygon() throws FactoryException {
WKTReader reader = new WKTReader();
try {
Polygon geometry = (Polygon) reader.read("POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))");
String srsURI = "http://www.opengis.net/def/crs/EPSG/0/4326";
Geometry expResult = reader.read("POLYGON ((10 30, 40 40, 40 20, 20 10, 10 30))");
Geometry result = GeometryReverse.check(geometry, srsURI);
//
//
assertEquals(expResult, result);
} catch (ParseException ex) {
System.err.println("ParseException: " + ex.getMessage());
}
}
use of org.locationtech.jts.geom.Polygon in project jena by apache.
the class GeometryReverse method reverseGeometry.
/**
* Reverses coordinate order of the supplied geometry and produces a new
* geometry.
*
* @param geometry
* @return Geometry in x,y coordinate order.
*/
public static Geometry reverseGeometry(Geometry geometry) {
if (geometry.isEmpty()) {
return geometry.copy();
}
GeometryFactory factory = geometry.getFactory();
Geometry finalGeometry;
Coordinate[] coordinates;
String type = geometry.getGeometryType();
switch(type) {
case "LineString":
coordinates = getReversedCoordinates(geometry);
finalGeometry = factory.createLineString(coordinates);
break;
case "LinearRing":
coordinates = getReversedCoordinates(geometry);
finalGeometry = factory.createLinearRing(coordinates);
break;
case "MultiPoint":
coordinates = getReversedCoordinates(geometry);
finalGeometry = factory.createMultiPointFromCoords(coordinates);
break;
case "Polygon":
finalGeometry = reversePolygon(geometry, factory);
break;
case "Point":
coordinates = getReversedCoordinates(geometry);
finalGeometry = factory.createPoint(coordinates[0]);
break;
case "MultiPolygon":
Polygon[] polygons = unpackPolygons((GeometryCollection) geometry);
finalGeometry = factory.createMultiPolygon(polygons);
break;
case "MultiLineString":
LineString[] lineString = unpackLineStrings((GeometryCollection) geometry);
finalGeometry = factory.createMultiLineString(lineString);
break;
case "GeometryCollection":
Geometry[] geometries = unpackGeometryCollection((GeometryCollection) geometry);
finalGeometry = factory.createGeometryCollection(geometries);
break;
default:
finalGeometry = geometry;
break;
}
return finalGeometry;
}
use of org.locationtech.jts.geom.Polygon in project jena by apache.
the class GeometryTransformation method transformMultiPolygon.
private static MultiPolygon transformMultiPolygon(Geometry sourceGeometry, MathTransform transform) throws TransformException {
GeometryFactory geometryFactory = sourceGeometry.getFactory();
MultiPolygon multiPolygon = (MultiPolygon) sourceGeometry;
int geometryNumber = multiPolygon.getNumGeometries();
ArrayList<Polygon> polygons = new ArrayList<>();
for (int i = 0; i < geometryNumber; i++) {
Polygon polygon = transformPolygon(multiPolygon.getGeometryN(i), transform);
polygons.add(polygon);
}
return geometryFactory.createMultiPolygon(polygons.toArray(new Polygon[polygons.size()]));
}
use of org.locationtech.jts.geom.Polygon in project jena by apache.
the class WKTReader method buildPolygon.
private Polygon buildPolygon(String coordinates) {
Polygon polygon;
String[] splitCoordinates = splitCoordinates(coordinates);
if (splitCoordinates.length == 1) {
// Polygon without holes.
CustomCoordinateSequence shellSequence = new CustomCoordinateSequence(dims, clean(coordinates));
polygon = GEOMETRY_FACTORY.createPolygon(shellSequence);
} else {
// Polygon with holes
String shellCoordinates = splitCoordinates[0];
CustomCoordinateSequence shellSequence = new CustomCoordinateSequence(dims, clean(shellCoordinates));
LinearRing shellLinearRing = GEOMETRY_FACTORY.createLinearRing(shellSequence);
String[] splitHoleCoordinates = Arrays.copyOfRange(splitCoordinates, 1, splitCoordinates.length);
LinearRing[] holesLinearRing = splitLinearRings(dims, splitHoleCoordinates);
polygon = GEOMETRY_FACTORY.createPolygon(shellLinearRing, holesLinearRing);
}
return polygon;
}
Aggregations