Example #1
0
 def test_two_types_in_params(self):
     q = query_args(FakeRequest({}), 'json', 'WMC,EUR')
     self.q_equality(
         q,
         Q(
             generation_high__gte=self.active_generation.id,
             generation_low__lte=self.active_generation.id,
         ) & (Q() | Q(type__code__in=['WMC', 'EUR'])))
Example #2
0
 def test_one_country_in_query(self):
     q = query_args(FakeRequest({'country': 'DE'}), 'json', None)
     self.q_equality(
         q,
         Q(
             generation_high__gte=self.active_generation.id,
             generation_low__lte=self.active_generation.id,
         ) & (Q() | Q(country__code='DE') | Q(countries__code='DE')))
Example #3
0
 def test_one_type_in_params(self):
     q = query_args(FakeRequest({}), 'json', 'WMC')
     self.q_equality(
         q,
         Q(
             generation_high__gte=self.active_generation.id,
             generation_low__lte=self.active_generation.id,
         ) & (Q() | Q(type__code='WMC')))
Example #4
0
 def test_no_type(self):
     q = query_args(FakeRequest({}), 'json', None)
     self.q_equality(
         q,
         Q(
             generation_high__gte=self.active_generation.id,
             generation_low__lte=self.active_generation.id,
         ))
Example #5
0
def convert_address(request, format='json'):
    address = request.GET.get('address')
    if not address:
        raise ViewException(format, 'No address was provided.', 400)

    converter = AddressConverter()
    locations = converter.resolve_address(address, partial=bool(request.GET.get('partial')))

    # this is a copy from mapit.views.areas.areas_by_point
    # because it's hard to reuse their code :(

    if PYGDAL:
        from osgeo import gdal
        gdal.UseExceptions()

    # we find areas for every lat/long coord we got back
    areas = []
    type = request.GET.get('type', '')
    for coords in locations:
        location = Point(float(coords['lng']), float(coords['lat']), srid=4326)
        try:
            location.transform(settings.MAPIT_AREA_SRID, clone=True)
        except:
            raise ViewException(format, _('Point outside the area geometry'), 400)

        args = query_args(request, format)
        if type:
            args = dict(("area__%s" % k, v) for k, v in args.items())
            # So this is odd. It doesn't matter if you specify types, PostGIS will
            # do the contains test on all the geometries matching the bounding-box
            # index, even if it could be much quicker to filter some out first
            # (ie. the EUR ones).
            coords['areas'] = []
            args['polygon__bbcontains'] = location
            shapes = Geometry.objects.filter(**args).defer('polygon')
            for shape in shapes:
                try:
                    area = Area.objects.get(polygons__id=shape.id, polygons__polygon__contains=location)
                    coords['areas'].append(str(area.id))
                    areas.append(area)
                except:
                    pass
        else:
            geoms = list(Geometry.objects.filter(polygon__contains=location).defer('polygon'))
            args['polygons__in'] = geoms
            matches = Area.objects.filter(**args).all()
            coords['areas'] = [str(m.id) for m in matches]
            areas.extend(matches)

    areas = add_codes(areas)
    if format == 'html':
        return output_html(request, _("Areas matching the address '{0}'").format(address), areas, indent_areas=True)

    # hack to include the geocoded addresses in the results
    data = iterdict(chain(
        ((area.id, area.as_dict()) for area in areas),
        [("addresses", locations)]))
    return output_json(data)
Example #6
0
 def test_two_types_in_params(self):
     q = query_args(FakeRequest({}), 'json', 'WMC,EUR')
     self.q_equality(
         q,
         Q(
             generation_high__gte=self.active_generation.id,
             generation_low__lte=self.active_generation.id,
         ) & (Q() | Q(type__code__in=['WMC', 'EUR']))
     )
Example #7
0
 def test_one_type_in_params(self):
     q = query_args(FakeRequest({}), 'json', 'WMC')
     self.q_equality(
         q,
         Q(
             generation_high__gte=self.active_generation.id,
             generation_low__lte=self.active_generation.id,
         ) & (Q() | Q(type__code='WMC'))
     )
Example #8
0
 def test_no_type(self):
     q = query_args(FakeRequest({}), 'json', None)
     self.q_equality(
         q,
         Q(
             generation_high__gte=self.active_generation.id,
             generation_low__lte=self.active_generation.id,
         )
     )
Example #9
0
 def test_two_countries_in_query(self):
     q = query_args(FakeRequest({'country': 'DE,FR'}), 'json', None)
     self.q_equality(
         q,
         Q(
             generation_high__gte=self.active_generation.id,
             generation_low__lte=self.active_generation.id,
         ) & (Q() | Q(country__code__in=['DE', 'FR']) | Q(countries__code__in=['DE', 'FR']))
     )
Example #10
0
 def test_min_generation_specified(self):
     q = query_args(FakeRequest({'min_generation': self.old_generation.id}),
                    'json', None)
     self.q_equality(
         q,
         Q(
             generation_high__gte=self.old_generation.id,
             generation_low__lte=self.active_generation.id,
         ))
Example #11
0
 def test_min_generation_specified(self):
     q = query_args(
         FakeRequest({'min_generation': self.old_generation.id}),
         'json',
         None)
     self.q_equality(
         q,
         Q(
             generation_high__gte=self.old_generation.id,
             generation_low__lte=self.active_generation.id,
         )
     )
Example #12
0
def convert_address(request, format='json'):
    address = request.GET.get('address')
    if not address:
        raise ViewException(format, 'No address was provided.', 400)

    converter = AddressConverter()
    locations = converter.resolve_address(address,
                                          partial=bool(
                                              request.GET.get('partial')))

    # this is a copy from mapit.views.areas.areas_by_point
    # because it's hard to reuse their code :(

    if PYGDAL:
        from osgeo import gdal
        gdal.UseExceptions()

    # we find areas for every lat/long coord we got back
    areas = []
    type = request.GET.get('type', '')
    for coords in locations:
        location = Point(float(coords['lng']), float(coords['lat']), srid=4326)
        try:
            location.transform(settings.MAPIT_AREA_SRID, clone=True)
        except:
            raise ViewException(format, _('Point outside the area geometry'),
                                400)

        args = query_args(request, format)
        if type:
            args = dict(("area__%s" % k, v) for k, v in args.items())
            # So this is odd. It doesn't matter if you specify types, PostGIS will
            # do the contains test on all the geometries matching the bounding-box
            # index, even if it could be much quicker to filter some out first
            # (ie. the EUR ones).
            coords['areas'] = []
            args['polygon__bbcontains'] = location
            shapes = Geometry.objects.filter(**args).defer('polygon')
            for shape in shapes:
                try:
                    area = Area.objects.get(
                        polygons__id=shape.id,
                        polygons__polygon__contains=location)
                    coords['areas'].append(str(area.id))
                    areas.append(area)
                except:
                    pass
        else:
            geoms = list(
                Geometry.objects.filter(
                    polygon__contains=location).defer('polygon'))
            args['polygons__in'] = geoms
            matches = Area.objects.filter(**args).all()
            coords['areas'] = [str(m.id) for m in matches]
            areas.extend(matches)

    areas = add_codes(areas)
    if format == 'html':
        return output_html(
            request,
            _("Areas matching the address '{0}'").format(address),
            areas,
            indent_areas=True)

    # hack to include the geocoded addresses in the results
    data = iterdict(
        chain(((area.id, area.as_dict()) for area in areas),
              [("addresses", locations)]))
    return output_json(data)