Loading data into PostGIS: Difference between revisions
Brian Wilson (talk | contribs) |
Brian Wilson (talk | contribs) |
||
Line 17: | Line 17: | ||
<pre> | <pre> | ||
#!/usr/bin/env python | |||
import arcgisscripting | import arcgisscripting | ||
gp = arcgisscripting.create(9.3) | gp = arcgisscripting.create(9.3) | ||
# Hard coded data source, for simplicity. | # Hard coded data source, for simplicity. | ||
workspace = 'D:/AGIProducts/IncidentView_Data/Data/ | workspace = 'D:/AGIProducts/IncidentView_Data/Data/TEMPORARY_WORKSPACE/WA_King.gdb' | ||
featureclass = ' | featureclass = 'test_Addresses_points' | ||
gp.workspace = workspace | gp.workspace = workspace | ||
desc = gp.describe(featureclass) # Get some metadata | desc = gp.describe(featureclass) # Get some metadata | ||
fields = gp. | 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) | rows = gp.SearchCursor(featureclass) | ||
row = rows.Next() | row = rows.Next() | ||
i = 0 | |||
while row: | 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! | |||
</pre> | </pre> |
Revision as of 23:21, 3 July 2009
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!