GeoJSON 是一种使用 JSON 来编码各种地理数据结构的格式,是一种轻量级的数据交换格式,可以用来表示几何对象、属性数据、空间参考系统等信息
由两种对象组成:Geometry(几何对象)和 Feature(空间行状)
几何对象类型有:
PointMultiPointLineStringMultiLineStringPolygonMultiPolygonGeometryCollection空间行状类型有:
FeatureFeatureCollection几何对象和空间行状可以相互嵌套
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 是 Feature 对象的集合,用来表示一组 Feature 对象
由 type 和 features 两个属性组成:
type 属性的值为 FeatureCollectionfeatures 属性的值为 Feature 对象的数组const FeatureCollectionJSON = {
type: "FeatureCollection",
features: [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 用来表示一个点
由 type 和 coordinates 两个属性组成:
type 属性的值为 Pointcoordinates 属性的值为一个数组,数组的第一个元素为经度,第二个元素为纬度const PointJSON = {
type: "Point",
coordinates: [121.4737, 31.2304],
};
MultiPoint 用来表示多个点
由 type 和 coordinates 两个属性组成:
type 属性的值为 MultiPointcoordinates 属性的值为一个数组,数组的每个元素都是一个点的坐标const MultiPointJSON = {
type: "MultiPoint",
coordinates: [
[121.4737, 31.2304],
[121.4837, 31.2504],
],
};
LineString 用来表示一条线
由 type 和 coordinates 两个属性组成:
type 属性的值为 LineStringcoordinates 属性的值为一个数组,数组的每个元素都是一个点的坐标const LineStringJSON = {
type: "LineString",
coordinates: [
[121.4737, 31.2304],
[121.4837, 31.2504],
],
};
MultiLineString 用来表示多条线
由 type 和 coordinates 两个属性组成:
type 属性的值为 MultiLineStringcoordinates 属性的值为一个数组,数组的每个元素都是一个线的坐标数组const MultiLineStringJSON = {
type: "MultiLineString",
coordinates: [
[
[121.4737, 31.2304],
[121.4837, 31.2504],
],
[
[121.4727, 31.2314],
[121.4827, 31.2514],
],
],
};
Polygon 用来表示一个面
由 type 和 coordinates 两个属性组成:
type 属性的值为 Polygoncoordinates 属性的值为一个数组,数组的第一个元素为外环的坐标数组,后面的元素为内环的坐标数组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 用来表示多个面
由 type 和 coordinates 两个属性组成:
type 属性的值为 MultiPolygoncoordinates 属性的值为一个数组,数组的每个元素都是一个面的坐标数组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 用来表示几何对象的集合
由 type 和 geometries 两个属性组成:
type 属性的值为 GeometryCollectiongeometries 属性的值为几何对象的数组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 是一个数组,表示一个点的坐标,数组的长度表示坐标的维度,一般是 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 使用 GeoJSON 能够有类型约束,我整理整理了一些 GeoJSON 的 ts 类型定义和创建 GeoJSON 的方法:
举例:
表示一个点和多个点的 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地理信息表示法的资料请关注其它相关文章!
