WebGIS 中常用空间数据格式
看过的知识不等于学会。唯有用心总结、系统记录,并通过温故知新反复实践,才能真正掌握一二
作为一名摸爬滚打三年的前端开发,开源社区给了我饭碗,我也将所学的知识体系回馈给大家,助你少走弯路!
OpenLayers、Leaflet 快速入门 ,每周保持更新 2 个案例
Cesium 快速入门,每周保持更新 4 个案例
地理空间数据格式介绍
在地理空间信息领域,数据格式是存储、交换和表示地理空间信息的基础。了解这些格式的特点、用途和结构对于进行地理空间分析和开发至关重要
GeoJSON
GeoJSON 基于 JSON 的开放标准,用于地理空间数据交换,是 WebGIS 领域最流行和广泛支持的格式之一。
-
官网:geojson.org,数据示例
-
工具:在线编辑 geojson.io
-
数据格式:
- GeoJSON 对象可以是
Geometry
(几何)、Feature
(特征)或FeatureCollection
(特征集合)。
- GeoJSON 对象可以是
-
几何类型 (Geometry):
Point
(点):[经度, 纬度]
LineString
(线):[[经度1, 纬度1], [经度2, 纬度2], ...]
Polygon
(多边形):[[[经度1, 纬度1], ...], [[内环经度1, 内环纬度1], ...]]
(第一个环是外环,后续是内环/洞)MultiPoint
(多点)MultiLineString
(多线)MultiPolygon
(多多边形)GeometryCollection
(几何集合)
-
特征 (Feature):包含一个
geometry
对象和一个properties
对象(用于存储非地理属性)。
{"type": "Feature","geometry": {"type": "Point","coordinates": [102.0, 0.5]},"properties": {"name": "Dinagat Islands"}
}
- 特征集合 (FeatureCollection):包含一个
features
数组,每个元素都是一个Feature
对象。
{"type": "FeatureCollection","features": [{"type": "Feature","geometry": {"type": "Point","coordinates": [102.0, 0.5]},"properties": {"name": "Dinagat Islands"}},{"type": "Feature","geometry": {"type": "LineString","coordinates": [[102.0, 0.0],[103.0, 1.0],[104.0, 0.0],[105.0, 1.0]]},"properties": {"name": "Line"}}]
}
WKT (Well-Known Text)
-
数据格式:文本格式描述几何对象(点、线、面)和坐标系
-
几何类型:
POINT (x y)
:POINT (10 20)
LINESTRING (x1 y1, x2 y2, ...)
:LINESTRING (10 20, 30 40, 50 60)
POLYGON ((x1 y1, x2 y2, ..., x1 y1), (hole_x1 hole_y1, ...))
POLYGON ((10 10, 20 10, 20 20, 10 20, 10 10))
MULTIPOINT
,MULTILINESTRING
,MULTIPOLYGON
,GEOMETRYCOLLECTION
-
可以包含 Z (高程) 和 M (测量值) 维度,例如
POINT Z (10 20 30)
。
几何类型 | WKT 格式 | GeoJSON 格式 |
---|---|---|
Point | POINT (30 10) | { "type": "Point", "coordinates": [30, 10] } |
LineString | LINESTRING (30 10, 10 30, 40 40) | { "type": "LineString", "coordinates": [[30, 10], [10, 30], [40, 40]] } |
Polygon | POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10)) | { "type": "Polygon", "coordinates": [[[30, 10], [40, 40], [20, 40], [10, 20], [30, 10]]] } |
Polygon | POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30)) | { "type": "Polygon", "coordinates": [[[35, 10], [45, 45], [15, 40], [10, 20], [35, 10]], [[20, 30], [35, 35], [30, 20], [20, 30]]] } |
MultiPoint | MULTIPOINT ((10 40), (40 30), (20 20), (30 10)) | { "type": "MultiPoint", "coordinates": [[10, 40], [40, 30], [20, 20], [30, 10]] } |
MultiPoint | MULTIPOINT (10 40, 40 30, 20 20, 30 10) | { "type": "MultiPoint", "coordinates": [[10, 40], [40, 30], [20, 20], [30, 10]] } |
MultiLineString | MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10)) | { "type": "MultiLineString", "coordinates": [[[10, 10], [20, 20], [10, 40]], [[40, 40], [30, 30], [40, 20], [30, 10]]] } |
MultiPolygon | MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5))) | { "type": "MultiPolygon", "coordinates": [[[[30, 20], [45, 40], [10, 40], [30, 20]]], [[[15, 5], [40, 10], [10, 20], [5, 10], [15, 5]]] } |
MultiPolygon | MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20))) | { "type": "MultiPolygon", "coordinates": [[[[40, 40], [20, 45], [45, 30], [40, 40]]], [[[20, 35], [10, 30], [10, 10], [30, 5], [45, 20], [20, 35]], [[30, 20], [20, 15], [20, 25], [30, 20]]] } |
WKB (Well-Known Binary)
WKB 是二进制编码的几何数据,与 WKT 等价但更高效。
-
数据格式:
- 二进制字节流,不可直接人类阅读。
- 包含字节序(大端/小端)、几何类型代码、坐标数据等。
- 例如,一个点
POINT (10 20)
的 WKB 可能会是010100000000000000000024400000000000003440
(十六进制表示)。
TopoJSON
TopoJSON 是 GeoJSON 的一个扩展,它在编码地理空间数据时包含了拓扑信息。这意味着共享的边界(例如,相邻国家之间的边界)只存储一次,从而显著减小文件大小,并支持拓扑操作(如合并、简化)。
-
官网:topojson.org
-
工具:通常与 D3.js 等可视化库结合使用,例如
d3-geo-topojson
。 -
数据格式:
- TopoJSON 对象是一个
Topology
,包含objects
(地理特征)、arcs
(共享的几何路径)和transform
(坐标转换信息)。 arcs
: 存储所有几何图形的原始坐标路径。共享的边界会作为单个弧线存储。objects
: 引用arcs
中的索引来构建几何图形。每个对象可以是一个Point
、LineString
、Polygon
或它们的集合,但其坐标不再直接存储,而是通过arcs
的索引和方向来表示。transform
: 包含scale
和translate
值,用于将量化后的整数坐标转换回原始的浮点坐标,进一步减小文件大小。
- TopoJSON 对象是一个
{"type": "Topology","transform": {"scale": [0.0005, 0.0005],"translate": [100, 0]},"objects": {"example_polygons": {"type": "GeometryCollection","geometries": [{"type": "Polygon","arcs": [[0]],"properties": { "name": "Polygon A" }},{"type": "Polygon","arcs": [[1, 2]],"properties": { "name": "Polygon B" }}]}},"arcs": [[[0, 0],[100, 0],[100, 100],[0, 100],[0, 0]],[[100, 0],[200, 0],[200, 100],[100, 100]],[[100, 100],[0, 100]]]
}
- 文件大小更小:通过消除冗余的边界数据,文件大小通常比 GeoJSON 小 80% 左右。
KML (Keyhole Markup Language)
KML 是一种基于 XML 的文件格式,用于在地球浏览器(如 Google Earth)中显示地理空间数据。它不仅可以表示几何图形,还可以包含样式、描述、时间信息和相机视角等丰富的可视化属性。
-
数据格式:
- XML 结构,包含
<Placemark>
(地标)、<Point>
、<LineString>
、<Polygon>
等标签。 - 可以定义样式 (
<Style>
)、描述 (<description>
)、名称 (<name>
)、图标 (<Icon>
) 等。 - 支持文件夹 (
<Folder>
) 组织数据。
- XML 结构,包含
<?xml version="1.0" encoding="UTF-8"?><kmlxmlns="http://www.opengis.net/kml/2.2"xmlns:ns2="http://www.google.com/kml/ext/2.2"xmlns:ns3="http://www.w3.org/2005/Atom"xmlns:ns4="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"
><Document><LookAt><longitude>-74.01020256421765</longitude><latitude>40.70974625546435</latitude><altitude>694.1959814771428</altitude><heading>0.0</heading><tilt>0.0</tilt><range>560.9467647722848</range><altitudeMode>clampToGround</altitudeMode></LookAt><NetworkLink><name>Manhattan (NY) points of interest</name><visibility>1</visibility><open>1</open><description>Points of interest in New York, New York (on Manhattan). One of theattributes contains the name of a file with a picture of the point ofinterest.</description><LookAt><longitude>-74.00668161555254</longitude><latitude>40.71371615223534</latitude><altitude>2173.081355567034</altitude><heading>0.0</heading><tilt>0.0</tilt><range>1755.96371704499</range><altitudeMode>clampToGround</altitudeMode></LookAt><Url><href>http://localhost:8520/geoserver/tiger/wms?service=wms&request=GetMap&version=1.1.1&format=application/vnd.google-earth.kml+xml&layers=poi&styles=poi&height=2048&width=2048&transparent=false&srs=EPSG:4326&format_options=AUTOFIT:true;KMATTR:true;KMPLACEMARK:false;KMSCORE:40;MODE:refresh;SUPEROVERLAY:false</href><refreshInterval>0.0</refreshInterval><viewRefreshMode>onStop</viewRefreshMode><viewRefreshTime>1.0</viewRefreshTime><viewBoundScale>1.0</viewBoundScale></Url></NetworkLink></Document>
</kml>
- 易于分享:单个
.kml
或.kmz
(压缩包) 文件即可包含所有数据和样式。 - 官网/规范:OGC KML
- 在线查看器/编辑器:Google Earth Pro (桌面版), KML Viewer
Shapefile
Shapefile 广泛应用于桌面 GIS 软件中。它实际上不是一个单一文件,而是一组相关文件的集合,共同构成一个完整的地理数据集。
-
数据格式:
-
一个完整的 Shapefile 至少包含以下三个文件:
.shp
:存储几何图形数据(点、线、面)。.shx
:存储几何图形的索引,用于快速查找。.dbf
:存储属性数据,以 dBase 格式组织。
-
常见可选文件:
.prj
:存储投影信息(WKT 格式)。.cpg
:存储字符编码信息。.sbn
,.sbx
:空间索引。
GIS 数据转换器
geosaas.com
-
支持 DWG、SHP、MDB、Kml、Kmz、Gpx、GeoJson、EXCEL、TXT、CSV、SQL Server、MySQL、PostgreSQL、JPG、PDF、GeoTiff、Image(img)、Bitmap(bmp)、Png 格式之间的批量转换;
-
支持国家 2000、西安 80、北京 54、WGS84、火星坐标、百度坐标、墨卡托坐标等坐标系任意转换;支持批量转换、图层合并和地图数据浏览;