GeoJSON入门

242 阅读3分钟

GeoJSON

简介

GeoJSON,是一种使用 JSON数据格式来表示地理数据的数据结构。


坐标系

GeoJSON 的坐标系统都是地理坐标系(经纬度坐标),使用通用的 WGS84 坐标。

经纬度使用作为单位,而非弧度。


GeoJSON 对象

一个 GeoJSON 对象,可以为一个几何对象(Geometry Object)、一个要素对象(Feature Object)或一个要数集对象(FeatureCollection Object)。

Geometry 对象

Geometry 对象(几何对象),用来表示点(points)、线(curves)、面(surfaces )。

每个 Geometry 对象有两个属性:type,coordinates。一个用来表示几何对象的类型,一个为对应几何对象的坐标。

几何类型有以下类型:

  • Point
  • MultiPoint
  • LineString
  • MultiLineString
  • Polygon
  • MultiPolygon
  • GeometryCollection

Position

Position 为地理位置点,其值为一个一个二元元组([a,b])。代表经纬度分别为 a 和 b 的点。

//position:位置,由经纬度坐标数组组成
[longitude, latitude];

Point

Point 对象的type属性为Point; coordinates,为一个二元元组([longitude,latitude]),代表一个点。

示例:

{
  "type": "Point",
  "coordinates": [105.654594, 30.438666]
}

MultiPoint

MultiPoint 对象,即多点对象,其type属性的值为MultiPointcoordinates属性的值为一个二维数组,其中数组每个对象为一个二元元组,代表着一个个的点。

{
  "type": "MultiPoint",
  "coordinates": [
    [105.654594, 30.438666],
    [105.65453, 30.438656]
  ]
}

LineString

LineString 对象,即线对象,其type属性的值为LineStringcoordinates属性的值为一个二维数组,数组中的每一项为一个二元元组,代表线上每个转折点。

示例:

{
  "type": "LineString",
  "coordinates": [
    [105.654594, 30.438666],
    [105.65453, 30.438656]
  ]
}

MultiLineString

LineString 对象,即多线对象。其type属性的值为MultiLineString;由单条线构成多条线,可知,其coordinates属性的值为一个三维数组,其中每一项代表一条线对象的 coordinates 值。

示例:

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

Polygon

Polygon 对象,即面对象。

type属性的值为Polygon

由点构成线,线构成面,可以知道,其实面就是首尾坐标相同的闭合线。其coordinates属性的值为一个线对象coordinates值的数组。

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

MultiPolygon

MultiPolygon 对象,即多面对象。其type属性的值为MultiPolygon,其coordinates属性的值Polygon 对象coordinates值的数组。

示例:

{
  "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]
      ]
    ]
  ]
}

GeometryCollection

GeometryCollection对象,即几何体集合对象。也就是说,一个GeometryCollection对象可以包含一个或多个Point、MultiPoint、LineString、MultiLineString、Polygon、MultiPolygon对象。

type属性的值为GeometryCollection,其coordinates属性的值为一个数组。数组中每一项为Point、MultiPoint、LineString、MultiLineString、Polygon、MultiPolygon对象。

{
  "type": "GeometryCollection",
  "geometries": [
    {
      "type": "Point",
      "coordinates": [23.53123, -63.1123]
    },
    {
      "type": "LineString",
      "coordinates": [
        [-152.6123, 51.2123],
        [5.2123, 10.6123]
      ]
    }
  ]
}

Feature 对象

Feature 对象(要素对象),用来代表具有空间边界几何实体。

  1. Feature 对象,有一个"type"属性,其值为"Feature"
  2. 有一个"geometry"属性,其值为上面的Geometry对象
  3. 有一个"properties"属性,其值为json对象或null。表示要素的属性信息。
  4. 如果需要使用唯一标识标识要素对象,使用"id"属性,其值为number或string。
{
    "type":"Feature",
    "geometry":{
        //上面的Geometry对象
    },
    "properties":null
    "id":12
}

示例:

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [-3.68123, 40.4123]
  },
  "properties": { "country": "Spain" }
}

FeatureCollection 对象

FeatureCollection 对象(要素集)。

  1. 有一个"type"属性,其值为"FeatureCollection"
  2. 有一个"features"属性,其值为Feature对象定义的JSON结构,代表一个一个的要素。
"type":"FeatureCollection",
"features":[]

示例:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [1.643123, -19.12123]
      },
      "properties": {}
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [-80.23123, -22.53123]
      },
      "properties": {}
    }
  ]
}