how to overlay a raster image on leaflet, on demand?

1.9k views Asked by At

I am using a lat&long coordinates I receive in input by the user to visualize a map with leaflet.js. As I do that, I compute a heatmap in the background (e.g. population density map in the 5km-wide area surrounding the chosen coordinates).

How do I overlay the heatmap to my map?

Here's what I got so far: an html file to generate the map, and a .js file created by my background computations where I store the chosen coordinates, some polygons or interest, and where I hope to store/point-to the raster map I generate for that region.

<!DOCTYPE html>
<html>
<head>
  <title>Testmap</title>
  <script src="http://d3js.org/d3.v3.min.js"></script>
  <script src="http://d3js.org/topojson.v1.min.js"></script>
  <style>
    @import url(http://cdn.leafletjs.com/leaflet-0.6.1/leaflet.css);
    #overlay{
      fill:None;
      stroke:#ff00ff;
      stroke-width:4px;
    }
  </style>
</head>
<body>
  <div id="map" style="width: 960px; height: 600px"></div>
  <script src="http://cdn.leafletjs.com/leaflet-0.6.1/leaflet.js"
  </script>
  <script src="rois.js"></script>
  <script>
  var toolserver = L.tileLayer('http://{s}.www.toolserver.org/tiles/bw-mapnik/{z}/{x}/{y}.png');
  var stamen = L.tileLayer('http://{s}.tile.stamen.com/toner/{z}/{x}/{y}.png', {attribution: 'myMap'}).addTo(map);
  var baseLayers = {"stamen": stamen, "toolserver-mapnik":toolserver};

  var geojson = L.geoJson(rois, {
      onEachFeature: onEachFeature
  }).addTo(map)

  var overlays = {
      "geoJson": geojson
  };

  function onEachFeature(feature, layer){
    if (feature.properties) {
      layer.bindPopup("<b>" + feature.properties.street + "</b> is " + feature.properties.length + "km long.");
    }
  }

  var svgContainer= d3.select(map.getPanes().overlayPane).append("svg");
  var group= svgContainer.append("g").attr("class", "leaflet-zoom-hide");
  var path = d3.geo.path().projection(project);
  function project(point) {
    var latlng = new L.LatLng(point[1], point[0]);
    var layerPoint = map.latLngToLayerPoint(latlng);
    return [layerPoint.x, layerPoint.y];
  }  

  </script>
 </body>
</html>

The file rois.js is generated by my program in the background and contains the chosen coordinates and some polygons to be drawn on the map. I would like to understand how I can include raster information in here.

var map = L.map('map').setView([52.500,13.385], 13);
var rois = [{
  "type": "FeatureCollection",                                                                        
  "features": [{
    "type": "Feature",
    "id": 1, 
    "geometry": {
      "type": "Polygon",
      "coordinates": [[[13.370, 52.491], [13.400, 52.491], [13.400, 52.509],[13.370, 52.509],[13.370, 52.491]]]
    }
  },
  { "type": "Feature",
    "id": 2, 
    "geometry": {
      "type": "Polygon",
      "coordinates": [[[13.415, 52.496], [13.425, 52.505], [13.435, 52.496], [13.415, 52.496]]]
    }
  }]
}];

Thanks FP

1

There are 1 answers

0
IvanSanchez On

See the Leaflet plugins for generating heatmaps (and update your leaflet version, 0.6.x is more than three years old).