经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » JSON » 查看文章
写给小白学习的地理信息的表示法GeoJSON
来源:jb51  时间:2023/5/17 8:51:38  对本文有异议

简介

GeoJSON 是一种使用 JSON 来编码各种地理数据结构的格式,是一种轻量级的数据交换格式,可以用来表示几何对象、属性数据、空间参考系统等信息

由两种对象组成:Geometry(几何对象)和 Feature(空间行状)

  • 几何对象用来描述地理空间中的点、线、面等几何形状
  • 空间行状用来描述一个有界的实体,包括几何对象和其他属性信息

几何对象类型有:

  • 点:Point
  • 多点:MultiPoint
  • 线:LineString
  • 多线:MultiLineString
  • 面:Polygon
  • 多面:MultiPolygon
  • 几何集合:GeometryCollection

空间行状类型有:

  • 空间行状:Feature
  • 空间形状集合:FeatureCollection

举例

几何对象和空间行状可以相互嵌套

  1. const GeoJSON = {
  2. type: "FeatureCollection",
  3. features: [
  4. {
  5. type: "Feature",
  6. geometry: { type: "Point", coordinates: [121.4737, 31.2304] },
  7. properties: { id: 1 },
  8. },
  9. {
  10. type: "Feature",
  11. geometry: { type: "Point", coordinates: [121.4837, 31.2504] },
  12. properties: { id: 2 },
  13. },
  14. ],
  15. };

空间行状

FeatureCollection

FeatureCollection 是 Feature 对象的集合,用来表示一组 Feature 对象

由 type 和 features 两个属性组成:

  • type 属性的值为 FeatureCollection
  • features 属性的值为 Feature 对象的数组
  1. const FeatureCollectionJSON = {
  2. type: "FeatureCollection",
  3. features: [feature],
  4. };

Feature

Feature 对象用来表示几何对象的属性信息

由 typegeometry 和 properties 三个属性组成:

  • type 属性的值为 Feature
  • geometry 属性的值为 Geometry 几何对象
  • properties 属性的值为属性对象(可选)
  1. const FeatureJSON = {
  2. type: "Feature",
  3. geometry: { type: "Point", coordinates: [121.4737, 31.2304] },
  4. properties: { id: 1 },
  5. };

几何对象

Point

Point 用来表示一个点

由 type 和 coordinates 两个属性组成:

  • type 属性的值为 Point
  • coordinates 属性的值为一个数组,数组的第一个元素为经度,第二个元素为纬度
  1. const PointJSON = {
  2. type: "Point",
  3. coordinates: [121.4737, 31.2304],
  4. };

MultiPoint

MultiPoint 用来表示多个点

由 type 和 coordinates 两个属性组成:

  • type 属性的值为 MultiPoint
  • coordinates 属性的值为一个数组,数组的每个元素都是一个点的坐标
  1. const MultiPointJSON = {
  2. type: "MultiPoint",
  3. coordinates: [
  4. [121.4737, 31.2304],
  5. [121.4837, 31.2504],
  6. ],
  7. };

LineString

LineString 用来表示一条线

由 type 和 coordinates 两个属性组成:

  • type 属性的值为 LineString
  • coordinates 属性的值为一个数组,数组的每个元素都是一个点的坐标
  1. const LineStringJSON = {
  2. type: "LineString",
  3. coordinates: [
  4. [121.4737, 31.2304],
  5. [121.4837, 31.2504],
  6. ],
  7. };

MultiLineString

MultiLineString 用来表示多条线

由 type 和 coordinates 两个属性组成:

  • type 属性的值为 MultiLineString
  • coordinates 属性的值为一个数组,数组的每个元素都是一个线的坐标数组
  1. const MultiLineStringJSON = {
  2. type: "MultiLineString",
  3. coordinates: [
  4. [
  5. [121.4737, 31.2304],
  6. [121.4837, 31.2504],
  7. ],
  8. [
  9. [121.4727, 31.2314],
  10. [121.4827, 31.2514],
  11. ],
  12. ],
  13. };

Polygon

Polygon 用来表示一个面

由 type 和 coordinates 两个属性组成:

  • type 属性的值为 Polygon
  • coordinates 属性的值为一个数组,数组的第一个元素为外环的坐标数组,后面的元素为内环的坐标数组

polygon 的坐标数组的第一个元素和最后一个元素是相同的,表示闭合

  1. const PolygonJSON = {
  2. type: "Polygon",
  3. coordinates: [
  4. [
  5. [121.4737, 31.2304],
  6. [121.4837, 31.2504],
  7. [121.4937, 31.2304],
  8. [121.4737, 31.2304],
  9. ],
  10. [
  11. [121.4717, 31.2314],
  12. [121.4827, 31.2524],
  13. [121.4937, 31.2334],
  14. [121.4757, 31.2344],
  15. ],
  16. ],
  17. };

MultiPolygon

MultiPolygon 用来表示多个面

由 type 和 coordinates 两个属性组成:

  • type 属性的值为 MultiPolygon
  • coordinates 属性的值为一个数组,数组的每个元素都是一个面的坐标数组
  1. const MultiPolygonJSON = {
  2. type: "MultiPolygon",
  3. coordinates: [
  4. [
  5. [
  6. [121.4737, 31.2304],
  7. [121.4837, 31.2504],
  8. [121.4937, 31.2304],
  9. [121.4737, 31.2304],
  10. ],
  11. [
  12. [121.4737, 31.2304],
  13. [121.4837, 31.2504],
  14. [121.4937, 31.2304],
  15. [121.4737, 31.2304],
  16. ],
  17. ],
  18. [
  19. [
  20. [121.4737, 31.2304],
  21. [121.4837, 31.2504],
  22. [121.4937, 31.2304],
  23. [121.4737, 31.2304],
  24. ],
  25. [
  26. [121.4737, 31.2304],
  27. [121.4837, 31.2504],
  28. [121.4937, 31.2304],
  29. [121.4737, 31.2304],
  30. ],
  31. ],
  32. ],
  33. };

GeometryCollection

GeometryCollection 用来表示几何对象的集合

由 type 和 geometries 两个属性组成:

  • type 属性的值为 GeometryCollection
  • geometries 属性的值为几何对象的数组
  1. const GeometryCollectionJSON = {
  2. type: "GeometryCollection",
  3. geometries: [
  4. { type: "Point", coordinates: [121.4737, 31.2304] },
  5. {
  6. type: "LineString",
  7. coordinates: [
  8. [121.4737, 31.2304],
  9. [121.4837, 31.2504],
  10. ],
  11. },
  12. ],
  13. };

可选属性

这些属性都是 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 集合:

使用geojson.d.ts

  1. type PointType = FeatureCollection<Point | MultiPoint, GeoJsonProperties<T>>;
  2. const point2Geojson: PointType<{ id: string; name?: string }> = {
  3. type: "FeatureCollection",
  4. features: [
  5. {
  6. type: "Feature",
  7. geometry: {
  8. type: "Point",
  9. coordinates: [120.4737, 31.2304],
  10. },
  11. properties: { id: "12", name: "uccs" },
  12. },
  13. {
  14. type: "Feature",
  15. geometry: {
  16. type: "MultiPoint",
  17. coordinates: [
  18. [121.4737, 31.2304],
  19. [111.4737, 31.2204],
  20. ],
  21. },
  22. properties: { id: "1" },
  23. },
  24. ],
  25. };

创建一个几何对象

使用geojson.helper.ts

  1. const pointGeometry = point<{ id: string }>([120.4737, 31.2304], {
  2. id: "1",
  3. });
  4. const featureGeoJSON = feature<Point>(pointGeometry);

参考

以上就是写给小白的地理信息的表示法GeoJSON的详细内容,更多关于GeoJSON地理信息表示法的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号