Loading data into PostGIS

From Wildsong
Revision as of 21:08, 30 October 2011 by Brian Wilson (talk | contribs)
Jump to navigationJump to search

This page is now getting a little more attention and organization.

The ESRI world

I want to be able to move data from ESRI proprietary formats into PostGIS. I probably want to be able to hand a set of tools to my GIS Analyst co-workers so they can get the data into the fabled PostGIS data warehouse without my help.

Note also I might be able to use an ArcSDE license (aka ArcGIS Server) to access PostGIS but I regard that as so much extra work it's not worth it. Also it requires an ArcInfo license on the desktop and I would prefer not to.

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

In fact you only need the license for a few things these days.

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?

I probably could use the GDAL python bindings. I can probably figure out how to write the geometry directly to PostgreSQL

References

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