这篇文章主要为大家介绍了写给小白学习的地理信息的表示法GeoJSON的详细实例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
简介
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地理信息表示法的资料请关注编程学习网其它相关文章!
本文标题为:写给小白学习的地理信息的表示法GeoJSON
 
				
         
 
            
        - 关于 html:如何从 css 表中删除边距和填充 2022-09-21
- javascript 判断当前浏览器版本并判断ie版本 2023-08-08
- 深入浅析AjaxFileUpload实现单个文件的 Ajax 文件上传库 2022-12-15
- 1 Vue - 简介 2023-10-08
- 基于CORS实现WebApi Ajax 跨域请求解决方法 2023-02-14
- layui数据表格以及传数据方式 2022-12-13
- JS实现左侧菜单工具栏 2022-08-31
- jsPlumb+vue创建字段映射关系 2023-10-08
- ajax实现输入提示效果 2023-02-14
- vue keep-alive 2023-10-08
 
						 
						 
						 
						 
						 
				 
				 
				 
				