寫給小白學(xué)習(xí)的地理信息的表示法GeoJSON
簡(jiǎn)介
GeoJSON
是一種使用 JSON
來(lái)編碼各種地理數(shù)據(jù)結(jié)構(gòu)的格式,是一種輕量級(jí)的數(shù)據(jù)交換格式,可以用來(lái)表示幾何對(duì)象、屬性數(shù)據(jù)、空間參考系統(tǒng)等信息
由兩種對(duì)象組成:Geometry
(幾何對(duì)象)和 Feature
(空間行狀)
- 幾何對(duì)象用來(lái)描述地理空間中的點(diǎn)、線、面等幾何形狀
- 空間行狀用來(lái)描述一個(gè)有界的實(shí)體,包括幾何對(duì)象和其他屬性信息
幾何對(duì)象類型有:
- 點(diǎn):
Point
- 多點(diǎn):
MultiPoint
- 線:
LineString
- 多線:
MultiLineString
- 面:
Polygon
- 多面:
MultiPolygon
- 幾何集合:
GeometryCollection
空間行狀類型有:
- 空間行狀:
Feature
- 空間形狀集合:
FeatureCollection
舉例
幾何對(duì)象和空間行狀可以相互嵌套
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
對(duì)象的集合,用來(lái)表示一組 Feature
對(duì)象
由 type
和 features
兩個(gè)屬性組成:
type
屬性的值為FeatureCollection
features
屬性的值為Feature
對(duì)象的數(shù)組
const FeatureCollectionJSON = { type: "FeatureCollection", features: [feature], };
Feature
Feature
對(duì)象用來(lái)表示幾何對(duì)象的屬性信息
由 type
、geometry
和 properties
三個(gè)屬性組成:
type
屬性的值為Feature
,geometry
屬性的值為Geometry
幾何對(duì)象properties
屬性的值為屬性對(duì)象(可選)
const FeatureJSON = { type: "Feature", geometry: { type: "Point", coordinates: [121.4737, 31.2304] }, properties: { id: 1 }, };
幾何對(duì)象
Point
Point
用來(lái)表示一個(gè)點(diǎn)
由 type
和 coordinates
兩個(gè)屬性組成:
type
屬性的值為Point
coordinates
屬性的值為一個(gè)數(shù)組,數(shù)組的第一個(gè)元素為經(jīng)度,第二個(gè)元素為緯度
const PointJSON = { type: "Point", coordinates: [121.4737, 31.2304], };
MultiPoint
MultiPoint
用來(lái)表示多個(gè)點(diǎn)
由 type
和 coordinates
兩個(gè)屬性組成:
type
屬性的值為MultiPoint
coordinates
屬性的值為一個(gè)數(shù)組,數(shù)組的每個(gè)元素都是一個(gè)點(diǎn)的坐標(biāo)
const MultiPointJSON = { type: "MultiPoint", coordinates: [ [121.4737, 31.2304], [121.4837, 31.2504], ], };
LineString
LineString
用來(lái)表示一條線
由 type
和 coordinates
兩個(gè)屬性組成:
type
屬性的值為LineString
coordinates
屬性的值為一個(gè)數(shù)組,數(shù)組的每個(gè)元素都是一個(gè)點(diǎn)的坐標(biāo)
const LineStringJSON = { type: "LineString", coordinates: [ [121.4737, 31.2304], [121.4837, 31.2504], ], };
MultiLineString
MultiLineString
用來(lái)表示多條線
由 type
和 coordinates
兩個(gè)屬性組成:
type
屬性的值為MultiLineString
coordinates
屬性的值為一個(gè)數(shù)組,數(shù)組的每個(gè)元素都是一個(gè)線的坐標(biāo)數(shù)組
const MultiLineStringJSON = { type: "MultiLineString", coordinates: [ [ [121.4737, 31.2304], [121.4837, 31.2504], ], [ [121.4727, 31.2314], [121.4827, 31.2514], ], ], };
Polygon
Polygon
用來(lái)表示一個(gè)面
由 type
和 coordinates
兩個(gè)屬性組成:
type
屬性的值為Polygon
coordinates
屬性的值為一個(gè)數(shù)組,數(shù)組的第一個(gè)元素為外環(huán)的坐標(biāo)數(shù)組,后面的元素為內(nèi)環(huán)的坐標(biāo)數(shù)組
polygon
的坐標(biāo)數(shù)組的第一個(gè)元素和最后一個(gè)元素是相同的,表示閉合
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
用來(lái)表示多個(gè)面
由 type
和 coordinates
兩個(gè)屬性組成:
type
屬性的值為MultiPolygon
coordinates
屬性的值為一個(gè)數(shù)組,數(shù)組的每個(gè)元素都是一個(gè)面的坐標(biāo)數(shù)組
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
用來(lái)表示幾何對(duì)象的集合
由 type
和 geometries
兩個(gè)屬性組成:
type
屬性的值為GeometryCollection
geometries
屬性的值為幾何對(duì)象的數(shù)組
const GeometryCollectionJSON = { type: "GeometryCollection", geometries: [ { type: "Point", coordinates: [121.4737, 31.2304] }, { type: "LineString", coordinates: [ [121.4737, 31.2304], [121.4837, 31.2504], ], }, ], };
可選屬性
這些屬性都是 GeoJSON
的擴(kuò)展屬性,不是 GeoJSON
規(guī)范的一部分
id
屬性,用來(lái)描述FeatureCollection
的唯一標(biāo)識(shí)bbox
屬性,用來(lái)描述FeatureCollection
的邊界框- 四至坐標(biāo),一般用來(lái)做數(shù)據(jù)裁剪
- 這是一組左上角和右下角的坐標(biāo),示例:
[minLon, minLat, maxLon, maxLat]
properties
屬性,用來(lái)描述FeatureCollection
的屬性crs
屬性,用來(lái)描述坐標(biāo)參考系
其他
coordinate
coordinate
是一個(gè)數(shù)組,表示一個(gè)點(diǎn)的坐標(biāo),數(shù)組的長(zhǎng)度表示坐標(biāo)的維度,一般是 2
維或 3
維
2
維:[lon, lat]
3
維:[lon, lat, height]
coordinate
的第一個(gè)元素表示經(jīng)度,第二個(gè)元素表示緯度,第三個(gè)元素表示高度
坐標(biāo)順序是 [lon, lat]
,這個(gè)是推薦順序,可由 crs
屬性指定
coordinates
是多維數(shù)組:
- 點(diǎn):
[lon, lat]
- 線:
[[lon, lat], [lon, lat]]
- 面:
[[[lon, lat], [lon, lat]]]
- 多面:
[[[[lon, lat], [lon, lat]]]]
坐標(biāo)參考系
最常使用的坐標(biāo)系是 EPSG:4326
和 EPSG:3857
:
EPSG:4326
是WGS84
(CGCS2000,大地) 坐標(biāo)系,是GeoJSON
規(guī)范的默認(rèn)坐標(biāo)系EPSG:3857
是Web Mercator
(墨卡托) 坐標(biāo)系,是OpenLayers
的默認(rèn)坐標(biāo)系
它們的區(qū)別:
EPSG:4326
是經(jīng)緯度坐標(biāo)系,EPSG:3857
是投影坐標(biāo)系EPSG:4326
的坐標(biāo)范圍是[-180, -90, 180, 90]
,EPSG:3857
的坐標(biāo)范圍是[-20037508.342789244, -20037508.342789244, 20037508.342789244, 20037508.342789244]
EPSG:4326
的坐標(biāo)單位是度,EPSG:3857
的坐標(biāo)單位是米EPSG:4326
的坐標(biāo)原點(diǎn)是[0, 0]
,EPSG:3857
的坐標(biāo)原點(diǎn)是[-20037508.342789244, -20037508.342789244]
EPSG:4326
的坐標(biāo)軸方向是[x, y]
,EPSG:3857
的坐標(biāo)軸方向是[x, -y]
在 ts 中使用
為了在 ts
使用 GeoJSON
能夠有類型約束,我整理整理了一些 GeoJSON
的 ts
類型定義和創(chuàng)建 GeoJSON
的方法:
舉例:
表示一個(gè)點(diǎn)和多個(gè)點(diǎn)的 GeoJSON
集合:
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" }, }, ], };
創(chuàng)建一個(gè)幾何對(duì)象
const pointGeometry = point<{ id: string }>([120.4737, 31.2304], { id: "1", }); const featureGeoJSON = feature<Point>(pointGeometry);
參考
以上就是寫給小白的地理信息的表示法GeoJSON的詳細(xì)內(nèi)容,更多關(guān)于GeoJSON地理信息表示法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
json-lib出現(xiàn)There is a cycle in the hierarchy解決辦法
如果需要解析的數(shù)據(jù)間存在級(jí)聯(lián)關(guān)系,而互相嵌套引用,在hibernate中極容易嵌套而拋出net.sf.json.JSONException: There is a cycle in the hierarchy異常。2010-02-02什么是json和jsonp,jQuery json實(shí)例詳詳細(xì)說(shuō)明
JSON能夠以非常簡(jiǎn)單的方式來(lái)描述數(shù)據(jù)結(jié)構(gòu),XML能做的它都能做,因此在跨平臺(tái)方面兩者完全不分伯仲.其實(shí)網(wǎng)上關(guān)于JSONP的講解有很多,但卻千篇一律,用自己的方式來(lái)闡釋一下這個(gè)問題,看看是否有幫助2012-12-12JQuery用$.ajax或$.getJSON跨域獲取JSON數(shù)據(jù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了JQuery用$.ajax或$.getJSON跨域獲取JSON數(shù)據(jù)的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-09-09用JSON做數(shù)據(jù)傳輸格式中的一些問題總結(jié)
Json 憑借其自身的優(yōu)勢(shì),在Web數(shù)據(jù)處理方面已經(jīng)占據(jù)了一定的位置,這段時(shí)間涉及到用Json做為數(shù)據(jù)傳輸格式的項(xiàng)目有3個(gè),其中有部分頁(yè)面就采用了Json 數(shù)據(jù)傳輸格式, 這里我總結(jié)下這段時(shí)間采用這種方式的一些問題總結(jié)2011-12-12淺談JSON中stringify 函數(shù)、toJosn函數(shù)和parse函數(shù)
這篇文章主要介紹了淺談JSON中stringify 函數(shù)、toJosn函數(shù)和parse函數(shù),需要的朋友可以參考下2015-01-01JSONP跨域GET請(qǐng)求解決Ajax跨域訪問問題
本文主要是介紹了JSONP跨域是如何實(shí)現(xiàn)的,并探討下JSONP跨域的原理。以及采用JSONP跨域GET請(qǐng)求解決Ajax跨域訪問問題,需要的朋友可以參考下2014-12-12