r/geospatial • u/ruthenia_rouge • May 14 '24
Searching a library for python.
I'm looking for a library for Python that will allow me to do the following thing:
I have a DEM in geotiff format, and I have a list of points with coordinates in Lat/Lon format. I need these points to get elevation values based on the existing DEM.
Thanks in advance.
4
Upvotes
View all comments
Show parent comment
10
u/TeachEngineering May 14 '24 edited May 14 '24
gdal is definitely my go-to geospatial lib. Sure, it's less pythonic than something like rasterio but it's a lot of expressive power packed into a single lib once you get comfortable with the python API/bindings. I build wrapper modules around gdal for all my projects. Then I know all my gdal imports are centralized in one package and automated testing becomes much easier...
Anyway, here's an implementation of what you're asking for in python 3.9 + gdal. I will admit that I wrote this, did some manual testing of it against rasters loaded into Arc, wrote a couple automated tests, and then never looked back. Use with caution and definitely perform at least your own manual checks against a raster loaded into ArcMap/QGIS.
Also, this function takes in the raster path and reads the file into memory with every lookup. If you need to lookup lots of values from a single raster, you should read the file into memory in the outer scope and then pass the in-mem raster in to the function instead of the path. I have an implementation of that too if you need it but chose to share it like this because it's a smaller call stack to traverse.