我正在尝试使用Morphia将GeoJson Polygon对象存储到Mongodb。请注意,GeoJson Polygon坐标必须是3级深度数组,如下所示。
{
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
答案 0 :(得分: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索引并执行$ intersect查询没问题。