目前使用mongodb的地理空间查询来搜索多边形内的点。
geojson 就像:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
103.959259,
30.320742
],
[
103.93563,
30.346203
],
[
103.942237,
30.357773
],
[
103.913904,
30.353972
],
[
103.910432,
30.330663
],
[
103.887699,
30.328761
],
[
103.87426,
30.348104
],
[
103.845368,
30.34331
],
[
103.794749,
30.345872
],
[
103.769776,
30.332068
],
[
103.75141,
30.307927
],
[
103.727893,
30.30462
],
[
103.736516,
30.285848
],
[
103.718374,
30.283201
],
[
103.705271,
30.268975
],
[
103.681754,
30.269885
],
[
103.693065,
30.245894
],
[
103.648046,
30.229263
],
[
103.649502,
30.199799
],
[
103.628,
30.214863
],
[
103.617473,
30.195412
],
[
103.597204,
30.203524
],
[
103.533819,
30.178109
],
[
103.544233,
30.161218
],
[
103.533819,
30.146643
],
[
103.504142,
30.128503
],
[
103.486224,
30.104147
],
[
103.474577,
30.131237
],
[
103.450948,
30.132231
],
[
103.468082,
30.114255
],
[
103.462483,
30.096027
],
[
103.4505,
30.096524
],
[
103.443893,
30.117486
],
[
103.443893,
30.120054
],
[
103.439637,
30.119557
],
[
103.439637,
30.119474
],
[
103.395178,
30.094866
],
[
103.360574,
30.136787
],
[
103.372109,
30.148382
],
[
103.36091,
30.168008
],
[
103.370317,
30.22827
],
[
103.350047,
30.236048
],
[
103.320259,
30.23489
],
[
103.313875,
30.260372
],
[
103.282295,
30.263598
],
[
103.242651,
30.243081
],
[
103.233356,
30.213456
],
[
103.215998,
30.209235
],
[
103.211631,
30.232407
],
[
103.183634,
30.248128
],
[
103.147238,
30.207
],
[
103.076798,
30.220242
],
[
103.071534,
30.260951
],
[
103.061456,
30.270464
],
[
103.068623,
30.294283
],
[
103.062128,
30.302553
],
[
103.079038,
30.324545
],
[
103.091132,
30.326529
],
[
103.127976,
30.367111
],
[
103.116329,
30.380002
],
[
103.115993,
30.413954
],
[
103.12596,
30.451115
],
[
103.141751,
30.470845
],
[
103.162244,
30.472579
],
[
103.185874,
30.511945
],
[
103.180834,
30.525228
],
[
103.157765,
30.530013
],
[
103.166276,
30.555749
],
[
103.147798,
30.585847
],
[
103.126856,
30.593927
],
[
103.135591,
30.624096
],
[
103.13156,
30.65475
],
[
103.119017,
30.670898
],
[
103.103115,
30.672957
],
[
103.068735,
30.716194
],
[
103.031331,
30.703678
],
[
103.033907,
30.719076
],
[
103.005126,
30.74649
],
[
102.992919,
30.778339
],
[
103.037938,
30.802364
],
[
103.083629,
30.812728
],
[
103.123945,
30.79282
],
[
103.165716,
30.842171
],
[
103.199088,
30.845707
],
[
103.228653,
30.836579
],
[
103.25833,
30.842418
],
[
103.284647,
30.829589
],
[
103.305252,
30.810507
],
[
103.337617,
30.824655
],
[
103.354527,
30.850887
],
[
103.383084,
30.862891
],
[
103.409401,
30.864946
],
[
103.451396,
30.892565
],
[
103.454644,
30.924695
],
[
103.465058,
30.941536
],
[
103.516461,
30.971761
],
[
103.52094,
31.005095
],
[
103.517132,
31.030129
],
[
103.504814,
31.041044
],
[
103.521164,
31.067792
],
[
103.525644,
31.101092
],
[
103.557224,
31.149055
],
[
103.564839,
31.175034
],
[
103.583205,
31.191422
],
[
103.57671,
31.215834
],
[
103.581413,
31.246626
],
[
103.569095,
31.262019
],
[
103.579733,
31.291241
],
[
103.569991,
31.314563
],
[
103.569543,
31.338453
],
[
103.60717,
31.351295
],
[
103.627104,
31.344097
],
[
103.669323,
31.354812
],
[
103.700792,
31.391118
],
[
103.739876,
31.398885
],
[
103.75029,
31.419156
],
[
103.787246,
31.434439
],
[
103.802588,
31.430108
],
[
103.816027,
31.431906
],
[
103.828682,
31.412454
],
[
103.868437,
31.420954
],
[
103.897554,
31.416377
],
[
103.894754,
31.408858
],
[
103.92835,
31.355139
],
[
103.894418,
31.326018
],
[
103.891618,
31.308017
],
[
103.914128,
31.278964
],
[
103.928014,
31.272497
],
[
103.929134,
31.234425
],
[
103.949404,
31.215997
],
[
103.983,
31.200925
],
[
104.014693,
31.167577
],
[
104.009429,
31.153235
],
[
104.022868,
31.142661
],
[
104.019844,
31.103963
],
[
104.057584,
31.07821
],
[
104.060608,
31.0641
],
[
104.094876,
31.032756
],
[
104.10641,
31.031771
],
[
104.106522,
31.031607
],
[
104.107194,
31.032017
],
[
104.10697,
31.032263
],
[
104.117385,
31.017572
],
[
104.143478,
31.01158
],
[
104.166884,
30.993438
],
[
104.166324,
30.951311
],
[
104.142582,
30.925845
],
[
104.149974,
30.91311
],
[
104.168787,
30.918533
],
[
104.202384,
30.91311
],
[
104.21571,
30.8984
],
[
104.244491,
30.896921
],
[
104.288726,
30.90292
],
[
104.322322,
30.891743
],
[
104.351887,
30.902345
],
[
104.367117,
30.93365
],
[
104.389739,
30.925681
],
[
104.39601,
30.942358
],
[
104.44506,
30.940468
],
[
104.465218,
30.9509
],
[
104.495791,
30.935457
],
[
104.511917,
30.935868
],
[
104.533083,
30.917875
],
[
104.520764,
30.882456
],
[
104.585269,
30.862891
],
[
104.596355,
30.864535
],
[
104.613937,
30.847763
],
[
104.629952,
30.852449
],
[
104.640366,
30.837895
],
[
104.698376,
30.825559
],
[
104.718086,
30.771756
],
[
104.732084,
30.778257
],
[
104.750786,
30.726898
],
[
104.768032,
30.71817
],
[
104.788526,
30.721299
],
[
104.78875,
30.694043
],
[
104.800956,
30.690995
],
[
104.818315,
30.664307
],
[
104.821226,
30.643874
],
[
104.842728,
30.649395
],
[
104.847319,
30.631101
],
[
104.819994,
30.598296
],
[
104.819098,
30.580076
],
[
104.858854,
30.582385
],
[
104.879124,
30.550635
],
[
104.892786,
30.541645
],
[
104.8761,
30.5131
],
[
104.875092,
30.437078
],
[
104.881699,
30.393551
],
[
104.895922,
30.382315
],
[
104.881027,
30.360831
],
[
104.860982,
30.364384
],
[
104.855494,
30.347194
],
[
104.837016,
30.361988
],
[
104.850455,
30.338929
],
[
104.848775,
30.306852
],
[
104.823354,
30.293787
],
[
104.807228,
30.296847
],
[
104.795133,
30.279397
],
[
104.774863,
30.30156
],
[
104.738579,
30.290314
],
[
104.723797,
30.308341
],
[
104.703975,
30.301643
],
[
104.691993,
30.275427
],
[
104.643614,
30.283036
],
[
104.638239,
30.268148
],
[
104.653133,
30.240185
],
[
104.599603,
30.244488
],
[
104.600163,
30.220408
],
[
104.569254,
30.196985
],
[
104.541594,
30.184236
],
[
104.533754,
30.193673
],
[
104.484368,
30.181504
],
[
104.485264,
30.156001
],
[
104.468354,
30.157077
],
[
104.464994,
30.151529
],
[
104.449428,
30.132894
],
[
104.457715,
30.120634
],
[
104.437109,
30.107544
],
[
104.450212,
30.101081
],
[
104.428374,
30.089729
],
[
104.41012,
30.125521
],
[
104.39881,
30.124776
],
[
104.386379,
30.161798
],
[
104.358158,
30.173638
],
[
104.364541,
30.185974
],
[
104.327138,
30.204269
],
[
104.315379,
30.200792
],
[
104.282231,
30.220904
],
[
104.2726,
30.217842
],
[
104.264537,
30.241592
],
[
104.243259,
30.244239
],
[
104.23094,
30.256153
],
[
104.243371,
30.267403
],
[
104.231836,
30.280472
],
[
104.23318,
30.281051
],
[
104.232732,
30.281878
],
[
104.2315,
30.282209
],
[
104.232284,
30.313798
],
[
104.2016,
30.316526
],
[
104.192977,
30.329836
],
[
104.17853,
30.318262
],
[
104.174051,
30.298666
],
[
104.152213,
30.265335
],
[
104.150533,
30.245646
],
[
104.116601,
30.227194
],
[
104.091628,
30.24879
],
[
104.047393,
30.270381
],
[
104.030035,
30.266245
],
[
104.030035,
30.266245
],
[
103.998343,
30.264508
],
[
103.974713,
30.290727
],
[
103.965866,
30.316774
],
[
103.959259,
30.320742
]
]
]
]
}
}
]
}
这可以在https://geojson.io中查看。
db.location.find({
lnglat:{
$geoWithin : {
$geometry : {
type:"Polygon",
coordinates:[
[
[ 103.959259, 30.320742 ] , // start loop
......
[ 103.959259, 30.320742 ] // close loop
]
]
}
}
}
})
因为起始坐标和结束坐标是相同的值。
但仍然出现 mongodb 错误:
MongoServerError: Loop is not valid
Edges 139 and 141 cross. Edge locations in degrees: [31.0316070, 104.1065220]-[31.0320170, 104.1071940] and [31.0322630, 104.1069700]-[31.0175720, 104.1173850]
geojson 似乎有问题,但不知道如何修复?
我建议使用 JTS Topology Suite 检查您的多边形,它是您必须下载并运行的单个 Java 文件:
set JAVA_OPTS=-Xms256M -Xmx1024M
javaw %JAVA_OPTS% -jar JTSTestBuilder.jar
您需要将 GEOJSON 转换为 WKT,例如使用 GeoJSON 到 WKT。当您将多边形放入工具并检查它时,您会得到这张图片:
当你放大时,你就会看到问题: