GeoJSON是一种对地理数据结构进行编码的格式。
GeoJSON对象可以表示几何信息、要素或者要素集合。
GeoJSON支持下面几何类型:Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon和GeometryCollection。
GeoJSON里的要素包含一个几何对象和属性对象,要素集合表示多个要素的集合。一个完整的GeoJSON数据结构总是一个JSON对象。在GeoJSON里,对象由键值对的集合组成。对每个成员来说,名字总是字符串。
成员的值要么是字符串、数字、对象、数组,要么是"true","false"和"null",其中数组和对象都是由以上类型的值组成。
GeoJSON 官方规范定义了以下几种对象类型:
- Point(点) – 表示一个具有一组地理坐标的点
- LineString(线) – 表示一个由线段组成的路径
- Polygon(面)-- 包括由线形环组成的封闭面
- MultiPoint(多个点) – 包含多个点的集合
- MultiLineString(多条线) – 包含多条线的集合
- MultiPolygon(多个面) – 包含多个面的集合
- GeometryCollection(几何图元集合) – 包含多个简单几何对象的集合
- Feature(要素) – 表示一个要素,包含一个几何图元和其他属性
- FeatureCollection(要素集) – 包含多个要素的集合
GeoJSON总是由一个单独的对象组成。这个对象表示几何、要素或者要素集合,对象可能有任意数目成员(键值对)。
- GeoJSON对象必须有一个名字为"type"的成员。这个成员的值是由GeoJSON对象的类型所确定的字符串。
type成员的值必须是下面之一:“Point”, “MultiPoint”, “LineString”, “MultiLineString”, “Polygon”, “MultiPolygon”, “GeometryCollection”, “Feature”, 或者 “FeatureCollection”。(类似上面的代码)。
GeoJSON对象可能有一个可选的"crs"成员,它的值必须是一个坐标参考系统的对象(见3.坐标参考系统对象)
{ "type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [104.0, 30.0]
},
"properties": {
"name": "test point"
}
}
在这个示例中,"type":"Feature" 表示这是一个要素对象。"geometry" 包含着要素对象的几何信息,"properties" 包含着要素对象的其他属性。 "geometry" 对象中的 "type":"Point" 表示这是一个点对象,而 "coordinates" 数组则包含着该点的经纬度坐标。
使用 GeoJSON 可以方便地存储和传输地图和空间数据,并且易于处理。GeoJSON 格式已被广泛应用于 Web 地图应用程序、GPS 设备、地图编辑器和空间数据分析工具等领域。
1、Point
对类型"Point"来说,“coordinates"成员必须是一个单独的position。
// Point(点)
{
"type": "Point",
"coordinates": [104.6, 30.1]
}
2、LineString
对类型"LineString"来说,“coordinates"成员必须是两个或者多个position的数组。
线性环是具有4个或者更多position的封闭的线。第一个和最后一个位置是相同的(它们表示相同的点)。虽然线性环没有鲜明地作为GeoJSON几何类型,不过在面几何类型定义里有提到它。
// LineString(线)
{
"type": "LineString",
"coordinates": [
[104.6, 30.1],
[104.7, 30.2],
[104.8, 30.3]
]
}
3、Polygon
Polygons由GeoJSON LinearRing坐标数组组成。这些 LinearRings都是闭合线(Closed LineStrings)。
闭合线(Closed LineStrings)的至少具有四个坐标对(coordinate pairs),并指定与第一个和最后一个坐标相同的位置。
3.1、单环多边形(Polygons with a Single Ring)
以下示例指定了一个带外部环但没有内部环(或孔)的GeoJSON多边形(Polygon)。第一个和最后一个坐标必须匹配才能闭合这个多边形:
对类型"Polygon"来说,"coordinates"成员必须是一个线性环坐标数组的数组。对拥有多个环的的面来说,第一个环必须是外部环,其他的必须是内部环或者孔,而且内部环和外部环的走向是相反的。
// Polygon(面)
{
"type": "Polygon",
"coordinates": [
[
[104.6, 30.1],
[104.7, 30.2],
[104.8, 30.3]
[104.6, 30.1]
]
]
}
对于带有单个环的多边形,环不能自相交。
没有孔的:
{
"type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
]
}
有孔的(外环和内环的方向始终相反):
{
"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] ]
]
}
3.2、多环多边形(Polygons with Multiple Rings)
对于具有多个环的多边形:
- 描述的第一个环必须是外环。
- 外环不能自相交。
- 任何内环必须完全由外环容纳。
- 内环不能相交或重叠。内环不能共享边缘。
以下示例表示一个带内环的GeoJSON多边形:
{
"type" : "Polygon",
"coordinates" : [
[ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ],
[ [ 2 , 2 ] , [ 3 , 3 ] , [ 4 , 2 ] , [ 2 , 2 ] ]
]
}
4、MultiPoint
对类型"MultiPoint"来说,"coordinates"成员必须是position数组。
// MultiPoint(多个点)
{
"type": "MultiPoint",
"coordinates": [
[125.6, 10.1],
[125.7, 10.2],
[125.8, 10.3]
]
}
5、MultiLineString
对类型“MultiLineString"来说,"coordinates"成员必须是一个线坐标数组的数组。
// MultiLineString(多条线)
{
"type": "MultiLineString",
"coordinates": [
[
[125.6, 10.1],
[126.0, 11.0],
[126.4, 12.0]
],
[
[125.6, 10.1],
[125.7, 10.2],
[125.8, 10.3]
]
]
}
6、MultiPolygon
// MultiPolygon(多个面)
{
"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
类型为"FeatureCollection"的GeoJSON对象是特征集合对象。
类型为"FeatureCollection"的对象必须有一个名字为"features"的成员。与“features"相对应的值是一个数组。这个数组中的每个元素都是上面定义的feature对象。
// GeometryCollection(几何图元集合)
{
"type": "GeometryCollection",
"geometries": [
{
"type": "Point",
"coordinates": [125.6, 10.1]
},
{
"type": "LineString",
"coordinates": [
[125.6, 10.1],
[126.0, 11.0],
[126.4, 12.0]
]
},
{
"type": "Polygon",
"coordinates": [
[
[100.0, 0.0],
[101.0, 0.0],
[101.0, 1.0],
[100.0, 1.0],
[100.0, 0.0]
]
]
}
]
}
8、Feature
要素对象必须有一个名字为"geometry"的成员,这个几何成员的值是上面定义的geometry对象或者JSON的null值。
要素对象必须有一个名字为“properties"的成员,这个属性成员的值是一个对象(任何JSON对象或者JSON的null值)。
如果要素是常用的标识符,那么这个标识符应当包含名字为“id”的对象成员。
// Feature(地物)
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [125.6, 10.1]
},
"properties": {
"name": "test point"
}
}
9、FeatureCollection
// FeatureCollection(地物集)
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [125.6, 10.1]
},
"properties": {
"name": "test point 1"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [125.7, 10.2]
},
"properties": {
"name": "test point 2"
}
}
]
}
其他说明:
点:[103.8810, 31.0896]
线:[[103.7767, 30.8424],[104.2546, 30.8150],[104.3068, 30.4552]]
面:[[[103.8810, 31.0896],[104.0129, 30.8891],[103.7520, 30.8809],[103.8810, 31.0896]]]
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [102.0, 0.5, 1000.0] // 这里的三个值分别表示 x, y, z 坐标
},
"properties": {
"name": "Example Point"
}
}
在这个例子中,geometry
对象的 type
是 "Point"
,coordinates
数组包含三个值,分别表示该点在 x、y 和 z 轴上的坐标。
如果你想表示一个三维的多边形,可以这样:
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[100.0, 0.0, 1000.0],
[101.0, 0.0, 1000.0],
[101.0, 1.0, 1000.0],
[100.0, 1.0, 1000.0],
[100.0, 0.0, 1000.0]
]
]
},
"properties": {
"name": "Example Polygon"
}
}