I am building an application in leaflet, and I need to be able to access slope and aspect as a function of latitude and longitude. Currently I am able to get elevation data from the mapbox elevation dataset here, which can be processed pretty simply to get elevation as a function of latitude and longitude.

Does anyone know of a similar dataset, accesible in leaflet, that gives slope and aspect?

I know that ArcGIS online has a terrain dataset which contains these things. There is also a leaflet esri plugin that allows you to add the terrain layer in with L.esri.basemapLayer('Terrain'). But I don't see a way to actually extract data from that, just to view it within leaflet.

There is this article which praises leaflet's creator for his ability to calculate aspect and slope in realtime as the user pans or zooms, based on the mapbox elevation dataset I mentioned above. But frankly I don't understand the code that he's quoting, and this seems both inaccurate at low zoom levels, and computationally expensive for high resolution maps. I thought of potentially creating my own function which takes a point, gets 6 or 8 points around it, get the elevation of those points, and then calculates slope and aspect from that. But that seems inefficient and computationally expensive, especially when I'm going to need to get slope and aspect for hundreds of points many times per second.

I have been casually searching around for months for how to get slope and aspect data within leaflet. If I have to learn a whole new framework to import into leaflet, I'm open to that. I'm just not sure where to start. Forgive me if this question is in the wrong place or has been answered elsewhere.

  • 1
    If you have access to MapBox elevation data, you can use it in Leaflet. If formula is efficient for example you are citing, it will be equally efficient in Leaflet.
    – TomazicM
    Oct 24, 2019 at 7:27
  • Right except I don't understand the code in the example I am citing.... Is the codeblock quoted in the Digital Geography article complete? I don't see how those few lines of code results in aspect and slope. I'm not understanding the raster2dem function there. Oct 24, 2019 at 23:52
  • 2
    To see the complete code that you will be able to use in Leaflet you have to take a look at the example itself: labs.mapbox.com/bites/00009. Calculating part for hillshade you are interested is in JS at labs.mapbox.com/bites/00009/hillshade.js
    – TomazicM
    Oct 25, 2019 at 6:01

1 Answer 1


I had put this on the backburner for some time. But the problem had been sufficiently bothering me, and I really needed a solution for the app I'm working on. So I figured it out. The answer is a bit much to post here, so I wrote a 2 part article using ObservableHQ.

Slope and Aspect in leaflet:

Writing these helped organize my thoughts, hopefully they will be helpful to others in the future who need this information.

Edit: way later...

I wrote all that functionality into a handy npm package, should anyone ever need it. Check it out here: leaflet-topography

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.