目录简介举例空间行状FeatureCollectionFeature几何对象PointMultiPointLineStringMultiLineStringPolygonMultiPolygonGeometryCollection可选属性其他coordinate坐标参考系在 ts 中使用
简介
GeoJSON
是一种使用JSON
来编码各种地理数据结构的格式,是一种轻量级的数据交换格式,可以用来表示几何对象、属性数据、空间参考系统等信息
由两种对象组成:Geometry
(几何对象)和Feature
(空间行状)
几何对象用来描述地理空间中的点、线、面等几何形状空间行状用来描述一个有界的实体,包括几何对象和其他属性信息
几何对象类型有:
点:Point
多点:MultiPoint
线:LineString
多线:MultiLineString
面:Polygon
多面:MultiPolygon
几何集合:GeometryCollection
空间行状类型有:
空间行状:Feature
空间形状集合:FeatureCollection
举例
几何对象和空间行状可以相互嵌套
const GeoJSON = { type: "FeatureCollection", features: [ { type: "Feature", geometry: { type: "Point", coordinates: [121.4737, 31.2304] }, properties: { id: 1 }, }, { type: "Feature", geometry: { type: "Point", coordinates: [121.4837, 31.2504] }, properties: { id: 2 }, }, ], };
空间行状
FeatureCollection
FeatureCollection
是Feature
对象的集合,用来表示一组Feature
对象由
type
和features
两个属性组成:type
属性的值为FeatureCollection
features
属性的值为Feature
对象的数组
const FeatureCollectionJSON = { type: "FeatureCollection", features: [feature], };
Feature
Feature
对象用来表示几何对象的属性信息由
type
、geometry
和properties
三个属性组成:type
属性的值为Feature
,geometry
属性的值为Geometry
几何对象properties
属性的值为属性对象(可选)
const FeatureJSON = { type: "Feature", geometry: { type: "Point", coordinates: [121.4737, 31.2304] }, properties: { id: 1 }, };
几何对象
Point
Point
用来表示一个点由
type
和coordinates
两个属性组成:type
属性的值为Point
coordinates
属性的值为一个数组,数组的第一个元素为经度,第二个元素为纬度
const PointJSON = { type: "Point", coordinates: [121.4737, 31.2304], };
MultiPoint
MultiPoint
用来表示多个点由
type
和coordinates
两个属性组成:type
属性的值为MultiPoint
coordinates
属性的值为一个数组,数组的每个元素都是一个点的坐标
const MultiPointJSON = { type: "MultiPoint", coordinates: [ [121.4737, 31.2304], [121.4837, 31.2504], ], };
LineString
LineString
用来表示一条线由
type
和coordinates
两个属性组成:type
属性的值为LineString
coordinates
属性的值为一个数组,数组的每个元素都是一个点的坐标
const LineStringJSON = { type: "LineString", coordinates: [ [121.4737, 31.2304], [121.4837, 31.2504], ], };
MultiLineString
MultiLineString
用来表示多条线由
type
和coordinates
两个属性组成:type
属性的值为MultiLineString
coordinates
属性的值为一个数组,数组的每个元素都是一个线的坐标数组
const MultiLineStringJSON = { type: "MultiLineString", coordinates: [ [ [121.4737, 31.2304], [121.4837, 31.2504], ], [ [121.4727, 31.2314], [121.4827, 31.2514], ], ], };
Polygon
Polygon
用来表示一个面由
type
和coordinates
两个属性组成:type
属性的值为Polygon
coordinates
属性的值为一个数组,数组的第一个元素为外环的坐标数组,后面的元素为内环的坐标数组
polygon
的坐标数组的第一个元素和最后一个元素是相同的,表示闭合const PolygonJSON = { type: "Polygon", coordinates: [ [ [121.4737, 31.2304], [121.4837, 31.2504], [121.4937, 31.2304], [121.4737, 31.2304], ], [ [121.4717, 31.2314], [121.4827, 31.2524], [121.4937, 31.2334], [121.4757, 31.2344], ], ], };
MultiPolygon
MultiPolygon
用来表示多个面由
type
和coordinates
两个属性组成:type
属性的值为MultiPolygon
coordinates
属性的值为一个数组,数组的每个元素都是一个面的坐标数组
const MultiPolygonJSON = { type: "MultiPolygon", coordinates: [ [ [ [121.4737, 31.2304], [121.4837, 31.2504], [121.4937, 31.2304], [121.4737, 31.2304], ], [ [121.4737, 31.2304], [121.4837, 31.2504], [121.4937, 31.2304], [121.4737, 31.2304], ], ], [ [ [121.4737, 31.2304], [121.4837, 31.2504], [121.4937, 31.2304], [121.4737, 31.2304], ], [ [121.4737, 31.2304], [121.4837, 31.2504], [121.4937, 31.2304], [121.4737, 31.2304], ], ], ], };
GeometryCollection
GeometryCollection
用来表示几何对象的集合由
type
和geometries
两个属性组成:type
属性的值为GeometryCollection
geometries
属性的值为几何对象的数组
const GeometryCollectionJSON = { type: "GeometryCollection", geometries: [ { type: "Point", coordinates: [121.4737, 31.2304] }, { type: "LineString", coordinates: [ [121.4737, 31.2304], [121.4837, 31.2504], ], }, ], };
可选属性 这些属性都是
GeoJSON
的扩展属性,不是GeoJSON
规范的一部分id
属性,用来描述FeatureCollection
的唯一标识bbox
属性,用来描述FeatureCollection
的边界框四至坐标,一般用来做数据裁剪这是一组左上角和右下角的坐标,示例:
[minLon, minLat, maxLon, maxLat]
properties
属性,用来描述FeatureCollection
的属性crs
属性,用来描述坐标参考系
其他
coordinate
coordinate
是一个数组,表示一个点的坐标,数组的长度表示坐标的维度,一般是2
维或3
维2
维:[lon, lat]
3
维:[lon, lat, height]
coordinate
的第一个元素表示经度,第二个元素表示纬度,第三个元素表示高度坐标顺序是
[lon, lat]
,这个是推荐顺序,可由crs
属性指定coordinates
是多维数组:点:
[lon, lat]
线:[[lon, lat], [lon, lat]]
面:[[[lon, lat], [lon, lat]]]
多面:[[[[lon, lat], [lon, lat]]]]
坐标参考系 最常使用的坐标系是
EPSG:4326
和EPSG:3857
:EPSG:4326
是WGS84
(CGCS2000,大地) 坐标系,是GeoJSON
规范的默认坐标系EPSG:3857
是Web Mercator
(墨卡托) 坐标系,是OpenLayers
的默认坐标系
它们的区别:EPSG:4326
是经纬度坐标系,EPSG:3857
是投影坐标系EPSG:4326
的坐标范围是[-180, -90, 180, 90]
,EPSG:3857
的坐标范围是[-20037508.342789244, -20037508.342789244, 20037508.342789244, 20037508.342789244]
EPSG:4326
的坐标单位是度,EPSG:3857
的坐标单位是米EPSG:4326
的坐标原点是[0, 0]
,EPSG:3857
的坐标原点是[-20037508.342789244, -20037508.342789244]
EPSG:4326
的坐标轴方向是[x, y]
,EPSG:3857
的坐标轴方向是[x, -y]
在 ts 中使用 为了在
ts
使用GeoJSON
能够有类型约束,我整理整理了一些GeoJSON
的ts
类型定义和创建GeoJSON
的方法:geojson.d.tsgeojson.helper.ts 举例:
表示一个点和多个点的
GeoJSON
集合:使用geojson.d.ts
type PointType = FeatureCollection<Point | MultiPoint, GeoJsonProperties<T>>; const point2Geojson: PointType<{ id: string; name?: string }> = { type: "FeatureCollection", features: [ { type: "Feature", geometry: { type: "Point", coordinates: [120.4737, 31.2304], }, properties: { id: "12", name: "uccs" }, }, { type: "Feature", geometry: { type: "MultiPoint", coordinates: [ [121.4737, 31.2304], [111.4737, 31.2204], ], }, properties: { id: "1" }, }, ], };创建一个几何对象
使用geojson.helper.ts
const pointGeometry = point<{ id: string }>([120.4737, 31.2304], { id: "1", }); const featureGeoJSON = feature<Point>(pointGeometry);参考
GeoJSONGeoJSON 格式GeoJSON 格式规范EPSG 4326 vs EPSG 3857 (投影,数据集,坐标系)turf.js 以上就是写给小白的地理信息的表示法GeoJSON的详细内容,更多关于GeoJSON地理信息表示法的资料请关注其它相关文章!