def _getResponsibleParty(self, node, ns): # TODO: manage mutiplicities (e.g. online resource, phone, etc) individualName = getTextFromXMLNode( node, './gmd:individualName/gco:CharacterString/', ns) organisationName = getTextFromXMLNode( node, './gmd:organisationName/gco:CharacterString/', ns) roleNode = getXMLNode(node, './gmd:role/gco:CharacterString/', ns) role = getXMLCodeText(roleNode, ns=ns) email = getTextFromXMLNode( node, './gmd:contactInfo/gmd:CI_Contact/gmd:address/gmd:CI_Address/gmd:electronicMailAddress/gco:CharacterString/', ns) phone = getTextFromXMLNode( node, './gmd:contactInfo/gmd:CI_Contact/gmd:phone/gmd:CI_Telephone/gmd:voice/gco:CharacterString/', ns) url = getTextFromXMLNode( node, './gmd:contactInfo/gmd:CI_Contact/gmd:onlineResource/gmd:CI_OnlineResource/gmd:linkage/gmd:URL/', ns) onlineResources = [] for onlineResourceNode in node.findall( './gmd:contactInfo/gmd:CI_Contact/gmd:onlineResource/gmd:CI_OnlineResource' ): onlineResource = self._getOnlineResource(onlineResourceNode, ns) onlineResources.append(onlineResource) return { 'individualName': individualName, 'organisationName': organisationName, 'role': role, 'email': email, 'phone': phone, 'url': url }
def _getOnlineResource(self, node, ns): url = getTextFromXMLNode(node, './gmd:linkage/gmd:URL', ns) protocol = getTextFromXMLNode(node, './gmd:protocol/gco:CharacterString', ns) name = getTextFromXMLNode(node, './gmd:name/gco:CharacterString', ns) description = getTextFromXMLNode(node, './gmd:description/gco:CharacterString', ns) applicationProfile = getTextFromXMLNode(node, './gmd:applicationProfile/gco:CharacterString', ns) function = getTextFromXMLNode(node, './gmd:function/gco:CharacterString', ns) return { 'name': sanitizeXmlText(name), 'description': sanitizeXmlText(description), 'applicationProfile': sanitizeXmlText(applicationProfile), 'function': sanitizeXmlText(function), 'protocol': sanitizeXmlText(protocol), 'url': sanitizeXmlText(url) }
def gn_get_metadata(self, metadata_id): #curl -X DELETE --header 'Accept: */*' 'http://localhost:8080/geonetwork/srv/api/0.1/records/97769e85-2e7b-418b-a8c8-0163bfb97aac?withBackup=false' url = self.service_url + "/srv/api/0.1/records/" + str( metadata_id) + "" print("Getting metadata from uuid:" + str(metadata_id) + " -> " + url) headers = { 'Accept': 'application/xml', 'X-XSRF-TOKEN': self.get_csrf_token() } r = self.session.get(url, headers=headers, timeout=get_default_timeout(), proxies=settings.PROXIES) if r.status_code == 200: try: tree = ET.fromstring(r.content) logger.debug(r.content) ns = { 'gmd': 'http://www.isotc211.org/2005/gmd', 'gco': 'http://www.isotc211.org/2005/gco' } metadata_id = getTextFromXMLNode(tree, './gmd:fileIdentifier/', ns) title = getTextFromXMLNode( tree, './gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:title/', ns) abstract = getTextFromXMLNode( tree, './gmd:identificationInfo/gmd:MD_DataIdentification/gmd:abstract/', ns) publish_date = getTextFromXMLNode( tree, './gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:date/gmd:CI_Date/gmd:date/', ns) period_start = '' period_end = '' aux = tree.findall( './gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:temporalElement/gmd:EX_TemporalExtent/gmd:extent/', ns) if len(aux) > 0 and len(aux[0]) == 2: period_start = aux[0][0].text period_end = aux[0][1].text categories = [] for category in tree.findall( './gmd:identificationInfo/gmd:MD_DataIdentification/gmd:topicCategory/gmd:MD_TopicCategoryCode/', ns): categories.append(sanitizeXmlText(category.text)) keywords = [] for keyword in tree.findall( './gmd:identificationInfo/gmd:MD_DataIdentification/gmd:descriptiveKeywords/gmd:MD_Keywords/gmd:keyword/', ns): keywords.append(sanitizeXmlText(keyword.text)) representation_type = '' aux = tree.findall( './gmd:identificationInfo/gmd:MD_DataIdentification/gmd:spatialRepresentationType/', ns) if len(aux) > 0: representation_type = aux[0].attrib['codeListValue'] scale = getTextFromXMLNode( tree, './gmd:identificationInfo/gmd:MD_DataIdentification/gmd:spatialResolution/gmd:MD_Resolution/gmd:equivalentScale/gmd:MD_RepresentativeFraction/gmd:denominator/', ns) srs = getTextFromXMLNode( tree, './gmd:referenceSystemInfo/gmd:MD_ReferenceSystem/gmd:referenceSystemIdentifier/gmd:RS_Identifier/gmd:code/', ns) #https://test.gvsigonline.com/geonetwork/srv/spa/region.getmap.png?mapsrs=EPSG:3857&width=250&background=settings&geomsrs=EPSG:4326&geom=Polygon((-18.1595005217%2043.9729489023,4.96320908311%2043.9729489023,4.96320908311%2025.9993588695,-18.1595005217%2025.9993588695,-18.1595005217%2043.9729489023)) coords_w = getTextFromXMLNode( tree, './gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_GeographicBoundingBox/gmd:westBoundLongitude/', ns) coords_e = getTextFromXMLNode( tree, './gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_GeographicBoundingBox/gmd:eastBoundLongitude/', ns) coords_s = getTextFromXMLNode( tree, './gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_GeographicBoundingBox/gmd:southBoundLatitude/', ns) coords_n = getTextFromXMLNode( tree, './gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_GeographicBoundingBox/gmd:northBoundLatitude/', ns) image_url = self.service_url + '/srv/spa/region.getmap.png?mapsrs=EPSG:3857&width=250&background=osm&geomsrs=EPSG:4326&geom=Polygon((' + coords_w + ' ' + coords_s + ',' + coords_e + ' ' + coords_s + ',' + coords_e + ' ' + coords_n + ',' + coords_w + ' ' + coords_n + ',' + coords_w + ' ' + coords_s + '))' #thumbnails thumbnails_urls = [] for thumbnail_url in tree.findall( './gmd:identificationInfo/gmd:MD_DataIdentification/gmd:graphicOverview/gmd:MD_BrowseGraphic/gmd:fileName/', ns): thumbnails_urls.append(thumbnail_url.text) thumbnail_names = [] for thumbnail_name in tree.findall( './gmd:identificationInfo/gmd:MD_DataIdentification/gmd:graphicOverview/gmd:MD_BrowseGraphic/gmd:fileDescription/', ns): thumbnail_names.append(thumbnail_name.text) thumbnails = [] if thumbnails_urls.__len__() == thumbnail_names.__len__(): for i in range(0, thumbnails_urls.__len__()): thumbnail = { 'url': sanitizeXmlText(thumbnails_urls[i]), 'name': sanitizeXmlText(thumbnail_names[i]) } thumbnails.append(thumbnail) resource_constraints = self._getXMLConstraints( tree, './gmd:identificationInfo/gmd:MD_DataIdentification/gmd:resourceConstraints', ns) metadata_constraints = self._getXMLConstraints( tree, './gmd:metadataConstraints', ns) #resources resources = [] for onlineResourceNode in tree.findall( './gmd:distributionInfo/gmd:MD_Distribution/gmd:transferOptions/gmd:MD_DigitalTransferOptions/gmd:onLine/gmd:CI_OnlineResource', ns): onlineResource = self._getOnlineResource( onlineResourceNode, ns) resources.append(onlineResource) resource_contacts = [] for pointOfContact in tree.findall( './gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact', ns): contact = {} organisation = pointOfContact.find( './gmd:CI_ResponsibleParty/gmd:organisationName/gco:CharacterString', ns) role = pointOfContact.find( './gmd:CI_ResponsibleParty/gmd:role/gmd:CI_RoleCode', ns) if organisation is not None: contact['organisation'] = sanitizeXmlText( organisation.text) if role is not None: contact['role'] = sanitizeXmlText(getXMLCodeText(role)) contactInfoNode = pointOfContact.find( './gmd:CI_ResponsibleParty/gmd:contactInfo/gmd:CI_Contact/gmd:onlineResource/gmd:CI_OnlineResource', ns) if contactInfoNode is not None: onlineResource = self._getOnlineResource( contactInfoNode, ns) contact['onlineResource'] = onlineResource resource_contacts.append(contact) metadata_contacts = [] for pointOfContact in tree.findall('./gmd:contact', ns): contact = {} organisation = pointOfContact.find( './gmd:CI_ResponsibleParty/gmd:organisationName/gco:CharacterString', ns) role = pointOfContact.find( './gmd:CI_ResponsibleParty/gmd:role/gmd:CI_RoleCode', ns) if organisation is not None: contact['organisation'] = sanitizeXmlText( organisation.text) if role is not None: contact['role'] = sanitizeXmlText(getXMLCodeText(role)) contactInfoNode = pointOfContact.find( './gmd:CI_ResponsibleParty/gmd:contactInfo/gmd:CI_Contact/gmd:onlineResource/gmd:CI_OnlineResource', ns) if contactInfoNode is not None: onlineResource = self._getOnlineResource( contactInfoNode, ns) contact['onlineResource'] = onlineResource metadata_contacts.append(contact) responsible_parties = [] for responsibleParty in tree.findall( './gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:citedResponsibleParty/gmd:CI_ResponsibleParty', ns): contact = {} organisation = responsibleParty.find( './gmd:organisationName/gco:CharacterString', ns) role = responsibleParty.find('./gmd:role/gmd:CI_RoleCode', ns) if organisation is not None: contact['organisation'] = sanitizeXmlText( organisation.text) if role is not None: contact['role'] = sanitizeXmlText(getXMLCodeText(role)) contactInfoNode = responsibleParty.find( './gmd:contactInfo/gmd:CI_Contact/gmd:onlineResource/gmd:CI_OnlineResource', ns) if contactInfoNode is not None: onlineResource = self._getOnlineResource( contactInfoNode, ns) contact['onlineResource'] = onlineResource responsible_parties.append(contact) contacts = { 'metadata_contacts': metadata_contacts, 'resource_contacts': resource_contacts, 'responsible_parties': responsible_parties } resource = { 'metadata_id': sanitizeXmlText(metadata_id), 'title': sanitizeXmlText(title), 'abstract': sanitizeXmlText(abstract), 'publish_date': sanitizeXmlText(publish_date), 'period_start': sanitizeXmlText(period_start), 'period_end': sanitizeXmlText(period_end), 'categories': categories, 'keywords': keywords, 'representation_type': sanitizeXmlText(representation_type), 'scale': sanitizeXmlText(scale), 'srs': sanitizeXmlText(srs), 'image_url': sanitizeXmlText(image_url), 'thumbnails': thumbnails, 'resources': resources, 'resource_constraints': resource_constraints, 'metadata_constraints': metadata_constraints, 'contacts': contacts } return resource except Exception as e: logger.exception(e) #return ET.tostring(tree, encoding='UTF-8') #return r.content logger.error(r.status_code) logger.error(r.content) raise FailedRequestError(r.status_code, r.content)