예제 #1
0
파일: WMS.py 프로젝트: bnordgren/OGCServer
 def register_layer(self, layer, defaultstyle, extrastyles=()):
     layername = layer.name
     if not layername:
         raise ServerConfigurationError('Attempted to register an unnamed layer.')
     if not layer.wms_srs and 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.')
     layer.wmsdefaultstyle = defaultstyle
     if isinstance(extrastyles, tuple):
         for stylename in extrastyles:
             if type(stylename) == type(''):
                 if stylename not in self.styles.keys() + self.aggregatestyles.keys():
                     raise ServerConfigurationError('Attempted to register a layer with an non-existent extra style.')
             else:
                 ServerConfigurationError('Attempted to register a layer with an invalid extra style name.')
         layer.wmsextrastyles = extrastyles
     else:
         raise ServerConfigurationError('Layer "%s" was passed an invalid list of extra styles.  List must be a tuple of strings.' % layername)
     layerproj = common.Projection(layer.srs)
     env = getattr(layer, 'maximum_extent', layer.envelope())
     llp = layerproj.inverse(Coord(env.minx, env.miny))
     urp = layerproj.inverse(Coord(env.maxx, env.maxy))
     if self.latlonbb is None:
         self.latlonbb = Box2d(llp, urp)
     else:
         self.latlonbb.expand_to_include(Box2d(llp, urp))
     self.ordered_layers.append(layer)
     self.layers[layername] = layer
예제 #2
0
파일: WMS.py 프로젝트: bnordgren/OGCServer
    def loadXML(self, xmlfile=None, strict=False, xmlstring='', basepath=''):
        """Loads and stores a complete map definition in leiu of a set of layers.
        Stores an empty style of the same name, which is the only option for rendering.
        The map will be rendered as prescribed in the XML."""
        config = ConfigParser.SafeConfigParser()
        map_wms_srs = None
        if self.configpath:
            config.readfp(open(self.configpath))

            if config.has_option('map', 'wms_srs'):
                map_wms_srs = config.get('map', 'wms_srs')

        tmp_map = Map(0,0)
        if xmlfile:
            load_map(tmp_map, xmlfile, strict)
        elif xmlstring:
            load_map_from_string(tmp_map, xmlstring, strict, basepath)
        else:
            raise ServerConfigurationError("Mapnik configuration XML is not specified - 'xmlfile' and 'xmlstring' variables are empty.\
Please set one of this variables to load mapnik map object.")
        # parse map level attributes
        if tmp_map.background:
            self.map_attributes['bgcolor'] = tmp_map.background
        if tmp_map.buffer_size:
            self.map_attributes['buffer_size'] = tmp_map.buffer_size

        if xmlfile is None : 
            # Map objects have no name, so just call it default.
            layer_name = 'default'
        else : 
            # The layer name is the basename of the xml file or the 
            # whole file name
            layer_name = xmlfile
            fname_match = re.match('([A-Za-z0-9_\-\.]+).xml', xmlfile)
            if fname_match is not None : 
                layer_name = fname_match.group(1)
            else : 
                layer_name = xmlfile

        style_name = layer_name
        style_obj = Style()

        # Make the map have attributes expected of layers
        tmp_map.name = layer_name
        map_p = common.Projection(tmp_map.srs)
        if map_wms_srs is None : 
            tmp_map.wms_srs = map_p.epsgstring()
        else : 
            tmp_map.wms_srs = map_wms_srs
        tmp_map.queryable = False

        # set map extent from config file.
        geog_coords = config.get('map','wms_extent').split(',')
        geog_box = Box2d(float(geog_coords[0]), float(geog_coords[1]), float(geog_coords[2]), float(geog_coords[3]))
        proj_box = geog_box.forward(map_p)
        tmp_map.zoom_to_box(proj_box)
        tmp_map.maximum_extent = proj_box

        self.register_style(style_name, style_obj)
        self.register_layer(tmp_map, style_name) 
예제 #3
0
 def register_layer(self, layer, defaultstyle, extrastyles=()):
     layername = layer.name
     #差错机制,检测相关参数是否有效
     if not layername:
         #ServerConfigurationError为从ogcserver.exceptions中导入的方法,执行内容为空
         raise ServerConfigurationError(
             'Attempted to register an unnamed layer.')
     if not layer.wms_srs and 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.'
         )
     layer.wmsdefaultstyle = defaultstyle
     # 判断是否为tuple类型,('a',)为tuple类型,aggregates也为tuple类型
     # type(('a',))输出为 tuple
     if isinstance(extrastyles, tuple):
         #
         for stylename in extrastyles:
             # 如果stylename类型为str
             if type(stylename) == type(''):
                 if stylename not in self.styles.keys(
                 ) + self.aggregatestyles.keys():
                     raise ServerConfigurationError(
                         'Attempted to register a layer with an non-existent extra style.'
                     )
             else:
                 ServerConfigurationError(
                     'Attempted to register a layer with an invalid extra style name.'
                 )
         # wmsextrastyles????
         layer.wmsextrastyles = extrastyles
     else:
         raise ServerConfigurationError(
             'Layer "%s" was passed an invalid list of extra styles.  List must be a tuple of strings.'
             % layername)
     # 调用common中Projection
     layerproj = common.Projection(layer.srs)
     env = layer.envelope()
     llp = layerproj.inverse(Coord(env.minx, env.miny))
     urp = layerproj.inverse(Coord(env.maxx, env.maxy))
     if self.latlonbb is None:
         self.latlonbb = Envelope(llp, urp)
     else:
         self.latlonbb.expand_to_include(Envelope(llp, urp))
     self.ordered_layers.append(layer)
     self.layers[layername] = layer