最近做的項目需要詳細了解geojson,因此查了一些資料,現在整理一份標準格式的記錄,要理解本文需要首先了解json的基本知識,這里不過多展開,可以去參考w3school上的教程,簡言之,json是通過鍵值對表示數據對象的一種格式,可以很好地表達數據,其全稱為JavaScript Object Notation(JavaScript Object Notation),正如這個名稱,JavaScript和json聯系緊密,但是json可以應用的范圍很廣,不止于前端,它比XML數據更輕量、更容易解析(某種角度上說xml可以更自由地封裝更多的數據)。很多編程語言都有對應的json解析庫,例如Python的json庫,C#的Newtonsoft.Json,Java的org.json。geojson是用json的語法表達和存儲地理數據,可以說是json的子集。
例如下面就是一個點數據:
{
"type": "FeatureCollection",
"features": [
{"type":"Feature",
"properties":{},
"geometry":{
"type":"Point",
"coordinates":[105.380859375,31.57853542647338]
}
}
]
}
(注:以下geojson的效果截圖都來自geojson.io在線生成)
geojson將所有的地理要素分為Point、MultiPoint、LineString、MultiLineString、Polygon、MultiPolygon、GeometryCollection。首先是將這些要素封裝到單個的geometry里,然后作為一個個的Feature(也就是要素);要素放到一個要素集合里,從樹狀結構來理解FeatureCollection就是根節點,表示為:
{
"type": "FeatureCollection",
"features": []
}
所有地理要素放在features的列表里。
點要素Point
點要素是最簡單的,類型type對應Point,然后坐標是一個1維的數組,里面有兩個元素(如果是立體的坐標就是三維x,y,z),分別為經度和緯度。properties里面可以封裝各種屬性,例如名稱、標識顏色等等。
{"type":"Feature",
"properties":{},
"geometry":{
"type":"Point",
"coordinates":[105.380859375,31.57853542647338]
}
}
多點要素MultiPoint
{"type":"Feature",
"properties":{},
"geometry":{
"type":"MultiPoint",
"coordinates":[[105.380859375,31.57853542647338],
[105.580859375,31.52853542647338]
]
}
}
其核心坐標:
105.380859375,31.57853542647338
105.580859375,31.52853542647338
線要素LineString
線要素就是指線段,記錄的是線的端點坐標,可視化時會按照記錄順序聯結。對于曲線(如貝塞爾曲線)目前還沒有很好的表達,但是在地理數據中,曲線一般會用LineString去擬合,現實地理世界中也沒有標準的曲線地理要素。
線要素的坐標coordinates里的二維數組和多點要素基本一樣,區別就在type上了。
{"type":"Feature",
"properties":{},
"geometry":{
"type":"LineString",
"coordinates":[[105.6005859375,30.65681556429287],
[107.95166015624999,31.98944183792288],
[109.3798828125,30.031055426540206],
[107.7978515625,29.935895213372444]]
}
}
對應的Kml表達:
<Placemark>
<ExtendedData></ExtendedData>
<LineString>
<coordinates>108.65753173828125,34.1873818599505 108.72413635253905,34.25154099726973 108.77151489257812,34.16977214177208 108.88481140136719,34.229970811273084
</coordinates>
</LineString>
</Placemark>
MultiLineString
也是一個三維數組(和多邊形一樣);
{"type":"Feature",
"properties":{},
"geometry":{
"type":"MultiLineString",
"coordinates":
[
[
[105.6005859375,30.65681556429287],
[107.95166015624999,31.98944183792288],
[109.3798828125,30.031055426540206],
[107.7978515625,29.935895213372444]
],
[
[109.3798828125,30.031055426540206],
[107.1978515625,31.235895213372444]
]
]
}
}
多邊形Polygon
注:單個多邊形是一個3維數組,可以包含多個二維數組,這種情況和MultiPolygon效果很像。
{"type":"Feature",
"properties":{},
"geometry":{
"type":"Polygon",
"coordinates":[
[
[106.10595703125,33.33970700424026],
[106.32568359375,32.41706632846282],
[108.03955078125,32.2313896627376],
[108.25927734375,33.15594830078649],
[106.10595703125,33.33970700424026]
]
]
}
}
多多邊形MultiPolygon
type 1 兩個不會相交的多邊形
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiPolygon",
"coordinates":
[
[
[
[109.2041015625,30.088107753367257],
[115.02685546875,30.088107753367257],
[115.02685546875,32.7872745269555],
[109.2041015625,32.7872745269555],
[109.2041015625,30.088107753367257]
]
],
[
[
[112.9833984375,26.82407078047018],
[116.69677734375,26.82407078047018],
[116.69677734375,29.036960648558267],
[112.9833984375,29.036960648558267],
[112.9833984375,26.82407078047018]
]
]
]
}
}
type 2 兩個鑲套的多邊形
小的在前面,范圍大的在后面,用上4個中括號,但效果不是有洞的
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiPolygon",
"coordinates":
[
[
[
[101.6455078125,27.68352808378776],
[114.78515624999999,27.68352808378776],
[114.78515624999999,35.209721645221386],
[101.6455078125,35.209721645221386],
[101.6455078125,27.68352808378776]
]
],
[
[
[104.2822265625,30.107117887092357],
[108.896484375,30.107117887092357],
[108.896484375,33.76088200086917],
[104.2822265625,33.76088200086917],
[104.2822265625,30.107117887092357]
]
]
]
}
}
type 3 有孔洞的多邊形
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiPolygon",
"coordinates":
[
[
[
[101.6455078125,27.68352808378776],
[114.78515624999999,27.68352808378776],
[114.78515624999999,35.209721645221386],
[101.6455078125,35.209721645221386],
[101.6455078125,27.68352808378776]
],
[
[104.2822265625,30.107117887092357],
[108.896484375,30.107117887092357],
[108.896484375,33.76088200086917],
[104.2822265625,33.76088200086917],
[104.2822265625,30.107117887092357]
]
]
]
}
}
可以仔細去品味type2和type3的區別。它們對應的kml表達區別是比較大的。
GeometryCollection
GeometryCollection是多種基本地理要素的集合,就是里面可以包含點、線、面要素。
{
"type": "GeometryCollection",
"geometries": [
{
"type": "Point",
"coordinates": [108.62, 31.02819]
}, {
"type": "LineString",
"coordinates": [[108.896484375,30.1071178870],
[108.2184375,30.91717870],
[109.5184375,31.2175780]]
}]
}
GeometryCollection不需要放在FeatureCollection里:
{
"type": "FeatureCollection",
"features": []
}
geojson里面還有其他標簽表達其他的屬性,如外包矩形等,其中特別重要的是坐標系統,一般里面的坐標默認為WGS84,當然也可以是其他坐標系統的坐標,但是要標識。這部分內容之后再補充了。
更具體的內容可以參考rfc7946。
歡迎關注本人公眾號,有更多有趣內容和資料: