def new_chapter_json(request): ''' Exracted from geonode.maps.views.new_map_json :param request: :return: ''' if request.method == 'POST': if not request.user.is_authenticated(): return HttpResponse( 'You must be logged in to save new maps', content_type="text/plain", status=401 ) map_obj = Map(owner=request.user, zoom=0, center_x=0, center_y=0) map_obj.is_published = False map_obj.save() map_obj.set_default_permissions() # If the body has been read already, use an empty string. # See https://github.com/django/django/commit/ # 58d555caf527d6f1bdfeab14527484e4cca68648 # for a better exception to catch when we move to Django 1.7. try: body = request.body if isinstance(body, basestring): body = json.loads(body) story_id = body.get('story_id', 0) story_obj = Story.objects.get(id=story_id) mapping = StoryChapter() mapping.chapter_index = body['chapter_index'] mapping.map = map_obj mapping.story = story_obj mapping.save() except Exception as e: print e body = '' try: map_obj.update_from_viewer(body) MapSnapshot.objects.create( config=clean_config(body), map=map_obj, user=request.user) except ValueError as e: return HttpResponse(str(e), status=400) else: return HttpResponse( json.dumps({'id': map_obj.id}), status=200, content_type='application/json' ) else: return HttpResponse(status=405)
def snapshot_create(request): """ Create a permalinked map """ conf = request.body if isinstance(conf, string_types): config = json.loads(conf) snapshot = MapSnapshot.objects.create( config=clean_config(conf), map=Map.objects.get(id=config['id'])) return HttpResponse(num_encode(snapshot.id), content_type="text/plain") else: return HttpResponse("Invalid JSON", content_type="text/plain", status=500)
def new_map_json_wm(request): if request.method == 'GET': config = new_map_config(request) if isinstance(config, HttpResponse): return config else: return HttpResponse(config) elif request.method == 'POST': if not request.user.is_authenticated(): return HttpResponse( 'You must be logged in to save new maps', content_type="text/plain", status=401 ) map_obj = Map(owner=request.user, zoom=0, center_x=0, center_y=0) map_obj.save() map_obj.set_default_permissions() map_obj.handle_moderated_uploads() # If the body has been read already, use an empty string. # See https://github.com/django/django/commit/58d555caf527d6f1bdfeab14527484e4cca68648 # for a better exception to catch when we move to Django 1.7. try: body = request.body except Exception: body = '' try: map_obj.update_from_viewer(body, context={'request': request, 'mapId': map_obj.id, 'map': map_obj}) update_ext_map(request, map_obj) MapSnapshot.objects.create( config=clean_config(body), map=map_obj, user=request.user) except ValueError as e: return HttpResponse(str(e), status=400) else: return HttpResponse( json.dumps({'id': map_obj.id}), status=200, content_type='application/json' ) else: return HttpResponse(status=405)
def new_map_json_wm(request): if request.method == 'GET': config = new_map_config(request) if isinstance(config, HttpResponse): return config else: return HttpResponse(config) elif request.method == 'POST': if not request.user.is_authenticated(): return HttpResponse('You must be logged in to save new maps', content_type="text/plain", status=401) map_obj = Map(owner=request.user, zoom=0, center_x=0, center_y=0) map_obj.save() map_obj.set_default_permissions() map_obj.handle_moderated_uploads() # If the body has been read already, use an empty string. # See https://github.com/django/django/commit/58d555caf527d6f1bdfeab14527484e4cca68648 # for a better exception to catch when we move to Django 1.7. try: body = request.body except Exception: body = '' try: map_obj.update_from_viewer(body, context={ 'request': request, 'mapId': map_obj.id, 'map': map_obj }) update_ext_map(request, map_obj) MapSnapshot.objects.create(config=clean_config(body), map=map_obj, user=request.user) except ValueError as e: return HttpResponse(str(e), status=400) else: return HttpResponse(json.dumps({'id': map_obj.id}), status=200, content_type='application/json') else: return HttpResponse(status=405)
def map_json_wm(request, mapid, snapshot=None): if request.method == 'GET': map_obj = _resolve_map( request, mapid, 'base.view_resourcebase', _PERMISSION_MSG_VIEW) return HttpResponse( json.dumps( map_obj.viewer_json(request))) elif request.method == 'PUT': if not request.user.is_authenticated(): return HttpResponse( _PERMISSION_MSG_LOGIN, status=401, content_type="text/plain" ) map_obj = Map.objects.get(id=mapid) if not request.user.has_perm( 'change_resourcebase', map_obj.get_self_resource()): return HttpResponse( _PERMISSION_MSG_SAVE, status=401, content_type="text/plain" ) try: map_obj.update_from_viewer(request.body, context={'request': request, 'mapId': mapid, 'map': map_obj}) update_ext_map(request, map_obj) MapSnapshot.objects.create( config=clean_config( request.body), map=map_obj, user=request.user) return HttpResponse( json.dumps( map_obj.viewer_json(request))) except ValueError as e: return HttpResponse( "The server could not understand the request." + str(e), content_type="text/plain", status=400 )
def map_json_wm(request, mapid, snapshot=None): if request.method == 'GET': map_obj = _resolve_map( request, mapid, 'base.view_resourcebase', _PERMISSION_MSG_VIEW) return HttpResponse( json.dumps( map_obj.viewer_json(request))) elif request.method == 'PUT': if not request.user.is_authenticated(): return HttpResponse( _PERMISSION_MSG_LOGIN, status=401, content_type="text/plain" ) map_obj = Map.objects.get(id=mapid) if not request.user.has_perm( 'change_resourcebase', map_obj.get_self_resource()): return HttpResponse( _PERMISSION_MSG_SAVE, status=401, content_type="text/plain" ) try: map_obj.update_from_viewer(request.body, context={'request': request, 'mapId': mapid, 'map': map_obj}) update_ext_map(request, map_obj) MapSnapshot.objects.create( config=clean_config( request.body), map=map_obj, user=request.user) return HttpResponse( json.dumps( map_obj.viewer_json(request))) except ValueError as e: return HttpResponse( "The server could not understand the request." + str(e), content_type="text/plain", status=400 )
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(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 ]
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
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))
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