GeoJSON是一种用JSON格式表示地理空间数据的标准(RFC 7946),前端地图(如Leaflet、Mapbox)经常用它传递点、线、面等几何信息。
名词解释:
**RFC 7946:**GeoJSON 地理数据格式的官方国际标准,2016年发布,取代了08年发布的旧标准。
Leaflet: 是一个开源并且对移动端友好的交互式地图 JavaScript 库。 它大小仅仅只有 42 KB of JS, 并且拥有绝大部分开发者所需要的所有地图特性 ,官网:Leaflet - 一个交互式地图 JavaScript 库 (leafletjs.cn)。
Mapbox: 是全球领先的地图技术服务平台,提供定制化地图、导航、搜索及位置数据解决方案,官网在这,有兴趣的自己看:www.mapbox.com
数据类型:
GeoJSON的基本结构如下:
java
{
"type": "几何类型",
"coordinates": [坐标数据]
}
其中type有七种类型,分别是Point(点)、MultiPoint(多点)、LineString(线串)、MultiLineString(多线串)、Polygon(多边形)、MultiPolygon(多多边形)、GeometryCollection(几何集合)
数据示例:
1. Point(点)
java
{
"type": "Point",
"coordinates": [113.324568, 23.106682]
}
2. MultiPoint(多点)
javascript
{
"type": "MultiPoint",
"coordinates": [
[113.32, 23.10],
[113.33, 23.11],
[113.34, 23.12]
]
}
3. LineString(线串)
javascript
{
"type": "LineString",
"coordinates": [
[113.1, 23.0],
[113.2, 23.1],
[113.3, 23.2]
]
}
4. MultiLineString(多线串)
java
{
"type": "MultiLineString",
"coordinates": [
[[113.1,23.0], [113.2,23.1]],
[[113.3,23.2], [113.4,23.3]]
]
}
// 多条独立的路线
5. Polygon(多边形)
java
{
"type": "Polygon",
"coordinates": [
[
[113.2, 23.0],
[113.4, 23.0],
[113.4, 23.2],
[113.2, 23.2],
[113.2, 23.0]
]
]
}
// 第一个数组是外环,后面的数组是内环(洞),首尾坐标必须相同
6. MultiPolygon(多多边形)
java
{
"type": "MultiPolygon",
"coordinates": [
[
[
[113.2, 23.0],
[113.3, 23.0],
[113.3, 23.1],
[113.2, 23.1],
[113.2, 23.0]
]
],
[
[
[113.4, 23.2],
[113.5, 23.2],
[113.5, 23.3],
[113.4, 23.3],
[113.4, 23.2]
]
]
]
}
// 多个不相连的面,如广东省各市分开的行政区划
7. GeometryCollection(几何集合)
java
{
"type": "GeometryCollection",
"geometries": [{
"type": "Point",
"coordinates": [113.32, 23.10]
},
{
"type": "LineString",
"coordinates": [
[113.1, 23.0],
[113.2, 23.1]
]
}
]
}
// 混合不同类型,注意这里用 "geometries" 而不是 "coordinates"
注意 :type 除了上述 7 种几何类型,还有两种顶层类型用于包裹:
java
// Feature:一个几何 + 它的属性
{
"type": "Feature",
"geometry": { ...
},
"properties": {
"name": "广州塔"
}
}
// FeatureCollection:多个 Feature 的集合(最常见的前端传参格式)
{
"type": "FeatureCollection",
"features": [{ ...
}, { ...
}]
}
//示例数据:
//Feature(要素)
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [113.324568, 23.106682]
},
"properties": {
"name": "广州塔",
"height": 600,
"buildYear": 2010,
"category": "地标"
}
}
//geometry 里放任意几何类型,properties 里放业务属性,可以自定义任意字段。
//FeatureCollection(要素集合)
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [113.324568, 23.106682]
},
"properties": {
"id": "BRIDGE_001",
"name": "珠江大桥",
"bridgeType": "梁桥",
"length": 1200,
"检测等级": "A"
}
},
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[113.100, 23.000],
[113.150, 23.050],
[113.200, 23.100]
]
},
"properties": {
"id": "ROAD_001",
"name": "G107国道广州段",
"roadClass": "国道",
"totalLength": 15000
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [[
[113.20, 23.00],
[113.40, 23.00],
[113.40, 23.20],
[113.20, 23.20],
[113.20, 23.00]
]]
},
"properties": {
"id": "AREA_001",
"name": "广州市区范围",
"adminLevel": "市辖区"
}
}
]
}