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 set_geonode_map(self, caller, serializer, map_obj=None, data=None): _map_name = serializer.validated_data['title'] or map_obj.title _map_title = serializer.validated_data['title'] or map_obj.title _map_abstract = serializer.validated_data.get( 'abstract', '') or '' if not hasattr(map_obj, 'abstract') else map_obj.abstract if data: try: data_blob = data.copy() _map_conf = dict(data) _map_conf["about"] = { "name": _map_name, "title": _map_title, "abstract": _map_abstract } _map_conf['sources'] = {} from geonode.layers.views import layer_detail _map_obj = data.pop('map', None) if _map_obj: _map_bbox = [] for _lyr in _map_obj['layers']: _lyr_context = {} _lyr_store = _lyr['store'] if 'store' in _lyr else None if not _lyr_store: try: _url = urlparse(_lyr['catalogURL']) _lyr_store = Layer.objects.get( uuid=parse_qs(_url.query)['id'][0]).store except Exception: try: _lyr_store = Layer.objects.get( alternate=_lyr['name'], remote_service__base_url=_lyr['url'] ).store except Exception: _lyr_store = None _lyr_name = f"{_lyr_store}:{_lyr['name']}" if _lyr_store else _lyr[ 'name'] try: # Retrieve the Layer Params back from GeoNode _gn_layer = layer_detail(caller.request, _lyr_name) if _gn_layer and _gn_layer.context_data: _context_data = json.loads( _gn_layer.context_data['viewer']) for _gn_layer_ctx in _context_data['map'][ 'layers']: if 'name' in _gn_layer_ctx and _gn_layer_ctx[ 'name'] == _lyr['name']: _lyr['store'] = _lyr_store if 'style' in _lyr: _lyr_context['style'] = _lyr[ 'style'] if 'capability' in _gn_layer_ctx: # Add selected style to capability in layer_params _gn_layer_ctx['capability'][ 'style'] = _lyr['style'] _lyr_context = _gn_layer_ctx _src_idx = _lyr_context['source'] _map_conf['sources'][ _src_idx] = _context_data[ 'sources'][_src_idx] except Http404: tb = traceback.format_exc() logger.debug(tb) except Exception: raise # Store ms2 layer idq if "id" in _lyr and _lyr["id"]: _lyr['extraParams'] = {"msId": _lyr["id"]} # Store the Capabilities Document into the Layer Params of GeoNode if _lyr_context: if 'ftInfoTemplate' in _lyr_context: _lyr['ftInfoTemplate'] = _lyr_context[ 'ftInfoTemplate'] if 'getFeatureInfo' in _lyr_context: _lyr['getFeatureInfo'] = _lyr_context[ 'getFeatureInfo'] if 'capability' in _lyr_context: _lyr['capability'] = _lyr_context['capability'] if 'bbox' in _lyr_context['capability']: _lyr_bbox = _lyr_context['capability'][ 'bbox'] if _map_obj['projection'] in _lyr_bbox: x0 = _lyr_bbox[ _map_obj['projection']]['bbox'][0] x1 = _lyr_bbox[ _map_obj['projection']]['bbox'][2] y0 = _lyr_bbox[ _map_obj['projection']]['bbox'][1] y1 = _lyr_bbox[ _map_obj['projection']]['bbox'][3] if len(_map_bbox) == 0: _map_bbox = [x0, x1, y0, y1] else: from geonode.utils import bbox_to_wkt from django.contrib.gis.geos import GEOSGeometry _l_wkt = bbox_to_wkt( x0, x1, y0, y1, srid=_map_obj['projection']) _m_wkt = bbox_to_wkt( _map_bbox[0], _map_bbox[2], _map_bbox[1], _map_bbox[3], srid=_map_obj['projection']) _map_srid = int( _map_obj['projection'][5:]) _l_poly = GEOSGeometry( _l_wkt, srid=_map_srid) _m_poly = GEOSGeometry( _m_wkt, srid=_map_srid).union(_l_poly) _map_bbox = _m_poly.extent if 'source' in _lyr_context: _source = _map_conf['sources'][ _lyr_context['source']] if 'remote' in _source and _source[ 'remote'] is True: _lyr['source'] = _lyr_context['source'] elif 'source' in _lyr: _map_conf['sources'][_lyr['source']] = {} event_type = None if is_analytics_enabled: event_type = EventType.EVENT_CHANGE if not map_obj: # Create a new GeoNode Map from geonode.maps.models import Map map_obj = Map(title=_map_title, abstract=_map_abstract, owner=caller.request.user, center_x=_map_obj['center']['x'], center_y=_map_obj['center']['y'], projection=_map_obj['projection'], zoom=_map_obj['zoom'], srid=_map_obj['projection'], resource_type="map") if 'bbox' in _map_obj: if hasattr(map_obj, 'bbox_polygon'): map_obj.bbox_polygon = BBOXHelper.from_xy( _map_obj['bbox']).as_polygon() else: map_obj.bbox_x0 = _map_obj['bbox'][0] map_obj.bbox_y0 = _map_obj['bbox'][1] map_obj.bbox_x1 = _map_obj['bbox'][2] map_obj.bbox_y1 = _map_obj['bbox'][3] elif hasattr(map_obj, 'bbox_polygon' ) and map_obj.bbox_polygon is None: # set the bbox_polygon to the obj and then to serializer instance map_obj.set_bounds_from_center_and_zoom( _map_obj['center']['x'], _map_obj['center']['y'], _map_obj['zoom']) if is_analytics_enabled: event_type = EventType.EVENT_CREATE else: map_obj.title = _map_title map_obj.abstract = _map_abstract if not map_obj.uuid: map_obj.uuid = str(uuid4()) map_obj.blob = data_blob # Update GeoNode Map _map_conf['map'] = _map_obj if is_analytics_enabled: register_event(caller.request, event_type, map_obj) serializer.instance = map_obj serializer.save() serializer.instance.update_from_viewer( _map_conf, context={'config': _map_conf}) return serializer except Exception as e: tb = traceback.format_exc() logger.error(tb) raise APIException(e) else: raise APIException("Map Configuration (data) is Mandatory!")
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