OrientDB spatial query to find all pairs within X

2019-09-15 05:01发布

问题:

I'm testing out the orientdb spatial module. I've put together a simple dataset with coordinates for a few of geoglyphs in the nazca lines (nazca_lines.csv):

Name,Latitude,Longitude
Hummingbird,-14.692131,-75.148892
Monkey,-14.706940,-75.138532
Condor,-14.697444,-75.126208
Spider,-14.694145,-75.122381
Spiral,-14.688277,-75.122746
Hands,-14.694459,-75.113881
Tree,-14.693898,-75.114520
Astronaut,-14.745222,-75.079755
Dog,-14.706401,-75.130788
Wing,-14.680309,-75.100385
Parrot,-14.689463,-75.107498

I create a spatial index using:

CREATE INDEX GeoGlyph.index.Location 
ON GeoGlyph(Latitude,Longitude) SPATIAL ENGINE LUCENE

I can generate a list of nodes that are within, say 2km, of a specific geoglyph using a query like the one I generated in this stack-overflow question:

SELECT $temp.Name AS SourceName, Name AS TargetName, $distance.format("%.4f") AS Distance 
FROM GeoGlyph 
LET $temp = first((SELECT * FROM GeoGlyph WHERE Name = "Tree"))
WHERE [Latitude,Longitude,$spatial] 
NEAR [$temp.Latitude, $temp.Longitude,{"maxDistance":2}] 
ORDER BY Distance

which gives me this result:

+----+----------+----------+--------+
|#   |SourceName|TargetName|Distance|
+----+----------+----------+--------+
|0   |Tree      |Tree      |0.0000  |
|1   |Tree      |Hands     |0.0884  |
|2   |Tree      |Spider    |0.9831  |
|3   |Tree      |Spiral    |1.0883  |
|4   |Tree      |Condor    |1.5735  |
+----+----------+----------+--------+

This is nice, but I can only find the nodes relative to a specific node. I would like to expand this to ask for all pairs of nodes that are within 2km of each other.

A result that I'm interested in would look something like this:

+----+-----------+-----------+--------+
|#   |SourceName |TargetName |Distance|
+----+-----------+-----------+--------+
|1   |Hummingbird|Monkey     |1.6314  |
|2   |Monkey     |Dog        |1.8035  |
|3   |Dog        |Condor     |0.9349  |  
|4   |Dog        |Spider     |1.5487  |
|5   |Condor     |Spider     |0.6772  |
|6   |Condor     |Spiral     |1.2685  |
|7   |Condor     |Tree       |1.5735  |
|8   |Condor     |Hands      |1.6150  |
|9   |Spider     |Spiral     |0.6797  |
 ...

Any ideas?

回答1:

you should use the new spatial module feature with OPoint type

and use ST_Distance_Sphere function

http://orientdb.com/docs/2.2/Spatial-Index.html#stdistancesphere-from-orientdb-224