def _parse_response(self, response): '''parse in-memory xml string from a file obj or _invoke ''' # parse result see if it's XML self._exml = etree.parse(StringIO.StringIO(response)) # it's XML. Attempt to decipher whether the XML response is CSW-ish """ valid_xpaths = [ util.nspath_eval('ows:ExceptionReport', namespaces), util.nspath_eval('csw:Capabilities', namespaces), util.nspath_eval('csw:DescribeRecordResponse', namespaces), util.nspath_eval('csw:GetDomainResponse', namespaces), util.nspath_eval('csw:GetRecordsResponse', namespaces), util.nspath_eval('csw:GetRecordByIdResponse', namespaces), util.nspath_eval('csw:HarvestResponse', namespaces), util.nspath_eval('csw:TransactionResponse', namespaces) ] if self._exml.getroot().tag not in valid_xpaths: raise RuntimeError('Document is XML, but not CSW-ish') # check if it's an OGC Exception val = self._exml.find(util.nspath_eval('ows:Exception', namespaces)) if val is not None: raise ows.ExceptionReport(self._exml, self.owscommon.namespace) else: self.exceptionreport = None
def parse_siteinfo(self, xml=None): if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: xml_dict = _xml_to_dict(self._root) self.site_name = xml_dict.get('site_name') self.site_codes = [ testXMLValue(code) for code in self._findall('siteCode') ] self.elevation = xml_dict.get('elevation_m') self.vertical_datum = xml_dict.get('vertical_datum') self.site_types = [ testXMLValue(typ) for typ in self._findall('siteType') ] self.site_properties = dict([ (prop.attrib.get('name'), testXMLValue(prop)) for prop in self._findall('siteProperty') ]) self.altname = xml_dict.get('altname') self.notes = [testXMLValue(note) for note in self._findall('note')] # sub-objects tzi = self._find('timeZoneInfo') if tzi is not None: self.time_zone_info = TimeZoneInfo(tzi, self._ns) self.location = Location(self._find('geoLocation'), self._ns)
def parse_location(self,xml=None): if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: xml_dict = _xml_to_dict(self._root) geogs = self._findall('geogLocation') self.geo_coords = list() self.srs = list() for g in geogs: self.geo_coords.append((testXMLValue(g.find(ns(self._ns) + 'longitude')),testXMLValue(g.find(ns(self._ns) + 'latitude')))) self.srs.append(g.attrib.get('srs')) locsite = self._findall('localSiteXY') self.local_sites = list() self.notes = list() self.projections = list() for ls in locsite: z = testXMLValue(ls.find(ns(self._ns) + 'Z')) if z is not None: self.local_sites.append((testXMLValue(ls.find(ns(self._ns) + 'X')),testXMLValue(ls.find(ns(self._ns) + 'Y')),z)) else: self.local_sites.append((testXMLValue(ls.find(ns(self._ns) + 'X')),testXMLValue(ls.find(ns(self._ns) + 'Y')),'0')) self.notes.append([testXMLValue(note) for note in ls.findall(ns(self._ns) + 'note')]) self.projections.append(ls.attrib.get('projectionInformation'))
def parse_criteria(self, xml=None): if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: xml_dict = _xml_to_dict(self._root, depth=4) self.method_called = self._root.attrib.get('MethodCalled') self.location_param = xml_dict.get('location_param') self.variable_param = xml_dict.get('variable_param') try: self.begin_date_time = parser.parse(xml_dict['begin_date_time']) except: self.begin_date_time = None try: self.end_date_time = parser.parse(xml_dict['end_date_time']) except: self.end_date_time = None self.parameters = [(param.attrib.get('name'), param.attrib.get('value')) for param in self._findall('parameter')]
def parse_series(self, xml=None): if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: xml_dict = _xml_to_dict(self._root, depth=3) self.value_count = xml_dict.get('value_count') self.value_type = xml_dict.get('value_type') self.general_category = xml_dict.get('general_category') self.sample_medium = xml_dict.get('sample_medium') self.data_type = xml_dict.get('data_type') # date-time self.begin_date_time = parser.parse(xml_dict.get('begin_date_time')) self.begin_date_time_utc = parser.parse( xml_dict.get('begin_date_time_utc')) if xml_dict.get( 'begin_date_time_utc') is not None else None self.end_date_time = parser.parse(xml_dict.get('end_date_time')) self.end_date_time_utc = parser.parse( xml_dict.get('end_date_time_utc')) if xml_dict.get( 'end_date_time_utc') is not None else None # method info self.method_description = xml_dict.get('method_description') self.method_code = xml_dict.get('method_code') self.method_link = xml_dict.get('method_link') method = self._find('method') if method is not None: self.method_id = method.attrib.get('methodID') else: self.method_id = None # source info self.organization = xml_dict.get('organization') self.source_description = xml_dict.get('source_description') self.citation = xml_dict.get('citation') source = self._find('source') if source is not None: self.source_id = source.attrib.get('sourceID') else: self.source_id = None # quality control info self.quality_control_level_code = xml_dict.get( 'quality_control_level_code') self.definition = xml_dict.get('definition') qa = self._find('qualityControlLevel') if qa is not None: self.quality_control_level_id = qa.attrib.get( 'qualityControlLevelID') else: self.quality_control_level_id = None # properties self.properties = dict([(prop.attrib.get('name'), testXMLValue(prop)) for prop in self._findall('seriesProperty')]) # sub-objects self.variable = Variable(self._find('variable'), self._ns)
def xmlvalid(xml, xsd): """ Test whether an XML document is valid Parameters ---------- - xml: XML content - xsd: pointer to XML Schema (local file path or URL) """ xsd1 = etree.parse(xsd) xsd2 = etree.XMLSchema(xsd1) doc = etree.parse(StringIO(xml)) return xsd2.validate(doc)
def parse_seriescatalog(self,xml=None): if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: self.series = [Series(elm,self._ns) for elm in self._findall('series')]
def parse_variable(self,xml=None): if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: xml_dict = _xml_to_dict(self._root) self.value_type = xml_dict.get('value_type') self.data_type = xml_dict.get('data_type') self.general_category = xml_dict.get('general_category') self.sample_medium = xml_dict.get('sample_medium') self.no_data_value = xml_dict.get('no_data_value') self.variable_name = xml_dict.get('variable_name') self.variable_code = xml_dict.get('variable_code') self.variable_description = xml_dict.get('variable_description') self.speciation = xml_dict.get('speciation') # notes and properties notes = [(note.attrib.get('title'),testXMLValue(note)) for note in self._findall('note')] none_notes = [note[1] for note in notes if note[0] is None] self.notes = dict([note for note in notes if note[0] is not None]) if len(none_notes) > 0: self.notes['none'] = none_notes self.properties = dict([(prop.attrib.get('name'),testXMLValue(prop)) for prop in self._findall('variableProperty')]) # related related = self._find('related') if related is not None: self.parent_codes = [dict([('network',code.attrib.get('network')),('vocabulary',code.attrib.get('vocabulary')),('default',code.attrib.get('default'))]) for code in related.findall(ns(self._ns) + 'parentCode')] self.related_codes = [dict([('network',d.get('network')),('vocabulary',d.get('vocabulary')),('default',d.get('default'))]) for code in related.findall(ns(self._ns) + 'relatedCode')] else: self.parent_codes = None self.related_codes = None # sub-objects if self._ns == 'wml1.0': unit = self._find('units') self.unit = Unit1_0(unit, self._ns) if unit is not None else None timesupport = self._find('timeSupport') self.time_support = TimeScale(timesupport, self._ns) if timesupport is not None else None else: unit = self._find('unit') self.unit = Unit(unit, self._ns) if unit is not None else None timescale = self._find('timeScale') self.time_scale = TimeScale(timescale, self._ns) if timescale is not None else None categories = self._find('categories') if categories is not None: self.categories = [Category(cat,self._ns) for cat in categories.findall(ns(self._ns) + 'category')] else: self.categories = None
def parse_site(self,xml=None): if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: self.site_info = SiteInfo(self._find('siteInfo'), self._ns) self.series_catalogs = [SeriesCatalog(elm, self._ns) for elm in self._findall('seriesCatalog')]
def __init__(self,xml_root,namespace): try: self._root = etree.parse(xml_root) except: self._root = xml_root if not namespace in namespaces: raise ValueError('Unsupported namespace passed in to parser!') self._ns = namespace
def __init__(self, xml_root, namespace): try: self._root = etree.parse(xml_root) except: self._root = xml_root if not namespace in namespaces: raise ValueError('Unsupported namespace passed in to parser!') self._ns = namespace
def parse_seriescatalog(self, xml=None): if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: self.series = [ Series(elm, self._ns) for elm in self._findall('series') ]
def parse_sites_response(self, xml=None): """ """ if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: self.query_info = QueryInfo(self._find('queryInfo'), self._ns) self.sites = [Site(site, self._ns) for site in self._findall('site')]
def parse_sites_response(self,xml=None): """ """ if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: self.query_info = QueryInfo(self._find('queryInfo'), self._ns) self.sites = [Site(site, self._ns) for site in self._findall('site')]
def parse_series(self,xml=None): if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: xml_dict = _xml_to_dict(self._root,depth=3) self.value_count = xml_dict.get('value_count') self.value_type = xml_dict.get('value_type') self.general_category = xml_dict.get('general_category') self.sample_medium = xml_dict.get('sample_medium') self.data_type = xml_dict.get('data_type') # date-time self.begin_date_time = parser.parse(xml_dict.get('begin_date_time')) self.begin_date_time_utc = parser.parse(xml_dict.get('begin_date_time_utc')) if xml_dict.get('begin_date_time_utc') is not None else None self.end_date_time = parser.parse(xml_dict.get('end_date_time')) self.end_date_time_utc = parser.parse(xml_dict.get('end_date_time_utc')) if xml_dict.get('end_date_time_utc') is not None else None # method info self.method_description = xml_dict.get('method_description') self.method_code = xml_dict.get('method_code') self.method_link = xml_dict.get('method_link') method = self._find('method') if method is not None: self.method_id = method.attrib.get('methodID') else: self.method_id = None # source info self.organization = xml_dict.get('organization') self.source_description = xml_dict.get('source_description') self.citation = xml_dict.get('citation') source = self._find('source') if source is not None: self.source_id = source.attrib.get('sourceID') else: self.source_id = None # quality control info self.quality_control_level_code = xml_dict.get('quality_control_level_code') self.definition = xml_dict.get('definition') qa = self._find('qualityControlLevel') if qa is not None: self.quality_control_level_id = qa.attrib.get('qualityControlLevelID') else: self.quality_control_level_id = None # properties self.properties = dict([(prop.attrib.get('name'),testXMLValue(prop)) for prop in self._findall('seriesProperty')]) # sub-objects self.variable = Variable(self._find('variable'),self._ns)
def parse_site(self, xml=None): if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: self.site_info = SiteInfo(self._find('siteInfo'), self._ns) self.series_catalogs = [ SeriesCatalog(elm, self._ns) for elm in self._findall('seriesCatalog') ]
def parse_query_info(self, xml=None): if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: # create queryinfo object from dict xml_dict = _xml_to_dict(self._root) self.creation_time = parser.parse(xml_dict.get('creation_time')) if xml_dict.get('creation_time') is not None else None self.notes = [testXMLValue(note) for note in self._findall('note')] self.criteria = Criteria(self._find('criteria'), self._ns)
def parse_query_info(self, xml=None): if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: # create queryinfo object from dict xml_dict = _xml_to_dict(self._root) self.creation_time = parser.parse( xml_dict.get('creation_time')) if xml_dict.get( 'creation_time') is not None else None self.notes = [testXMLValue(note) for note in self._findall('note')] self.criteria = Criteria(self._find('criteria'), self._ns)
def parse_timezoneinfo(self,xml=None): if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: xml_dict = _xml_to_dict(self._root) default = self._find('defaultTimeZone') if default is not None: self.zone_offset = default.attrib.get('zoneOffset') self.zone_abbreviation = default.attrib.get('zoneAbbreviation') daylight = self._find('daylightSavingsTimeZone') if daylight is not None: self.daylight_zone_offset = daylight.attrib.get('zoneOffset') self.daylight_zone_abbreviation = daylight.attrib.get('zoneAbbreviation')
def parse_timezoneinfo(self, xml=None): if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: xml_dict = _xml_to_dict(self._root) default = self._find('defaultTimeZone') if default is not None: self.zone_offset = default.attrib.get('zoneOffset') self.zone_abbreviation = default.attrib.get('zoneAbbreviation') daylight = self._find('daylightSavingsTimeZone') if daylight is not None: self.daylight_zone_offset = daylight.attrib.get('zoneOffset') self.daylight_zone_abbreviation = daylight.attrib.get( 'zoneAbbreviation')
def parse_location(self, xml=None): if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: xml_dict = _xml_to_dict(self._root) geogs = self._findall('geogLocation') self.geo_coords = list() self.srs = list() for g in geogs: self.geo_coords.append( (testXMLValue(g.find(ns(self._ns) + 'longitude')), testXMLValue(g.find(ns(self._ns) + 'latitude')))) self.srs.append(g.attrib.get('srs')) locsite = self._findall('localSiteXY') self.local_sites = list() self.notes = list() self.projections = list() for ls in locsite: z = testXMLValue(ls.find(ns(self._ns) + 'Z')) if z is not None: self.local_sites.append( (testXMLValue(ls.find(ns(self._ns) + 'X')), testXMLValue(ls.find(ns(self._ns) + 'Y')), z)) else: self.local_sites.append( (testXMLValue(ls.find(ns(self._ns) + 'X')), testXMLValue(ls.find(ns(self._ns) + 'Y')), '0')) self.notes.append([ testXMLValue(note) for note in ls.findall(ns(self._ns) + 'note') ]) self.projections.append(ls.attrib.get('projectionInformation'))
def parse_siteinfo(self,xml=None): if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: xml_dict = _xml_to_dict(self._root) self.site_name = xml_dict.get('site_name') self.site_codes = [testXMLValue(code) for code in self._findall('siteCode')] self.elevation = xml_dict.get('elevation_m') self.vertical_datum = xml_dict.get('vertical_datum') self.site_types = [testXMLValue(typ) for typ in self._findall('siteType')] self.site_properties = dict([(prop.attrib.get('name'),testXMLValue(prop)) for prop in self._findall('siteProperty')]) self.altname = xml_dict.get('altname') self.notes = [testXMLValue(note) for note in self._findall('note')] # sub-objects tzi = self._find('timeZoneInfo') if tzi is not None: self.time_zone_info = TimeZoneInfo(tzi, self._ns) self.location = Location(self._find('geoLocation'), self._ns)
def parse_criteria(self, xml=None): if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: xml_dict = _xml_to_dict(self._root,depth=4) self.method_called = self._root.attrib.get('MethodCalled') self.location_param = xml_dict.get('location_param') self.variable_param = xml_dict.get('variable_param') try: self.begin_date_time = parser.parse(xml_dict['begin_date_time']) except: self.begin_date_time = None try: self.end_date_time = parser.parse(xml_dict['end_date_time']) except: self.end_date_time = None self.parameters = [(param.attrib.get('name'),param.attrib.get('value')) for param in self._findall('parameter')]
def parse_variable(self, xml=None): if xml is not None: try: self._root = etree.parse(xml) except: self._root = xml # try: xml_dict = _xml_to_dict(self._root) self.value_type = xml_dict.get('value_type') self.data_type = xml_dict.get('data_type') self.general_category = xml_dict.get('general_category') self.sample_medium = xml_dict.get('sample_medium') self.no_data_value = xml_dict.get('no_data_value') self.variable_name = xml_dict.get('variable_name') self.variable_code = xml_dict.get('variable_code') self.variable_description = xml_dict.get('variable_description') self.speciation = xml_dict.get('speciation') # notes and properties notes = [(note.attrib.get('title'), testXMLValue(note)) for note in self._findall('note')] none_notes = [note[1] for note in notes if note[0] is None] self.notes = dict([note for note in notes if note[0] is not None]) if len(none_notes) > 0: self.notes['none'] = none_notes self.properties = dict([(prop.attrib.get('name'), testXMLValue(prop)) for prop in self._findall('variableProperty')]) # related related = self._find('related') if related is not None: self.parent_codes = [ dict([('network', code.attrib.get('network')), ('vocabulary', code.attrib.get('vocabulary')), ('default', code.attrib.get('default'))]) for code in related.findall(ns(self._ns) + 'parentCode') ] self.related_codes = [ dict([('network', d.get('network')), ('vocabulary', d.get('vocabulary')), ('default', d.get('default'))]) for code in related.findall(ns(self._ns) + 'relatedCode') ] else: self.parent_codes = None self.related_codes = None # sub-objects if self._ns == 'wml1.0': unit = self._find('units') self.unit = Unit1_0(unit, self._ns) if unit is not None else None timesupport = self._find('timeSupport') self.time_support = TimeScale( timesupport, self._ns) if timesupport is not None else None else: unit = self._find('unit') self.unit = Unit(unit, self._ns) if unit is not None else None timescale = self._find('timeScale') self.time_scale = TimeScale( timescale, self._ns) if timescale is not None else None categories = self._find('categories') if categories is not None: self.categories = [ Category(cat, self._ns) for cat in categories.findall(ns(self._ns) + 'category') ] else: self.categories = None
def __init__(self, elem, parse_remote_metadata=False, timeout=30): """.""" self.id = testXMLValue(elem.find(nspath_eval('wfs:Name', namespaces))) self.title = testXMLValue( elem.find(nspath_eval('wfs:Title', namespaces))) self.abstract = testXMLValue( elem.find(nspath_eval('wfs:Abstract', namespaces))) self.keywords = [ f.text for f in elem.findall( nspath_eval('ows:Keywords/ows:Keyword', namespaces)) ] boxes = elem.findall(nspath_eval('ows:BoundingBox', namespaces)) self.boundingBoxes = [] for b in boxes: try: # sometimes the SRS attribute is (wrongly) not provided srs = b.attrib['SRS'] except KeyError: srs = None self.boundingBoxes.append(( float(b.attrib['minx']), float(b.attrib['miny']), float(b.attrib['maxx']), float(b.attrib['maxy']), srs, )) # bbox self.boundingBoxWGS84 = None b = BoundingBox( elem.find(nspath_eval('ows:WGS84BoundingBox', namespaces)), namespaces['ows']) if b is not None: self.boundingBoxWGS84 = ( float(b.minx), float(b.miny), float(b.maxx), float(b.maxy), ) # crs options self.crsOptions = [ str(Crs(srs.text)) for srs in elem.findall(nspath_eval('wfs:OtherSRS', namespaces)) ] dsrs = testXMLValue( elem.find(nspath_eval('wfs:DefaultSRS', namespaces))) if dsrs is not None: # first element is default srs self.crsOptions.insert(0, str(Crs(dsrs))) # verbs self.verbOptions = [ op.text for op in elem.findall( nspath_eval('wfs:Operations/wfs:Operation', namespaces)) ] # output formats self.outputFormats = [ op.text for op in elem.findall( nspath_eval('wfs:OutputFormats/wfs:Format', namespaces)) ] # MetadataURLs self.metadataUrls = [] for m in elem.findall(nspath_eval('wfs:MetadataURL', namespaces)): metadataUrl = { 'type': testXMLValue(m.attrib['type'], attrib=True), 'format': testXMLValue(m.find('Format')), 'url': testXMLValue(m) } if metadataUrl[ 'url'] is not None and parse_remote_metadata: # download URL try: content = urlopen(metadataUrl['url'], timeout=timeout) doc = etree.parse(content) if metadataUrl['type'] is not None: if metadataUrl['type'] == 'FGDC': metadataUrl['metadata'] = Metadata(doc) if metadataUrl['type'] in ['TC211', '19115', '19139']: metadataUrl['metadata'] = MD_Metadata(doc) except Exception: metadataUrl['metadata'] = None self.metadataUrls.append(metadataUrl) # others not used but needed for iContentMetadata harmonisation self.styles = None self.timepositions = None self.defaulttimeposition = None self.attribution = None
def __init__(self, elem, parent, parse_remote_metadata=False, timeout=30): """.""" self.id = testXMLValue(elem.find(nspath('Name'))) self.title = testXMLValue(elem.find(nspath('Title'))) self.abstract = testXMLValue(elem.find(nspath('Abstract'))) self.keywords = [f.text for f in elem.findall(nspath('Keywords'))] # bboxes boxes = elem.findall('BoundingBox') self.boundingBoxes = [] for b in boxes: try: # sometimes the SRS attribute is (wrongly) not provided srs = b.attrib['SRS'] except KeyError: srs = None self.boundingBoxes.append(( float(b.attrib['minx']), float(b.attrib['miny']), float(b.attrib['maxx']), float(b.attrib['maxy']), srs, )) self.boundingBoxes = list(set(self.boundingBoxes)) self.boundingBoxWGS84 = None b = elem.find(nspath('LatLongBoundingBox')) if b is not None: self.boundingBoxWGS84 = ( float(b.attrib['minx']), float(b.attrib['miny']), float(b.attrib['maxx']), float(b.attrib['maxy']), ) # crs options self.crsOptions = [ str(Crs(srs.text)) for srs in elem.findall(nspath('SRS')) ] # verbs self.verbOptions = [ op.tag for op in parent.findall(nspath('Operations/*')) ] self.verbOptions + [ op.tag for op in elem.findall(nspath('Operations/*')) if op.tag not in self.verbOptions ] # others not used but needed for iContentMetadata harmonisation self.styles = None self.timepositions = None self.defaulttimeposition = None self.attribution = None # MetadataURLs self.metadataUrls = [] for m in elem.findall(nspath('MetadataURL')): metadataUrl = { 'type': testXMLValue(m.attrib['type'], attrib=True), 'format': testXMLValue(m.find('Format')), 'url': testXMLValue(m) } if metadataUrl[ 'url'] is not None and parse_remote_metadata: # download URL try: content = urlopen(metadataUrl['url'], timeout=timeout) doc = etree.parse(content) if metadataUrl['type'] is not None: if metadataUrl['type'] == 'FGDC': metadataUrl['metadata'] = Metadata(doc) if metadataUrl['type'] == 'TC211': metadataUrl['metadata'] = MD_Metadata(doc) except Exception: metadataUrl['metadata'] = None self.metadataUrls.append(metadataUrl)
def __init__(self, elem, parent=None, index=0, parse_remote_metadata=False, timeout=30): if strip_ns(elem.tag) != 'Layer': raise ValueError('%s should be a Layer' % (elem,)) self.parent = parent if parent: self.index = "%s.%d" % (parent.index, index) else: self.index = str(index) self.id = self.name = testXMLValue(elem.find(nspath('Name', WMS_NAMESPACE))) # layer attributes self.queryable = int(elem.attrib.get('queryable', 0)) self.cascaded = int(elem.attrib.get('cascaded', 0)) self.opaque = int(elem.attrib.get('opaque', 0)) self.noSubsets = int(elem.attrib.get('noSubsets', 0)) self.fixedWidth = int(elem.attrib.get('fixedWidth', 0)) self.fixedHeight = int(elem.attrib.get('fixedHeight', 0)) # title is mandatory property self.title = None title = testXMLValue(elem.find(nspath('Title', WMS_NAMESPACE))) if title is not None: self.title = title.strip() self.abstract = testXMLValue(elem.find(nspath('Abstract', WMS_NAMESPACE))) # bboxes boxes = elem.findall(nspath('BoundingBox', WMS_NAMESPACE)) self.boundingBoxes = [] for b in boxes: try: # sometimes the SRS attribute is (wrongly) not provided srs = b.attrib['CRS'] except KeyError: srs = None self.boundingBoxes.append(( float(b.attrib['minx']), float(b.attrib['miny']), float(b.attrib['maxx']), float(b.attrib['maxy']), srs, )) if self.parent: if hasattr(self.parent, 'boundingBox'): self.boundingBoxes.append(self.parent.boundingBox) self.boundingBoxes = list(set(self.boundingBoxes)) # ScaleHint sh = elem.find(nspath('ScaleHint', WMS_NAMESPACE)) self.scaleHint = None if sh is not None: if 'min' in sh.attrib and 'max' in sh.attrib: self.scaleHint = {'min': sh.attrib['min'], 'max': sh.attrib['max']} attribution = elem.find(nspath('Attribution', WMS_NAMESPACE)) self.attribution = {} if attribution is not None: title = attribution.find(nspath('Title', WMS_NAMESPACE)) url = attribution.find(nspath('OnlineResource', WMS_NAMESPACE)) logo = attribution.find(nspath('LogoURL', WMS_NAMESPACE)) if title is not None: self.attribution['title'] = title.text if url is not None: self.attribution['url'] = url.attrib['{http://www.w3.org/1999/xlink}href'] if logo is not None: self.attribution['logo_size'] = ( int(logo.attrib['width']), int(logo.attrib['height']) ) self.attribution['logo_url'] = logo.find( nspath('OnlineResource', WMS_NAMESPACE) ).attrib['{http://www.w3.org/1999/xlink}href'] b = elem.find(nspath('EX_GeographicBoundingBox', WMS_NAMESPACE)) if b is not None: west_longitude = b.find(nspath('westBoundLongitude', WMS_NAMESPACE)) east_longitude = b.find(nspath('eastBoundLongitude', WMS_NAMESPACE)) south_latitude = b.find(nspath('southBoundLatitude', WMS_NAMESPACE)) north_latitude = b.find(nspath('northBoundLatitude', WMS_NAMESPACE)) self.boundingBoxWGS84 = ( float(west_longitude.text if west_longitude is not None else ''), float(south_latitude.text if south_latitude is not None else ''), float(east_longitude.text if east_longitude is not None else ''), float(north_latitude.text if north_latitude is not None else ''), ) elif self.parent: self.boundingBoxWGS84 = self.parent.boundingBoxWGS84 else: self.boundingBoxWGS84 = None # TODO: get this from the bbox attributes instead (deal with parents) # SRS options self.crsOptions = [] # Copy any parent SRS options (they are inheritable properties) if self.parent: self.crsOptions = list(self.parent.crsOptions) # Look for SRS option attached to this layer if elem.find(nspath('CRS', WMS_NAMESPACE)) is not None: # some servers found in the wild use a single SRS # tag containing a whitespace separated list of SRIDs # instead of several SRS tags. hence the inner loop for srslist in map(lambda x: x.text, elem.findall(nspath('CRS', WMS_NAMESPACE))): if srslist: for srs in srslist.split(): self.crsOptions.append(srs) # Get rid of duplicate entries self.crsOptions = list(set(self.crsOptions)) # Set self.crsOptions to None if the layer (and parents) had no SRS options if len(self.crsOptions) == 0: # raise ValueError('%s no SRS available!?' % (elem,)) # Comment by D Lowe. # Do not raise ValueError as it is possible that a layer is purely a parent layer and does not have SRS specified. Instead set crsOptions to None # Comment by Jachym: # Do not set it to None, but to [], which will make the code # work further. Fixed by anthonybaxter self.crsOptions = [] # Styles self.styles = {} # Copy any parent styles (they are inheritable properties) if self.parent: self.styles = self.parent.styles.copy() # Get the styles for this layer (items with the same name are replaced) for s in elem.findall(nspath('Style', WMS_NAMESPACE)): name = s.find(nspath('Name', WMS_NAMESPACE)) title = s.find(nspath('Title', WMS_NAMESPACE)) if name is None or title is None: raise ValueError('%s missing name or title' % (s,)) style = {'title': title.text} # legend url legend = s.find(nspath('LegendURL/OnlineResource', WMS_NAMESPACE)) if legend is not None: style['legend'] = legend.attrib['{http://www.w3.org/1999/xlink}href'] self.styles[name.text] = style # keywords self.keywords = [f.text for f in elem.findall(nspath('KeywordList/Keyword', WMS_NAMESPACE))] # timepositions - times for which data is available. # return a tuple (for now) of (default, positions) self.timepositions = [] for extent in elem.findall(nspath('Extent', WMS_NAMESPACE)): if extent.attrib.get("name").lower() == 'time': if extent.text: timeposition = extent.text defaulttimeposition = extent.attrib.get("default", '') self.timepositions.append((defaulttimeposition, timeposition)) # Elevations - available vertical levels self.elevations = None for extent in elem.findall(nspath('Extent', WMS_NAMESPACE)): if extent.attrib.get("name").lower() == 'elevation': if extent.text: self.elevations = extent.text.split(',') break # MetadataURLs self.metadataUrls = [] for m in elem.findall(nspath('MetadataURL', WMS_NAMESPACE)): metadataUrl = { 'type': testXMLValue(m.attrib['type'], attrib=True), 'format': testXMLValue(m.find(nspath('Format', WMS_NAMESPACE))), 'url': testXMLValue(m.find(nspath('OnlineResource', WMS_NAMESPACE)).attrib['{http://www.w3.org/1999/xlink}href'], attrib=True) } if metadataUrl['url'] is not None and parse_remote_metadata: # download URL try: content = urllib2.urlopen(metadataUrl['url'], timeout=timeout) doc = etree.parse(content) if metadataUrl['type'] is not None: if metadataUrl['type'] == 'FGDC': metadataUrl['metadata'] = Metadata(doc) if metadataUrl['type'] == 'TC211': metadataUrl['metadata'] = MD_Metadata(doc) except Exception: metadataUrl['metadata'] = None self.metadataUrls.append(metadataUrl) # DataURLs self.dataUrls = [] for m in elem.findall(nspath('DataURL', WMS_NAMESPACE)): dataUrl = { 'format': m.find(nspath('Format', WMS_NAMESPACE)).text.strip(), 'url': m.find(nspath('OnlineResource', WMS_NAMESPACE)).attrib['{http://www.w3.org/1999/xlink}href'] } self.dataUrls.append(dataUrl) self.layers = [] for child in elem.findall(nspath('Layer', WMS_NAMESPACE)): self.layers.append(ContentMetadata(child, self))
def __init__(self, elem, parent=None, index=0, parse_remote_metadata=False, timeout=30): if strip_ns(elem.tag) != 'Layer': raise ValueError('%s should be a Layer' % (elem, )) self.parent = parent if parent: self.index = "%s.%d" % (parent.index, index) else: self.index = str(index) self.id = self.name = testXMLValue( elem.find(nspath('Name', WMS_NAMESPACE))) # layer attributes self.queryable = int(elem.attrib.get('queryable', 0)) self.cascaded = int(elem.attrib.get('cascaded', 0)) self.opaque = int(elem.attrib.get('opaque', 0)) self.noSubsets = int(elem.attrib.get('noSubsets', 0)) self.fixedWidth = int(elem.attrib.get('fixedWidth', 0)) self.fixedHeight = int(elem.attrib.get('fixedHeight', 0)) # title is mandatory property self.title = None title = testXMLValue(elem.find(nspath('Title', WMS_NAMESPACE))) if title is not None: self.title = title.strip() self.abstract = testXMLValue( elem.find(nspath('Abstract', WMS_NAMESPACE))) # bboxes boxes = elem.findall(nspath('BoundingBox', WMS_NAMESPACE)) self.boundingBoxes = [] for b in boxes: try: # sometimes the SRS attribute is (wrongly) not provided srs = b.attrib['CRS'] except KeyError: srs = None self.boundingBoxes.append(( float(b.attrib['minx']), float(b.attrib['miny']), float(b.attrib['maxx']), float(b.attrib['maxy']), srs, )) if self.parent: if hasattr(self.parent, 'boundingBox'): self.boundingBoxes.append(self.parent.boundingBox) self.boundingBoxes = list(set(self.boundingBoxes)) # ScaleHint sh = elem.find(nspath('ScaleHint', WMS_NAMESPACE)) self.scaleHint = None if sh is not None: if 'min' in sh.attrib and 'max' in sh.attrib: self.scaleHint = { 'min': sh.attrib['min'], 'max': sh.attrib['max'] } attribution = elem.find(nspath('Attribution', WMS_NAMESPACE)) self.attribution = {} if attribution is not None: title = attribution.find(nspath('Title', WMS_NAMESPACE)) url = attribution.find(nspath('OnlineResource', WMS_NAMESPACE)) logo = attribution.find(nspath('LogoURL', WMS_NAMESPACE)) if title is not None: self.attribution['title'] = title.text if url is not None: self.attribution['url'] = url.attrib[ '{http://www.w3.org/1999/xlink}href'] if logo is not None: self.attribution['logo_size'] = (int(logo.attrib['width']), int(logo.attrib['height'])) self.attribution['logo_url'] = logo.find( nspath('OnlineResource', WMS_NAMESPACE) ).attrib['{http://www.w3.org/1999/xlink}href'] b = elem.find(nspath('EX_GeographicBoundingBox', WMS_NAMESPACE)) if b is not None: west_longitude = b.find(nspath('westBoundLongitude', WMS_NAMESPACE)) east_longitude = b.find(nspath('eastBoundLongitude', WMS_NAMESPACE)) south_latitude = b.find(nspath('southBoundLatitude', WMS_NAMESPACE)) north_latitude = b.find(nspath('northBoundLatitude', WMS_NAMESPACE)) self.boundingBoxWGS84 = ( float( west_longitude.text if west_longitude is not None else ''), float( south_latitude.text if south_latitude is not None else ''), float( east_longitude.text if east_longitude is not None else ''), float( north_latitude.text if north_latitude is not None else ''), ) elif self.parent: self.boundingBoxWGS84 = self.parent.boundingBoxWGS84 else: self.boundingBoxWGS84 = None # TODO: get this from the bbox attributes instead (deal with parents) # SRS options self.crsOptions = [] # Copy any parent SRS options (they are inheritable properties) if self.parent: self.crsOptions = list(self.parent.crsOptions) # Look for SRS option attached to this layer if elem.find(nspath('CRS', WMS_NAMESPACE)) is not None: # some servers found in the wild use a single SRS # tag containing a whitespace separated list of SRIDs # instead of several SRS tags. hence the inner loop for srslist in map(lambda x: x.text, elem.findall(nspath('CRS', WMS_NAMESPACE))): if srslist: for srs in srslist.split(): self.crsOptions.append(srs) # Get rid of duplicate entries self.crsOptions = list(set(self.crsOptions)) # Set self.crsOptions to None if the layer (and parents) had no SRS options if len(self.crsOptions) == 0: # raise ValueError('%s no SRS available!?' % (elem,)) # Comment by D Lowe. # Do not raise ValueError as it is possible that a layer is purely a parent layer and does not have SRS specified. Instead set crsOptions to None # Comment by Jachym: # Do not set it to None, but to [], which will make the code # work further. Fixed by anthonybaxter self.crsOptions = [] # Styles self.styles = {} # Copy any parent styles (they are inheritable properties) if self.parent: self.styles = self.parent.styles.copy() # Get the styles for this layer (items with the same name are replaced) for s in elem.findall(nspath('Style', WMS_NAMESPACE)): name = s.find(nspath('Name', WMS_NAMESPACE)) title = s.find(nspath('Title', WMS_NAMESPACE)) if name is None or title is None: raise ValueError('%s missing name or title' % (s, )) style = {'title': title.text} # legend url legend = s.find(nspath('LegendURL/OnlineResource', WMS_NAMESPACE)) if legend is not None: style['legend'] = legend.attrib[ '{http://www.w3.org/1999/xlink}href'] self.styles[name.text] = style # keywords self.keywords = [ f.text for f in elem.findall(nspath('KeywordList/Keyword', WMS_NAMESPACE)) ] # timepositions - times for which data is available. # return a tuple (for now) of (default, positions) self.timepositions = [] for extent in elem.findall(nspath('Extent', WMS_NAMESPACE)): if extent.attrib.get("name").lower() == 'time': if extent.text: timeposition = extent.text defaulttimeposition = extent.attrib.get("default", '') self.timepositions.append( (defaulttimeposition, timeposition)) # Elevations - available vertical levels self.elevations = None for extent in elem.findall(nspath('Extent', WMS_NAMESPACE)): if extent.attrib.get("name").lower() == 'elevation': if extent.text: self.elevations = extent.text.split(',') break # MetadataURLs self.metadataUrls = [] for m in elem.findall(nspath('MetadataURL', WMS_NAMESPACE)): metadataUrl = { 'type': testXMLValue(m.attrib['type'], attrib=True), 'format': testXMLValue(m.find(nspath('Format', WMS_NAMESPACE))), 'url': testXMLValue(m.find(nspath('OnlineResource', WMS_NAMESPACE)). attrib['{http://www.w3.org/1999/xlink}href'], attrib=True) } if metadataUrl[ 'url'] is not None and parse_remote_metadata: # download URL try: content = urllib2.urlopen(metadataUrl['url'], timeout=timeout) doc = etree.parse(content) if metadataUrl['type'] is not None: if metadataUrl['type'] == 'FGDC': metadataUrl['metadata'] = Metadata(doc) if metadataUrl['type'] == 'TC211': metadataUrl['metadata'] = MD_Metadata(doc) except Exception: metadataUrl['metadata'] = None self.metadataUrls.append(metadataUrl) # DataURLs self.dataUrls = [] for m in elem.findall(nspath('DataURL', WMS_NAMESPACE)): dataUrl = { 'format': m.find(nspath('Format', WMS_NAMESPACE)).text.strip(), 'url': m.find(nspath('OnlineResource', WMS_NAMESPACE)). attrib['{http://www.w3.org/1999/xlink}href'] } self.dataUrls.append(dataUrl) self.layers = [] for child in elem.findall(nspath('Layer', WMS_NAMESPACE)): self.layers.append(ContentMetadata(child, self))
def __init__(self, elem, parse_remote_metadata=False, timeout=30): """.""" self.id = testXMLValue(elem.find(nspath_eval('wfs:Name', namespaces))) self.title = testXMLValue(elem.find(nspath_eval('wfs:Title', namespaces))) self.abstract = testXMLValue(elem.find(nspath_eval('wfs:Abstract', namespaces))) self.keywords = [ f.text for f in elem.findall(nspath_eval('ows:Keywords/ows:Keyword', namespaces)) ] boxes = elem.findall(nspath_eval('ows:BoundingBox', namespaces)) self.boundingBoxes = [] for b in boxes: try: # sometimes the SRS attribute is (wrongly) not provided srs = b.attrib['SRS'] except KeyError: srs = None self.boundingBoxes.append(( float(b.attrib['minx']), float(b.attrib['miny']), float(b.attrib['maxx']), float(b.attrib['maxy']), srs, )) # bbox self.boundingBoxWGS84 = None b = BoundingBox( elem.find(nspath_eval('ows:WGS84BoundingBox', namespaces)), namespaces['ows'] ) if b is not None: self.boundingBoxWGS84 = ( float(b.minx), float(b.miny), float(b.maxx), float(b.maxy), ) # crs options self.crsOptions = [ str(Crs(srs.text)) for srs in elem.findall(nspath_eval('wfs:OtherSRS', namespaces)) ] dsrs = testXMLValue(elem.find(nspath_eval('wfs:DefaultSRS', namespaces))) if dsrs is not None: # first element is default srs self.crsOptions.insert(0, str(Crs(dsrs))) # verbs self.verbOptions = [ op.text for op in elem.findall(nspath_eval('wfs:Operations/wfs:Operation', namespaces)) ] # output formats self.outputFormats = [ op.text for op in elem.findall(nspath_eval('wfs:OutputFormats/wfs:Format', namespaces)) ] # MetadataURLs self.metadataUrls = [] for m in elem.findall(nspath_eval('wfs:MetadataURL', namespaces)): metadataUrl = { 'type': testXMLValue(m.attrib['type'], attrib=True), 'format': testXMLValue(m.find('Format')), 'url': testXMLValue(m) } if metadataUrl['url'] is not None and parse_remote_metadata: # download URL try: content = urlopen(metadataUrl['url'], timeout=timeout) doc = etree.parse(content) if metadataUrl['type'] is not None: if metadataUrl['type'] == 'FGDC': metadataUrl['metadata'] = Metadata(doc) if metadataUrl['type'] in ['TC211', '19115', '19139']: metadataUrl['metadata'] = MD_Metadata(doc) except Exception: metadataUrl['metadata'] = None self.metadataUrls.append(metadataUrl) # others not used but needed for iContentMetadata harmonisation self.styles = None self.timepositions = None self.defaulttimeposition = None self.attribution = None
def __init__(self, elem, parent, parse_remote_metadata=False, timeout=30): """.""" self.id = elem.find(nspath('Name',ns=WFS_NAMESPACE)).text self.title = elem.find(nspath('Title',ns=WFS_NAMESPACE)).text abstract = elem.find(nspath('Abstract',ns=WFS_NAMESPACE)) if abstract is not None: self.abstract = abstract.text else: self.abstract = None self.keywords = [f.text for f in elem.findall(nspath('Keywords',ns=WFS_NAMESPACE))] # bboxes self.boundingBoxWGS84 = None b = elem.find(nspath('WGS84BoundingBox',ns=OWS_NAMESPACE)) if b is not None: lc = b.find(nspath("LowerCorner",ns=OWS_NAMESPACE)) uc = b.find(nspath("UpperCorner",ns=OWS_NAMESPACE)) ll = [float(s) for s in lc.text.split()] ur = [float(s) for s in uc.text.split()] self.boundingBoxWGS84 = (ll[0],ll[1],ur[0],ur[1]) # there is no such think as bounding box # make copy of the WGS84BoundingBox self.boundingBox = (self.boundingBoxWGS84[0], self.boundingBoxWGS84[1], self.boundingBoxWGS84[2], self.boundingBoxWGS84[3], Crs("epsg:4326")) # crs options self.crsOptions = [Crs(srs.text) for srs in elem.findall(nspath('OtherCRS',ns=WFS_NAMESPACE))] defaultCrs = elem.findall(nspath('DefaultCRS',ns=WFS_NAMESPACE)) if len(defaultCrs) > 0: self.crsOptions.insert(0,Crs(defaultCrs[0].text)) # verbs self.verbOptions = [op.tag for op \ in parent.findall(nspath('Operations/*',ns=WFS_NAMESPACE))] self.verbOptions + [op.tag for op \ in elem.findall(nspath('Operations/*',ns=WFS_NAMESPACE)) \ if op.tag not in self.verbOptions] #others not used but needed for iContentMetadata harmonisation self.styles=None self.timepositions=None self.defaulttimeposition=None # MetadataURLs self.metadataUrls = [] for m in elem.findall('MetadataURL'): metadataUrl = { 'type': testXMLValue(m.attrib['type'], attrib=True), 'format': m.find('Format').text.strip(), 'url': testXMLValue(m.find('OnlineResource').attrib['{http://www.w3.org/1999/xlink}href'], attrib=True) } if metadataUrl['url'] is not None and parse_remote_metadata: # download URL try: content = urllib2.urlopen(metadataUrl['url'], timeout=timeout) doc = etree.parse(content) try: # FGDC metadataUrl['metadata'] = Metadata(doc) except: # ISO metadataUrl['metadata'] = MD_Metadata(doc) except Exception: metadataUrl['metadata'] = None self.metadataUrls.append(metadataUrl)
def __init__(self, elem, parent, parse_remote_metadata=False, timeout=30): """.""" self.id = testXMLValue(elem.find(nspath('Name'))) self.title = testXMLValue(elem.find(nspath('Title'))) self.abstract = testXMLValue(elem.find(nspath('Abstract'))) self.keywords = [f.text for f in elem.findall(nspath('Keywords'))] # bboxes boxes = elem.findall('BoundingBox') self.boundingBoxes = [] for b in boxes: try: # sometimes the SRS attribute is (wrongly) not provided srs = b.attrib['SRS'] except KeyError: srs = None self.boundingBoxes.append(( float(b.attrib['minx']), float(b.attrib['miny']), float(b.attrib['maxx']), float(b.attrib['maxy']), srs, )) self.boundingBoxes = list(set(self.boundingBoxes)) self.boundingBoxWGS84 = None b = elem.find(nspath('LatLongBoundingBox')) if b is not None: self.boundingBoxWGS84 = ( float(b.attrib['minx']), float(b.attrib['miny']), float(b.attrib['maxx']), float(b.attrib['maxy']), ) # crs options self.crsOptions = [str(Crs(srs.text)) for srs in elem.findall(nspath('SRS'))] # verbs self.verbOptions = [op.tag for op in parent.findall(nspath('Operations/*'))] self.verbOptions + [ op.tag for op in elem.findall(nspath('Operations/*')) if op.tag not in self.verbOptions ] # others not used but needed for iContentMetadata harmonisation self.styles = None self.timepositions = None self.defaulttimeposition = None self.attribution = None # MetadataURLs self.metadataUrls = [] for m in elem.findall(nspath('MetadataURL')): metadataUrl = { 'type': testXMLValue(m.attrib['type'], attrib=True), 'format': testXMLValue(m.find('Format')), 'url': testXMLValue(m) } if metadataUrl['url'] is not None and parse_remote_metadata: # download URL try: content = urlopen(metadataUrl['url'], timeout=timeout) doc = etree.parse(content) if metadataUrl['type'] is not None: if metadataUrl['type'] == 'FGDC': metadataUrl['metadata'] = Metadata(doc) if metadataUrl['type'] == 'TC211': metadataUrl['metadata'] = MD_Metadata(doc) except Exception: metadataUrl['metadata'] = None self.metadataUrls.append(metadataUrl)