Skip to content

Instantly share code, notes, and snippets.

@derickson derickson/kml.xqy
Created May 2, 2013

Embed
What would you like to do?
geo code for guest blog post
xquery version "1.0-ml";
declare namespace ns = "http://www.marklogic.com/MLU/logbook";
declare variable $bbox as xs:string? := xdmp:get-request-field("BBOX", ());
xdmp:set-response-content-type("text/kml; charset=utf-8"),
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
<Document>
{
(: If the bounding box was provided by Google Earth, use it for a geo query :)
let $pointQuery :=
if ($bbox) then
let $lon1 := fn:number(fn:tokenize($bbox, ",")[1])
let $lon2 := fn:number(fn:tokenize($bbox, ",")[3])
let $lat1 := fn:number(fn:tokenize($bbox, ",")[2])
let $lat2 := fn:number(fn:tokenize($bbox, ",")[4])
let $west := fn:min(($lon1, $lon2))
let $east := fn:max(($lon1, $lon2))
let $north := fn:max(($lat1, $lat2))
let $south := fn:min(($lat1, $lat2))
let $boundingBox := cts:box($south, $west, $north, $east)
return cts:element-pair-geospatial-query( xs:QName("ns:Place"),
xs:QName("ns:Lat"),
xs:QName("ns:Lon"),
$boundingBox)
else
()
(: Perform the search - only geo search for simplicity :)
let $docs := cts:search(fn:doc(), $pointQuery, "unfiltered")
(: KML is Lon, Lat, Alt :)
for $doc in $docs
return
<Placemark>
<name>{$doc//ns:Site/text()}</name>
<description>
{$doc//ns:DiverLName/text()}, {$doc//ns:DiverFName/text()}:
{$doc//ns:Divedate/text()}&lt;p&gt;{$doc//ns:Comments/text()}
</description>
<Point>
<coordinates>{$doc//ns:Lon/text()}, {$doc//ns:Lat/text()}</coordinates>
</Point>
</Placemark>
}
</Document>
</kml>
<doc xmlns="http://www.marklogic.com/MLU/logbook">
<uniqueID>xyz123</uniqueID>
<Place>
<Lat>37.111</Lat>
<Lon> -77.123</Lon>
</Place>
...
</doc>
<?xml version="1.0" encoding="UTF-8"?>
<results>
<doc ns="http://www.marklogic.com/MLU/logbook">
<uniqueID>xyz123</uniqueID>
<Place>
<Lat>37.111</Lat>
<Lon>-77.123</Lon>
</Place>
</doc>
</results>
xquery version "1.0-ml";
declare namespace ns = "http://www.marklogic.com/MLU/logbook";
let $center := cts:point(37.112, -77.121)
let $circle := cts:circle(0.2, $center)
let $pointQuery :=
cts:element-pair-geospatial-query(
xs:QName("ns:Place"),
xs:QName(“ns:Lat”),
xs:QName(“ns:Lon”),
$circle)
return
cts:search(fn:doc(), $pointQuery)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.