gpt4 book ai didi

java - 在 java 中为 mongodb 创建地理空间查询

转载 作者:可可西里 更新时间:2023-11-01 10:41:06 24 4
gpt4 key购买 nike

我做我的项目。它与 MongoDB 和 Java 有关。我需要从 MongoDB 获取多边形数据。

但是,我的查询总是返回未找到记录。

这是我的java代码:

 string coords = "28.56402,79.93652a27.27569,26.16394a42.69404,20.02808a48.61541,51.37207a"
String[] coors = coords.split("a")
final LinkedList<double[]> polygon = new LinkedList<double[]>();
for(int i= 0;i<coors.length;i++)
{
String[] coo = coors[i].split(",");
System.out.println("coors" + i + " : " + coors[i]);
polygon.addLast(new double[]{Double.parseDouble(coo[0]),Double.parseDouble(coo[1])});
}
BasicDBObject query = new BasicDBObject("loc", new BasicDBObject("$within", new BasicDBObject("$polygon", polygon)));
FindIterable<Document> results = this.getMongoCollectionProcessor().queryDocument(query);
MongoCursor<Document> resultsIterator = results.iterator();

这是我的输出查询:

{ "loc" : { "$within" : { "$polygon" : [ [ 28.56402 , 79.93652] , [ 27.27569 , 26.16394] , [ 42.69404 , 20.02808] , [ 48.61541 , 51.37207]]}}}

查询计数为零。

我做错了什么?你能帮助我吗?谢谢。

最佳答案

您可以尝试以下查询。

Geopolygon 以数组的数组为坐标。使用 Filters.geoWithinPolygon 辅助方法作为 within 已弃用。

 String coords = "28.56402,79.93652a27.27569,26.16394a42.69404,20.02808a48.61541,51.37207a";
String[] coors = coords.split("a");
final List<List<Double>> polygons = new ArrayList<>();

for(int i= 0;i<coors.length;i++)
{
String[] coo = coors[i].split(",");
System.out.println("coors" + i + " : " + coors[i]);
polygons.add(Arrays.asList(Double.parseDouble(coo[0]),Double.parseDouble(coo[1])));
}

Bson query = Filters.geoWithinPolygon("loc", polygons);
FindIterable<Document> results = this.getMongoCollectionProcessor().queryDocument(query);
MongoCursor<Document> resultsIterator = results.iterator();

预期查询:

{
loc: {
$geoWithin: {
$geometry: {
type: "Polygon",
coordinates: [
[
[28.56402, 79.93652],
[27.27569, 26.16394],
[42.69404, 20.02808],
[48.61541, 51.37207]
]
]
}
}
}
}

关于java - 在 java 中为 mongodb 创建地理空间查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42762473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com