Exemplo n.º 1
0
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)
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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