def distance(self,obj):
if 'location' in self.context.keys():
request = self.context['request']
tuple_x_y = (float(request.query_params['Latitude']),float(request.query_params['Longitude']))
point = Point(tuple_x_y,srid=4326)
result = distance(obj.Point,point).km
if result < 5:
return '5 km içerisinde'
elif result > 5 and result < 10:
return '10 km içerisinde'
elif result > 10 and result < 25:
return '25 km içerisinde'
elif result > 25 and result < 50:
return '50 km içerisinde'
else:
return obj.City + ' çevresinde'
return obj.City + ' çevresinde'
python类distance()的实例源码
def filter_by_location(self, distance, lat, lon):
min_lat, max_lat, min_lon, max_lon = self.bounding_coordinates(distance, lat, lon)
artists_within_bounds = self.filter(
lat__gte=min_lat,
lat__lte=max_lat,
lon__gte=min_lon,
lon__lte=max_lon
)
nearby_artist_ids = []
for artist in artists_within_bounds:
if calc_distance((lat, lon,), (artist.lat, artist.lon,)).miles <= distance:
nearby_artist_ids.append(artist.id)
return self.filter(id__in=nearby_artist_ids)
# TODO(lucas): Use annotations as much as possible to improve performance
def get_gains(dist=70):
"""Returns lat and lon gain
Gain is space between circles.
"""
start = Point(*bounds.center)
base = dist * sqrt(3)
height = base * sqrt(3) / 2
dis_a = distance(meters=base)
dis_h = distance(meters=height)
lon_gain = dis_a.destination(point=start, bearing=90).longitude
lat_gain = dis_h.destination(point=start, bearing=0).latitude
return abs(start.latitude - lat_gain), abs(start.longitude - lon_gain)
def closest(self, graph):
valid_nodes = self.closer_nodes(graph)
best_dist = None
best = None
for other in valid_nodes:
dist = distance.distance(self.pt, other.pt).meters
if not best_dist or dist < best_dist:
best_dist = dist
best = other
return best
def nearest(cls, lat, long, **kwargs):
"""
Search for the nearest stop from `lat` and `long`
params.
You should specify the stops to search, default is
`Stop.all()`.
@param lat: A float coercible value of latitude (eg.: -5.065533).
@param long: A float coercible value of longitude (eg.: -42.065533).
keywords params:
stops:
A list of `Stop` instance.
route:
A `Route` instance.
@return: A tuple with a `Stop` object and a `Distance`
object (see geopy.distance).
"""
if kwargs.get('stops', False):
stops = kwargs['stops']
elif kwargs.get('route', False):
stops = kwargs['route'].get_stops()
else:
stops = cls.all()
dists = map(lambda stop: distance(
(stop.lat, stop.long),
(lat, long)
), stops)
return min(zip(dists, stops))[::-1]
def nearest(cls, lat, long, **kwargs):
"""
Search for the nearest buses from `lat` and `long`
params.
You should specify the buses to search, default is
`Buses.all()`.
@param lat: A float coercible value of latitude (eg.: -5.065533).
@param long: A float coercible value of longitude (eg.: -42.065533).
keywords params:
buses:
A list of `Stop` instance.
route:
A `Route` instance.
@return: A tuple with a `Bus` object and a `Distance`
object (see geopy.distance).
"""
if kwargs.get('buses', False):
buses = kwargs['buses']
elif kwargs.get('route', False):
buses = kwargs['route'].get_buses()
else:
buses = cls.all()
dists = map(lambda bus: distance(
# Avoid using cached location, to avoid more requests.
(bus.__lat__, bus.__long__),
(lat, long)
), buses)
return min(zip(dists, buses))[::-1]
def bounding_coordinates(distance, lat, lon):
origin = geopy.Point((lat, lon,))
geopy_distance = calc_distance(miles=distance)
min_lat = geopy_distance.destination(origin, 180).latitude
max_lat = geopy_distance.destination(origin, 0).latitude
min_lon = geopy_distance.destination(origin, 270).longitude
max_lon = geopy_distance.destination(origin, 90).longitude
return min_lat, max_lat, min_lon, max_lon
def traceroute(self, source, dest):
"""
Trace a route between `source` and `dest`.
First, find the nearest stop to `source` and the nearest to `dest`
and try to find a common route to both. Otherwise, fallbacks searching
for all routes of the `source` stop and `dest` stop, choosing the one
what is nearest to the `source` or `dest`.
@param source: a pair latitude and longitude
@param dest: a pair latitude and longitude
@return: a tuple like ((<source stop>, <distance to the nearest stop>),
(<dest stop>, <distance to the nearest stop>), <route>)
"""
sourcestop, dsrc = Stop.nearest(source[0], source[1])
deststop, ddst = Stop.nearest(dest[0], dest[1])
sourceroutes = sourcestop.get_routes()
destroutes = deststop.get_routes()
for i, j in itertools.product(sourceroutes, destroutes):
if i == j:
return (sourcestop, dsrc), (deststop, ddst), i
dist, stop, route = min(
min(
Stop.nearest(
source[0],
source[1],
route=route
)[::-1] + (route,) for route in destroutes
),
min(
Stop.nearest(
dest[0],
dest[1],
route=route
)[::-1] + (route,) for route in sourceroutes
)
)
sourcestop = Stop.nearest(source[0], source[1], route=route)
deststop = Stop.nearest(dest[0], dest[1], route=route)
return sourcestop, deststop, route