def update_from_viewer(self, conf, context=None): """ Update this Map's details by parsing a JSON object as produced by a GXP Viewer. This method automatically persists to the database! """ template_name = hookset.update_from_viewer(conf, context=context) conf = context['config'] self.title = conf['title'] if 'title' in conf else conf['about']['title'] self.abstract = conf['abstract'] if 'abstract' in conf else conf['about']['abstract'] center = conf['map']['center'] if 'center' in conf['map'] else settings.DEFAULT_MAP_CENTER self.zoom = conf['map']['zoom'] if 'zoom' in conf['map'] else settings.DEFAULT_MAP_ZOOM self.center_x = center['x'] if isinstance(center, dict) else center[0] self.center_y = center['y'] if isinstance(center, dict) else center[1] if 'bbox' not in conf['map']: self.set_bounds_from_center_and_zoom( self.center_x, self.center_y, self.zoom) else: # Must be in the form : [x0, x1, y0, y1] self.set_bounds_from_bbox(conf['map']['bbox'], conf['map']['projection']) if self.projection is None or self.projection == '': self.projection = conf['map']['projection'] if self.uuid is None or self.uuid == '': self.uuid = str(uuid.uuid1()) def source_for(layer): try: return conf["sources"][layer["source"]] except BaseException: if 'url' in layer: return {'url': layer['url']} else: return {} layers = [l for l in conf["map"]["layers"]] layer_names = set([l.alternate for l in self.local_layers]) self.layer_set.all().delete() self.keywords.add(*conf['map'].get('keywords', [])) for ordering, layer in enumerate(layers): self.layer_set.add( layer_from_viewer_config( self.id, MapLayer, layer, source_for(layer), ordering )) self.save() if layer_names != set([l.alternate for l in self.local_layers]): map_changed_signal.send_robust(sender=self, what_changed='layers') return template_name
def update_from_viewer(self, conf): """ Update this Map's details by parsing a JSON object as produced by a GXP Viewer. This method automatically persists to the database! """ if isinstance(conf, basestring): conf = json.loads(conf) self.title = conf['about']['title'] self.abstract = conf['about']['abstract'] self.viewer_playbackmode = conf['viewer_playbackmode'] or 'Instant' self.set_bounds_from_center_and_zoom( conf['map']['center'][0], conf['map']['center'][1], conf['map']['zoom']) self.projection = conf['map']['projection'] if self.uuid is None or self.uuid == '': self.uuid = str(uuid.uuid1()) def source_for(layer): if 'sources' in conf: return conf["sources"][layer["source"]] else: # TODO: This is a work in progress will refactor # Handle special case for OL3 config model in storytools as source and sources may not available if layer['type'] == 'WMS': return {"restUrl": "/gs/rest", "title": "", "baseParams": {"VERSION": "1.1.1", "REQUEST": "GetCapabilities", "TILED": 'true', "SERVICE": "WMS"}, "ptype": "gxp_wmscsource", "id": "local"} else: return layer["source"] layers = [l for l in conf["map"]["layers"]] layer_names = set([l.alternate for l in self.local_layers]) for layer in self.layer_set.all(): layer.delete() self.keywords.add(*conf['map'].get('keywords', [])) for ordering, layer in enumerate(layers): self.layer_set.add( layer_from_viewer_config( MapLayer, layer, source_for(layer), ordering )) self.chapter_index = conf['chapter_index'] story_id = conf.get('story_id',0) story_obj = MapStory.objects.get(id=story_id) self.story = story_obj self.save() if layer_names != set([l.alternate for l in self.local_layers]): map_changed_signal.send_robust(sender=self, what_changed='layers')
def update_from_viewer(self, conf, context=None): """ Update this Map's details by parsing a JSON object as produced by a GXP Viewer. This method automatically persists to the database! """ template_name = hookset.update_from_viewer(conf, context=context) conf = context['config'] self.title = conf['about']['title'] self.abstract = conf['about']['abstract'] center = conf['map']['center'] if 'center' in conf['map'] else settings.DEFAULT_MAP_CENTER zoom = conf['map']['zoom'] if 'zoom' in conf['map'] else settings.DEFAULT_MAP_ZOOM center_x = center['x'] if isinstance(center, dict) else center[0] center_y = center['y'] if isinstance(center, dict) else center[1] self.set_bounds_from_center_and_zoom( center_x, center_y, zoom) self.projection = conf['map']['projection'] if self.uuid is None or self.uuid == '': self.uuid = str(uuid.uuid1()) def source_for(layer): try: return conf["sources"][layer["source"]] except BaseException: if 'url' in layer: return {'url': layer['url']} else: return {} layers = [l for l in conf["map"]["layers"]] layer_names = set([l.alternate for l in self.local_layers]) for layer in self.layer_set.all(): layer.delete() self.keywords.add(*conf['map'].get('keywords', [])) for ordering, layer in enumerate(layers): self.layer_set.add( layer_from_viewer_config( self.id, MapLayer, layer, source_for(layer), ordering )) self.save() if layer_names != set([l.alternate for l in self.local_layers]): map_changed_signal.send_robust(sender=self, what_changed='layers') return template_name
def snapshot_config(snapshot, map_obj, user, access_token): """ Get the snapshot map configuration - look up WMS parameters (bunding box) for local GeoNode layers """ # Match up the layer with it's source def snapsource_lookup(source, sources): for k, v in sources.iteritems(): if v.get("id") == source.get("id"): return k return None # Set up the proper layer configuration def snaplayer_config(layer, sources, user, access_token): cfg = layer.layer_config() src_cfg = layer.source_config() source = snapsource_lookup(src_cfg, sources) if source: cfg["source"] = source if src_cfg.get( "ptype", "gxp_wmscsource") == "gxp_wmscsource" or src_cfg.get( "ptype", "gxp_gnsource") == "gxp_gnsource": cfg["buffer"] = 0 return cfg decodedid = num_decode(snapshot) snapshot = get_object_or_404(MapSnapshot, pk=decodedid) if snapshot.map == map_obj.map: config = json.loads(clean_config(snapshot.config)) layers = [l for l in config["map"]["layers"]] sources = config["sources"] maplayers = [] for ordering, layer in enumerate(layers): maplayers.append( layer_from_viewer_config( map_obj.id, MapLayer, layer, config["sources"][ layer["source"]], ordering)) # map_obj.map.layer_set.from_viewer_config( # map_obj, layer, config["sources"][layer["source"]], ordering)) config['map']['layers'] = [ snaplayer_config( l, sources, user, access_token) for l in maplayers] else: config = map_obj.viewer_json(user, access_token) return config
def snapshot_config(snapshot, map_obj, request): """ Get the snapshot map configuration - look up WMS parameters (bunding box) for local GeoNode layers """ # Match up the layer with it's source def snapsource_lookup(source, sources): for k, v in sources.iteritems(): if v.get("id") == source.get("id"): return k return None # Set up the proper layer configuration def snaplayer_config(layer, sources, request): cfg = layer.layer_config() src_cfg = layer.source_config() source = snapsource_lookup(src_cfg, sources) if source: cfg["source"] = source if src_cfg.get( "ptype", "gxp_wmscsource") == "gxp_wmscsource" or src_cfg.get( "ptype", "gxp_gnsource") == "gxp_gnsource": cfg["buffer"] = 0 return cfg decodedid = num_decode(snapshot) snapshot = get_object_or_404(MapSnapshot, pk=decodedid) if snapshot.map == map_obj.map: config = json.loads(clean_config(snapshot.config)) layers = [_l for _l in config["map"]["layers"]] sources = config["sources"] maplayers = [] for ordering, layer in enumerate(layers): maplayers.append( layer_from_viewer_config( map_obj.id, MapLayer, layer, config["sources"][ layer["source"]], ordering)) # map_obj.map.layer_set.from_viewer_config( # map_obj, layer, config["sources"][layer["source"]], ordering)) config['map']['layers'] = [ snaplayer_config( _l, sources, request) for _l in maplayers] else: config = map_obj.viewer_json(request) return config
def update_from_viewer(self, conf, context=None): """ Update this Map's details by parsing a JSON object as produced by a GXP Viewer. This method automatically persists to the database! """ template_name = hookset.update_from_viewer(conf, context=context) conf = context['config'] self.title = conf['about']['title'] self.abstract = conf['about']['abstract'] self.set_bounds_from_center_and_zoom( conf['map']['center'][0], conf['map']['center'][1], conf['map']['zoom']) self.projection = conf['map']['projection'] if self.uuid is None or self.uuid == '': self.uuid = str(uuid.uuid1()) def source_for(layer): return conf["sources"][layer["source"]] layers = [l for l in conf["map"]["layers"]] layer_names = set([l.alternate for l in self.local_layers]) for layer in self.layer_set.all(): layer.delete() self.keywords.add(*conf['map'].get('keywords', [])) for ordering, layer in enumerate(layers): self.layer_set.add( layer_from_viewer_config( self.id, MapLayer, layer, source_for(layer), ordering )) self.save() if layer_names != set([l.alternate for l in self.local_layers]): map_changed_signal.send_robust(sender=self, what_changed='layers') return template_name
def update_from_viewer(self, conf): """ Update this Map's details by parsing a JSON object as produced by a GXP Viewer. This method automatically persists to the database! """ if isinstance(conf, basestring): conf = json.loads(conf) self.title = conf['about']['title'] self.abstract = conf['about']['abstract'] self.zoom = conf['map']['zoom'] self.center_x = conf['map']['center'][0] self.center_y = conf['map']['center'][1] self.projection = conf['map']['projection'] if self.uuid is None or self.uuid == '': self.uuid = str(uuid.uuid1()) def source_for(layer): return conf["sources"][layer["source"]] layers = [l for l in conf["map"]["layers"]] layer_names = set([l.typename for l in self.local_layers]) for layer in self.layer_set.all(): layer.delete() self.keywords.add(*conf['map'].get('keywords', [])) for ordering, layer in enumerate(layers): self.layer_set.add( layer_from_viewer_config( MapLayer, layer, source_for(layer), ordering )) self.set_bounds_from_layers(self.local_layers) self.save() if layer_names != set([l.typename for l in self.local_layers]): map_changed_signal.send_robust(sender=self,what_changed='layers')
def update_from_viewer(self, conf, context=None): """ Update this Map's details by parsing a JSON object as produced by a GXP Viewer. This method automatically persists to the database! """ template_name = hookset.update_from_viewer(conf, context=context) conf = context['config'] self.title = conf['about']['title'] self.abstract = conf['about']['abstract'] self.set_bounds_from_center_and_zoom(conf['map']['center'][0], conf['map']['center'][1], conf['map']['zoom']) self.projection = conf['map']['projection'] if self.uuid is None or self.uuid == '': self.uuid = str(uuid.uuid1()) def source_for(layer): return conf["sources"][layer["source"]] layers = [l for l in conf["map"]["layers"]] layer_names = set([l.alternate for l in self.local_layers]) for layer in self.layer_set.all(): layer.delete() self.keywords.add(*conf['map'].get('keywords', [])) for ordering, layer in enumerate(layers): self.layer_set.add( layer_from_viewer_config(self.id, MapLayer, layer, source_for(layer), ordering)) self.save() if layer_names != set([l.alternate for l in self.local_layers]): map_changed_signal.send_robust(sender=self, what_changed='layers') return template_name
def update_from_viewer(self, conf): """ Update this Map's details by parsing a JSON object as produced by a GXP Viewer. This method automatically persists to the database! """ if isinstance(conf, basestring): conf = json.loads(conf) self.title = conf['about']['title'] self.abstract = conf['about']['abstract'] self.viewer_playbackmode = conf['viewer_playbackmode'] or 'Instant' self.set_bounds_from_center_and_zoom(conf['map']['center'][0], conf['map']['center'][1], conf['map']['zoom']) self.projection = conf['map']['projection'] if self.uuid is None or self.uuid == '': self.uuid = str(uuid.uuid1()) def source_for(layer): if 'sources' in conf: return conf["sources"][layer["source"]] else: # TODO: This is a work in progress will refactor # Handle special case for OL3 config model in storytools as source and sources may not available if layer['type'] == 'WMS': return { "restUrl": "/gs/rest", "title": "", "baseParams": { "VERSION": "1.1.1", "REQUEST": "GetCapabilities", "TILED": 'true', "SERVICE": "WMS" }, "ptype": "gxp_wmscsource", "id": "local" } else: return layer["source"] layers = [l for l in conf["map"]["layers"]] layer_names = set([l.alternate for l in self.local_layers]) for layer in self.layer_set.all(): layer.delete() self.keywords.add(*conf['map'].get('keywords', [])) for ordering, layer in enumerate(layers): self.layer_set.add( layer_from_viewer_config(MapLayer, layer, source_for(layer), ordering)) self.chapter_index = conf['chapter_index'] story_id = conf.get('story_id', 0) story_obj = MapStory.objects.get(id=story_id) self.story = story_obj self.save() if layer_names != set([l.alternate for l in self.local_layers]): map_changed_signal.send_robust(sender=self, what_changed='layers')
def snapshot_config(snapshot, map_obj, request): """ Get the snapshot map configuration - look up WMS parameters (bunding box) for local GeoNode layers """ def source_config(maplayer): """ Generate a dict that can be serialized to a GXP layer source configuration suitable for loading this layer. """ try: cfg = json.loads(maplayer.source_params) except Exception: cfg = dict(ptype="gxp_gnsource", restUrl="/gs/rest") if maplayer.ows_url: cfg["url"] = ows_sub.sub('', maplayer.ows_url) if "ptype" not in cfg: cfg["ptype"] = "gxp_wmscsource" if "ptype" in cfg and cfg["ptype"] == "gxp_gnsource": cfg["restUrl"] = "/gs/rest" return cfg def layer_config(maplayer, user): """ Generate a dict that can be serialized to a GXP layer configuration suitable for loading this layer. The "source" property will be left unset; the layer is not aware of the name assigned to its source plugin. See :method:`geonode.maps.models.Map.viewer_json` for an example of generating a full map configuration. """ try: cfg = json.loads(maplayer.layer_params) except Exception: cfg = dict() if maplayer.format: cfg['format'] = maplayer.format if maplayer.name: cfg["name"] = maplayer.name if maplayer.opacity: cfg['opacity'] = maplayer.opacity if maplayer.styles: cfg['styles'] = maplayer.styles if maplayer.transparent: cfg['transparent'] = True cfg["fixed"] = maplayer.fixed if 'url' not in cfg: cfg['url'] = maplayer.ows_url if cfg['url']: cfg['url'] = ows_sub.sub('', cfg['url']) if maplayer.group: cfg["group"] = maplayer.group cfg["visibility"] = maplayer.visibility if maplayer.name is not None and maplayer.source_params.find("gxp_gnsource") > -1: # Get parameters from GeoNode instead of WMS GetCapabilities try: gnLayer = Layer.objects.get(alternate=maplayer.name) if gnLayer.srid: cfg['srs'] = gnLayer.srid if gnLayer.bbox: cfg['bbox'] = json.loads(gnLayer.bbox) if gnLayer.llbbox: cfg['llbbox'] = json.loads(gnLayer.llbbox) cfg['attributes'] = (get_layer_attributes(gnLayer)) attribute_cfg = gnLayer.attribute_config() if "getFeatureInfo" in attribute_cfg: cfg["getFeatureInfo"] = attribute_cfg["getFeatureInfo"] cfg['queryable'] = (gnLayer.storeType == 'dataStore'), cfg['disabled'] = user is not None and not user.has_perm('maps.view_layer', obj=gnLayer) # cfg["displayOutsideMaxExtent"] = user is not None and user.has_perm('maps.change_layer', obj=gnLayer) cfg['visibility'] = cfg['visibility'] and not cfg['disabled'] cfg['abstract'] = gnLayer.abstract cfg['styles'] = maplayer.styles cfg['local'] = True except Exception as e: # Give it some default values so it will still show up on the map, but disable it in the layer tree cfg['srs'] = 'EPSG:900913' cfg['llbbox'] = [-180, -90, 180, 90] cfg['attributes'] = [] cfg['queryable'] = False, cfg['disabled'] = False cfg['visibility'] = cfg['visibility'] and not cfg['disabled'] cfg['abstract'] = '' cfg['styles'] = '' print "Could not retrieve Layer with typename of %s : %s" % (maplayer.name, str(e)) elif maplayer.source_params.find("gxp_hglsource") > -1: # call HGL ServiceStarter asynchronously to load the layer into HGL geoserver from geonode.queue.tasks import loadHGL loadHGL.delay(maplayer.name) return cfg # Match up the layer with it's source def snapsource_lookup(source, sources): for k, v in sources.iteritems(): if v.get("id") == source.get("id"): return k return None # Set up the proper layer configuration # def snaplayer_config(layer, sources, user): def snaplayer_config(layer, sources, request): user = request.user if request else None cfg = layer_config(layer, user) src_cfg = source_config(layer) source = snapsource_lookup(src_cfg, sources) if source: cfg["source"] = source if src_cfg.get( "ptype", "gxp_wmscsource") == "gxp_wmscsource" or src_cfg.get( "ptype", "gxp_gnsource") == "gxp_gnsource": cfg["buffer"] = 0 return cfg from geonode.utils import num_decode from geonode.utils import layer_from_viewer_config decodedid = num_decode(snapshot) snapshot = get_object_or_404(MapSnapshot, pk=decodedid) if snapshot.map == map_obj.map: config = json.loads(clean_config(snapshot.config)) layers = [l for l in config["map"]["layers"]] sources = config["sources"] maplayers = [] for ordering, layer in enumerate(layers): maplayers.append( layer_from_viewer_config( map_obj.id, MapLayer, layer, config["sources"][ layer["source"]], ordering, False)) # map_obj.map.layer_set.from_viewer_config( # map_obj, layer, config["sources"][layer["source"]], ordering)) config['map']['layers'] = [ snaplayer_config( l, sources, request) for l in maplayers] else: config = map_obj.viewer_json(request) return config
from geonode.utils import num_decode from geonode.utils import layer_from_viewer_config decodedid = num_decode(snapshot) snapshot = get_object_or_404(MapSnapshot, pk=decodedid) if snapshot.map == map_obj.map: config = json.loads(clean_config(snapshot.config)) layers = [l for l in config["map"]["layers"]] sources = config["sources"] maplayers = [] for ordering, layer in enumerate(layers): maplayers.append( layer_from_viewer_config( map_obj.id, MapLayer, layer, config["sources"][ layer["source"]], ordering, False)) # map_obj.map.layer_set.from_viewer_config( # map_obj, layer, config["sources"][layer["source"]], ordering)) config['map']['layers'] = [ snaplayer_config( l, sources, request) for l in maplayers] else: config = map_obj.viewer_json(request) return config
cfg["buffer"] = 0 return cfg from geonode.utils import num_decode from geonode.utils import layer_from_viewer_config decodedid = num_decode(snapshot) snapshot = get_object_or_404(MapSnapshot, pk=decodedid) if snapshot.map == map_obj.map: config = json.loads(clean_config(snapshot.config)) layers = [l for l in config["map"]["layers"]] sources = config["sources"] maplayers = [] for ordering, layer in enumerate(layers): maplayers.append( layer_from_viewer_config(map_obj.id, MapLayer, layer, config["sources"][layer["source"]], ordering, False)) # map_obj.map.layer_set.from_viewer_config( # map_obj, layer, config["sources"][layer["source"]], ordering)) config['map']['layers'] = [ snaplayer_config(l, sources, request) for l in maplayers ] else: config = map_obj.viewer_json(request) return config def printmap(request, mapid=None, snapshot=None):
def update_from_viewer(self, conf, context=None): """ Update this Map's details by parsing a JSON object as produced by a GXP Viewer. This method automatically persists to the database! """ template_name = hookset.update_from_viewer(conf, context=context) if not isinstance(context, dict): try: context = json.loads(ensure_string(context)) except Exception: pass conf = context.get("config", {}) if not isinstance(conf, dict) or isinstance(conf, bytes): try: conf = json.loads(ensure_string(conf)) except Exception: conf = {} about = conf.get("about", {}) self.title = conf.get("title", about.get("title", "")) self.abstract = conf.get("abstract", about.get("abstract", "")) _map = conf.get("map", {}) center = _map.get("center", settings.DEFAULT_MAP_CENTER) self.zoom = _map.get("zoom", settings.DEFAULT_MAP_ZOOM) if isinstance(center, dict): self.center_x = center.get('x') self.center_y = center.get('y') else: self.center_x, self.center_y = center projection = _map.get("projection", settings.DEFAULT_MAP_CRS) bbox = _map.get("bbox", None) if bbox: self.set_bounds_from_bbox(bbox, projection) else: self.set_bounds_from_center_and_zoom( self.center_x, self.center_y, self.zoom) if self.projection is None or self.projection == '': self.projection = projection if self.uuid is None or self.uuid == '': self.uuid = str(uuid.uuid1()) def source_for(layer): try: return conf["sources"][layer["source"]] except Exception: if 'url' in layer: return {'url': layer['url']} else: return {} layers = [lyr for lyr in _map.get("layers", [])] layer_names = set(lyr.alternate for lyr in self.local_layers) self.layer_set.all().delete() self.keywords.add(*_map.get('keywords', [])) for ordering, layer in enumerate(layers): self.layer_set.add( layer_from_viewer_config( self.id, MapLayer, layer, source_for(layer), ordering )) self.save(notify=True) if layer_names != set(lyr.alternate for lyr in self.local_layers): map_changed_signal.send_robust(sender=self, what_changed='layers') return template_name
def snapshot_config(snapshot, map_obj, request): """ Get the snapshot map configuration - look up WMS parameters (bunding box) for local GeoNode layers """ def source_config(maplayer): """ Generate a dict that can be serialized to a GXP layer source configuration suitable for loading this layer. """ try: cfg = json.loads(maplayer.source_params) except Exception: cfg = dict(ptype="gxp_gnsource", restUrl="/gs/rest") if maplayer.ows_url: cfg["url"] = ows_sub.sub('', maplayer.ows_url) if "ptype" not in cfg: cfg["ptype"] = "gxp_wmscsource" if "ptype" in cfg and cfg["ptype"] == "gxp_gnsource": cfg["restUrl"] = "/gs/rest" return cfg def layer_config(maplayer, user): """ Generate a dict that can be serialized to a GXP layer configuration suitable for loading this layer. The "source" property will be left unset; the layer is not aware of the name assigned to its source plugin. See :method:`geonode.maps.models.Map.viewer_json` for an example of generating a full map configuration. """ try: cfg = json.loads(maplayer.layer_params) except Exception: cfg = dict() if maplayer.format: cfg['format'] = maplayer.format if maplayer.name: cfg["name"] = maplayer.name if maplayer.opacity: cfg['opacity'] = maplayer.opacity if maplayer.styles: cfg['styles'] = maplayer.styles if maplayer.transparent: cfg['transparent'] = True cfg["fixed"] = maplayer.fixed if 'url' not in cfg: cfg['url'] = maplayer.ows_url if cfg['url']: cfg['url'] = ows_sub.sub('', cfg['url']) if maplayer.group: cfg["group"] = maplayer.group cfg["visibility"] = maplayer.visibility if maplayer.name is not None and maplayer.source_params.find( "gxp_gnsource") > -1: # Get parameters from GeoNode instead of WMS GetCapabilities try: gnLayer = Layer.objects.get(alternate=maplayer.name) if gnLayer.srid: cfg['srs'] = gnLayer.srid if gnLayer.bbox: cfg['bbox'] = json.loads(gnLayer.bbox) if gnLayer.llbbox: cfg['llbbox'] = json.loads(gnLayer.llbbox) cfg['attributes'] = (get_layer_attributes(gnLayer)) attribute_cfg = gnLayer.attribute_config() if "getFeatureInfo" in attribute_cfg: cfg["getFeatureInfo"] = attribute_cfg["getFeatureInfo"] cfg['queryable'] = (gnLayer.storeType == 'dataStore'), cfg['disabled'] = user is not None and not user.has_perm( 'maps.view_layer', obj=gnLayer) # cfg["displayOutsideMaxExtent"] = user is not None and user.has_perm('maps.change_layer', obj=gnLayer) cfg['visibility'] = cfg['visibility'] and not cfg['disabled'] cfg['abstract'] = gnLayer.abstract cfg['styles'] = maplayer.styles cfg['local'] = True except Exception as e: # Give it some default values so it will still show up on the map, but disable it in the layer tree cfg['srs'] = 'EPSG:900913' cfg['llbbox'] = [-180, -90, 180, 90] cfg['attributes'] = [] cfg['queryable'] = False, cfg['disabled'] = False cfg['visibility'] = cfg['visibility'] and not cfg['disabled'] cfg['abstract'] = '' cfg['styles'] = '' print "Could not retrieve Layer with typename of %s : %s" % ( maplayer.name, str(e)) elif maplayer.source_params.find("gxp_hglsource") > -1: # call HGL ServiceStarter asynchronously to load the layer into HGL geoserver from geonode.queue.tasks import loadHGL loadHGL.delay(maplayer.name) return cfg # Match up the layer with it's source def snapsource_lookup(source, sources): for k, v in sources.iteritems(): if v.get("id") == source.get("id"): return k return None # Set up the proper layer configuration # def snaplayer_config(layer, sources, user): def snaplayer_config(layer, sources, request): user = request.user if request else None cfg = layer_config(layer, user) src_cfg = source_config(layer) source = snapsource_lookup(src_cfg, sources) if source: cfg["source"] = source if src_cfg.get("ptype", "gxp_wmscsource") == "gxp_wmscsource" or src_cfg.get( "ptype", "gxp_gnsource") == "gxp_gnsource": cfg["buffer"] = 0 return cfg from geonode.utils import num_decode from geonode.utils import layer_from_viewer_config decodedid = num_decode(snapshot) snapshot = get_object_or_404(MapSnapshot, pk=decodedid) if snapshot.map == map_obj.map: config = json.loads(clean_config(snapshot.config)) layers = [l for l in config["map"]["layers"]] sources = config["sources"] maplayers = [] for ordering, layer in enumerate(layers): maplayers.append( layer_from_viewer_config(map_obj.id, MapLayer, layer, config["sources"][layer["source"]], ordering, False)) # map_obj.map.layer_set.from_viewer_config( # map_obj, layer, config["sources"][layer["source"]], ordering)) config['map']['layers'] = [ snaplayer_config(l, sources, request) for l in maplayers ] else: config = map_obj.viewer_json(request) return config
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
cfg["buffer"] = 0 return cfg from geonode.utils import num_decode from geonode.utils import layer_from_viewer_config decodedid = num_decode(snapshot) snapshot = get_object_or_404(MapSnapshot, pk=decodedid) if snapshot.map == map_obj.map: config = json.loads(clean_config(snapshot.config)) layers = [l for l in config["map"]["layers"]] sources = config["sources"] maplayers = [] for ordering, layer in enumerate(layers): maplayers.append( layer_from_viewer_config(MapLayer, layer, config["sources"][layer["source"]], ordering)) # map_obj.map.layer_set.from_viewer_config( # map_obj, layer, config["sources"][layer["source"]], ordering)) config['map']['layers'] = [ snaplayer_config(l, sources, request) for l in maplayers ] else: config = map_obj.viewer_json(request) return config def printmap(request, mapid=None, snapshot=None):
return cfg from geonode.utils import num_decode from geonode.utils import layer_from_viewer_config decodedid = num_decode(snapshot) snapshot = get_object_or_404(MapSnapshot, pk=decodedid) if snapshot.map == map_obj.map: config = json.loads(clean_config(snapshot.config)) layers = [l for l in config["map"]["layers"]] sources = config["sources"] maplayers = [] for ordering, layer in enumerate(layers): maplayers.append( layer_from_viewer_config( MapLayer, layer, config["sources"][ layer["source"]], ordering)) # map_obj.map.layer_set.from_viewer_config( # map_obj, layer, config["sources"][layer["source"]], ordering)) config['map']['layers'] = [ snaplayer_config( l, sources, request) for l in maplayers] else: config = map_obj.viewer_json(request) return config