2012-02-07 17:50:46 +01:00
|
|
|
from logging import getLogger
|
|
|
|
|
2011-09-29 16:40:49 +02:00
|
|
|
from ckan.lib.base import config
|
2012-02-07 17:50:46 +01:00
|
|
|
from ckan import model
|
2011-04-13 13:05:59 +02:00
|
|
|
from ckan.model import Session
|
2011-09-29 16:40:49 +02:00
|
|
|
from ckan.model.meta import *
|
|
|
|
from ckan.model.domain_object import DomainObject
|
|
|
|
from geoalchemy import *
|
|
|
|
from geoalchemy.postgis import PGComparator
|
2011-04-13 13:05:59 +02:00
|
|
|
|
2012-02-07 17:50:46 +01:00
|
|
|
log = getLogger(__name__)
|
|
|
|
|
|
|
|
package_extent_table = None
|
|
|
|
|
2012-04-12 20:49:12 +02:00
|
|
|
DEFAULT_SRID = 4326 #(WGS 84)
|
2012-02-07 17:50:46 +01:00
|
|
|
|
|
|
|
def setup(srid=None):
|
|
|
|
|
|
|
|
if package_extent_table is None:
|
2012-04-13 11:58:39 +02:00
|
|
|
define_spatial_tables(srid)
|
2012-02-07 17:50:46 +01:00
|
|
|
log.debug('Spatial tables defined in memory')
|
|
|
|
|
|
|
|
if model.repo.are_tables_created():
|
|
|
|
if not Table('geometry_columns',metadata).exists() or \
|
|
|
|
not Table('spatial_ref_sys',metadata).exists():
|
|
|
|
raise Exception('The spatial extension is enabled, but PostGIS ' + \
|
|
|
|
'has not been set up in the database. ' + \
|
|
|
|
'Please refer to the "Setting up PostGIS" section in the README.')
|
|
|
|
|
|
|
|
|
|
|
|
if not package_extent_table.exists():
|
|
|
|
try:
|
|
|
|
package_extent_table.create()
|
|
|
|
except Exception,e:
|
|
|
|
# Make sure the table does not remain incorrectly created
|
|
|
|
# (eg without geom column or constraints)
|
|
|
|
if package_extent_table.exists():
|
|
|
|
Session.execute('DROP TABLE package_extent')
|
|
|
|
Session.commit()
|
|
|
|
|
|
|
|
raise e
|
|
|
|
|
|
|
|
log.debug('Spatial tables created')
|
|
|
|
else:
|
|
|
|
log.debug('Spatial tables already exist')
|
|
|
|
# Future migrations go here
|
|
|
|
|
|
|
|
else:
|
|
|
|
log.debug('Spatial tables creation deferred')
|
|
|
|
|
2011-04-13 13:05:59 +02:00
|
|
|
|
2011-09-29 16:40:49 +02:00
|
|
|
class PackageExtent(DomainObject):
|
|
|
|
def __init__(self, package_id=None, the_geom=None):
|
|
|
|
self.package_id = package_id
|
|
|
|
self.the_geom = the_geom
|
|
|
|
|
2012-04-13 11:58:39 +02:00
|
|
|
def define_spatial_tables(db_srid=None):
|
2011-09-29 16:40:49 +02:00
|
|
|
|
2012-02-07 17:50:46 +01:00
|
|
|
global package_extent_table
|
2011-09-29 16:40:49 +02:00
|
|
|
|
2012-04-13 11:58:39 +02:00
|
|
|
if not db_srid:
|
|
|
|
db_srid = int(config.get('ckan.spatial.srid', DEFAULT_SRID))
|
|
|
|
else:
|
|
|
|
db_srid = int(db_srid)
|
|
|
|
|
2012-02-07 17:50:46 +01:00
|
|
|
package_extent_table = Table('package_extent', metadata,
|
|
|
|
Column('package_id', types.UnicodeText, primary_key=True),
|
|
|
|
GeometryExtensionColumn('the_geom', Geometry(2,srid=db_srid)))
|
2011-09-29 16:40:49 +02:00
|
|
|
|
|
|
|
|
2012-02-07 17:50:46 +01:00
|
|
|
mapper(PackageExtent, package_extent_table, properties={
|
|
|
|
'the_geom': GeometryColumn(package_extent_table.c.the_geom,
|
|
|
|
comparator=PGComparator)})
|
2011-04-13 13:05:59 +02:00
|
|
|
|
2012-02-07 17:50:46 +01:00
|
|
|
# enable the DDL extension
|
|
|
|
GeometryDDL(package_extent_table)
|
2011-09-29 16:40:49 +02:00
|
|
|
|
2011-04-13 13:05:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
|