def GetFeatureInfo(self, params, querymethodname='query_point'): m = self._buildMap(params) if params['info_format'] == 'text/plain': writer = TextFeatureInfo() elif params['info_format'] == 'text/xml': writer = XMLFeatureInfo() if params['query_layers'] and params['query_layers'][0] == '__all__': for layerindex, layer in enumerate(m.layers): featureset = getattr(m, querymethodname)(layerindex, params['i'], params['j']) features = featureset.features if features: writer.addlayer(layer.name) for feat in features: writer.addfeature() for prop in feat.properties: writer.addattribute(prop[0], prop[1]) else: for layerindex, layername in enumerate(params['query_layers']): if layername in params['layers']: if m.layers[layerindex].queryable: featureset = getattr(m, querymethodname)(layerindex, params['i'], params['j']) features = featureset.features if features: writer.addlayer(m.layers[layerindex].name) for feat in features: writer.addfeature() for prop in feat.properties: writer.addattribute(prop[0], prop[1]) else: raise OGCException('Requested query layer "%s" is not marked queryable.' % layername, 'LayerNotQueryable') else: raise OGCException('Requested query layer "%s" not in the LAYERS parameter.' % layername) return Response(params['info_format'], str(writer))
def processParameters(self, requestname, params): finalparams = {} for paramname, paramdef in self.SERVICE_PARAMS[requestname].items(): if paramname not in params.keys() and paramdef.mandatory: raise OGCException( 'Mandatory parameter "%s" missing from request.' % paramname) elif paramname in params.keys(): try: params[paramname] = paramdef.cast(params[paramname]) except OGCException: raise except: raise OGCException( 'Invalid value "%s" for parameter "%s".' % (params[paramname], paramname)) if paramdef.allowedvalues and params[ paramname] not in paramdef.allowedvalues: if not paramdef.fallback: raise OGCException( 'Parameter "%s" has an illegal value.' % paramname) else: finalparams[paramname] = paramdef.default else: finalparams[paramname] = params[paramname] elif not paramdef.mandatory and paramdef.default: finalparams[paramname] = paramdef.default return finalparams
def process(self, req): reqparams = lowerparams(req.params) onlineresource = 'http://%s:%s%s?' % (req.environ['SERVER_NAME'], req.environ['SERVER_PORT'], req.environ['SCRIPT_NAME']) if not reqparams.has_key('request'): raise OGCException('Missing request parameter.') request = reqparams['request'] del reqparams['request'] if request == 'GetCapabilities' and not reqparams.has_key('service'): raise OGCException('Missing service parameter.') if request in ['GetMap', 'GetFeatureInfo']: service = 'WMS' else: service = reqparams['service'] if reqparams.has_key('service'): del reqparams['service'] try: mapnikmodule = __import__('mapnik2.ogcserver.' + service) except: raise OGCException('Unsupported service "%s".' % service) ServiceHandlerFactory = getattr(mapnikmodule.ogcserver, service).ServiceHandlerFactory servicehandler = ServiceHandlerFactory(self.conf, self.mapfactory, onlineresource, reqparams.get('version', None)) if reqparams.has_key('version'): del reqparams['version'] if request not in servicehandler.SERVICE_PARAMS.keys(): raise OGCException('Operation "%s" not supported.' % request, 'OperationNotSupported') ogcparams = servicehandler.processParameters(request, reqparams) try: requesthandler = getattr(servicehandler, request) except: raise OGCException('Operation "%s" not supported.' % request, 'OperationNotSupported') response = requesthandler(ogcparams) req.set_header('Content-Type', response.content_type) req.set_header('Content-Length', str(len(response.content))) req.write(response.content)
def __call__(self, environ, start_response): reqparams = {} for key, value in parse_qs(environ['QUERY_STRING'], True).items(): reqparams[key.lower()] = value[0] onlineresource = 'http://%s:%s%s?' % (environ['SERVER_NAME'], environ['SERVER_PORT'], environ['PATH_INFO']) try: if not reqparams.has_key('request'): raise OGCException('Missing request parameter.') request = reqparams['request'] del reqparams['request'] if request == 'GetCapabilities' and not reqparams.has_key( 'service'): raise OGCException('Missing service parameter.') if request in ['GetMap', 'GetFeatureInfo']: service = 'WMS' else: service = reqparams['service'] if reqparams.has_key('service'): del reqparams['service'] try: mapnikmodule = __import__('mapnik2.ogcserver.' + service) except: raise OGCException('Unsupported service "%s".' % service) ServiceHandlerFactory = getattr(mapnikmodule.ogcserver, service).ServiceHandlerFactory servicehandler = ServiceHandlerFactory( self.conf, self.mapfactory, onlineresource, reqparams.get('version', None)) if reqparams.has_key('version'): del reqparams['version'] if request not in servicehandler.SERVICE_PARAMS.keys(): raise OGCException('Operation "%s" not supported.' % request, 'OperationNotSupported') ogcparams = servicehandler.processParameters(request, reqparams) try: requesthandler = getattr(servicehandler, request) except: raise OGCException('Operation "%s" not supported.' % request, 'OperationNotSupported') response = requesthandler(ogcparams) except: version = reqparams.get('version', None) if not version: version = Version('1.3.0') else: version = Version(version) if version >= '1.3.0': eh = ExceptionHandler130(self.debug) else: eh = ExceptionHandler111(self.debug) response = eh.getresponse(reqparams) response_headers = [('Content-Type', response.content_type), ('Content-Length', str(len(response.content)))] if self.max_age: response_headers.append(('Cache-Control', max_age)) start_response('200 OK', response_headers) yield response.content
def __call__(self, crsstring): if not re.match('^[A-Z]{3,5}:\d+$', crsstring): raise OGCException('Invalid format for the CRS parameter: %s' % crsstring, 'InvalidCRS') crsparts = crsstring.split(':') if crsparts[0] in self.allowednamespaces: return CRS(crsparts[0], crsparts[1]) else: raise OGCException('Invalid CRS Namespace: %s' % crsparts[0], 'InvalidCRS')
def __init__(self, version): version = version.split('.') if len(version) != 3: raise OGCException('Badly formatted version number.') try: version = map(int, version) except: raise OGCException('Badly formatted version number.') self.version = version
def __call__(self, apacheReq): try: reqparams = util.FieldStorage(apacheReq, keep_blank_values=1) if not reqparams: eh = ExceptionHandler130(self.debug) response = eh.getresponse(reqparams) apacheReq.content_type = response.content_type else: reqparams = lowerparams(reqparams) port = apacheReq.connection.local_addr[1] onlineresource = 'http://%s:%s%s?' % ( apacheReq.hostname, port, apacheReq.subprocess_env['SCRIPT_NAME']) if not reqparams.has_key('request'): raise OGCException('Missing Request parameter.') request = reqparams['request'] del reqparams['request'] if request == 'GetCapabilities' and not reqparams.has_key( 'service'): raise OGCException('Missing service parameter.') if request in ['GetMap', 'GetFeatureInfo']: service = 'WMS' else: service = reqparams['service'] if reqparams.has_key('service'): del reqparams['service'] try: mapnikmodule = __import__('mapnik2.ogcserver.' + service) except: raise OGCException('Unsupported service "%s".' % service) ServiceHandlerFactory = getattr(mapnikmodule.ogcserver, service).ServiceHandlerFactory servicehandler = ServiceHandlerFactory( self.conf, self.mapfactory, onlineresource, reqparams.get('version', None)) if reqparams.has_key('version'): del reqparams['version'] if request not in servicehandler.SERVICE_PARAMS.keys(): raise OGCException( 'Operation "%s" not supported.' % request, 'OperationNotSupported') # Get parameters and pass to WMSFactory in custom "setup" method ogcparams = servicehandler.processParameters( request, reqparams) try: requesthandler = getattr(servicehandler, request) except: raise OGCException( 'Operation "%s" not supported.' % request, 'OperationNotSupported') response = requesthandler(ogcparams) apacheReq.content_type = response.content_type apacheReq.status = apache.HTTP_OK except Exception, E: return self.traceback(apacheReq, E)
def GetMap(self, params): if params['width'] > int(self.conf.get( 'service', 'maxwidth')) or params['height'] > int( self.conf.get('service', 'maxheight')): raise OGCException( 'Requested map size exceeds limits set by this server.') if str(params['crs']) != str(self.crs): raise OGCException( 'Unsupported CRS requested. Must be "%s" and not "%s".' % (self.crs, params['crs']), 'InvalidCRS') return WMSBaseServiceHandler.GetMap(self, params)
def GetMap(self, params): if params['width'] > int(self.conf.get( 'service', 'maxwidth')) or params['height'] > int( self.conf.get('service', 'maxheight')): raise OGCException( 'Requested map size exceeds limits set by this server.') return WMSBaseServiceHandler.GetMap(self, params)
def ColorFactory(colorstring): if re.match('^0x[a-fA-F0-9]{6}$', colorstring): return Color(eval('0x' + colorstring[2:4]), eval('0x' + colorstring[4:6]), eval('0x' + colorstring[6:8])) else: raise OGCException( 'Invalid color value. Must be of format "0xFFFFFF".')
def GetMap(self, params): if params['bbox'][0] >= params['bbox'][2]: raise OGCException( "BBOX values don't make sense. minx is greater than maxx.") if params['bbox'][1] >= params['bbox'][3]: raise OGCException( "BBOX values don't make sense. miny is greater than maxy.") if params.has_key('styles') and len(params['styles']) != len( params['layers']): raise OGCException('STYLES length does not match LAYERS length.') m = Map(params['width'], params['height']) if params.has_key('transparent') and params['transparent'] == 'FALSE': m.background = params['bgcolor'] else: m.background = Color(0, 0, 0, 0) maplayers = self.mapfactory.layers mapstyles = self.mapfactory.styles for layername in params['layers']: try: layer = maplayers[layername] except KeyError: raise OGCException('Layer "%s" not defined.' % layername, 'LayerNotDefined') for stylename in layer.styles: if stylename in mapstyles.keys(): m.append_style(stylename, mapstyles[stylename]) else: raise ServerConfigurationError( 'Layer "%s" refers to non-existent style "%s".' % (layername, stylename)) m.layers.append(layer) m.zoom_to_box( Envelope(params['bbox'][0], params['bbox'][1], params['bbox'][2], params['bbox'][3])) im = Image(params['width'], params['height']) render(m, im) im = fromstring('RGBA', (params['width'], params['height']), rawdata(im)) fh = StringIO() im.save(fh, PIL_TYPE_MAPPING[params['format']], quality=100) fh.seek(0) return Response(params['format'], fh.read())
def GetFeatureInfo(self, params, querymethodname='query_point'): m = self._buildMap(params) output = '' for layerindex, layername in enumerate(params['query_layers']): if layername in params['layers']: if m.layers[layerindex].queryable: for feature in getattr(m, querymethodname)(layerindex, params['i'], params['j']): output += '[%s]\n' % m.layers[layerindex].name for prop in feature.properties: output += '%s=%s\n' % (prop.key(), prop.data()) else: raise OGCException( 'Requested query layer "%s" is not marked queryable.' % layername, 'LayerNotQueryable') else: raise OGCException( 'Requested query layer "%s" not in the LAYERS parameter.' % layername) return Response('text/plain', output)
def _buildMap(self, params): if str(params['crs']) not in self.allowedepsgcodes: raise OGCException('Unsupported CRS "%s" requested.' % str(params['crs']).upper(), 'InvalidCRS') if params['bbox'][0] >= params['bbox'][2]: raise OGCException("BBOX values don't make sense. minx is greater than maxx.") if params['bbox'][1] >= params['bbox'][3]: raise OGCException("BBOX values don't make sense. miny is greater than maxy.") if params.has_key('styles') and len(params['styles']) != len(params['layers']): raise OGCException('STYLES length does not match LAYERS length.') m = Map(params['width'], params['height'], '+init=%s' % params['crs']) if params.has_key('transparent') and params['transparent'] == 'FALSE': if params['bgcolor']: m.background = params['bgcolor'] else: m.background = Color(0, 0, 0, 0) maplayers = self.mapfactory.layers orderedmaplayers = self.mapfactory.ordered_layers mapstyles = self.mapfactory.styles mapaggregatestyles = self.mapfactory.aggregatestyles # a non WMS spec way of requesting all layers if params['layers'] and params['layers'][0] == '__all__': for layername in orderedmaplayers: layer = copy_layer(layername) reqstyle = layer.wmsdefaultstyle if reqstyle in mapaggregatestyles.keys(): for stylename in mapaggregatestyles[reqstyle]: layer.styles.append(stylename) else: layer.styles.append(reqstyle) for stylename in layer.styles: if stylename in mapstyles.keys(): m.append_style(stylename, mapstyles[stylename]) m.layers.append(layer) else: for layerindex, layername in enumerate(params['layers']): try: layer = copy_layer(maplayers[layername]) except KeyError: raise OGCException('Layer "%s" not defined.' % layername, 'LayerNotDefined') try: reqstyle = params['styles'][layerindex] except IndexError: reqstyle = '' if reqstyle and reqstyle not in layer.wmsextrastyles: raise OGCException('Invalid style "%s" requested for layer "%s".' % (reqstyle, layername), 'StyleNotDefined') if not reqstyle: reqstyle = layer.wmsdefaultstyle if reqstyle in mapaggregatestyles.keys(): for stylename in mapaggregatestyles[reqstyle]: layer.styles.append(stylename) else: layer.styles.append(reqstyle) for stylename in layer.styles: if stylename in mapstyles.keys(): m.append_style(stylename, mapstyles[stylename]) else: raise ServerConfigurationError('Layer "%s" refers to non-existent style "%s".' % (layername, stylename)) m.layers.append(layer) m.zoom_to_box(Box2d(params['bbox'][0], params['bbox'][1], params['bbox'][2], params['bbox'][3])) return m
def GetMap(self, params): if str(params['srs']) != str(self.crs): raise OGCException( 'Unsupported SRS requested. Must be "%s" and not "%s".' % (self.crs, params['crs']), 'InvalidCRS') return WMSBaseServiceHandler.GetMap(self, params)