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

Reprojecting a Shapefile from one projection to another


Working with spatial data from multiple sources leads to data that's most likely from multiple regions on Earth with multiple coordinate systems. To perform consistent spatial analysis, we should transform all our input data into the same coordinate system. This means reprojecting your Shapefile into your chosen working coordinate system.

In this recipe, we will reproject a single Shapefile from ESPG:4326 into a web mercator system EPSG:3857 for use in a web application.

How to do it...

Our goal is to reproject a given Shapefile from one coordinate system to another; the steps to do this are as follows:

  1. Create a new Python file named ch02_06_re_project_shp.py in your /ch02/code/working/ directory and add the following code:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import ogr
    import osr
    import os
    
    shp_driver = ogr.GetDriverByName('ESRI Shapefile')
    
    # input SpatialReference
    input_srs = osr.SpatialReference()
    input_srs.ImportFromEPSG...