def write_record(result, esn, context, url=None): ''' Return csw:SearchResults child as lxml.etree.Element ''' typename = util.getqattr( result, context.md_core_model['mappings']['pycsw:Typename']) if esn == 'full' and typename == 'atom:entry': # dump record as is and exit return etree.fromstring( util.getqattr(result, context.md_core_model['mappings']['pycsw:XML']), context.parser) node = etree.Element(util.nspath_eval('atom:entry', NAMESPACES), nsmap=NAMESPACES) node.attrib[util.nspath_eval('xsi:schemaLocation', context.namespaces)] = \ '%s http://www.kbcafe.com/rss/atom.xsd.xml' % NAMESPACES['atom'] # author val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Creator']) if val: author = etree.SubElement(node, util.nspath_eval('atom:author', NAMESPACES)) etree.SubElement(author, util.nspath_eval('atom:name', NAMESPACES)).text = val # category val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Keywords']) if val: for kw in val.split(','): etree.SubElement(node, util.nspath_eval('atom:category', NAMESPACES), term=kw) for qval in ['pycsw:Contributor', 'pycsw:Identifier']: val = util.getqattr(result, context.md_core_model['mappings'][qval]) if val: etree.SubElement( node, util.nspath_eval(XPATH_MAPPINGS[qval], NAMESPACES)).text = val if qval == 'pycsw:Identifier': etree.SubElement( node, util.nspath_eval('dc:identifier', context.namespaces)).text = val rlinks = util.getqattr(result, context.md_core_model['mappings']['pycsw:Links']) if rlinks: for link in util.jsonify_links(rlinks): url2 = etree.SubElement(node, util.nspath_eval('atom:link', NAMESPACES), href=link['url']) if link['description']: url2.attrib['title'] = link['description'] if link['protocol']: if link['protocol'] == 'enclosure': url2.attrib['rel'] = link['protocol'] url2.attrib['type'] = 'application/octet-stream' else: url2.attrib['type'] = link['protocol'] etree.SubElement( node, util.nspath_eval('atom:link', NAMESPACES), href='%s?service=CSW&version=2.0.2&request=GetRepositoryItem&id=%s' % (url, util.getqattr(result, context.md_core_model['mappings']['pycsw:Identifier']))) # atom:title el = etree.SubElement( node, util.nspath_eval(XPATH_MAPPINGS['pycsw:Title'], NAMESPACES)) val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Title']) if val: el.text = val # atom:updated el = etree.SubElement( node, util.nspath_eval(XPATH_MAPPINGS['pycsw:Modified'], NAMESPACES)) val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Modified']) if val: el.text = val else: val = util.getqattr( result, context.md_core_model['mappings']['pycsw:InsertDate']) el.text = val for qval in [ 'pycsw:PublicationDate', 'pycsw:AccessConstraints', 'pycsw:Source', 'pycsw:Abstract' ]: val = util.getqattr(result, context.md_core_model['mappings'][qval]) if val: etree.SubElement( node, util.nspath_eval(XPATH_MAPPINGS[qval], NAMESPACES)).text = val # bbox extent val = util.getqattr(result, context.md_core_model['mappings']['pycsw:BoundingBox']) bboxel = write_extent(val, context.namespaces) if bboxel is not None: node.append(bboxel) return node
def record2json(record): """ OGC API - Records record generator from core pycsw record model :param record: pycsw record object :returns: `dict` of record GeoJSON """ record_dict = { 'id': record.identifier, 'type': 'Feature', 'geometry': None, 'properties': {} } record_dict['properties']['externalId'] = record.identifier record_dict['properties']['recordUpdated'] = record.insert_date if record.type: record_dict['properties']['type'] = record.type if record.date_creation: record_dict['properties']['created'] = record.date_creation if record.date_modified: record_dict['properties']['updated'] = record.date_modified if record.language: record_dict['properties']['language'] = record.language if record.title: record_dict['properties']['title'] = record.title if record.abstract: record_dict['properties']['description'] = record.abstract if record.format: record_dict['properties']['formats'] = [record.format] if record.keywords: record_dict['properties']['keywords'] = [ x for x in record.keywords.split(',') ] if record.links: record_dict['associations'] = [] for link in jsonify_links(record.links): association = { 'href': link['url'], 'name': link['name'], 'description': link['description'], 'type': link['protocol'] } if 'type' in link: association['rel'] = link['type'] record_dict['associations'].append(association) if record.wkt_geometry: minx, miny, maxx, maxy = wkt2geom(record.wkt_geometry) geometry = { 'type': 'Polygon', 'coordinates': [[[minx, miny], [minx, maxy], [maxx, maxy], [maxx, miny], [minx, miny]]] } record_dict['geometry'] = geometry record_dict['properties']['extents'] = { 'spatial': { 'bbox': [[minx, miny, maxx, maxy]], 'crs': 'http://www.opengis.net/def/crs/OGC/1.3/CRS84' } } return record_dict
def write_record(result, esn, context, url=None): ''' Return csw:SearchResults child as lxml.etree.Element ''' typename = util.getqattr( result, context.md_core_model['mappings']['pycsw:Typename']) if esn == 'full' and typename == 'dif:DIF': # dump record as is and exit return etree.fromstring( util.getqattr(result, context.md_core_model['mappings']['pycsw:XML']), context.parser) node = etree.Element(util.nspath_eval('dif:DIF', NAMESPACES)) node.attrib[util.nspath_eval('xsi:schemaLocation', context.namespaces)] = \ '%s http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/dif.xsd' % NAMESPACE # identifier etree.SubElement(node, util.nspath_eval( 'dif:Entry_ID', NAMESPACES)).text = util.getqattr( result, context.md_core_model['mappings']['pycsw:Identifier']) # title val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Title']) if not val: val = '' etree.SubElement(node, util.nspath_eval('dif:Entry_Title', NAMESPACES)).text = val # citation citation = etree.SubElement( node, util.nspath_eval('dif:Data_Set_Citation', NAMESPACES)) # creator val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Creator']) etree.SubElement(citation, util.nspath_eval('dif:Dataset_Creator', NAMESPACES)).text = val # date val = util.getqattr( result, context.md_core_model['mappings']['pycsw:PublicationDate']) etree.SubElement(citation, util.nspath_eval('dif:Dataset_Release_Date', NAMESPACES)).text = val # publisher val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Publisher']) etree.SubElement(citation, util.nspath_eval('dif:Dataset_Publisher', NAMESPACES)).text = val # format val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Format']) etree.SubElement( citation, util.nspath_eval('dif:Data_Presentation_Form', NAMESPACES)).text = val # iso topic category val = util.getqattr( result, context.md_core_model['mappings']['pycsw:TopicCategory']) etree.SubElement(node, util.nspath_eval('dif:ISO_Topic_Category', NAMESPACES)).text = val # keywords val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Keywords']) if val: for kw in val.split(','): etree.SubElement(node, util.nspath_eval('dif:Keyword', NAMESPACES)).text = kw # temporal temporal = etree.SubElement( node, util.nspath_eval('dif:Temporal_Coverage', NAMESPACES)) val = util.getqattr( result, context.md_core_model['mappings']['pycsw:TempExtent_begin']) val2 = util.getqattr( result, context.md_core_model['mappings']['pycsw:TempExtent_end']) etree.SubElement(temporal, util.nspath_eval('dif:Start_Date', NAMESPACES)).text = val etree.SubElement(temporal, util.nspath_eval('dif:End_Date', NAMESPACES)).text = val2 # bbox extent val = util.getqattr(result, context.md_core_model['mappings']['pycsw:BoundingBox']) bboxel = write_extent(val, NAMESPACES) if bboxel is not None: node.append(bboxel) # access constraints val = util.getqattr( result, context.md_core_model['mappings']['pycsw:AccessConstraints']) etree.SubElement(node, util.nspath_eval('dif:Access_Constraints', NAMESPACES)).text = val # language val = util.getqattr( result, context.md_core_model['mappings']['pycsw:ResourceLanguage']) etree.SubElement(node, util.nspath_eval('dif:Data_Set_Language', NAMESPACES)).text = val # contributor val = util.getqattr( result, context.md_core_model['mappings']['pycsw:OrganizationName']) etree.SubElement(node, util.nspath_eval('dif:Originating_Center', NAMESPACES)).text = val # abstract val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Abstract']) if not val: val = '' etree.SubElement(node, util.nspath_eval('dif:Summary', NAMESPACES)).text = val # date val = util.getqattr( result, context.md_core_model['mappings']['pycsw:CreationDate']) etree.SubElement(node, util.nspath_eval('dif:DIF_Creation_Date', NAMESPACES)).text = val # URL val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Relation']) url = etree.SubElement(node, util.nspath_eval('dif:Related_URL', NAMESPACES)) etree.SubElement(url, util.nspath_eval('dif:URL', NAMESPACES)).text = val rlinks = util.getqattr(result, context.md_core_model['mappings']['pycsw:Links']) if rlinks: for link in util.jsonify_links(rlinks): url2 = etree.SubElement( node, util.nspath_eval('dif:Related_URL', NAMESPACES)) urltype = etree.SubElement( url2, util.nspath_eval('dif:URL_Content_Type', NAMESPACES)) etree.SubElement(urltype, util.nspath_eval( 'dif:Type', NAMESPACES)).text = link['protocol'] etree.SubElement(url2, util.nspath_eval('dif:URL', NAMESPACES)).text = link['url'] etree.SubElement( url2, util.nspath_eval('dif:Description', NAMESPACES)).text = link['description'] etree.SubElement(node, util.nspath_eval('dif:Metadata_Name', NAMESPACES)).text = 'CEOS IDN DIF' etree.SubElement(node, util.nspath_eval('dif:Metadata_Version', NAMESPACES)).text = '9.7' return node
def write_record(result, esn, context, url=None): ''' Return csw:SearchResults child as lxml.etree.Element ''' typename = util.getqattr( result, context.md_core_model['mappings']['pycsw:Typename']) if typename == 'gm03:TRANSFER': # dump record as is and exit # TODO: provide brief and summary elementsetname's return etree.fromstring( util.getqattr(result, context.md_core_model['mappings']['pycsw:XML']), context.parser) node = etree.Element(util.nspath_eval('gm03:TRANSFER', NAMESPACES), nsmap=NAMESPACES) header = etree.SubElement( node, util.nspath_eval('gm03:HEADERSECTION', NAMESPACES)) header.attrib['version'] = '2.3' header.attrib['sender'] = 'pycsw' etree.SubElement(header, util.nspath_eval('gm03:MODELS', NAMESPACES)) data = etree.SubElement(node, util.nspath_eval('gm03:DATASECTION', NAMESPACES)) core = etree.SubElement( data, util.nspath_eval('gm03:GM03_2_1Core.Core', NAMESPACES)) core_meta = etree.SubElement( core, util.nspath_eval('gm03:GM03_2_1Core.Core.MD_Metadata', NAMESPACES)) val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Identifier']) etree.SubElement(core_meta, util.nspath_eval('gm03:fileIdentifier', NAMESPACES)).text = val language = util.getqattr( result, context.md_core_model['mappings']['pycsw:Language']) etree.SubElement(core_meta, util.nspath_eval('gm03:language', NAMESPACES)).text = language val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Modified']) etree.SubElement(core_meta, util.nspath_eval('gm03:dateStamp', NAMESPACES)).text = val hierarchy_level_val = util.getqattr( result, context.md_core_model['mappings']['pycsw:Type']) # metadata standard name standard = etree.SubElement( core_meta, util.nspath_eval('gm03:metadataStandardName', NAMESPACES)).text = 'GM03' # metadata standard version standardver = etree.SubElement( core_meta, util.nspath_eval('gm03:metadataStandardVersion', NAMESPACES)).text = '2.3' # hierarchy level hierarchy_level = etree.SubElement( core_meta, util.nspath_eval('gm03:hierarchyLevel', NAMESPACES)) scope_code = etree.SubElement( hierarchy_level, util.nspath_eval('gm03:GM03_2_1Core.Core.MD_ScopeCode_', NAMESPACES)) etree.SubElement(scope_code, util.nspath_eval('gm03:value', NAMESPACES)).text = hierarchy_level_val # parent identifier val = util.getqattr( result, context.md_core_model['mappings']['pycsw:ParentIdentifier']) parent_identifier = etree.SubElement( core_meta, util.nspath_eval('gm03:parentIdentifier', NAMESPACES)) scope_code = etree.SubElement( parent_identifier, util.nspath_eval('gm03:GM03_2_1Core.Core.MD_ScopeCode_', NAMESPACES)) etree.SubElement(scope_code, util.nspath_eval('gm03:value', NAMESPACES)).text = val # title val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Title']) citation = etree.SubElement( core, util.nspath_eval('gm03:GM03_2_1Core.Core.CI_Citation', NAMESPACES)) title = etree.SubElement(citation, util.nspath_eval('gm03:title', NAMESPACES)) title.append(_get_pt_freetext(val, language)) # abstract val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Abstract']) data_ident = etree.SubElement( core, util.nspath_eval('gm03:GM03_2_1Core.Core.MD_DataIdentification', NAMESPACES)) abstract = etree.SubElement(data_ident, util.nspath_eval('gm03:abstract', NAMESPACES)) abstract.append(_get_pt_freetext(val, language)) # resource language val = util.getqattr( result, context.md_core_model['mappings']['pycsw:ResourceLanguage']) if val: topicategory = etree.SubElement( data_ident, util.nspath_eval('gm03:language', NAMESPACES)) cat_code = etree.SubElement( topicategory, util.nspath_eval('gm03:CodeISO.LanguageCodeISO_', NAMESPACES)) etree.SubElement(cat_code, util.nspath_eval('gm03:value', NAMESPACES)).text = val # topic category val = util.getqattr( result, context.md_core_model['mappings']['pycsw:TopicCategory']) if val: topicategory = etree.SubElement( data_ident, util.nspath_eval('gm03:topicCategory', NAMESPACES)) cat_code = etree.SubElement( topicategory, util.nspath_eval('gm03:GM03_2_1Core.Core.MD_TopicCategoryCode_', NAMESPACES)) etree.SubElement(cat_code, util.nspath_eval('gm03:value', NAMESPACES)).text = val # keywords keywords_val = util.getqattr( result, context.md_core_model['mappings']['pycsw:Keywords']) if keywords_val: md_keywords = etree.SubElement( core, util.nspath_eval('gm03:GM03_2_1Core.Core.MD_Keywords', NAMESPACES)) val = util.getqattr( result, context.md_core_model['mappings']['pycsw:KeywordType']) if val: etree.SubElement(md_keywords, util.nspath_eval('gm03:type', NAMESPACES)).text = val keyword = etree.SubElement( md_keywords, util.nspath_eval('gm03:keyword', NAMESPACES)) for kw in keywords_val.split(','): keyword.append(_get_pt_freetext(kw, language)) # format val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Format']) if val: md_format = etree.SubElement( core, util.nspath_eval('gm03:GM03_2_1Core.Core.MD_Format', NAMESPACES)) etree.SubElement(md_format, util.nspath_eval('gm03:name', NAMESPACES)).text = val # creation date val = util.getqattr( result, context.md_core_model['mappings']['pycsw:CreationDate']) if val: ci_date = etree.SubElement( core, util.nspath_eval('gm03:GM03_2_1Core.Core.CI_Date', NAMESPACES)) etree.SubElement(ci_date, util.nspath_eval('gm03:date', NAMESPACES)).text = val etree.SubElement(ci_date, util.nspath_eval('gm03:dateType', NAMESPACES)).text = 'creation' # revision date val = util.getqattr( result, context.md_core_model['mappings']['pycsw:RevisionDate']) if val: ci_date = etree.SubElement( core, util.nspath_eval('gm03:GM03_2_1Core.Core.CI_Date', NAMESPACES)) etree.SubElement(ci_date, util.nspath_eval('gm03:date', NAMESPACES)).text = val etree.SubElement(ci_date, util.nspath_eval('gm03:dateType', NAMESPACES)).text = 'revision' # publication date val = util.getqattr( result, context.md_core_model['mappings']['pycsw:PublicationDate']) if val: ci_date = etree.SubElement( core, util.nspath_eval('gm03:GM03_2_1Core.Core.CI_Date', NAMESPACES)) etree.SubElement(ci_date, util.nspath_eval('gm03:date', NAMESPACES)).text = val etree.SubElement(ci_date, util.nspath_eval('gm03:dateType', NAMESPACES)).text = 'publication' # bbox extent val = util.getqattr(result, context.md_core_model['mappings']['pycsw:BoundingBox']) bboxel = write_extent(val, context.namespaces) if bboxel is not None: core.append(bboxel) # geographic description val = util.getqattr( result, context.md_core_model['mappings']['pycsw:GeographicDescriptionCode']) if val: geo_desc = etree.SubElement( core, util.nspath_eval('gm03:GM03_2_1Core.Core.EX_GeographicDescription', NAMESPACES)) etree.SubElement( geo_desc, util.nspath_eval('gm03:geographicIdentifier', NAMESPACES)).text = val # crs val = util.getqattr(result, context.md_core_model['mappings']['pycsw:CRS']) if val: rs_identifier = etree.SubElement( core, util.nspath_eval('gm03:GM03_2_1Core.Core.RS_Identifier', NAMESPACES)) rs_code = etree.SubElement(rs_identifier, util.nspath_eval('gm03:code', NAMESPACES)) rs_code.append(_get_pt_freetext(val, language)) # temporal extent time_begin = util.getqattr( result, context.md_core_model['mappings']['pycsw:TempExtent_begin']) time_end = util.getqattr( result, context.md_core_model['mappings']['pycsw:TempExtent_end']) if time_begin: temp_ext = etree.SubElement( core, util.nspath_eval('gm03:GM03_2_1Core.Core.EX_TemporalExtent', NAMESPACES)) extent = etree.SubElement(temp_ext, util.nspath_eval('gm03:extent', NAMESPACES)) tm_primitive = etree.SubElement( extent, util.nspath_eval('gm03:GM03_2_1Core.Core.TM_Primitive', NAMESPACES)) etree.SubElement(tm_primitive, util.nspath_eval('gm03:begin', NAMESPACES)).text = time_begin if time_end: etree.SubElement(tm_primitive, util.nspath_eval('gm03:end', NAMESPACES)).text = time_end # links rlinks = util.getqattr(result, context.md_core_model['mappings']['pycsw:Links']) if rlinks: for link in util.jsonify_links(rlinks): online_resource = etree.SubElement( core, util.nspath_eval('gm03:GM03_2_1Core.Core.OnlineResource', NAMESPACES)) if link['protocol']: etree.SubElement( online_resource, util.nspath_eval('gm03:protocol', NAMESPACES)).text = link['protocol'] if link['description']: desc = etree.SubElement( online_resource, util.nspath_eval('gm03:description', NAMESPACES)) desc.append(_get_pt_freetext(link['description'], language)) if link['name']: name_el = etree.SubElement( online_resource, util.nspath_eval('gm03:name', NAMESPACES)) name_el.append(_get_pt_freetext(link['name'], language)) linkage = etree.SubElement( online_resource, util.nspath_eval('gm03:linkage', NAMESPACES)) linkage.append(_get_pt_freeurl(link['url'], language)) return node
def write_record(result, esn, context, url=None): ''' Return csw:SearchResults child as lxml.etree.Element ''' typename = util.getqattr( result, context.md_core_model['mappings']['pycsw:Typename']) if esn == 'full' and typename == 'dif:DIF': # dump record as is and exit return etree.fromstring( util.getqattr(result, context.md_core_model['mappings']['pycsw:XML']), context.parser) node = etree.Element(util.nspath_eval('dif:DIF', NAMESPACES)) node.attrib[util.nspath_eval('xsi:schemaLocation', context.namespaces)] = \ '%s http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/dif.xsd' % NAMESPACE # identifier etree.SubElement(node, util.nspath_eval( 'dif:Entry_ID', NAMESPACES)).text = util.getqattr( result, context.md_core_model['mappings']['pycsw:Identifier']) # title val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Title']) if not val: val = '' etree.SubElement(node, util.nspath_eval('dif:Entry_Title', NAMESPACES)).text = val # citation citation = etree.SubElement( node, util.nspath_eval('dif:Data_Set_Citation', NAMESPACES)) # creator val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Creator']) etree.SubElement(citation, util.nspath_eval('dif:Dataset_Creator', NAMESPACES)).text = val # date val = util.getqattr( result, context.md_core_model['mappings']['pycsw:PublicationDate']) etree.SubElement(citation, util.nspath_eval('dif:Dataset_Release_Date', NAMESPACES)).text = val # publisher val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Publisher']) etree.SubElement(citation, util.nspath_eval('dif:Dataset_Publisher', NAMESPACES)).text = val # format val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Format']) etree.SubElement( citation, util.nspath_eval('dif:Data_Presentation_Form', NAMESPACES)).text = val # keywords dif:Parameters val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Keywords']) if val: kws = val.split(',') parameters_indexes = [] for index, kw in enumerate(kws): if "Earth Science".lower() in kw.lower() and len( kw.split(">")) >= 2: values = kw.upper().split(">") parameters = etree.SubElement(node, util.nspath_eval( 'dif:Parameters', NAMESPACES)) # .text = kw etree.SubElement( parameters, util.nspath_eval( 'dif:Category', NAMESPACES)).text = values[0].strip().upper() etree.SubElement( parameters, util.nspath_eval( 'dif:Topic', NAMESPACES)).text = values[1].strip().upper() etree.SubElement( parameters, util.nspath_eval( 'dif:Term', NAMESPACES)).text = values[2].strip().upper() for i, v in enumerate(values[3:]): etree.SubElement( parameters, util.nspath_eval(f'dif:Variable_Level_{i + 1}', NAMESPACES)).text = v.strip() parameters_indexes.append(index) # kws.pop(index) # iso topic category val = util.getqattr( result, context.md_core_model['mappings']['pycsw:TopicCategory']) etree.SubElement(node, util.nspath_eval('dif:ISO_Topic_Category', NAMESPACES)).text = val # keywords dif:keywords val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Keywords']) if val: kws = val.split(',') kws = [i for j, i in enumerate(kws) if j not in parameters_indexes] for index, kw in enumerate(kws): etree.SubElement(node, util.nspath_eval('dif:Keyword', NAMESPACES)).text = kw.strip() # temporal temporal = etree.SubElement( node, util.nspath_eval('dif:Temporal_Coverage', NAMESPACES)) val = util.getqattr( result, context.md_core_model['mappings']['pycsw:TempExtent_begin']) val2 = util.getqattr( result, context.md_core_model['mappings']['pycsw:TempExtent_end']) etree.SubElement(temporal, util.nspath_eval('dif:Start_Date', NAMESPACES)).text = val etree.SubElement(temporal, util.nspath_eval('dif:End_Date', NAMESPACES)).text = val2 # bbox extent val = util.getqattr(result, context.md_core_model['mappings']['pycsw:BoundingBox']) bboxel = write_extent(val, NAMESPACES) if bboxel is not None: node.append(bboxel) # access constraints val = util.getqattr( result, context.md_core_model['mappings']['pycsw:AccessConstraints']) etree.SubElement(node, util.nspath_eval('dif:Access_Constraints', NAMESPACES)).text = val # language val = util.getqattr( result, context.md_core_model['mappings']['pycsw:ResourceLanguage']) etree.SubElement(node, util.nspath_eval('dif:Data_Set_Language', NAMESPACES)).text = val # contributor val = util.getqattr( result, context.md_core_model['mappings']['pycsw:OrganizationName']) etree.SubElement(node, util.nspath_eval('dif:Originating_Center', NAMESPACES)).text = val # abstract val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Abstract']) if not val: val = '' etree.SubElement(node, util.nspath_eval('dif:Summary', NAMESPACES)).text = val # URL val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Relation']) if val: url = etree.SubElement(node, util.nspath_eval('dif:Related_URL', NAMESPACES)) etree.SubElement(url, util.nspath_eval('dif:URL', NAMESPACES)).text = val rlinks = util.getqattr(result, context.md_core_model['mappings']['pycsw:Links']) if rlinks: for link in util.jsonify_links(rlinks): url2 = etree.SubElement( node, util.nspath_eval('dif:Related_URL', NAMESPACES)) urltype = etree.SubElement( url2, util.nspath_eval('dif:URL_Content_Type', NAMESPACES)) if link['protocol'] == 'download': etree.SubElement(urltype, util.nspath_eval( 'dif:Type', NAMESPACES)).text = 'GET DATA' elif link['protocol'] == 'OPENDAP:OPENDAP': etree.SubElement(urltype, util.nspath_eval( 'dif:Type', NAMESPACES)).text = 'GET DATA' etree.SubElement(urltype, util.nspath_eval( 'dif:Subtype', NAMESPACES)).text = 'OPENDAP DATA (DODS)' elif link['protocol'] == 'OGC:WMS': etree.SubElement( urltype, util.nspath_eval('dif:Type', NAMESPACES)).text = 'GET SERVICE' etree.SubElement( urltype, util.nspath_eval( 'dif:Subtype', NAMESPACES)).text = 'GET WEB MAP SERVICE (WMS)' else: etree.SubElement(urltype, util.nspath_eval( 'dif:Type', NAMESPACES)).text = 'GET DATA' etree.SubElement(url2, util.nspath_eval('dif:URL', NAMESPACES)).text = link['url'] if link['description']: etree.SubElement( url2, util.nspath_eval('dif:Description', NAMESPACES)).text = link['description'] val = util.getqattr(result, context.md_core_model['mappings']['pycsw:Source']) if val: url2 = etree.SubElement( node, util.nspath_eval('dif:Related_URL', NAMESPACES)) urltype = etree.SubElement( url2, util.nspath_eval('dif:URL_Content_Type', NAMESPACES)) etree.SubElement(urltype, util.nspath_eval( 'dif:Type', NAMESPACES)).text = 'DATASET LANDING PAGE' etree.SubElement(url2, util.nspath_eval('dif:URL', NAMESPACES)).text = val etree.SubElement(node, util.nspath_eval('dif:Metadata_Name', NAMESPACES)).text = 'CEOS IDN DIF' etree.SubElement(node, util.nspath_eval('dif:Metadata_Version', NAMESPACES)).text = '9.7' # date val = util.getqattr( result, context.md_core_model['mappings']['pycsw:CreationDate']) etree.SubElement(node, util.nspath_eval('dif:DIF_Creation_Date', NAMESPACES)).text = val return node
def test_jsonify_links(linkstr, expected): result = util.jsonify_links(linkstr) assert isinstance(result, list) assert result == expected
def record2json(record, stac_item=False): """ OGC API - Records record generator from core pycsw record model :param record: pycsw record object :returns: `dict` of record GeoJSON """ if record.metadata_type == 'application/json': LOGGER.debug('Returning native JSON representation') return json.loads(record.metadata) record_dict = { 'id': record.identifier, 'type': 'Feature', 'geometry': None, 'properties': { 'externalId': [{'value': record.identifier}], 'datetime': record.date, 'start_datetime': record.time_begin, 'end_datetime': record.time_end }, 'links': [], 'assets': {} } if stac_item: record_dict['stac_version'] = '1.0.0' record_dict['collection'] = 'metadata:main' record_dict['properties']['externalId'] = record.identifier record_dict['properties']['recordUpdated'] = record.insert_date if record.type: record_dict['properties']['type'] = record.type if record.date_creation: record_dict['properties']['created'] = record.date_creation if record.date_modified: record_dict['properties']['updated'] = record.date_modified if record.language: record_dict['properties']['language'] = record.language if record.title: record_dict['properties']['title'] = record.title if record.abstract: record_dict['properties']['description'] = record.abstract if record.format: record_dict['properties']['formats'] = [record.format] if record.keywords: record_dict['properties']['keywords'] = [x for x in record.keywords.split(',')] if record.links: rdl = record_dict['links'] for link in jsonify_links(record.links): link = { 'href': link['url'], 'name': link['name'], 'description': link['description'], 'type': link['protocol'] } if 'type' in link: link['rel'] = link['type'] rdl.append(link) if record.wkt_geometry: minx, miny, maxx, maxy = wkt2geom(record.wkt_geometry) geometry = { 'type': 'Polygon', 'coordinates': [[ [minx, miny], [minx, maxy], [maxx, maxy], [maxx, miny], [minx, miny] ]] } record_dict['geometry'] = geometry record_dict['properties']['extent'] = { 'spatial': { 'bbox': [[minx, miny, maxx, maxy]], 'crs': 'http://www.opengis.net/def/crs/OGC/1.3/CRS84' } } return record_dict
def write_record(recobj, esn, context, url=None): ''' Return csw:SearchResults child as lxml.etree.Element ''' typename = util.getqattr( recobj, context.md_core_model['mappings']['pycsw:Typename']) if esn == 'full' and typename == 'fgdc:metadata': # dump record as is and exit return etree.fromstring( util.getqattr(recobj, context.md_core_model['mappings']['pycsw:XML']), context.parser) node = etree.Element('metadata') node.attrib[util.nspath_eval('xsi:noNamespaceSchemaLocation', context.namespaces)] = \ 'http://www.fgdc.gov/metadata/fgdc-std-001-1998.xsd' idinfo = etree.SubElement(node, 'idinfo') # identifier etree.SubElement(idinfo, 'datasetid').text = util.getqattr( recobj, context.md_core_model['mappings']['pycsw:Identifier']) citation = etree.SubElement(idinfo, 'citation') citeinfo = etree.SubElement(citation, 'citeinfo') # title val = util.getqattr(recobj, context.md_core_model['mappings']['pycsw:Title']) etree.SubElement(citeinfo, 'title').text = val # publisher publinfo = etree.SubElement(citeinfo, 'publinfo') val = util.getqattr( recobj, context.md_core_model['mappings']['pycsw:Publisher']) or '' etree.SubElement(publinfo, 'publish').text = val # origin val = util.getqattr( recobj, context.md_core_model['mappings']['pycsw:Creator']) or '' etree.SubElement(citeinfo, 'origin').text = val # keywords val = util.getqattr(recobj, context.md_core_model['mappings']['pycsw:Keywords']) if val: keywords = etree.SubElement(idinfo, 'keywords') theme = etree.SubElement(keywords, 'theme') for v in val.split(','): etree.SubElement(theme, 'themekey').text = v # accessconstraints val = util.getqattr( recobj, context.md_core_model['mappings']['pycsw:AccessConstraints']) or '' etree.SubElement(idinfo, 'accconst').text = val # abstract descript = etree.SubElement(idinfo, 'descript') val = util.getqattr( recobj, context.md_core_model['mappings']['pycsw:Abstract']) or '' etree.SubElement(descript, 'abstract').text = val # time datebegin = util.getqattr( recobj, context.md_core_model['mappings']['pycsw:TempExtent_begin']) dateend = util.getqattr( recobj, context.md_core_model['mappings']['pycsw:TempExtent_end']) if all([datebegin, dateend]): timeperd = etree.SubElement(idinfo, 'timeperd') timeinfo = etree.SubElement(timeperd, 'timeinfo') rngdates = etree.SubElement(timeinfo, 'timeinfo') begdate = etree.SubElement(rngdates, 'begdate').text = datebegin enddate = etree.SubElement(rngdates, 'enddate').text = dateend # bbox extent val = util.getqattr(recobj, context.md_core_model['mappings']['pycsw:BoundingBox']) bboxel = write_extent(val) if bboxel is not None: idinfo.append(bboxel) # contributor val = util.getqattr( recobj, context.md_core_model['mappings']['pycsw:Contributor']) or '' etree.SubElement(idinfo, 'datacred').text = val # direct spdoinfo = etree.SubElement(idinfo, 'spdoinfo') val = util.getqattr(recobj, context.md_core_model['mappings']['pycsw:Type']) or '' etree.SubElement(spdoinfo, 'direct').text = val # formname distinfo = etree.SubElement(node, 'distinfo') stdorder = etree.SubElement(distinfo, 'stdorder') digform = etree.SubElement(stdorder, 'digform') digtinfo = etree.SubElement(digform, 'digtinfo') val = util.getqattr( recobj, context.md_core_model['mappings']['pycsw:Format']) or '' etree.SubElement(digtinfo, 'formname').text = val etree.SubElement(citeinfo, 'geoform').text = val # source lineage = etree.SubElement(node, 'lineage') srcinfo = etree.SubElement(lineage, 'srcinfo') srccite = etree.SubElement(srcinfo, 'srccite') sciteinfo = etree.SubElement(srccite, 'citeinfo') val = util.getqattr( recobj, context.md_core_model['mappings']['pycsw:Source']) or '' etree.SubElement(sciteinfo, 'title').text = val val = util.getqattr( recobj, context.md_core_model['mappings']['pycsw:Relation']) or '' etree.SubElement(citeinfo, 'onlink').text = val # links rlinks = util.getqattr(recobj, context.md_core_model['mappings']['pycsw:Links']) if rlinks: for link in util.jsonify_links(rlinks): etree.SubElement(citeinfo, 'onlink', type=linkset[2]).text = link['url'] # metd metainfo = etree.SubElement(node, 'metainfo') val = util.getqattr( recobj, context.md_core_model['mappings']['pycsw:Modified']) or '' etree.SubElement(metainfo, 'metd').text = val return node