Ejemplo n.º 1
0
    def __call__(self, environ, start_response):
        resp = None
        req = Request(environ)

        if self.cors_origin:
            orig_start_response = start_response
            def start_response(status, headers, exc_info=None):
                headers.append(('Access-control-allow-origin', self.cors_origin))
                return orig_start_response(status, headers, exc_info)

        with local_base_config(self.base_config):
            match = self.handler_path_re.match(req.path)
            if match:
                handler_name = match.group(1)
                if handler_name in self.handlers:
                    try:
                        resp = self.handlers[handler_name].handle(req)
                    except Exception:
                        if self.base_config.debug_mode:
                            raise
                        else:
                            log_wsgiapp.fatal('fatal error in %s for %s?%s',
                                handler_name, environ.get('PATH_INFO'), environ.get('QUERY_STRING'), exc_info=True)
                            import traceback
                            traceback.print_exc(file=environ['wsgi.errors'])
                            resp = Response('internal error', status=500)
            if resp is None:
                if req.path in ('', '/'):
                    resp = self.welcome_response(req.script_url)
                else:
                    resp = Response('not found', mimetype='text/plain', status=404)
            return resp(environ, start_response)
Ejemplo n.º 2
0
def mapit(mapproxy_conf,
          layername,
          outputmimeformat,
          lonlatbbox,
          imagesize,
          outputfilename=None,
          time=None):
    """
    This automates the creation of a map image.
    :param mapproxy_conf: The mapproxy in-memory configuration dictionary.
    :param layername: The name of the layer to image.
    :param outputmimeformat: The mime format (i.e. image/png, image/jpeg) of the output
    :param lonlatbbox: The bounding box as a tuple, (west, south, east, north)
    :param imagesize: The images size as a tuple, (width, height)
    :param outputfilename: An optional output filename, if None, then no file will be written.
    :param time: An optional time string for layers that have the time dimension
    :return: The binary data for the image generated.
    """
    if time is None:
        time = ''
    mapproxy_conf = copy.deepcopy(mapproxy_conf)  # dup it for replacing time
    for source in mapproxy_conf['sources']:
        mapproxy_conf['sources'][source]['url'] = mapproxy_conf['sources'][
            source]['url'].replace("{Time}", time)

    transparent = 'false'
    if 'png' in outputmimeformat.lower():
        transparent = 'true'

    querystring = "LAYERS=%s&FORMAT=%s&SRS=EPSG:4326&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&TRANSPARENT=%s&STYLES=&BBOX=%f,%f,%f,%f&WIDTH=%d&HEIGHT=%d" % (
        layername, outputmimeformat, transparent, lonlatbbox[0], lonlatbbox[1],
        lonlatbbox[2], lonlatbbox[3], imagesize[0], imagesize[1])

    conf = ProxyConfiguration(mapproxy_conf,
                              conf_base_dir='',
                              seed=False,
                              renderd=False)

    services = conf.configured_services()

    myreq = {
        'QUERY_STRING': querystring,
        'SERVER_NAME': '',
        'SERVER_PORT': '',
        'wsgi.url_scheme': '',
    }

    response = services[0].handle(Request(myreq))
    data = response.data

    if outputfilename:
        f = open(outputfilename, "w")
        f.write(data)
    return data
Ejemplo n.º 3
0
 def __call__(self, environ, start_response):
     req = Request(environ)
     return self.handle(req)(environ, start_response)