def set_grid(self, lat0, lon0, width, length, grid_size=0.05): """ Set Target area based on given centroid coordinate and w, l of the rectangle area Input: lat0, lon0: Centroid coordinate width, length: Width, Length of the area grid_size: width of each grid, in km Post-Condition: self.grids updated """ # Generate grid coordinate in (x, y) xs = np.arange(float(-width + grid_size)/2, float(width + grid_size)/2, grid_size) ys = np.arange(float(-length + grid_size)/2, float(length + grid_size)/2, grid_size) # Convert to coordinate index = 1 start = geopy.Point(lat0, lon0) for _x in xs: for _y in ys: # Get Coordinate of (_x, _y) with (lat0, lon0) at (0, 0) north = gd.VincentyDistance(kilometers=_y) east = gd.VincentyDistance(kilometers=_x) point = north.destination(east.destination(start, 90), 0) self.grids.update({index: (point[0], point[1])}) # Update index index += 1
def get_gains(): """Returns lat and lon gain Gain is space between circles. """ start = Point(*get_map_center()) base = config.SCAN_RADIUS * math.sqrt(3) height = base * math.sqrt(3) / 2 dis_a = distance.VincentyDistance(meters=base) dis_h = distance.VincentyDistance(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 getPoints(lat0, lon0, x, y): """ Convert x,y (in km) to lat,long with given (lat, lon) represents (0,0) in km Inputs: lat0, lon0: coordinates of (0, 0) in km x, y (iterables such as list or ndarray): coordinates of points in km. Returns: the coordinate(s) in lat, lon """ start = geopy.Point(lat0, lon0) points = [] for _x, _y in zip(x, y): north = gd.VincentyDistance(kilometers=_y) east = gd.VincentyDistance(kilometers=_x) points.append(north.destination(east.destination(start, 90), 0)) lats, lons = zip(*points)[:2] return lats, lons
def get_queryset(self): if not self.request.query_params: return Party.objects.all() else: user = self.request.user.id lat = self.request.query_params.get('lat') lng = self.request.query_params.get('lng') if lat is None or lng is None: return Party.objects.none() d = self.request.query_params.get('d') if d is None: d = 10.0 d = convert_mile_to_kilometer(float(d)) origin = Point(lat, lng) north = distance.VincentyDistance(kilometers=d).destination( origin, 0) east = distance.VincentyDistance(kilometers=d).destination( origin, 90) south = distance.VincentyDistance(kilometers=d).destination( origin, 180) west = distance.VincentyDistance(kilometers=d).destination( origin, 270) north_lat = north.latitude east_lng = east.longitude south_lat = south.latitude west_lng = west.longitude q = Party.objects\ .filter(lat__gte=south_lat, lat__lte=north_lat)\ .exclude(host__id=user).exclude(bouncers__id=user).exclude(invitees__id=user) # edge case around the anti-meridian if west_lng > east_lng: # it seems like you can't chain Q-object queries with regular queries return q.filter(Q(lng__gte=west_lng) | Q(lng__lte=east_lng)) else: return q.filter(lng__gte=west_lng, lng__lte=east_lng)
def getAngle(angleAttempts, fromA, toB): start = Point(toB['lat'], toB['lng']) travelDistance = gDistance.VincentyDistance(kilometers=0.01) def worker(acc, bearing): destination = travelDistance.destination(point=start, bearing=bearing) distancePoint = gDistance.distance( Point(fromA['lat'], fromA['lng']), Point(destination.latitude, destination.longitude)).km if (distancePoint > acc['distance']): return {'bearing': bearing, 'distance': distancePoint} return acc r = reduce(worker, angleAttempts, {'bearing': -1, 'distance': -1}) return r['bearing']
def get_bounding_box_of_circle(center_lon, center_lat, radious): start = geopy.Point(latitude=center_lat, longitude=center_lon) d = distance.VincentyDistance(kilometers=radious) # bearing=0 -> north # bearing=90 -> east # bearing=180 -> south # bearing=270 -> west east_north_point = { 'longitude': d.destination(point=start, bearing=90).longitude, 'latitude': d.destination(point=start, bearing=0).latitude } west_south_point = { 'longitude': d.destination(point=start, bearing=270).longitude, 'latitude': d.destination(point=start, bearing=180).latitude } return west_south_point, east_north_point
def get_coordinates(start_point, end_point, distance_meters): """ Calculates the new coordinates between two points depending of the specified distance and the calculated bearing. Parameters ---------- start_point: geopy.Point end_point: geopy.Point distance_meters: float Returns ------- point: geopy.Point A new point between the start and the end points. """ bearing = get_bearing(start_point, end_point) distance_km = distance_meters / 1000 d = geo_dist.VincentyDistance(kilometers=distance_km) destination = d.destination(point=start_point, bearing=bearing) return geopy.Point(destination.latitude, destination.longitude)
def get_nearest(self, num=5): "Returns the nearest X people, but only within the same continent" # TODO: Add caching people = list(self.country.djangoperson_set.select_related().exclude( pk=self.id, )) if len(people) <= num: # Not enough in country # use people from the same continent instead people = list(DjangoPerson.objects.filter( country__continent=self.country.continent, ).exclude(pk=self.id).select_related()) # Sort and annotate people by distance for person in people: person.distance_in_miles = distance.VincentyDistance( (self.latitude, self.longitude), (person.latitude, person.longitude) ).miles # Return the nearest X people.sort(key=lambda x: x.distance_in_miles) return people[:num]
filename = os.path.expanduser('~/.cache/guifinetstudio/detail/26494.cnml') nid = 33968 # MLGInvisible if len(sys.argv) == 3: nid, filename = sys.argv[1:2] # Parse CNML file cnmlp = CNMLParser(filename) from_node = cnmlp.getNode(nid) from_coord = (from_node.latitude, from_node.longitude) nodes = cnmlp.getNodes() distances = [] nodes.remove(from_node) # Calculate distance to every node in the same zone for to_node in nodes: to_coord = (to_node.latitude, to_node.longitude) dist = distance.VincentyDistance(from_coord, to_coord) bearing = calcBearing(from_node.latitude, from_node.longitude, to_node.latitude, to_node.longitude) distances.append((dist.km, from_node.title, to_node.title, bearing)) # Sort by distance for d in sorted(distances): if d[0] >= 1: dist_metric = '%.3f Km' % d[0] else: dist_metric = '%d m' % (d[0] * 1000) print('{} -> {}: {} (Az: {:.2f})'.format(d[1], d[2], dist_metric, d[3]))
train = df.query('Sold < @d') test = df.query('Sold == @d') actual += list(test['PRICE']) # Model 1 : Full Variance ybar = train['lPRICE'].mean() model1 += [ybar] * len(test.index) # Model 2 : Simple OLS m2 = LinearRegression() m2.fit(train[pcols], train[['lPRICE']]) ypred = m2.predict(test[pcols]) model2 += list(ypred.ravel()) for i in list(test.index): tgt_lat = test['LATITUDE'][i] tgt_lon = test['LONGITUDE'][i] c1 = (tgt_lat, tgt_lon) train['Dist'] = map(lambda x, y: gd.VincentyDistance(c1, (x, y)).miles, train['LATITUDE'], train['LONGITUDE']) train['dWgt'] = 1 / train['Dist'] train['dWgt'] = np.where(train['dWgt'] > 25, 25, train['dWgt']) train['Wgt'] = train['tWgt'] * train['dWgt'] # Model 3 : Weighted OLS m3 = LinearRegression() m3.fit(train[pcols], train[['lPRICE']], np.array(train['Wgt'])) ypred = m3.predict(test[pcols].query('index == @i')) model3 += list(ypred.ravel()) # Model 4 : Weighted SVR # TODO: need to standardize the data, then use SVR m4 = SVR(C=0.1) m4.fit(train[pcols], train[['lPRICE']], np.array(train['Wgt'])) ypred = m4.predict(test[pcols].query('index == @i'))