使用Spring Data,Mongodb,如何避免重复顶点错误

Jer*_*ook 8 java spring mongodb

我在我导入的一个多边形中得到了错误.

Write failed with error code 16755 and error message 'Can't extract geo keys: { _id: "b9c5ac0c-e469-4b97-b059-436cd02ffe49", _class: .... ] Duplicate vertices: 0 and 15'
Run Code Online (Sandbox Code Playgroud)

完整堆栈跟踪:https://gist.github.com/boundaries-io/927aa14e8d1e42d7cf516dc25b6ebb66#file-stacktrace

GeoJson MultiPolygon我使用Spring Data MongoDB导入

public class MyPolgyon {

    @Id
    String id;

    @GeoSpatialIndexed(type=GeoSpatialIndexType.GEO_2DSPHERE)
    GeoJsonPoint position;

    @GeoSpatialIndexed(type=GeoSpatialIndexType.GEO_2DSPHERE)
    GeoJsonPoint location;

    @GeoSpatialIndexed(type=GeoSpatialIndexType.GEO_2DSPHERE)
    GeoJsonPolygon polygon;





public static GeoJsonPolygon generateGeoJsonPolygon(List<LngLatAlt> coordinates) {
        List<Point> points = new ArrayList<Point>();
        for ( LngLatAlt  point: coordinates) {                          
                org.springframework.data.geo.Point dataPoint = new org.springframework.data.geo.Point( point.getLongitude() ,point.getLatitude());              
                points.add(dataPoint);          
            }   
        return new  GeoJsonPolygon(points);
    }
Run Code Online (Sandbox Code Playgroud)

我怎样才能在Java中避免这个错误?

我可以在http://geojson.io中加载geojson

这是GEOJSON:https://gist.github.com/boundaries-io/4719bfc386c3728b36be10af29860f4c#file-rol-ca-part1-geojson

使用以下方法删除重复项:

for (com.vividsolutions.jts.geom.Coordinate coordinate : geometry.getCoordinates()) {
    Point lngLatAtl = new Point(coordinate.x, coordinate.y);
    boolean isADup = points.contains(lngLatAtl);
    if ( !notDup ){
        points.add(lngLatAtl);
    }else{
        LOGGER.debug("Duplicate,  [" + lngLatAtl.toString() +"] index["  + count  +"]");
    }
    count++;
}
Run Code Online (Sandbox Code Playgroud)

记录:

2017-10-27 22:38:18 DEBUG TestBugs:58 - Duplicate,  [Point [x=-97.009868, y=52.358242]] index[15]
2017-10-27 22:38:18 DEBUG TestBugs:58 - Duplicate,  [Point [x=-97.009868, y=52.358242]] index[3348]
Run Code Online (Sandbox Code Playgroud)

use*_*814 4

在这种情况下,第二个多边形的索引 0 和索引 1341 处有重复的顶点。

[ -62.95859676499998, 46.20653318300003 ]
Run Code Online (Sandbox Code Playgroud)

当 Mongo db 尝试为文档构建 2d 球体索引时,插入失败。删除索引 1341 处的坐标,您应该能够成功坚持。

当您发现错误时,您只需清理数据即可。您可以编写一个小程序来从 mongo db 读取错误并将更新提供回客户端。客户端可以根据这些消息采取行动并再次尝试请求。

可以找到有关地理错误的更多信息here。您可以查看此处的代码以GeoParser了解如何生成错误/生成哪些错误。对于您遇到的具体错误,您可以查看此处GeoParser。这error是由 Mongodb 用于验证的 S2 库生成的。