Skip to content

Commit

Permalink
Add irregular polygon test for centroid
Browse files Browse the repository at this point in the history
  • Loading branch information
utas-raymondng committed Sep 23, 2024
1 parent 29046a1 commit 6a4f698
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 291 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,19 +344,27 @@ protected static Point calculatePolygonCentroid(Geometry geometry) {
}

protected static List<Coordinate> calculateCollectionCentroid(GeometryCollection geometryCollection) {
List<Coordinate> coordinates = new ArrayList<>();
// Loop through each geometry in the collection
for (int i = 0; i < geometryCollection.getNumGeometries(); i++) {
Geometry geometry = geometryCollection.getGeometryN(i);

// Make sure the point will not fall out of the shape, for example a U shape will make
// centroid fall out of the U, so we check if the centroid is out of the shape? if yes then use
// interior point
// Try simplified the polygon to reduce the number of centroid.
Geometry geometry = geometryCollection.union();

if(geometry instanceof GeometryCollection c) {
List<Coordinate> coordinates = new ArrayList<>();
// That means it cannot be simplified, we need centroid of each polygon
for (int i = 0; i < c.getNumGeometries(); i++) {
geometry = geometryCollection.getGeometryN(i);

// Make sure the point will not fall out of the shape, for example a U shape will make
// centroid fall out of the U, so we check if the centroid is out of the shape? if yes then use
// interior point
Point centroid = calculatePolygonCentroid(geometry);
coordinates.add(new Coordinate(centroid.getX(), centroid.getY()));
}
return coordinates;
}
else {
Point centroid = calculatePolygonCentroid(geometry);
coordinates.add(new Coordinate(centroid.getX(), centroid.getY()));
return List.of(new Coordinate(centroid.getX(), centroid.getY()));
}
// Create and return the centroid point
return coordinates;
}
/**
* Create a centroid point for the polygon, this will help to speed up the map processing as there is no need
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ public void verifyMultipleTemporalExtentsNull() throws IOException, JSONExceptio

Map<?,?> content = objectMapper.readValue(lastRequest.get().document().toString(), Map.class);
String out = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(content);
log.info(out);
JSONAssert.assertEquals(
objectMapper.readTree(expected).toPrettyString(),
objectMapper.readTree(out.strip()).toPrettyString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@

import au.org.aodn.metadata.iso19115_3_2018.MDMetadataType;
import jakarta.xml.bind.JAXBException;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.geojson.feature.FeatureJSON;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.locationtech.jts.geom.*;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.FeatureType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static au.org.aodn.esindexer.BaseTestClass.readResourceFile;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class GeometryUtilsTest {
protected Logger logger = LoggerFactory.getLogger(GeometryUtilsTest.class);
Expand Down Expand Up @@ -80,4 +87,46 @@ public void verifyLandStrippedFromSpatialExtents() throws IOException, JAXBExcep
assertEquals(126.0, ncoors[3].getX(), 0.01);
assertEquals(-35.9999, ncoors[3].getY(), 0.01);
}
/**
* Given a irregular geojson, with hole the centroid point is still inside the polygon
* @throws IOException
*/
@Test
public void verifyCentroidCorrect() throws IOException {
// You can paste the geojson on geojson.io to see what it looks like
String geojson = readResourceFile("classpath:canned/irregular.geojson");
FeatureJSON json = new FeatureJSON();

// Read the GeoJSON file
StringReader reader = new StringReader(geojson);
FeatureCollection<SimpleFeatureType, SimpleFeature> feature = json.readFeatureCollection(reader);

List<Coordinate> point = GeometryUtils.calculateCollectionCentroid(convertToGeometryCollection(feature));
assertEquals(1, point.size(), "One item");
assertEquals(2.805438932281021, point.get(0).getX(),"X");
assertEquals( 2.0556251797475227, point.get(0).getY(), "Y");
}

protected GeometryCollection convertToGeometryCollection(
FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection) {
// Create a GeometryFactory
GeometryFactory geometryFactory = new GeometryFactory();

// List to hold the geometries extracted from the FeatureCollection
List<Geometry> geometries = new ArrayList<>();

// Iterate through the FeatureCollection and extract geometries
try(FeatureIterator<SimpleFeature> features = featureCollection.features()) {
while(features.hasNext()) {
Geometry geometry = (Geometry)(features.next()).getDefaultGeometry();
geometries.add(geometry);
}
}

// Convert the list of geometries to an array
Geometry[] geometryArray = geometries.toArray(new Geometry[0]);

// Create and return a GeometryCollection from the array of geometries
return geometryFactory.createGeometryCollection(geometryArray);
}
}
131 changes: 131 additions & 0 deletions indexer/src/test/resources/canned/irregular.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
[
0.2337242356863385,
4.518654452684871
],
[
1.0757323470082554,
4.063032694138045
],
[
2.0156483782505745,
4.772378565429946
],
[
2.6096230924383406,
4.082564826969502
],
[
4.378492845680455,
3.906758793933477
],
[
2.896819657540817,
3.0532809551275335
],
[
1.3237657441414399,
3.3595784740430616
],
[
0.4425944648511688,
2.55781029552125
],
[
1.3498745227876725,
1.8533999697946513
],
[
2.805438932281021,
2.0556251797475227
],
[
3.9933883606575193,
2.042579138064241
],
[
4.469873570940251,
1.2661726935846218
],
[
4.221840173806072,
0.8615572254442299
],
[
4.750542941380246,
0.16317964593562806
],
[
3.647447043602,
0.32635796829360686
],
[
3.3863592571466654,
1.2335444854399356
],
[
2.361589695305099,
0.46342576793742296
],
[
0.5861927474024355,
0.3851015854472166
],
[
-0.7061917955559807,
1.1617610340611009
],
[
-0.7779909368311166,
2.616495167329873
],
[
0.2337242356863385,
4.518654452684871
]
],
"type": "LineString"
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
[
1.2323850884183116,
3.8220980913438325
],
[
1.89815894388218,
4.258332255701447
],
[
2.4464432954400195,
3.7895340894263114
],
[
3.379832132021761,
3.724402418269719
],
[
2.59656877265283,
3.424735946359405
],
[
1.2323850884183116,
3.8220980913438325
]
],
"type": "LineString"
}
}
]
}
Loading

0 comments on commit 6a4f698

Please sign in to comment.