Monday, 6 June 2022

Use external WMS to retrieve tiles with EPSG:6706 projection in Mapbox

I am trying to add an external WMS as a source using mapbox-gl. I know how to do it when the WMS supports EPSG:3857, as it is explained here. Unfortunately, the WMS service I need to use does not support that projection, therefore i cannot use the {bbox-epsg-3857} in the tiles url to take care of the bounding box parameter. Based on this solution for Google Maps, I tried a similar approach. However, mapbox-gl method addSource does not support a function as a parameter and I don't know how to pass x, y and zoom variables to obtain the bounding box coordinates converted to EPSG:6706. What am I missing here? Is there another viable approach to obtain the same result?

function bbox(x,y,z) {
    bl_lng=tile2long(x,z);
    tr_lng=tile2long((x+1),z);
    bl_lat=tile2lat(y+1,z);
    tr_lat=tile2lat((y),z);
    bl=proj4("WGS84","EPSG:6706",[bl_lng,bl_lat]);
    tr=proj4("WGS84","EPSG:6706",[tr_lng,tr_lat]);
    return bl[1]+","+bl[0]+","+tr[1]+","+tr[0];
}
function tile2long(x,z) { 
    return (x/Math.pow(2,z)*360-180);
}
function tile2lat(y,z) {
    var n=Math.PI-2*Math.PI*y/Math.pow(2,z);
    return (180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n))));
}
function checkWms(map){
    proj4.defs("WGS84", "+proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees");
    proj4.defs("EPSG:6706", "+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs");
    if(!map.getSource('wms-overlay-source')){
        map.addSource('wms-overlay-source', {
          'type': 'raster',
          'tiles': function(coord, zoom) {
            return "https://wms.cartografia.agenziaentrate.gov.it/inspire/wms/ows01.php?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&BBOX="+bbox(coord.x,coord.y,zoom)+"&CRS=EPSG:6706&WIDTH=256&HEIGHT=256&LAYERS=province,CP.CadastralZoning,acque,CP.CadastralParcel,fabbricati,codice_plla,simbolo_graffa&STYLES=default&FORMAT=image/png&DPI=96&MAP_RESOLUTION=96&FORMAT_OPTIONS=dpi:96&TRANSPARENT=TRUE"
            },
        });
    }
    if(!map.getLayer('wms-overlay-layer')){
        map.addLayer({
            'id': 'wms-overlay-layer',
            'type': 'raster',
            'source': 'wms-overlay-source',
            'paint': {}
          });
    }
}


from Use external WMS to retrieve tiles with EPSG:6706 projection in Mapbox

No comments:

Post a Comment