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

Batch importing a folder of Shapefiles into PostGIS using ogr2ogr


We would like to extend our last script to loop over a folder full of Shapefiles and import them into PostGIS. Most importing tasks involve more than one file to import, so this makes it a very practical task.

How to do it...

Our script will reuse the previous code in the form of a function so that we can batch process a list of Shapefiles to import into the PostgreSQL PostGIS database.

  1. We will create our list of Shapefiles from a single folder for the sake of simplicity:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import subprocess
    import os
    import ogr
    
    
    def discover_geom_name(ogr_type):
        """
        
        :param ogr_type: ogr GetGeomType()
        :return: string geometry type name
        """
        return {ogr.wkbUnknown            : "UNKNOWN",
                ogr.wkbPoint              : "POINT",
                ogr.wkbLineString         : "LINESTRING",
                ogr.wkbPolygon            : "POLYGON",
                ogr.wkbMultiPoint       ...