Leaflet.TileLayer.Swiss icon indicating copy to clipboard operation
Leaflet.TileLayer.Swiss copied to clipboard

error message after integrating the script

Open strukturart opened this issue 1 year ago • 3 comments

could it be that the script from map.geo.admin.ch has a bug?

this is how i use leaflet + map.geo.admin.ch , it works but the error message is spit out ?

  let  map = new L.Map("map", {
      crs: L.CRS.EPSG3857,
      continuousWorld: true,
      worldCopyJump: false,
      zoom: 20,
      center: [47, 7],
    });


    var url =
      "https://wmts20.geo.admin.ch/1.0.0/ch.swisstopo.pixelkarte-grau/default/current/3857/{z}/{x}/{y}.jpeg";
    var tilelayer = new L.tileLayer(url, {
      attribution: "map.geo.admin.ch",
    });
    map.addLayer(tilelayer);

    var d = L.tileLayer.wms(" https://wms.geo.admin.ch/", {
      layers:
        "ch.swisstopo.amtliches-gebaeudeadressverzeichnis,ch.swisstopo.amtliches-strassenverzeichnis",
      SERVICE: "WMS",
      REQUEST: "GetMap",
      format: "image/png",
      transparent: true,
      attribution: "map.geo.admin.ch",
    });
    map.addLayer(d);

projection.js

Line 6

import L from 'leaflet';
import * as swissgrid from 'swissgrid';

// Bounding box for tiles in LV03
// Source: https://wmts.geo.admin.ch/EPSG/21781/1.0.0/WMTSCapabilities.xml
const LV03_BOUNDS = L.bounds(
  [420000, 30000],
  [900000, 350000],
);

// Bounding box for tiles in LV95
// Source: https://wmts.geo.admin.ch/EPSG/2056/1.0.0/WMTSCapabilities.xml
const LV95_BOUNDS = L.bounds(
  [2420000, 1030000],
  [2900000, 1350000],
);

function leafletProjection(swissgridProjection, bounds) {
  return {
    bounds,
    project: ({ lng, lat }) => {
      const [E, N] = swissgridProjection.project([lng, lat]);
      return L.point(E, N);
    },
    unproject: ({ x: E, y: N }) => {
      const [lng, lat] = swissgridProjection.unproject([E, N]);
      return L.latLng(lat, lng);
    },
  };
}

export const lv03 = leafletProjection(swissgrid.lv03, LV03_BOUNDS);
export const lv95 = leafletProjection(swissgrid.lv95, LV95_BOUNDS);

strukturart avatar Sep 10 '22 19:09 strukturart

It looks like you are using this URL:

https://wmts20.geo.admin.ch/1.0.0/ch.swisstopo.pixelkarte-grau/default/current/𝟯𝟴𝟱𝟳/{z}/{x}/{y}.jpeg

3857 refers to the Web Mercator projection. This is the same that OpenStreetMap is using, and Leaflet supports it by default, so you don't need this plugin.

The disadvantage of using Web Mercator for Swiss maps is that the national maps of Switzerland originally use the Swiss coordinate system LV95, so the Web Mercator version can look a bit distorted, especially in the East of Switzerland:

image Extract of Swiss map in Web Mercator projection

image Similar extract in original LV95 projection

I am still not sure where your error comes from, but I will leave this issue open because I should probably clarify the difference between projections in the README.

rkaravia avatar Sep 21 '22 09:09 rkaravia

By the way, here would be an example for showing the Gebäudeverzeichnis / Strassenverzeichnis layers with this plugin:

https://codepen.io/rkaravia/pen/eYrENYp

Created like this:

  • Go to https://leaflet-tilelayer-swiss.karavia.ch/layers.html and look for ch.swisstopo.pixelkarte-grau
  • Click "Edit on CodePen" link
  • Add WMS layer definition from issue description above
  • Add maxZoom: 27 and remove some lines that are not required in WMS layer options
var options = {
  layer: "ch.swisstopo.pixelkarte-grau"
};

var layers = [L.tileLayer.swiss(options)];

var map = L.map("mapid", {
  crs: L.CRS.EPSG2056,
  layers: layers
});

map.fitSwitzerland();

var overlay = L.tileLayer.wms("https://wms.geo.admin.ch/", {
  layers: "ch.swisstopo.amtliches-gebaeudeadressverzeichnis,ch.swisstopo.amtliches-strassenverzeichnis",
  format: "image/png",
  transparent: true,
  maxZoom: 27
});
map.addLayer(overlay);

rkaravia avatar Sep 21 '22 09:09 rkaravia

Thank you for your answer. I am now using the layers without your plugin. btw: do you know how to get the tiles to be loaded in zoom level 24? because i need the map display for bike couriers who need more details.

    map = new L.Map("map", {
      crs:L.CRS.EPSG3857,
      continuousWorld: true,
      worldCopyJump: false,
      zoom: 20,
      maxZoom: 27,
      minZoom: 5,
      center: [47, 7],
    });

    map.setView(L.latLng(46.57591, 7.84956), 8);

  
    var d = L.tileLayer.wms(" https://wms.geo.admin.ch/", {
      layers:
        "ch.swisstopo.amtliches-gebaeudeadressverzeichnis,ch.swisstopo.amtliches-strassenverzeichnis,ch.kantone.cadastralwebmap-farbe",
      SERVICE: "WMS",
      REQUEST: "GetMap",
      format: "image/png",
      transparent: true,
      TILED: true,
      TileSetId: 24,
      attribution: "map.geo.admin.ch",
    });

    map.addLayer(d);

strukturart avatar Sep 21 '22 19:09 strukturart