2019-12-11 13:22:28 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-12-11 13:23:03 +01:00
|
|
|
from __future__ import print_function
|
|
|
|
from builtins import str
|
2019-12-11 13:22:28 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import logging
|
|
|
|
from ckan.lib.helpers import json
|
|
|
|
from lxml import etree
|
|
|
|
from pprint import pprint
|
|
|
|
|
|
|
|
from ckanext.spatial.lib import save_package_extent
|
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def report(pkg=None):
|
|
|
|
from ckan import model
|
|
|
|
from ckanext.harvest.model import HarvestObject
|
|
|
|
from ckanext.spatial.lib.reports import validation_report
|
|
|
|
|
|
|
|
if pkg:
|
2019-12-11 13:23:03 +01:00
|
|
|
package_ref = str(pkg)
|
2019-12-11 13:22:28 +01:00
|
|
|
pkg = model.Package.get(package_ref)
|
|
|
|
if not pkg:
|
2019-12-11 13:23:03 +01:00
|
|
|
print('Package ref "%s" not recognised' % package_ref)
|
2019-12-11 13:22:28 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
report = validation_report(package_id=pkg.id)
|
|
|
|
for row in report.get_rows_html_formatted():
|
2019-12-11 13:23:03 +01:00
|
|
|
print()
|
2019-12-11 13:22:28 +01:00
|
|
|
for i, col_name in enumerate(report.column_names):
|
2019-12-11 13:23:03 +01:00
|
|
|
print(' %s: %s' % (col_name, row[i]))
|
2019-12-11 13:22:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
def validate_file(metadata_filepath):
|
|
|
|
from ckanext.spatial.harvesters import SpatialHarvester
|
|
|
|
from ckanext.spatial.model import ISODocument
|
|
|
|
|
|
|
|
if not os.path.exists(metadata_filepath):
|
2019-12-11 13:23:03 +01:00
|
|
|
print('Filepath %s not found' % metadata_filepath)
|
2019-12-11 13:22:28 +01:00
|
|
|
sys.exit(1)
|
|
|
|
with open(metadata_filepath, 'rb') as f:
|
|
|
|
metadata_xml = f.read()
|
|
|
|
|
|
|
|
validators = SpatialHarvester()._get_validator()
|
2019-12-11 13:23:03 +01:00
|
|
|
print('Validators: %r' % validators.profiles)
|
2019-12-11 13:22:28 +01:00
|
|
|
try:
|
|
|
|
xml_string = metadata_xml.encode("utf-8")
|
2019-12-11 13:23:03 +01:00
|
|
|
except UnicodeDecodeError as e:
|
|
|
|
print('ERROR: Unicode Error reading file \'%s\': %s' % \
|
|
|
|
(metadata_filepath, e))
|
2019-12-11 13:22:28 +01:00
|
|
|
sys.exit(1)
|
|
|
|
#import pdb; pdb.set_trace()
|
|
|
|
xml = etree.fromstring(xml_string)
|
|
|
|
|
|
|
|
# XML validation
|
|
|
|
valid, errors = validators.is_valid(xml)
|
|
|
|
|
|
|
|
# CKAN read of values
|
|
|
|
if valid:
|
|
|
|
try:
|
|
|
|
iso_document = ISODocument(xml_string)
|
|
|
|
iso_values = iso_document.read_values()
|
2019-12-11 13:23:03 +01:00
|
|
|
except Exception as e:
|
2019-12-11 13:22:28 +01:00
|
|
|
valid = False
|
|
|
|
errors.append(
|
|
|
|
'CKAN exception reading values from ISODocument: %s' % e)
|
|
|
|
|
2019-12-11 13:23:03 +01:00
|
|
|
print('***************')
|
|
|
|
print('Summary')
|
|
|
|
print('***************')
|
|
|
|
print('File: \'%s\'' % metadata_filepath)
|
|
|
|
print('Valid: %s' % valid)
|
2019-12-11 13:22:28 +01:00
|
|
|
if not valid:
|
2019-12-11 13:23:03 +01:00
|
|
|
print('Errors:')
|
|
|
|
print(pprint(errors))
|
|
|
|
print('***************')
|
2019-12-11 13:22:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
def report_csv(csv_filepath):
|
|
|
|
from ckanext.spatial.lib.reports import validation_report
|
|
|
|
report = validation_report()
|
|
|
|
with open(csv_filepath, 'wb') as f:
|
|
|
|
f.write(report.get_csv())
|
|
|
|
|
|
|
|
|
|
|
|
def initdb(srid=None):
|
|
|
|
if srid:
|
2019-12-11 13:23:03 +01:00
|
|
|
srid = str(srid)
|
2019-12-11 13:22:28 +01:00
|
|
|
|
|
|
|
from ckanext.spatial.model import setup as db_setup
|
|
|
|
|
|
|
|
db_setup(srid)
|
|
|
|
|
2019-12-11 13:23:03 +01:00
|
|
|
print('DB tables created')
|
2019-12-11 13:22:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2019-12-11 13:23:03 +01:00
|
|
|
except ValueError as e:
|
2019-12-11 13:22:28 +01:00
|
|
|
errors.append(u'Package %s - Error decoding JSON object: %s' %
|
|
|
|
(package.id, str(e)))
|
2019-12-11 13:23:03 +01:00
|
|
|
except TypeError as e:
|
2019-12-11 13:22:28 +01:00
|
|
|
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)
|
2019-12-11 13:23:03 +01:00
|
|
|
print(msg)
|
2019-12-11 13:22:28 +01:00
|
|
|
|
|
|
|
msg = "Done. Extents generated for %i out of %i packages" % (count,
|
|
|
|
len(packages))
|
|
|
|
|
2019-12-11 13:23:03 +01:00
|
|
|
print(msg)
|