demand

The project uses vector data as geodey 2000 coordinate system (ESPG:4490), while remote sensing image data adopts multiple coordinate systems. According to requirements, the base map of different coordinate systems should be displayed on the same map control. After switching the base map each time, it should be automatically and correctly scaled to the display range of the map before switching.

Coordinate system wkid describe
China Geodetic Coordinate System 2000 4490 2000 National geodetic coordinate system
CGCS2000 / 3-degree Gauss-Kruger zone 37 4525 2000 National geodetic coordinate system gauss-Kluge projection 3 degree zone zoning No. 37
CGCS2000 / 3-degree Gauss-Kruger zone 38 4526 2000 Gauss-Kluge projection 3rd degree zoning in the National Geodetic coordinate system no. 38 zoning
CGCS2000 / 3-degree Gauss-Kruger zone 39 4527 2000 Gauss-Kluge projection 3rd degree zoning in the National Geodetic coordinate system no. 39 zoning
WGS 84 — WGS84 – World Geodetic System 1984 4326 World geodetic coordinate system
WGS 84 / Pseudo-Mercator — Spherical Mercator 3857 World geodetic coordinates Mercator projection

The resources

1. Change the CRS property of map dynamically

The problem

The Leaflet itself does not provide the setting of CRS for different base maps. Therefore, after the base map is switched, the base maps of different coordinate systems are not displayed at correct positions because the calculation parameters of slice range are not changed.

solution

  1. By checking the source code, it is found that all the vector coordinates built in Leaflet are converted into WGS84 world geodetic coordinate system for rendering and display. Meanwhile, the slice calculation method is based on the projection parameters of map. CRS object. The calculation of the display position of vector data and slice data does not affect each other, so when the slice is offset, only the projection parameter of the map.crs object needs to be changed.
  2. Create CRS objects for the projection parameters of each coordinate system.
  3. When the base map is switched, map. CRS is automatically updated to the CRS object in the corresponding coordinate system.

The solution

  1. Using Proj4Leaflet, create CRS objects (the LEAFlet comes with CRS object definitions for wKID 4326 and 3857)
import L from 'leaflet'
import 'proj4leaflet'
const  resolutions = []  // Especially important, from the map service definition
L.CRS.EPSG4525 = new L.Proj.CRS('EPSG:4525'.'+proj=tmerc +lat_0=0 +lon_0=111 +k=1 +x_0=37500000 +y_0=0 +ellps=GRS80 +units=m +no_defs',
      {
        resolutions: resolutions,
        origin: []// Especially important, from the map service definition}); L.CRS.EPSG4526 =new L.Proj.CRS('EPSG:4526'.'+proj=tmerc +lat_0=0 +lon_0=114 +k=1 +x_0=38500000 +y_0=0 +ellps=GRS80 +units=m +no_defs',
      {
        resolutions: resolutions,
        origin: []// Especially important, from the map service definition}); L.CRS.EPSG4527 =new L.Proj.CRS('EPSG:4527'.'+proj=tmerc +lat_0=0 +lon_0=117 +k=1 +x_0=39500000 +y_0=0 +ellps=GRS80 +units=m +no_defs',
      {
        resolutions: resolutions,
        origin: []// Especially important, from the map service definition});Copy the code
  1. Toggle CRS while toggle layers
map.on('baselayerchange'.function(layer) {
        let center = map.getCenter();
        let zoom = map.getZoom();

        map.options.crs = L.CRS.EPSG4525; // Set according to the layer coordinate system
        map._resetView(center, zoom, true);
})
Copy the code

Pay attention to

Since the definition of wGS84 coordinate system is close to that of Dadei 2000 coordinate system, and the vector data of 4490 passed into the Leaflet for rendering does not generate any deviation, so the vector data does not need to be projected.