0

I am trying to use display shapefile data on top of raster images using Leaflet. I render the shapefile data using leaflet L.tileLayer.wms function.

The size of shapefile is 300mb. The problem is that the rendering is crazy slow when I use MapServer. It takes almost a minute to render it on raster images.

I did the same thing using GeoServer with Leaflet and it rendered the same shapefile almost instantly.

This is the Leaflet viewer I Used for both MapServer and GeoServer.

<html>
  <head>
    <title>Leaflet WMS and XYZ</title>
    <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
    <style>
      #map {
        width: 100%;
        height: 500px;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
    <script>
      var map = L.map("map").setView([30.479506155652967, 71.2240726004029], 5);
      
     //This is the raster images stored locally on my disk.
      var xyz = L.tileLayer(
        "http:/localhost:3000/{z}/{x}/{y}",
        {
          attribution:
            'This renders raster images as basemap which was downloaded from open street map'
        }
      );


      //This is the mapserver link which uses wms to render shapefile
      //Notes: This is using mapserver In case of Geoserver I simply replace it
      // with geoserver link.
      var wms_layer = L.tileLayer.wms(
        "http://localhost/cgi-bin/mapserv.exe?map=C:/ms4w/apps/msolp/mymap.map",
        {
          layers: "road",
          format: "image/png",
          transparent: true,
          version: "1.1.0",
          attribution: "ROADS"
        }
      );

      var baseLayers = {
        XYZ: xyz
      };

      var overlays = {
        WMS: wms_layer
      };

      var control = L.control.layers(baseLayers, overlays).addTo(map);

      xyz.addTo(map);
      wms_layer.addTo(map);
    </script>
  </body>
</html>

As for Mapfile This is is how my mapfile look likes

MAP
  NAME "MyMap"
  STATUS ON
  SIZE 400 300
  EXTENT 60.817362 24.043970 77.126716 37.143147
  UNITS DD
  SHAPEPATH "data"
  PROJECTION
    "init=epsg:4326"
  END 

  WEB
    IMAGEPATH "/ms4w/tmp/ms_tmp/"
    IMAGEURL "/ms_tmp/"
    METADATA
      "tile_map_edge_buffer" "10"
      "tile_metatile_level" "0"
      "wms_title" "My Global Map WMS Server"
      "wms_onlineresource"  "http://localhost/cgi-bin/mapserv.exe?map=C:/ms4w/apps/msolp/mymap.map" #must change mapfile path
      "wms_srs"             "EPSG:4326 EPSG:3857 EPSG:4269"
      "wms_enable_request"  "*"
    END
  END

 
  LAYER
    NAME road
    TYPE LINE
    DATA "gis_osm_roads_free_1.shp"
    STATUS DEFAULT
    CLASS
      STYLE
        COLOR 255 0 0
        OUTLINECOLOR 0 0 255
      END
    END


  
  END



END
8
  • 1
    Does your shapefile has a spatial index file .qix? If it is missing, create it with shptree and see if it helps. But a minute is so long time that there is probably some other fundamental issue. Is the MapServer layer also slow in QGIS?
    – user30184
    Commented Feb 5, 2023 at 13:26
  • @user30184 its not a minute but solid 30-35 seconds and takes even longer when I zoom in and zoom out since it loads again for every zoom level. I have found tilemode in mapserver which servers only the requested tile of that specific zoom level which might help in increasing the speed significantly but the problem is I cannot get it to work with leaflet. I haven't tried using it with qgis let me check but Geoserver renders the shapefile much faster. Also qix is present
    – SomeOne
    Commented Feb 5, 2023 at 13:33
  • Update I tried it with qgis it is giving fast enough speed considering how big the size of the file is.
    – SomeOne
    Commented Feb 5, 2023 at 13:53
  • Do you run MapServer as cgi or fcgi? Time to start cgi may pile up with many small tiled requests.
    – user30184
    Commented Feb 5, 2023 at 19:46
  • @user30184 i use cgi
    – SomeOne
    Commented Feb 6, 2023 at 18:07

1 Answer 1

1

I'd try and avoid using tiles when accessing dynamically generated data, and instead request a single WMS image. This will avoid reading from the large file 20-30 times each time you move around the map.

I'm not sure Leaflet supports this without plugins. https://github.com/ptv-logistics/Leaflet.NonTiledLayer is one plugin that should support it (see also thread at https://github.com/Leaflet/Leaflet/issues/6936).

You can test with OpenLayers to see if there is a difference in speed - https://openlayers.org/en/latest/examples/wms-image.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.