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 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 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 _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 coordinates(self): try: from haystack.utils.geo import Point except ImportError: return None else: return Point(self.longitude, self.latitude, srid=4326)
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 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 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 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 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 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 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 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 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 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_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 _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_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 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"])
def convert(self, value): if value is None: return None if hasattr(value, 'geom_type'): value = ensure_point(value) return value if isinstance(value, basestring): lat, lng = value.split(',') elif isinstance(value, (list, tuple)): # GeoJSON-alike lat, lng = value[1], value[0] elif ininstance(value, dict): lat = value.get('lat', 0) lng = value.get('lon', 0) value = Point(float(lng), float(lat)) return value
def test_build_query_with_dwithin_range(self): backend = connections["elasticsearch"].get_backend() search_kwargs = backend.build_search_kwargs( "where", dwithin={ "field": "location_field", "point": Point(1.2345678, 2.3456789), "distance": D(m=500), }, ) self.assertEqual( search_kwargs["query"]["bool"]["filter"]["geo_distance"], { "distance": "0.500000km", "location_field": { "lat": 2.3456789, "lon": 1.2345678 }, }, )
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)): # GeoJSON-alike lat, lng = value[1], value[0] elif isinstance(value, dict): lat = value.get('lat', 0) lng = value.get('lon', 0) value = Point(float(lng), float(lat)) return value
def test_build_query_with_dwithin_range(self): backend = connections['default'].get_backend() search_kwargs = backend.build_search_kwargs('where', dwithin={ 'field': "location_field", 'point': Point( 1.2345678, 2.3456789), 'distance': D(m=500) }) self.assertEqual( search_kwargs['query']['bool']['filter']['geo_distance'], { 'distance': "0.500000km", 'location_field': { 'lat': 2.3456789, 'lon': 1.2345678 } })