On 12/03/2009 12:20 PM, David Winslow wrote:
Hi all,
For a project I'm working on, I'd like to access the Attribution field
on some layers in a WMS capabilities document. I'd love to use OWSLib
(looks like it would be a lot easier to use than parsing the XML
directly), but when I try to retrieve the Attribution I just get an
empty string (or None if there is no attribution for the layer.) I
was expecting an object with the title, logo URL, and link for the
Attribution.
Digging into the code a bit I see that Attribution is parsed in
exactly the same way as Name and Title, so it will always be a
string. Would it be reasonable to change this to an object or
dictionary with the fields I mentioned above?
I would be happy to put together a patch if there's interest.
--
David Winslow
OpenGeo - http://opengeo.org/
Ignoring some test failures, here is a patch for the attribution
feature. Attribution is presented like so:
>>> wms['opengeo:poi'].attribution
{'url':
'http://svn.codehaus.org/geoserver/trunk/data/release/data/',
'logo_size': (353, 112), 'logo_url':
'http://geoserver.org/s/1518/25/0.1/_/download/resources/com.atlassian.confluence.themes.geoserver%3Ageoserver/chrome/geoserver-logo.png',
'title': 'GeoServer Sample Data'}
I am attaching the full patch.
--
David Winslow
OpenGeo - http://opengeo.org/
Index: owslib/wms.py
===================================================================
--- owslib/wms.py (revision 1506)
+++ owslib/wms.py (working copy)
@@ -308,7 +308,7 @@
self.parent = parent
if elem.tag != 'Layer':
raise ValueError('%s should be a Layer' % (elem,))
- for key in ('Name', 'Title', 'Attribution'):
+ for key in ('Name', 'Title'):
val = elem.find(key)
if val is not None:
setattr(self, key.lower(), val.text.strip())
@@ -333,7 +333,23 @@
elif self.parent:
if hasattr(self.parent, 'boundingBox'):
self.boundingBox = self.parent.boundingBox
-
+
+ attribution = elem.find('Attribution')
+ if attribution is not None:
+ self.attribution = dict()
+ title = attribution.find('Title')
+ url = attribution.find('OnlineResource')
+ logo = attribution.find('LogoURL')
+ 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('OnlineResource').attrib['{http://www.w3.org/1999/xlink}href']
+
+
+
b = elem.find('LatLonBoundingBox')
if b is not None:
self.boundingBoxWGS84 = (
Index: tests/wms_GeoServerCapabilities.txt
===================================================================
--- tests/wms_GeoServerCapabilities.txt (revision 0)
+++ tests/wms_GeoServerCapabilities.txt (revision 0)
@@ -0,0 +1,92 @@
+Imports
+
+ >>> from owslib.wms import WebMapService
+
+Fake a request to a WMS Server using saved doc from GeoServer, using a subset
+of the sample data distributed with the installer.
+http://localhost:8080/geoserver/wms?request=GetCapabilities
+
+ >>> xml = open_file('geoserver-wms-cap.xml', 'r').read()
+ >>> wms = WebMapService('url', version='1.1.1', xml=xml)
+
+Test capabilities
+
+ >>> wms.identification.type
+ 'OGC:WMS'
+
+ >>> wms.identification.title
+ 'My GeoServer WMS'
+
+ >>> wms.identification.abstract
+ 'This is a description of your Web Map Server.'
+
+ >>> wms.provider.url
+ 'http://localhost:8080/geoserver/wms'
+
+ >>> wms.identification.keywords
+ ['WFS', 'WMS', 'GEOSERVER']
+
+ >>> p = wms.provider.contact
+ >>> p.name
+ >>> p.email
+
+Test available content layers
+
+ >>> wms.contents.keys()
+ [None, 'opengeo:poi']
+
+
+ >>> [wms[layer].title for layer in wms.contents]
+ ['My GeoServer WMS', 'Points of Interest']
+
+
+Test single item accessor
+
+ >>> wms['opengeo:poi'].title
+ 'Points of Interest'
+
+ >>> wms['opengeo:poi'].boundingBox
+ (-74.012, 40.707999999999998, -74.001999999999995, 40.719999999999999,
'EPSG:4326')
+
+ >>> wms['opengeo:poi'].boundingBoxWGS84
+ (-74.012, 40.707999999999998, -74.001999999999995, 40.719999999999999)
+
+ >>> wms['opengeo:poi'].crsOptions
+ ['EPSG:4326']
+
+ >>> wms['opengeo:poi'].attribution
+ {'url': 'http://svn.codehaus.org/geoserver/trunk/data/release/data/',
'logo_size': (353, 112), 'logo_url':
'http://geoserver.org/s/1518/25/0.1/_/download/resources/com.atlassian.confluence.themes.geoserver%3Ageoserver/chrome/geoserver-logo.png',
'title': 'GeoServer Sample Data'}
+
+ >>> wms['opengeo:poi'].styles
+ {'point': {'legend':
'http://localhost:8080/geoserver/wms?request=GetLegendGraphic&format=image%2Fpng&width=20&height=20&layer=poi',
'title': 'A boring default style'}}
+
+Expect a KeyError for invalid names
+
+ >>> wms['utterly bogus'].title
+ Traceback (most recent call last):
+ ...
+ KeyError: 'No content named utterly bogus'
+
+Test operations
+
+ >>> [op.name for op in wms.operations]
+ ['GetCapabilities', 'GetMap', 'GetFeatureInfo', 'DescribeLayer',
'GetLegendGraphic']
+
+ >>> wms.getOperationByName('GetMap').methods
+ {'Get': {'url': 'http://localhost:8080/geoserver/wms?SERVICE=WMS&'}}
+
+ >>> wms.getOperationByName('GetMap').formatOptions
+ ['image/png', 'application/atom xml', 'application/atom+xml',
'application/openlayers', 'application/pdf', 'application/rss xml',
'application/rss+xml', 'application/vnd.google-earth.kml',
'application/vnd.google-earth.kml xml', 'application/vnd.google-earth.kml+xml',
'application/vnd.google-earth.kmz', 'application/vnd.google-earth.kmz xml',
'application/vnd.google-earth.kmz+xml', 'atom', 'image/geotiff',
'image/geotiff8', 'image/gif', 'image/jpeg', 'image/png8', 'image/svg',
'image/svg xml', 'image/svg+xml', 'image/tiff', 'image/tiff8', 'kml', 'kmz',
'openlayers', 'rss']
+
+Test exceptions
+
+ >>> wms.exceptions
+ ['application/vnd.ogc.se_xml', 'application/vnd.ogc.se_inimage']
+
+Lastly, test the getcapabilities method
+
+# >>> wms = WebMapService('http://wms.telascience.org/cgi-bin/ngBM_wms?',
version='1.1.1')
+# >>> xml = wms.getcapabilities().read()
+# >>> xml.find('<WMT_MS_Capabilities version="1.1.1">') > 0
+# True
+
Index: tests/geoserver-wms-cap.xml
===================================================================
--- tests/geoserver-wms-cap.xml (revision 0)
+++ tests/geoserver-wms-cap.xml (revision 0)
@@ -0,0 +1,174 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE WMT_MS_Capabilities SYSTEM
"http://localhost:8080/geoserver/schemas/wms/1.1.1/WMS_MS_Capabilities.dtd">
+<WMT_MS_Capabilities version="1.1.1" updateSequence="0">
+ <Service>
+ <Name>OGC:WMS</Name>
+ <Title>My GeoServer WMS</Title>
+ <Abstract>This is a description of your Web Map Server.</Abstract>
+ <KeywordList>
+ <Keyword>WFS</Keyword>
+ <Keyword>WMS</Keyword>
+ <Keyword>GEOSERVER</Keyword>
+ </KeywordList>
+ <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="simple" xlink:href="http://localhost:8080/geoserver/wms"/>
+ <ContactInformation>
+ <ContactPersonPrimary>
+ <ContactPerson/>
+ <ContactOrganization/>
+ </ContactPersonPrimary>
+ <ContactPosition/>
+ <ContactAddress>
+ <AddressType/>
+ <Address/>
+ <City/>
+ <StateOrProvince/>
+ <PostCode/>
+ <Country/>
+ </ContactAddress>
+ <ContactVoiceTelephone/>
+ <ContactFacsimileTelephone/>
+ <ContactElectronicMailAddress/>
+ </ContactInformation>
+ <Fees>NONE</Fees>
+ <AccessConstraints>NONE</AccessConstraints>
+ </Service>
+ <Capability>
+ <Request>
+ <GetCapabilities>
+ <Format>application/vnd.ogc.wms_xml</Format>
+ <DCPType>
+ <HTTP>
+ <Get>
+ <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="simple"
xlink:href="http://localhost:8080/geoserver/wms?SERVICE=WMS&"/>
+ </Get>
+ <Post>
+ <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="simple"
xlink:href="http://localhost:8080/geoserver/wms?SERVICE=WMS&"/>
+ </Post>
+ </HTTP>
+ </DCPType>
+ </GetCapabilities>
+ <GetMap>
+ <Format>image/png</Format>
+ <Format>application/atom xml</Format>
+ <Format>application/atom+xml</Format>
+ <Format>application/openlayers</Format>
+ <Format>application/pdf</Format>
+ <Format>application/rss xml</Format>
+ <Format>application/rss+xml</Format>
+ <Format>application/vnd.google-earth.kml</Format>
+ <Format>application/vnd.google-earth.kml xml</Format>
+ <Format>application/vnd.google-earth.kml+xml</Format>
+ <Format>application/vnd.google-earth.kmz</Format>
+ <Format>application/vnd.google-earth.kmz xml</Format>
+ <Format>application/vnd.google-earth.kmz+xml</Format>
+ <Format>atom</Format>
+ <Format>image/geotiff</Format>
+ <Format>image/geotiff8</Format>
+ <Format>image/gif</Format>
+ <Format>image/jpeg</Format>
+ <Format>image/png8</Format>
+ <Format>image/svg</Format>
+ <Format>image/svg xml</Format>
+ <Format>image/svg+xml</Format>
+ <Format>image/tiff</Format>
+ <Format>image/tiff8</Format>
+ <Format>kml</Format>
+ <Format>kmz</Format>
+ <Format>openlayers</Format>
+ <Format>rss</Format>
+ <DCPType>
+ <HTTP>
+ <Get>
+ <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="simple"
xlink:href="http://localhost:8080/geoserver/wms?SERVICE=WMS&"/>
+ </Get>
+ </HTTP>
+ </DCPType>
+ </GetMap>
+ <GetFeatureInfo>
+ <Format>text/plain</Format>
+ <Format>text/html</Format>
+ <Format>application/vnd.ogc.gml</Format>
+ <DCPType>
+ <HTTP>
+ <Get>
+ <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="simple"
xlink:href="http://localhost:8080/geoserver/wms?SERVICE=WMS&"/>
+ </Get>
+ <Post>
+ <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="simple"
xlink:href="http://localhost:8080/geoserver/wms?SERVICE=WMS&"/>
+ </Post>
+ </HTTP>
+ </DCPType>
+ </GetFeatureInfo>
+ <DescribeLayer>
+ <Format>application/vnd.ogc.wms_xml</Format>
+ <DCPType>
+ <HTTP>
+ <Get>
+ <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="simple"
xlink:href="http://localhost:8080/geoserver/wms?SERVICE=WMS&"/>
+ </Get>
+ </HTTP>
+ </DCPType>
+ </DescribeLayer>
+ <GetLegendGraphic>
+ <Format>image/png</Format>
+ <Format>image/jpeg</Format>
+ <Format>image/gif</Format>
+ <DCPType>
+ <HTTP>
+ <Get>
+ <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="simple"
xlink:href="http://localhost:8080/geoserver/wms?SERVICE=WMS&"/>
+ </Get>
+ </HTTP>
+ </DCPType>
+ </GetLegendGraphic>
+ </Request>
+ <Exception>
+ <Format>application/vnd.ogc.se_xml</Format>
+ <Format>application/vnd.ogc.se_inimage</Format>
+ </Exception>
+ <UserDefinedSymbolization SupportSLD="1" UserLayer="1" UserStyle="1"
RemoteWFS="1"/>
+ <Layer>
+ <Title>My GeoServer WMS</Title>
+ <Abstract>This is a description of your Web Map Server.</Abstract>
+ <!--Limited list of EPSG projections:-->
+ <SRS>EPSG:4326</SRS>
+ <LatLonBoundingBox minx="-74.012" miny="40.708" maxx="-74.002"
maxy="40.72"/>
+ <Layer queryable="1">
+ <Name>opengeo:poi</Name>
+ <Title>Points of Interest</Title>
+ <Abstract>Points of Interest in New York City</Abstract>
+ <KeywordList/>
+ <SRS>EPSG:4326</SRS>
+ <!--WKT definition of this CRS:
+GEOGCS["WGS 84",
+ DATUM["World Geodetic System 1984",
+ SPHEROID["WGS 84", 6378137.0, 298.257223563, AUTHORITY["EPSG","7030"]],
+ AUTHORITY["EPSG","6326"]],
+ PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]],
+ UNIT["degree", 0.017453292519943295],
+ AXIS["Geodetic longitude", EAST],
+ AXIS["Geodetic latitude", NORTH],
+ AUTHORITY["EPSG","4326"]]-->
+ <LatLonBoundingBox minx="-74.012" miny="40.708" maxx="-74.002"
maxy="40.72"/>
+ <BoundingBox SRS="EPSG:4326" minx="-74.012" miny="40.708"
maxx="-74.002" maxy="40.72"/>
+ <Attribution>
+ <Title>GeoServer Sample Data</Title>
+ <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="simple"
xlink:href="http://svn.codehaus.org/geoserver/trunk/data/release/data/"/>
+ <LogoURL height="112" width="353">
+ <Format>image/png;charset=UTF-8</Format>
+ <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="simple"
xlink:href="http://geoserver.org/s/1518/25/0.1/_/download/resources/com.atlassian.confluence.themes.geoserver%3Ageoserver/chrome/geoserver-logo.png"/>
+ </LogoURL>
+ </Attribution>
+ <Style>
+ <Name>point</Name>
+ <Title>A boring default style</Title>
+ <Abstract>A sample style that just prints out a purple
square</Abstract>
+ <LegendURL width="20" height="20">
+ <Format>image/png</Format>
+ <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:type="simple"
xlink:href="http://localhost:8080/geoserver/wms?request=GetLegendGraphic&format=image%2Fpng&width=20&height=20&layer=poi"/>
+ </LegendURL>
+ </Style>
+ </Layer>
+ </Layer>
+ </Capability>
+</WMT_MS_Capabilities>
_______________________________________________
Community mailing list
[email protected]
http://lists.gispython.org/mailman/listinfo/community