def new_linked_map(request, smapid, snapshot=None, template='maps/map_view.html'): """ The view that returns the map composer opened to the map with the given map ID. """ smap=StaticMap.objects.filter(id=smapid) DEFAULT_MAP_CONFIG, DEFAULT_BASE_LAYERS = default_map_config() m = Map() user = request.user m.owner = user m.title = 'GISMap_%s' % smap[0].title m.abstract = DEFAULT_ABSTRACT m.projection = "EPSG:900913" m.zoom = 0 m.center_x = 0 m.center_y = 0 m.save() smap.update(linked_map=m.id) if snapshot is None: config = m.viewer_json(request.user, *DEFAULT_BASE_LAYERS) else: config = snapshot_config(snapshot, m, request.user) return render_to_response(template, RequestContext(request, { 'config': json.dumps(config), 'map': m }))
def test_save_story_draft(self): """ Can save draft story """ user = User.objects.create_user(username='******', email='*****@*****.**', password='******') mapstory = MapStory() self.assertIsInstance(mapstory, MapStory) mapstory.title = "Test story" mapstory.owner = user mapstory.save() testMap = Map() testMap.story = mapstory testMap.zoom = 3 testMap.projection = "EPSG:900913" testMap.center_x = -7377090.47385893 testMap.center_y = 3463514.6256579063 testMap.owner = user testMap.save()
def create_from_layer_list(user, layers, title, abstract): newmap = Map() """Copied from maps.models and fixed """ newmap.owner = user newmap.title = title newmap.abstract = abstract newmap.projection = "EPSG:900913" newmap.zoom = 0 newmap.center_x = 0 newmap.center_y = 0 #bbox = None index = 0 incr_bbox = None DEFAULT_BASE_LAYERS = settings.MAP_BASELAYERS is_published = True if settings.RESOURCE_PUBLISHING: is_published = False newmap.is_published = is_published # Save the map in order to create an id in the database # used below for the maplayers. newmap.save() # Add background layers for layer in DEFAULT_BASE_LAYERS: logger.info("Adding baselayer %r", layer) maplayer = layer_from_viewer_config( MapLayer, layer, layer['source'], # source index) if not maplayer.group == 'background': logger.info("Skipping not base layer %r", layer) continue if 'name' not in layer or not layer['name']: logger.info("Unnamed base layer %r", layer) maplayer.name = 'UNNAMED BACKGROUND LAYER' maplayer.map = newmap maplayer.save() index += 1 # Add local layers for layer in layers: if not isinstance(layer, Layer): try: layer = Layer.objects.get(typename=layer) except ObjectDoesNotExist: raise Exception('Could not find layer with name %s' % layer) if not user.has_perm('base.view_resourcebase', obj=layer.resourcebase_ptr): # invisible layer, skip inclusion or raise Exception? raise Exception( 'User %s tried to create a map with layer %s without having premissions' % (user, layer)) ### Add required parameters for GXP lazy-loading # compute (incremental) bbox layer_bbox = layer.bbox if incr_bbox is None: incr_bbox = list(layer_bbox[0:4]) else: incr_bbox[0] = min(incr_bbox[0], layer_bbox[0]) incr_bbox[1] = max(incr_bbox[1], layer_bbox[1]) incr_bbox[2] = min(incr_bbox[2], layer_bbox[2]) incr_bbox[3] = max(incr_bbox[3], layer_bbox[3]) config = layer.attribute_config() config["title"] = layer.title config["queryable"] = True config[ "srs"] = layer.srid if layer.srid != "EPSG:4326" else "EPSG:900913" config["bbox"] = llbbox_to_mercator( [float(coord) for coord in incr_bbox]) #if layer.storeType == "remoteStore": #service = layer.service #maplayer = MapLayer(map=map_obj, #name=layer.typename, #ows_url=layer.ows_url, #layer_params=json.dumps(config), #visibility=True, #source_params=json.dumps({ #"ptype": service.ptype, #"remote": True, #"url": service.base_url, #"name": service.name})) #else: #maplayer = MapLayer( #map=map_obj, #name=layer.typename, #ows_url=layer.ows_url, #layer_params=json.dumps(config), #visibility=True #) MapLayer.objects.create(map=newmap, name=layer.typename, ows_url=layer.ows_url, stack_order=index, visibility=True, layer_params=json.dumps(config)) index += 1 # Set bounding box based on all layers extents. bbox = newmap.get_bbox_from_layers(newmap.local_layers) newmap.set_bounds_from_bbox(bbox) newmap.set_missing_info() # Save again to persist the zoom and bbox changes and # to generate the thumbnail. newmap.save() return newmap
def create_from_layer_list(user, layers, title, abstract): newmap = Map() """Copied from maps.models and fixed """ newmap.owner = user newmap.title = title newmap.abstract = abstract newmap.projection = "EPSG:900913" newmap.zoom = 0 newmap.center_x = 0 newmap.center_y = 0 #bbox = None index = 0 incr_bbox = None DEFAULT_BASE_LAYERS = settings.MAP_BASELAYERS is_published = True if settings.RESOURCE_PUBLISHING: is_published = False newmap.is_published = is_published # Save the map in order to create an id in the database # used below for the maplayers. newmap.save() # Add background layers for layer in DEFAULT_BASE_LAYERS: logger.info("Adding baselayer %r", layer) maplayer = layer_from_viewer_config(MapLayer, layer, layer['source'], # source index) if not maplayer.group == 'background': logger.info("Skipping not base layer %r", layer) continue if 'name' not in layer or not layer['name']: logger.info("Unnamed base layer %r", layer) maplayer.name = 'UNNAMED BACKGROUND LAYER' maplayer.map = newmap maplayer.save() index += 1 # Add local layers for layer in layers: if not isinstance(layer, Layer): try: layer = Layer.objects.get(typename=layer) except ObjectDoesNotExist: raise Exception( 'Could not find layer with name %s' % layer) if not user.has_perm( 'base.view_resourcebase', obj=layer.resourcebase_ptr): # invisible layer, skip inclusion or raise Exception? raise Exception( 'User %s tried to create a map with layer %s without having premissions' % (user, layer)) ### Add required parameters for GXP lazy-loading # compute (incremental) bbox layer_bbox = layer.bbox if incr_bbox is None: incr_bbox = list(layer_bbox[0:4]) else: incr_bbox[0] = min(incr_bbox[0], layer_bbox[0]) incr_bbox[1] = max(incr_bbox[1], layer_bbox[1]) incr_bbox[2] = min(incr_bbox[2], layer_bbox[2]) incr_bbox[3] = max(incr_bbox[3], layer_bbox[3]) config = layer.attribute_config() config["title"] = layer.title config["queryable"] = True config["srs"] = layer.srid if layer.srid != "EPSG:4326" else "EPSG:900913" config["bbox"] = llbbox_to_mercator([float(coord) for coord in incr_bbox]) #if layer.storeType == "remoteStore": #service = layer.service #maplayer = MapLayer(map=map_obj, #name=layer.typename, #ows_url=layer.ows_url, #layer_params=json.dumps(config), #visibility=True, #source_params=json.dumps({ #"ptype": service.ptype, #"remote": True, #"url": service.base_url, #"name": service.name})) #else: #maplayer = MapLayer( #map=map_obj, #name=layer.typename, #ows_url=layer.ows_url, #layer_params=json.dumps(config), #visibility=True #) MapLayer.objects.create( map=newmap, name=layer.typename, ows_url=layer.ows_url, stack_order=index, visibility=True, layer_params=json.dumps(config) ) index += 1 # Set bounding box based on all layers extents. bbox = newmap.get_bbox_from_layers(newmap.local_layers) newmap.set_bounds_from_bbox(bbox) newmap.set_missing_info() # Save again to persist the zoom and bbox changes and # to generate the thumbnail. newmap.save() return newmap