Example #1
0
 def add_mapbox_layer(self,
                      layer_name=False,
                      mapbox_json_path=False,
                      layer_icon='fa fa-globe',
                      is_basemap=False):
     if layer_name != False and mapbox_json_path != False:
         with open(mapbox_json_path) as data_file:
             data = json.load(data_file)
             with transaction.atomic():
                 for layer in data['layers']:
                     if 'source' in layer:
                         layer[
                             'source'] = layer['source'] + '-' + layer_name
                 for source_name, source_dict in data['sources'].iteritems(
                 ):
                     map_source = models.MapSource.objects.get_or_create(
                         name=source_name + '-' + layer_name,
                         source=source_dict)
                 map_layer = models.MapLayer(
                     name=layer_name,
                     layerdefinitions=data['layers'],
                     isoverlay=(not is_basemap),
                     icon=layer_icon)
                 try:
                     map_layer.save()
                 except IntegrityError as e:
                     print "Cannot save layer: {0} already exists".format(
                         layer_name)
Example #2
0
 def add_tileserver_layer(self,
                          layer_name=False,
                          mapnik_xml_path=False,
                          layer_icon='fa fa-globe',
                          is_basemap=False,
                          tile_config_path=False):
     if layer_name != False:
         config = None
         extension = "png"
         layer_type = "raster"
         tile_size = 256
         if mapnik_xml_path != False:
             path = os.path.abspath(mapnik_xml_path),
             config = {
                 "provider": {
                     "name": "mapnik",
                     "mapfile": os.path.abspath(mapnik_xml_path)
                 }
             }
             layer_list = [{
                 "id": layer_name,
                 "type": "raster",
                 "source": layer_name,
                 "minzoom": 0,
                 "maxzoom": 22
             }]
         elif tile_config_path != False:
             path = os.path.abspath(tile_config_path)
             with open(path) as content:
                 config_data = json.load(content)
             config = config_data["config"]
             layer_type = config_data["type"]
             layer_list = config_data["layers"]
             for layer in layer_list:
                 layer["source"] = layer_name
                 if layer_type == "vector":
                     layer["source-layer"] = layer_name
             if layer_type == "vector":
                 extension = "pbf"
                 tile_size = 512
         if config is not None:
             with transaction.atomic():
                 tileserver_layer = models.TileserverLayer(name=layer_name,
                                                           path=path,
                                                           config=config)
                 source_dict = {
                     "type":
                     layer_type,
                     "tiles": [("/tileserver/%s/{z}/{x}/{y}.%s") %
                               (layer_name, extension)],
                     "tileSize":
                     tile_size
                 }
                 try:
                     map_source = models.MapSource(name=layer_name,
                                                   source=source_dict)
                     map_layer = models.MapLayer(
                         name=layer_name,
                         layerdefinitions=layer_list,
                         isoverlay=(not is_basemap),
                         icon=layer_icon)
                     map_source.save()
                     map_layer.save()
                     tileserver_layer.map_layer = map_layer
                     tileserver_layer.map_source = map_source
                     tileserver_layer.save()
                 except IntegrityError as e:
                     print "Cannot save tile server layer: {0} already exists".format(
                         layer_name)