This page was generated
August  8,  2011
2:24  AM
XQuery Built-In and Modules Function Reference

Module: Geospatial Supporting Functions - KML Functions

The KML module provides support for geospatial queries using KML markup.

To use the KML module as part of your own XQuery module, include the following line in your XQuery prolog:

import module namespace kml = "http://earth.google.com/kml/2.0" at "/MarkLogic/geospatial/kml.xqy";

The library namespace prefix kml is not predefined in the server.

Function Summary
kml:box Create a cts:point value from a KML LatLongBox element.
kml:circle Create a cts:circle value from a radius and KML Point or Location element.
kml:geospatial-query Returns a cts:query matching points within given regions.
kml:geospatial-query-from-elements Returns a cts:query matching points within given regions.
kml:interior-polygon Create a sequence of cts:polygon values from a KML Polygon element.
kml:point Create a cts:point value from a KML Point or Location element.
kml:polygon Create a cts:polygon value from a sequence of KML Point or Location elements.
kml:polygon Create a cts:polygon value from a KML Polygon element.
Function Detail
kml:box(
$envelope as element(kml:LatLongBox)
)  as   cts:box
Summary:

Create a cts:point value from a KML LatLongBox element.

Parameters:
$envelope : A LatLongBox element.

Example:
  xquery version "1.0-ml";
  import module namespace kml = "http://earth.google.com/kml/2.0"
         at "/MarkLogic/geospatial/kml.xqy";

  kml:box(
     <kml:LatLongBox>
       <kml:north>30</kml:north>
       <kml:south>12.5</kml:south>
       <kml:east>-122.24</kml:east>
       <kml:west>-127.24</kml:west>
     </kml:LatLongBox>)
  

kml:circle(
$radius as xs:double,
$center as element()
)  as   cts:circle
Summary:

Create a cts:circle value from a radius and KML Point or Location element.

Parameters:
$radius : The radius of the circle, in miles.
$center : A KML Point or Location element representing the center of the circle.

Example:
  xquery version "1.0-ml";
  import module namespace kml = "http://earth.google.com/kml/2.0"
         at "/MarkLogic/geospatial/kml.xqy";

  kml:circle(47, 
	 <kml:Point>
		 <kml:coordinates>-127.24,12.5</kml:coordinates>
	 </kml:Point>)
  

kml:geospatial-query(
$regions as cts:region*,
[$options as xs:string*],
[$weight as xs:double?]
)  as   cts:query
Summary:

Returns a cts:query matching points within given regions.

Parameters:
$regions : One or more geographic boxes, circles, polygons, or points. Where multiple boxes, circles, polygons, or points are specified, the query matches if any box, circle, polygon, or point matches.
$options (optional): Options to this query. The default is ().

Options include:

"coordinate-system=wgs84"
Use the WGS84 coordinate system.
"boundaries-included"
Points on boxes' and circles' boundaries are counted as matching. This is the default.
"boundaries-excluded"
Points on boxes' and circles' boundaries are not counted as matching.
"boundaries-latitude-excluded"
Points on boxes' latitude boundaries are not counted as matching.
"boundaries-longitude-excluded"
Points on boxes' longitude boundaries are not counted as matching.
"boundaries-south-excluded"
Points on the boxes' southern boundaries are not counted as matching.
"boundaries-west-excluded"
Points on the boxes' western boundaries are not counted as matching.
"boundaries-north-excluded"
Points on the boxes' northern boundaries are not counted as matching.
"boundaries-east-excluded"
Points on the boxes' eastern boundaries are not counted as matching.
"boundaries-circle-excluded"
Points on circles' boundary are not counted as matching.
$weight (optional): A weight for this query. The default is 1.0.

Usage Notes:

The point value is expressed in the content of the element as a pair of numbers, separated by whitespace and punctuation (excluding decimal points and sign characters).

Point values and boundary specifications of boxes are given in degrees relative to the WGS84 coordinate system. Southern latitudes and Western longitudes take negative values. Longitudes will be wrapped to the range (-180,+180) and latitudes will be clipped to the range (-90,+90).

If the northern boundary of a box is south of the southern boundary, no points will match. However, longitudes wrap around the globe, so that if the western boundary is east of the eastern boundary, then the box crosses the anti-meridian.

Special handling occurs at the poles, as all longitudes exist at latitudes +90 and -90.


Example:
xquery version "1.0-ml";

(: create a document with test data :)
xdmp:document-insert("/points.xml",
<root xmlns:kml="http://earth.google.com/kml/2.0">
  <item><kml:Point><kml:coordinates>30.0,10.5</kml:coordinates></kml:Point></item>
  <item><kml:Point><kml:coordinates>35.34,15.35</kml:coordinates></kml:Point></item>
  <item><kml:Point><kml:coordinates>40.55,5.11</kml:coordinates></kml:Point></item>
</root> );

xquery version "1.0-ml";
import module namespace kml = "http://earth.google.com/kml/2.0"
   at "/MarkLogic/geospatial/kml.xqy";

cts:search(doc("/points.xml")//item, 
  kml:geospatial-query(
    kml:box(
      <kml:LatLongBox>
        <kml:north>20.0</kml:north>
        <kml:south>10.0</kml:south>
        <kml:east>40.0</kml:east>
        <kml:west>35.0</kml:west>
      </kml:LatLongBox>) ))
(:
  returns the following node: 
  <item><kml:Point><kml:coordinates>35.34,15.35</kml:coordinates></kml:Point></item>
:)
,

cts:search(doc("/points.xml")//item, 
  kml:geospatial-query(kml:box(
    <kml:LatLongBox>
      <kml:north>20.0</kml:north>
      <kml:south>10.0</kml:south>
      <kml:east>35.0</kml:east>
      <kml:west>40.0</kml:west>
    </kml:LatLongBox>) ))
(:
  returns the following nodes (wrapping around the Earth):
  <item><kml:Point><kml:coordinates>30.0,10.5</kml:coordinates></kml:Point></item>
:)
 

kml:geospatial-query-from-elements(
$regions as element()*,
[$options as xs:string*],
[$weight as xs:double?]
)  as   cts:query
Summary:

Returns a cts:query matching points within given regions.

Parameters:
$regions : One or more geographic boxes, circles, polygons, or points, represented as KML elements. Where multiple boxes, circles, polygons, or points are specified, the query matches if any box, circle, polygon, or point matches.
$options (optional): Options to this query. The default is ().

Options include:

"coordinate-system=wgs84"
Use the WGS84 coordinate system.
"boundaries-included"
Points on boxes' and circles' boundaries are counted as matching. This is the default.
"boundaries-excluded"
Points on boxes' and circles' boundaries are not counted as matching.
"boundaries-latitude-excluded"
Points on boxes' latitude boundaries are not counted as matching.
"boundaries-longitude-excluded"
Points on boxes' longitude boundaries are not counted as matching.
"boundaries-south-excluded"
Points on the boxes' southern boundaries are not counted as matching.
"boundaries-west-excluded"
Points on the boxes' western boundaries are not counted as matching.
"boundaries-north-excluded"
Points on the boxes' northern boundaries are not counted as matching.
"boundaries-east-excluded"
Points on the boxes' eastern boundaries are not counted as matching.
"boundaries-circle-excluded"
Points on circles' boundary are not counted as matching.
$weight (optional): A weight for this query. The default is 1.0.

Usage Notes:

The point value is expressed in the content of the element as a pair of numbers, separated by whitespace and punctuation (excluding decimal points and sign characters).

Point values and boundary specifications of boxes are given in degrees relative to the WGS84 coordinate system. Southern latitudes and Western longitudes take negative values. Longitudes will be wrapped to the range (-180,+180) and latitudes will be clipped to the range (-90,+90).

If the northern boundary of a box is south of the southern boundary, no points will match. However, longitudes wrap around the globe, so that if the western boundary is east of the eastern boundary, then the box crosses the anti-meridian.

Special handling occurs at the poles, as all longitudes exist at latitudes +90 and -90.

This function will take into account interior polygons, if any, and properly construct the query to account for them.


Example:
xquery version "1.0-ml";

(: create a document with test data :)
xdmp:document-insert("/points.xml",
<root xmlns:kml="http://earth.google.com/kml/2.0">
  <item><kml:Point><kml:coordinates>30.0,10.5</kml:coordinates></kml:Point></item>
  <item><kml:Point><kml:coordinates>35.34,15.35</kml:coordinates></kml:Point></item>
  <item><kml:Point><kml:coordinates>40.55,5.11</kml:coordinates></kml:Point></item>
</root> );

xquery version "1.0-ml";
import module namespace kml = "http://earth.google.com/kml/2.0"
   at "/MarkLogic/geospatial/kml.xqy";

cts:search(doc("/points.xml")//item, 
  kml:geospatial-query-from-elements(
      <kml:LatLongBox>
        <kml:north>20.0</kml:north>
        <kml:south>10.0</kml:south>
        <kml:east>40.0</kml:east>
        <kml:west>35.0</kml:west>
      </kml:LatLongBox>) )
(:
  returns the following node: 
  <item><kml:Point><kml:coordinates>35.34,15.35</kml:coordinates></kml:Point></item>
:)
,

cts:search(doc("/points.xml")//item, 
  kml:geospatial-query-from-elements(
    <kml:LatLongBox>
      <kml:north>20.0</kml:north>
      <kml:south>10.0</kml:south>
      <kml:east>35.0</kml:east>
      <kml:west>40.0</kml:west>
    </kml:LatLongBox>) )
(:
  returns the following nodes (wrapping around the Earth):
  <item><kml:Point><kml:coordinates>30.0,10.5</kml:coordinates></kml:Point></item>
:)
 

kml:interior-polygon(
$points as element(kml:Polygon)
)  as   cts:polygon
Summary:

Create a sequence of cts:polygon values from a KML Polygon element. The polygons returned represent the interior polygons, if any.

Parameters:
$points : A KML Polygon element representing the polygon.

Example:
   xquery version "1.0-ml";
   import module namespace kml = "http://earth.google.com/kml/2.0"
         at "/MarkLogic/geospatial/kml.xqy";

  kml:interior-polygon(
    <kml:Polygon>
      <kml:outerBoundaryIs><kml:LinearRing><kml:coordinates>
      -127.24,12.5 -127.8,15.25 -126.1,13.45 -127.24,12.5
      </kml:coordinates></kml:LinearRing></kml:outerBoundaryIs>
      <kml:innerBoundaryIs><kml:LinearRing><kml:coordinates>
      -127,13 127,-14 126,-14 127,-13
      </kml:coordinates></kml:LinearRing></kml:innerBoundaryIs>
    </kml:Polygon>
  )
  

kml:point(
$point as element()
)  as   cts:point
Summary:

Create a cts:point value from a KML Point or Location element.

Parameters:
$point : A Point or Location element.

Usage Notes:

The KML Point and Location elements include additional information that is not retained in the cts:point value. In particular, the elevation part of the point value will be dropped. Therefore two KML points at the same latitude and longitude with different elevations will nevertheless have equal cts:point values.

Example:
  xquery version "1.0-ml";
  import module namespace kml = "http://earth.google.com/kml/2.0"
         at "/MarkLogic/geospatial/kml.xqy";

  kml:point(
    <kml:Point>
      <kml:coordinates>-127.24,12.5,10.0</kml:coordinates>
    </kml:Point>)
  ,
  kml:point(
    <kml:Location>
      <kml:latitude>12.5</kml:latitude>
      <kml:longitude>-127.24</kml:longitude>
      <kml:altitude>10.0</kml:altitude>
    </kml:Location>)
  

kml:polygon(
$points as element(kml:Point)
)  as   cts:polygon
Summary:

Create a cts:polygon value from a sequence of KML Point or Location elements.

Parameters:
$points : A sequence of Point or Location element representing the vertices of the polygon.

Example:
  xquery version "1.0-ml";
  import module namespace kml = "http://earth.google.com/kml/2.0"
         at "/MarkLogic/geospatial/kml.xqy";

  kml:polygon((
    <kml:Point><kml:coordinates>-127.24,12.5</kml:coordinates></kml:Point>,
    <kml:Point><kml:coordinates>-127.8,15.25</kml:coordinates></kml:Point>,
    <kml:Point><kml:coordinates>-126.1,13.45</kml:coordinates></kml:Point>,
    <kml:Point><kml:coordinates>-127.24,12.5</kml:coordinates></kml:Point>
  ))
  

kml:polygon(
$points as element(kml:Polygon)
)  as   cts:polygon
Summary:

Create a cts:polygon value from a KML Polygon element. The polygon returned represents the exterior polygon.

Parameters:
$points : A KML Polygon element representing the polygon.

Example:
  xquery version "1.0-ml";
  import module namespace kml = "http://earth.google.com/kml/2.0"
         at "/MarkLogic/geospatial/kml.xqy";

  kml:polygon(
  <kml:Polygon><kml:outerBoundaryIs><kml:LinearRing>
   <kml:coordinates>-127.24,12.5 -127.8,15.25 -126.1,13.45 -127.24,12.5</kml:coordinates>
  </kml:LinearRing></kml:outerBoundaryIs></kml:Polygon>
  )