Slicing rasters and DTMs into sections using Python and geo spatial libraries
Hellow! I am a professional hydrographic data processor and when my next job comes to the stage of exporting processed data, I often need to output the results by dividing them into small parts. This process usually takes a lot of attention and time as the most of modern utilities offer to export data one by one: one border - one export. I know only one software that allows to export data by many polygons at once. And anyway, usually I will need to rename a pile of files with my hands when the exports is done. Once I thought that it would be great to export the whole data at once and then slice and rename them automatically.
If you experience the same difficulties from time to time - then this article may give you a answer on how to make your life easier.
It turned out that my old assistant - Scripting Language - Python - can do it again. This programming language is distinguished by a huge number of separately installable libraries that allow you to work with almost any task. That's why it is so popular among professionals in various fields.
This time we will need just a few libraries:
"Pandas" - to load CSV data
"Rasterio" - to work with raster images
"Shapely" - to work with Borders in .shp format
"Geopandas" - for geospatial calculation
5. Define the input and output folders:
6. Run the cycle though all the files fined:
领英推荐
The code has comments for each section so I won't explain it in detail. However, I will tell you the main points of its work. First of all this is the cycle in cycle first cycle goes through the raster files placed to the "in_" - folder. Once first file is selected and loaded it starts another cycle through the each border placed to "shp_in" folder. It clips loaded raster file with the border and renames it. In this case it just changes "ALL" word to the border file name. There are many options how to make it more sophisticated and adjustable. Then it saves the clipped part to the "out__" folder and goes to the next border. Once all border used for current file it goes to the next file.
For the example I used 6.2 gb encoded raster and 4 .shp borders and I've got the results within 10 seconds:
The script for the DTM data is similar excepting there is no "rasterio" library used ther is a "pandas" to load the DTM csv file into DataFrame.
The scripts for raster and TDM are available for anyone by request.
Survey Data Processor (Freelancer).Currently Available For Opportunities.
1 个月Useful tips
Data processor and Marine Geologist in GEOxyz
1 个月Thanks for sharing! Very useful!!! ;)
Cat A Hydrographic Surveyor / Senior Data Processor
1 个月Thanks for sharing! pretty useful Qgis also has a nice tool called retile raster that may also help but not sure if it allows you to use a predefined shape file however you can tile you raster based on the desired tile width and height.
Senior Data Processor at Sulmara Subsea
1 个月The scripts are placed in: https://drive.google.com/drive/folders/1vmcOhwRfqP4z-lmEDliQ_zVdI8yvvv5X?usp=drive_link