Exemplo n.º 1
0
def create_layer_from_metadata_xml(resourcetype,
                                   xml,
                                   monitor=False,
                                   service=None,
                                   catalog=None):
    """
    Create a layer / keyword list from a metadata record if it does not already exist.
    """
    from .models import gen_anytext, Layer

    if resourcetype == 'http://www.opengis.net/cat/csw/2.0.2':  # Dublin core
        md = CswRecord(etree.fromstring(xml))

    layer = Layer(is_monitored=monitor,
                  name=md.title,
                  title=md.title,
                  abstract=md.abstract,
                  xml=xml,
                  service=service,
                  catalog=catalog,
                  anytext=gen_anytext(md.title, md.abstract, md.subjects))

    if hasattr(md, 'alternative'):
        layer.name = md.alternative

    if md.bbox is not None:
        layer.bbox_x0 = format_float(md.bbox.minx)
        layer.bbox_y0 = format_float(md.bbox.miny)
        layer.bbox_x1 = format_float(md.bbox.maxx)
        layer.bbox_y1 = format_float(md.bbox.maxy)

        layer.wkt_geometry = bbox2wktpolygon(
            [md.bbox.minx, md.bbox.miny, md.bbox.maxx, md.bbox.maxy])

    return layer, md.subjects
Exemplo n.º 2
0
    def test_layer_fields(self):
        """test Layer metadata fields"""
        layer = Layer.objects.filter(type='OGC:WMS').all()[0]
        self.assertIsNotNone(layer.xml, 'Expected XML document')

        xml = etree.fromstring(layer.xml)
        csw_record = CswRecord(xml)

        self.assertIsInstance(xml, etree._Element, 'Expected lxml instance')
        self.assertEqual(layer.title, csw_record.title,
                         'Expected title equality')
        self.assertEqual(layer.abstract, csw_record.abstract,
                         'Expected abstract equality')
        self.assertEqual(layer.csw_type, 'dataset',
                         'Expected CSW type equality')
        self.assertEqual(layer.csw_typename, 'csw:Record',
                         'Expected CSW typename equality')
        self.assertEqual(layer.csw_schema,
                         'http://www.opengis.net/cat/csw/2.0.2',
                         'Expected CSW schema equality')

        self.assertEqual(str(layer.service.uuid), csw_record.relation,
                         'Expected relation equality')
        self.assertEqual(layer.url, csw_record.source,
                         'Expected URL/source equality')

        anytext = gen_anytext(layer.title, layer.abstract,
                              list(layer.keywords.names()).sort())
        self.assertEqual(
            anytext, layer.anytext,
            'Expected anytext equality: %s != %s' % (anytext, layer.anytext))
Exemplo n.º 3
0
    def test_service_fields(self):
        """test Service metadata fields"""
        layer = Service.objects.filter(type='OGC:WMS').all()[0]
        self.assertIsNotNone(layer.xml, 'Expected XML document')

        xml = etree.fromstring(layer.xml)
        csw_record = CswRecord(xml)

        self.assertIsInstance(xml, etree._Element, 'Expected lxml instance')
        self.assertEqual(layer.title, csw_record.title, 'Expected title equality')
        self.assertEqual(layer.abstract, csw_record.abstract, 'Expected abstract equality')
        self.assertEqual(layer.csw_type, 'service', 'Expected CSW type equality')
        self.assertEqual(layer.csw_typename, 'csw:Record', 'Expected CSW typename equality')
        self.assertEqual(layer.csw_schema, 'http://www.opengis.net/cat/csw/2.0.2', 'Expected CSW schema equality')

        anytext = gen_anytext(layer.title, layer.abstract)
        self.assertEqual(anytext, layer.anytext, 'Expected anytext equality')
Exemplo n.º 4
0
def dc2dict(exml):
    """generate dict of properties from csw:Record"""

    vals = {}
    keywords = []

    mdata = CswRecord(exml)
    vals['language'] = mdata.language
    vals['spatial_representation_type'] = mdata.type
    keywords = mdata.subjects
    vals['temporal_extent_start'] = mdata.temporal
    vals['temporal_extent_end'] = mdata.temporal
    vals['constraints_other'] = mdata.license
    vals['date'] = sniff_date(mdata.modified)
    vals['title'] = mdata.title
    vals['abstract'] = mdata.abstract

    return [vals, keywords]
Exemplo n.º 5
0
def _parse_dc(context, repos, exml):

    from owslib.csw import CswRecord

    recobj = repos.dataset()
    links = []

    md = CswRecord(exml)

    if md.bbox is None:
        bbox = None
    else:
        bbox = md.bbox

    _set(context, recobj, 'pycsw:Identifier', md.identifier)
    _set(context, recobj, 'pycsw:Typename', 'csw:Record')
    _set(context, recobj, 'pycsw:Schema', context.namespaces['csw'])
    _set(context, recobj, 'pycsw:MdSource', 'local')
    _set(context, recobj, 'pycsw:InsertDate', util.get_today_and_now())
    _set(context, recobj, 'pycsw:XML', md.xml)
    _set(context, recobj, 'pycsw:AnyText', util.get_anytext(exml))
    _set(context, recobj, 'pycsw:Language', md.language)
    _set(context, recobj, 'pycsw:Type', md.type)
    _set(context, recobj, 'pycsw:Title', md.title)
    _set(context, recobj, 'pycsw:AlternateTitle', md.alternative)
    _set(context, recobj, 'pycsw:Abstract', md.abstract)

    if len(md.subjects) > 0 and None not in md.subjects:
        _set(context, recobj, 'pycsw:Keywords', ','.join(md.subjects))

    _set(context, recobj, 'pycsw:ParentIdentifier', md.ispartof)
    _set(context, recobj, 'pycsw:Relation', md.relation)
    _set(context, recobj, 'pycsw:TempExtent_begin', md.temporal)
    _set(context, recobj, 'pycsw:TempExtent_end', md.temporal)
    _set(context, recobj, 'pycsw:ResourceLanguage', md.language)
    _set(context, recobj, 'pycsw:Creator', md.creator)
    _set(context, recobj, 'pycsw:Publisher', md.publisher)
    _set(context, recobj, 'pycsw:Contributor', md.contributor)
    _set(context, recobj, 'pycsw:OrganizationName', md.rightsholder)
    _set(context, recobj, 'pycsw:AccessConstraints', md.accessrights)
    _set(context, recobj, 'pycsw:OtherConstraints', md.license)
    _set(context, recobj, 'pycsw:Date', md.date)
    _set(context, recobj, 'pycsw:CreationDate', md.created)
    _set(context, recobj, 'pycsw:PublicationDate', md.issued)
    _set(context, recobj, 'pycsw:Modified', md.modified)
    _set(context, recobj, 'pycsw:Format', md.format)
    _set(context, recobj, 'pycsw:Source', md.source)

    for ref in md.references:
        tmp = ',,%s,%s' % (ref['scheme'], ref['url'])
        links.append(tmp)
    for uri in md.uris:
        tmp = '%s,%s,%s,%s' % \
        (uri['name'], uri['description'], uri['protocol'], uri['url'])
        links.append(tmp)

    if len(links) > 0:
        _set(context, recobj, 'pycsw:Links', '^'.join(links))

    if bbox is not None:
        try:
            tmp = '%s,%s,%s,%s' % (bbox.minx, bbox.miny, bbox.maxx, bbox.maxy)
            _set(context, recobj, 'pycsw:BoundingBox',
                 util.bbox2wktpolygon(tmp))
        except:  # coordinates are corrupted, do not include
            _set(context, recobj, 'pycsw:BoundingBox', None)
    else:
        _set(context, recobj, 'pycsw:BoundingBox', None)

    return recobj