GeoJSON

818 阅读4分钟

GeoJSON

一、介绍

GeoJSON 是一种用于编码各种地理数据结构的格式。

二、基本格式

{ 
"type": "FeatureCollection",
"features": [
  { "type": "Feature",
    "geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
    "properties": {"prop0": "value0"}
    },
  { "type": "Feature",
    "geometry": {
      "type": "LineString",
      "coordinates": [
        [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
        ]
      },
    "properties": {
      "prop0": "value0",
      "prop1": 0.0
      }
    },
  { "type": "Feature",
     "geometry": {
       "type": "Polygon",
       "coordinates": [
         [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
           [100.0, 1.0], [100.0, 0.0] ]
         ]
     },
     "properties": {
       "prop0": "value0",
       "prop1": {"this": "that"}
       }
     }
   ]
 }

GeoJSON 支持以下几何类型:PointLineStringPolygonMultiPointMultiLineStringMultiPolygon。具有附加属性的几何对象是Feature对象。特征集包含在FeatureCollection对象中。

三、GeoJSON对象

  • GeoJSON 对象可以有任意数量的成员(名称/值对)。
  • GeoJSON 对象必须有一个名为"type"的成员。该成员的值是一个字符串,用于确定 GeoJSON 对象的类型。
  • type的值必须是以下之一:"Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon", "GeometryCollection", "Feature", 或"FeatureCollection"。类型成员值的大小写必须如此处所示。
  • GeoJSON 对象可能有一个可选"crs"成员,其值必须是坐标参考系统对象。
  • GeoJSON 对象可能有一个"bbox"成员,其值必须是边界框数组。

3.1 geometry对象

type属性的值是以下字符串之一:"Point""MultiPoint""LineString""MultiLineString""Polygon""MultiPolygon""GeometryCollection"

coordinates属性的值始终是一个数组。该数组中元素的结构由几何类型决定。

3.1.1 positions

positions是基本的几何结构。几何对象的"coordinates"成员由一个位置(在 Point 几何的情况下)、位置数组(LineString 或 MultiPoint 几何)、位置数组(Polygons、MultiLineStrings)或多维位置数组组成(多多边形)。

3.1.2 Point

点元素的坐标只能是单个坐标数组

3.1.3 MultiPoint

多点元素的坐标是多个坐标数组的集合,即二维数组

3.1.4 LineString

"coordinates"成员必须是两个或多个位置的数组

3.1.45 MultiLineString

"coordinates"成员必须是 LineString 坐标数组的数组

3.1.6 Polygon

"coordinates"成员必须是 ps:LinearRing 坐标数组的数组

ps:LinearRing 是具有 4 个或更多位置的封闭 LineString。第一个和最后一个位置是等价的(它们代表等价点)。尽管 LinearRing 没有明确表示为 GeoJSON 几何类型,但它在 Polygon 几何类型定义中被引用。

3.1.7 MultiPolygon

"coordinates"成员必须是 Polygon 坐标数组的数组

3.1.7 GeometryCollection

表示几何对象的集合。

几何集合必须有一个名为 的属性"geometries"。对应的值"geometries"是一个数组。此数组中的每个元素都是一个 GeoJSON 几何对象。

3.2 Feature

"Feature"是一个特征对象:

  • 要素对象必须有一个名为 的成员"geometry"。几何成员的值是上面定义的几何对象或 JSON 空值。
  • 要素对象必须有一个名为 的成员"properties"。属性成员的值是一个对象(任何 JSON 对象或 JSON 空值)。
  • 如果一个特征有一个常用的标识符,该标识符应该作为特征对象的成员包含在名称"id"中。

3.3 FeatureCollection

具有该类型的 GeoJSON 对象"FeatureCollection"是特征集合对象。

类型的对象"FeatureCollection"必须有一个名为 的成员 "features"。对应的值"features"是一个数组。数组中的每个元素都是上面定义的特征对象。

四、CRS

4.1 命名

CRS 对象可以通过名称指示坐标参考系。在这种情况下,其"type"成员的值必须是 字符串"name"。其 "properties"成员的值必须是包含"name"成员的对象。该"name"成员的值必须是标识坐标参考系统的字符串。OGC CRS URN标识符"urn:ogc:def:crs:OGC:1.3:CRS84"应优先于传统标识符,例如"EPSG:4326"

  "crs": {
    "type": "name",
    "properties": {
      "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
      }
    }

4.2 link

一个link对象有一个必需成员:"href"和一个可选成员: "type"

所需"href"成员的值必须是可取消引用的 URI。

可选"type"成员的值必须是一个字符串,该字符串提示用于在提供的 URI 中表示 CRS 参数的格式。建议的值为:"proj4""ogcwkt""esriwkt",但也可以使用其他值:

  "crs": {
    "type": "link", 
    "properties": {
      "href": "http://example.com/crs/42",
      "type": "proj4"
      }
    }

相对链接可用于将处理器定向到辅助文件中的 CRS 参数:

  "crs": {
    "type": "link",
    "properties": {
      "href": "data.crs",
      "type": "ogcwkt"
      }
    }

五、Bounding Boxes

为了包含几何、特征或特征集合的坐标范围信息,GeoJSON 对象可能有一个名为"bbox". bbox 成员的值必须是一个 2*n 数组,其中 n 是包含的几何图形中表示的维数,所有轴的最小值后跟最大值。bbox 的轴顺序遵循几何的轴顺序。此外,假定 bbox 的坐标参考系统与它所属的 GeoJSON 对象的坐标参考系统相匹配。

功能上的 bbox 成员示例:

{ 
    "type": "Feature",
    "bbox": [-10.0, -10.0, 10.0, 10.0],
    "geometry": {
      "type": "Polygon",
      "coordinates": [[
        [-10.0, -10.0], [10.0, -10.0], [10.0, 10.0], [-10.0, 10.0]
        ]]
      }
    ...
}

特征集合上的 bbox 成员示例:

{ 
    "type": "FeatureCollection",
    "bbox": [100.0, 0.0, 105.0, 1.0],
    "features": [
      ...
    ] 
}

PS:示例(FeatureCollection集合中的features成员的单个Feature)

1.Point

点坐标按 x、y 顺序排列(投影坐标为东、北,地理坐标为经度、纬度):

  { "type": "Point", "coordinates": [100.0, 0.0] }

2.LineString

LineString 的坐标是一个位置数组:

  { "type": "LineString",
    "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]
    }

3.Polygon

polygon 的坐标是 LinearRing 坐标数组的数组。数组中的第一个元素代表外环。任何后续元素都表示内部环(或孔)。

3.1 无孔:

  { "type": "Polygon",
    "coordinates": [
      [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
      ]
   }

3.2 带孔:

  { "type": "Polygon",
    "coordinates": [
      [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ],
      [ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ]
      ]
   }

4.MultiPoint

MultiPoint 的坐标是位置数组:

  { "type": "MultiPoint",
    "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]
    }

5.MultiLineString

MultiLineString 的坐标是 LineString 坐标数组的数组:

  { "type": "MultiLineString",
    "coordinates": [
        [ [100.0, 0.0], [101.0, 1.0] ],
        [ [102.0, 2.0], [103.0, 3.0] ]
      ]
    }

6.MultiPolygon

MultiPolygon的坐标是 Polygon 坐标数组的数组:

  { "type": "MultiPolygon",
    "coordinates": [
      [[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],
      [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]],
       [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]
      ]
    }

7.GeometryCollection

GeometryCollection 的几何数组中的每个元素都是上述几何对象之一,它本质上也是一个feature:

  { "type": "GeometryCollection",
    "geometries": [
      { "type": "Point",
        "coordinates": [100.0, 0.0]
        },
      { "type": "LineString",
        "coordinates": [ [101.0, 0.0], [102.0, 1.0] ]
        }
    ]
  }