From be2c27ff86ab003daef1180e51faed1522b88bc8 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Thu, 12 Feb 2009 02:12:26 +0000 Subject: [PATCH] OGCServer: add support for load_map() within WMSFactory (thanks xcacou,theosys,and tmcw for early patches) (closes #129) --- CHANGELOG | 3 +++ bindings/python/mapnik/ogcserver/WMS.py | 33 +++++++++++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index aa641d759..69de6efd9 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -13,6 +13,9 @@ For a complete change history, see the SVN log. Current Version (0.6.0-dev, SVN trunk): --------------------------------------- + +- OGCServer: Added support for load_map(), allowing reading of XML styles and layers (r901) + - OGCServer: Enabled friendly html output when in debug mode (debug=1 in ogcserver.conf) (r899) - Filter parsing: Allow numbers in the filter field name. diff --git a/bindings/python/mapnik/ogcserver/WMS.py b/bindings/python/mapnik/ogcserver/WMS.py index 7b940e2ad..3630dffa4 100644 --- a/bindings/python/mapnik/ogcserver/WMS.py +++ b/bindings/python/mapnik/ogcserver/WMS.py @@ -19,11 +19,11 @@ # # $Id$ -from common import Version +from common import Version, copy from exceptions import OGCException, ServerConfigurationError from wms111 import ServiceHandler as ServiceHandler111 from wms130 import ServiceHandler as ServiceHandler130 -from mapnik import Style, Layer +from mapnik import Style, Map, load_map import re def ServiceHandlerFactory(conf, mapfactory, onlineresource, version): @@ -43,11 +43,34 @@ class BaseWMSFactory: self.styles = {} self.aggregatestyles = {} + def loadXML(self, xmlfile, strict=False): + tmp_map = Map(0,0) + load_map (tmp_map, xmlfile, strict) + for lyr in tmp_map.layers: + style_count = len(lyr.styles) + if style_count == 0: + raise ServerConfigurationError("Cannot register Layer '%s' without a style" % lyr.name) + elif style_count == 1: + style_obj = tmp_map.find_style(lyr.styles[0]) + style_name = lyr.styles[0] + if style_name not in self.aggregatestyles.keys() and style_name not in self.styles.keys(): + self.register_style(style_name, copy(style_obj)) + self.register_layer(copy(lyr), style_name, extrastyles=(style_name,)) + elif style_count > 1: + for style_name in lyr.styles: + style_obj = tmp_map.find_style(style_name) + self.register_style(style_name, copy(style_obj)) + del style_obj + aggregates = tuple([sty for sty in lyr.styles]) + aggregates_name = '%s_aggregates' % lyr.name + self.register_aggregate_style(aggregates_name,aggregates) + self.register_layer(copy(lyr), aggregates_name, extrastyles=aggregates) + def register_layer(self, layer, defaultstyle, extrastyles=()): layername = layer.name if not layername: raise ServerConfigurationError('Attempted to register an unnamed layer.') - if not re.match('^\+init=epsg:\d+$', layer.srs): + if not re.match('^\+init=epsg:\d+$', layer.srs) and not re.match('^\+proj=.*$', layer.srs): raise ServerConfigurationError('Attempted to register a layer without an epsg projection defined.') if defaultstyle not in self.styles.keys() + self.aggregatestyles.keys(): raise ServerConfigurationError('Attempted to register a layer with an non-existent default style.') @@ -68,7 +91,7 @@ class BaseWMSFactory: if not name: raise ServerConfigurationError('Attempted to register a style without providing a name.') if name in self.aggregatestyles.keys() or name in self.styles.keys(): - raise ServerConfigurationError('Attempted to register an aggregate style with a name already in use.') + raise ServerConfigurationError("Attempted to register a style with a name already in use: '%s'" % name) if not isinstance(style, Style): raise ServerConfigurationError('Bad style object passed to register_style() for style "%s".' % name) self.styles[name] = style @@ -92,4 +115,4 @@ class BaseWMSFactory: for layer in self.layers.values(): for style in list(layer.styles) + list(layer.wmsextrastyles): if style not in self.styles.keys() + self.aggregatestyles.keys(): - raise ServerConfigurationError('Layer "%s" refers to undefined style "%s".' % (layer.name(), style)) + raise ServerConfigurationError('Layer "%s" refers to undefined style "%s".' % (layer.name, style)) \ No newline at end of file