2011-04-11 19:04:28 +02:00
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
from pprint import pprint
|
|
|
|
|
|
|
|
from ckan.lib.cli import CkanCommand
|
|
|
|
from ckanext.spatial.lib import save_extent
|
|
|
|
|
2011-04-13 13:05:59 +02:00
|
|
|
class Spatial(CkanCommand):
|
|
|
|
'''Performs spatially related operations.
|
2011-04-11 19:04:28 +02:00
|
|
|
|
|
|
|
Usage:
|
2011-04-13 13:05:59 +02:00
|
|
|
spatial initdb [srid]
|
|
|
|
Creates the necessary tables. You must have PostGIS installed
|
|
|
|
and configured in the database.
|
|
|
|
You can provide the SRID of the geometry column. Default is 4258.
|
2011-04-11 19:04:28 +02:00
|
|
|
|
2011-04-13 13:05:59 +02:00
|
|
|
spatial extents
|
|
|
|
Creates or updates the extent geometry column for packages with
|
|
|
|
a bounding box defined in extras
|
|
|
|
|
2011-04-11 19:04:28 +02:00
|
|
|
The commands should be run from the ckanext-spatial directory and expect
|
|
|
|
a development.ini file to be present. Most of the time you will
|
|
|
|
specify the config explicitly though::
|
|
|
|
|
|
|
|
paster extents update --config=../ckan/development.ini
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
summary = __doc__.split('\n')[0]
|
|
|
|
usage = __doc__
|
2011-04-13 13:05:59 +02:00
|
|
|
max_args = 2
|
2011-04-11 19:04:28 +02:00
|
|
|
min_args = 0
|
|
|
|
|
|
|
|
def command(self):
|
|
|
|
self._load_config()
|
|
|
|
print ''
|
|
|
|
|
|
|
|
if len(self.args) == 0:
|
|
|
|
self.parser.print_usage()
|
|
|
|
sys.exit(1)
|
|
|
|
cmd = self.args[0]
|
2011-04-13 13:05:59 +02:00
|
|
|
if cmd == 'initdb':
|
|
|
|
self.initdb()
|
|
|
|
elif cmd == 'extents':
|
2011-04-11 19:04:28 +02:00
|
|
|
self.update_extents()
|
|
|
|
else:
|
|
|
|
print 'Command %s not recognized' % cmd
|
|
|
|
|
2011-04-13 13:05:59 +02:00
|
|
|
def initdb(self):
|
|
|
|
if len(self.args) >= 2:
|
|
|
|
srid = unicode(self.args[1])
|
|
|
|
else:
|
|
|
|
srid = None
|
|
|
|
|
|
|
|
from ckanext.spatial.model import setup as db_setup
|
|
|
|
|
|
|
|
db_setup(srid)
|
|
|
|
|
|
|
|
print 'DB tables created'
|
|
|
|
|
2011-04-11 19:04:28 +02:00
|
|
|
def update_extents(self):
|
|
|
|
from ckan.model import PackageExtra, Package, Session
|
|
|
|
conn = Session.connection()
|
|
|
|
packages = [extra.package \
|
|
|
|
for extra in \
|
|
|
|
Session.query(PackageExtra).filter(PackageExtra.key == 'bbox-east-long').all()]
|
|
|
|
|
|
|
|
error = False
|
|
|
|
for package in packages:
|
|
|
|
try:
|
|
|
|
save_extent(package)
|
|
|
|
except:
|
|
|
|
errors = True
|
|
|
|
|
|
|
|
if error:
|
|
|
|
msg = "There was an error saving the package extent. Have you set up the package_extent table in the DB?"
|
|
|
|
else:
|
|
|
|
msg = "Done. Extents generated for %i packages" % len(packages)
|
|
|
|
|
|
|
|
print msg
|
|
|
|
|