Search in sources :

Example 86 with Polygon

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));
        }
    }
}
Also used : SurfaceProperty(org.geosdi.geoplatform.gml.api.SurfaceProperty) Polygon(org.locationtech.jts.geom.Polygon)

Example 87 with Polygon

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);
    }
}
Also used : Arrays(java.util.Arrays) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) Coordinate(org.locationtech.jts.geom.Coordinate) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GeoShapeProvider(de.symeda.sormas.api.geo.GeoShapeProvider) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Map(java.util.Map) GeoLatLon(de.symeda.sormas.api.geo.GeoLatLon) LocalBean(javax.ejb.LocalBean) ConfigFacadeEjbLocal(de.symeda.sormas.backend.common.ConfigFacadeEjb.ConfigFacadeEjbLocal) EJB(javax.ejb.EJB) ContentFeatureSource(org.geotools.data.store.ContentFeatureSource) Stateless(javax.ejb.Stateless) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Logger(org.slf4j.Logger) Files(java.nio.file.Files) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) Point(org.locationtech.jts.geom.Point) DistrictFacadeEjbLocal(de.symeda.sormas.backend.infrastructure.district.DistrictFacadeEjb.DistrictFacadeEjbLocal) JTSFactoryFinder(org.geotools.geometry.jts.JTSFactoryFinder) List(java.util.List) TopologyException(org.locationtech.jts.geom.TopologyException) MathTransform(org.opengis.referencing.operation.MathTransform) Paths(java.nio.file.Paths) RegionFacadeEjbLocal(de.symeda.sormas.backend.infrastructure.region.RegionFacadeEjb.RegionFacadeEjbLocal) Entry(java.util.Map.Entry) PostConstruct(javax.annotation.PostConstruct) Polygon(org.locationtech.jts.geom.Polygon) Optional(java.util.Optional) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) Collections(java.util.Collections) RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) MathTransform(org.opengis.referencing.operation.MathTransform) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) SimpleFeature(org.opengis.feature.simple.SimpleFeature) TopologyException(org.locationtech.jts.geom.TopologyException) ContentFeatureSource(org.geotools.data.store.ContentFeatureSource)

Example 88 with Polygon

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();
}
Also used : Arrays(java.util.Arrays) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) Coordinate(org.locationtech.jts.geom.Coordinate) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GeoShapeProvider(de.symeda.sormas.api.geo.GeoShapeProvider) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Map(java.util.Map) GeoLatLon(de.symeda.sormas.api.geo.GeoLatLon) LocalBean(javax.ejb.LocalBean) ConfigFacadeEjbLocal(de.symeda.sormas.backend.common.ConfigFacadeEjb.ConfigFacadeEjbLocal) EJB(javax.ejb.EJB) ContentFeatureSource(org.geotools.data.store.ContentFeatureSource) Stateless(javax.ejb.Stateless) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Logger(org.slf4j.Logger) Files(java.nio.file.Files) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) Point(org.locationtech.jts.geom.Point) DistrictFacadeEjbLocal(de.symeda.sormas.backend.infrastructure.district.DistrictFacadeEjb.DistrictFacadeEjbLocal) JTSFactoryFinder(org.geotools.geometry.jts.JTSFactoryFinder) List(java.util.List) TopologyException(org.locationtech.jts.geom.TopologyException) MathTransform(org.opengis.referencing.operation.MathTransform) Paths(java.nio.file.Paths) RegionFacadeEjbLocal(de.symeda.sormas.backend.infrastructure.region.RegionFacadeEjb.RegionFacadeEjbLocal) Entry(java.util.Map.Entry) PostConstruct(javax.annotation.PostConstruct) Polygon(org.locationtech.jts.geom.Polygon) Optional(java.util.Optional) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) Collections(java.util.Collections) RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) MathTransform(org.opengis.referencing.operation.MathTransform) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) SimpleFeature(org.opengis.feature.simple.SimpleFeature) TopologyException(org.locationtech.jts.geom.TopologyException) ContentFeatureSource(org.geotools.data.store.ContentFeatureSource)

Example 89 with Polygon

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;
}
Also used : Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) GeoLatLon(de.symeda.sormas.api.geo.GeoLatLon)

Example 90 with Polygon

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);
}
Also used : Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint)

Aggregations

Polygon (org.locationtech.jts.geom.Polygon)234 MultiPolygon (org.locationtech.jts.geom.MultiPolygon)112 Coordinate (org.locationtech.jts.geom.Coordinate)99 Point (org.locationtech.jts.geom.Point)73 Geometry (org.locationtech.jts.geom.Geometry)72 LinearRing (org.locationtech.jts.geom.LinearRing)67 LineString (org.locationtech.jts.geom.LineString)60 Test (org.junit.Test)56 ArrayList (java.util.ArrayList)51 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)42 MultiPoint (org.locationtech.jts.geom.MultiPoint)40 MultiLineString (org.locationtech.jts.geom.MultiLineString)23 Test (org.junit.jupiter.api.Test)22 List (java.util.List)21 GeometryCollection (org.locationtech.jts.geom.GeometryCollection)19 WKTReader (org.locationtech.jts.io.WKTReader)14 HashMap (java.util.HashMap)11 IOException (java.io.IOException)10 Map (java.util.Map)9 CustomCoordinateSequence (org.apache.jena.geosparql.implementation.jts.CustomCoordinateSequence)8