Representation of geographical information for beginners: GeoJSON

Introduction

GeoJSONIt is a JSONformat 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

FeatureCollectionIs Featurea collection of objects, used to represent a group of Featureobjects

It consists of two attributes: typeandfeatures

  • typeThe value of the attribute isFeatureCollection
  • featuresThe value of the property is Featurean array of objects
const  FeatureCollectionJSON = {
   type : "FeatureCollection" ,
   features : [feature],
};

Feature

FeatureObject used to represent attribute information of geometric objects

It consists of three attributes type:geometryproperties

  • typeThe value of the attribute is Feature,
  • geometryThe value of the property is GeometryGeometryObject
  • propertiesThe 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

Pointused to represent a point

It consists of two attributes: typeandcoordinates

  • typeThe value of the attribute isPoint
  • coordinatesThe 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

MultiPointUsed to represent multiple points

It consists of two attributes: typeandcoordinates

  • typeThe value of the attribute isMultiPoint
  • coordinatesThe 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

LineStringused to represent a line

It consists of two attributes: typeandcoordinates

  • typeThe value of the attribute isLineString
  • coordinatesThe 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

MultiLineStringUsed to represent multiple lines

It consists of two attributes: typeandcoordinates

  • typeThe value of the attribute isMultiLineString
  • coordinatesThe 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

Polygonused to represent a face

It consists of two attributes: typeandcoordinates

  • typeThe value of the attribute isPolygon
  • coordinatesThe 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.

polygonThe 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

MultiPolygonUsed to represent multiple faces

It consists of two attributes: typeandcoordinates

  • typeThe value of the attribute isMultiPolygon
  • coordinatesThe 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

GeometryCollectionUsed to represent a collection of geometric objects

It consists of two attributes: typeandgeometries

  • typeThe value of the attribute isGeometryCollection
  • geometriesThe 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 GeoJSONextensions of and are not GeoJSONpart of the specification

  • idAttribute, FeatureCollectiona unique identifier used to describe
  • bboxProperty describing FeatureCollectionthe 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]
  • propertiesAttributes, used to describe FeatureCollectionthe properties of
  • crsAttributes used to describe the coordinate reference system

other

coordinate

coordinateIs an array representing the coordinates of a point. The length of the array represents the dimension of the coordinates, usually 2dimension or 3dimension.

  • 2dimension:[lon, lat]
  • 3dimension:[lon, lat, height]

coordinateThe 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 crsbe specified by the attribute

coordinatesis 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:4326and EPSG:3857:

  • EPSG:4326is WGS84(CGCS2000, geodetic) coordinate system, is GeoJSONthe canonical default coordinate system
  • EPSG:3857is Web Mercatorthe (Mercator) coordinate system, OpenLayersthe default coordinate system for

Their differences:

  • EPSG:4326is the latitude and longitude coordinate system and EPSG:3857is the projected coordinate system
  • EPSG:4326The coordinate range of is [-180, -90, 180, 90]EPSG:3857the coordinate range of is[-20037508.342789244, -20037508.342789244, 20037508.342789244, 20037508.342789244]
  • EPSG:4326The coordinate unit of is degrees, and EPSG:3857the coordinate unit of is meters
  • EPSG:4326The origin of coordinates is [0, 0], and EPSG:3857the origin of coordinates is[-20037508.342789244, -20037508.342789244]
  • EPSG:4326The direction of the coordinate axis is [x, y], and EPSG:3857the direction of the coordinate axis is[x, -y]

Used in ts

In order to have type constraints when tsusing GeoJSON, I have compiled some GeoJSONtype tsdefinitions and creation GeoJSONmethods:

Example:

  1. 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” }, }, ], };
  2. Create a geometric objectUsing geojson.helper.tsconst pointGeometry = point<{ id : string }>([ 120.4737 , 31.2304 ], { id : “1” , }); const featureGeoJSON = feature<Point> ( pointGeometry);

refer to