Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm attempting a python script that takes data from a file and updates only the geometry and a key field in a post gis table. But no matter what I try the geometry always seems to be updated null.

I've tried many variations but the below is my most simplified version, yet is still exhibiting the problem.

from osgeo import ogr
import sys
import psycopg2

#... Open file
driver = ogr.GetDriverByName('Mapinfo File')
ds = driver.Open('C:/Users/me/workspace/BoundaryData/MyFile.tab', 0)
layer = ds.GetLayer()
sf = layer.GetNextFeature()
geom = sf.GetGeometryRef()

#... setup database connection (Extrenalise later)
connection = psycopg2.connect(dbname="spatial", user="postgres", password="******")
cursor = connection.cursor()

text = geom.ExportToWkt()
print text

binary = geom.ExportToWkb()
print binary

cursor.execute("""UPDATE "boundaryAreas" SET "areaObject"= st_geomfromtext(%s,4326), "areaDesc"='PythonUpdate' WHERE "boundaryName" = 'myBoundary' AND "areaCode" = 'WA406';""", (text,))        
connection.commit()

cursor.execute("""UPDATE "boundaryAreas" SET "areaObject"= st_geomfromwkb(%s,4326), "areaDesc"='PythonUpdate' WHERE "boundaryName" = 'myBoundary' AND "areaCode" = 'WA406';""", (binary,))        
connection.commit()
sys.exit()

Both print statements work giving a wkt and a binary output.

The first cursor.execute does not error. But the geomerty is updated to null while the areaDesc gets it proper update. No SQL error in the postgis log. If I manually copy the text into the update statement and just run it as a query using pgadmin it works/updates fine.

The second update does error with "WKB structure does not match expected size!". The displayed statement has the wkb as null (ie. st_geomfromwkb('',4326) ).

Also, if I watch the text variable it shows as null/None, but it still outputs when printed. Watching the binary says it is a huge string.

From what I can gather it is behaving like the variable are sometimes treated as null and sometimes with their intended value.

I'm a Python Newb and I've been stuck on this for a few days now so any help would be most welcome.

Anyone got any ideas, suggestions, leads?

share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.