Loading data into PostGIS

From Wildsong
Jump to navigationJump to search


Paulo Corti's notes on Migrating shapefiles to PostGIS from "Thinking in GIS"

What I am really about on this page is using Python to transfer data from ESRI proprietary formats into PostGIS.

You need an ArcGIS license for this to access the ESRI formats. If your data are in shapefiles you don't need this...

I already know how to iterate over an ESRI feature class using a cursor. I might have trouble with the geometry... pretty sure I can do it though, especially for point files! Points are easy.

Iterate a feature class

Here is a simple script to iterate over a feature class and read its geometry. The nice thing about using the ESRI code for this is that it does not matter what the data source is, it's just a 'feature class' that can be stored in a shapefile or a personal geodatabase or ArcSDE... etc...

#!/usr/bin/env python

import arcgisscripting 
gp = arcgisscripting.create(9.3)

# Hard coded data source, for simplicity.
workspace = 'D:/AGIProducts/IncidentView_Data/Data/TEMPORARY_WORKSPACE/WA_King.gdb'
featureclass = 'test_Addresses_points'

gp.workspace = workspace
desc = gp.describe(featureclass) # Get some metadata
shapefieldname = desc.ShapeFieldName
print "ShapeType %s" % desc.ShapeType
print "Indexed =", desc.HasSpatialIndex
print "Shape field", shapefieldname

fields = gp.ListFields(featureclass, '*') # Get a list of the attributes

rows = gp.SearchCursor(featureclass)
row = rows.Next()
i = 0
while row:
    feature = row.GetValue(shapefieldname)
    # If this is a point, we can just grab its shape
    point = feature.GetPart()
    print i, point.x, point.y
    row = rows.Next()
    i += 1

del gp # Release the geoprocessing object
# That's all!

Write the feature to PostGIS

So now I want to put the points into PostGIS... how to do that? The only lead I can find is GeoTypes 0.7.0, here http://initd.org/svn/psycopg/geotypes/