GDAL and OGR Python tips

Online documentation for GDAL/OGR Python is sparse. Here I show some recommended ways of learning more about GDAL/OGR in Python.

Using Python interpreter

You can learn about GDAL and OGR from inside the Python interpreter.

Start python interpreter:

$ python

Import the modules:

from osgeo import gdal,ogr,osr

Learn about the modules using ‘help’ and ‘dir’ in Python. These built-in functions work for any type of object (module, class, functions etc):

help(ogr) # display help for the ogr module
dir(ogr) # what's contained in ogr module?
help(ogr.Geometry) # display help for ogr.Geometry class
dir(ogr.Geometry) # show contents of an object, like functions on a class

Using is a Q&A site about GIS. Many people here know about GDAL and ORG, also about the Python bindings.

Some good questions I’ve found:

Transformation using OGR in Python

Will add more as I find them…

Importing data from a CSV file into a Postgres table

Simple CSV file import

You have a CSV file called “data.csv”. It has a header line, and is delimited using “;”. You want to import it into Postgres and a table called “your_table”:

Create the database table. Set column-types so the string fields in the CSV file, can be cast to values in columns.

CREATE TABLE your_table
  -- Your columns

Continue reading “Importing data from a CSV file into a Postgres table”

How to load an ESRI Shapefile into a PostGIS DB

Assuming a shapefile called myshapefile.shp, a table mytable in schema xyz, in a PostGIS enabled database called mydb on localhost. The table is owned by user dbuser who has password “secret”.

Using shp2pgsql

shp2pgsql myshapefile -I xyz.mytable > statements.sql
psql -d mydb -h localhost -U dbuser -f statements.sql

This tip and many more can be read in Making Maps Fast.

Using ogr2ogr

This is even easier with ogr2ogr:

ogr2ogr -f "PostgreSQL" PG:"host=localhost user=dbuser dbname=mydb password=secret" -lco SCHEMA=xyz myshapefile.shp

OpenStreetMap tiles with custom projection and grid using Mapnik

Previous effort

In a previous post I tried generating OpenStreetMap tiles using GeoServer. It ended when I couldn’t find a good style (SLD) to apply the OSM layer.

The (new) idea, that also failed miserably

In this post I tried to use Mapnik to generating OSM map tiles in EPSG:25832. It failed mainly because the Python scripts published by OSM for generating tiles don’t support epsg:25832 out of the box. Mapnik is however an obvious choice for OSM because:

  • Mapnik can read .osm files
  • There is a comprehensive style for Mapnik, that is being maintained by OSM

Continue reading “OpenStreetMap tiles with custom projection and grid using Mapnik”

OpenStreetMap tiles in EPSG:25832 projection using GeoServer

Warning: This is a description of how to create a OpenStreetMap WMS with GeoServer. It works fine up to the point where the layers published as an unstyled WMS. This is where I’ve not been able to produce a good result, because of lack of a good Styled Layer Descriptor (SLD). If you have hints about a good SLD, feel welcome to submit a comment!

The idea

The idea is/was to first create a good general purpose OpenStreetMap WMS, and then use GeoWebCache to generate tiles from this WMS source in a custom projection, epsg:25832 in our case.

Continue reading “OpenStreetMap tiles in EPSG:25832 projection using GeoServer”