Book Image

Python Geospatial Analysis Cookbook

Book Image

Python Geospatial Analysis Cookbook

Overview of this book

Table of Contents (20 chapters)
Python Geospatial Analysis Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Other Geospatial Python Libraries
Mapping Icon Libraries
Index

Converting a raster (GeoTiff) to a vector (Shapefile) using GDAL


We have now looked at how we can go from a vector to a raster, so it is now time to go from a raster to a vector. This method is much more common because most of our vector data is derived from remotely sensed data, such as satellite images, orthophotos, or some other remote sensing dataset, such as lidar.

Getting ready

As usual, enter the workon pygeoan_cb command in your Python virtual environment:

$ source venvs/pygeoan_cb/bin/activate

How to do it...

This recipe only requires four steps utilizing OGR and GDAL so please open up a new file for your code:

  1. Import the ogr and gdal modules and go straight ahead and open the raster we want to convert by passing it the filename on disk and getting a raster band:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    from osgeo import ogr
    from osgeo import gdal
    
    #  get raster data source
    open_image = gdal.Open( "../geodata/cadaster_borders-2tone-black-white.png" )
    input_band = open_image.GetRasterBand...