use of org.locationtech.jts.geom.Polygon in project geo-platform by geosdi.
the class SurfaceMember method buildMember.
/**
* @param gmlGeometry
* @param polygons
* @throws ParserException
*/
@Override
public void buildMember(@Nonnull(when = NEVER) MultiSurface gmlGeometry, @Nonnull(when = NEVER) List<Polygon> polygons) throws ParserException {
checkArgument(gmlGeometry != null, "The Parameter gmlGeometry must not be null.");
checkArgument(polygons != null, "The Parameter polygons must not be null");
if (gmlGeometry.isSetSurfaceMember()) {
for (SurfaceProperty surfaceProperty : gmlGeometry.getSurfaceMember()) {
org.geosdi.geoplatform.gml.api.Polygon abstractSurface = (org.geosdi.geoplatform.gml.api.Polygon) surfaceProperty.getAbstractSurface();
if ((gmlGeometry.isSetSrsDimension()) && !(abstractSurface.isSetSrsDimension()))
abstractSurface.setSrsDimension(gmlGeometry.getSrsDimension());
polygons.add(polygonParser.parseGeometry(abstractSurface));
}
}
}
use of org.locationtech.jts.geom.Polygon in project SORMAS-Project by hzi-braunschweig.
the class GeoShapeProviderEjb method loadDistrictData.
private void loadDistrictData(String countryName, String wkt) {
districtShapes.clear();
districtMultiPolygons.clear();
List<DistrictReferenceDto> districts = districtFacade.getAllActiveAsReference();
try {
// load shapefile
ContentFeatureSource featureSource = GeoShapeHelper.featureSourceOfShapefile(countryName, "districts.shp");
if (featureSource == null) {
return;
}
MathTransform transform = GeoShapeHelper.getLatLonMathTransform(featureSource, wkt);
SimpleFeatureIterator iterator = featureSource.getFeatures().features();
while (iterator.hasNext()) {
SimpleFeature feature = iterator.next();
String shapeDistrictName = GeoShapeHelper.sniffShapeAttribute(feature, Arrays.asList("LGAName", "DISTRICT", "GEN"));
if (shapeDistrictName == null) {
continue;
}
MultiPolygon multiPolygon = GeoShapeHelper.getPolygon(feature, transform);
if (multiPolygon == null) {
// there might me entries without a polygon -> not relevant
continue;
}
Optional<DistrictReferenceDto> districtResult;
// Use IDs in germany (could also be used for other countries, if fitting externalIDs are provided. Those can then be mapped to the externalID in SORMAS
if (countryName.equals("germany")) {
String shapeDistrictId = GeoShapeHelper.sniffShapeAttribute(feature, Collections.singletonList("ARS"));
if (shapeDistrictId == null) {
continue;
}
districtResult = districts.stream().filter(r -> {
String districtExtID = r.getExternalId();
if (districtExtID == null) {
return false;
}
return districtExtID.contains(shapeDistrictId) || shapeDistrictId.contains(districtExtID);
}).reduce((r1, r2) -> {
// in germany, the external IDs in SORMAS usually contain a leading '110'
if (r1.getExternalId().equals(shapeDistrictId) || r1.getExternalId().equals("110" + shapeDistrictId))
return r1;
if (r2.getExternalId().equals(shapeDistrictId) || r2.getExternalId().equals("110" + shapeDistrictId))
return r2;
return Double.compare(GeoShapeHelper.similarity(r1.getExternalId(), shapeDistrictId), GeoShapeHelper.similarity(r2.getExternalId(), shapeDistrictId)) <= 0 ? r1 : r2;
});
} else {
districtResult = districts.stream().filter(r -> {
String districtName = r.getCaption().replaceAll("\\W", "").toLowerCase();
return districtName.contains(shapeDistrictName) || shapeDistrictName.contains(districtName) || GeoShapeHelper.similarity(shapeDistrictName, districtName) > 0.7f;
}).reduce((r1, r2) -> {
if (r1.getCaption().replaceAll("\\W", "").toLowerCase().equals(shapeDistrictName))
return r1;
if (r2.getCaption().replaceAll("\\W", "").toLowerCase().equals(shapeDistrictName))
return r2;
return Double.compare(GeoShapeHelper.similarity(r1.getCaption(), shapeDistrictName), GeoShapeHelper.similarity(r2.getCaption(), shapeDistrictName)) <= 0 ? r1 : r2;
});
}
GeoShapeHelper.storeShape(districtMultiPolygons, districtShapes, multiPolygon, shapeDistrictName, districtResult);
}
iterator.close();
GeoShapeHelper.reportNotFound(districtShapes, districts);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.locationtech.jts.geom.Polygon in project SORMAS-Project by hzi-braunschweig.
the class GeoShapeProviderEjb method loadRegionData.
private void loadRegionData(String countryName, String wkt) {
regionShapes.clear();
regionMultiPolygons.clear();
List<RegionReferenceDto> regions = regionFacade.getAllActiveByServerCountry();
try {
// load shapefile
ContentFeatureSource featureSource = GeoShapeHelper.featureSourceOfShapefile(countryName, "regions.shp");
if (featureSource == null) {
return;
}
MathTransform transform = GeoShapeHelper.getLatLonMathTransform(featureSource, wkt);
SimpleFeatureIterator iterator = featureSource.getFeatures().features();
while (iterator.hasNext()) {
SimpleFeature feature = iterator.next();
String shapeRegionName = GeoShapeHelper.sniffShapeAttribute(feature, Arrays.asList("StateName", "REGION", "GEN"));
if (shapeRegionName == null) {
continue;
}
MultiPolygon multiPolygon = GeoShapeHelper.getPolygon(feature, transform);
if (multiPolygon == null) {
// there might me entries without a polygon -> not relevant
continue;
}
Optional<RegionReferenceDto> regionResult = regions.stream().filter(r -> {
String regionName = r.getCaption().replaceAll("\\W", "").toLowerCase();
return regionName.contains(shapeRegionName) || shapeRegionName.contains(regionName);
}).reduce((r1, r2) -> {
// dumb heuristic: take the result that best fits the length
if (Math.abs(r1.getCaption().length() - shapeRegionName.length()) <= Math.abs(r2.getCaption().length() - shapeRegionName.length())) {
return r1;
} else {
return r2;
}
});
GeoShapeHelper.storeShape(regionMultiPolygons, regionShapes, multiPolygon, shapeRegionName, regionResult);
}
iterator.close();
GeoShapeHelper.reportNotFound(regionShapes, regions);
} catch (Exception e) {
throw new RuntimeException(e);
}
updateCenterOfAllRegions();
}
use of org.locationtech.jts.geom.Polygon in project SORMAS-Project by hzi-braunschweig.
the class GeoShapeHelper method polygonToGeoLatLons.
/**
* Convert a polygon to an 2D array of Lat/Lon coordinates.
*
* @param multiPolygon
* The polygon which Lat/Lon coordinates get extracted.
* @return
* 2D array of Lat/Lon coordinates of the polygon.
*/
private static GeoLatLon[][] polygonToGeoLatLons(MultiPolygon multiPolygon) {
GeoLatLon[][] shape = new GeoLatLon[multiPolygon.getNumGeometries()][];
for (int i = 0; i < multiPolygon.getNumGeometries(); i++) {
Polygon polygon = (Polygon) multiPolygon.getGeometryN(i);
shape[i] = Arrays.stream(polygon.getExteriorRing().getCoordinates()).map(c -> new GeoLatLon(c.y, c.x)).toArray(GeoLatLon[]::new);
}
return shape;
}
use of org.locationtech.jts.geom.Polygon in project qupath by qupath.
the class ROITypeAdapters method parseMultiPolygon.
static MultiPolygon parseMultiPolygon(JsonArray coords, GeometryFactory factory) {
int n = coords.size();
Polygon[] polygons = new Polygon[n];
for (int i = 0; i < n; i++) {
polygons[i] = parsePolygon(coords.get(i).getAsJsonArray(), factory);
}
return factory.createMultiPolygon(polygons);
}
Aggregations