GeoJSON

作者

杰夫麦克纳

联系

jmckenna在gatewaygeomatics.com

最后更新

2021-05-10

GeoJSON是一种基于文本的开放式标准格式,可以表示矢量点、直线、多边形和多部分集合。按照官方说法,它是一种基于JavaScript Object Notation(JSON)的地理空间数据交换格式。GeoJSON是互联网工程任务组(IETF)的一部分。

更多信息

有关GeoJSON的更多信息,请访问:

文件列表

GeoJSON文件通常是带有JSON文件扩展名的单个文本文件,例如:

countries.json

您可以在文本编辑器中打开该文件,它将如下所示:

{
    "type": "FeatureCollection",
    "name": "countries",
    "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
    "features": [
    { "type": "Feature", "properties": { "name_en": "Fiji" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 180.0, -16.067132663642447 ], [clipped for display here...], [ -179.793320109048636, -16.020882256741224 ] ] ] ] } },
    { "type": "Feature", "properties": { "name_en": "Tanzania" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 33.903711197104528, -0.95 ], [ 34.07262, -1.05982 ], [clipped for display here...], [ 33.903711197104528, -0.95 ] ] ] ] } },
    ...
    ]
}

小技巧

你也可以把你的GeoJSON文件拖放到一个现代的网络浏览器中,比如Firefox,它会很好地显示你的文件。

备注

想要跟随的Windows用户可以转换 MS4W 包括在内 demo.db SpatiaLite数据库转换为GeoJSON文件(适用于 国家 表)和命令(在内部执行 /ms4w/apps/local-demo/data/ )::

ogr2ogr -f GeoJSON countries.json demo.db countries

数据访问/连接方法

  • 可通过OGR在地图服务器中访问GeoJSON GeoJSON driver

  • 必须使用connectiontype ogr参数。

  • 需要GeoJSON文件的路径,包括文件扩展名。

CONNECTIONTYPE OGR
CONNECTION "name.json"
DATA "layername"

步骤1:使用ogrinfo检查

对GeoJSON文件使用ogrinfo:

> ogrinfo countries.json

INFO: Open of `countries.json'
      using driver `GeoJSON' successful.
1: countries (Multi Polygon)

使用ogrinfo检查 国家 层:

> ogrinfo countries.json countries -summary

INFO: Open of `countries.json'
      using driver `GeoJSON' successful.

Layer name: countries
Geometry: Multi Polygon
Feature Count: 177
Extent: (-180.000000, -90.000000) - (180.000000, 83.645130)
Layer SRS WKT:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.0174532925199433,
        AUTHORITY["EPSG","9122"]],
    AUTHORITY["EPSG","4326"]]
featurecla: String (0.0)
scalerank: Integer (0.0)
labelrank: Integer (0.0)
sovereignt: String (0.0)
sov_a3: String (0.0)
adm0_dif: Integer (0.0)
level: Integer (0.0)
type: String (0.0)
admin: String (0.0)
adm0_a3: String (0.0)
...

步骤2:在 Mapfile 中添加图层

对于OGR连接,始终建议设置CONNECTIONTYPE、CONNECTIONTYPE和DATA,如下所示:

/* Countries */
LAYER
  NAME "countries"
  TYPE POLYGON
  STATUS ON
  CONNECTIONTYPE OGR
  CONNECTION "countries.json"
  DATA "countries"              # the OGR layername, found through ogrinfo
  CLASS
    NAME "World Countries"
    STYLE
      COLOR 200 200 200
      OUTLINECOLOR 0 0 0
      WIDTH 0.1
    END #style
  END #class
END #layer

步骤3:使用shp2img测试您的映射文件

使用MapServer命令行实用程序 Sp2IMG 要验证 Mapfile 是否创建了有效的地图图像,并显示绘制时间,例如:

shp2img -m geojson.map -o ttt.png -map_debug 3

msDrawMap(): rendering using outputformat named png (AGG/PNG).
msDrawMap(): WMS/WFS set-up and query, 0.000s
msDrawMap(): Layer 0 (countries), 0.091s
msDrawMap(): Drawing Label Cache, 0.000s
msDrawMap() total time: 0.091s
msSaveImage(ttt.png) total time: 0.007s
../../_images/countries-shp2img.png

小技巧

MS4W用户会注意到,演示 Mapfile 是每个安装的一部分(位于 /ms4w/apps/local-demo/local.map )配置为输出GeoJSON。