GeoJSON是什么
GeoJSON是一种基于JSON的地理空间数据格式。
它主要用于描述:
- 点
- 线
- 面
- 多点
- 多线
- 多面
- 空间要素
- 要素集合
GeoJSON特别适合Web GIS,因为它使用JSON结构,可以直接被JavaScript以及各种Web GIS框架读取和处理。
例如:
{
"type": "Point",
"coordinates": [116.397, 39.908]
}
表示一个Point几何对象。
GeoJSON包含什么
GeoJSON主要由以下几类对象组成:
GeoJSON
├── Geometry
│ ├── Point
│ ├── LineString
│ ├── Polygon
│ ├── MultiPoint
│ ├── MultiLineString
│ └── MultiPolygon
│
├── Feature
│ ├── Geometry
│ └── Properties
│
└── FeatureCollection
└── Features
可以简单理解为:
Geometry
↓
描述空间形状
Feature
↓
Geometry + 属性
FeatureCollection
↓
多个Feature
Geometry是什么
Geometry用于描述空间对象的几何形状。
GeoJSON常见Geometry类型包括:
- Point
- LineString
- Polygon
- MultiPoint
- MultiLineString
- MultiPolygon
- GeometryCollection
例如:
{
"type": "Point",
"coordinates": [116.397, 39.908]
}
其中:
type
↓
Geometry类型
coordinates
↓
空间坐标
GeoJSON中的Geometry主要负责回答:
对象在哪里,以及它是什么形状。
Point
Point表示一个点。
例如:
{
"type": "Point",
"coordinates": [116.397, 39.908]
}
通常可以理解为:
X = 116.397
Y = 39.908
如果使用经纬度坐标,通常表示:
经度 = 116.397
纬度 = 39.908
Point常用于:
- POI
- 城市
- 门店
- 学校
- 医院
- 摄像头
- 传感器
- GPS定位点
LineString
LineString表示一条线。
例如:
{
"type": "LineString",
"coordinates": [
[116.397, 39.908],
[116.405, 39.915],
[116.412, 39.920]
]
}
多个坐标点按照顺序连接:
Point 1
↓
Point 2
↓
Point 3
就形成一条LineString。
LineString常用于:
- 道路
- 河流
- 铁路
- 管线
- GPS轨迹
- 路径
Polygon
Polygon表示一个封闭区域。
例如:
{
"type": "Polygon",
"coordinates": [
[
[116.390, 39.900],
[116.400, 39.900],
[116.400, 39.910],
[116.390, 39.910],
[116.390, 39.900]
]
]
}
Polygon常用于:
- 行政区
- 建筑物
- 地块
- 公园
- 湖泊
- 土地利用区域
Polygon的外部边界需要形成闭合环:
起点
↓
多个坐标
↓
回到起点
也就是说,第一个坐标和最后一个坐标通常需要保持一致。
MultiPoint
MultiPoint表示多个相互独立的点。
例如:
{
"type": "MultiPoint",
"coordinates": [
[116.397, 39.908],
[116.405, 39.915],
[116.412, 39.920]
]
}
可以用于:
- 多个采样点
- 多个监测点
- 一组设施位置
- 多个空间事件
与多个独立Point相比,MultiPoint可以将多个点作为一个Geometry进行管理。
MultiLineString
MultiLineString表示多个LineString组成的Geometry。
例如:
{
"type": "MultiLineString",
"coordinates": [
[
[116.397, 39.908],
[116.405, 39.915]
],
[
[116.420, 39.920],
[116.430, 39.925]
]
]
}
可以用于表示:
- 多段道路
- 多段河流
- 铁路网络
- 多段管线
- 多段轨迹
MultiLineString中的每一条线可以是相互独立的。
MultiPolygon
MultiPolygon表示多个Polygon组成的Geometry。
例如:
{
"type": "MultiPolygon",
"coordinates": [
[
[
[116.390, 39.900],
[116.400, 39.900],
[116.400, 39.910],
[116.390, 39.910],
[116.390, 39.900]
]
],
[
[
[116.420, 39.920],
[116.430, 39.920],
[116.430, 39.930],
[116.420, 39.930],
[116.420, 39.920]
]
]
]
}
常用于:
- 群岛
- 多个不连续行政区域
- 多个地块
- 多个独立区域
Feature是什么
Feature是GeoJSON中非常重要的对象。
它将:
Geometry
+
Properties
组合在一起。
例如:
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [116.397, 39.908]
},
"properties": {
"name": "北京",
"type": "城市"
}
}
可以理解为:
Feature
├── Geometry
│ └── 空间位置和形状
│
└── Properties
└── 属性信息
因此:
Geometry
↓
只描述空间信息
Feature
↓
空间信息 + 业务属性
Properties是什么
Properties用于保存Feature的属性信息。
例如:
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [116.397, 39.908]
},
"properties": {
"name": "北京",
"population": 21893095,
"capital": true
}
}
其中:
name
population
capital
都是属性。
Properties可以保存:
- 名称
- 编号
- 类型
- 状态
- 数量
- 时间
- 分类
- 业务信息
FeatureCollection是什么
FeatureCollection用于组织多个Feature。
例如:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [116.397, 39.908]
},
"properties": {
"name": "北京"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [121.473, 31.230]
},
"properties": {
"name": "上海"
}
}
]
}
结构可以理解为:
FeatureCollection
├── Feature
├── Feature
└── Feature
一个地图图层中的多个空间对象,经常使用FeatureCollection进行组织。
Geometry与Feature的区别
Geometry:
只描述空间形状和位置
Feature:
Geometry
+
Properties
因此:
Geometry
↓
空间位置 + 空间形状
Feature
↓
Geometry + Attributes
GeoJSON坐标
GeoJSON的核心数据之一就是coordinates。
二维坐标通常包含:
X
Y
例如:
[116.397, 39.908]
在经纬度数据中通常可以理解为:
X = 经度
Y = 纬度
也就是:
[经度, 纬度]
GeoJSON坐标顺序
GeoJSON中的二维坐标通常按照:
[经度, 纬度]
排列。
例如:
[116.397, 39.908]
表示:
经度 = 116.397
纬度 = 39.908
这是处理GeoJSON时非常常见的错误来源。
GeoJSON的Z坐标
GeoJSON坐标可以扩展到三维。
二维:
X Y
三维:
X Y Z
例如:
{
"type": "Point",
"coordinates": [116.397, 39.908, 50]
}
其中Z可以用于表示:
- 高程
- 建筑高度
- 地形高度
- 三维空间位置
GeoJSON与坐标系
GeoJSON中的坐标数字本身并不能完整说明地理位置。
例如:
116.397
39.908
还需要知道这些坐标属于什么坐标参考系统。
常见坐标系包括:
EPSG:4326
以及:
EPSG:3857
可以理解为:
GeoJSON
↓
Coordinates
↓
CRS
↓
确定空间位置
GeoJSON与EPSG:4326
GeoJSON经常使用WGS 84地理坐标。
例如:
{
"type": "Point",
"coordinates": [116.397, 39.908]
}
可以表示:
经度:116.397
纬度:39.908
这类经纬度数据通常对应:
EPSG:4326
GeoJSON与EPSG:3857
Web地图中经常使用EPSG:3857进行地图显示。
因此常见的数据流程是:
GeoJSON
↓
EPSG:4326
↓
坐标转换
↓
EPSG:3857
↓
Web地图
需要注意:
GeoJSON中的坐标和地图显示坐标不一定使用相同的坐标参考系统。
GeoJSON与WKT
WKT(Well-Known Text)也是一种常见的空间数据表示方式。
例如Point:
POINT (116.397 39.908)
GeoJSON:
{
"type": "Point",
"coordinates": [116.397, 39.908]
}
两者都可以表示Point Geometry。
区别在于:
WKT
↓
文本形式的Geometry
GeoJSON
↓
JSON形式的空间数据
GeoJSON与CSV
CSV经常用于保存坐标数据。
例如:
id,name,longitude,latitude
1,北京,116.397,39.908
2,上海,121.473,31.230
3,广州,113.264,23.129
可以转换为:
CSV
↓
longitude + latitude
↓
Point
↓
Feature
↓
FeatureCollection
↓
GeoJSON
GeoJSON与Shapefile
GeoJSON和Shapefile都是常见的GIS矢量数据格式。
| 格式 | 特点 | 常见场景 |
|---|---|---|
| GeoJSON | JSON、Web友好 | Web GIS |
| Shapefile | 传统GIS格式 | 桌面GIS |
| WKT | 文本Geometry | 空间数据交换 |
| KML | XML格式 | 地图数据 |
| GeoPackage | 单文件数据库 | GIS数据管理 |
GeoJSON与JavaScript
因为GeoJSON本质上使用JSON数据结构,所以JavaScript可以直接处理。
例如:
const geojson = {
type: 'Point',
coordinates: [116.397, 39.908]
}
console.log(geojson.coordinates)
Feature也可以直接访问:
const feature = {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [116.397, 39.908]
},
properties: {
name: '北京'
}
}
console.log(feature.properties.name)
GeoJSON与OpenLayers
OpenLayers可以直接读取GeoJSON。
典型流程:
GeoJSON
↓
GeoJSON Format
↓
Feature
↓
VectorSource
↓
VectorLayer
↓
Map
例如:
const format = new GeoJSON()
const features = format.readFeatures(data)
然后将Feature添加到VectorSource:
const source = new VectorSource({
features
})
最后将VectorSource交给VectorLayer:
VectorSource
↓
VectorLayer
↓
Map
GeoJSON与地图可视化
GeoJSON可以结合Properties实现地图可视化。
例如:
status
↓
颜色
type
↓
图标
population
↓
大小
category
↓
样式
常见流程:
GeoJSON
↓
Feature
↓
Properties
↓
Style
↓
Vector Layer
↓
地图
GeoJSON常见错误
coordinates结构错误
Point:
{
"type": "Point",
"coordinates": [116.397, 39.908]
}
不能错误地写成:
{
"type": "Point",
"coordinates": [
[116.397, 39.908]
]
}
因为Point只需要一组坐标。
经纬度顺序错误
通常应该:
[经度, 纬度]
例如:
[116.397, 39.908]
不要错误地写成:
[39.908, 116.397]
Polygon没有闭合
Polygon外环通常需要:
A → B → C → D → A
也就是第一个坐标和最后一个坐标相同。
Geometry类型错误
例如:
type = Point
却使用了Polygon的coordinates结构,就会导致GeoJSON结构错误。
type必须与coordinates结构对应。
GeoJSON有效性
一个GeoJSON文件不仅需要满足JSON语法,还需要满足GeoJSON的数据结构要求。
常见问题包括:
JSON语法错误
Geometry类型错误
coordinates结构错误
Polygon未闭合
Feature结构错误
FeatureCollection结构错误
坐标为空
坐标顺序错误
因此实际使用GeoJSON时,可以进行:
GeoJSON
↓
解析
↓
验证
↓
格式化
↓
地图预览
GeoJSON知识结构
可以把GeoJSON总结为:
GeoJSON
│
├── Geometry
│ ├── Point
│ ├── LineString
│ ├── Polygon
│ ├── MultiPoint
│ ├── MultiLineString
│ └── MultiPolygon
│
├── Feature
│ ├── Geometry
│ ├── Properties
│ └── ID
│
└── FeatureCollection
└── Features
核心关系:
Coordinates
↓
Geometry
↓
Feature
↓
FeatureCollection
↓
Vector Layer
↓
Map
总结
GeoJSON是一种基于JSON的地理空间数据格式,非常适合Web GIS和前端地图开发。
最重要的几个概念是:
Geometry
↓
描述空间形状
Feature
↓
Geometry + Properties
FeatureCollection
↓
多个Feature
常见Geometry包括:
Point
LineString
Polygon
MultiPoint
MultiLineString
MultiPolygon
GeoJSON二维坐标通常按照:
[经度, 纬度]
表示。
可以记住:
Coordinates描述坐标,Geometry描述空间形状,Feature将Geometry与属性信息结合起来,FeatureCollection则用于组织多个Feature。
在Web GIS中,GeoJSON通常承担:
空间数据
↓
GeoJSON
↓
前端解析
↓
Vector Layer
↓
地图显示
因此GeoJSON是学习Web GIS、OpenLayers、MapLibre以及前端空间数据处理时必须掌握的基础格式。
相关工具
使用 IYATools 在线工具快速处理 GIS 数据