Skip to content

Commit

Permalink
Remove duplicated regions
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakarFin committed Nov 22, 2024
1 parent ca26f44 commit 157720c
Show file tree
Hide file tree
Showing 3 changed files with 102,371 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ protected static SimpleFeatureCollection parseGMLFeatureCollection(InputStream i

protected static List<Region> parse(SimpleFeatureCollection fc, String idProperty, String nameProperty)
throws ServiceException {
final SimpleFeatureIterator it = fc.features();
try {
final List<Region> nameCodes = new ArrayList<>();
final List<String> duplicateIdCheckList = new ArrayList<>();
final List<Region> result = new ArrayList<>();
try (SimpleFeatureIterator it = fc.features()){
while (it.hasNext()) {
final SimpleFeature feature = it.next();
// id might be numeric on source data
Expand All @@ -166,22 +166,26 @@ protected static List<Region> parse(SimpleFeatureCollection fc, String idPropert
") property for region. Properties are:", LOG.getAsString(feature.getProperties()));
continue;
}
if (duplicateIdCheckList.contains(id)) {
LOG.info("Region with id (", id, ") and name(", name,
") has duplicates on the regions listing. Using the first one.");
continue;
}
Region region = new Region(id, name);
try {
region.setPointOnSurface(getPointOnSurface(feature));
region.setGeojson(toGeoJSON((Geometry) feature.getDefaultGeometry(), id, name));
nameCodes.add(region);
result.add(region);
duplicateIdCheckList.add(id);
} catch (Exception ex) {
LOG.warn("Region had invalid geometry:", region, "Error:", ex.getMessage());
}
}
if (nameCodes.isEmpty()) {
if (result.isEmpty()) {
throw new ServiceException("Empty result, check configuration for region id-property=" +
idProperty + " and name-property=" + nameProperty);
}
return nameCodes;
} finally {
it.close();
return result;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ public void testGeoJSONResourceFileWorks() throws MismatchedDimensionException,
}
}

@Test
public void testDuplicatedRegions() throws MismatchedDimensionException, FactoryException, TransformException, ServiceException, IOException, JSONException {
String endPoint = "resources://ely4500k.json";
RegionSet elyJson = new RegionSet();
elyJson.setId(-1);
elyJson.setName("oskari:ely4500k");
elyJson.setSrs_name("EPSG:3067");
elyJson.setAttributes(getAttributes("ely", "nimi", endPoint));
List<Region> regions = RegionSetHelper.getRegions(elyJson, "EPSG:3067");
assertEquals(16, regions.size());
}

@Test
public void testFeaturesUrl() throws MismatchedDimensionException, FactoryException, TransformException, ServiceException, IOException, JSONException {
String endPoint = "https://my.domain";
Expand Down
Loading

0 comments on commit 157720c

Please sign in to comment.