def distcarb(points, elevations, data, steps): # carbonFunction = f(grade)=km/L lenTotal = 0 carbon = 0 avgmpg = 0 cVel = 0 distToNext = -0.0001 step = 0 # vincenty's formula with height approximation using exponential moving averages to smooth data lastEMA = sum([(elevations[i] - elevations[i + 1]) / (math.sqrt( vincenty.vincenty(points[i], points[i + 1])**2 + ((elevations[i] - elevations[i + 1]) / 1000)**2) * 1000) for i in range(0, 3)]) / 3 for i in range(0, len(points) - 1): if lenTotal > distToNext: while lenTotal > distToNext and step < len(steps) - 1: step += 1 distToNext += steps[step]['distance']['value'] / 1000 cVel = steps[step]['distance']['value'] / ( steps[step]['duration']['value']) * 3.6 dist = math.sqrt( vincenty.vincenty(points[i], points[i + 1])**2 + ((elevations[i] - elevations[i + 1]) / 1000)**2) lastEMA = (elevations[i + 1] - elevations[i]) / (dist * 1000) * (0.125) + lastEMA * (0.875) dcarbon, mpg = carbonFunction(dist, lastEMA, data, cVel) carbon += dcarbon avgmpg += mpg * dist lenTotal += dist return [lenTotal, carbon, avgmpg / lenTotal]
def add_city_to_graph(city_coords, city_name, nearest_roads, graph): # add the city to the graph graph.add_node(city_coords, name=city_name) # graph.remove_node(source_city_coords) #initialize `closest_point` to be the first point in `nearest_road` closest_point = nearest_roads[0][0] # initialize `closest_distance` to be the distance between the first # point in `nearest_road` and the city # Must be careful!!! `vincenty` accepts coordinates in (lat,lon) instead # of (lon,lat). I do the switching here, hence the indexing, but I still # return the closest point as (lon,lat). closest_distance = vincenty( (nearest_roads[0][0][1], nearest_roads[0][0][0]), (city_coords[1], city_coords[0])) # for every point in the road, calculate its distance from the city # node and return the point that's closest to the city for road in nearest_roads: closest_distance = vincenty((road[0][1], road[0][0]), (city_coords[1], city_coords[0])) for vertex_tuple in road: current_distance = vincenty((city_coords[1], city_coords[0]), (vertex_tuple[1], vertex_tuple[0])) if current_distance < closest_distance: closest_distance = current_distance closest_point = vertex_tuple graph.add_edge(city_coords, closest_point, type="Unpaved")
def test_vincenty(): from vincenty import vincenty boston = (42.3541165, -71.0693514) #緯度,經度 newyork = (40.7791472, -73.9680804) print(vincenty(boston, newyork)) p1 = (34, 117) p2 = (32, 108) print(vincenty(p1, p2))
def test_vincenty(): from vincenty import vincenty boston = (42.3541165, -71.0693514) #緯度,經度 newyork = (40.7791472, -73.9680804) print(vincenty(boston, newyork)) p1=(34,117) p2=(32,108) print(vincenty(p1, p2))
def reduce(self, events): latitude = self.latfield longitude = self.longfield relative_distance = self.output_field use_haversine = bool(self.use_haversine) self.logger.info("[%s] - Starting geodistance instance" % str(self.metadata.searchinfo.sid)) self.logger.debug("[%s] - Using parameters - %s" % (str(self.metadata.searchinfo.sid), str(self.metadata))) if self.group_by: position_tracker = {} for event in events: current = event if not (current[latitude] or current[longitude]): current[relative_distance] = 0.0 self.logger.debug("[%s] - Using distance=0 for private IPs or unknown coordinates. " "Exclude if undesired." % str(self.metadata.searchinfo.sid)) else: current_pos = (float(current[latitude]), float(current[longitude])) if current[self.group_by] not in position_tracker.keys(): last_pos = None else: last_pos = position_tracker[current[self.group_by]] if last_pos is None: current[relative_distance] = 0.0 self.logger.debug( "[%s] - Initializing the first location with distance=0" % str(self.metadata.searchinfo.sid) ) else: if use_haversine: current[relative_distance] = haversine(last_pos, current_pos, miles=bool(self.miles)) else: current[relative_distance] = vincenty(last_pos, current_pos, miles=bool(self.miles)) position_tracker[current[self.group_by]] = current_pos yield current else: last_pos = None for event in events: current = event if not (current[latitude] or current[longitude]): current[relative_distance] = 0.0 self.logger.debug( "[%s] - Using distance=0 for private IPs or unknown coordinates. Exclude if undesired." % str( self.metadata.searchinfo.sid)) else: current_pos = (float(current[latitude]), float(current[longitude])) if last_pos is None: current[relative_distance] = 0.0 self.logger.debug("[%s] - Initializing the first location with distance=0" % str( self.metadata.searchinfo.sid)) else: if use_haversine: current[relative_distance] = haversine(last_pos, current_pos, miles=bool(self.miles)) else: current[relative_distance] = vincenty(last_pos, current_pos, miles=bool(self.miles)) last_pos = current_pos self.logger.debug(current) yield current self.logger.info("[%s] - Completed successfully." % str(self.metadata.searchinfo.sid))
def IDW_Slice_nc4(loc, date_index, variable, data): """ Returns weighted average of temperature at a given location interp(target ,neighborhood, depth_index, variable, date_index) date_index from date in hours since 2000/1/1 depth_index from the depth profile of HYCOM assymilated data variable is a string that calls the ID of a given variable """ import netCDF4 import numpy as np from sklearn.preprocessing import normalize lons = data.variables['lon'][:] lats = data.variables['lat'][:] depth = data.variables['depth'][:] y, x, found_lat, found_lon = location_to_index(loc[0], loc[1], lats, lons) target = [y, x] neighborlist = nearest_neighbors(y, x) neighborhood = [[lats[i[0]], lons[i[1]]] for i in neighborlist] dist_list = [vincenty(loc, neighbor) for neighbor in neighborhood] #weight_list = np.array([1./(dist*dist) if dist > 0 else 1.0 for dist in dist_list]) #weight_list = weight_list / np.linalg.norm(weight_list) var_matrix = [] for neighbor in neighborlist: tmp = data.variables[variable][date_index, :, neighbor[0], neighbor[1]] var_matrix.append(np.squeeze(tmp)) var_matrix = np.asarray(var_matrix) var_list = [ IDW_interp(np.array(var_matrix[:, i]), np.array(dist_list)) for i, j in enumerate(depth) ] #print var_list[0] return var_list
def procesarDistancias(): header = ["id_votante", "id_centro", "distancia"] with open(RUTA_MERGE, 'w') as merge: with open(RUTA_VOTANTES) as votantes: with open(RUTA_CENTROS) as centros: votantes_reader = csv.reader(votantes, delimiter=',') centros_reader = csv.reader(centros, delimiter=',') merge_csv = csv.writer(merge) merge_csv.writerow(header) header_votante = True header_centro = True for votante in votantes_reader: if header_votante: header_votante = False continue centros.seek(0) for centro in centros_reader: if header_centro: header_centro = False continue distancias = [] distancias.append(votante[0]) #id_votante distancias.append(centro[0]) #id_centro posicion_votante = (float(votante[LATITUD]), float(votante[LONGITUD])) posicion_centro = (float(centro[LATITUD]), float(centro[LONGITUD])) distancia = vincenty(posicion_centro, posicion_votante) distancias.append(distancia) #distancia merge_csv.writerow(distancias) header_centro = True
def nearest_subway(lat, lon): L = [] index = 0 ref = False with open("stations.csv") as f: # Hasty implementation of csv.DictReader. Will save some lines if # switched, and will be cleaner. text = csv.reader(f, delimiter=',', quotechar='"') for line in text: if (not ref): ref = line continue L.append({}) for i, val in enumerate(line): L[-1][ref[i]] = val L[-1]["dist"] = vincenty.vincenty( ( float(L[-1]["GTFS Latitude"]), float(L[-1]["GTFS Longitude"]) ), (float(lat), float(lon)) ) ref.append("dist") L = sorted(L, key=lambda item: item["dist"]) return L
def main(fileins, fileout): out = open( fileout, "w" ) gf = GridFloat( "./data/34293486/34293486" ) for filein in fileins: print "working on %s"%filein for track in get_tracksegs( filein ): for (lat1, lon1, time1), (lat2, lon2, time2) in cons(track, 3): e1 = gf.elevation( lon1, lat1 ) e2 = gf.elevation( lon2, lat2 ) dx = vincenty(lat1, lon1, lat2, lon2) dy = e2-e1 dt = (time2-time1).seconds if dt==0: print "teleported %f"%dt continue if dx==0: continue v = dx/dt grade = (dy/dx)*100 out.write( "%s,%s\n"%(grade,v) )
def get_distances(args): hotel_coords, trip_coords = args dists = [ vincenty(hotel_coords, coord, miles=True) for coord in trip_coords ] return [dist * 5280 if dist is not None else np.inf for dist in dists]
def vincenty_vec(vec_coord): vin_vec_dist = np.zeros(vec_coord.shape[0]) if vec_coord.shape[1] != 4: print('ERROR: Bad number of columns (shall be = 4)') else: vin_vec_dist = [vincenty(vec_coord[m,0:2],vec_coord[m,2:]) for m in range(vec_coord.shape[0])] return vin_vec_dist
def distance(u, v, d): """ Purpose: to ultimately determine the distance between two coordinates in a graph Inputs: `u`: a tuple (lon,lat) of a source node `v`: a tuple (lon,lat) of a target node `d`: the data assigned to the edge between `u` and `v` Outputs: `distance_uv`: the distance in miles between `u` and `v` """ # this function must accept three parameters if called in `single_source_dijkstra`. # `single_source_dijkstra` automatically sends those arguments # `u`: a tuple (lon,lat) of a source node # `v`: a tuple (lon,lat) of a target node # `d`: the data assigned to the edge between `u` and `v` distance_uv = vincenty((u[1], u[0]), (v[1], v[0])) # since when I loaded the roads shapefile with `geom_attrs=True`, # any data stored for each LineString in the shapefile gets assigned # to the respective graph edges. I use that data here to halve the # distance between `u` and `v` if the road is a railroad if d['type'] == "Railroad": distance_uv /= 2.0 # you can do much more here, e.g. if the road is a secondary road, # run a nearest neighbor query for all ufo sightings; depending on # the results, change the `distance_uv` value by a certain amount, like # I did for railroads. Or, for every X number of roads, search for a body # of water. If the body of water is Y miles away, call a function that # creates an edge between two vertices (`v` to `z`, where `z` is the vertex # on the other side of the water) that crosses the body of water with type=bridge. return distance_uv
def main(fileins, fileout): out = open(fileout, "w") gf = GridFloat("./data/34293486/34293486") for filein in fileins: print "working on %s" % filein for track in get_tracksegs(filein): for (lat1, lon1, time1), (lat2, lon2, time2) in cons(track, 3): e1 = gf.elevation(lon1, lat1) e2 = gf.elevation(lon2, lat2) dx = vincenty(lat1, lon1, lat2, lon2) dy = e2 - e1 dt = (time2 - time1).seconds if dt == 0: print "teleported %f" % dt continue if dx == 0: continue v = dx / dt grade = (dy / dx) * 100 out.write("%s,%s\n" % (grade, v))
def getDistanceFromCoordinates(self): value = vincenty(self.nodeA.getCoordinates(), self.nodeB.getCoordinates()) if value is not None: return value * 1000 else: raise ValueError("Failed get distance from coordinates.")
def get_lat_lon(df): ''' Get the latitude and longitude of each firm and its distance to the White House. ''' time.sleep(1) address = df['address'] # Create googlemaps instance with a valid API key. # Some addresses are not valid; for example, one address is (Universal # Corporate Center, 367 South Gulph Road, PO Box 615, King Of Prussia, # PA, 19406). gmaps.geocode would return an empty string for this address. # I looked it up online, the right address should have "PO Box 61558" # instead of "PO Box 615". geocode_result = GMAPS.geocode(address) # If the address is invalid, geocode_result would be empty; # NAs will be returned. if len(geocode_result) == 0: print("Address not valid: {}".format(address)) df['lat'], df['lng'] = np.nan, np.nan df['distance(km)'] = np.nan else: # Extract the location of the firm and its latitude, longitude. locations = geocode_result[0]['geometry']['location'] lat, lng = locations['lat'], locations['lng'] df['lat'], df['lng'] = lat, lng # (latitude, longitude) of the firm. firm = (lat, lng) # Calculate the distance(km) from each firm to the White House. df['distance(km)'] = vincenty(firm, WHITE_HOUSE) return df
def main(): usage = """usage: python gdb_link_gtfs_gtfs.py <graphdb_filename> <gtfsdb_filename> <range>""" parser = OptionParser(usage=usage) (options, args) = parser.parse_args() if len(args) != 3: parser.print_help() exit(-1) graphdb_filename = args[0] gtfsdb_filename = args[1] range = float(args[2]) gtfsdb = GTFSDatabase( gtfsdb_filename ) gdb = GraphDatabase( graphdb_filename ) n_stops = gtfsdb.count_stops() for i, (stop_id, stop_name, stop_lat, stop_lon) in enumerate( gtfsdb.stops() ): print "%d/%d %s"%(i,n_stops,stop_id), station_vertex_id = "sta-%s"%stop_id for link_stop_id, link_stop_name, link_stop_lat, link_stop_lon in gtfsdb.nearby_stops( stop_lat, stop_lon, range ): if link_stop_id == stop_id: continue print ".", link_length = vincenty( stop_lat, stop_lon, link_stop_lat, link_stop_lon) link_station_vertex_id = "sta-%s"%link_stop_id gdb.add_edge( station_vertex_id, link_station_vertex_id, Street("link", link_length) ) print ""
def calcdistance(latitudes, longitudes): """ Calculate the distance along the route. :param latitudes: :type latitudes: list :param longitudes: :type longitudes: list :return: :rtype: float """ positions = [] distances = [] i = 0 while i < len(latitudes): positions.append((latitudes[i], longitudes[i])) i += 1 i = 0 while i < len(positions) - 1: distances.append(vincenty(positions[i], positions[i + 1])) i += 1 return sum(distances)
def find_closest_station(solar_loc, uscrn_df): """ This is a helper function to find the closest weather station given solar location and a dataframe with weather station locations. """ # start from subset of stations w.in a small radius neighbors = uscrn_df[0:0] radius = 1 while len(neighbors) == 0: # range of lat/long to search xmin, xmax = solar_loc[0] - radius, solar_loc[0] + radius ymin, ymax = solar_loc[1] - radius, solar_loc[1] + radius # pull stations in that range lat_r = (uscrn_df.latitude>=xmin)&(uscrn_df.latitude<=xmax) long_r = (uscrn_df.longitude>=ymin)&(uscrn_df.longitude<=ymax) neighbors = uscrn_df[lat_r & long_r] radius += 1 # find closest neighbor closest_station = None dist_to_closest_station = np.inf for wban_id, (lat, lng) in neighbors.iterrows(): dist = vincenty.vincenty((lat, lng), solar_loc) if dist < dist_to_closest_station: dist_to_closest_station = dist closest_station = wban_id return closest_station, dist_to_closest_station
def distance(source, target): """ 1 = 1km :param source: :param target: :return: """ return vincenty(source, target)
def distance_from(origin, point): if cache.get((origin,point)) != None: return cache.get((origin,point)) else: distance = vincenty(origin,point) cache[(origin,point)] = distance sleep(10) return distance
def func(x, points): if x[0] > 90 or x[1] > 90 or x[0] < -90 or x[1] < -90: return 9999999999 distsum = 0 for c in points: dist = vincenty([x[0], x[1]], [c[1], c[0]]) - x[2] * c[2] distsum += dist * dist return distsum
def Speed_algo(rawPointsAnnotated, points, MaxSpeed, deploymentDatestr): eliminatedSpeed = [] pointsfiltered = [] deploymentDateobj = datetime.datetime.strptime(deploymentDatestr, '%Y-%m-%dT%H:%M') deploymentDateobj = deploymentDateobj.isoformat() L = len(points) start = 0 alertDate = 0 # Vérification que la date de déploiement soit bien avant la date de la dernière donnée if points[L - 1]['date'] < deploymentDateobj: alertDate = 1 return rawPointsAnnotated, eliminatedSpeed, pointsfiltered, alertDate # Recherche de l'indice de la donnée correspondant au déploiement pour commencer le filtre de vitesse à partir de cet indice if points[0]['date'] < deploymentDateobj: for d in range(L): if points[d]['date'] >= deploymentDateobj: start = d break else: eliminatedSpeed.append(points[d]) for l in range(len(rawPointsAnnotated)): if rawPointsAnnotated[l]['id'] == points[d]['id']: rawPointsAnnotated[l]['status'] = 'before deployment' points[d]['distance1'] = 0 points[d]['speed'] = 0 points[start]['distance1'] = 0 points[start]['speed'] = 0 i = start # Recherche des points valides sur la vitesse while i < L - 1: for j in range(1, L - i): # Calcul de la distance points[i + j]['distance1'] = vincenty( (float(points[i]['LAT']), float(points[i]['LON'])), (float(points[i + j]['LAT']), float(points[i + j]['LON']))) # Calcul de la durée diftimeS = datetime.datetime.strptime( points[i + j]['date'], '%Y-%m-%dT%H:%M:%S') - datetime.datetime.strptime( points[i]['date'], '%Y-%m-%dT%H:%M:%S') diftimeH = diftimeS.total_seconds() / 3600 # Calcul de la vitesse speed = points[i + j]['distance1'] / float(diftimeH) points[i + j]['speed'] = speed # Comparaison à la vitesse maximale entrée en paramètre, # Si la vitesse est considérée aberrante on ajoute le point aux données éliminées et on l'annote dans les données brutes if speed > MaxSpeed: eliminatedSpeed.append(points[i + j]) for l in range(len(rawPointsAnnotated)): if rawPointsAnnotated[l]['id'] == points[i + j]['id']: rawPointsAnnotated[l]['status'] = 'speed outlier' else: i = i + j break # Elimination des points dont la vitesse a été jugée aberrante pointsfiltered = [x for x in points if x not in eliminatedSpeed] return rawPointsAnnotated, eliminatedSpeed, pointsfiltered, alertDate, points
def create_and_populate_edges_table(self, tolerant=False): self.set_endnode_ref_counts() self.index_endnodes() print "splitting ways and inserting into edge table" c = self.get_cursor() c.execute( "CREATE TABLE edges (id TEXT, parent_id TEXT, start_nd TEXT, end_nd TEXT, dist FLOAT, geom TEXT)" ) for i, way in enumerate(self.ways()): try: if i % 5000 == 0: print i subways = [] curr_subway = [way.nds[0] ] # add first node to the current subway for nd in way.nds[1:-1]: # for every internal node of the way curr_subway.append(nd) if self.node( nd )[4] > 1: # node reference count is greater than one, node is shared by two ways subways.append(curr_subway) curr_subway = [nd] curr_subway.append( way.nds[-1] ) # add the last node to the current subway, and store the subway subways.append(curr_subway) #insert into edge table for i, subway in enumerate(subways): coords = [(lambda x: (x[3], x[2]))(self.node(nd)) for nd in subway] packt = pack_coords(coords) dist = sum([ vincenty(lat1, lng1, lat2, lng2) for (lng1, lat1), (lng2, lat2) in cons(coords) ]) c.execute("INSERT INTO edges VALUES (?, ?, ?, ?, ?, ?)", ("%s-%s" % (way.id, i), way.id, subway[0], subway[-1], dist, packt)) except IndexError: if tolerant: continue else: raise print "indexing edges...", c.execute("CREATE INDEX edges_id ON edges (id)") c.execute("CREATE INDEX edges_parent_id ON edges (parent_id)") print "done" self.conn.commit() c.close()
def add_location(): infos = session['result'] me = User.find_by('username', session['user']) my_loc = (float(me.getLat()), float(me.getLong())) for item in infos: other = User.find_by('username', item['username']) else_loc = (float(other.getLat()), float(other.getLong())) delta = vincenty(my_loc, else_loc) item['delta'] = delta session['result'] = infos
def speed(self): if not self._prev_data or not self._current_data: return 0 # no data else: prev_pos = (self._prev_data.latitude, self._prev_data.longitude) curr_pos = (self._current_data.latitude, self._current_data.longitude) speed = vincenty(prev_pos, curr_pos) * 1000 / (self._curr_time - self._prev_time) return speed
def length(self): """nodedir is a dictionary of nodeid->node objects""" ret = 0 for i in range(len(self.nd_ids)-1): thisnode = self.osm.nodes[ self.nd_ids[i] ] nextnode = self.osm.nodes[ self.nd_ids[i+1] ] ret += vincenty(thisnode.lat, thisnode.lon, nextnode.lat, nextnode.lon) return ret
def caculate_distance(user_loc,lst): store_distance_name = [] store_detail_list = [] for r in lst: stores_loation_before_choice = (float(r[1].latitude),float(r[1].longtitute)) distance = vincenty(user_loc, stores_loation_before_choice) #caculate distance store_distance_name.append(r[1].store) store_detail_list.append((distance,str(r[1].soup))) city_dis_dic = dict(zip(store_distance_name, store_detail_list)) sorted_city_dis_dic = {k: v for k, v in sorted(city_dis_dic.items(), key=lambda item: item[1][0])} return sorted_city_dis_dic
def tracking_gps_2_durations_distances(x): times, lon_lat = tracking_gps_2_times_lonlat(x) durations = [] # durations in second distances = [] # distance in km t_prev, lon_prev, lat_prev = None, None, None for t, (lon, lat) in zip(times, lon_lat): if t_prev: durations.append((t - t_prev).total_seconds()) distances.append(vincenty((lat_prev, lon_prev), (lat, lon))) t_prev, lon_prev, lat_prev = t, lon, lat return durations, distances
def extractNode(self, coordinates: list, origin, latLon=False): self.setCoordinates(coordinates, latLon=latLon) if (latLon): originPoint: tuple = (origin[0], origin[1]) else: originPoint: tuple = (origin[1], origin[0]) thisPoint: tuple = self.getCoordinates() distanceX = vincenty(originPoint, (originPoint[0], thisPoint[1])) * 1000 distanceY = vincenty(originPoint, (thisPoint[0], originPoint[1])) * 1000 posX = distanceX posY = distanceY if (thisPoint[1] < originPoint[1]): posX = -distanceX if (thisPoint[0] < originPoint[0]): posY = -distanceY self.xPos = posX self.yPos = posY
def length(self): """nodedir is a dictionary of nodeid->node objects""" ret = 0 for i in range(len(self.nd_ids) - 1): thisnode = self.osm.nodes[self.nd_ids[i]] nextnode = self.osm.nodes[self.nd_ids[i + 1]] ret += vincenty(thisnode.lat, thisnode.lon, nextnode.lat, nextnode.lon) return ret
def distance(point1: tuple, point2: tuple): """Calculate the distance in kilometers between two points using vincenty function :param point1: coordinates (latitude, longitude) of point 1 :param point2: coordinates (latitude, longitude) of point 1 :return: distance in kilometers between two points """ if None in (point1, point2): return None result = vincenty(point1, point2) if result is None: return None return result
def absolute_disp(lat, long): k = [] for i in range(1, len(lat)): lat1 = lat[i - 1] lat2 = lat[i] lon1 = long[i - 1] lon2 = long[i] kk = vincenty((lat1, lon1), (lat2, lon2)) # kk=kk*1609344 k.append(kk) return np.reshape(k, (len(k), 1))
def get_station_latlng(latlng): if latlng == None: print 'No input to get_station' return None df_lookup = pd.read_csv(fldir + 'Weather Data Mapping to Use.csv') lat = latlng[0] lng = latlng[1] df_lookup['distance'] = df_lookup.apply(lambda r: vincenty((lat, lng), (r['Latitude'], r['Longitude']), miles=True), axis=1) min_distance = df_lookup['distance'].min() df_temp = df_lookup[df_lookup['distance'] == min_distance] icao = df_temp['StationID'].tolist()[0] distance = df_temp['distance'].tolist()[0] return (icao, distance)
def closest_pt(pt, trajectory): """Finds closest pt to pt (lat, long) in trajectory (lats, longs). Parameters ---------- pt is a tuple (lat, long) trajectory can be: - a tuple (lats, longs) where lats is an iterable of floats (and longs also) - a (2 * N) numpy array where N is the length of the trajectory - any other structure equivalent in terms of unpacking a, b = trajectory """ lats, longs = trajectory ds = [vincenty((x, y), pt) for (x, y) in zip(lats, longs)] return np.argmin(ds)
def find_stop(stop_list, lat_lng): for key in stop_list.keys(): # Calculate the geographical distance between 2 points using the vincenty formula and updating the list stop_list[key].update({ "distance": vincenty([stop_list[key]["lat"], stop_list[key]["lng"]], lat_lng) }) # Sort the dictionary by the shortest distance sorted_list = sorted(stop_list.items(), key=lambda x_y: x_y[1]["distance"]) # Return the closest stop id to the given lat_lng stop = sorted_list[0][0] return stop
def calculate_the_nearest_3_cities(input_latitude, input_longitude, suggested_data): """ get most suggested cities based on vincenty distance of the potiential matched cities :param: suggested_data list of dictionary of the matched cities info :return the dictionary of dictionary of cities with the smallest distance :rtype: dict of dict """ nearest_3_cities = [float("-inf"), float("-inf"), float("-inf")] ret_dict = {} input_city = (input_latitude, input_longitude) for d in suggested_data: suggested_city = (d['lat'], d['long']) distance = vincenty(suggested_city, input_city) if distance > nearest_3_cities[0]: nearest_3_cities = [ distance, nearest_3_cities[0], nearest_3_cities[1] ] ret_dict['top'] = { 'name': d['name'], 'latitude': d['lat'], 'longitude': d['long'], 'score': 1 } elif distance > nearest_3_cities[1]: nearest_3_cities = [ nearest_3_cities[0], distance, nearest_3_cities[1] ] ret_dict['second'] = { 'name': d['name'], 'latitude': d['lat'], 'longitude': d['long'], 'score': 0.9 } elif distance > nearest_3_cities[2]: nearest_3_cities = [ nearest_3_cities[0], nearest_3_cities[1], distance ] ret_dict['third'] = { 'name': d['name'], 'latitude': d['lat'], 'longitude': d['long'], 'score': 0.8 } return ret_dict
def create_and_populate_edges_table( self, tolerant=False ): self.set_endnode_ref_counts() self.index_endnodes() print "splitting ways and inserting into edge table" c = self.conn.cursor() c.execute( "CREATE TABLE edges (id TEXT, parent_id TEXT, start_nd TEXT, end_nd TEXT, dist FLOAT, geom TEXT)" ) for i, way in enumerate(self.ways()): try: if i%5000==0: print i subways = [] curr_subway = [ way.nds[0] ] # add first node to the current subway for nd in way.nds[1:-1]: # for every internal node of the way curr_subway.append( nd ) if self.node(nd)[4] > 1: # node reference count is greater than one, node is shared by two ways subways.append( curr_subway ) curr_subway = [ nd ] curr_subway.append( way.nds[-1] ) # add the last node to the current subway, and store the subway subways.append( curr_subway ); #insert into edge table for i, subway in enumerate(subways): coords = [(lambda x:(x[3],x[2]))(self.node(nd)) for nd in subway] packt = pack_coords( coords ) dist = sum([vincenty(lat1, lng1, lat2, lng2) for (lng1, lat1), (lng2, lat2) in cons(coords)]) c.execute( "INSERT INTO edges VALUES (?, ?, ?, ?, ?, ?)", ("%s-%s"%(way.id, i), way.id, subway[0], subway[-1], dist, packt) ) except IndexError: if tolerant: continue else: raise print "indexing edges...", c.execute( "CREATE INDEX edges_id ON edges (id)" ) c.execute( "CREATE INDEX edges_parent_id ON edges (parent_id)" ) print "done" self.conn.commit() c.close()
def get_station_dist(b, latlng, length): if latlng == None: print 'No input to get_station for {0}'.format(b) return None df_lookup = pd.read_csv(os.getcwd() + \ '/input/Weather Data Mapping to Use.csv') lat = latlng[0] lng = latlng[1] print b df_lookup['distance'] = df_lookup.apply(lambda r: vincenty((lat, lng), (r['Latitude'], r['Longitude']), miles=True), axis=1) df_lookup_sort = df_lookup.sort(columns='distance') df_lookup_sort = df_lookup_sort.head(n=length) icao = df_lookup_sort['StationID'].tolist() distance = df_lookup_sort['distance'].tolist() return zip(icao, distance)
def load_streets_to_graph(g, osmdb, reporter=None): n_ways = osmdb.count_ways() for i, way in enumerate( osmdb.ways() ): if reporter and i%(n_ways//100+1)==0: reporter.write( "%d/%d ways loaded\n"%(i, n_ways)) distance = sum( [vincenty(y1,x1,y2,x2) for (x1,y1), (x2,y2) in cons(way.geom)] ) vertex1_label = "osm-%s"%way.nds[0] vertex2_label = "osm-%s"%way.nds[-1] x1, y1 = way.geom[0] x2, y2 = way.geom[-1] g.add_vertex( vertex1_label ) g.add_vertex( vertex2_label ) g.add_edge( vertex1_label, vertex2_label, Street( way.id, distance ) ) g.add_edge( vertex2_label, vertex1_label, Street( way.id, distance ) )
def link_nearby_stops(g, gtfsdb, range=0.05, obstruction=1.4): """Adds Street links of length obstruction*dist(A,B) directly between all station pairs closer than <range>""" print "Linking nearby stops..." for stop_id1, stop_name1, lat1, lon1 in gtfsdb.stops(): g.add_vertex( "sta-%s"%stop_id1 ) for stop_id2, stop_name2, lat2, lon2 in gtfsdb.nearby_stops(lat1, lon1, range): if stop_id1 == stop_id2: continue print "linking %s to %s"%(stop_id1, stop_id2) g.add_vertex( "sta-%s"%stop_id2 ) dd = obstruction*vincenty( lat1, lon1, lat2, lon2 ) print dd g.add_edge( "sta-%s"%stop_id1, "sta-%s"%stop_id2, Street("walk", dd) ) g.add_edge( "sta-%s"%stop_id2, "sta-%s"%stop_id1, Street("walk", dd) )
def split_line_segment(lng1, lat1, lng2, lat2, max_section_length): # Split line segment defined by (x1, y1, x2, y2) into a set of points # (x,y,displacement) spaced less than max_section_length apart if lng1==lng2 and lat1==lat2: yield [lng1, lat1, 0] yield [lng2, lat2, 0] return street_len = vincenty(lat1, lng1, lat2, lng2) n_sections = int(street_len/max_section_length)+1 geolen = ((lat2-lat1)**2 + (lng2-lng1)**2)**0.5 section_len = geolen/n_sections street_vector = (lng2-lng1, lat2-lat1) unit_vector = [x/geolen for x in street_vector] for i in range(n_sections+1): vec = [x*section_len*i for x in unit_vector] vec = [lng1+vec[0], lat1+vec[1], (street_len/n_sections)*i] yield vec
def distance_air_lines(self,distance_type="geom",**kwargs): """ Args: distance_type (varchar): 'geom' for geometrical distance or 'vincenty' for WGS 84 with distance from vincenty algorithm """ assert self.one('route/test_point_id_range')[0] self.do('route/create_distance') featured_points=self.do('route/select_od_point').fetchall() all_points=self.do('route/select_point').fetchall() for start_point_geometry,start,_, in TaurusLongTask(\ featured_points,\ additional_text='Distances',\ **self.kwargs): new_distances=[] sp_json_geometry=json.loads(start_point_geometry) for end_point_geometry,i,_, in all_points: ep_json_geometry=json.loads(end_point_geometry) if distance_type=="geom": dist=sum([(i[0]-i[1])**2 for i in zip(ep_json_geometry,sp_json_geometry)])**0.5 elif distance_type=="vincenty": dist=vincenty(sp_json_geometry,ep_json_geometry) else: #dist=None raise ValueError("Unknown value") new_distances.append({ 'start_id': start, 'end_id': i, 'weight': dist, 'successor_id': i, 'predecessor_id': start }) self.transaction('route/import_distance',new_distances) self.commit()
def _get_distance(id_, user_latlng): ''' given: comment id and user latlng tuple calculates distance to all locations for that comment id returns: the closest distance ''' if not all(user_latlng): return 25000 user_latlng = tuple(float(i) for i in user_latlng) sql_command = 'SELECT lat, lng FROM id_geocode WHERE id == ?' cursor = g.db.execute(sql_command, (id_,)) locations_names = [description[0] for description in cursor.description] locations = cursor.fetchall() # [('lat', 'lng')] closest_distance = 25000 for job_latlng in locations: distance = vincenty.vincenty(user_latlng, job_latlng, miles=True) if distance < closest_distance: closest_distance = distance return closest_distance
def distance(lat1, lon1, lat2, lon2): """ Calculate the distance in meters between two points. """ return vincenty((lat1, lon1), (lat2, lon2)) * 1000
f_interp = open(sys.argv[1],'r') f_our = open(directory + "/our_" + num + "_" + num + "_" + progressive,'r') START = -1 END = -1 for line in f_interp.readlines(): if START == -1: START = line END = line f_interp.close() f_interp = open(sys.argv[1],'r') START_POINT= (float(START.split()[3]),float(START.split()[2])) END_POINT= (float(END.split()[3]),float(END.split()[2])) total_distance = vincenty(START_POINT, END_POINT) * 1000 for line_interp in f_interp.readlines(): line_interp = line_interp.strip() time_interp = line_interp.strip().split()[1] # Now find the row in GT found = False f_their = open(directory + "/their_" + num + "_" + progressive,'r') f_gt = open("COLOGNE/sampling_data/cologne_gap_1/" + num, 'r') while not found: line_gt = f_gt.readline().strip() if "time" in line_gt: continue # First line try: time_gt = line_gt.split(",")[1] t_gt = math.ceil(time.mktime(time.strptime(time_gt, "%Y-%m-%d %H:%M:%S")))
def distance_by_geo(lat1, long1, lat2, long2): # boston = (42.3541165, -71.0693514) #lat,long # newyork = (40.7791472, -73.9680804) pos1 = (lat1, long1) pos2 = (lat2, long2) return vincenty(pos1, pos2)