Ejemplo n.º 1
0
def image(request, id=None):
    @logged_in_or_basicauth()
    def handle_update(request, image):
        data = simplejson.loads(request.raw_post_data)
        image.from_json(data, request.user)
        image.save()
        return json_response(request, image)

    if id == None and request.method == "POST":
        i = Image()
        return handle_update(request, i)
    elif id != None:
        i = Image.objects.get(pk=id)
        if request.method == "DELETE":
            i.delete()
            return json_response(request, "")
        elif request.method == "POST":
            return handle_update(request, i)
        return json_response(request, i)
    else:
        images = Image.objects.all().select_related()
        output = request.GET.get('output', 'simple')
        if 'archive' in request.GET and request.GET['archive'].lower() in (
                "true", "t", "1"):
            images = images.filter(archive=True)
        else:
            images = images.filter(archive=False)
        if 'layer' in request.GET:
            images = images.filter(layers__id=request.GET['layer'])
        if 'bbox' in request.GET:
            left, bottom, right, top = map(float,
                                           request.GET['bbox'].split(","))
            box = Polygon.from_bbox([left, bottom, right, top])
            images = images.filter(bbox__intersects=box)
        limit = min(int(request.GET.get("limit", 1000)), 10000)
        start = int(request.GET.get("start", 0))
        end = start + limit
        images = images.order_by("-id")

        # Instantiating full image objects for thousands of images is slow;
        # instead, just use .values and make our own dict here. Adding more
        # properties here should be done with consideration.
        if output == 'simple':
            data = {
                'images': [
                    dict(x) for x in images[start:end].values(
                        "id", "width", "height", "url", "bbox")
                ]
            }
            for i in data['images']:
                i['bbox'] = list(i['bbox'].extent)

        else:
            data = {
                'images':
                [i.to_json(output=output) for i in images[start:end]]
            }
        return json_response(request, data)
Ejemplo n.º 2
0
Archivo: views.py Proyecto: aaronr/oam
def image(request, id=None):
    @logged_in_or_basicauth()
    def handle_update(request, image):
        data = simplejson.loads(request.raw_post_data)
        image.from_json(data, request.user)
        image.save()
        return json_response(request, image)
        
    if id == None and request.method == "POST":
        i = Image()
        return handle_update(request,i)
    elif id != None:
        i = Image.objects.get(pk=id)
        if request.method == "DELETE":
            i.delete()
            return json_response(request, "")
        elif request.method == "POST":
            return handle_update(request, i)
        return json_response(request, i)
    else:
        images = Image.objects.all().select_related()
        output = request.GET.get('output', 'simple')
        if 'archive' in request.GET and request.GET['archive'].lower() in ("true", "t", "1"):
            images = images.filter(archive=True)
        else:
            images = images.filter(archive=False)
        if 'layer' in request.GET:
            images = images.filter(layers__id=request.GET['layer'])
        if 'bbox' in request.GET:
            left, bottom, right, top = map(float, request.GET['bbox'].split(","))
            box = Polygon.from_bbox([left, bottom, right, top])
            images = images.filter(bbox__intersects=box)
        limit = min(int(request.GET.get("limit", 1000)), 10000)
        start = int(request.GET.get("start", 0))    
        end = start + limit
        images = images.order_by("-id") 

        # Instantiating full image objects for thousands of images is slow;
        # instead, just use .values and make our own dict here. Adding more 
        # properties here should be done with consideration.
        if output == 'simple':
           data = {'images': 
            [dict(x) for x in images[start:end].values("id", "width", "height", "url", "bbox")]
           } 
           for i in data['images']:
            i['bbox'] = list(i['bbox'].extent)
        
        else:
            data = {'images': [
                i.to_json(output=output) for i in images[start:end]
                ]
            }   
        return json_response(request, data)
Ejemplo n.º 3
0
def image(request, id=None):
    if id == None and request.method == "POST":
        data = simplejson.loads(request.raw_post_data)
        i = Image()
        i.from_json(data)
        i.save()
        return json_response(request, i)
    elif id != None:
        i = Image.objects.get(pk=id)
        if request.method == "POST":
            data = simplejson.loads(request.raw_post_data)
            i.from_json(data)
            i.save()
        return json_response(request, i)
    else:
        images = Image.objects.all()
        if 'archive' in request.GET and request.GET['archive'].lower() in (
                "true", "t", "1"):
            images = images.filter(archive=True)
        else:
            images = images.filter(archive=False)
        limited_images = images
        if 'bbox' in request.GET:
            limited_images = []
            left, bottom, right, top = map(float,
                                           request.GET['bbox'].split(","))
            for image in images:
                ileft, ibottom, iright, itop = map(float,
                                                   image.bbox.split(","))
                inbottom = (((ibottom >= bottom) and (ibottom <= top))
                            or ((bottom >= ibottom) and (bottom <= itop)))
                intop = (((itop >= bottom) and (itop <= top))
                         or ((top > ibottom) and (top < itop)))
                inleft = (((ileft >= left) and (ileft <= right))
                          or ((left >= ileft) and (left <= iright)))
                inright = (((iright >= left) and (iright <= right))
                           or ((right >= iright) and (right <= iright)))
                intersects = ((inbottom or intop) and (inleft or inright))
                if intersects:
                    limited_images.append(image)
        data = {'images': [i.to_json() for i in limited_images]}
        return json_response(request, data)
Ejemplo n.º 4
0
def image(request, id=None):
    if id == None and request.method == "POST":
        data = simplejson.loads(request.raw_post_data)
        i = Image()
        i.from_json(data)
        i.save()
        return json_response(request, i)
    elif id != None:
        i = Image.objects.get(pk=id)
        if request.method == "POST":
            data = simplejson.loads(request.raw_post_data)
            i.from_json(data)
            i.save()
        return json_response(request, i)
    else:
        images = Image.objects.all()
        if 'archive' in request.GET and request.GET['archive'].lower() in ("true", "t", "1"):
            images = images.filter(archive=True)
        else:
            images = images.filter(archive=False)
        limited_images = images
        if 'bbox' in request.GET:
            limited_images = []
            left, bottom, right, top = map(float, request.GET['bbox'].split(","))
            for image in images:
                ileft, ibottom, iright, itop = map(float, image.bbox.split(","))
                inbottom = (((ibottom >= bottom) and (ibottom <= top)) or ((bottom >= ibottom) and (bottom <= itop)))
                intop = (((itop >= bottom) and (itop <= top)) or ((top > ibottom) and (top < itop)))
                inleft = (((ileft >= left) and (ileft <= right)) or ((left >= ileft) and (left <= iright)))
                inright = (((iright >= left) and (iright <= right)) or ((right >= iright) and (right <= iright)))
                intersects = ((inbottom or intop) and (inleft or inright))
                if intersects: 
                    limited_images.append(image)
        data = {'images': [
            i.to_json() for i in limited_images
            ]
        }   
        return json_response(request, data)