Ejemplo n.º 1
0
def within(request, id):
    ''' GET
       Gets places where the centroids are within this place's geometry
       Results are unsorted
       also accept
            per_page: results per page int, default=100
            page: page no (starting with 1) int, default=1

        Returns:
            GeoJSON feed of search results
            Extra properties of feed:
                total: total number of results
                max_score: max score in results (?)
                page: page number
    '''
    place = get_place_or_404(id)
    per_page = int(request.GET.get("per_page", 100))
    page = int(request.GET.get("page", 1))
    page_0 = page - 1 
    
    result = Place.objects.within(place, per_page=per_page, page=page_0)
        
    total = result['total']
    pages = int(math.ceil(total / (per_page + .0))) #get total number of pages
    
    ret = {
        'type': 'FeatureCollection',
        'features': [p.to_geojson() for p in result['places']],
        'total': total,
        'page': result['page'],
        'pages': pages,
        'per_page': result['per_page'],
        'max_score': result['max_score']
    }
    return render_to_json_response(ret)
Ejemplo n.º 2
0
def revision(request, id, revision):
    '''
        Get GeoJSON of a place at given revision
    '''
    place = get_place_or_404(id)

    if request.method == 'GET':
        revision = Place.objects.revision(place, revision)
        geojson = revision['place'].to_geojson()
        geojson['version'] = revision['version']
        geojson['digest'] = revision['digest']
        return render_to_json_response(geojson)

    elif request.method == 'PUT':
        user_id = request.user.id
        username = request.user.username
        data = json.loads(request.body)
        comment = data.get('comment', '')
        metadata = {'user': username, 'user_id': user_id, 'comment': comment}
        place = place.rollback(
            revision, metadata=metadata)  #FIXME: handle invalid revision ids
        return render_to_json_response(place.to_geojson())

    else:
        return render_to_json_response({'error': 'Method Not Allowed'},
                                       status=405)
Ejemplo n.º 3
0
def revision(request, id, revision):
    '''
        Get GeoJSON of a place at given revision
    '''
    place = get_place_or_404(id)

    if request.method == 'GET':
        revision = Place.objects.revision(place,revision)
        geojson = revision['place'].to_geojson()
        geojson['version'] = revision['version']
        geojson['digest'] = revision['digest']
        return render_to_json_response(geojson)


    elif request.method == 'PUT':
        user_id = request.user.id
        username = request.user.username
        data = json.loads(request.body)
        comment = data.get('comment', '')
        metadata = {
            'user': username,
            'user_id': user_id,
            'comment': comment
        }       
        place = place.rollback(revision, metadata=metadata) #FIXME: handle invalid revision ids
        return render_to_json_response(place.to_geojson())

    else:
        return render_to_json_response({'error': 'Method Not Allowed'}, status=405)
Ejemplo n.º 4
0
def within(request, id):
    ''' GET
       Gets places where the centroids are within this place's geometry
       Results are unsorted
       also accept
            per_page: results per page int, default=100
            page: page no (starting with 1) int, default=1

        Returns:
            GeoJSON feed of search results
            Extra properties of feed:
                total: total number of results
                max_score: max score in results (?)
                page: page number
    '''
    place = get_place_or_404(id)
    per_page = int(request.GET.get("per_page", 100))
    page = int(request.GET.get("page", 1))
    page_0 = page - 1

    result = Place.objects.within(place, per_page=per_page, page=page_0)

    total = result['total']
    pages = int(math.ceil(total / (per_page + .0)))  #get total number of pages

    ret = {
        'type': 'FeatureCollection',
        'features': [p.to_geojson() for p in result['places']],
        'total': total,
        'page': result['page'],
        'pages': pages,
        'per_page': result['per_page'],
        'max_score': result['max_score']
    }
    return render_to_json_response(ret)
Ejemplo n.º 5
0
def place_json(request, id):

    place = get_place_or_404(id)
              
    if request.method == 'GET':
        '''
            Return GeoJSON for Place
        '''
        geo_json = place.to_geojson()
        return render_to_json_response(geo_json)
    
    elif request.method == 'PUT':
        '''
            Takes a GeoJSON string as PUT data and saves Place
            Saves and returns  back GeoJSON for place.
        '''
        #FIXME: check permissions
#        if not request.user.is_staff():
#            return render_to_json_response({'error': 'You do not have permissions to edit this place.'}, status=403) 
        geojson = json.loads(request.body)
        if geojson.has_key("comment"):
            comment = geojson.pop("comment")
        else:
            comment = ''
        json_obj = geojson.pop("properties")
        json_obj['geometry'] = geojson['geometry']

        #handle getting centroid:
        if json_obj['geometry']:
            centroid = GEOSGeometry(json.dumps(json_obj['geometry'])).centroid
            json_obj['centroid'] = list(centroid.coords)
        else:
            json_obj['centroid'] = []
            json_obj['geometry'] = {}
       
        #updates place but doesn't save it
        place.update(json_obj)
        
        user_id = request.user.id
        username = request.user.username
        metadata = { 
            'user': username,
            'user_id': user_id,
            'comment': comment
        }

        place.save(metadata=metadata)
        new_place = place.copy()#returns a copy of the newly saved place
        
        return render_to_json_response(new_place.to_geojson())
        

    elif request.method == 'DELETE':
        return render_to_json_response({'error': 'Not implemented'}, status=501)

    else:
        return render_to_json_response({'error': 'Method Not Allowed'}, status=405)
Ejemplo n.º 6
0
def add_delete_relation(request, id1, relation_type, id2):
    place1 = get_place_or_404(id1)
    place2 = get_place_or_404(id2)
    if relation_type not in Place.RELATION_CHOICES.keys():
        return render_to_json_response({'error': 'Invalid relation type'},
                                       status=404)

    user_id = request.user.id
    username = request.user.username
    comment = json.loads(request.body).get("comment", "")

    metadata = {'user': username, 'user_id': user_id, 'comment': comment}

    if request.method == 'PUT':
        place1.add_relation(place2, relation_type, metadata)
    if request.method == 'DELETE':
        place1.delete_relation(place2, metadata)

    return relations(request, place1.id)
Ejemplo n.º 7
0
def place_json(request, id):

    place = get_place_or_404(id)

    if request.method == 'GET':
        '''
            Return GeoJSON for Place
        '''
        geo_json = place.to_geojson()
        return render_to_json_response(geo_json)

    elif request.method == 'PUT':
        '''
            Takes a GeoJSON string as PUT data and saves Place
            Saves and returns  back GeoJSON for place.
        '''
        #FIXME: check permissions
        #        if not request.user.is_staff():
        #            return render_to_json_response({'error': 'You do not have permissions to edit this place.'}, status=403)
        geojson = json.loads(request.body)
        if geojson.has_key("comment"):
            comment = geojson.pop("comment")
        else:
            comment = ''
        json_obj = geojson.pop("properties")
        json_obj['geometry'] = geojson['geometry']

        #handle getting centroid:
        if json_obj['geometry']:
            centroid = GEOSGeometry(json.dumps(json_obj['geometry'])).centroid
            json_obj['centroid'] = list(centroid.coords)
        else:
            json_obj['centroid'] = []
            json_obj['geometry'] = {}

        #updates place but doesn't save it
        place.update(json_obj)

        user_id = request.user.id
        username = request.user.username
        metadata = {'user': username, 'user_id': user_id, 'comment': comment}

        place.save(metadata=metadata)
        new_place = place.copy()  #returns a copy of the newly saved place

        return render_to_json_response(new_place.to_geojson())

    elif request.method == 'DELETE':
        return render_to_json_response({'error': 'Not implemented'},
                                       status=501)

    else:
        return render_to_json_response({'error': 'Method Not Allowed'},
                                       status=405)
Ejemplo n.º 8
0
def add_delete_relation(request, id1, relation_type, id2):
    place1 = get_place_or_404(id1)
    place2 = get_place_or_404(id2)
    if relation_type not in Place.RELATION_CHOICES.keys():
        return render_to_json_response({'error': 'Invalid relation type'}, status=404)

  
    user_id = request.user.id
    username = request.user.username
    comment = json.loads(request.body).get("comment", "")

    metadata = {
        'user': username,
        'user_id': user_id,
        'comment': comment
    }

    if request.method == 'PUT':
        place1.add_relation(place2, relation_type, metadata)
    if request.method == 'DELETE':
        place1.delete_relation(place2, metadata)

    return relations(request, place1.id)
Ejemplo n.º 9
0
def relations(request, id):
    '''
        Returns GeoJSON feed for related places. Adds a property 'relation_type' to geojson properties to indicate type of relations
    '''

    place = get_place_or_404(id)

    features = []
    if place.relationships:
        for obj in place.relationships:
            geojson = Place.objects.get(obj['id']).to_geojson()
            geojson['properties']['relation_type'] = obj['type']
            features.append(geojson)

    relations_geojson = {'type': 'FeatureCollection', 'features': features}

    return render_to_json_response(relations_geojson)
Ejemplo n.º 10
0
def relations(request, id):
    '''
        Returns GeoJSON feed for related places. Adds a property 'relation_type' to geojson properties to indicate type of relations
    '''

    place = get_place_or_404(id)

    features = []
    if place.relationships:
        for obj in place.relationships:
            geojson = Place.objects.get(obj['id']).to_geojson()
            geojson['properties']['relation_type'] = obj['type']
            features.append(geojson)

    relations_geojson = {
        'type': 'FeatureCollection',
        'features': features
    }
   
    return render_to_json_response(relations_geojson)