source: DPPP/kml/csml2kml/python/csml2kml/csml2kml/scripts/wms2kml.py @ 3541

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/DPPP/kml/csml2kml/python/csml2kml/csml2kml/scripts/wms2kml.py@3541
Revision 3541, 2.5 KB checked in by mkochan, 14 years ago (diff)

Created a scripts directory and the script "wms2kml.py".

Line 
1import sys
2import getopt
3from cElementTree import ElementTree, XML
4
5import utils
6from WMSCapabilities import *
7
8# -------------------------------------------------------------------------------------------------------------------------
9
10def usage():
11    print '''
12| wms2kml.py, script for converting WMS GetCapabilities response into KML.
13|
14| The script will generate a directory/KML file structure reflecting the structure of the <wms:Layer>
15| elements in the GetCapabilities response.
16|
17| Usage: python wms2kml.py -c <config_file> -o <output root directory> --break_into_kml <break_depth>
18'''
19
20def process_args():
21    global confFilename, rootOutputDirectory, maxDirDepth
22    try:
23        (opts, args) = getopt.getopt(sys.argv[1:], 'c:o:', ['break_into_kml='])
24        for (o, val) in opts:
25            if o == '-c':
26                confFilename = val
27            elif o == '-o':
28                rootOutputDirectory = val
29            elif o == '--break_into_kml':
30                maxDirDepth = int(val)
31
32        # Test for existence of these parameters:
33        confFilename
34        rootOutputDirectory
35        maxDirDepth
36
37    except (getopt.GetoptError, UnboundLocalError):
38        usage()
39        sys.exit(2)
40
41# -------------------------------------------------------------------------------------------------------------------------
42
43process_args()
44           
45# Read in the config file, which contains inforamation about how to run WMS requests, and what URL to use.
46configElementTree = ElementTree()
47configElementTree.parse(confFilename)
48wmsRequestConfigElement = configElementTree.getroot().find('WMSRequest')
49
50# Run the 'GetCapabilities' request against the WMS and retrieve the response.
51print 'Retrieving data from "' + wmsRequestConfigElement.find('URL').text + '"...'
52wmsCapabilitiesUrl = wmsRequestConfigElement.find('URL').text + '?request=GetCapabilities'
53wmsCapabilitiesElement = XML( utils.wget(wmsCapabilitiesUrl) )
54
55# Create and parse in a WMSCapabilities object which wraps the response.
56print 'Parsing the GetCapabilities response...'
57wmsCapabilities = WMSCapabilities()
58wmsCapabilities.parseXML(wmsCapabilitiesElement)
59
60# Now, wmsCapabilities.topWmsLayer is a WMSLayer object which represents the whole structure of <wms:Layer> elements.
61topWmsLayer = wmsCapabilities.topWmsLayer
62
63# We use a WMSLayersConvertor object to convert topWmsLayer into a structure of directories and KML documents.
64print 'Writing files...'
65wmsLayersConvertor = WMSLayersConvertor(topWmsLayer, wmsRequestConfigElement, rootOutputDirectory, maxDirDepth)
66wmsLayersConvertor.convert()
Note: See TracBrowser for help on using the repository browser.