示例#1
0
def public_instances_geojson(request):
    def instance_geojson(instance):
        return {
            'type': 'Feature',
            'geometry': {
                'type':
                'Point',
                'coordinates':
                [instance.center_lat_lng.x, instance.center_lat_lng.y]
            },
            'properties': {
                'name':
                instance.name,
                'url':
                reverse('instance_index_view',
                        kwargs={'instance_url_name': instance.url_name}),
                'plot_count':
                instance.plot_count()
            }
        }

    instances = (Instance.objects.filter(is_public=True).filter(
        get_viewable_instances_filter()))

    return [instance_geojson(instance) for instance in instances]
示例#2
0
def instances_closest_to_point(request, lat, lng):
    """
    Get all the info we need about instances near a given point
    Includes only public instances the user does not belong to.
    If a user has been specified instances that user belongs to will
    also be included in a separate list.

    Unlike instance_info, this does not return the field permissions for the
    instance
    """
    user = request.user
    user_instance_ids = []
    if user and not user.is_anonymous():
        user_instance_ids = InstanceUser.objects.filter(user=user)\
                                        .values_list('instance_id', flat=True)\
                                        .distinct()

    point = Point(float(lng), float(lat), srid=4326)

    try:
        max_instances = int(request.GET.get('max', '10'))

        if not (1 <= max_instances <= 500):
            raise ValueError()
    except ValueError:
        raise HttpBadRequestException(
            'The max parameter must be a number between 1 and 500')

    try:
        distance = float(request.GET.get(
            'distance', settings.NEARBY_INSTANCE_RADIUS))
    except ValueError:
        raise HttpBadRequestException(
            'The distance parameter must be a number')

    instances = Instance.objects \
                        .filter(get_viewable_instances_filter())
    personal_predicate = Q(pk__in=user_instance_ids)

    def get_annotated_contexts(instances):
        results = []
        for instance in instances:
            instance_info = _instance_info_dict(instance)
            d = D(m=point.distance(instance.bounds.geom)).km
            instance_info['distance'] = d
            results.append(instance_info)
        return sorted(results, key=itemgetter('distance'))

    return {
        'nearby': get_annotated_contexts(
            instances
            .filter(is_public=True)
            .filter(bounds__geom__distance_lte=(
                point, D(m=distance)))
            .exclude(personal_predicate)
            [0:max_instances]),
        'personal': get_annotated_contexts(
            instances.filter(personal_predicate))
    }
示例#3
0
def instances_closest_to_point(request, lat, lng):
    """
    Get all the info we need about instances near a given point
    Includes only public instances the user does not belong to.
    If a user has been specified instances that user belongs to will
    also be included in a separate list.

    Unlike instance_info, this does not return the field permissions for the
    instance
    """
    user = request.user
    user_instance_ids = []
    if user and not user.is_anonymous():
        user_instance_ids = InstanceUser.objects.filter(user=user)\
                                        .values_list('instance_id', flat=True)\
                                        .distinct()

    point = Point(float(lng), float(lat), srid=4326)

    try:
        max_instances = int(request.GET.get('max', '10'))

        if not (1 <= max_instances <= 500):
            raise ValueError()
    except ValueError:
        raise HttpBadRequestException(
            'The max parameter must be a number between 1 and 500')

    try:
        distance = float(
            request.GET.get('distance', settings.NEARBY_INSTANCE_RADIUS))
    except ValueError:
        raise HttpBadRequestException(
            'The distance parameter must be a number')

    instances = Instance.objects \
                        .filter(get_viewable_instances_filter())
    personal_predicate = Q(pk__in=user_instance_ids)

    def get_annotated_contexts(instances):
        results = []
        for instance in instances:
            instance_info = _instance_info_dict(instance)
            d = D(m=point.distance(instance.bounds.geom)).km
            instance_info['distance'] = d
            results.append(instance_info)
        return sorted(results, key=itemgetter('distance'))

    return {
        'nearby':
        get_annotated_contexts(
            instances.filter(is_public=True).filter(
                bounds__geom__distance_lte=(point, D(m=distance))).exclude(
                    personal_predicate)[0:max_instances]),
        'personal':
        get_annotated_contexts(instances.filter(personal_predicate))
    }
示例#4
0
文件: misc.py 项目: lorenanicole/OTM2
def public_instances_geojson(request):
    def instance_geojson(instance):
        return {
            'type': 'Feature',
            'geometry': {
                'type':
                'Point',
                'coordinates':
                [instance.center_lat_lng.x, instance.center_lat_lng.y]
            },
            'properties': {
                'name':
                instance.name,
                'url':
                reverse('instance_index_view',
                        kwargs={'instance_url_name': instance.url_name}),
                'tree_count':
                instance.tree_count,
                'plot_count':
                instance.plot_count
            }
        }

    tree_query = "SELECT COUNT(*) FROM treemap_tree WHERE "\
        "treemap_tree.instance_id = treemap_instance.id"

    plot_query = "SELECT COUNT(*) FROM treemap_mapfeature"\
        " WHERE treemap_mapfeature.instance_id = treemap_instance.id"\
        " AND treemap_mapfeature.feature_type = 'Plot'"

    # You might think you can do .annotate(tree_count=Count('tree'))
    # But it is horribly slow due to too many group bys
    instances = (Instance.objects.filter(is_public=True).filter(
        get_viewable_instances_filter()).extra(select={
            'tree_count': tree_query,
            'plot_count': plot_query
        }))

    return [instance_geojson(instance) for instance in instances]
示例#5
0
def public_instances_geojson(request):
    def instance_geojson(instance):
        return {
            'type': 'Feature',
            'geometry': {
                'type': 'Point',
                'coordinates': [instance.center_lat_lng.x,
                                instance.center_lat_lng.y]
            },
            'properties': {
                'name': instance.name,
                'url': reverse(
                    'instance_index_view',
                    kwargs={'instance_url_name': instance.url_name}),
                'plot_count': instance.plot_count()
            }
        }

    instances = (Instance.objects
                 .filter(is_public=True)
                 .filter(get_viewable_instances_filter()))

    return [instance_geojson(instance) for instance in instances]
示例#6
0
def public_instances_geojson(request):
    def instance_geojson(instance):
        return {
            'type': 'Feature',
            'geometry': {
                'type': 'Point',
                'coordinates': [instance.center_lat_lng.x,
                                instance.center_lat_lng.y]
            },
            'properties': {
                'name': instance.name,
                'url': reverse(
                    'instance_index_view',
                    kwargs={'instance_url_name': instance.url_name}),
                'tree_count': instance.tree_count,
                'plot_count': instance.plot_count
            }
        }

    tree_query = "SELECT COUNT(*) FROM treemap_tree WHERE "\
        "treemap_tree.instance_id = treemap_instance.id"

    plot_query = "SELECT COUNT(*) FROM treemap_mapfeature"\
        " WHERE treemap_mapfeature.instance_id = treemap_instance.id"\
        " AND treemap_mapfeature.feature_type = 'Plot'"

    # You might think you can do .annotate(tree_count=Count('tree'))
    # But it is horribly slow due to too many group bys
    instances = (Instance.objects
                 .filter(is_public=True)
                 .filter(get_viewable_instances_filter())
                 .extra(select={
                     'tree_count': tree_query,
                     'plot_count': plot_query
                 }))

    return [instance_geojson(instance) for instance in instances]
示例#7
0
def public_instances(request):
    return _contextify_instances(Instance.objects
                                 .filter(is_public=True)
                                 .filter(get_viewable_instances_filter()))
示例#8
0
def public_instances(request):
    return _contextify_instances(
        Instance.objects.filter(is_public=True).filter(
            get_viewable_instances_filter()))