Map projection 4490, 4326, 3857

1. Geodesy

Geodesy is the discipline of measuring and describing the Earth’s surface, including determining the Earth’s gravity field and ocean floor topography.

1.1 Geoid (geoid)

The geoid is the shape of the ocean surface after excluding other influences such as wind and tide, and only considering the influence of gravity and rotation. This shape extends across the land, forming a closed curved surface. Although we usually say that the earth is a sphere or ellipsoid, due to the uneven distribution of the earth’s gravity (due to different densities and other reasons), the geoid is an irregular smooth surface. Although it is irregular, it can be approximately represented as an ellipsoid, which is called a reference ellipsoid (Reference ellipsoid). The height of the geoid relative to the reference ellipsoid is called theUndulation of the geoid. This fluctuation is not very large. The highest is 85m in Iceland and the lowest is ?106m in southern India. The total is less than 200m. The figure below is from Wikipedia and represents the Undulation of different regions under the EGM96 geoid.

img

1.2 Reference ellipsoid

A reference ellipsoid is a mathematically defined surface of the Earth that approximates the geoid. Because it is a geometric model, it can be determined by the semi-major axis, semi-minor axis and oblateness. What we usually call longitude, latitude and altitude are based on this.

On the one hand, our measurements of the shape of the Earth have become increasingly accurate over time. On the other hand, because the geoid is irregular, different regions on the Earth often need to use different reference ellipsoids. Fit the local geoid as much as possible. There have been many different reference ellipsoids throughout history, and many are still in use. Two coordinate systems, “Beijing 54” and “Xi’an 90”, have been used in China in the past. Beijing 54 uses the reference ellipsoid of Krasovsky 1940, and Xi’an 80 uses the 1975 International Geodetic Survey and Earth. Reference ellipsoid recommended by the 16th Congress of the Union of Physics. Currently, the reference ellipsoid defined by WGS is more commonly used around the world.

2. Coordinate system

With a geometric model such as a reference ellipsoid, you can define a coordinate system to describe positions, measure distances, etc. Using the same coordinate system can ensure that the positions under the same coordinates are the same, and the results obtained by the same measurement are also identical. There are usually two types of coordinate systems: geographic coordinate systems and projected coordinate systems.

2.1 Geographic coordinate system

A geographical coordinate system generally refers to a coordinate system consisting of longitude, latitude and altitude, which can mark any location on the earth. As mentioned earlier, different reference ellipsoids may be used in different regions. Even if the same ellipsoid is used, the orientation and even size of the ellipsoid may be adjusted in order to make the ellipsoid better fit the local geoid. This requires the use of different geodetic systems (Geodetic datum) for identification. Therefore, for a certain location on the earth, the coordinates obtained using different measurement systems are different. When we process geographic data, we must first confirm the measurement system used in the data. In fact, as we become more and more precise in measuring the shape of the earth, the NAD83 datum used in North America and the ETRS89 datum used in Europe are basically consistent with the WGS 84 datum. Even the difference between my country’s CGCS2000 and WGS84 is very small. of. But the difference is very small, which does not mean it is completely consistent. Take NAD83 as an example. Because it wants to ensure stability in North America, the difference between it and WGS84 is constantly changing. For most parts of the United States, there is 1-2cm every year. difference.

2.2 Projected coordinate systems

The geographical coordinate system is three-dimensional. If we want to display it on a map or screen, it needs to be converted into two dimensions. This is called projection (Map projection). It is obvious that the transformation from three-dimensional to two-dimensional will inevitably lead to deformation and distortion. Distortion is inevitable, but there will be different distortions under different projections, which allows us to have choices. Commonly used projections include equirectangular projection (Platte Carre) and Mercator projection (Mercator). The following figure is from Mercator vs. well…not Mercator (Platte Carre), which vividly illustrates the distortion under these two projections:

img

The left picture shows circles of the same size on the Earth’s sphere, and the upper right is the Mercator projection. After projection, it is still a circle, but the object is severely magnified at high latitudes. The lower right is an isometric projection. The size change of the object is not so obvious, but the image is elongated. The Platte Carre projection is not suitable for activities such as navigation because of the distortion in the projection. However, because the correspondence between coordinates and pixels is very simple, it is very suitable for the display of raster maps. The Platte Carre projection is the default projection of many GIS software. .

It should be noted that for the Mercator projection, the size distortion becomes more serious as you go to higher latitudes, and it will be placed at infinity at the poles. Therefore, the Mercator projection cannot display the polar regions. The picture below is from Wikipedia. You can see the difference between the size of each country under the Mercator projection and its actual size. But the two characteristics of conformity and straight rhumb lines make it very suitable for navigation.

The size and actual size of each country under the Mercator projection

3. Implications for Web Map developers

For Web Map developers, the most familiar ones should be EPSG:4326 (WGS84) and EPSG:3857 (Pseudo-Mercator). What are these?

3.1 EPSG:4326 (WGS84)

As mentioned earlier, WGS84 is currently the most popular geographic coordinate system. Internationally, each coordinate system is assigned an EPSG code, and EPSG:4326 is the code for WGS84. GPS is based on WGS84, so usually the coordinate data we get is WGS84. Generally, when we store data, we still store it according to WGS84.

3.2 EPSG:3857 (Pseudo-Mercator)

Pseudo-Mercator projection, also known as Sphere Mercator, Web Mercator. It is based on the Mercator projection, which projects the WGS84 coordinate system into a square. We already know that WGS84 is based on an ellipsoid, but the pseudo-Mercator projection projects coordinates onto a sphere, which results in larger distortion at the poles, but is easier to calculate. This may be why it is called “pseudo” Mercator. In addition, the pseudo-Mercator projection also cuts off areas above 85.051129° latitude in the north and south to ensure that the entire projection is square. Because of the conformal characteristics such as Mercator projection, the shape of objects remains unchanged on different levels of layers, and a square can be continuously divided into more and smaller squares to show clearer details. Obviously, the pseudo-Mercator coordinate system is very suitable for displaying data, but it is not suitable for storing data. Usually we use WGS84 to store data and use pseudo-Mercator to display data.

Web Mercator was first proposed by Google and has now become the de facto standard for Web Map. But perhaps due to the “pseudo” reasons above, Web Mercator was initially refused to assign EPSG codes. So everyone generally uses the unofficial code of EPSG:900913 (Google’s digital variant) to represent it. It was not until 2008 that it was assigned the code EPSG:3785, but not long after that same year, it was abandoned and the official code of EPSG:3857 was reassigned, which is still in use today.

Combined with the above pictures, we summarize the biggest differences:

EPSG:3857 (projection): The readability of the data is poor and the storage of large values takes up memory.
EPSG:4326 (Geography): Using this coordinate system will cause page distortion.

So to summarize: In actual development, because most of the map source data is in the data source format of EPSG:4326, maps using the EPSG:4326 coordinate system will feel compressed. Therefore, we all use the coordinate system type of EPSG:3857 and convert the data source to the data source of EPSG:3857. However, this EPSG:3857 data source is difficult to read and the value occupies memory. To combine the shortcomings of both, we use coordinate conversion, that is, EPSG:4326 to EPSG:3857. So please understand this sentence: Normally: data is stored in EPSG:4326 and displayed in EPSG:3857

4.Zhejiang Tianmap

Actually it is 4490, temporarily use 4326 coordinates

const CustomEPSG4326 = L.extend({}, L.CRS.Earth, {

? code: "EPSG:4326", // The standard code name of the CRS passed to the WMS service (such as "EPSG:3857")

? projection: L.Projection.LonLat,

? /*

? L.Projection.LonLat: Equirectangular projection, or Flat Cali projection - the simplest projection, mainly used by GIS enthusiasts. Directly map x to longitude and y to latitude. Also suitable for flat worlds, such as game maps. Used by EPSG:4326 and Simple CRS.

? L.Projection.Mercator: Elliptical Mercator projection - more complex than Spherical Mercator. Assume that the earth is an ellipsoid. EPSG:3395 CRS used.

? L.Projection.SphericalMercator: Spherical Mercator projection - the most common projection for online maps, used by almost all free and commercial tile providers. Assume that the earth is a sphere. EPSG uses: 3857 CRS.

? */

? transformation: new L.Transformation(1 / 180, 1, -1 / 180, 0.5), // Affine transformation: (a*x + b, c*y + d) Replace lat and lon with the required 0~ 1.

? scale: function (t) {

? return 256 * Math.pow(2, t - 1); // For Mercator-based CRS, it returns 256*2^ scaling. Zhejiang Tianmap starts slicing from 1, generally starts from 0 (main difference)

? },

   });

Custom tileLayer.wmts

L.TileLayer.WMTS = L.TileLayer.extend({

 defaultWmtsParams: {

  service: 'WMTS',

  request: 'GetTile',

  version: '1.0.0',

  layer: '',

  style: '',

  tilematrixset: '',

  format: 'image/jpeg'

 },

 initialize: function (url, options) { // (String, Object)

  this._url = url;

  var lOptions= {};

  var cOptions = Object.keys(options);

  cOptions.forEach(element=>{

   lOptions[element.toLowerCase()]=options[element];

  });

  var wmtsParams = L.extend({}, this.defaultWmtsParams);

  var tileSize = lOptions.tileSize || this.options.tileSize;

  if (lOptions.detectRetina & amp; & amp; L.Browser.retina) {

   wmtsParams.width = wmtsParams.height = tileSize * 2;

  } else {

   wmtsParams.width = wmtsParams.height = tileSize;

  }

  for (var i in lOptions) {

? // all keys that are in defaultWmtsParams options go to WMTS params

   if (wmtsParams.hasOwnProperty(i) & amp; & amp; i!="matrixIds") {

? wmtsParams[i] = lOptions[i];

   }

  }

  this.wmtsParams = wmtsParams;

  this.matrixIds = options.matrixIds||this.getDefaultMatrix();

  L.setOptions(this, options);

 },
 onAdd: function (map) {

  this._crs = this.options.crs || map.options.crs;

  L.TileLayer.prototype.onAdd.call(this, map);

 },
 getTileUrl: function (coords) { // (Point, Number) -> String

  var tileSize = this.options.tileSize;

  var nwPoint = coords.multiplyBy(tileSize);

  nwPoint.x + =1;

  nwPoint.y-=1;

  var sePoint = nwPoint.add(new L.Point(tileSize, tileSize));

  var zoom = this._tileZoom;

  var nw = this._crs.project(this._map.unproject(nwPoint, zoom));

  var se = this._crs.project(this._map.unproject(sePoint, zoom));

  tilewidth = se.x-nw.x;

  var ident = this.matrixIds[zoom].identifier;

  var tilematrix = this.wmtsParams.tilematrixset + ":" + ident;

  var X0 = this.matrixIds[zoom].topLeftCorner.lng;

  var Y0 = this.matrixIds[zoom].topLeftCorner.lat;

  var tilecol=Math.floor((nw.x-X0)/tilewidth);

  var tilerow=-Math.floor((nw.y-Y0)/tilewidth);

  var url = L.Util.template(this._url, {s: this._getSubdomain(coords)});

  return url + L.Util.getParamString(this.wmtsParams, url) + " & tilematrix=" + ident + " & tilerow=" + tilerow + " & tilecol=" + tilecol + " & amp; token=" + this.options.token;

 },
 setParams: function (params, noRedraw) {

  L.extend(this.wmtsParams, params);

  if (!noRedraw) {

   this.redraw();

  }

  return this;

 },
 getDefaultMatrix : function () {

  /**

   \* the matrix3857 represents the projection

   \* for in the IGN WMTS for the google coordinates.

   */

  var matrixIds3857 = new Array(22);

  for (var i= 0; i<22; i + + ) {

   matrixIds3857[i]= {

? identifier : "" + i,

? topLeftCorner: new L.LatLng(90,-180)

   };

  }

  return matrixIds3857;

 }

});

L.tileLayer.wmts = function (url, options) {

 return new L.TileLayer.WMTS(url, options);

};