Contents
Introduction
GeoJSON
It is a JSON
format that uses to encode various geographical data structures. It is a lightweight data exchange format that can be used to represent geometric objects, attribute data, spatial reference systems and other information.
Composed of two types of objects: Geometry
(geometric objects) and Feature
(spatial rows)
- Geometric objects are used to describe geometric shapes such as points, lines, and surfaces in geographical space.
- Spatial rows are used to describe a bounded entity, including geometric objects and other attribute information.
Geometry object types are:
- point:
Point
- More:
MultiPoint
- Wire:
LineString
- Multi-line:
MultiLineString
- noodle:
Polygon
- Multi-faceted:
MultiPolygon
- Geometry collection:
GeometryCollection
The spatial row types are:
- Space row shape:
Feature
- Space shape collection:
FeatureCollection
Example
Geometric objects and spatial rows can be nested within each other
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 }, }, ], };
space row
FeatureCollection
FeatureCollection
Is Feature
a collection of objects, used to represent a group of Feature
objects
It consists of two attributes: type
andfeatures
type
The value of the attribute isFeatureCollection
features
The value of the property isFeature
an array of objects
const FeatureCollectionJSON = { type : "FeatureCollection" , features : [feature], };
Feature
Feature
Object used to represent attribute information of geometric objects
It consists of three attributes type
:geometry
properties
type
The value of the attribute isFeature
,geometry
The value of the property isGeometry
GeometryObjectproperties
The value of the property is a property object (optional)
const FeatureJSON = { type : "Feature" , geometry : { type : "Point" , coordinates : [ 121.4737 , 31.2304 ] }, properties : { id : 1 }, };
Geometric objects
Point
Point
used to represent a point
It consists of two attributes: type
andcoordinates
type
The value of the attribute isPoint
coordinates
The value of the attribute is an array. The first element of the array is the longitude and the second element is the latitude.
const PointJSON = { type : "Point" , coordinates : [ 121.4737 , 31.2304 ], };
MultiPoint
MultiPoint
Used to represent multiple points
It consists of two attributes: type
andcoordinates
type
The value of the attribute isMultiPoint
coordinates
The value of the attribute is an array, and each element of the array is the coordinate of a point.
const MultiPointJSON = { type : "MultiPoint" , coordinates : [ [ 121.4737 , 31.2304 ], [ 121.4837 , 31.2504 ], ], };
LineString
LineString
used to represent a line
It consists of two attributes: type
andcoordinates
type
The value of the attribute isLineString
coordinates
The value of the attribute is an array, and each element of the array is the coordinate of a point.
const LineStringJSON = { type : "LineString" , coordinates : [ [ 121.4737 , 31.2304 ], [ 121.4837 , 31.2504 ], ], };
MultiLineString
MultiLineString
Used to represent multiple lines
It consists of two attributes: type
andcoordinates
type
The value of the attribute isMultiLineString
coordinates
The value of the attribute is an array, and each element of the array is an array of coordinates of a line.
const MultiLineStringJSON = { type : "MultiLineString" , coordinates : [ [ [ 121.4737 , 31.2304 ], [ 121.4837 , 31.2504 ], ], [ [ 121.4727 , 31.2314 ], [ 121.4827 , 31.2514 ], ], ], };
Polygon
Polygon
used to represent a face
It consists of two attributes: type
andcoordinates
type
The value of the attribute isPolygon
coordinates
The value of the attribute is an array. The first element of the array is the coordinate array of the outer ring, and the subsequent elements are the coordinate array of the inner ring.
polygon
The first element and the last element of the coordinate array are the same, indicating closure
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
Used to represent multiple faces
It consists of two attributes: type
andcoordinates
type
The value of the attribute isMultiPolygon
coordinates
The value of the attribute is an array, and each element of the array is an array of coordinates of a surface.
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
Used to represent a collection of geometric objects
It consists of two attributes: type
andgeometries
type
The value of the attribute isGeometryCollection
geometries
The value of the attribute is an array of geometric objects
const GeometryCollectionJSON = { type : "GeometryCollection" , geometries : [ { type : "Point" , coordinates : [ 121.4737 , 31.2304 ] }, { type : "LineString" , coordinates : [ [ 121.4737 , 31.2304 ], [ 121.4837 , 31.2504 ], ], }, ], };
Optional attributes
These properties are GeoJSON
extensions of and are not GeoJSON
part of the specification
id
Attribute,FeatureCollection
a unique identifier used to describebbox
Property describingFeatureCollection
the bounding box of- Four coordinates, generally used for data clipping
- This is a set of coordinates for the upper left and lower right corners, example:
[minLon, minLat, maxLon, maxLat]
properties
Attributes, used to describeFeatureCollection
the properties ofcrs
Attributes used to describe the coordinate reference system
other
coordinate
coordinate
Is an array representing the coordinates of a point. The length of the array represents the dimension of the coordinates, usually 2
dimension or 3
dimension.
2
dimension:[lon, lat]
3
dimension:[lon, lat, height]
coordinate
The first element represents longitude, the second element represents latitude, and the third element represents height
The coordinate sequence is [lon, lat]
, this is the recommended sequence, and can crs
be specified by the attribute
coordinates
is a multidimensional array:
- point:
[lon, lat]
- Wire:
[[lon, lat], [lon, lat]]
- noodle:
[[[lon, lat], [lon, lat]]]
- Multi-faceted:
[[[[lon, lat], [lon, lat]]]]
coordinate reference system
The most commonly used coordinate systems are EPSG:4326
and EPSG:3857
:
EPSG:4326
isWGS84
(CGCS2000, geodetic) coordinate system, isGeoJSON
the canonical default coordinate systemEPSG:3857
isWeb Mercator
the (Mercator) coordinate system,OpenLayers
the default coordinate system for
Their differences:
EPSG:4326
is the latitude and longitude coordinate system andEPSG:3857
is the projected coordinate systemEPSG:4326
The coordinate range of is[-180, -90, 180, 90]
,EPSG:3857
the coordinate range of is[-20037508.342789244, -20037508.342789244, 20037508.342789244, 20037508.342789244]
EPSG:4326
The coordinate unit of is degrees, andEPSG:3857
the coordinate unit of is metersEPSG:4326
The origin of coordinates is[0, 0]
, andEPSG:3857
the origin of coordinates is[-20037508.342789244, -20037508.342789244]
EPSG:4326
The direction of the coordinate axis is[x, y]
, andEPSG:3857
the direction of the coordinate axis is[x, -y]
Used in ts
In order to have type constraints when ts
using GeoJSON
, I have compiled some GeoJSON
type ts
definitions and creation GeoJSON
methods:
Example:
- Represents a point and a set of points
GeoJSON
:Using geojson.d.tstype 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” }, }, ], }; - Create a geometric objectUsing geojson.helper.tsconst pointGeometry = point<{ id : string }>([ 120.4737 , 31.2304 ], { id : “1” , }); const featureGeoJSON = feature<Point> ( pointGeometry);