I'm using sqlalchemy for a mostly non geographic database, queried and populated using a desktop program that knows nothing of geometries.
I'm now slowly adding geographic awareness to it and I'm going to write a separate program, I'm not going to enhance the old desktop application, which should be able to continue doing its job regardless whether or not the database has been expanded to a spatial database.
The program is about botanic collections, and in the expanded database plants would be located to a point in space. The decision is not definitive, but I'm considering a spatial database (a point
column) rather than just adding the two lat
and lon
columns to the plant
table.
If I go for a spatial database, even though I do not plan to let the user insert coordinates in the desktop application, still it would be nice to be able to show the coordinates for plants that do have coordinates, for databases that did get expanded spatially.
If I go for the lat
and lon
columns, I can expand the SQLAlchemy database model as to include the coordinates of plants and there would be no technical issues.
If the database system is PostgreSQL+PostGIS, I execute this and it works:
session = object_session(row)
result = session.execute('select id, coords from plant where id=%d' % row.id)
myid, mycoords = result.fetchone()
if mycoords[:2] == '01':
r = '>Q'
else:
r = '<Q'
import struct
lon = struct.unpack('d', struct.pack(r, int(mycoords[-32:-16], 16)))[0]
lat = struct.unpack('d', struct.pack(r, int(mycoords[-16:], 16)))[0]
but with SQLite, the above query gives me a None
where I would expect at least a BLOB. I do get a 60 bytes BLOB if I connect to the database directly like this:
import sqlite3
cn = sqlite3.connect("/home/mario/.bauble/geo-btuu-11.db")
cr = cn.cursor()
cr.execute("select id, coords from plant where coords is not null")
r = cr.fetchall()
In any case I do not know how to handle the BLOB should I manage to retrieve it.