2016-12-02 36 views
1

我正在嘗試使用Morphia存儲一個GeoJson多邊形對象到Mongodb。請注意,GeoJson多邊形座標必須是3級深度數組,如下所示。無法使用Morphia和Java在Mongodb中存儲GeoJson多邊形對象

{ 
    type: "Polygon", 
    coordinates: [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] ] 
} 

以下是我的實體類。

static class Loc { 
     private final String type = "Polygon"; 
     private ArrayList<ArrayList<Double[]>> coordinates = new ArrayList<ArrayList<Double[]>>(); 
     public ArrayList<ArrayList<Double[]>> getCoordinates() { 
      return coordinates; 
     } 
     public void setCoordinates(ArrayList<Double[]> coordinates) { 
      this.coordinates.add(coordinates); 

     } 
    } 

當我存儲它時,Morphia給了我下面的例外。

Exception in thread "main" org.mongodb.morphia.mapping.MappingException: Error mapping field:com.utils.JavaUtils$Poly.loc 
    at org.mongodb.morphia.mapping.Mapper.toDBObject(Mapper.java:983) 
    at org.mongodb.morphia.mapping.Mapper.toDBObject(Mapper.java:593) 
    at org.mongodb.morphia.DatastoreImpl.entityToDBObj(DatastoreImpl.java:1359) 
    at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:1279) 
    at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:775) 
    at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:758) 
    at com.utils.JavaUtils.polygonizeAndSave(JavaUtils.java:105) 
    at com.examples.MainExample$$anonfun$main$4.apply(MainExample.scala:71) 
    at com.examples.MainExample$$anonfun$main$4.apply(MainExample.scala:71) 
    at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59) 
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48) 
    at com.examples.MainExample$.main(MainExample.scala:71) 
    at com.examples.MainExample.main(MainExample.scala) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:736) 
    at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:185) 
    at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:210) 
    at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:124) 
    at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala) 


    Caused by: org.mongodb.morphia.mapping.MappingException: Error mapping field:com.utils.JavaUtils$Poly$Loc.coordinates 
    at org.mongodb.morphia.mapping.Mapper.toDBObject(Mapper.java:983) 
    at org.mongodb.morphia.mapping.Mapper.toDBObject(Mapper.java:593) 
    at org.mongodb.morphia.mapping.EmbeddedMapper.toDBObject(EmbeddedMapper.java:98) 
    at org.mongodb.morphia.mapping.Mapper.writeMappedField(Mapper.java:875) 
    at org.mongodb.morphia.mapping.Mapper.toDBObject(Mapper.java:981) 
    ... 21 more 


    Caused by: java.lang.IllegalArgumentException: BasicBSONList can only work with numeric keys, not: [className] 
    at org.bson.types.BasicBSONList._getInt(BasicBSONList.java:168) 
    at org.bson.types.BasicBSONList._getInt(BasicBSONList.java:160) 
    at org.bson.types.BasicBSONList.removeField(BasicBSONList.java:117) 
    at org.mongodb.morphia.mapping.EmbeddedMapper.writeCollection(EmbeddedMapper.java:243) 
    at org.mongodb.morphia.mapping.EmbeddedMapper.toDBObject(EmbeddedMapper.java:90) 
    at org.mongodb.morphia.mapping.Mapper.writeMappedField(Mapper.java:875) 
    at org.mongodb.morphia.mapping.Mapper.toDBObject(Mapper.java:981) 
    ... 25 more 

回答

1

有同樣的確切問題,並得出以下結論。您應該使用Morphia爲您提供的內容,而不是使用自定義類。主要是你可以創建你的多邊形這樣的:

import org.mongodb.morphia.geo.Point; 
import org.mongodb.morphia.geo.Polygon; 
import org.mongodb.morphia.geo.GeoJson; 

public class Main { 
    private static PlaceDAO placeDAO = ...; 

    public static void main(String[] args) {    
     Place place = new Place(); 

     Point point1 = GeoJson.point(0.0, 0.0); 
     Point point2 = GeoJson.point(0.0, 0.1); 
     Point point3 = GeoJson.point(0.1, 0.0); 
     Point point4 = GeoJson.point(0.0, 0.0); 
     Polygon location = GeoJson.polygon(point1, point2, point3, point4); 

     place.setLocation(location); 

     placeDAO.save(place); 
     List<Place> places = shopDAO.find().asList(); 
    } 
} 

您可以創建2dsphere指數和執行$相交查詢沒有問題。

+0

我最終創建了一個類似'private HashMap loc = new HashMap ()'的散列表,然後將整個'ArrayList >座標作爲一個值放到地圖中。但是這是一個解決方法。看起來像Morphia中的一個錯誤? – Arjit

+0

這不起作用,因爲多邊形的GeoJSON格式是{「type」:「Point」,「coordinates」:[....]}並且您的解決方案獲得{「Point」:[... ]}。你能舉一個例子,用你的解決方案將一個多邊形保存到db中嗎?無論如何,這不是Morphia中的一個錯誤,但在我看來,文檔是不夠的。希望我們能夠爲其他遇到這個問題的人提供足夠的信息。我提到的HashMap中的 – RafalManka

+0

。我把兩個關鍵值對。 'loc.put(「type」,「Polygon」); loc.put(「coordinates」,coordinates);' 這裏的座標是一個點類型的集合'ArrayList >' – Arjit

相關問題