use of com.vividsolutions.jts.geom.Polygon in project elasticsearch by elastic.
the class ShapeBuilderTests method testNewPolygon_coordinates.
public void testNewPolygon_coordinates() {
Polygon polygon = ShapeBuilders.newPolygon(new CoordinatesBuilder().coordinates(new Coordinate(-45, 30), new Coordinate(45, 30), new Coordinate(45, -30), new Coordinate(-45, -30), new Coordinate(-45, 30))).toPolygon();
LineString exterior = polygon.getExteriorRing();
assertEquals(exterior.getCoordinateN(0), new Coordinate(-45, 30));
assertEquals(exterior.getCoordinateN(1), new Coordinate(45, 30));
assertEquals(exterior.getCoordinateN(2), new Coordinate(45, -30));
assertEquals(exterior.getCoordinateN(3), new Coordinate(-45, -30));
}
use of com.vividsolutions.jts.geom.Polygon in project elasticsearch by elastic.
the class GeoJSONShapeParserTests method testParsePolygonWithHole.
public void testParsePolygonWithHole() throws IOException {
XContentBuilder polygonGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Polygon").startArray("coordinates").startArray().startArray().value(100.0).value(1.0).endArray().startArray().value(101.0).value(1.0).endArray().startArray().value(101.0).value(0.0).endArray().startArray().value(100.0).value(0.0).endArray().startArray().value(100.0).value(1.0).endArray().endArray().startArray().startArray().value(100.2).value(0.8).endArray().startArray().value(100.2).value(0.2).endArray().startArray().value(100.8).value(0.2).endArray().startArray().value(100.8).value(0.8).endArray().startArray().value(100.2).value(0.8).endArray().endArray().endArray().endObject();
// add 3d point to test ISSUE #10501
List<Coordinate> shellCoordinates = new ArrayList<>();
shellCoordinates.add(new Coordinate(100, 0, 15.0));
shellCoordinates.add(new Coordinate(101, 0));
shellCoordinates.add(new Coordinate(101, 1));
shellCoordinates.add(new Coordinate(100, 1, 10.0));
shellCoordinates.add(new Coordinate(100, 0));
List<Coordinate> holeCoordinates = new ArrayList<>();
holeCoordinates.add(new Coordinate(100.2, 0.2));
holeCoordinates.add(new Coordinate(100.8, 0.2));
holeCoordinates.add(new Coordinate(100.8, 0.8));
holeCoordinates.add(new Coordinate(100.2, 0.8));
holeCoordinates.add(new Coordinate(100.2, 0.2));
LinearRing shell = GEOMETRY_FACTORY.createLinearRing(shellCoordinates.toArray(new Coordinate[shellCoordinates.size()]));
LinearRing[] holes = new LinearRing[1];
holes[0] = GEOMETRY_FACTORY.createLinearRing(holeCoordinates.toArray(new Coordinate[holeCoordinates.size()]));
Polygon expected = GEOMETRY_FACTORY.createPolygon(shell, holes);
assertGeometryEquals(jtsGeom(expected), polygonGeoJson);
}
use of com.vividsolutions.jts.geom.Polygon in project spatial-portal by AtlasOfLivingAustralia.
the class ShapefileUtils method saveShapefile.
public static void saveShapefile(File shpfile, String wktString, String name) {
try {
final SimpleFeatureType type = createFeatureType();
List<SimpleFeature> features = new ArrayList<SimpleFeature>();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(type);
WKTReader wkt = new WKTReader();
Geometry geom = wkt.read(wktString);
if (geom instanceof GeometryCollection) {
GeometryCollection gc = (GeometryCollection) geom;
for (int i = 0; i < gc.getNumGeometries(); i++) {
Geometry g = gc.getGeometryN(i);
if (g instanceof Polygon) {
g = new GeometryBuilder().multiPolygon((Polygon) g);
}
featureBuilder.add(g);
SimpleFeature feature = featureBuilder.buildFeature(null);
feature.setAttribute("name", name);
features.add(feature);
}
} else {
Geometry g = geom;
if (g instanceof Polygon) {
g = new GeometryBuilder().multiPolygon((Polygon) g);
}
featureBuilder.add(g);
SimpleFeature feature = featureBuilder.buildFeature(null);
features.add(feature);
}
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put("url", shpfile.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
newDataStore.createSchema(type);
newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
Transaction transaction = new DefaultTransaction("create");
String typeName = newDataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);
if (featureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
DefaultFeatureCollection collection = new DefaultFeatureCollection();
collection.addAll(features);
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(collection);
transaction.commit();
} catch (Exception problem) {
LOGGER.error("error pricessing shape file: " + shpfile.getAbsolutePath(), problem);
transaction.rollback();
} finally {
transaction.close();
}
}
LOGGER.debug("Active Area shapefile written to: " + shpfile.getAbsolutePath());
} catch (Exception e) {
LOGGER.error("Unable to save shapefile: " + shpfile.getAbsolutePath(), e);
}
}
use of com.vividsolutions.jts.geom.Polygon in project ddf by codice.
the class TwitterFilterVisitor method visit.
/**
* Contains filter maps to a Polygon or BBox Spatial search criteria.
*/
@Override
public Object visit(Contains filter, Object data) {
LOGGER.trace("ENTERING: Contains filter");
if (currentNest == null || NestedTypes.AND.equals(currentNest)) {
// The geometric point is wrapped in a <Literal> element, so have to
// get geometry expression as literal and then evaluate it to get
// the geometry.
// Example:
// <ogc:Literal>org.geotools.geometry.jts.spatialschema.geometry.primitive.SurfaceImpl@64a7c45e</ogc:Literal>
Literal literalWrapper = (Literal) filter.getExpression2();
Object geometryExpression = literalWrapper.getValue();
if (geometryExpression instanceof SurfaceImpl) {
SurfaceImpl polygon = (SurfaceImpl) literalWrapper.evaluate(null);
Point point = polygon.getJTSGeometry().getCentroid();
longitude = point.getX();
latitude = point.getY();
radius = point.getBoundary().getLength() * 10;
hasSpatial = true;
filters.add(filter);
} else if (geometryExpression instanceof Polygon) {
Polygon polygon = (Polygon) geometryExpression;
Point centroid = polygon.getCentroid();
longitude = centroid.getX();
latitude = centroid.getY();
radius = polygon.getBoundary().getLength() * 10;
hasSpatial = true;
filters.add(filter);
} else {
LOGGER.warn("Only POLYGON geometry WKT for Contains filter is supported");
}
} else {
LOGGER.warn(ONLY_AND_MSG);
}
LOGGER.trace("EXITING: Contains filter");
return super.visit(filter, data);
}
use of com.vividsolutions.jts.geom.Polygon in project ddf by codice.
the class TestWfs10JTStoGML200Converter method testPolygonTypeToJAXB.
@Test
public void testPolygonTypeToJAXB() throws JAXBException, SAXException, IOException, ParseException, NullPointerException {
Polygon polygon = (Polygon) getGeometryFromWkt(POLYGON);
assertThat(polygon == null, is(Boolean.FALSE));
String polygonGML = Wfs10JTStoGML200Converter.convertGeometryToGML(polygon);
PolygonType polygonType = (PolygonType) Wfs10JTStoGML200Converter.convertGMLToGeometryType(polygonGML, Wfs10Constants.POLYGON);
assertThat(null != polygonType, is(Boolean.TRUE));
JAXBElement<PolygonType> polygonTypeJAXBElement = (JAXBElement<PolygonType>) Wfs10JTStoGML200Converter.convertGeometryTypeToJAXB(polygonType);
assertThat(polygonTypeJAXBElement.getName().getLocalPart().equals(Wfs10Constants.POLYGON.getLocalPart()), is(Boolean.TRUE));
assertThat(polygonTypeJAXBElement.getDeclaredType() == PolygonType.class, is(Boolean.TRUE));
JAXB.marshal(polygonTypeJAXBElement, writer);
String xml = writer.toString();
Diff diff = XMLUnit.compareXML(xml, POLYGON_GML);
assertTrue(XMLUNIT_SIMILAR, diff.similar());
assertThat(diff.similar(), is(Boolean.TRUE));
assertThat(diff.identical(), is(Boolean.FALSE));
}
Aggregations