def metadata(dataset, node, sceneids, extended=False, api_key=None): """ Request metadata for a given scene in a USGS dataset. :param dataset: :param node: :param sceneids: :param extended: Send a second request to the metadata url to get extended metadata on the scene. :param api_key: """ api_key = api_key if api_key else _get_api_key() xml = soap.metadata(dataset, node, sceneids, api_key=api_key) r = requests.post(USGS_API, xml) root = ElementTree.fromstring(r.text) _check_for_usgs_error(root) items = root.findall("SOAP-ENV:Body/ns1:metadataResponse/return/item", NAMESPACES) data = map(lambda item: {el.tag: xsi.get(el) for el in item}, items) if extended: metadata_urls = map(_get_metadata_url, data) results = _async_requests(metadata_urls) data = map(lambda idx: _get_extended(data[idx], results[idx]), range(len(data))) return data
def test_metadata(self): expected = """ <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://earthexplorer.usgs.gov/inventory/soap"> <soapenv:Header/> <soapenv:Body> <soap:metadata soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <datasetName xsi:type="xsd:string">LANDSAT_8</datasetName> <node xsi:type="xsd:string">EE</node> <apiKey xsi:type="xsd:string">USERS API KEY</apiKey> <entityIds xsi:type="soap:ArrayOfString"> <item xsi:type="xsd:string">LC80130292014100LGN00</item> <item xsi:type="xsd:string">LC80130282014100LGN00</item> </entityIds> </soap:metadata> </soapenv:Body> </soapenv:Envelope> """ request = soap.metadata( "LANDSAT_8", "EE", ["LC80130292014100LGN00", "LC80130282014100LGN00"], api_key="USERS API KEY") request = minidom.parseString(request).toprettyxml() assert compare_xml(request, expected)
def metadata(dataset, node, sceneids, extended=False, api_key=None): """ Request metadata for a given scene in a USGS dataset. :param dataset: :param node: :param sceneids: :param extended: Send a second request to the metadata url to get extended metadata on the scene. :param api_key: """ api_key = _get_api_key() xml = soap.metadata(dataset, node, sceneids, api_key=api_key) r = requests.post(USGS_API, xml) root = ElementTree.fromstring(r.text) _check_for_usgs_error(root) items = root.findall("SOAP-ENV:Body/ns1:metadataResponse/return/item", NAMESPACES) data = map(lambda item: { el.tag: xsi.get(el) for el in item }, items) if extended: def get_extended(scene): metadata_url = scene.get('metadataUrl') r = requests.get(metadata_url) root = ElementTree.fromstring(r.text) items = root.findall("eemetadata:metadataFields/eemetadata:metadataField", NAMESPACES) scene['extended'] = { item.attrib.get('name').strip(): xsi.get(item[0]) for item in items } return scene data = map(get_extended, data) return data
def test_metadata(self): expected = """ <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://earthexplorer.usgs.gov/inventory/soap"> <soapenv:Header/> <soapenv:Body> <soap:metadata soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <datasetName xsi:type="xsd:string">LANDSAT_8</datasetName> <node xsi:type="xsd:string">EE</node> <apiKey xsi:type="xsd:string">USERS API KEY</apiKey> <entityIds xsi:type="soap:ArrayOfString"> <item xsi:type="xsd:string">LC80130292014100LGN00</item> <item xsi:type="xsd:string">LC80130282014100LGN00</item> </entityIds> </soap:metadata> </soapenv:Body> </soapenv:Envelope> """ request = soap.metadata("LANDSAT_8", "EE", ["LC80130292014100LGN00", "LC80130282014100LGN00"], api_key="USERS API KEY") request = minidom.parseString(request).toprettyxml() assert compare_xml(request, expected)