2012-02-07 17:50:46 +01:00
|
|
|
from logging import getLogger
|
|
|
|
|
2015-03-27 16:57:55 +01:00
|
|
|
from sqlalchemy import Table
|
2012-06-25 20:27:18 +02:00
|
|
|
|
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
|
2012-06-25 20:27:18 +02:00
|
|
|
from ckan.model import meta
|
2011-09-29 16:40:49 +02:00
|
|
|
from ckan.model.domain_object import DomainObject
|
2011-04-13 13:05:59 +02:00
|
|
|
|
2015-03-27 16:57:55 +01:00
|
|
|
from ckanext.spatial.geoalchemy_common import setup_spatial_table
|
|
|
|
|
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')
|
|
|
|
|
2012-08-09 12:02:13 +02:00
|
|
|
if model.package_table.exists():
|
2012-06-25 20:27:18 +02:00
|
|
|
if not Table('geometry_columns',meta.metadata).exists() or \
|
|
|
|
not Table('spatial_ref_sys',meta.metadata).exists():
|
2012-02-07 17:50:46 +01:00
|
|
|
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
|
|
|
|
|
2015-03-27 16:57:55 +01:00
|
|
|
|
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)
|
|
|
|
|
2015-03-27 16:57:55 +01:00
|
|
|
package_extent_table = setup_spatial_table(PackageExtent, db_srid)
|