Converting the route to a shapefile
The raster version of the least cost path route is useful for visualization, but it isn’t very good for analysis because it is embedded in the raster, and it is, therefore, difficult to relate to other datasets as we have done so many other times in this book. Our next goal will be to use the path data that we saved when creating the route to create a shapefile since the saved data is in the proper order. The following code will convert our raster path to a shapefile that is easier to use in a GIS for analysis:
- First, let’s import the necessary modules. We’ll need
pickle
to load the previously saved path,getline
fromlinecache
to read the header information from a file, andshapefile
to write shapefiles:import pickle from linecache import getline import shapefile
- Next, we define a function named
pix2coord
to convert pixel coordinates to real-world coordinates. This function takes in three parameters:gt
for geotransform...