您的位置:首頁技術文章
文章詳情頁

寫給小白學習的地理信息的表示法GeoJSON

瀏覽:169日期:2022-06-09 14:32:35
目錄
  • 簡介
    • 舉例
  • 空間行狀
    • FeatureCollection
    • Feature
  • 幾何對象
    • Point
    • MultiPoint
    • LineString
    • MultiLineString
    • Polygon
    • MultiPolygon
    • GeometryCollection
  • 可選屬性
    • 其他
      • 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.ts
    • geojson.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);

    參考

    • GeoJSON
    • GeoJSON 格式
    • GeoJSON 格式規范
    • EPSG 4326 vs EPSG 3857 (投影,數據集,坐標系)
    • turf.js

    以上就是寫給小白的地理信息的表示法GeoJSON的詳細內容,更多關于GeoJSON地理信息表示法的資料請關注其它相關文章!

    標簽: JavaScript
    国产综合久久一区二区三区