Merge pull request '#23447: Add centroids of polygons to indexed records' (#4) from centroids into master
Reviewed-on: #4
This commit is contained in:
commit
8b2b703c6f
|
@ -133,6 +133,11 @@
|
||||||
<version>1.5.0</version>
|
<version>1.5.0</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.locationtech.jts</groupId>
|
||||||
|
<artifactId>jts-core</artifactId>
|
||||||
|
<version>1.19.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
|
@ -14,9 +14,16 @@ import org.elasticsearch.action.index.IndexRequest;
|
||||||
import org.elasticsearch.client.RequestOptions;
|
import org.elasticsearch.client.RequestOptions;
|
||||||
import org.elasticsearch.client.RestClient;
|
import org.elasticsearch.client.RestClient;
|
||||||
import org.elasticsearch.client.RestHighLevelClient;
|
import org.elasticsearch.client.RestHighLevelClient;
|
||||||
|
import org.elasticsearch.common.geo.GeoPoint;
|
||||||
import org.elasticsearch.common.geo.builders.CoordinatesBuilder;
|
import org.elasticsearch.common.geo.builders.CoordinatesBuilder;
|
||||||
import org.elasticsearch.common.geo.builders.PolygonBuilder;
|
import org.elasticsearch.common.geo.builders.PolygonBuilder;
|
||||||
import org.elasticsearch.common.xcontent.XContentType;
|
import org.elasticsearch.common.xcontent.XContentType;
|
||||||
|
import org.locationtech.jts.algorithm.Centroid;
|
||||||
|
import org.locationtech.jts.geom.Coordinate;
|
||||||
|
import org.locationtech.jts.geom.Geometry;
|
||||||
|
import org.locationtech.jts.geom.util.GeometryTransformer;
|
||||||
|
import org.locationtech.jts.io.ParseException;
|
||||||
|
import org.locationtech.jts.io.WKTReader;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@ -41,6 +48,8 @@ public class BulkUpload {
|
||||||
|
|
||||||
private RestHighLevelClient client;
|
private RestHighLevelClient client;
|
||||||
|
|
||||||
|
private WKTReader wktReader = new WKTReader();
|
||||||
|
|
||||||
public void init(String elasticSearchHostName, String elasticSearchIndexName) throws IOException {
|
public void init(String elasticSearchHostName, String elasticSearchIndexName) throws IOException {
|
||||||
this.elasticSearchIndexName = elasticSearchIndexName;
|
this.elasticSearchIndexName = elasticSearchIndexName;
|
||||||
client = new RestHighLevelClient(
|
client = new RestHighLevelClient(
|
||||||
|
@ -58,6 +67,7 @@ public class BulkUpload {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int index(ResourceManager manager, boolean isCollection) {
|
public int index(ResourceManager manager, boolean isCollection) {
|
||||||
|
|
||||||
BulkRequest request = new BulkRequest();
|
BulkRequest request = new BulkRequest();
|
||||||
int esResponseCode = 0;
|
int esResponseCode = 0;
|
||||||
while (manager.hasNext()){
|
while (manager.hasNext()){
|
||||||
|
@ -88,6 +98,7 @@ public class BulkUpload {
|
||||||
double lon = Double.parseDouble(s.getLon());
|
double lon = Double.parseDouble(s.getLon());
|
||||||
org.elasticsearch.common.geo.GeoPoint geopoint = new org.elasticsearch.common.geo.GeoPoint(lat, lon);
|
org.elasticsearch.common.geo.GeoPoint geopoint = new org.elasticsearch.common.geo.GeoPoint(lat, lon);
|
||||||
s.setGeopoint(geopoint);
|
s.setGeopoint(geopoint);
|
||||||
|
s.setCentroid(geopoint);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (isCollection) {
|
if (isCollection) {
|
||||||
|
@ -98,6 +109,7 @@ public class BulkUpload {
|
||||||
.filter(s -> Objects.nonNull(s.getWkt()))
|
.filter(s -> Objects.nonNull(s.getWkt()))
|
||||||
.forEach(s -> {
|
.forEach(s -> {
|
||||||
s.setPolygon(s.getWkt());
|
s.setPolygon(s.getWkt());
|
||||||
|
s.setCentroid(calculateCentroid(s.getWkt()));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -132,11 +144,13 @@ public class BulkUpload {
|
||||||
PolygonBuilder polygonBuilder = new PolygonBuilder(coordinatesBuilder);
|
PolygonBuilder polygonBuilder = new PolygonBuilder(coordinatesBuilder);
|
||||||
String wkt = polygonBuilder.toWKT();
|
String wkt = polygonBuilder.toWKT();
|
||||||
s.setBoundingbox(wkt);
|
s.setBoundingbox(wkt);
|
||||||
|
s.setCentroid(calculateCentroid(wkt));
|
||||||
});
|
});
|
||||||
ace.getSpatial()
|
ace.getSpatial()
|
||||||
.stream()
|
.stream()
|
||||||
.filter(s -> Objects.nonNull(s.getPolygonGeoPoints())&&s.getPolygonGeoPoints().size()>=4)
|
.filter(s -> Objects.nonNull(s.getPolygonGeoPoints()) && s.getPolygonGeoPoints().size() >= 4)
|
||||||
.forEach(s -> {
|
.forEach(s -> {
|
||||||
|
//FIXME: What did you want to do? Nothing is set anywhere
|
||||||
CoordinatesBuilder coordinatesBuilder = new CoordinatesBuilder();
|
CoordinatesBuilder coordinatesBuilder = new CoordinatesBuilder();
|
||||||
s.getPolygonGeoPoints().forEach(p -> {
|
s.getPolygonGeoPoints().forEach(p -> {
|
||||||
coordinatesBuilder.coordinate(
|
coordinatesBuilder.coordinate(
|
||||||
|
@ -149,11 +163,12 @@ public class BulkUpload {
|
||||||
.filter(s -> Objects.nonNull(s.getWkt()))
|
.filter(s -> Objects.nonNull(s.getWkt()))
|
||||||
.forEach(s -> {
|
.forEach(s -> {
|
||||||
s.setPolygon(s.getWkt());
|
s.setPolygon(s.getWkt());
|
||||||
|
s.setCentroid(calculateCentroid(s.getWkt()));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ace.getSpatial()!=null) {
|
if (ace.getSpatial() != null) {
|
||||||
if (ace.getSpatial().size()==2) {
|
if (ace.getSpatial().size() == 2) {
|
||||||
Spatial uniqueSpatial = new Spatial();
|
Spatial uniqueSpatial = new Spatial();
|
||||||
boolean uniquePlaceNameFound = ace.getSpatial().stream().filter(s -> s.getPlaceName()!=null).count()==1;
|
boolean uniquePlaceNameFound = ace.getSpatial().stream().filter(s -> s.getPlaceName()!=null).count()==1;
|
||||||
boolean uniqueLocationFound = ace.getSpatial().stream().filter(s -> s.getGeopoint()!=null).count()==1;
|
boolean uniqueLocationFound = ace.getSpatial().stream().filter(s -> s.getGeopoint()!=null).count()==1;
|
||||||
|
@ -163,6 +178,7 @@ public class BulkUpload {
|
||||||
});
|
});
|
||||||
ace.getSpatial().stream().filter(s -> s.getGeopoint()!=null).forEach(s -> {
|
ace.getSpatial().stream().filter(s -> s.getGeopoint()!=null).forEach(s -> {
|
||||||
uniqueSpatial.setGeopoint(s.getGeopoint());
|
uniqueSpatial.setGeopoint(s.getGeopoint());
|
||||||
|
uniqueSpatial.setCentroid(s.getGeopoint());
|
||||||
});
|
});
|
||||||
ace.getSpatial().clear();
|
ace.getSpatial().clear();
|
||||||
ace.setSpatial(Arrays.asList(uniqueSpatial));
|
ace.setSpatial(Arrays.asList(uniqueSpatial));
|
||||||
|
@ -204,6 +220,17 @@ public class BulkUpload {
|
||||||
return esResponseCode;
|
return esResponseCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected GeoPoint calculateCentroid(final String wkt){
|
||||||
|
try {
|
||||||
|
Geometry geo = wktReader.read(wkt);
|
||||||
|
Coordinate coord = Centroid.getCentroid(geo);
|
||||||
|
return new org.elasticsearch.common.geo.GeoPoint(coord.getY(), coord.getX());
|
||||||
|
} catch (ParseException e) {
|
||||||
|
log.fatal("Cannot calculate centroid for WKT "+wkt+"\n Cause: "+e.getCause().getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static List<Spatial> removeDuplicates(List<Spatial> spatialList) {
|
public static List<Spatial> removeDuplicates(List<Spatial> spatialList) {
|
||||||
Map<String, List<Spatial>> duplicatesMap = getDuplicatesMap(spatialList);
|
Map<String, List<Spatial>> duplicatesMap = getDuplicatesMap(spatialList);
|
||||||
return duplicatesMap.values().stream()
|
return duplicatesMap.values().stream()
|
||||||
|
|
|
@ -15,6 +15,7 @@ public class Spatial {
|
||||||
private String polygon;
|
private String polygon;
|
||||||
private String spatialPrecision;
|
private String spatialPrecision;
|
||||||
private String coordinatePrecision;
|
private String coordinatePrecision;
|
||||||
|
private GeoPoint centroid;
|
||||||
|
|
||||||
private transient String boundingBoxMaxLat;
|
private transient String boundingBoxMaxLat;
|
||||||
private transient String boundingBoxMaxLon;
|
private transient String boundingBoxMaxLon;
|
||||||
|
@ -145,6 +146,14 @@ public class Spatial {
|
||||||
this.wkt = wkt;
|
this.wkt = wkt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public GeoPoint getCentroid() {
|
||||||
|
return centroid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCentroid(GeoPoint centroid) {
|
||||||
|
this.centroid = centroid;
|
||||||
|
}
|
||||||
|
|
||||||
public Spatial() {
|
public Spatial() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
package eu.dnetlib.ariadneplus.elasticsearch;
|
||||||
|
|
||||||
|
import org.elasticsearch.common.geo.GeoPoint;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class BulkUploadTest {
|
||||||
|
|
||||||
|
private BulkUpload bu;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup(){
|
||||||
|
bu = new BulkUpload();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCalculateCentroidPoint(){
|
||||||
|
String wkt = "POINT (30 10)";
|
||||||
|
GeoPoint centroid = bu.calculateCentroid(wkt);
|
||||||
|
System.out.println("Long: "+centroid.getLon()+", Lat: "+centroid.getLat());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCalculateCentroidMultiPolygon(){
|
||||||
|
String wkt = "MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)))";
|
||||||
|
GeoPoint centroid = bu.calculateCentroid(wkt);
|
||||||
|
System.out.println("Long: "+centroid.getLon()+", Lat: "+centroid.getLat());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCalculateCentroidPolygon(){
|
||||||
|
String wkt = "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))";
|
||||||
|
GeoPoint centroid = bu.calculateCentroid(wkt);
|
||||||
|
System.out.println("Long: "+centroid.getLon()+", Lat: "+centroid.getLat());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCalculateCentroidBB(){
|
||||||
|
String wkt = "polygon ((-1.42173131195844 51.778172135497, -1.42173131195844 51.7692537152273, -1.40712262768071 51.7692537152273, -1.40712262768071 51.778172135497, -1.42173131195844 51.778172135497))";
|
||||||
|
GeoPoint centroid = bu.calculateCentroid(wkt);
|
||||||
|
System.out.println("Long: "+centroid.getLon()+", Lat: "+centroid.getLat());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue