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 ...

Get Python Geospatial Analysis Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.