def get(self, request): #takes a latitude and longitude and returns artifacts within 2 miles try: lon = float(request.GET.get('lon', None)) lat = float(request.GET.get('lat', None)) except: return HttpResponse('You Must Specify a latitude and longitude', content_type='application/json', status=status.HTTP_400_BAD_REQUEST) username = request.user.username pnt = Point(lon, lat) # Within a two miles. max_dist = D(mi=2) # 'location' is the fieldname from our ``SearchIndex``... # Do the radius query. sqs = SearchQuerySet().distance('location',pnt).order_by('distance') within = sqs.dwithin('location', pnt, max_dist) if len(within) != len(sqs) and len(within) !=0 and len(sqs) !=0: closest_not_within = pnt.distance(sqs[len(within)])*100*0.6214 else: closest_not_within='null' the_data = [] for result in sqs: # get number of pickups as well as whether or not the user has already picked up already_picked_up = (username in result.pickup_count) _pickup_count = count_m2m(result.pickup_count) # get number of upvotes as well as whether or not the user has already upvoted already_upvoted = (username in result.upvoted) upvote_count = count_m2m(result.upvoted) # make a point artifactpnt = Point(float(result.longitude), float(result.latitude)) loopy_data = {'author': result.author, 'upvote_count': upvote_count, 'already_upvoted': already_upvoted, 'already_picked_up': already_picked_up, 'longitude': result.longitude, 'radius': result.radius, 'latitude': result.latitude, 'pub_date': str(result.pub_date), 'pickup_count': _pickup_count, 'distance': (artifactpnt.distance(pnt) *100)* 0.6214, 'pk': result.pk} the_data += [loopy_data] the_data = [json.dumps(the_data + [{'closest_not_within':closest_not_within}])] return HttpResponse(the_data, content_type='application/json', status=status.HTTP_200_OK)
def search(self): # First, store the SearchQuerySet received from other processing. (the main work is run internally by Haystack here). sqs = super(AdSearchForm, self).search() try: if self.cleaned_data.get('lat') and self.cleaned_data.get( 'lng') and self.cleaned_data.get('radius'): location = Point(self.cleaned_data.get('lng'), self.cleaned_data.get('lat')) max_dist = D( km=self.cleaned_data.get('radius') ) # Un bug en haystack demanda que se multiplique por 1000. asique ahi lo esta tomando en metros sqs = sqs.dwithin('location', location, max_dist) if self.cleaned_data.get('order_by'): # TODO: Add support for multiple order fields # TODO: Add support for non-numeric field ordering ( so far only "pride" and pub_date" are working) order_by = self.cleaned_data.get('order_by') # if Search Query Set is order by distance, it might no be secure to order by any other field ( accordind to the hasytack codmumentation ) if ((order_by == 'distance' or order_by == '-distance')) and self.cleaned_data.get( 'lat') and self.cleaned_data.get('lng'): location = Point(self.cleaned_data.get('lng'), self.cleaned_data.get('lat')) sqs = sqs.distance('location', location).order_by(order_by) else: sqs = sqs.order_by(order_by) except: pass # if something goes wrong if not self.is_valid(): return self.no_query_found() return sqs
def test_generate_bounding_box(self): downtown_bottom_left = Point(-95.23947, 38.9637903) downtown_top_right = Point(-95.23362278938293, 38.973081081164715) ((min_lat, min_lng), (max_lat, max_lng)) = generate_bounding_box(downtown_bottom_left, downtown_top_right) self.assertEqual(min_lat, 38.9637903) self.assertEqual(min_lng, -95.23947) self.assertEqual(max_lat, 38.973081081164715) self.assertEqual(max_lng, -95.23362278938293)
def test_generate_bounding_box_crossing_line_date(self): downtown_bottom_left = Point(95.23947, 38.9637903) downtown_top_right = Point(-95.23362278938293, 38.973081081164715) ((south, west), (north, east)) = generate_bounding_box(downtown_bottom_left, downtown_top_right) self.assertEqual(south, 38.9637903) self.assertEqual(west, 95.23947) self.assertEqual(north, 38.973081081164715) self.assertEqual(east, -95.23362278938293)
def test_within(self): # This is a meaningless query but we're just confirming that the manager updates the parameters here: p1 = Point(-90, -90) p2 = Point(90, 90) sqs = self.search_index.objects.within('location', p1, p2) self.assertTrue(isinstance(sqs, SearchQuerySet)) params = sqs.query.build_params() self.assertIn('within', params) self.assertDictEqual(params['within'], {'field': 'location', 'point_1': p1, 'point_2': p2})
def setUp(self): super(SpatialSolrTestCase, self).setUp() self.ui = connections[self.using].get_unified_index() self.checkindex = self.ui.get_index(Checkin) self.checkindex.reindex(using=self.using) self.sqs = SearchQuerySet().using(self.using) self.downtown_pnt = Point(-95.23592948913574, 38.97127105172941) self.downtown_bottom_left = Point(-95.23947, 38.9637903) self.downtown_top_right = Point(-95.23362278938293, 38.973081081164715) self.lawrence_bottom_left = Point(-95.345535, 39.002643) self.lawrence_top_right = Point(-95.202713, 38.923626)
def homepage(request): ip_forwarded = request.META.get('HTTP_X_FORWARDED_FOR') if ip_forwarded: ip = ip_forwarded.split(',')[0] else: ip = request.META.get('REMOTE_ADDR') #convert ip into zipcode #if user doesn't input zipcode/location, default will be the automatically #converted zipcode if request.GET.get('zipcode'): query = request.GET.get('q') zip_code = request.GET.get('zipcode') """ search = ZipcodeSearchEngine() zip = search.by_zipcode(zip_code) longitude = zip['Longitude'] lat = zip['Latitude'] """ zip = zipcode.isequal(str(zip_code)) longitude = 0.00 lat = 0.00 sqs = SearchQuerySet().dwithin('location', Point(longitude, lat), D(mi=30)) f = PostingFilter(request.GET, queryset=sqs) context = { 'city': zip['City'], 'filter': f, 'sqs': sqs, 'zipcode':'00001', 'query': query, } else: #Temporary stand-in ip-address query = request.GET.get('q') ip = '24.6.173.143' g = GeoIP2() location_dict = g.city(ip) city = location_dict['city'] lat = location_dict['latitude'] longitude = location_dict['longitude'] zip_code = location_dict['postal_code'] sqs = SearchQuerySet().dwithin('location', Point(longitude, lat), D(mi=30)) f = PostingFilter(request.GET, queryset=sqs) context = { 'city': city, 'filter': f, 'sqs': sqs, 'zipcode': '10001', 'query': query, } return render(request, 'search/filter_template.html', context)
def _get_location(self, location): if not all((self._point, self._max_range)): _, coords, radius = GoogleGeocoder().geocode(location) self._max_range = radius or DEFAULT_RADIUS if all(coords): self._point = Point(coords) return (self._point, self._max_range)
def test_ensure_point(self): self.assertRaises(SpatialError, ensure_point, [38.97127105172941, -95.23592948913574]) self.assertRaises( SpatialError, ensure_point, GEOSGeometry('POLYGON((-95 38, -96 40, -97 42, -95 38))')) ensure_point(Point(-95.23592948913574, 38.97127105172941))
def coordinates(self): try: from haystack.utils.geo import Point except ImportError: return None else: return Point(self.longitude, self.latitude, srid=4326)
def get_location(self): # Nothing special about this Point, but ensure that's we don't have to worry # about import paths. from haystack.utils.geo import Point pnt = Point(self.longitude, self.latitude) return pnt
def convert(self, value): from haystack.utils.geo import ensure_point, Point if value is None: return None if hasattr(value, 'geom_type'): value = ensure_point(value) return value if isinstance(value, six.string_types): lat, lng = value.split(',') elif isinstance(value, (list, tuple)): # WGS-84 format if isinstance(value[0], str) and len(value) == 1: value_string = value[0] lng = value_string[:value_string.find(',')] lat = value_string[value_string.find(',') + 1:] else: # GeoJSON-alike lat, lng = value[1], value[0] elif isinstance(value, dict): lat = value.get('lat', 0) lng = value.get('lon', 0) else: raise TypeError('Unable to extract coordinates from %r' % value) value = Point(float(lng), float(lat)) return value
def search(self): # First, store the SearchQuerySet received from other processing. sqs = super(ResourceSearchForm, self).search() address = self.cleaned_data.get('a') if (address): g = geocoders.Nominatim( ) # this should be changed to openstreetmap try: place, (lat, lng) = g.geocode(address) print "address: %s, lat : %g, lng : %g" % (address, lat, lng) loc = Point(lng, lat) max_dist = D(km=10) #sqs = sqs.dwithin('location',loc,max_dist).distance('location',loc) except geopy.exc.GeocoderServiceError: pass time = self.cleaned_data.get('t') if (time): # extract serialized events from search query set index events = sqs.exclude(event='toto') excluded = list() for e in events: if e.event: # we only check if we can go to the next upcoming occurrence # checking all occurrences would be too costly ev = deserialize(e.event).after(datetime.now()) if not ev in time.occurrences(dtstart=ev): excluded.append(e.pk) if (excluded): sqs = sqs.exclude(id__in=excluded) return sqs
def test_ensure_geometry(self): self.assertRaises(SpatialError, ensure_geometry, [38.97127105172941, -95.23592948913574]) ensure_geometry( GEOSGeometry("POLYGON((-95 38, -96 40, -97 42, -95 38))")) ensure_geometry( GEOSGeometry("POINT(-95.23592948913574 38.97127105172941)")) ensure_geometry(Point(-95.23592948913574, 38.97127105172941))
def get_location(self): """ This method is utilised by Django Haystack in construction of the Solr index. It is responsible for converting the lat/long into a single geo point. """ return Point(self.longitude, self.latitude)
def test_distance(self): p = Point(0, 0) sqs = self.search_index.objects.distance('location', p) self.assertTrue(isinstance(sqs, SearchQuerySet)) params = sqs.query.build_params() self.assertIn('distance_point', params) self.assertDictEqual(params['distance_point'], {'field': 'location', 'point': p})
def build_form(self, form_kwargs=None): """ Instantiates the form the class should use to process the search query. """ data = None kwargs = {"load_all": self.load_all} if form_kwargs: kwargs.update(form_kwargs) if len(self.request.GET): data = self.request.GET # Radius is defaulted to 30 miles #import pdb; pdb.set_trace() if self.request.GET.get('zipcode'): self.zipcode = self.request.GET.get('zipcode') """ search = ZipcodeSearchEngine() zip = search.by_zipcode(zip_code) longitude = zip['Longitude'] lat = zip['Latitude'] """ zip = zipcode.isequal(str(self.zipcode)) longitude = zip.lng lat = zip.lat self.city = zip.city self.state = zip.state self.searchqueryset = SearchQuerySet().dwithin('location', Point(longitude, lat), D(mi=30)) #f = PostingFilter(self.request.GET, queryset=sqs) else: ip = '24.6.173.143' g = GeoIP2() location_dict = g.city(ip) self.city = location_dict['city'] self.state = location_dict['region'] lat = location_dict['latitude'] longitude = location_dict['longitude'] self.zipcode = location_dict['postal_code'] self.searchqueryset = SearchQuerySet().dwithin('location', Point(longitude, lat), D(mi=30)) #f = PostingFilter(self.request.GET, queryset=sqs) if self.searchqueryset is not None: kwargs["searchqueryset"] = self.searchqueryset self.zipcode_form = Zipcode_Form(self.request.GET) return self.form_class(data, **kwargs)
def test_ensure_wgs84(self): self.assertRaises(SpatialError, ensure_wgs84, GEOSGeometry('POLYGON((-95 38, -96 40, -97 42, -95 38))')) orig_pnt = Point(-95.23592948913574, 38.97127105172941) std_pnt = ensure_wgs84(orig_pnt) self.assertEqual(orig_pnt.srid, None) self.assertEqual(std_pnt.srid, 4326) self.assertEqual(std_pnt.x, -95.23592948913574) self.assertEqual(std_pnt.y, 38.97127105172941) orig_pnt = Point(-95.23592948913574, 38.97127105172941) orig_pnt.set_srid(2805) std_pnt = ensure_wgs84(orig_pnt) self.assertEqual(orig_pnt.srid, 2805) self.assertEqual(std_pnt.srid, 4326) # These should be different, since it got transformed. self.assertNotEqual(std_pnt.x, -95.23592948913574) self.assertNotEqual(std_pnt.y, 38.97127105172941)
def incident_location(sender, instance, *args, **kwargs): """ Creates a Metadata instance whenever an Asset is added, and then extracts the metadata and populates the Metadata instance """ if instance.json and ('latitude' in instance.json) and ( 'longitude' in instance.json) and (not instance.coordinates): instance.coordinates = Point(instance.json['longitude'], instance.json['latitude']) print('saving coordinates...')
def get(self,request, *args, **kwargs): lat = float(request.GET.get('lat1','45')) lng = float(request.GET.get('lng1','-93.265')) qs = BestBikeTrails.objects.filter(the_geom__distance_lte=(Point(lng,lat,srid=4326),D(mi=2))) gj = [] for item in qs: poly = loads(GEOSGeometry(item.the_geom,srid=4326).geojson) poly['properties'] = {'name': item.ccp_name, 'tag': item.item_tags} gj.append(poly) return HttpResponse(dumps(gj),content_type="application/json")
def point_from_lat_long(value): if isinstance(value, Point): return value if isinstance(value, basestring): lat, lng = value.split(',') elif isinstance(value, (list, tuple)): lat, lng = value else: raise ValueError("I don't know what to do with this.") return Point(float(lng), float(lat))
def test_dwithin(self): p = Point(0, 0) distance = D(mi=500) sqs = self.search_index.objects.dwithin('location', p, distance) self.assertTrue(isinstance(sqs, SearchQuerySet)) params = sqs.query.build_params() self.assertIn('dwithin', params) self.assertDictEqual(params['dwithin'], {'field': 'location', 'point': p, 'distance': distance})
def test_build_query_with_dwithin_range(self): """ Test build_search_kwargs with dwithin range for Elasticsearch versions >= 1.0.0 """ search_kwargs = self.backend.build_search_kwargs('where', dwithin={ 'field': "location_field", 'point': Point(1.2345678, 2.3456789), 'distance': D(m=500) }) self.assertEqual(search_kwargs['query']['filtered']['filter']['bool']['must'][1]['geo_distance'], {'distance': "0.500000km", 'location_field': {'lat': 2.3456789, 'lon': 1.2345678}})
def test_ensure_wgs84(self): self.assertRaises( SpatialError, ensure_wgs84, GEOSGeometry('POLYGON((-95 38, -96 40, -97 42, -95 38))')) orig_pnt = Point(-95.23592948913574, 38.97127105172941) std_pnt = ensure_wgs84(orig_pnt) self.assertEqual(orig_pnt.srid, None) self.assertEqual(std_pnt.srid, 4326) self.assertEqual(std_pnt.x, -95.23592948913574) self.assertEqual(std_pnt.y, 38.97127105172941) orig_pnt = Point(-95.23592948913574, 38.97127105172941) orig_pnt.srid = 2805 std_pnt = ensure_wgs84(orig_pnt) self.assertEqual(orig_pnt.srid, 2805) self.assertEqual(std_pnt.srid, 4326) # These should be different, since it got transformed. self.assertNotEqual(std_pnt.x, -95.23592948913574) self.assertNotEqual(std_pnt.y, 38.97127105172941)
def test_distance(self): p = Point(0, 0) sqs = self.search_index.objects.distance("location", p) self.assertTrue(isinstance(sqs, SearchQuerySet)) params = sqs.query.build_params() self.assertIn("distance_point", params) self.assertDictEqual(params["distance_point"], { "field": "location", "point": p })
def license_coordinates(sender, instance, *args, **kwargs): """ Extracts and sets license.coordinates based on the centroid of a given polygon, or a single set of x,y coordinates. """ if instance.json: coords = instance.json['geometry']['coordinates'] if len(coords) == 2: instance.coordinates = Point(coords) print('point: {},{}'.format(instance.coordinates.x, instance.coordinates.y))
def save(self, *args, **kwargs): if self.point is None: from haystack.utils.geo import Point if self.lon is not None and self.lat is not None: self.point = Point(self.lon, self.lat) elif self.full_address != "": try: from geopy.geocoders import GoogleV3 geolocator = GoogleV3() try: lat, lon = geolocator.geocode(self.full_address)[1] self.lat = lat self.lon = lon self.point = Point(lon, lat) except IndexError: pass except Exception: pass # TODO: else with the street address and geo encoder via google or something super(CampusLocation, self).save(*args, **kwargs)
def test_order_by_distance(self): p = Point(1.23, 4.56) sqs = self.search_index.objects.distance('location', p).order_by('distance') self.assertTrue(isinstance(sqs, SearchQuerySet)) params = sqs.query.build_params() self.assertIn('distance_point', params) self.assertDictEqual(params['distance_point'], {'field': 'location', 'point': p}) self.assertTupleEqual(params['distance_point']['point'].get_coords(), (1.23, 4.56)) self.assertListEqual(params['sort_by'], ['distance'])
def _parse_near_param(s): """ Parse the ``near`` query string parameter The parameter should be of the form lat@lng,miles Returns a tuple of a Point and D object suitable for passing to ``SearchQuerySet.dwithin()`` """ (latlng, dist) = s.split(',') (lat, lng) = latlng.split('@') return (Point(float(lng), float(lat)), D(mi=dist))
def test_dwithin(self): p = Point(0, 0) distance = D(mi=500) sqs = self.search_index.objects.dwithin("location", p, distance) self.assertTrue(isinstance(sqs, SearchQuerySet)) params = sqs.query.build_params() self.assertIn("dwithin", params) self.assertDictEqual(params["dwithin"], { "field": "location", "point": p, "distance": distance })
def get_neighbours(self, user): location = user.location point = Point(location.lon, location.lat) max_dist = D(m=self.visibility_range) logger.debug("user: %s", user) logger.debug("location: %s", location) logger.debug("max_dist: %s", max_dist) sqs = SearchQuerySet().dwithin('location', point, max_dist).distance( 'location', point).filter(game=self.id) logger.debug("Query: %s", sqs.query) logger.debug("Query distance: %s", sqs.distance) return sqs
def test_order_by_distance(self): p = Point(1.23, 4.56) sqs = self.search_index.objects.distance("location", p).order_by("distance") self.assertTrue(isinstance(sqs, SearchQuerySet)) params = sqs.query.build_params() self.assertIn("distance_point", params) self.assertDictEqual(params["distance_point"], { "field": "location", "point": p }) self.assertTupleEqual(params["distance_point"]["point"].coords, (1.23, 4.56)) self.assertListEqual(params["sort_by"], ["distance"])