Skip to content

Instantly share code, notes, and snippets.

@derickson
Created January 6, 2012 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derickson/1569041 to your computer and use it in GitHub Desktop.
Save derickson/1569041 to your computer and use it in GitHub Desktop.
GeoReverseQuery
xquery version "1.0-ml";
(: a whole bunch of random circles to make the reverse-query harder :)
let $n := 10000
for $count in (1 to $n)
let $lat := xdmp:random(180) - 90
let $lon := xdmp:random(360) - 180
let $query-doc :=
let $a := cts:point( $lat,$lon)
let $b := cts:point( $lat + 0.0001, $lon)
let $circle := cts:circle( cts:distance($a, $b), $a)
return
<query>
{cts:element-geospatial-query( xs:QName("point"), $circle , ("coordinate-system=wgs84"))}
<a>{$a}</a>
<b>{$b}</b>
</query>
return
xdmp:document-insert( fn:concat("/circle-",$count,".xml"), $query-doc );
(: the actual matching circle :)
let $query-doc :=
let $a := cts:point( 10.0005,36)
let $b := cts:point( 10.00051,36)
let $circle := cts:circle( cts:distance($a, $b), $a)
return
<query>
{cts:element-geospatial-query( xs:QName("point"), $circle , ("coordinate-system=wgs84"))}
<a>{$a}</a>
<b>{$b}</b>
</query>
return
xdmp:document-insert( fn:concat("/matching-cicle.xml"), $query-doc )
xquery version "1.0-ml";
(: the line of points which will become a reverse query to match circles:)
let $a := cts:point( 10.0005, 36)
let $b := cts:point( 10.00052, 36)
let $distance := cts:distance($a, $b)
let $bearing := cts:bearing($a, $b)
let $steps := 100
let $step-distance := $distance div $steps
let $points :=
for $i in (0 to $steps)
return
cts:destination($a, $bearing, $step-distance * $i)
let $doc :=
<document>{
for $point in $points
return
<point>{$point}</point>
}</document>
return
(
cts:search( /, cts:reverse-query( $doc ) ) ,
xdmp:elapsed-time()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment