示例#1
0
    def generated_map(self, context, lon, lat, width, height):
        hash, metadata = fit_to_map(
            (lon, lat, 'green'),
            [],
            0,
            context['zoom'],
            width,
            height,
        )
        url = reverse('osm:generated_map', args=[hash])

        if self.just_url:
            return url
        else:
            return '<img src="%s" alt="Map"/>' % xml.sax.saxutils.escape(url)
示例#2
0
    def handle_GET(self, request, context, ptypes, entity=None):

        entity_types, entities, point = (
            context['entity_types'], context['entities'],
            context['point'],
        )

        if entity and not point:
            context = {'entity': entity}
            raise Exception
            return self.render(request, context, 'places/entity_without_location')

        if context['zoom'] is None:
            min_points = 5
        else:
            min_points = 0

        map_hash, (new_points, zoom) = fit_to_map(
            centre_point = (point[0], point[1], 'green'),
            points = ((e.location[0], e.location[1], 'red') for e in entities),
            min_points = min_points,
            zoom = context['zoom'],
            width = request.map_width,
            height = request.map_height,
        )

        entities = [[entities[i] for i in b] for a,b in new_points]

        found_entity_types = set()
        for e in chain(*entities):
            e.bearing = e.get_bearing(point)
            found_entity_types |= set(e.all_types.all())
        found_entity_types -= set(entity_types)


        context.update({
            'entities': entities,
            'zoom': zoom,
            'map_hash': map_hash,
            'count': sum(map(len, entities)),
            'entity_types': entity_types,
            'found_entity_types': found_entity_types,
        })
        #raise Exception(context)
        return self.render(request, context, 'places/nearby_detail')
示例#3
0
    def handle_GET(self, request, context, ptypes, entity=None):

        entity_types, entities, point = (context["entity_types"], context["entities"], context["point"])

        if entity and not point:
            context = {"entity": entity}
            raise Exception
            return self.render(request, context, "places/entity_without_location")

        if context["zoom"] is None:
            min_points = 5
        else:
            min_points = 0

        map_hash, (new_points, zoom) = fit_to_map(
            centre_point=(point[0], point[1], "green"),
            points=((e.location[0], e.location[1], "red") for e in entities),
            min_points=min_points,
            zoom=context["zoom"],
            width=request.map_width,
            height=request.map_height,
        )

        entities = [[entities[i] for i in b] for a, b in new_points]

        found_entity_types = set()
        for e in chain(*entities):
            e.bearing = e.get_bearing(point)
            found_entity_types |= set(e.all_types.all())
        found_entity_types -= set(entity_types)

        context.update(
            {
                "entities": entities,
                "zoom": zoom,
                "map_hash": map_hash,
                "count": sum(map(len, entities)),
                "entity_types": entity_types,
                "found_entity_types": found_entity_types,
            }
        )
        # raise Exception(context)
        return self.render(request, context, "places/nearby_detail")
示例#4
0
    def handle_POST(cls, request, context):
        form = context['form']

        if form.is_valid() and form.cleaned_data['force']:
            return cls.handle_set_location(request, context)

        if form.is_valid():
            results = geocode(form.cleaned_data['name'], cls.conf.local_name)

            if len(results) == 1:
                form.cleaned_data.update(results[0])
                return cls.handle_set_location(request, context)

            if results:
                points = [(o['location'][0], o['location'][1], 'red') for o in results]
                map_hash, (new_points, zoom) = fit_to_map(
                    None,
                    points = points,
                    min_points = len(points),
                    zoom = None if len(points)>1 else 15,
                    width = request.map_width,
                    height = request.map_height,
                )
            else:
                map_hash, zoom = None, None
            context.update({
                'results': results,
                'map_url': reverse('osm:generated_map', args=[map_hash]) if map_hash else None,
                'zoom': zoom,
                'zoom_controls': False,
            })

        if context['format'] == 'embed':
            if form.is_valid():
                return cls.render(request, context, 'geolocation/update_location_confirm')
            else:
                return cls.render(request, context, 'geolocation/update_location_embed')
        else:
            return cls.render(request, context, 'geolocation/update_location')
示例#5
0
    def handle_with_location(cls, request, context):
        points = []
        location = request.session.get('geolocation:location')

        all_libraries = context['item'].libraries.items()
        libraries, stacks = [], []
        for library, items in all_libraries:
            if library.oxpoints_id and library.oxpoints_entity.is_stack:
                stacks.append( (library, items) )
            else:
                libraries.append( (library, items) )

        if libraries:
            entity_ids = set(l.oxpoints_id for l in context['item'].libraries if l.oxpoints_id)
            entities = Entity.objects.filter(_identifiers__scheme='oxpoints', _identifiers__value__in = entity_ids)
            if location:
                point = Point(location[1], location[0], srid=4326)

                with_location = entities.filter(location__isnull=False)
                without_location = entities.filter(location__isnull=True)

                if with_location.count() == 0:
                    return cls.handle_without_location(request, context)

                entities = chain(
                    with_location.distance(point).order_by('distance'),
                    without_location.order_by('title'),
                )

                ordering = dict((e.identifiers['oxpoints'], i) for i, e in enumerate(entities))

                libraries.sort(key=lambda l:(ordering[l[0].oxpoints_id] if l[0].oxpoints_id else float('inf')))

            else:
                entities.order_by('title')

            for library, books in libraries:
                if not (library.oxpoints_id and library.oxpoints_entity.location):
                    library.has_location = False
                    continue
                color = AVAIL_COLORS[max(b['availability'] for b in books)]
                points.append( (
                    library.oxpoints_entity.location[0],
                    library.oxpoints_entity.location[1],
                    color,
                ) )

            map_hash, (new_points, zoom) = fit_to_map(
                centre_point = (location[0], location[1], 'green') if location else None,
                points = points,
                min_points = 0 if context['zoom'] else len(points),
                zoom = context['zoom'],
                width = request.map_width,
                height = request.map_height,
            )

            # Yes, this is weird. fit_to_map() groups libraries with the same location
            # so here we add a marker_number to each library to display in the
            # template.
            lib_iter = iter(libraries)
            for i, (a,b) in enumerate(new_points):
                for j in range(len(b)):
                    lib_iter.next()[0].marker_number = i + 1
            # Finish off by adding a marker_number for those that aren't on the map.
            # (lib_iter still contains the remaining items after the above calls to
            # next() ).
            for library in lib_iter:
                library[0].marker_number = None

            context['zoom'] = zoom
            context['map_hash'] = map_hash

        context.update({
            'libraries': libraries,
            'stacks': stacks
        })

        return cls.render(request, context, 'z3950/item_detail')