แก้ไข

แชร์ผ่าน


ST_DISTANCE (NoSQL query)

APPLIES TO: NoSQL

Returns the distance between two GeoJSON Point, Polygon, MultiPolygon or LineString expressions.

Note

For more information, see Geospatial and GeoJSON location data.

Syntax

ST_DISTANCE(<spatial_expr_1>, <spatial_expr_2>)  

Arguments

Description
spatial_expr_1 Any valid GeoJSON Point, Polygon, MultiPolygon or LineString expression.
spatial_expr_2 Any valid GeoJSON Point, Polygon, MultiPolygon or LineString expression.

Return types

Returns a numeric expression that enumerates the distance between two expressions.

Examples

The following example assumes a container exists with two items.

[
  {
    "name": "Headquarters",
    "location": {
      "type": "Point",
      "coordinates": [
        -122.12826822304672,
        47.63980239335718
      ]
    },
    "category": "business-offices"
  },
  {
    "name": "Research and development",
    "location": {
      "type": "Point",
      "coordinates": [
        -96.84368664765994,
        46.81297794314663
      ]
    },
    "category": "business-offices"
  }
]

The example shows how to use the function as a filter to return items within a specified distance.

SELECT
    o.name,
    ST_DISTANCE(o.location, {
        "type": "Point", 
        "coordinates": [-122.11758113953535, 47.66901087006131]
    }) / 1000 AS distanceKilometers
FROM
    offices o
WHERE
    o.category = "business-offices"
[
  {
    "name": "Headquarters",
    "distanceKilometers": 3.345269817267368
  },
  {
    "name": "Research and development",
    "distanceKilometers": 1907.438421299902
  }
]

Remarks

  • The result is expressed in meters for the default reference system.
  • This function benefits from a geospatial index except in queries with aggregates.
  • The GeoJSON specification requires that points within a Polygon be specified in counter-clockwise order. A Polygon specified in clockwise order represents the inverse of the region within it.