2011-04-11 19:04:28 +02:00
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
from pprint import pprint
|
2011-09-29 16:40:49 +02:00
|
|
|
import logging
|
2011-04-11 19:04:28 +02:00
|
|
|
|
|
|
|
from ckan.lib.cli import CkanCommand
|
2011-09-29 16:40:49 +02:00
|
|
|
from ckan.lib.helpers import json
|
|
|
|
from ckanext.spatial.lib import save_package_extent
|
|
|
|
log = logging.getLogger(__name__)
|
2011-04-11 19:04:28 +02:00
|
|
|
|
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.
|
2011-09-29 16:40:49 +02:00
|
|
|
You can provide the SRID of the geometry column. Default is 4326.
|
2011-04-11 19:04:28 +02:00
|
|
|
|
2011-04-13 13:05:59 +02:00
|
|
|
spatial extents
|
2011-10-03 14:39:18 +02:00
|
|
|
Creates or updates the extent geometry column for datasets with
|
2011-09-29 16:40:49 +02:00
|
|
|
an extent defined in the 'spatial' extra.
|
2011-04-13 13:05:59 +02:00
|
|
|
|
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 \
|
2011-09-29 16:40:49 +02:00
|
|
|
Session.query(PackageExtra).filter(PackageExtra.key == 'spatial').all()]
|
2011-04-11 19:04:28 +02:00
|
|
|
|
2011-09-29 16:40:49 +02:00
|
|
|
errors = []
|
|
|
|
count = 0
|
2011-04-11 19:04:28 +02:00
|
|
|
for package in packages:
|
|
|
|
try:
|
2011-09-29 16:40:49 +02:00
|
|
|
value = package.extras['spatial']
|
|
|
|
log.debug('Received: %r' % value)
|
|
|
|
geometry = json.loads(value)
|
|
|
|
|
|
|
|
count += 1
|
|
|
|
except ValueError,e:
|
|
|
|
errors.append(u'Package %s - Error decoding JSON object: %s' % (package.id,str(e)))
|
|
|
|
except TypeError,e:
|
|
|
|
errors.append(u'Package %s - Error decoding JSON object: %s' % (package.id,str(e)))
|
|
|
|
|
|
|
|
save_package_extent(package.id,geometry)
|
|
|
|
|
|
|
|
|
|
|
|
Session.commit()
|
|
|
|
|
|
|
|
if errors:
|
|
|
|
msg = 'Errors were found:\n%s' % '\n'.join(errors)
|
|
|
|
print msg
|
|
|
|
|
|
|
|
msg = "Done. Extents generated for %i out of %i packages" % (count,len(packages))
|
2011-04-11 19:04:28 +02:00
|
|
|
|
|
|
|
print msg
|
|
|
|
|