Loading data into PostGIS: Difference between revisions

From Wildsong
Jump to navigationJump to search
Brian Wilson (talk | contribs)
Brian Wilson (talk | contribs)
mNo edit summary
Line 1: Line 1:
[[Category: GIS]]
This page is now getting a little more attention and organization.
 
== The ESRI world ==


Paulo Corti's notes on
I want to be able to move data from ESRI proprietary formats into PostGIS.
[http://www.paolocorti.net/2006/07/27/migrating-shapefiles-to-postgis/ Migrating shapefiles to PostGIS] from "Thinking in GIS"
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.


What I am really about on this page is using Python to transfer data from ESRI proprietary formats into PostGIS.
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 [http://www.bartleby.com/129/ I would prefer not to.]


You need an ArcGIS license for this to access the ESRI formats. If your data are in shapefiles you don't need this...
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.


I already know how to iterate over an ESRI feature class using a cursor.
In fact you only need the license for a few things these days.  
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 ==
=== Iterate a feature class ===


Here is a simple script to iterate over a feature class and read its geometry.
Here is a simple script to iterate over a feature class and read its geometry.
Line 50: Line 51:
</pre>
</pre>


=== Write the feature to PostGIS ===
==== Write the feature to PostGIS ====


So now I want to put the points into PostGIS... how to do that?
So now I want to put the points into PostGIS... how to do that?
Line 56: Line 57:
I probably could use the GDAL python bindings.
I probably could use the GDAL python bindings.
I can probably figure out how to write the geometry directly to PostgreSQL
I can probably figure out how to write the geometry directly to PostgreSQL
== References ==
Paulo Corti's notes on
[http://www.paolocorti.net/2006/07/27/migrating-shapefiles-to-postgis/ Migrating shapefiles to PostGIS] from "Thinking in GIS"
[[Category: GIS]]

Revision as of 21:08, 30 October 2011

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"