Search code examples
pythonxmlelementtree

Accessing Layer in XML response


I am having trouble accessing a particular layer in a XML response to a request.get.

I am trying to access the 'Time' values for the Radar_UK_Composite_Highres which lies underneath Contents. The XML response is attached at the end of the post.

I tried to access the layer through layer_elem.find, as well as manually indexing to the position. The .find method turns out no results and the indexing approach gives an over range error if I try to go further than tree[3]. It seems like after </ows:OperationsMetadata> the file ends as in the eyes of the elementtree methods.

<?xml version="1.0" encoding="UTF-8"?>
<Capabilities xmlns="http://www.opengis.net/wmts/1.0"
xmlns:ows="http://www.opengis.net/ows/1.1"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:gml="http://www.opengis.net/gml" xsi:schemaLocation="http://www.opengis.net/wmts/1.0 http://schemas.opengis.net/wmts/1.0/wmtsGetCapabilities_response.xsd"
version="1.0.0">
<ows:ServiceIdentification>
  <ows:Title>GeoWebCache</ows:Title>
  <ows:Abstract>GeoWebCache is an advanced tile cache for WMS servers.It supports a large variety of protocols and
      formats, including WMS-C, WMTS, KML, Google Maps and Virtual Earth.</ows:Abstract>
  <ows:Keywords>
    <ows:Keyword>WFS</ows:Keyword>
    <ows:Keyword>WMS</ows:Keyword>
    <ows:Keyword>WMTS</ows:Keyword>
    <ows:Keyword>GEOWEBCACHE</ows:Keyword>
  </ows:Keywords>
  <ows:ServiceType>OGC WMTS</ows:ServiceType>
  <ows:ServiceTypeVersion>1.0.0</ows:ServiceTypeVersion>
  <ows:Fees>NONE</ows:Fees>
  <ows:AccessConstraints>NONE</ows:AccessConstraints>
</ows:ServiceIdentification>
<ows:ServiceProvider>
  <ows:ProviderName>Met Office</ows:ProviderName>
  <ows:ProviderSite xlink:href="http://www.metoffice.gov.uk/" />
  <ows:ServiceContact>
    <ows:ContactInfo>
      <ows:Phone>
      <ows:Voice>+441392885680</ows:Voice>
      <ows:Facsimile>+441392885681</ows:Facsimile>
      </ows:Phone>
      <ows:Address>
      <ows:DeliveryPoint>Fitzroy Road</ows:DeliveryPoint>
      <ows:City>Exeter</ows:City>
      <ows:AdministrativeArea>Devon</ows:AdministrativeArea>
      <ows:PostalCode>EX1 3PB</ows:PostalCode>
      <ows:Country>United Kingdom</ows:Country>
      <ows:ElectronicMailAddress>[email protected]</ows:ElectronicMailAddress>
      </ows:Address>
    </ows:ContactInfo>
  </ows:ServiceContact>
</ows:ServiceProvider>
<ows:OperationsMetadata>
  <ows:Operation name="GetCapabilities">
    <ows:DCP>
      <ows:HTTP>
        <ows:Get xlink:href="http://datapoint.metoffice.gov.uk/public/data/inspire/view/wmts?">
          <ows:Constraint name="GetEncoding">
            <ows:AllowedValues>
              <ows:Value>KVP</ows:Value>
            </ows:AllowedValues>
          </ows:Constraint>
        </ows:Get>
      </ows:HTTP>
    </ows:DCP>
  </ows:Operation>
  <ows:Operation name="GetTile">
    <ows:DCP>
      <ows:HTTP>
        <ows:Get xlink:href="http://datapoint.metoffice.gov.uk/public/data/inspire/view/wmts?">
          <ows:Constraint name="GetEncoding">
            <ows:AllowedValues>
              <ows:Value>KVP</ows:Value>
            </ows:AllowedValues>
          </ows:Constraint>
        </ows:Get>
      </ows:HTTP>
    </ows:DCP>
  </ows:Operation>
  <ows:Operation name="GetFeatureInfo">
    <ows:DCP>
      <ows:HTTP>
        <ows:Get xlink:href="http://datapoint.metoffice.gov.uk/public/data/inspire/view/wmts?">
          <ows:Constraint name="GetEncoding">
            <ows:AllowedValues>
              <ows:Value>KVP</ows:Value>
            </ows:AllowedValues>
          </ows:Constraint>
        </ows:Get>
      </ows:HTTP>
    </ows:DCP>
  </ows:Operation>
</ows:OperationsMetadata>
<Contents>
  <Layer>
    <ows:Title>RADAR_UK_Composite_Highres</ows:Title>
    <ows:WGS84BoundingBox>
      <ows:LowerCorner>-180.0 -90.0</ows:LowerCorner>
      <ows:UpperCorner>180.0 90.0</ows:UpperCorner>
    </ows:WGS84BoundingBox>
    <ows:Identifier>RADAR_UK_Composite_Highres</ows:Identifier>
    <Style>
      <ows:Identifier>Bitmap 1km Blue-Pale blue gradient 0.01 to 32mm/hr</ows:Identifier>
    </Style>
    <Style>
      <ows:Identifier>Bitmap 1km Blue-Pale blue gradient 0.25 to 32mm/hr</ows:Identifier>
    </Style>
    <Style>
      <ows:Identifier>Bitmap Interpolated 1km Blue-Pale blue gradient 0.25 to 32mm/hr</ows:Identifier>
    </Style>
    <Format>image/png</Format>
    <Dimension>      <Identifier>TIME</Identifier>     
    <Default>2023-11-23T14:15:00Z</Default>   
    <Value>2023-11-22T00:00:00Z</Value> 
    </Dimension>    <TileMatrixSetLink>      <TileMatrixSet>EPSG:29903</TileMatrixSet>
    </TileMatrixSetLink>    <TileMatrixSetLink>      <TileMatrixSet>EPSG:4326</TileMatrixSet>
    </TileMatrixSetLink>    <TileMatrixSetLink>      <TileMatrixSet>EPSG:4258</TileMatrixSet>
    </TileMatrixSetLink>    <TileMatrixSetLink>      <TileMatrixSet>EPSG:27700</TileMatrixSet>
    </TileMatrixSetLink>    <TileMatrixSetLink>      <TileMatrixSet>EPSG:2157</TileMatrixSet>
    </TileMatrixSetLink>  </Layer>

The XML data goes on for quite a bit, but this is the section that I am currently wanting to work with.


Solution

  • Your problem I assume is the namespace definition:

    import xml.etree.ElementTree as ET
    import datetime
    
    tree = ET.parse("Capabilities.xml")
    root = tree.getroot()
    
    ns = dict([elem for event, elem in ET.iterparse("Capabilities.xml", events=("start-ns",))])
    time = root.find(".//Dimension/Default", ns).text
    zulu_time = datetime.datetime.strptime(time,"%Y-%m-%dT%H:%M:%SZ")
    print("Zulu time:",zulu_time)
    

    Output:

    Zulu time: 2023-11-23 14:15:00