Example #1
0
    def check_spatial_extra(self,package):
        '''
        For a given package, looks at the spatial extent (as given in the
        extra "spatial" in GeoJSON format) and records it in PostGIS.
        '''
        if not package.id:
            log.warning('Couldn\'t store spatial extent because no id was provided for the package')
            return

        # TODO: deleted extra
        for extra in package.extras_list:
            if extra.key == 'spatial':
                if extra.state == 'active' and extra.value:
                    try:
                        log.debug('Received: %r' % extra.value)
                        geometry = json.loads(extra.value)
                    except ValueError,e:
                        error_dict = {'spatial':[u'Error decoding JSON object: %s' % str(e)]}
                        raise p.toolkit.ValidationError(error_dict, error_summary=package_error_summary(error_dict))
                    except TypeError,e:
                        error_dict = {'spatial':[u'Error decoding JSON object: %s' % str(e)]}
                        raise p.toolkit.ValidationError(error_dict, error_summary=package_error_summary(error_dict))

                    try:
                        save_package_extent(package.id,geometry)

                    except ValueError,e:
                        error_dict = {'spatial':[u'Error creating geometry: %s' % str(e)]}
                        raise p.toolkit.ValidationError(error_dict, error_summary=package_error_summary(error_dict))
                    except Exception, e:
                        raise
                        if bool(os.getenv('DEBUG')):
                            raise
                        error_dict = {'spatial':[u'Error: %s' % str(e)]}
                        raise p.toolkit.ValidationError(error_dict, error_summary=package_error_summary(error_dict))
Example #2
0
def update_extents():
    from ckan.model import PackageExtra, Package, Session
    conn = Session.connection()
    packages = [extra.package \
                for extra in \
                Session.query(PackageExtra).filter(PackageExtra.key == 'spatial').all()]

    errors = []
    count = 0
    for package in packages:
        try:
            value = package.extras['spatial']
            log.debug('Received: %r' % value)
            geometry = json.loads(value)

            count += 1
        except ValueError as e:
            errors.append(u'Package %s - Error decoding JSON object: %s' %
                          (package.id, six.text_type(e)))
        except TypeError as e:
            errors.append(u'Package %s - Error decoding JSON object: %s' %
                          (package.id, six.text_type(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))

    print(msg)
Example #3
0
    def check_spatial_extra(self,package):
        '''
        For a given package, looks at the spatial extent (as given in the
        extra "spatial" in GeoJSON format) and records it in PostGIS.
        '''
        if not package.id:
            log.warning('Couldn\'t store spatial extent because no id was provided for the package')
            return

        # TODO: deleted extra
        for extra in package.extras_list:
            if extra.key == 'spatial':
                if extra.state == 'active':
                    try:
                        log.debug('Received: %r' % extra.value)
                        geometry = json.loads(extra.value)
                    except ValueError,e:
                        error_dict = {'spatial':[u'Error decoding JSON object: %s' % str(e)]}
                        raise ValidationError(error_dict, error_summary=package_error_summary(error_dict))
                    except TypeError,e:
                        error_dict = {'spatial':[u'Error decoding JSON object: %s' % str(e)]}
                        raise ValidationError(error_dict, error_summary=package_error_summary(error_dict))

                    try:
                        save_package_extent(package.id,geometry)

                    except ValueError,e:
                        error_dict = {'spatial':[u'Error creating geometry: %s' % str(e)]}
                        raise ValidationError(error_dict, error_summary=package_error_summary(error_dict))
                    except Exception, e:
                        if bool(os.getenv('DEBUG')):
                            raise
                        error_dict = {'spatial':[u'Error: %s' % str(e)]}
                        raise ValidationError(error_dict, error_summary=package_error_summary(error_dict))
    def check_spatial_extra(self,package):
        if not package.id:
            log.warning('Couldn\'t store spatial extent because no id was provided for the package')
            return

        # TODO: deleted extra
        for extra in package.extras_list:
            if extra.key == 'spatial':
                if extra.state == 'active':
                    try:
                        log.debug('Received: %r' % extra.value)
                        geometry = json.loads(extra.value)
                    except ValueError,e:
                        error_dict = {'spatial':[u'Error decoding JSON object: %s' % str(e)]}
                        raise ValidationError(error_dict, error_summary=package_error_summary(error_dict))
                    except TypeError,e:
                        error_dict = {'spatial':[u'Error decoding JSON object: %s' % str(e)]}
                        raise ValidationError(error_dict, error_summary=package_error_summary(error_dict))

                    try:
                        save_package_extent(package.id,geometry)

                    except ValueError,e:
                        error_dict = {'spatial':[u'Error creating geometry: %s' % str(e)]}
                        raise ValidationError(error_dict, error_summary=package_error_summary(error_dict))
                    except Exception, e:
                        error_dict = {'spatial':[u'Error: %s' % str(e)]}
                        raise ValidationError(error_dict, error_summary=package_error_summary(error_dict))
Example #5
0
    def delete(self, package):
        '''

        :param package: 

        '''
        from ckanext.spatial.lib import save_package_extent
        save_package_extent(package.id, None)
Example #6
0
    def check_spatial_extra(self, package):
        if not package.id:
            log.warning(
                'Couldn\'t store spatial extent because no id was provided for the package'
            )
            return

        # TODO: deleted extra
        for extra in package.extras_list:
            if extra.key == 'spatial':
                if extra.state == 'active':
                    try:
                        log.debug('Received: %r' % extra.value)
                        geometry = json.loads(extra.value)
                    except ValueError, e:
                        error_dict = {
                            'spatial':
                            [u'Error decoding JSON object: %s' % str(e)]
                        }
                        raise ValidationError(
                            error_dict,
                            error_summary=package_error_summary(error_dict))
                    except TypeError, e:
                        error_dict = {
                            'spatial':
                            [u'Error decoding JSON object: %s' % str(e)]
                        }
                        raise ValidationError(
                            error_dict,
                            error_summary=package_error_summary(error_dict))

                    try:
                        save_package_extent(package.id, geometry)

                    except ValueError, e:
                        error_dict = {
                            'spatial':
                            [u'Error creating geometry: %s' % str(e)]
                        }
                        raise ValidationError(
                            error_dict,
                            error_summary=package_error_summary(error_dict))
                    except Exception, e:
                        error_dict = {'spatial': [u'Error: %s' % str(e)]}
                        raise ValidationError(
                            error_dict,
                            error_summary=package_error_summary(error_dict))
Example #7
0
    def check_spatial_extra(self, package):
        for extra in package.extras_list:
            if extra.key == "spatial":
                if extra.state == "active":
                    try:
                        log.debug("Received: %r" % extra.value)
                        geometry = json.loads(extra.value)
                    except ValueError, e:
                        error_dict = {"spatial": [u"Error decoding JSON object: %s" % str(e)]}
                        raise ValidationError(error_dict, error_summary=package_error_summary(error_dict))
                    except TypeError, e:
                        error_dict = {"spatial": [u"Error decoding JSON object: %s" % str(e)]}
                        raise ValidationError(error_dict, error_summary=package_error_summary(error_dict))

                    try:
                        save_package_extent(package.id, geometry)

                    except ValueError, e:
                        error_dict = {"spatial": [u"Error creating geometry: %s" % str(e)]}
                        raise ValidationError(error_dict, error_summary=package_error_summary(error_dict))
                    except Exception, e:
                        error_dict = {"spatial": [u"Error: %s" % str(e)]}
                        raise ValidationError(error_dict, error_summary=package_error_summary(error_dict))
Example #8
0
    def check_spatial_extra(self,package):
        '''
        For a given package, looks at the spatial extent (as given in the
        extra "spatial" in GeoJSON format) and records it in PostGIS.
        '''
        if not package.id:
            log.warning('Couldn\'t store spatial extent because no id was provided for the package')
            return

        # TODO: deleted extra
        for extra in package.extras_list:
            if spatial_helpers.spatial_is_spatial_key(extra.key):
                if extra.state == 'active' and extra.value:
                    if spatial_helpers.spatial_is_json(extra.value) and not spatial_helpers.spatial_is_geojson(json.loads(extra.value)):
                        error_dict = {spatial_helpers.spatial_get_spatial_key():[u'Error creating geometry: Context does not provide geo interface']}
                        raise p.toolkit.ValidationError(error_dict, error_summary=package_error_summary(error_dict))

                    try:
                        log.debug('Received: %r' % extra.value)
                        geometry = json.loads(extra.value)
                    except ValueError,e:
                        #Value is not GeoJson... delete extent
                        save_package_extent(package.id,None)
                        break
                        #error_dict = {spatial_helpers.spatial_get_spatial_key():[u'Error decoding JSON object: %s' % str(e)]}
                        #raise p.toolkit.ValidationError(error_dict, error_summary=package_error_summary(error_dict))
                    except TypeError,e:
                        #Value is not GeoJson... delete extent
                        save_package_extent(package.id,None)
                        break
                        #error_dict = {spatial_helpers.spatial_get_spatial_key():[u'Error decoding JSON object: %s' % str(e)]}
                        #raise p.toolkit.ValidationError(error_dict, error_summary=package_error_summary(error_dict))

                    try:
                        save_package_extent(package.id,geometry)

                    except ValueError,e:
                        error_dict = {spatial_helpers.spatial_get_spatial_key():[u'Error creating geometry: %s' % str(e)]}
                        raise p.toolkit.ValidationError(error_dict, error_summary=package_error_summary(error_dict))
                    except Exception, e:
                        if bool(os.getenv('DEBUG')):
                            raise
                        error_dict = {spatial_helpers.spatial_get_spatial_key():[u'Error: %s' % str(e)]}
                        raise p.toolkit.ValidationError(error_dict, error_summary=package_error_summary(error_dict))
Example #9
0
 def delete(self, package):
     from ckanext.spatial.lib import save_package_extent
     save_package_extent(package.id,None)
Example #10
0
                    try:
                        save_package_extent(package.id,geometry)

                    except ValueError,e:
                        error_dict = {'spatial':[u'Error creating geometry: %s' % str(e)]}
                        raise p.toolkit.ValidationError(error_dict, error_summary=package_error_summary(error_dict))
                    except Exception, e:
                        if bool(os.getenv('DEBUG')):
                            raise
                        error_dict = {'spatial':[u'Error: %s' % str(e)]}
                        raise p.toolkit.ValidationError(error_dict, error_summary=package_error_summary(error_dict))

                elif (extra.state == 'active' and not extra.value) or extra.state == 'deleted':
                    # Delete extent from table
                    save_package_extent(package.id,None)

                break


    def delete(self, package):
        from ckanext.spatial.lib import save_package_extent
        save_package_extent(package.id,None)

    ## ITemplateHelpers

    def get_helpers(self):
        from ckanext.spatial import helpers as spatial_helpers
        return {
                'get_reference_date' : spatial_helpers.get_reference_date,
                'get_responsible_party': spatial_helpers.get_responsible_party,
Example #11
0
 def delete(self, package):
     save_package_extent(package.id, None)
Example #12
0
                        error_dict = {
                            'spatial':
                            [u'Error creating geometry: %s' % str(e)]
                        }
                        raise ValidationError(
                            error_dict,
                            error_summary=package_error_summary(error_dict))
                    except Exception, e:
                        error_dict = {'spatial': [u'Error: %s' % str(e)]}
                        raise ValidationError(
                            error_dict,
                            error_summary=package_error_summary(error_dict))

                elif extra.state == 'deleted':
                    # Delete extent from table
                    save_package_extent(package.id, None)

                break

    def delete(self, package):
        save_package_extent(package.id, None)


class SpatialQuery(SingletonPlugin):

    implements(IRoutes, inherit=True)
    implements(IPackageController, inherit=True)

    def before_map(self, map):

        map.connect('api_spatial_query',
Example #13
0
 def delete(self, package):
     save_package_extent(package.id,None)
Example #14
0
class Spatial(CkanCommand):
    '''Performs spatially related operations.

    Usage:
        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 4326.

        spatial extents
            Creates or updates the extent geometry column for datasets with
            an extent defined in the 'spatial' extra.

        spatial wms-check <wms_url>
            Checks if a given URL responds like a WMS server does.

    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__
    max_args = 2
    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]
        if cmd == 'initdb':
            self.initdb()
        elif cmd == 'extents':
            self.update_extents()
        elif cmd == 'wms-check':
            self.wms_check()
        else:
            print 'Command %s not recognized' % cmd

    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'

    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 == 'spatial').all()]

        errors = []
        count = 0
        for package in packages:
            try:
                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)
Example #15
0
    def check_spatial_extra(self, package):
        '''
        For a given package, looks at the spatial extent (as given in the
        extra "spatial" in GeoJSON format) and records it in PostGIS.
        '''
        from ckanext.spatial.lib import save_package_extent

        if not package.id:
            log.warning(
                'Couldn\'t store spatial extent because no id was provided for the package'
            )
            return

        # TODO: deleted extra
        for extra in _get_package_extras(package.id):
            if extra.key != 'spatial':
                continue
            if extra.state == 'active' and extra.value:
                try:
                    log.debug('Received: %r' % extra.value)
                    geometry = json.loads(extra.value)
                except ValueError as e:
                    error_dict = {
                        'spatial':
                        [u'Error decoding JSON object: %s' % six.text_type(e)]
                    }
                    raise tk.ValidationError(
                        error_dict,
                        error_summary=package_error_summary(error_dict))
                except TypeError as e:
                    error_dict = {
                        'spatial':
                        [u'Error decoding JSON object: %s' % six.text_type(e)]
                    }
                    raise tk.ValidationError(
                        error_dict,
                        error_summary=package_error_summary(error_dict))

                try:
                    save_package_extent(package.id, geometry)

                except ValueError as e:
                    error_dict = {
                        'spatial':
                        [u'Error creating geometry: %s' % six.text_type(e)]
                    }
                    raise tk.ValidationError(
                        error_dict,
                        error_summary=package_error_summary(error_dict))
                except Exception as e:
                    if bool(os.getenv('DEBUG')):
                        raise
                    error_dict = {'spatial': [u'Error: %s' % six.text_type(e)]}
                    raise tk.ValidationError(
                        error_dict,
                        error_summary=package_error_summary(error_dict))

            elif (extra.state == 'active'
                  and not extra.value) or extra.state == 'deleted':
                # Delete extent from table
                save_package_extent(package.id, None)

            break