- GeoJSON
GeoJSON 是用於描述地理空間信息的數據格式。GeoJSON 不是一種新的格式,其語法規范是符合 JSON 格式的,只不過對其名稱進行了規范,專門用於表示地理信息。
GeoJSON 的最外層是一個單獨的對象(object)。這個對象可表示:
幾何體(Geometry)。
特征(Feature)。
特征集合(FeatureCollection)。
最外層的 GeoJSON 里可能包含有很多子對象,每一個 GeoJSON 對象都有一個 type 屬性,表示對象的類型,type 的值必須是下面之一。
Point:點。
MultiPoint:多點。
LineString:線。
MultiLineString:多線。
Polygon:面。
MultiPolygon:多面。
GeometryCollection:幾何體集合。
Feature:特征。
FeatureCollection:特征集合。
下面舉幾個例子。
點對象:
{
"type": "Point",
"coordinates": [ -105, 39 ]
}
1
2
3
4
{
"type": "Point",
"coordinates": [ -105, 39 ]
}
線對象:
{
"type": "LineString",
"coordinates": [[-105, 39 ], [-107, 38 ]]
}
1
2
3
4
{
"type": "LineString",
"coordinates": [[-105, 39 ], [-107, 38 ]]
}
面對象:
{
"type": "Polygon",
"coordinates":[[ [30, 0], [31, 0], [31, 5], [30, 5], [30, 0] ]]
}
1
2
3
4
{
"type": "Polygon",
"coordinates":[[ [30, 0], [31, 0], [31, 5], [30, 5], [30, 0] ]]
}
由以上格式可以發現,每一個對象都有一個成員變量 coordinates。如果 type 的值為 Point、MultiPoint、LineString、MultiLineString、Polygon、MultiPolygon 之一,則該對象必須有變量 coordinates。
如果 type 的值為 GeometryCollection(幾何體集合),那么該對象必須有變量 geometries,其值是一個數組,數組的每一項都是一個 GeoJSON 的幾何對象。例如:
{
"type": "GeometryCollection",
"geometries": [
{
"type": "Point",
"coordinates": [100, 40]
},
{
"type": "LineString",
"coordinates": [ [100, 30], [100, 35] ]
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
{
"type": "GeometryCollection",
"geometries": [
{
"type": "Point",
"coordinates": [100, 40]
},
{
"type": "LineString",
"coordinates": [ [100, 30], [100, 35] ]
}
]
}
如果 type 的值為 Feature(特征),那么此特征對象必須包含有變量 geometry,表示幾何體,geometry 的值必須是幾何體對象。此特征對象還包含有一個 properties,表示特性,properties 的值可以是任意 JSON 對象或 null。例如:
{
"type": "Feature",
"properties": {
"name": "北京"
},
"geometry": {
"type": "Point",
"coordinates": [ 116.3671875, 39.977120098439634]
}
}
1
2
3
4
5
6
7
8
9
10
{
"type": "Feature",
"properties": {
"name": "北京"
},
"geometry": {
"type": "Point",
"coordinates": [ 116.3671875, 39.977120098439634]
}
}
如果 type 的值為 FeatureCollection(特征集合),則該對象必須有一個名稱為 features 的成員。features 的值是一個數組,數組的每一項都是一個特征對象。
- TopoJSON
TopoJSON 是 GeoJSON 按拓撲學編碼后的擴展形式,是由 D3 的作者 Mike Bostock 制定的。相比 GeoJSON 直接使用 Polygon、Point 之類的幾何體來表示圖形的方法,TopoJSON 中的每一個幾何體都是通過將共享邊(被稱為arcs)整合后組成的。
TopoJSON 消除了冗余,文件大小縮小了 80%,因為:
邊界線只記錄一次(例如廣西和廣東的交界線只記錄一次)。
地理坐標使用整數,不使用浮點數。
3. 在線工具
在線生成 GeoJSON:http://geojson.io/
簡化、轉換 GeoJSON 和 TopoJSON:http://mapshaper.org/