WebGIS 标准数据格式 GeoJSON 格式介绍及数据处理、可视化工具推荐

1,009 阅读4分钟

什么是Geojson

GeoJson 是一种基于 JSON 的地理空间数据交换格式。 它定义了几种类型的 JSON 对象,以及将它们组合起来表示有关地理特征、属性和空间范围的数据的方式。 GeoJson 使用了经纬度参考系统、 WGS84 坐标系统和十进制单位。

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [125.6, 10.1]
  },
  "properties": {
    "name": "Dinagat Islands"
  }
}

GeoJSON 数据类型

地理数据常见类型就是点、线、面三种,实际应用中又扩展了多点、多线、多面几种数据格式。

简单几何体

TypeExamples
Point{ "type": "Point", "coordinates": [30, 10] }
LineString{ "type": "LineString", "coordinates": [ [30, 10], [10, 30], [40, 40] ] }
Polygon{ "type": "Polygon", "coordinates": [ [[30, 10], [40, 40], [20, 40], [10, 20], [30, 10]] ] }
{ "type": "Polygon", "coordinates": [ [[35, 10], [45, 45], [15, 40], [10, 20], [35, 10]], [[20, 30], [35, 35], [30, 20], [20, 30]] ] }

poygon 可以表达带洞的多边形, Polygon Coordinates 结果中第一个点序列表示外环,第二个及其他表示内环。 带洞的多边形使用场景很多,比如水系相关表达河流三角洲等等。

image.png

Polygon 坐标约束

  • 对于类型“Polygon” ,“coordinates”成员必须是一个”线性环坐标数组“组成的数组,既第一个点和最后一个点是一样的。因此 表达一个4边形需要5个顶点。
  • 对于多边形有一个以上的环,第一个必须是外环,其他的必须是内环。 外环与表面形成边界,内环(如果存在)与表面形成边界孔。

多几何体

TypeExamples
MultiPoint{ "type": "MultiPoint", "coordinates": [ [10, 40], [40, 30], [20, 20], [30, 10] ] }
MultiLineString{ "type": "MultiLineString", "coordinates": [ [[10, 10], [20, 20], [10, 40]], [[40, 40], [30, 30], [40, 20], [30, 10]] ] }
MultiPolygon{ "type": "MultiPolygon", "coordinates": [ [ [[30, 20], [45, 40], [10, 40], [30, 20]] ], [ [[15, 5], [40, 10], [10, 20], [5, 10], [15, 5]] ] ] }
{ "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]] ] ] }

多几何体和单几何体的数据格式最大差别在于,coordinates 数据维度比单几何体多一维。单点是一维数组,多点是二维。 多面的使用场景比较多,在行政区区划中很多城市不是连续的多边形。

image.png

属性定义

GeoJSON 除了定义空间数据的格式还约定了如何表达属性, 属性通过properties 字段定义。

{
      "type": "Feature",
      "properties": {
        name:"122"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          225,
          -43.06888777416961
        ]
      }
    }

数据集合

FeatureCollection 对象有一个名为“ features”的属性。 “features”的值是一个 JSON 数组。 数组的每个元素都是上面定义的特征对象。 这个数组可能为空,一般数据中features 会存储同一类型的数据,比如点数据(点或多点)一组,一般 不会混合 定义,分开定义在数据可视化的时候方便表达。

点集合
{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [
          225,
          -43.06888777416961
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [
          -91.40625,
          24.5271348225978
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [
          36.5625,
          67.60922060496382
        ]
      }
    }
  ]
}
线集合
{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            37.880859375,
            24.216909537721747
          ],
          [
            38.25439453125,
            25.403584973186703
          ],
          [
            38.8037109375,
            26.046912801683984
          ],
          [
            40.1220703125,
            26.63763888664592
          ],
          [
            40.23193359375,
            26.70635985763354
          ],
          [
            41.90185546875,
            26.716173757934094
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            39.90234375,
            24.946219074360084
          ],
          [
            41.1328125,
            25.48295117535531
          ],
          [
            41.39648437499999,
            25.522614647623293
          ],
          [
            41.451416015625,
            25.55235365216549
          ],
          [
            41.868896484375,
            24.946219074360084
          ]
        ]
      }
    }
  ]
}

GeoJSON 数据编辑

geojson.io Geojso 数据在线查看,编辑,可视化工具。 该网站包含一个交互式地图和一个绘图控件,你可以使用它来绘制新的矢量图层和编辑绘制的矢量图层。 绘制内容的 GeoJSON 字符串显示在地图旁边,并在你编辑时自动与当前绘制的形状同步。 image.png

geojson.io.

GeoJSON 数据简化编辑

mapshaper 是目前几何简化的最佳工具之一。 它是一款免费的开源软件,具有多种几何编辑功能,尽管它以快速轻松地简化矢量图层而闻名。 重要的是,mapshaper 执行拓扑感知多边形简化。 这意味着相邻多边形之间的共享边界始终保持完整,没有间隙或重叠,即使在高度简化时也是如此。

www.mapshaper.org/

GeoJSON 数据可视化

L7 是 蚂蚁集团 AntV 数据可视化团队推出的基于 WebGL 的开源大规模地理空间数据可视分析引擎。L7 支持 JSON、CSV、GeoJSON 等 数据格式。其中 GeoJSON 需要 FeatureCollection 一些列的地理数据,不支持单个geometry 传入,必须包装成featureCollection.

demo 地址:l7.antv.vision/zh/examples… GitHub:github.com/antvis/L7 image.png

import { Scene, PolygonLayer, LineLayer, Popup } from '@antv/l7';
import { Mapbox } from '@antv/l7-maps';

const scene = new Scene({
  id: 'map',
  map: new Mapbox({
    pitch: 0,
    style: 'light',
    center: [ -96, 37.8 ],
    zoom: 3
  })
});
scene.on('loaded', () => {
  fetch(
    'https://gw.alipayobjects.com/os/basement_prod/d36ad90e-3902-4742-b8a2-d93f7e5dafa2.json'
  )
    .then(res => res.json())
    .then(data => {
      const color = [ 'rgb(255,255,217)', 'rgb(237,248,177)', 'rgb(199,233,180)', 'rgb(127,205,187)', 'rgb(65,182,196)', 'rgb(29,145,192)', 'rgb(34,94,168)', 'rgb(12,44,132)' ];
      const layer = new PolygonLayer({})
        .source(data)
        .scale('density', {
          type: 'quantile'
        })
        .color(
          'density', color
        )
        .shape('fill')
        .active(true)
        .style({
          opacity: 1.0
        });

      scene.addLayer(layer);
    });
});

其他

GeoJSON 标准草案: www.rfc-editor.org/rfc/rfc7946