Loading TOC...

MarkLogic 12 EA 1 Product Documentation
geo:distance

geo:distance(
   $p1 as cts:point,
   $p2 as cts:point,
   [$options as xs:string*]
) as xs:double

Summary

Returns the distance (in units) between two points.

Parameters
p1 The first point.
p2 The second point.
options Options for the operation. The default is ().

Options include:

"coordinate-system=string"
Use the given coordinate system. Valid values are:
wgs84
The WGS84 coordinate system with degrees as the angular unit.
wgs84/radians
The WGS84 coordinate system with radians as the angular unit.
wgs84/double
The WGS84 coordinate system at double precision with degrees as the angular unit.
wgs84/radians/double
The WGS84 coordinate system at double precision with radians as the angular unit.
etrs89
The ETRS89 coordinate system.
etrs89/double
The ETRS89 coordinate system at double precision.
raw
The raw (unmapped) coordinate system.
raw/double
The raw coordinate system at double precision.
"precision=value"
Use the coordinate system at the given precision. Allowed values: float and double.
"units=value"
Measure distance and the radii of circles in the specified units. Allowed values: miles (default), km, feet, meters.

Usage Notes

The value of the precision option takes precedence over that implied by the governing coordinate system name, including the value of the coordinate-system option. For example, if the governing coordinate system is "wgs84/double" and the precision option is "float", then the operation uses single precision.

See Also

Example

let $sf := cts:point(37, -122)
let $ny := cts:point(40, -73)
return
geo:distance($sf, $ny)

=> 2623.2017796533

Example

xquery version "1.0-ml";
import module namespace op = 'http://marklogic.com/optic' 
           at 'MarkLogic/optic.xqy';
import module namespace ogeo = 'http://marklogic.com/optic/expression/geo' 
           at 'MarkLogic/optic/optic-geo.xqy';

(: Optic example using the Value Processing Function ogeo:distance() :)
let $plan:= op:from-view('buildings', 'builds')
             =>op:bind(op:as('result',ogeo:distance(op:col('interiorPoint'),cts:point('POINT(-98.83211485551952 36.1919336424082)'),'units=miles')))
             =>op:select(('result', op:col('interiorPoint')))
             =>op:order-by('result')

return $plan=>op:result()
(:
==>
rows representing the distance in miles between each point in the 'interiorPoint' column
and the wkt point 'POINT(-98.83211485551952 36.1919336424082)'
sorted by distance
:)

Example

xquery version "1.0-ml";
(: SQL example using ST_Distance() :)
xquery version "1.0-ml";
  xdmp:sql("select ST_Distance(interiorPoint, 'POINT(-98.83211485551952 36.1919336424082)', 'units=feet') from builds order by geoid limit 20")
(:
==>
rows representing the distance in feet between the interiorPoint column
and the WKT point 'POINT(-98.83211485551952 36.1919336424082)'
sorting by geoid
:)

Example

xquery version "1.0-ml";
(: GeoSPARQL example using geof:distance() :)
let $query:=
'
PREFIX my: <http://example.org/ApplicationSchema#>
PREFIX geoml: <http://marklogic.com/geospatial#>
PREFIX cts: <http://marklogic.com/cts#>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
 
SELECT *
WHERE { ?s my:hasExactGeometry ?o
  BIND(geof:distance(?o, ''POINT(39.801681875000014 66.64631330892108)'', ''units=km'') as ?distance)
  FILTER BOUND(?distance)
}
ORDER BY ?distance'
return sem:sparql($query)
(:
==>
rows representing the distance in kilometers between each point in a triple's object (if exists)
and the wkt point 'POINT(39.801681875000014 66.64631330892108)'
sorting by distance
:)

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.