If you have the spatialite libraries and the pysqlite2 module, it's as
easy as writing a SQL statement. It has a little GIS overhead, but the
software involved is all FOSS.

 It goes something like this:


import os
from pysqlite2 import dbapi2 as sql
sql_connection = sql.Connection('Geo.sqlite')     # This is the path
to your spatial database. If it doesn't exist, it will be created,
without the required tables that make it a true spatial database (you
can still query the geometry)
cursor = sql.Cursor(sql_connection)
sql_connection.enable_load_extension(1)
sql_connection.load_extension('libspatialite-2.dll')        #You need
this DLL (for Windows) and some others, all available on the
Spatialite download page. (http://www.gaia-gis.it/spatialite-2.3.0/
binaries.html)
 
#  Linux/Mac, it's similar, but you'll have to figure out the
differences


# Once the extension is loaded and the database created, you have to
add the shapefile as a virtual table. You could add the shapefile to
the Spatialite database directly, and that would be much more simple,
but not pure python;

shapename = "C:/SHAPEFILE.shp"
shapename = shapename.rsplit('.',1)[0]
tablename = shapename.rsplit('/',1)[1]                   # the  right
splits get rid of the extension and the filepath. Adjust as necessary
to your file path and shapefile name

spatialref = 2227              # You have to indicate the spatial
reference code (also known as an SRID or EPSG code). This particular
code is for NAD83 California Zone 3

virtual_table_create = "Create VIRTUAL TABLE '%s' USING VirtualShape
('%s','%s',%s)" % (tablename, shapename, 'CP1252', str(spatialref) )

cursor.execute(virtual_table_create)

#Now the table is added to the database as a virtual table. and you
can query the table

cursor.execute('select * from %s where Y(Geometry) LIKE 38.017821'  %
tablename)
# this is an example SQL statement. Geometry will be returned as a
BLOB, so to make it human readable, use X(Geometry), Y(Geometry), or
just AsText(Geometry)

results =  cursor.fetchall()
first_record = results[0]
next_record = results[1]    #and so on...



Good luck!!!

Reply via email to