def new_map_json(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', mimetype="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() try: map_obj.update_from_viewer(request.body) MapSnapshot.objects.create(config=clean_config(request.body),map=map_obj,user=request.user) except ValueError, e: return HttpResponse(str(e), status=400) else: return HttpResponse( json.dumps({'id':map_obj.id }), status=200, mimetype='application/json' )
def new_map_json(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', mimetype="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() try: map_obj.update_from_viewer(request.raw_post_data) except ValueError, e: return HttpResponse(str(e), status=400) else: return HttpResponse(json.dumps({'id': map_obj.id}), status=200, mimetype='application/json')
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 new_map_json(request): if request.method == 'GET': config = new_map_config(request) if isinstance(config, HttpResponse): return config else: return HttpResponse(config) elif settings.RESOURCE_PUBLISHING: return HttpResponse( _PERMISSION_MSG_SAVE, status=401, mimetype="text/plain" ) elif request.method == 'POST': if not request.user.is_authenticated(): return HttpResponse( 'You must be logged in to save new maps', mimetype="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() # 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) 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, mimetype='application/json' ) else: return HttpResponse(status=405)
def test_map_save(self): """POST /maps -> Test saving a new map""" # since django's test client doesn't support providing a JSON request # body, just test the model directly. # the view's hooked up right, I promise. map = Map(zoom=7, center_x=0, center_y=0) map.save() # can't attach layers to a map whose pk isn't set yet map.update_from_viewer(json.loads(self.viewer_config)) self.assertEquals(map.title, "Title") self.assertEquals(map.abstract, "Abstract") self.assertEquals(map.layer_set.all().count(), 1)
def new_map_json(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, category=TopicCategory.objects.get(id=1), group=GroupProfile.objects.get(title='wasa')) #hardcoded for now 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 except Exception: 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 new_map_json(request): if request.method == 'GET': map_obj, 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}) MapSnapshot.objects.create( config=clean_config(body), map=map_obj, user=request.user) except ValueError as e: return HttpResponse(str(e), status=400) else: register_event(request, EventType.EVENT_UPLOAD, map_obj) return HttpResponse( json.dumps({'id': map_obj.id}), status=200, content_type='application/json' ) else: return HttpResponse(status=405)
def test_map_save(self): """POST /maps -> Test saving a new map""" # This is a valid map viewer config, based on the sample data provided # by andreas in issue 566. -dwins viewer_config = """ { "defaultSourceType": "gx_wmssource", "about": { "title": "Title", "abstract": "Abstract" }, "sources": { "capra": { "url":"http://localhost:8001/geoserver/wms" } }, "map": { "projection":"EPSG:900913", "units":"m", "maxResolution":156543.0339, "maxExtent":[-20037508.34,-20037508.34,20037508.34,20037508.34], "center":[-9428760.8688778,1436891.8972581], "layers":[{ "source":"capra", "buffer":0, "wms":"capra", "name":"base:nic_admin" }], "zoom":7 } } """ # since django's test client doesn't support providing a JSON request # body, just test the model directly. # the view's hooked up right, I promise. map = Map(zoom=7, center_x=0, center_y=0) map.save() # can't attach layers to a map whose pk isn't set yet map.update_from_viewer(json.loads(viewer_config)) self.assertEquals(map.title, "Title") self.assertEquals(map.abstract, "Abstract") self.assertEquals(map.layer_set.all().count(), 1)
def new_map_json(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", mimetype="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() try: map_obj.update_from_viewer(request.raw_post_data) except ValueError, e: return HttpResponse(str(e), status=400) else: return HttpResponse(json.dumps({"id": map_obj.id}), status=200, mimetype="application/json")
def new_map_json(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 ) data = json.loads(request.body) title = data['about']['title'] category_id = int(data['about']['category']) organization_id = int(data['about']['organization']) group = GroupProfile.objects.get(id=organization_id) map_obj = Map(owner=request.user, zoom=0, center_x=0, center_y=0, category=TopicCategory.objects.get(id=category_id), group=group, title=title) map_obj.save() map_obj.set_default_permissions() permissions = _perms_info_json(map_obj) perm_dict = json.loads(permissions) if 'download_resourcebase' in perm_dict['groups']['anonymous']: perm_dict['groups']['anonymous'].remove('download_resourcebase') if 'view_resourcebase' in perm_dict['groups']['anonymous']: perm_dict['groups']['anonymous'].remove('view_resourcebase') # map_obj.set_permissions(perm_dict) # 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) # notify layer owners that this layer is used to create this map layers = map_obj.layers layer_owners = [layer.owner for layer in map_obj.local_layers] notify.send(request.user, recipient_list=layer_owners, actor=request.user, verb='created map using your layer', target=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 set_geonode_map(self, caller, serializer, map_obj=None, data=None, attributes=None): def decode_base64(data): """Decode base64, padding being optional. :param data: Base64 data as an ASCII byte string :returns: The decoded byte string. """ _thumbnail_format = 'png' _invalid_padding = data.find(';base64,') if _invalid_padding: _thumbnail_format = data[data.find('image/') + len('image/'):_invalid_padding] data = data[_invalid_padding + len(';base64,'):] missing_padding = len(data) % 4 if missing_padding != 0: data += b'=' * (4 - missing_padding) return (base64.b64decode(data), _thumbnail_format) _map_name = None _map_title = None _map_abstract = None _map_thumbnail = None _map_thumbnail_format = 'png' if attributes: for _a in attributes: if _a['name'] == 'name' and 'value' in _a: _map_name = _a['value'] if _a['name'] == 'title' and 'value' in _a: _map_title = _a['value'] if _a['name'] == 'abstract' and 'value' in _a: _map_abstract = _a['value'] if 'thumb' in _a['name'] and 'value' in _a: try: (_map_thumbnail, _map_thumbnail_format) = decode_base64(_a['value']) except Exception: if _a['value']: _map_thumbnail = _a['value'] _map_thumbnail_format = 'link' elif map_obj: _map_title = map_obj.title _map_abstract = map_obj.abstract _map_name = _map_name or None if not _map_name and 'name' in serializer.validated_data: _map_name = serializer.validated_data['name'] _map_title = _map_title or _map_name _map_abstract = _map_abstract or "" if data: try: _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 = "%s:%s" % ( _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'] _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: # Update Map BBox if 'bbox' not in _map_obj and (not _map_bbox or len(_map_bbox) != 4): _map_bbox = _map_obj['maxExtent'] # Must be in the form : [x0, x1, y0, y1] _map_obj['bbox'] = [ _map_bbox[0], _map_bbox[1], _map_bbox[2], _map_bbox[3] ] # Create a new GeoNode Map from geonode.maps.models import Map map_obj = Map(title=_map_title, 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']) 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] map_obj.save() if is_analytics_enabled: event_type = EventType.EVENT_CREATE # Dumps thumbnail from MapStore2 Interface if _map_thumbnail: # note: dumping a thumbnail should be performed before update_from_viewer(), to remove # a race hazard. update_from_viewer() saves an existing map without a thumbnail, # triggering asynchronous generation of a default thumbnail, which may overwrite uploaded thumb if _map_thumbnail_format == 'link': map_obj.thumbnail_url = _map_thumbnail else: _map_thumbnail_filename = "map-%s-thumb.%s" % ( map_obj.uuid, _map_thumbnail_format) map_obj.save_thumbnail(_map_thumbnail_filename, _map_thumbnail) # Update GeoNode Map _map_conf['map'] = _map_obj map_obj.update_from_viewer(_map_conf, context={'config': _map_conf}) if is_analytics_enabled: register_event(caller.request, event_type, map_obj) serializer.validated_data['id'] = map_obj.id serializer.save(user=caller.request.user) except Exception as e: tb = traceback.format_exc() logger.error(tb) raise APIException(e) else: raise APIException("Map Configuration (data) is Mandatory!")
def set_geonode_map(self, caller, serializer, map_obj=None, data=None, attributes=None): def decode_base64(data): """Decode base64, padding being optional. :param data: Base64 data as an ASCII byte string :returns: The decoded byte string. """ _thumbnail_format = 'png' _invalid_padding = data.find(';base64,') if _invalid_padding: _thumbnail_format = data[data.find('image/') + len('image/'):_invalid_padding] data = data[_invalid_padding + len(';base64,'):] missing_padding = len(data) % 4 if missing_padding != 0: data += b'=' * (4 - missing_padding) return (data.decode('base64'), _thumbnail_format) _map_name = None _map_title = None _map_abstract = None _map_thumbnail = None _map_thumbnail_format = 'png' if attributes: for _a in attributes: if _a['name'] == 'name': _map_name = _a['value'] if _a['name'] == 'title': _map_title = _a['value'] if _a['name'] == 'abstract': _map_abstract = _a['value'] if 'thumb' in _a['name']: (_map_thumbnail, _map_thumbnail_format) = decode_base64(_a['value']) elif map_obj: _map_title = map_obj.title _map_abstract = map_obj.abstract _map_name = _map_name or None if not _map_name and 'name' in serializer.validated_data: _map_name = serializer.validated_data['name'] _map_title = _map_title or _map_name _map_abstract = _map_abstract or "" if data: try: _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 = {} 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']: if 'style' in _lyr: _lyr_context['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 BaseException: 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 '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']] = {} # Update Map BBox if not _map_bbox or len(_map_bbox) != 4: _map_bbox = _map_obj['maxExtent'] # Must be in the form : [x0, x1, y0, y1] _map_obj['bbox'] = [ _map_bbox[0], _map_bbox[1], _map_bbox[2], _map_bbox[3] ] if not map_obj: # Create a new GeoNode Map from geonode.maps.models import Map map_obj = Map(title=_map_title, owner=caller.request.user, center_x=_map_obj['center']['x'], center_y=_map_obj['center']['y'], projection=_map_obj['projection'], zoom=_map_obj['zoom'], bbox_x0=_map_obj['bbox'][0], bbox_x1=_map_obj['bbox'][1], bbox_y0=_map_obj['bbox'][2], bbox_y1=_map_obj['bbox'][3], srid=_map_obj['projection']) map_obj.save() # Update GeoNode Map _map_conf['map'] = _map_obj map_obj.update_from_viewer(_map_conf, context={'config': _map_conf}) # Dumps thumbnail from MapStore2 Interface if _map_thumbnail: _map_thumbnail_filename = "map-%s-thumb.%s" % ( map_obj.uuid, _map_thumbnail_format) map_obj.save_thumbnail(_map_thumbnail_filename, _map_thumbnail) serializer.validated_data['id'] = map_obj.id serializer.save(user=caller.request.user) except BaseException: tb = traceback.format_exc() logger.error(tb) raise APIException(tb) else: raise APIException("Map Configuration (data) is Mandatory!")