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.

What I'm trying to achieve is to create a geometry column of a point based on the latitude and longitude of that particular row in the already created table.

Given the following code:

#!/usr/local/bin/python
from pysqlite2 import dbapi2 as sqlite3

conn = sqlite3.connect('adde.sqlite')
conn.enable_load_extension(True)
conn.execute("SELECT load_extension('libspatialite-2.dll');")
conn.execute("SELECT InitSpatialMetadata();")
conn.enable_load_extension(False)
conn.execute("SELECT AddGeometryColumn('coords','geom_column', 4326, 'Point', 2);")
conn.execute("UPDATE coords SET geom_column = GEOMETRYFROMTEXT('POINT('||longitude||' '||latitude||')', 4326);")
conn.close()

I receive the error:

AddGeometryColumn() error: "insert on table 'geometry_columns' violates constraint: 'spatial_ref_sys.srid'"

I've been able to create the column in the Spatialite GUI no problem, but I'd like to do all of this programmatically using Python. I've searched for a while now and I'm coming up a bit short. Any guidance as to why I'm receiving this particular error would be greatly appreciated.

I'm using Python 2.6 and pysqlite2 on Windows 7 64-bit

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

I solved it by inserting the definition for the required SRS into the database before adding the geometry column:

conn.execute("INSERT INTO spatial_ref_sys (srid, auth_name, auth_srid, ref_sys_name, proj4text) VALUES (4326, 'epsg', 4326, 'WGS 84', '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')")

share|improve this answer
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.