def compute_errors(known): for i, p in known.iterrows(): modern_coords = (p.modern_lat, p.modern_lon) lat_err = p.modern_lat - p.pred_lat lon_err = p.modern_lon - p.pred_lon sq_err = lat_err ** 2 + lon_err ** 2 pred_coords = (p.pred_lat, p.pred_lon) dist_err = vincenty(modern_coords, pred_coords).kilometers known.loc[i, 'lat_err'] = lat_err known.loc[i, 'lon_err'] = lon_err known.loc[i, 'sq_err'] = sq_err known.loc[i, 'dist_err'] = dist_err adjust_lat_err = p.modern_lat - p.adjust_lat adjust_lon_err = p.modern_lon - p.adjust_lon adjust_sq_err = adjust_lat_err ** 2 + adjust_lon_err ** 2 modern_coords = (p.modern_lat, p.modern_lon) adjust_coords = (p.adjust_lat, p.adjust_lon) adjust_dist_err = vincenty(modern_coords, adjust_coords).kilometers known.loc[i, 'adjust_lat_err'] = adjust_lat_err known.loc[i, 'adjust_lon_err'] = adjust_lon_err known.loc[i, 'adjust_sq_err'] = adjust_sq_err known.loc[i, 'adjust_dist_err'] = adjust_dist_err known.loc[i, 'adjust_dist_err_diff'] = dist_err - adjust_dist_err
def make_grid(zone_unit_km, min_long, max_long, min_lat, max_lat, consider_visualization=False): W_end_gps, E_end_gps = (min_long, (min_lat + max_lat) / float(2)), (max_long, (min_lat + max_lat) / float(2)) S_end_gps, N_end_gps = ((min_lat + max_lat) / float(2), min_lat), ((min_lat + max_lat) / float(2), max_lat) width_km = (vincenty(W_end_gps, E_end_gps).meters) / float(METER1000) height_km = (vincenty(S_end_gps, N_end_gps).meters) / float(METER1000) num_cols, num_rows = map(int, map(ceil, [v / float(zone_unit_km) for v in [height_km, width_km]])) # hl_length_gps, vl_length_gps = max_long - min_long, max_lat - min_lat xaxis_unit, yaxis_unit = hl_length_gps / float(num_cols), vl_length_gps / float(num_rows) x_points = [min_long + i * xaxis_unit for i in xrange(num_cols)] y_points = [min_lat + j * yaxis_unit for j in xrange(num_rows)] p0, p1 = (y_points[0], x_points[0]), (y_points[0], x_points[1]) x_dist = (vincenty(p0, p1).meters) / float(METER1000) p0, p1 = (y_points[0], x_points[0]), (y_points[1], x_points[0]) y_dist = (vincenty(p0, p1).meters) / float(METER1000) d = VincentyDistance(kilometers=zone_unit_km) print p0, d.destination(point=p0, bearing=0).latitude, d.destination(point=p0, bearing=0).longitude # print x_dist, y_dist, len(x_points), len(y_points) assert False return xaxis_unit, yaxis_unit, x_points, y_points
def distance_handler(location_obj): log("[[DISTAMCE HANDLER START]]") geolocator = Nominatim() input_data = str(location_obj['latitude']) + ", " + str(location_obj['longitude']) log("[Input location str:" + input_data) location = geolocator.reverse(input_data) adres = location.address try: log("LOCATION ADRESS:" + adres) except Exception: try: log("LOCATION ADRESS:" + adres.encode("utf8")) except: pass # log("\nLOCATION RAW:"+location.address) POINT_A = (SOURCE_LOCATION_CORDINANTES['latitude'], SOURCE_LOCATION_CORDINANTES['longitude']) POINT_B = (location_obj['latitude'], location_obj['longitude']) vincentkm = (vincenty(POINT_A, POINT_B).kilometers) circlekm = (great_circle(POINT_A, POINT_B).kilometers) log("VINCENT KM:" + str(vincentkm)) log("CIRCLE KM:") log(circlekm) vincent = (vincenty(POINT_A, POINT_B).meters) circle = (great_circle(POINT_A, POINT_B).meters) log("\nVINCENT meters:" + str(vincent)) log("CIRCLE meters:") log(circle) log("[[DISTAMCE HANDLER END BEFORE RETURN]]") return { 'vincent': vincent, 'circle': circle, 'adres': adres }
def getNeighbrs(i,eps,segment): dis = 0 pre = None neighbors = deque([]) #向前查找 i_left=0 for point in segment.points[i::-1]: i_left+=1 if (pre): dis += vincenty((pre.latitude, pre.longitude), (point.latitude, point.longitude)).meters neighbors.appendleft(point) if dis > eps: i_left+=-1 neighbors.popleft() break pre = point #向后查找 pre = None dis=0.0 i_last=0 for point in segment.points[i::1]: i_last+=1 if (pre): dis += vincenty((pre.latitude,pre.longitude), (point.latitude,point.longitude)).meters neighbors.append(point) if dis > eps: i_last+=-1 neighbors.pop() break pre = point return (neighbors,i_left,i_last)
def get_scheduled_trips(): lat_start = float(request.args.get("lat_start")) long_start = float(request.args.get("long_start")) lat_end = float(request.args.get("lat_end")) long_end = float(request.args.get("long_end")) start_time = datetime.strptime(request.args.get("start_time"), DATE_FORMAT) end_time = datetime.strptime(request.args.get("end_time"), DATE_FORMAT) distance = float(request.args.get("distance")) scheduled_trips = model.ScheduledTrip.query.filter(db.and_(model.ScheduledTrip.date <= end_time, model.ScheduledTrip.date >= start_time)) trips = [] for trip in scheduled_trips: start_distance = vincenty((lat_start, long_start), (trip.lat_start, trip.long_start)).miles start_good = start_distance < distance end_distance = vincenty((lat_end, long_end), (trip.lat_end, trip.long_end)).miles end_good = end_distance < distance print (lat_start, long_start), (trip.lat_start, trip.long_start) print "{0} {1}".format(start_distance, end_distance) print start_good, end_good if start_good and end_good: trips.append(trip.to_json()) response = { 'results': trips } return flask.jsonify(**response)
def nearest_neighbor(start, locations, output, total): if len(locations) == 0: conn = sql.connect('database.db') conn.execute("DROP TABLE IF EXISTS places") # only want to keep one set of results at a time, so clear if an old table is around conn.execute("CREATE TABLE IF NOT EXISTS places (name TEXT, lat REAL, lng REAL, total REAL)") conn.execute("INSERT INTO places (name, total) VALUES (?, ?)", ("total", total)) # storing total distance to table for loc in output: # type elements and add to array name = str(loc[0]) lat = float(loc[1]) lng = float(loc[2]) conn.execute("INSERT INTO places (name, lat, lng) VALUES (?, ?, ?)", (name, lat, lng)) conn.commit() conn.close() else: start_coordinates = (start[1], start[2]) # set min_distance and min_index to first, item then check the others (if any exist) current_coordinates = (locations[0][1], locations[0][2]) # start the index with the first item, which we know exists due to the base case check min_distance = vincenty(start_coordinates, current_coordinates).miles # use Vincenty's formula to calculate distance min_index = 0 i = 1 # we've already checked the zero index, so we start at 1 while (i < len(locations)): current_coordinates = (locations[i][1], locations[i][2]) distance = vincenty(start_coordinates, current_coordinates).miles if (distance < min_distance): # update min_index and min_distance if a new smallest distance has been found min_index = i min_distance = distance i += 1 total += min_distance # add the min_distance to the total distance, ensuring we only travel the shortes distance at every step start = locations.pop(min_index) # remove the the min_index item from locations and set it to start output.append(start) # append the new start to the output array nearest_neighbor(start, locations, output, total) # recursively call the function
def get_traveled_distance(df_sequence): last_stop = "" last_lat = "" last_long = "" distance = 0 for index, stage in df_sequence.iterrows(): par_subida = stage.par_subida par_bajada = stage.par_bajada if par_subida != par_subida: continue origin_stop = par_subida origin_lat = stage.lat_subida origin_long = stage.long_subida if last_stop != "": distance += vincenty((last_lat,last_long),(origin_lat,origin_long)).meters if par_bajada != par_bajada: last_stop = origin_stop last_lat = origin_lat last_long = origin_long else : destinatination_stop = par_bajada destination_lat = stage.lat_bajada destination_long = stage.long_bajada distance += vincenty((origin_lat,origin_long),(destination_lat,destination_long)).meters last_stop = destinatination_stop last_lat = destination_lat last_long = destination_long return distance/1000
def validateCoordinates(self, carLat, carLong, resultSet, carSpeed): """ This validates the car distance difference in km and/or meters against the coordinates in the resultset. :carPos: Car position (lat,long) :distKm: Distance in km between car pos, and coordinate in resultSet :distM: Distance in m between car pos, and coordinate in resultSet :param carLat: The car latitude coordinate :param carLong: The car longitude coordinate :param resultSet: The resultset containing the coordinates to compare the cars given coordiantes against. :return 'C' =< 100m < 'A' < 1km < 'N', SlipperyCoordinates, time: """ carPos = (carLat, carLong) # check if car coordinates is close to the resultSet (slippery Coordinates) for tup3 in resultSet: tup = (tup3[0], tup3[1]) distKM = vincenty(carPos, tup).km if distKM < 1: # calculates new distance to slippery path distM = vincenty(carPos, tup).meters if carSpeed != None: time = (distKM / carSpeed * 1.6) * 60 else: time = None if distM <= 100: return "C", tup, time # C for close else: return "A", tup, time # A for approaching # If not stopped by now, by one of the returns. That means that nearby coordinates are not slippery return ("N", None) # N for none
def make_circular_stamp(station, ruleset, submap, separation_distance_km, far_side_separation_distance_km = None): if station.get_erp_watts() == 0: raise ValueError("TV station's ERP cannot be zero. Please check input.") station_location = station.get_location() max_protected_radius = ruleset.get_tv_protected_radius_km(station, station_location) distance_east = vincenty(station_location, (station_location[0], station_location[1] + 2)) distance_west = vincenty(station_location, (station_location[0], station_location[1] - 2)) distance_north = vincenty(station_location, (station_location[0] + 2, station_location[1])) distance_south = vincenty(station_location, (station_location[0] - 2, station_location[1])) if max_protected_radius > min([distance_east, distance_west, distance_north, distance_south]): raise ValueError("Part of contour is outside the bounding box. Please check correctness of data for this protected entity.") def point_not_within_circular_contour(latitude, longitude, latitude_index, longitude_index, current_value): if current_value == 0: return 0 current_location = (latitude, longitude) if far_side_separation_distance_km is not None: return not (vincenty(station_location, current_location) <= max_protected_radius + separation_distance_km or vincenty(station_location, current_location) + max_protected_radius <= far_side_separation_distance_km) else: return not vincenty(station_location, current_location) <= max_protected_radius + separation_distance_km #Making the circular contour with buffer submap.update_all_values_via_function(point_not_within_circular_contour) return submap
def insertTimes(currList, listElem, origin): for stop in listElem.getchildren(): # Get stop distance from origin sList = list(Stop.objects.filter(tag=stop.get('tag'))) stop_point = sList[0].get_point() flag = False #Add title to the stop dictionary stop_title = sList[0].get_title().__str__() stop_list_node = Element('stop_list') stop_list_node.append(stop) appendItem = {'stop':stop_list_node, 'title':stop_title, 'distance':vincenty(stop_point, origin).mi, 'point': stop_point, 'tag':stop.get('tag'), 'route':stop.get('route')} #print 'Append: {0}'.format(appendItem) #Where does it go for curr in currList: #print 'Stop: {0}'.format(stop) if curr['distance'] == vincenty(stop_point, origin).mi: #If distance is the same, && Location is the same ( Same stop title) # Then add stop the list of curr['stop'] if curr['title'] == stop_title: curr['stop'].append(stop) #Otherwise insert into list as new entry before current. else: currList.insert(currList.index(curr), appendItem) flag = True break # If current in list is closer than new stop, add new stop before current and break. elif curr['distance'] > stop_point.distance(origin): currList.insert(currList.index(curr), appendItem) flag = True break if flag is False: currList.append(appendItem) return currList
def compute_level(temp_data,threshold): time_feature=np.array([ts/300.0 for ts in temp_data.TimeDif]) loc_feature=np.array([vincenty(temp_data.Loc[i], geo_centre).miles/vincenty(city_BB[0],city_BB[1]).miles for i in range(temp_data.shape[0])]) raduse=np.sqrt(np.power(time_feature,2)+np.power(loc_feature,2)) temp_data.Radius= np.array(raduse <= threshold) grouped = temp_data.groupby(['Type', 'Radius']).groups tmp_df=pd.DataFrame({'Id':[],'Type':[],'Name':[] ,'Time':[],'TimeDif':[],'Loc':[], 'W':[],'Radius':[]}) level=[] ids_list=[] Gran_Level=[item.split('/') for item in list(gran_event_df.columns.values)] for i in range(len(grouped.values())): ids=grouped.values()[i] if temp_data.Type[ids[0]]=="TransportationEvent": gran_Names_ids=gran_event_df.isin(list(tmp_df.Name[ids].values)).values (gran_Names_rows,gran_Names_cols)=np.where(gran_Names_ids) # temp_data.Name[ids[0]]=Gran_Level[gran_Names_cols[0]][0] if gran_Names_cols: temp_data.set_value(ids[0],'Name',Gran_Level[gran_Names_cols[0]][0]) level.append(int(Gran_Level[gran_Names_cols[0]][1])) else: level.append(len(ids)) else: level.append(len(ids)) ids_list.append(ids[0]) ids_list.sort() grouped_df=tmp_df.append([temp_data.loc[j] for j in ids_list], ignore_index=True) for gdf_index,row in grouped_df.iterrows(): if row['W']==-1: # grouped_df.xs(gdf_index, copy = False)['Loc']=('','') grouped_df.set_value(gdf_index,'Loc',(np.inf,np.inf)) return grouped_df,level
def findmech(request): if request.method == "POST": #r1=request.GET.get('usrid') r2=request.POST.get('long') r3=request.POST.get('lat') r4=request.POST.get('radius') from geopy.distance import vincenty latf=vincenty(r3,r3+1.0).meters latf=latf*radius longf=vincenty(r2,r2+1.0).meters longf=longf*radius rlat1=float(r3)+float(r4)/latf rlat2=float(r3)-float(r4)/latf rlon1=float(r2)+float(r4)/longf rlon2=float(r2)-float(r4)/longf from minor.models import avail q=avail.objects.filter(mlat__lte=rlat1) q1=q.filter(mlat__gte=rlat2) q2=q1.filter(mlon__lte=rlon1) q3=q2.filter(mlon__gte=rlon2) data = serializers.serialize('json', q3) message="OK-"+str(data) else: message="NO-" return HttpResponse(message)
def energy_generated(self): """ The total energy of u_i located at l_i is: G(u_i,l_i) = -1 * sum of sij * g<u_i,u_j> over all friends. """ #building dr, making ten bins based on social similarity distances_by_social_similarity = defaultdict(list) for user in self.users_with_location: location_user = self.mention_network.node_data(user) for neighbor in self.mention_network.neighbor_iter(user): if user == neighbor: continue location_neighbor = self.mention_network.node_data(neighbor) social_similarity_rounded = round(self.sij[user][neighbor],1) #rounded to one significant figure distance = vincenty(location_user,location_neighbor) distances_by_social_similarity[social_similarity_rounded].append(distance) for social_similarity in distances_by_social_similarity: distances = distances_by_social_similarity[social_similarity] self.dr[social_similarity] = sum(distances)/len(distances) for user in self.users_with_location: location_user = self.mention_network.node_data(user) for neighbor in self.mention_network.neighbors_iter(user): if not user in self.users_with_location: continue location_neighbor = self.mention_network.node_data(neighbor) social_similarity = self.sij[user][neighbor] #the exponent term, g<u_i,u_j> = -e^(-|l_i - l_j|/d_r) x = - vincenty(location_user,location_neighbor) / self.dr[round(social_similarity,1)] #summing sij * g<u_i,u_j> over all friends #I've factored out a -1 from np.exp(x) and cancelled it with #the leading -1 in the summation. self.g_ui[user] += social_similarity* np.exp(x)
def CalculateDistance(place,city_list,Path_to_Take,Distance,length): geolocator = Nominatim(); main_location = geolocator.geocode(place, exactly_one=True, timeout=None); main_coordinates = (main_location.latitude,main_location.longitude); list_toSort = []; for x in city_list: place_location = geolocator.geocode(x, exactly_one=True, timeout=None); place_coordinates = (place_location.latitude,place_location.longitude); print place_coordinates; result = vincenty(main_coordinates,place_coordinates).miles print result; list_toSort.append({'Name':x,'Distance':result}); newlist = sorted(list_toSort, key=lambda k: k['Distance']) Path_to_Take.append(newlist[1]); Distance += newlist[1]['Distance'] city_list.remove(city_list[0]); if len(Path_to_Take) < length: CalculateDistance(newlist[1]['Name'],city_list,Path_to_Take,Distance,length); if len(Path_to_Take) == length: main_location = geolocator.geocode(Path_to_Take[0]['Name'], exactly_one=True, timeout=None); main_coordinates = (main_location.latitude,main_location.longitude); place_location = geolocator.geocode(Path_to_Take[length-1]['Name'], exactly_one=True, timeout=None); place_coordinates = (place_location.latitude,place_location.longitude); result = vincenty(main_coordinates,place_coordinates).miles Path_to_Take.append({'Name':Path_to_Take[0]['Name'],'Distance':result});
def get_location_ranking(source, current, tweet, flag): if flag == "global" or flag == "national": return 0.00079228162 else: source_coords = get_location_coordinates(source) if tweet == "None": try: dist = vincenty(source_coords, current).miles except Exception: dist = vincenty(source_coords, current) else: try: dist = vincenty(tweet, current).miles except Exception: dist = vincenty(tweet, current) # based on distance, return ranking if dist <= 1: return 0.9 elif 1 < dist < 10: return 0.8 elif 10 <= dist < 20: return 0.7 elif 20 <= dist < 50: return 0.6 elif 50 <= dist < 100: return 0.5 elif 100 <= dist < 200: return 0.4 elif 200 <= dist < 300: return 0.3 elif 300 <= dist < 400: return 0.2 else: return 0.1
def reverse_geocode(self, lat, lon): """ Returns the closest city name given a latitude and lonitude. """ cities = self.reverse_geocoder[(round(lat,2),round(lon,2))] closest_location = float("inf") best_location = None for city in cities: lat2 = city[0] lon2 = city[1] distance = vincenty((lat,lon),(lat2,lon2)).km if distance < closest_location: closest_location = distance best_location = city[2] #if the direct search fails to find a location within 20km of the requested location, #the lat/lon is assumed to be either not available or a fringe case. In the latter #we check all the surrounding boxes in the reverse geocoder. if closest_location > 20: cities = self.reverse_geocoder[(round(lat+0.01,2),round(lon+0.01,2))] \ + self.reverse_geocoder[(round(lat+0.01,2),round(lon-0.01,2))] + self.reverse_geocoder[(round(lat-0.01,2),round(lon+0.01,2))] \ + self.reverse_geocoder[(round(lat-0.01,2),round(lon-0.01,2))] + self.reverse_geocoder[(round(lat,2),round(lon+0.01,2))] \ + self.reverse_geocoder[(round(lat,2),round(lon-0.01,2))] + self.reverse_geocoder[(round(lat+0.01,2),round(lon,2))] \ + self.reverse_geocoder[(round(lat-0.01,2),round(lon,2))] for city in cities: lat2 = city[0] lon2 = city[1] distance = vincenty((lat,lon),(lat2,lon2)).km if distance < closest_location: closest_location = distance best_location = city[2] return best_location
def get_location_ranking(source, current, tweet): source_coords = get_location_coordinates(source) if tweet == "None": try: dist = vincenty(source_coords, current).miles except Exception: dist = vincenty(source_coords, current) else: try: dist = vincenty(tweet, current).miles except Exception: dist = vincenty(tweet, current) # based on distance, return ranking if dist <= 1: return 0.8 elif 1 < dist < 10: return 0.64 elif 10 <= dist < 20: return 0.4096 elif 20 <= dist < 50: return 0.16777216 elif 50 <= dist < 100: return 0.02814749767 elif 100 <= dist < 200: return 0.00079228162 else: return 0.00000062771
def test_circular_stamp_creation(self): """Tests for submaps created using circular contours""" def inside_us_function(latitude, longitude, latitude_index, longitude_index, current_value): return self.region.location_is_in_region((latitude, longitude)) #Control submap submap_lat = (self.min_lat_us + self.max_lat_us)/2 submap_lon = (self.min_lon_us + self.max_lon_us)/2 tv_station = ProtectedEntityTVStation(protected_entities_tv_stations_vidya.ProtectedEntitiesTVStationsUnitedStatesIncentiveAuction_PruneData(self.region), self.region, submap_lat, submap_lon, 20, 0, 45, 'DT') bounding_box = tv_station.get_bounding_box() min_lat_index = west.helpers.find_last_value_below_or_equal(self.region_map.latitudes, bounding_box['min_lat']) max_lat_index = west.helpers.find_first_value_above_or_equal(self.region_map.latitudes, bounding_box['max_lat']) min_lon_index = west.helpers.find_last_value_below_or_equal(self.region_map.longitudes, bounding_box['min_lon']) max_lon_index = west.helpers.find_first_value_above_or_equal(self.region_map.longitudes, bounding_box['max_lon']) submap = west.data_map.DataMap2D.from_specification((self.region_map.latitudes[min_lat_index], self.region_map.latitudes[max_lat_index]), (self.region_map.longitudes[min_lon_index], self.region_map.longitudes[max_lon_index]), max_lat_index - min_lat_index + 1, max_lon_index - min_lon_index + 1) submap.update_all_values_via_function(inside_us_function) submap_control = west.data_map.DataMap2D.get_copy_of(submap) submap_control.reset_all_values(1) #This is temporarily done to make sure we have a nice control submap with all points inside continental US for i in range(max_lat_index - min_lat_index + 1): for j in range(max_lon_index - min_lon_index + 1): self.assertEqual(submap.get_value_by_index(i, j), submap_control.get_value_by_index(i, j)) #Test for station with ERP = 0. self.assertRaisesRegexp(ValueError, "TV station's ERP cannot be zero. Please check input.", stamp_maker.make_circular_stamp, tv_station, tv_station.get_location(), self.region, self.ruleset, submap_control, 0) #Test for station with large ERP such that protection radius > bounding box size: No easy way to test this right now as protected radius is not independent of direction :( tv_station._ERP_Watts = 1000000000 max_protected_radius = self.ruleset.get_tv_protected_radius_km(tv_station, tv_station.get_location()) dist1 = vincenty(tv_station.get_location(), (tv_station.get_location()[0], min(submap_control.longitudes))) dist2 = vincenty(tv_station.get_location(), (tv_station.get_location()[0], max(submap_control.longitudes))) dist3 = vincenty(tv_station.get_location(), (min(submap_control.latitudes), tv_station.get_location()[1])) dist4 = vincenty(tv_station.get_location(), (max(submap_control.latitudes), tv_station.get_location()[1])) self.assertRaisesRegexp(ValueError, "Part of contour is outside the bounding box. Please check correctness of data for this protected entity.", stamp_maker.make_circular_stamp, tv_station, tv_station.get_location(), self.region, self.ruleset, submap_control, 0) #Test for station with regular circular contour + buffer tv_station._ERP_Watts = 230 * 1000 buffer_dist = 4 max_protected_radius = self.ruleset.get_tv_protected_radius_km(tv_station, tv_station.get_location()) + buffer_dist initial_circular_polygon_coords = [(tv_station.get_location()[1], tv_station.get_location()[0]), (tv_station.get_location()[1], tv_station.get_location()[0]), (tv_station.get_location()[1], tv_station.get_location()[0]), (tv_station.get_location()[1], tv_station.get_location()[0])] circular_polygon_coords = self.buffer_maker.add_buffer(initial_circular_polygon_coords, max_protected_radius) circular_polygon = shapely.geometry.polygon.Polygon(shell = circular_polygon_coords) def outside_circular_polygon(latitude, longitude, latitude_index, longitude_index, current_value): point = shapely.geometry.Point((longitude, latitude)) return not point.within(circular_polygon) submap.update_all_values_via_function(outside_circular_polygon) submap_control.reset_all_values(1) test_submap = stamp_maker.make_circular_stamp(tv_station, tv_station.get_location(), self.region, self.ruleset, submap_control, buffer_dist) for i in range(max_lat_index - min_lat_index + 1): for j in range(max_lon_index - min_lon_index + 1): self.assertEqual(submap.get_value_by_index(i, j), test_submap.get_value_by_index(i, j))
def get_parking_coords(starting_coordinates, radius): # Get the parking spaces data table_name = "SF_bike_parking" sql_query = "SELECT Y,X,racks,spaces,yr_inst FROM " + table_name + " WHERE yr_installed < 2016" df_parking = pd.read_sql_query(sql_query, engine) # Calculate distances to list of bike racks and filter by racks within a radius rack_coords = [] num_racks = [] num_spaces = [] parking_address = [] dist_from_dest = [] radius_racks = radius # in miles space_count_in_crime_area = 0 # keep track of the total number of racks in the local region defined for index, row in df_parking.ix[0:].iterrows(): if vincenty(starting_coordinates, [row["Y"], row["X"]]).miles <= radius_racks: rack_coords.append([row["Y"], row["X"]]) num_racks.append(row["racks"]) num_spaces.append(row["spaces"]) parking_address.append(row["yr_inst"]) dist_from_dest.append(round(vincenty(starting_coordinates, [row["Y"], row["X"]]).miles, 2)) space_count_in_crime_area += row["spaces"] elif (vincenty(starting_coordinates, [row["Y"], row["X"]]).miles > radius_racks) and ( vincenty(starting_coordinates, [row["Y"], row["X"]]).miles <= 0.75 ): space_count_in_crime_area += row["spaces"] continue else: continue return (rack_coords, num_racks, num_spaces, dist_from_dest, parking_address, space_count_in_crime_area)
def distance_handler(location_obj): geolocator = Nominatim() input_data = str(location_obj['latitude']) + ", " + str(location_obj['longitude']) log("[Input location str:" + input_data) location = geolocator.reverse(input_data) adres = location.address log("\nLOCATION ADRESS:" + adres) # log("\nLOCATION RAW:"+location.address) POINT_A = (SYRKOMLI['latitude'], SYRKOMLI['longitude']) POINT_B = (location_obj['latitude'], location_obj['longitude']) vincentkm = (vincenty(POINT_A, POINT_B).kilometers) circlekm = (great_circle(POINT_A, POINT_B).kilometers) log("VINCENT KM:" + str(vincentkm)) log("CIRCLE KM:") log(circlekm) vincent = (vincenty(POINT_A, POINT_B).meters) circle = (great_circle(POINT_A, POINT_B).meters) log("\nVINCENT meters:" + str(vincent)) log("CIRCLE meters:") log(circle) return { 'vincent': vincent, 'circle': circle, 'adres': adres }
def point_not_within_circular_contour(latitude, longitude, latitude_index, longitude_index, current_value): if current_value == 0: return 0 current_location = (latitude, longitude) if far_side_separation_distance_km is not None: return not (vincenty(station_location, current_location) <= max_protected_radius + separation_distance_km or vincenty(station_location, current_location) + max_protected_radius <= far_side_separation_distance_km) else: return not vincenty(station_location, current_location) <= max_protected_radius + separation_distance_km
def example2(): for la in range(28, 48): print(vincenty((la, -102.1234), (la, -103.1234)).miles, great_circle((la, -102.1234), (la, -103.1234)).miles) for lg in range(-125, -67): print(vincenty((34.1234, lg), (35.1234, lg)).miles, great_circle((34.1234, lg), (35.1234, lg)).miles)
def points_to_grids(grids, start_point, end_point, total_proportion, average_lat_diff, average_lon_diff, neighbors): grid1 = lat_lon_to_grid(start_point, neighbors) grid2 = lat_lon_to_grid(end_point, neighbors) ## In case point out of map if not grid1 or not grid2: return None if grid1 == grid2: return [(grid1, total_proportion)] if same_point(start_point, end_point): return [(grid1, total_proportion)] else: # Start and End point belong to different grids. Need to find the intersection of boundaries. # Use binary search # First check if two grids share an edge. If so, find the intersection of the line and the boundary directly. # Else, take the midpoint, then repeat the process for both (start, mid), (mid, end). truth_value, lat_or_lon, value = share_an_edge(grids, lat_lon_to_grid(start_point, neighbors), lat_lon_to_grid(end_point, neighbors), average_lat_diff, average_lon_diff) if truth_value == True: # Then it becomes the intersection of two lines problem. slope = (start_point[0] - end_point[0]) / (start_point[1] - end_point[1] + 0.0000000001) y_intercept = start_point[0] - slope * start_point[1] if lat_or_lon == "lat": lon_intercept = (value - y_intercept) / slope mid_point = [value, lon_intercept] start_to_mid = vincenty(start_point, mid_point) mid_to_end = vincenty(mid_point, end_point) prop1 = start_to_mid / (start_to_mid + mid_to_end) prop2 = mid_to_end / (start_to_mid + mid_to_end) elif lat_or_lon == "lon": lat_intercept = slope * value + y_intercept mid_point = [lat_intercept, value] start_to_mid = vincenty(start_point, mid_point) mid_to_end = vincenty(mid_point, end_point) prop1 = start_to_mid / (start_to_mid + mid_to_end) prop2 = mid_to_end / (start_to_mid + mid_to_end) else: raise Exception("Bad value in share_an_edge() return", lat_or_lon) return [(grid1, prop1 * total_proportion), (grid2, prop2 * total_proportion)] else: mid_point = [(start_point[0] + end_point[0])/2.0, (start_point[1] + end_point[1])/2.0] firstHalf = points_to_grids(grids,start_point, mid_point, total_proportion / 2.0, average_lat_diff, average_lon_diff, neighbors) secondHalf = points_to_grids(grids, mid_point, end_point, total_proportion / 2.0, average_lat_diff, average_lon_diff, neighbors) if firstHalf == None and secondHalf == None: return None elif firstHalf == None: return secondHalf elif secondHalf == None: return firstHalf else: return firstHalf + secondHalf
def cluster_geo(posts, method='dbscan', eps=0.15, min_samples=10, max_cluster_size=float('inf')): print 'Clustering %i points: ' % posts.shape[0] if method.lower() == 'kmeans': from sklearn.cluster import KMeans print 'clustering lat,long by kmeans' # Classify into n_clusters: n_clust = int(np.sqrt(posts[['lat', 'long']].shape[0]/2)) km = KMeans(n_clust, init='k-means++') # initialize km.fit(posts[['lat', 'long']]) cluster_labels = km.predict(posts[['lat', 'long']]) # classify elif method.lower() == 'dbscan': print 'clustering lat,long by dbscan' # if posts.shape[0] from sklearn.cluster import DBSCAN from sklearn import metrics from sklearn.datasets.samples_generator import make_blobs from sklearn.preprocessing import StandardScaler standardized = StandardScaler().fit_transform(posts[['lat', 'long']].values) # eps = 0.2 db = DBSCAN(eps=eps, min_samples=min_samples).fit(standardized) # db = DBSCAN(eps=eps, min_samples=min_samples).fit(standardized) core_samples_mask = np.zeros_like(db.labels_, dtype=bool) core_samples_mask[db.core_sample_indices_] = True cluster_labels = db.labels_ # Number of clusters in labels, ignoring noise if present. n_clust = len(set(cluster_labels)) - (1 if -1 in cluster_labels else 0) print('Estimated number of clusters: %d' % n_clust) print("Silhouette Coefficient: %0.3f" % metrics.silhouette_score(posts[['lat', 'long']].values, cluster_labels)) for clust in list(set(cluster_labels[cluster_labels>=0])): # dat = posts geo_mean = posts.loc[cluster_labels == clust, ['lat', 'long']].mean() geo_std = posts.loc[cluster_labels == clust, ['lat', 'long']].std() lat_range = vincenty((geo_mean['lat'] - 2*geo_std['lat'], geo_mean['long']), (geo_mean['lat'] + 2*geo_std['lat'], geo_mean['long'])).miles long_range = vincenty((geo_mean['lat'], geo_mean['long'] - 2*geo_std['long']), (geo_mean['lat'], geo_mean['long'] + 2*geo_std['long'])).miles if (lat_range * long_range) > max_cluster_size: print 'cluster %i is %.3f, removing' % (clust, lat_range * long_range) cluster_labels[cluster_labels == clust] = -1 # remove cluster return cluster_labels
def sendNearPOI(self, bot, chat_id, pos): io = (pos['latitude'], pos['longitude']) distDict = {} db = pickledb.load('db/mensaDB.db', False) for key in db.getall(): a = db.get(key) mensaCoord = (a['coord']['lat'], a['coord']['lon']) distDict[key] = vincenty(io, mensaCoord).kilometers MensaNearPOI = min(distDict, key=distDict.get) km = str(round(float(distDict[MensaNearPOI]), 4)) prettyNearPOI = str(MensaNearPOI).title() if prettyNearPOI == 'Sanfrancesco': prettyNearPOI = 'San Francesco' textMensa = 'Mensa più vicina: ' + str(prettyNearPOI) + \ ', distanza: ' + str(km) + ' km' + \ '. \nPer maggiori informazioni: /' + str(MensaNearPOI) io = (pos['latitude'], pos['longitude']) distDict = {} db = pickledb.load('db/aulastudioDB.db', False) for key in db.getall(): a = db.get(key) asCoord = (a['coord']['lat'], a['coord']['lon']) distDict[key] = vincenty(io, asCoord).kilometers AsNearPOI = min(distDict, key=distDict.get) km = str(round(float(distDict[AsNearPOI]), 4)) prettyNearPOI = str(AsNearPOI).title() if prettyNearPOI == 'Viavenezia': prettyNearPOI = 'Via Venezia' elif prettyNearPOI == 'Titolivio': prettyNearPOI = 'Tito Livio' elif prettyNearPOI == 'Vbranca': prettyNearPOI = 'Vittore Branca' elif prettyNearPOI == 'Reset': prettyNearPOI = 'Circolo Reset' textAS = '\n\nAula studio più vicina: ' + str(prettyNearPOI) + \ ', distanza: ' + str(km) + ' km' + \ '. \nPer maggiori informazioni: /' + str(AsNearPOI) distDict = {} db = pickledb.load('db/biblioDB.db', False) for key in db.getall(): a = db.get(key) biblioCoord = (a['coord']['lat'], a['coord']['lon']) distDict[key] = vincenty(io, biblioCoord).kilometers biblioNearPOI = min(distDict, key=distDict.get) km = str(round(float(distDict[biblioNearPOI]), 4)) prettyNearPOI = db.get(biblioNearPOI)['nome'] textBiblio = '\n\nBiblioteca più vicina: ' + str(prettyNearPOI) + \ ', distanza: ' + str(km) + ' km' + \ '. \nPer maggiori informazioni: /' + str(biblioNearPOI) text = textMensa + textAS + textBiblio markup = [['/' + MensaNearPOI, '/' + AsNearPOI], ['/' + biblioNearPOI, '/home']] reply_markup = telegram.ReplyKeyboardMarkup(markup) bot.sendMessage(chat_id=chat_id, text=text, reply_markup=reply_markup)
def get_distances(cities, format): locator = Nominatim() geolocations = [locator.geocode(city) for city in cities] distances = [] for i in range(len(cities)-1): if format == "kilometers": distances.append(vincenty(get_coordinates(geolocations[i]), get_coordinates(geolocations[i+1])).kilometers) else: distances.append(vincenty(get_coordinates(geolocations[i]), get_coordinates(geolocations[i+1])).miles) return distances
def search(args): start = args['start_lat'], args['start_lon'] dest = args['dest_lat'], args['dest_lon'] atob = distance.vincenty(start, dest).km if atob < 2: radius = atob / 2 else: radius = 1 # Get the results # Setting radius here, however it is varied within the get Data method starting at 200 meters results = json.loads(db_access.getData(args, radius=radius, online=True)) if len(results) == 0: result = dt.timedelta(hours=0, minutes=0, seconds=0, microseconds=0) return str(result) for res in results: # Get start end and point as tuples a = res['start_lat'], res['start_long'] b = res['dest_lat'], res['dest_long'] # Calculate distance between the results dist = distance.vincenty(a, b) # Calculate the deviation from the Original Requested location deviation = distance.vincenty(a, start).km + distance.vincenty(b, dest).km res['deviation'] = deviation delta = res['duration'] res['adjustedDurationS'] = (atob/ dist.km) * int(delta) devSum = sum(res['deviation'] for res in results) durSum = 0 # Add up the weighted times for res in sorted(results, key=lambda k: k['deviation']): alpha = res['deviation'] / devSum #print str(alpha) # Only one result, weighting is not necessary if not alpha == 1: durSum += res['adjustedDurationS'] * (1 - alpha) else: durSum = res['adjustedDurationS'] # Add Information result = {} result['estimatedDuration'] = str(dt.timedelta(seconds=int(durSum))) result['numberOfResults'] = str(len(results)) return result['estimatedDuration']
def converttomts(self): pdb.set_trace() origin = (self.latmatrix[0, 0], self.longmatrix[0, 0]) self.x_mt = np.empty_like(self.latmatrix) self.y_mt = np.empty_like(self.x_mt) for i in range(self.no_points): for j in range(self.no_points): point = (self.latmatrix[i, j], self.longmatrix[i, j]) self.x_mt[i, j] = vincenty((self.latmatrix[i, 0], self.longmatrix[i, 0]), point).meters self.y_mt[i, j] = vincenty((self.latmatrix[0, j], self.longmatrix[0, j]), point).meters return (self.x_mt, self.y_mt)
def closestStation(df,station1=station1,station2=station2): StationDistance = [] for index,item in df.iterrows(): test_site = item['test_geo'] s1Distance = vincenty(station1, test_site).miles s2Distance = vincenty(station2, test_site).miles if s1Distance > s2Distance: StationDistance.append({'test_geo':test_site,'Station':2}) else: StationDistance.append({'test_geo':test_site,'Station':1}) return StationDistance
def getClusterIndex(coordinate): distance = 1000000 index = -1 for i in range(len(areaCoordinateList)): tmp = vincenty(areaCoordinateList[i], coordinate).km if tmp>1: continue if vincenty(areaCoordinateList[i], coordinate).km<distance: distance = vincenty(areaCoordinateList[i], coordinate).km index = i return index
lineArray = line.split(",") if len(lineArray) == 1: continue if prevLine != "": osm_id = lineArray[4] if osm_id != prevLine.split(",")[4]: prevLineArray = prevLine.split(",") coordinate1 = (lineArray[1], lineArray[2]) coordinate2 = (prevLineArray[1], prevLineArray[2]) # print str(coordinate1) + " " + str(coordinate2) # Get distance b/w start and end point distance = vincenty(coordinate1, coordinate2).miles # Get time difference b/w start and end point timeSpent = int(lineArray[0]) - int( prevLineArray[0]) + straightTimeSpent # Get time class timeClass = classifyTime(int(prevLineArray[0])) # Get day class (1: weekday, 2: weekend, holiday) dayClass = 2 # Output: osm_id1 / osm_id2 / time spent / r_type1 / r_type2 / laneNum1 / laneNum2 / timeClass / dayClass / turnType if 0 < int(timeSpent) < 400: outputFile.write( '%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n' %
def coord_regression(predictions_b,predictions,train,test,method): mean_error = [] if(method==1): machine_learn = KNeighborsRegressor(n_neighbors=5, weights = 'distance') elif(method==2): #machine_learn = MLPClassifier(solver='sgd',learning_rate = 'adaptive',verbose='true',activation='tanh',alpha=1e-5) machine_learn = MLPClassifier(solver='sgd',learning_rate = 'adaptive',verbose='false',activation='tanh',alpha=1e-5,max_iter=400) #THE BEST #machine_learn = MLPClassifier(hidden_layer_sizes=(100,5), solver='sgd',learning_rate = 'adaptive',verbose='true',activation='tanh',alpha=1e-5,max_iter=500) #model = MLPClassifier(learning_rate = 'adaptive') #solvers = ['lbfgs', 'sgd', 'adam'] #activations = ['identity', 'logistic', 'tanh', 'relu'] #max_its = [200,400,600] #machine_learn = GridSearchCV(estimator=model, param_grid=dict(activation =activations,max_iter=max_its),n_jobs=7) #GRID #for each building for j in range(3): new_train1 = train.loc[train['BUILDINGID'] == j] #select for training only buildings with that label (0,1, or 2) ind = [x for x in range(len(predictions_b)) if predictions_b[x]==j] #get the position of the samples that have building == i new_test1 = test.iloc[ind,:] if(ind): #for each floor for i in range(5): new_train2 = new_train1.loc[new_train1['FLOOR'] == i] if(not new_train2.empty): indexes = [x for x in range(len(predictions)) if (predictions[x]==i and predictions_b[x]==j)] #get the position of the samples that have building == i else: index = [] if (indexes): #if list is not empty X_train = new_train2.ix[:,0:519] Y_train = new_train2[['LONGITUDE','LATITUDE']] machine_learn.fit(X_train,Y_train) #testing samples with prediction building == i new_test2 = test.iloc[indexes,:] X_test = new_test2.ix[:,0:519] Y_test = new_test2[['LONGITUDE','LATITUDE']] #Turn into list predicts_lon_lat = machine_learn.predict(X_test).tolist() Y_test = Y_test.values.tolist() distance = [] for j in range(len(predicts_lon_lat)): #change the latitude and longitude unit myProj = Proj("+proj=utm +zone=23K, +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs") lon_pred,lat_pred = myProj(predicts_lon_lat[j][0], predicts_lon_lat[j][1], inverse=True) lon_Y, lat_Y = myProj(Y_test[j][0], Y_test[j][1], inverse=True) #join in a unique list Y = [] Y.append(lon_Y) Y.append(lat_Y) predict = [] predict.append(lon_pred) predict.append(lat_pred) #The distance between the two latitudes is the error distance.append(vincenty(Y, predict).meters) print "distance" print distance #If you want to use haversine distance, uncomment the line below #print haversine(lon_Y, lat_Y, lon_pred, lat_pred) mean_error.append(np.mean(distance)) #print(np.mean(distance)) return np.mean(mean_error)
def distance_to(self, lat, lng): return vincenty((self.lat, self.lng), (lat, lng))
(-22.910738, -43.270668), (-22.910721, -43.270664), (-22.8144, -43.187374), (-22.910677, -43.270573), (-22.910633, -43.269493), (-22.814739, -43.188255)] while contfog <= len(fog_coord): #for line in list_localidades_fog: FileTemp = open(str(FileName) + ".tmp", 'a') qntd_recebidos = 0 cont = 0 bus_test = simulador.Bus() fog_test = simulador.Fog() #fog_localization = fog_test.fog_localization(contfog) #print fog_localization for i in range(0, 3600): distance = vincenty(bus_test.bus_request(i), fog_coord[contfog]).meters if (distance <= RANGE_FOG): #bus_test.send_data() qntd_recebidos = qntd_recebidos + 1 #print qntd_recebidos #cont = cont + 1 time.sleep(1) #print cont #print i print 'oi' FileTemp.write(str(fog_coord[contfog]) + " " + str(qntd_recebidos) + "\n") contfog = contfog + 1
def center_width_m(self): """ Returns the width of the bounding box at its center latitude, in meters. """ ave_lat = (self.n_lat + self.s_lat) / 2 return vincenty((ave_lat, self.w_lng), (ave_lat, self.e_lng)).meters
configs=[['hard', 'hard', 'hard']] mechanisms = ['planar_lap'] run_time = 400 eta_tolerance = 100 req_delay = 10 privacy_levels = [1.4] genric_utilities = [0.05,0.1,0.15,0.2,0.4,0.6,0.8,1.0,1.2,1.4] obfuscation_level = None debug_='MEDIUM' z_qlg=-1 g_res=200 geo_lat=g_res/(vincenty([0,0], [0,1]).meters) geo_lon=g_res/(vincenty([0,0], [1,0]).meters) alpha=calculate_pg_normalizer(privacy_levels[0],genric_utilities[0],g_res) uniform=1 r_uniform=1 uniform_eta=1 uniform_dm=1 g_remap=0 exp_folder='./planar_lap_diff_epss_uniform_RD' if not os.path.exists(exp_folder): os.makedirs(exp_folder) count=0
list(set(['school', 'opp']) - set([var]))[0] + '_id_', 'site_' ]: var_list = [ var + '_id_lat', var + '_id_long', loc_type + 'lat', loc_type + 'long' ] if loc_type == 'site_': temp = games.loc[(games[var_list].isnull().sum(axis=1) == 0), var_list].drop_duplicates() else: temp = games.loc[(games[var_list].isnull().sum(axis=1) == 0) & pd.isnull(games.site), var_list].drop_duplicates() temp.loc[:, var + '_' + loc_type + 'dist'] = temp.apply( lambda x: vincenty( (x[var + '_id_lat'], x[var + '_id_long']), (x[loc_type + 'lat'], x[loc_type + 'long'])).miles, axis=1) distances = pd.merge(distances, temp, how='left', on=var_list) distances.loc[:, 'school_dist'] = distances.apply( lambda x: x.school_site_dist if pd.isnull(x.site) == False else x.school_opp_id_dist if x.location == 'Away' else None, axis=1) distances.loc[:, 'opp_dist'] = distances.apply( lambda x: x.opp_site_dist if pd.isnull(x.site) == False else x.opp_school_id_dist if x.location == 'Home' else None, axis=1)
lat.append(row[8]) lon.append(row[9]) sys.stdout.write("\r%d sensors have been appended." % len(sensors)) sys.stdout.flush() print '' sensor = '74DA3895E1AC' for s, la, lo in zip(sensors, lat, lon): if s == sensor: base = (float(la), float(lo)) break for s, la, lo in zip(sensors, lat, lon): place = (float(la), float(lo)) dist.append(vincenty(base, place).kilometers) # discretize distance discretized_dist = [] for d in dist: if d % dist_discretize != 0: d = d - d % dist_discretize + dist_discretize else: d = d discretized_dist.append(int(d)) # write transactions date = '2017-03-27' #time = '12:00:00' transaction = [0, 0]
def distance(point1,point2): return vincenty(point1,point2).m
def __distance(self,p1,p2): #No se tiene en cuenta la elevacion return vincenty(p1, p2)
def distance_to(self, other): return vincenty(self.gps, other.gps)
def dist(p1, p2): return vincenty(p1, p2).km
with open('Columbus_TVData_GradeB_SepDis.csv', 'w') as csvoutput: next(csvfile) writer = csv.writer(csvoutput, lineterminator='\n') fieldnames = [ "app_id", "facility_id", "Grade_A", "Grade_B", "effective_erp", "HAAT_m", "station_channel" ] for ls in range(361): fieldnames.extend(["Latitude", "Longitude"]) writer.writerow(fieldnames) tv_reader = csv.reader(csvfile, delimiter=',') for row in tv_reader: print j Lat_Tower = row[7] Lon_Tower = row[8] if vincenty([lat_S, long_S], [Lat_Tower, Lon_Tower]) <= DIS: pol = [] if row[2] in (None, ""): App_ID = "" Facility_ID = "" Rad_A = "" Rad_B = "" ERP_Tower = "" HT_Tower = float(row[5]) Lat_Tower = float(row[7]) Lon_Tower = float(row[8]) Lat_list = row[9::2] Lon_list = row[10::2] for ls in range(360): pol.extend([float(Lat_list[ls]), float(Lon_list[ls])]) chan = float(row[6])
def calculate_distance_matrix(self): self.distances = np.zeros((len(self.unique_labels)-1, len(self.unique_labels)-1)) for first in range(len(self.distances)): for second in range(len(self.distances)): self.distances[first, second] = vincenty(self.centers.get(first)[0], self.centers.get(second)[0]).kilometers
def get_closest_bar(json_data, longitude, latitude): position = (longitude, latitude) closest_bar = min( json_data, key=lambda x: vincenty(x['geoData']['coordinates'], position).miles) return closest_bar
def get_distance(self, a, b): d = vincenty(a, b).meters return d
data_after = y.values[i+1] timelist.append(int(data_after[2])-int(data_before[2])) before_gps = eval(data_before[3]) after_gps = eval(data_after[3]) if data_after[1] != data_before[1]: new_order_num += 1 tempSpeed.append(np.nan) a_speed.append(np.nan) Bearing.append(np.nan) a_Bearing.append(np.nan) if new_order_num > 50: print('订单切换太多,说明共乘订单没有处理好') exit() else: tempSpeed.append(vincenty(before_gps, after_gps).meters / timelist[i]) # radians() 方法将角度转换为弧度 degrees() 将弧度转换为角度 yy = math.sin(math.radians(after_gps[0]) - math.radians(before_gps[0])) * math.radians( math.cos(after_gps[1])) xx = math.radians(math.cos(before_gps[1])) * math.radians(math.sin(after_gps[1])) - \ math.radians(math.sin(before_gps[1])) * math.radians(math.cos(after_gps[1])) \ * math.radians(math.cos(after_gps[0]) - math.radians(before_gps[0])) # Convert radian from -pi to pi to [0, 360] degree b = (math.atan2(yy, xx) * 180. / math.pi + 360) % 360 Bearing.append(b) if i >= 1: if np.isnan(tempSpeed[i]) or np.isnan(tempSpeed[i - 1]): a_speed.append(np.nan) else: a_speed.append((tempSpeed[i] - tempSpeed[i - 1]) / timelist[i - 1])
def vincenty_distance(pos_1, pos_2): pos_1 = (pos_1[0], pos_1[1]) pos_2 = (pos_2[0], pos_2[1]) return vincenty(pos_1, pos_2).meters
def distance_in_meters(coords_1, coords_2): return vincenty(coords_1, coords_2).m
counter = counter.reset_index(drop=True) #A trial of the index counter.buyer_coordinates[0] ############################################################################## ####################################Distance Data############################# ############################################################################## #The data now being generated we move on to the creation of distance data #We turn to a list comprehension to create this column as it is faster #we are also unable to do so otherwise because of the idiosyncracies of the #sara as well... data['distance'] = [ vincenty(data.buyer_coordinates.iloc[i], data.target_coordinates.iloc[i]).miles for i in range(len(data)) ] #We now create the distances for the counterfactuals counter['distance'] = [ vincenty(counter.buyer_coordinates.iloc[i], counter.target_coordinates.iloc[i]).miles for i in range(len(counter)) ] #We now print the data to ensure that we are doing things rightly data.head(n=3) #We do the same for the counterfactuals counter.head(n=3)
location = walk(geodat, 'location') if location: return location[1] return None def distance(origin, dest): values = "origins={},{}%20destinations={},{}".format( origin['lat'], origin['lng'], dest['lat'], dest['lng']) #data = urllib.urlencode(values) url = distance_url + values response = requests.get(url) if response.ok: geodat = json.loads(response.text) return geodat return None if __name__ == '__main__': reply = geocode('07481') me = geocode('280 Monroe Ave, Wyckoff NJ 07481') reply = reverse_geocode(reply['lat'], reply['lng']) allendale = geocode('Crestwood Lake, Allendale, NJ 07401') #reply = distance( me, allendale ) distance = vincenty((float(me['lat']), float(me['lng'])), (float(allendale['lat']), float(allendale['lng']))) print(distance.miles) #reply = distance( reverse_geocode(me['lat'],me['lng']), reverse_geocode(allendale['lat'],allendale['lng']) ) pass
def distance_between_points(point_a, point_b): a = (point_a.latitude, point_a.longitude) b = (point_b.latitude, point_b.longitude) return round(vincenty(a, b).kilometers * 1000)
def parseBlogLocations(path_blogLocation): for path, fileName in rscandir(path_blogLocation): try: filePath = os.path.join(path, fileName) print("\nReading locations from {}".format(fileName)) cityPairCand = fileName.split('_and_') cityPair = (cityPairCand[0], cityPairCand[1].rstrip('json').replace('.', '')) #dist = srcDest_distance(cityPair[0], cityPair[1]) src = cityPair[0] dest = cityPair[1] if comboResultsKnown(fileName): print("Combo {},{} was already captured results in : {}{}". format(src, dest, output_dir, fileName)) continue srcLocDict = getGeoCodeDict( src) #ca_geolocator.geocode(src + ', California',timeout=5) destLocDict = getGeoCodeDict( dest) #ca_geolocator.geocode(dest+ ', California',timeout=5) if not srcLocDict: print("Unable to locate src : {}".format(src)) continue if not destLocDict: print("Unable to locate dest : {}".format(dest)) continue print("srcLocDict['display_name'] : {}".format( srcLocDict['display_name'])) print("destLocDict['display_name'] : {}".format( destLocDict['display_name'])) srcLatLong = (srcLocDict['lat'], srcLocDict['lon']) destLatLong = (destLocDict['lat'], destLocDict['lon']) threshold_dist = vincenty(srcLatLong, destLatLong).miles print('srcDest_distance of {} is {} miles'.format( cityPair, threshold_dist)) mentionsDict = {} mentionsDict[src] = srcLocDict mentionsDict[dest] = destLocDict mentionsDict['mentions'] = {} ignored_mentionsDict = {} ignored_mentionsDict[src] = srcLocDict ignored_mentionsDict[dest] = destLocDict ignored_mentionsDict['mentions'] = {} with open(filePath) as data_file: data = json.load(data_file) locationsParsed = (data['location_BlogUrls'].keys()) all_locations_count = len(locationsParsed) print( "No of locations Parsed : {}".format(all_locations_count)) ignored_locations_count = 0 print( "Following location mentions are too far from src and destination, hence ignoring..." ) for location in locationsParsed: blogLinks = data['location_BlogUrls'][location] location = location.lower() mentionLocDict = getGeoCodeDict( location) #ca_geolocator.geocode(location,timeout=5) #print(location) #print(mentionLoc) if not mentionLocDict: #print("invalid location : ", location) ignored_locations_count += 1 continue mentionLatLong = (mentionLocDict['lat'], mentionLocDict['lon']) src_mentionDist = vincenty(srcLatLong, mentionLatLong).miles dest_mentionDist = vincenty(destLatLong, mentionLatLong).miles closest_to = "" srcCloser = False destCloser = False #print(closest_to) if (src_mentionDist <= threshold_dist): srcCloser = True closest_to = src if (dest_mentionDist <= threshold_dist): destCloser = True closest_to = dest #print(closest_to) locationMentionDict = dict( address=mentionLocDict['display_name'], latitude=mentionLocDict['lat'], longitude=mentionLocDict['lon'], closest_to=closest_to, locType=mentionLocDict['type'], blogLinks=blogLinks) if srcCloser or destCloser: mentionsDict['mentions'][ location] = locationMentionDict else: #print("\t\tthreshold_dist : {}".format(threshold_dist)) #print("\t\tsrc_mentionDist : {}".format(src_mentionDist)) #print("\t\tdest_mentionDist : {}".format(dest_mentionDist)) ignored_locations_count += 1 ignored_mentionsDict['mentions'][ location] = locationMentionDict print(location) print("No of locations Ignored : {}".format( ignored_locations_count)) print("Valid locations extracted : {}".format( all_locations_count - ignored_locations_count)) #print(mentionsDict) with open(output_dir + fileName, 'w') as outfile: json.dump(mentionsDict, outfile, indent=4) print("Valid locations dumped to {}{}".format( output_dir, fileName)) with open(outputIgnore_dir + fileName, 'w') as outfile: json.dump(ignored_mentionsDict, outfile, indent=4) print("Ignored locations dumped to {}{}".format( outputIgnore_dir, fileName)) except ValueError as e: print(e) print("Hence Ignoring {}".format(fileName)) continue
def landout_check(flight_reg, flight_no, af_centre, radius, landing_coords, mode, settings): # # This function determines if a flight has landed within the vicinity of the airfield take off point. # If not it sends an email to the designated address specifying the landing coordinates, these # can then be used by the recovery team to locate the aircraft using appropriate mapping technology. # The algorithm used is that of a circle of defined radius centred on the originating airfield. # The function is intended to send the msg either by email or SMS but at the moment only email # is supported. The SMS code is included, has not been tested and requires an account to be created # and of course each SMS msg would be charged to that account. The provenance of the SMS code is # included if this helps with subsequent development. # # Returns: True - landed out. False - landed inside airfield limits # print "landout_check called. Registration: ", flight_reg, " Start coords: ", af_centre, " End coords: ", landing_coords print "settings.FLOGGER_SMTP_SERVER_URL: ", settings.FLOGGER_SMTP_SERVER_URL, " settings.FLOGGER_SMTP_SERVER_PORT: ", settings.FLOGGER_SMTP_SERVER_PORT landing_dist = vincenty(af_centre, landing_coords).meters print "Landing distance is: %d metres from airfield centre" % landing_dist if landing_dist <= radius: landing_status = "landed" result = False print "Landed in airfield" else: landing_status = "landed out" result = True print "Flight landed out, send msg. Registration: ", flight_reg, " Flight No: ", flight_no # Is an email or SMS of landing status requested? if settings.FLOGGER_LANDING_EMAIL <> "Y" and settings.FLOGGER_LANDING_EMAIL <> "y": # No email or SMS of landing status required, return landing status return result # # Email or SMS of landed status to be sent # landing_point = LatLon(landing_coords[0], landing_coords[1]) # Decimal degrees to object landing_coords = landing_point.to_string( 'd% %m% %S% %H') # Coordinates to degrees minutes seconds now = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M") # Date and time of event if mode == "SMS": #----------------------------------- # Send SMS Text Message using Python # # Author : Matt Hawkins # Site : http://www.raspberrypi-spy.co.uk/ # Date : 01/04/2016 # # Requires account with TxtLocal # http://www.txtlocal.co.uk/?tlrx=114032 # #----------------------------------- # Import required libraries import urllib # URL functions import urllib2 # URL functions # Set YOUR TextLocal username username = '******' # Set YOUR unique API hash # It is available from the docs page # https://control.txtlocal.co.uk/docs/ hash = '1234567890abcdefghijklmnopqrstuvwxyz1234' # Set a sender name. # Sender name must alphanumeric and # between 3 and 11 characters in length. sender = 'RPiSpy' sender = settings.FLOGGER_YGC_ADMIN # Set flag to 1 to simulate sending # This saves your credits while you are # testing your code. # To send real message set this flag to 0 test_flag = 1 # Set the phone number you wish to send # message to. # The first 2 digits are the country code. # 44 is the country code for the UK # Multiple numbers can be specified if required # e.g. numbers = ('447xxx123456','447xxx654321') numbers = ('447xxx123456') # Define your message message = 'Test message sent from my Raspberry Pi' #----------------------------------------- # No need to edit anything below this line #----------------------------------------- values = { 'test': test_flag, 'uname': username, 'hash': hash, 'message': message, 'from': sender, 'selectednums': numbers } url = 'http://www.txtlocal.com/sendsmspost.php' postdata = urllib.urlencode(values) req = urllib2.Request(url, postdata) print 'Attempt to send SMS ...' try: response = urllib2.urlopen(req) response_url = response.geturl() if response_url == url: print 'SMS sent!' return result except urllib2.URLError, e: print 'Send failed!' print e.reason return result
# import GPS coordinates and compute distance matrix gps = pd.read_csv('./data/gps.csv', index_col='Sottoarea', dtype={ 'LATITUDINE': float, 'LONGITUDINE': float }) dm = pd.DataFrame(0.0, index=subareas, columns=subareas) for sa1 in subareas: for sa2 in subareas: if dm[sa1][sa2] == 0 and sa1 != sa2: coords_sa1 = gps.ix[sa1] coords_sa2 = gps.ix[sa2] pt1 = gp.Point(coords_sa1[0], coords_sa1[1]) pt2 = gp.Point(coords_sa2[0], coords_sa2[1]) dm[sa1][sa2] = round(gpd.vincenty(pt1, pt2).kilometers) dm[sa2][sa1] = dm[sa1][sa2] # return a clean value (0 if the prediction is negative, the rounded value if it is positive) def clean_value(x): if math.isnan(x): return x elif x < 0: return 0 return round(x) # compute the mean absolute percentage error (MAPE), corrected to avoid null denominators def mean_absolute_percentage_error(y_true, y_pred): mean_actual = np.mean(y_true)
def distance(self, d1, d2): return vincenty(d1, d2).miles
def travel_time(self, current_positon, next_position): if not current_positon or current_positon[0] is None: return distance = vincenty(current_positon, next_position).m return self.__sleep_seconds(distance)[1]
def insertiNewPoint(newpoint, edges, liste_ad): # costruisco nuovo grafo inserendo questi nuovi punti Gsol = nx.Graph() for i in xrange(len(newpoint)): Gsol.add_node(newpoint[i]) if edges[i] != None: # aggiungo i Gsol.add_node(edges[i][0]) # aggiungo j Gsol.add_node(edges[i][1]) # aggiungo arco tra i e new point Gsol.add_edge(edges[i][0], newpoint[i]) # aggiungo arco tra new point e j Gsol.add_edge(newpoint[i], edges[i][1]) nx.write_shp(Gsol, '../soli_nuovi_punti') # qui inserisco un controllo negli archi per verificarte se due nodi cadono # sullo stesso segmento. gli archi che ho sono unici, orientati: # sia che sia senso unico, sia che non lo sia, ogni punto può essere associato # ad un singolo arco.. se mi ritrovo più punti dello stesso arco, mi serve # sapere quanti appartengono allo stesso arco, chi cade prima di chi, e a che distanza.. # in base a questo, aggiorno le liste di adiacenza.. # creo lista di nodi che cadono sullo stesso segmento l_id_stess = [] l_visita = [] for i in xrange(len(newpoint)): l_visita.append(False) for i in xrange(len(newpoint)): # inserisco nuovo punto, anche se già presente fa lo stesso. # il set toglie duplicati set_visita.add(newpoint[i]) # se il cliente cade sull'incrocio, allora controllo se ci sono più clienti # per lo stesso incrocio. se ci sono devo creare la lista delle adiacenze # in entrambi i versi. So che il primo nodo corrisponde all'incrocio, gli # altri differenzieranno (eventualmente se esistono) di poco if edges[i] == None: # scorro tutti i nodi per recuperare gli indici di quelli molto simili # uno con l'altro differenziano di 0.00000000000001 # metto una tolleranza di 100 : 100 nodi sullo stesso punto della mappa # stima forse sufficiente for j in xrange(len(newpoint)): # se j diverso da i # se anche lui non ha arco # se la prima coordinata è uguale # se la seconda differenzia di poco o in positivo o negativo if j != i and edges[j] == None and newpoint[i][0] == newpoint[ j][0] and ((float(newpoint[i][1]) + 0.000000000001) > float(newpoint[j][1]) or (float(newpoint[i][1]) - 0.000000000001) < float(newpoint[j][1])): # allora crea lista adiacenze: # creo la lista delle sole adiacenze di i. In realtà essendo # punti molto vicini dovrei creare anche la lista al contrario. # in realtà però il punto j lo visiterò successivamente e lì # creerò la lista di j che punta ad i. liste_ad[newpoint[i]].append(newpoint[j]) # se invece l'arco è presente.. else: # devo trovare tutti i punti con uno stesso arco # se già visitato allora lo salto if l_visita[i] == True: continue # trovo tutti gli id dei nuovi punti che hanno stesso arco. l_id_stess.append(i) l_visita[i] = True # cerco tutti gli indici di quelli che cadono nello stesso segmento for j in xrange(len(newpoint)): if i != j and edges[i] == edges[j]: l_id_stess.append(j) l_visita[j] = True # adesso li devo ordinare per ordine di visita. lista_ordinata = [] prima_coord = edges[i][0] for j in l_id_stess: lista_ordinata.append(newpoint[j]) # ordina prima per la prima cordinata, e dopo per la seconda. # ordina però per grandezza del valore sorted(lista_ordinata) # controllo anche se l'arco è ordinato # se non lo è allora la lista ordinata di punti la inverto, così # dovrei già avere l'ordine di visita dell'altra parte if (edges[i] != sorted(edges[j])): lista_ordinata = lista_ordinata[::-1] # trovo l'indice del secondo estremo dell'arco nella lista di adiacenze # del primo estremo e lo devo togliere perchè ora deve puntare al nuovo # punto index = liste_ad[edges[i][0]].index(edges[i][1]) lista = liste_ad[edges[i][0]] del (lista[index]) # tolgo completamente la lista delle adiacenze di i e rinserisco la lista # aggiornata del (liste_ad[edges[i][0]]) # aggiungo il primo cliente liste_ad[edges[i][0]] = lista # devo scorrere per ogni nodo con stesso segmento. # parto dal primo estremo dell'arco primo = edges[i][0] for j in xrange(len(l_id_stess)): # se non è l'estremo del segmento (primo punto), # allora inizializza lista adiacenze a nulla if j != 0: liste_ad[primo] = [] # inserisco il nuovo punto nella lista di adiacenze del precedente liste_ad[primo].append(lista_ordinata[j]) primo = lista_ordinata[j] # una volta in fondo mi rimane solo l'altro capo dell'arco liste_ad[primo] = [edges[i][1]] # dopodichè guardo al contrario per verificare se ci sono nodi vicini e quindi # creare anche l'arco inverso per quei nodi che distano < 20m dal successivo lista_ordinata = lista_ordinata[::-1] primo = edges[i][1] for j in xrange(len(l_id_stess)): # se la distanza tra uno e l'altro è inferiore a 20 m # allora costruisco un arco a doppio senso d = vincenty(primo, lista_ordinata[j]).meters # se sono vicini allora anche dal mio nodo posso andare al precedente if d <= 20: liste_ad[lista_ordinata[j]].append(primo) # aggiorno primo al mio attuale nodo primo = lista_ordinata[j] l_id_stess = [] # ricostruisco il nuovo grafo dopo l'inserimento delle proiezioni dei clienti Gsol = nx.Graph() Gsol = list2graph(liste_ad, None) nx.write_shp(Gsol, '../con_clienti') return liste_ad
def update_device_location(t_obj, lock, device, sleep, process_id): last_teleport = 0 last_lat = 0 last_lon = 0 locked = False while True: try: is_boss = False LOG.debug('Running device controller task for {}'.format(device)) fort = None lock.acquire() locked = True if t_obj.is_teleport_locked(process_id): lock.release() locked = False time.sleep(5) continue forts_no_boss = t_obj.get_forts_no_boss() forts_no_raid = t_obj.get_forts_no_raid() forts_no_raid_priority = t_obj.get_forts_no_raid_priority() forts_no_raid_all = forts_no_raid + forts_no_raid_priority forts = t_obj.get_forts() if len(forts_no_boss) > 0: fort = forts_no_boss[0] elif len(forts_no_raid_priority) > 0: fort = forts_no_raid_priority[0] elif len(forts_no_raid) > 0: fort = forts_no_raid[0] if fort is not None: session = database.Session() close_fort_ids = database.get_fort_ids_within_range( session, forts, 600, fort.lat, fort.lon) session.close() locked_forts = t_obj.get_locked_forts() index = 0 for close_fort_id in close_fort_ids: if index == 6: break index += 1 if close_fort_id in [fort.id for fort in forts_no_boss]: lock_time = 60 close_forts = [ fort for fort in forts_no_boss if fort.id == close_fort_id ] close_fort = close_forts[0] forts_no_boss.remove(close_fort) elif close_fort_id in [ fort.id for fort in forts_no_raid_all ]: lock_time = 180 close_forts = [ fort for fort in forts_no_raid_all if fort.id == close_fort_id ] close_fort = close_forts[0] try: forts_no_raid.remove(close_fort) except: pass try: forts_no_raid_priority.remove(close_fort) except: pass else: continue locked_forts.append( FortTime(close_fort.id, time.time() + lock_time)) forts_fort = [ fort for fort in forts if fort.id == close_fort_id ] forts_fort[0].updated = time.time() t_obj.set_forts_no_raid(forts_no_raid) t_obj.set_forts_no_raid_priority(forts_no_raid_priority) t_obj.set_forts_no_boss(forts_no_boss) t_obj.set_locked_forts(locked_forts) t_obj.set_forts(forts) lock.release() locked = False FNULL = open(os.devnull, 'w') if fort.lat < 0: lat_str = '-- {}'.format(fort.lat) else: lat_str = str(fort.lat) if fort.lon < 0: lon_str = '-- {}'.format(fort.lon) else: lon_str = str(fort.lon) process = subprocess.Popen( 'idevicelocation -u {} {} {}'.format( device, lat_str, lon_str), shell=True, stdout=FNULL, stderr=FNULL) time_start = time.time() process.wait(5) time_end = time.time() last_tp_time = time_end - last_teleport delay = time_end - time_start last_teleport = time.time() distance = vincenty((last_lat, last_lon), (fort.lat, fort.lon)).meters last_lat = fort.lat last_lon = fort.lon LOG.info( 'Teleporting device with ID {} to {},{} over {:0.0f}m (delay: {:0.2f}s, last ago: {:0.2f}s)' .format(device, fort.lat, fort.lon, distance, delay, last_tp_time)) session = database.Session() database.add_device_location_history(session, device, time_end, fort.lat, fort.lon) session.close() else: lock.release() locked = False time_start = time.time() if is_boss: sleep_r = sleep + sleep else: sleep_r = sleep sleep_time = sleep_r - time.time() + time_start if sleep_time > 0: time.sleep(sleep_time) except KeyboardInterrupt: os.killpg(0, signal.SIGINT) sys.exit(1) except Exception as e: if locked: lock.release() LOG.error( 'Failed to update device location for device {}: {}'.format( device, e)) time.sleep(1)
def clean_location(df, user_location, place, latitude, longitude): """ This function cleans the location to grab a city and country if possible. It will first try to grab the closest location based on the latitude, longitude. If that fails, it will concatenate user_location and place, and split it into bigrams and unigrams. It will try matching bigrams and unigrams to find locations within the locations database. :param df: pd.DataFrame :param user_location: str :param place: str :param latitude: double :param longitude: double :return: cleaned_location: str """ if latitude != '' and longitude != '': geo_location = (latitude, longitude) distances = np.zeros(len(df)) for i in range(len(df)): city_geo_location = (df['Latitude'][i], df['Longitude'][i]) try: distances[i] = vincenty(geo_location, city_geo_location).kilometers except ValueError: distances[i] = 1000000 if distances[i] <= 50: return df['City'][i] + ',' + df['Country'][i] return df['City'][distances.argmin()] + ',' + df['Country'][ distances.argmin()] location = user_location + " " + place cleaned_location = '' if len(location) == 0: return cleaned_location cleaned_unigrams = unigrams(location) cleaned_bigrams = bigrams(location) for city in cleaned_bigrams: if city in set(df['City']): cleaned_location = df[df['City'] == city]['City'].to_string().split()[1] + ' ' \ + df[df['City'] == city]['City'].to_string().split()[2] + ',' \ + df[df['City'] == city]['Country'].to_string().split()[1] return cleaned_location for city in cleaned_unigrams: if city in set(df['City']): cleaned_location = df[df['City'] == city]['City'].to_string().split()[1] + ',' \ + df[df['City'] == city]['Country'].to_string().split()[1] return cleaned_location for country in cleaned_unigrams: if country in set(df['Country']): cleaned_location = "," + country for country in cleaned_bigrams: if country in set(df['Country']): cleaned_location = "," + country return cleaned_location