xy 形状字段类型

一个 xy 形状字段类型包含一个形状,例如多边形或一组 xy 点。它基于 Lucene XYShape 字段类型。为了索引 xy 形状,OpenSearch 将形状分解成三角网格,并将每个三角形存储在一个 BKD 树(一组平衡的 k 维树)中。这提供了 10-7 小数位的精度,代表了几乎完美的空间分辨率。

xy形状字段类型类似于geoshape形状字段类型,但它表示笛卡尔平面上的形状,而不是基于地球固定地面参考系统。xy形状的坐标是单精度浮点值。有关浮点值的范围和精度信息,请参阅数字字段类型

示例

创建一个具有xy形状字段类型的映射:xy形状类型

PUT testindex
{
  "mappings": {
    "properties": {
      "location": {
        "type": "xy_shape"
      }
    }
  }
}

格式

xy 形状可以以下格式索引:

在GeoJSON和WKT中,坐标必须在坐标数组中以x, y顺序指定。

xy 形状类型

以下表格描述了可能的 xy 形状类型及其与 GeoJSON 和 WKT 类型的关系。

UDB-SX类型 GeoJSON 类型 WKT 类型 描述
point Point POINT 地理坐标点,由纬度和经度指定。UDB-SX使用世界大地测量系统(WGS84)坐标。
linestring LineString LINESTRING 由两个或多个点指定的一条线。可能是一条直线或由连接的线段组成的路径。
polygon Polygon POLYGON 多边形由坐标形式的顶点列表指定。多边形必须是封闭的,这意味着最后一个点必须与第一个点相同。因此,要创建一个n边形,需要n+1个顶点。顶点的最小数量是四个,这会形成一个三角形。
multipoint MultiPoint MULTIPOINT 一组离散的相关点,这些点之间没有连接。
multilinestring MultiLineString MULTILINESTRING 一组线字符串。
multipolygon MultiPolygon MULTIPOLYGON 一个多边形的数组。
geometrycollection GeometryCollection GEOMETRYCOLLECTION 一组可能为不同类型的地理形状。
envelope N/A BBOX 由上左和下右顶点指定的边界矩形。

Point

一个点是由纬度和经度指定的单一坐标对。

索引GeoJSON格式的点:

PUT testindex/_doc/1
{
  "location" : {
    "type" : "point",
    "coordinates" : [0.5, 4.5]        
  }
}

WKT格式中索引一个点:

PUT testindex/_doc/1
{
  "location" : "POINT (0.5 4.5)"        
}

Linestring

线段是由两个或多个点指定的线。如果点共线,则线段为直线。否则,线段表示由线段组成的路径。

GeoJSON格式中索引一个线字符串:

PUT testindex/_doc/2
{
  "location" : {
    "type" : "linestring",
    "coordinates" : [[0.5, 4.5], [-1.5, 2.3]]
  }
}

在WKT格式中索引线字符串:

PUT testindex/_doc/2
{
  "location" : "LINESTRING (0.5 4.5, -1.5 2.3)"
}

Polygon

多边形由坐标形式的顶点列表指定。多边形必须是封闭的,这意味着最后一个点必须与第一个点相同。在以下示例中,使用四个点创建了一个三角形。

GeoJSON要求您按逆时针方向列出多边形的顶点。WKT不对顶点顺序做具体要求。

在GeoJSON格式中索引多边形(三角形):

PUT testindex/_doc/3
{
  "location" : {
    "type" : "polygon",
    "coordinates" : [
      [[0.5, 4.5], 
      [2.5, 6.0], 
      [1.5, 2.0], 
      [0.5, 4.5]]
    ]
  }
}

在WKT格式中索引多边形(三角形):

PUT testindex/_doc/3
{
  "location" : "POLYGON ((0.5 4.5, 2.5 6.0, 1.5 2.0, 0.5 4.5))"
}

多边形内部可能有空洞。在这种情况下,coordinates字段将包含多个数组。第一个数组表示外部多边形,每个后续数组表示一个空洞。空洞用多边形表示,并以坐标数组的形式指定。

GeoJSON要求您按逆时针方向列出多边形的顶点,并按顺时针方向列出孔洞的顶点。WKT不对顶点顺序施加特定要求。

在GeoJSON格式中索引带三角形孔的(三角形)多边形:

PUT testindex/_doc/4
{
  "location" : {
    "type" : "polygon",
    "coordinates" : [
      [[0.5, 4.5], 
      [2.5, 6.0], 
      [1.5, 2.0], 
      [0.5, 4.5]],
      
      [[1.0, 4.5], 
      [1.5, 4.5], 
      [1.5, 4.0], 
      [1.0, 4.5]]
    ]
  }
}

使用WKT格式索引具有三角形空洞的多边形(三角形):

PUT testindex/_doc/4
{
  "location" : "POLYGON ((0.5 4.5, 2.5 6.0, 1.5 2.0, 0.5 4.5), (1.0 4.5, 1.5 4.5, 1.5 4.0, 1.0 4.5))"
}

默认情况下,多边形的顶点按逆时针顺序遍历。您可以在映射时定义一个orientation参数来指定顶点遍历顺序:

PUT testindex
{
  "mappings": {
    "properties": {
      "location": {
        "type": "xy_shape",
        "orientation" : "left"
      }
    }
  }
}

随后索引的文档可以覆盖 orientation 设置:

PUT testindex/_doc/3
{
  "location" : {
    "type" : "polygon",
    "orientation" : "cw",
    "coordinates" : [
      [[0.5, 4.5], 
      [2.5, 6.0], 
      [1.5, 2.0], 
      [0.5, 4.5]]
    ]
  }
}

Multipoint

多点是一系列离散的、相互关联但不相连的点。

以GeoJSON格式索引多点:

PUT testindex/_doc/6
{
  "location" : {
    "type" : "multipoint",
    "coordinates" : [
      [0.5, 4.5], 
      [2.5, 6.0]
    ]
  }
}

在WKT格式中索引多点:

PUT testindex/_doc/6
{
  "location" : "MULTIPOINT (0.5 4.5, 2.5 6.0)"
}

Multilinestring

多线字符串是线字符串的数组。

GeoJSON格式中索引一个线字符串:

PUT testindex/_doc/2
{
  "location" : {
    "type" : "multilinestring",
    "coordinates" : [
      [[0.5, 4.5], [2.5, 6.0]],
      [[1.5, 2.0], [3.5, 3.5]]
      ]
  }
}

在WKT格式中索引线字符串:

PUT testindex/_doc/2
{
  "location" : "MULTILINESTRING ((0.5 4.5, 2.5 6.0), (1.5 2.0, 3.5 3.5))"
}

Multipolygon

多边形是多边形的集合。在这个例子中,第一个多边形包含一个洞,而第二个没有。

GeoJSON格式中索引多边形

PUT testindex/_doc/4
{
  "location" : {
    "type" : "multipolygon",
    "coordinates" : [
    [
      [[0.5, 4.5], 
      [2.5, 6.0], 
      [1.5, 2.0], 
      [0.5, 4.5]],
      
      [[1.0, 4.5], 
      [1.5, 4.5], 
      [1.5, 4.0], 
      [1.0, 4.5]]
    ],
    [
      [[2.0, 0.0], 
      [1.0, 2.0], 
      [3.0, 1.0], 
      [2.0, 0.0]]
      ]
    ]
  }
}

WKT格式的多边形索引

PUT testindex/_doc/4
{
  "location" : "MULTIPOLYGON (((0.5 4.5, 2.5 6.0, 1.5 2.0, 0.5 4.5), (1.0 4.5, 1.5 4.5, 1.5 4.0, 1.0 4.5)), ((2.0 0.0, 1.0 2.0, 3.0 1.0, 2.0 0.0)))"
}

Geometry collection

几何集合是不同类型的地理形状的集合。

以GeoJSON格式索引几何集合

PUT testindex/_doc/7
{
  "location" : {
    "type": "geometrycollection",
    "geometries": [
      {
        "type": "point",
        "coordinates": [0.5, 4.5]
      },
      {
        "type": "linestring",
        "coordinates": [[2.5, 6.0], [1.5, 2.0]]
      }
    ]
  }
}

以WKT格式的几何集合进行索引:

PUT testindex/_doc/7
{
  "location" : "GEOMETRYCOLLECTION (POINT (0.5 4.5), LINESTRING(2.5 6.0, 1.5 2.0))"
}

Envelope

边界是由上左和下右顶点指定的边界矩形。GeoJSON格式是[[minLon, maxLat], [maxLon, minLat]]。

在GeoJSON格式中索引边界

PUT testindex/_doc/2
{
  "location" : {
    "type" : "envelope",
    "coordinates" : [[3.0, 2.0], [6.0, 0.0]]
  }
}

在WKT格式中,使用BBOX (minLon, maxLon, maxLat, minLat)。

在WKT BBOX格式中索引边界:

PUT testindex/_doc/8
{
  "location" : "BBOX (3.0, 2.0, 6.0, 0.0)"
}

参数

以下表格列出了 xy 形状字段类型接受的参数。所有参数都是可选的。

参数 描述
coerce 布尔值,指定是否自动关闭未闭合的线性环。默认为false。
ignore_malformed 布尔值,指定忽略不规则的GeoJSON或WKT xy形状,并且不抛出异常。默认是false(当xy形状不规则时抛出异常)。
ignore_z_value 针对具有三个坐标的点。如果ignore_z_value是true,则第三个坐标未索引但仍然存储在_source字段中。如果ignore_z_value是false,则抛出异常。默认是true。
orientation 指定了地理形状坐标列表中顶点的遍历顺序。orientation取以下值:
1.RIGHT:逆时针。通过使用以下字符串之一(大写或小写)指定右侧方向:right, counterclockwise, ccw:
2. LEFT: 顺时针。通过以下字符串(大写或小写)指定左侧方向:left, clockwise, cw
默认是RIGHT