2017-09-23 81 views
8

我得到我要導入的其中一個多邊形的錯誤。使用Mongodb的Spring數據,我該如何避免重複的頂點錯誤

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' 

完整的堆棧跟蹤:https://gist.github.com/boundaries-io/927aa14e8d1e42d7cf516dc25b6ebb66#file-stacktrace

GeoJson MultiPolygon I am importing using 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); 
    } 

我如何才能避免在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 notDup = points.contains(lngLatAtl); 
    if (!notDup){ 
     points.add(lngLatAtl); 
    }else{ 
     LOGGER.debug("Duplicate, [" + lngLatAtl.toString() +"] index[" + count +"]"); 
    } 
    count++; 
} 

日誌記錄:

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] 

回答

2

在這種情況下您在索引0和索引1處有重複的頂點第二個多邊形爲341。

[ -62.95859676499998, 46.20653318300003 ] 

插入失敗時蒙戈分貝試圖建立文檔的2D球體索引。刪除索引1341處的座標,您應該能夠成功保存。

您只需在發現錯誤時清理數據。 您可以編寫一個小程序來從mongo數據庫中讀取錯誤並將更新提供給客戶端。客戶端可以處理這些消息並再次嘗試請求。

有關地理錯誤的更多信息可以參考here。 你可以在這裏查看代碼GeoParser來查找如何生成錯誤。對於你得到的具體錯誤,你可以看看這裏GeoParser。此error由Mongodb用於驗證的S2庫生成。

+0

原始錯誤實際上是:重複的頂點:0和15 –

+0

根據您的建議更新測試用例仍然會給MongoDb保存錯誤。 –

+1

其實,這工作。謝謝。 –