def test_distance_method(self): north_pole=(90,0) south_pole=(-90,0) pontianak_equator=(0,109.2) self.assertEqual(0, distance(north_pole,north_pole)) self.assertEqual(20015, int(distance(north_pole, south_pole))) # cast to an int to get rid of floating point self.assertEqual(0, distance(pontianak_equator,pontianak_equator))
def edge(start, end, mid): c = distance(start, end) b = distance(start, mid) a = distance(mid, end) if b > 0 and c > 0: degree = math.degrees(np.arccos((b**2 + c**2 - a**2) / (2 * b * c))) return degree else: return 0
def nearest_distance(origin): g = geocoder.google(origin) input = g.latlng lat1 = float(input[0]) lon1 = float(input[1]) min_distance = float("inf") smallest_index = 0 with open("store-locations.csv", "rU") as infile: reader = csv.DictReader(infile, delimiter=",") for counter, row in enumerate(reader): lat2 = float(row["Latitude"]) lon2 = float(row["Longitude"]) distance = (haversine.distance((lat1, lon1), (lat2, lon2))) if distance < min_distance: min_distance = distance smallest_index = counter address = reader[smallest_index]["Address"] city = reader[smallest_index]["City"] state = reader[smallest_index]["State"] zip_code = reader[smallest_index]["Zip Code"] ## WHOA! A read-world usage of locals()!!! How on earth did you end up using that?!?! Super cool, although at the same time totally insane ;) return "%(address)s, %(city)s, %(state)s %(zip_code)s" % locals()
def filter_breweries(self, point, km): from brewerytrip.models import Geocode filtered = [] for brewery in self.all(): if km > distance(point, Geocode.objects.coordinates(brewery.id)): filtered.append(brewery) return filtered
def evaluate_gradients(point1, point2, ascending1, ascending2, descending1, descending2): mydistance = haversine.distance([point1[1],point1[0]],[point2[1],point2[0]]); print("Gradient in ASCENDING track from point1 to point2: %f mm/yr in %f km " % (np.abs(ascending1-ascending2), mydistance) ); print("Equal to: %f mm/yr per 100 km \n" % (100*np.abs(ascending1-ascending2)/mydistance) ); print("Gradient in DESCENDING track from point1 to point2: %f mm/yr in %f km " % (np.abs(descending1-descending2), mydistance) ); print("Equal to: %f mm/yr per 100 km \n" % (100*np.abs(descending1-descending2)/mydistance) ); return;
def calc_cost_per_mile(self): """Given one airfare object, calculate the cost per mile.""" a = Port.query.filter_by(code=self.arrive).first() d = Port.query.filter_by(code=self.depart).first() return self.average_price / distance([a.lat, a.lon], [d.lat, d.lon])
def calc_cheapest_month(month, depart): """Figure out which locations to recommend to user based on their desired month of travel. Sample inputs: month = "April" depart = Port instance object """ lst = Airfare.query.filter_by(cheapest_month=month).all() # bucketing distances to diversify recommendations short_distance = set() # 0 - 999 miles; 10% medium_distance = set() # 1,000 - 4,999 miles; 60% long_distance = set() # <= 5,000 miles; 30% if lst: for item in lst: arrive = Port.query.filter_by(code=item.arrive).first() a = distance([depart.lat, depart.lon], [arrive.lat, arrive.lon]) if 1 < a <= 999: short_distance.add(arrive.code.encode('ascii', 'ignore')) elif 1000 <= a <= 4999: medium_distance.add(arrive.code.encode('ascii', 'ignore')) elif a >= 5000: long_distance.add(arrive.code.encode('ascii', 'ignore')) return sample(short_distance, 1) + sample(medium_distance, 6) + sample( long_distance, 3)
def get_distance(self): principal_point = (float(self.principal_lat), float(self.principal_long)) comp_point = (float(self.home.latitude), float(self.home.longitude)) miles = haversine.distance(principal_point, comp_point) clean_miles = "%.2f" % miles return float(clean_miles)
def get_min_distance(event, lat, lon): min_dist = sys.float_info.max for area in event['affected_areas']: for coord in area['coordinates']: dist = haversine.distance(coord['wgs84_latitude'], coord['wgs84_longitude'], lat, lon) min_dist = min(min_dist, dist) return min_dist
def latlon2xy(loni,lati,lon0,lat0): # returns the distance between a point and a reference in km. radius = haversine.distance([lat0,lon0], [lati,loni]); bearing = haversine.calculate_initial_compass_bearing((lat0, lon0),(lati, loni)) azimuth = 90 - bearing; x = radius * np.cos(np.deg2rad(azimuth)); y = radius * np.sin(np.deg2rad(azimuth)); return [x, y];
def shortestPath(currentLocation, places): nearestDistance = 20000 nearestPlace = None for place in places: placeDistance = distance(currentLocation, place) if placeDistance < nearestDistance: nearestDistance = placeDistance nearestPlace = place ## retune nearestPlace & nearestDistance return (nearestPlace, nearestDistance)
def compute_circle(myVelfield, center, radius): close_stations = [] rad_distance = [] for i in range(len(myVelfield.name)): mydist = haversine.distance([center[1], center[0]], [myVelfield.nlat[i], myVelfield.elon[i]]) if mydist <= radius: rad_distance.append(mydist) close_stations.append(myVelfield.name[i]) print("Returning %d stations that are within %f km of center %f, %f" % (len(close_stations), radius, center[0], center[1])) return close_stations, rad_distance
def getImgWithinRadius(currCoordinate, imgToGPScoordinates, Radius): """Get all the Images within the specified radius""" """By calculating the distance between the two coordinates by using haversine formula""" imgDir = [] for img, nextCoordinate in imgToGPScoordinates.items(): dist = (haversine.distance((currCoordinate), (nextCoordinate))) if dist <= Radius: # To know if the image is within the radius imgDir.append(img) return imgDir # List of all such images
def within_worlds(self): userloc = self.user["userloc"]["coordinates"] lat1, long1 = userloc[0], userloc[1] within_worlds_list = [] for world in self.active_worlds(): worldloc = world["loc"]["coordinates"] worldradius = world["radius"] lat2, long2 = worldloc[0], worldloc[1] distance = haversine.distance((lat1, long1), (lat2, long2)) if distance < worldradius: within_worlds_list.append(world) return within_worlds_list
def shortest_path(current_place,places): nearest_distance = 22000 nearest_place = None for place in places: try: place_distance = distance(current_place, place) except: place_distance = nearest_distance if place_distance < nearest_distance: nearest_distance = place_distance nearest_place = place return (nearest_place, nearest_distance)
def get_disks(load_array, disk_radius, threshold_distance, sta_lon, sta_lat): # threshold_distance is in degrees circle_lons = []; circle_lats = []; circle_radius = []; circle_pressure = []; threshold_distance = threshold_distance * (np.pi / 180) * 6370; # converting to km (6370km = 1 radian). circle_area = np.pi * disk_radius * disk_radius; # Area = pi * r^2 square_side_length = np.sqrt(circle_area); # in same units as disk_radius for i in range(len(load_array.lon)): # for each rectangular load cell if abs(load_array.lat[i]) > 85: # ignoring the arctic and antarctic continue; dist_from_center = haversine.distance([load_array.lat[i], load_array.lon[i]], [sta_lat, sta_lon]); # reported in km if dist_from_center < threshold_distance: # if the load cell is less than a certain distance from the station, discretize it. center = [load_array.lon[i], load_array.lat[i]]; load_akm = load_array.east_width[i] * 111.0 * np.cos(center[1] * np.pi / 180); load_bkm = load_array.north_width[i] * 111.0; # The placement of each disk-shaped tile within the load n_tiles_x = int(np.floor(load_akm * 2 / square_side_length)); n_tiles_y = int(np.floor( load_bkm * 2 / square_side_length)); # number of tiles that fit in x and y dimensions of rectangle x_array = []; y_array = []; x_nodes = np.linspace(center[0] - load_array.east_width[i], center[0] + load_array.east_width[i], n_tiles_x + 1, endpoint=True); y_nodes = np.linspace(center[1] - load_array.north_width[i], center[1] + load_array.north_width[i], n_tiles_y + 1, endpoint=True); for j in range(len(x_nodes) - 1): x_array.append((x_nodes[j] + x_nodes[j + 1]) * 0.5); # average of two nodes for j in range(len(y_nodes) - 1): y_array.append((y_nodes[j] + y_nodes[j + 1]) * 0.5); # average of two nodes [centerx, centery] = np.meshgrid(x_array, y_array); centerx = np.reshape(centerx, (np.size(centerx), 1)) centery = np.reshape(centery, (np.size(centery), 1)) # HERE WE RE-PACKAGE INTO CIRCULAR LOADS for j in range(np.size(centerx)): circle_lons.append(centerx[j]); circle_lats.append(centery[j]); circle_radius.append(disk_radius); circle_pressure.append(load_array.pressure[i]); CircleLoads = helper_functions.Load_Array(lon=circle_lons, lat=circle_lats, east_width=circle_radius, north_width=[], pressure=circle_pressure); return CircleLoads;
def make_queue(start, end, km, detour, max_km): if km > max_km: km = max_km queue = [end] while km > 0 and len(detour) > 0: total_distance = 0 queue.insert(0, detour[0]) detour.pop(0) point = start for var in queue: total_distance += distance(point, var[4]) point = var[4] if km < total_distance - end[2]: queue.pop(0) return queue
def nearby_stn(qcode,point): if (point[0]==None or point[1]==None) or (point[0]==0.0 and point[1]==0.0): return radius=25 #km (search within this radius) m=[] with db.opendb(db.STNDB) as cdb: cdb._exec("SELECT code,lat,lng FROM slist WHERE code!=(?)",(qcode,)) while(1): t=cdb._fetchone() if(t==None): break if(distance(point,(t['lat'],t['lng']))<=radius): m.append(t['code']) #append the nearby station code return m
def lookup(): """Calculate ten closest stations from input latitude and longitude, return data for ten closest stations as a JSON object.""" # From Google maps API: l = request.values.get("lat", 0, type=float) g = request.values.get("lng", 0, type=float) session["location"] = {"input":(l,g)} # Geohash encode the input, then determine the expanded neighborhood based on expanded geohash reference_location = geohash.encode(l, g) location_box = geohash.expand(reference_location[:3]) neighborhoods = [] for place in location_box: geohash_str = place + '%' neighbor = dbsession.query(model.Station_Geohash).\ select_from(model.Station_Geohash).\ filter(model.Station_Geohash.geohash_loc.ilike(geohash_str)).\ all() neighborhoods = neighborhoods + neighbor dist_list = [] # For all of the stations found in neighborhoods, check for data and snow. # If there is data and snow for a given station, add it to the heap for location in neighborhoods: try: station = dbsession.query(model.Station).filter(model.Station.id == location.station_id).one() snow = station.snow_data[-1] origin = float(l), float(g) destination = float(station.latitude), float(station.longitude) kms = int(distance(origin, destination)) mi = int(0.621371*kms) if snow.depth != None and snow.depth > 0: if snow.water_equiv != None and snow.water_equiv != 0: density = (int((snow.water_equiv / snow.depth) * 100)) if density > 100: density = 100 else: density = "No Data" dist_list.append({'dist':mi, 'text-code':station.id, 'id':station.given_id, 'ele':station.elevation,\ 'lat':station.latitude, 'lng':station.longitude, 'name':station.name, 'depth':snow.depth,\ 'depth_change':snow.depth_change, 'density':density, 'date':snow.date.strftime("%m/%d/%y %H:%M")}) else: continue except IndexError: continue # Return the 10 closest stations, their distances away in miles (converted from kms) # and basic telemetry data for that station closest_sta = sorted(dist_list, key=lambda k: k['dist'])[0:10] time_stamps = [x['date'] for x in closest_sta] time_stamp = max(time_stamps) response = json.dumps({"closest": closest_sta, "time_stamp":time_stamp}) return response
def nearby_stn(qcode, point): if (point[0] == None or point[1] == None) or (point[0] == 0.0 and point[1] == 0.0): return radius = 25 #km (search within this radius) m = [] with db.opendb(db.STNDB) as cdb: cdb._exec("SELECT code,lat,lng FROM slist WHERE code!=(?)", (qcode, )) while (1): t = cdb._fetchone() if (t == None): break if (distance(point, (t['lat'], t['lng'])) <= radius): m.append(t['code']) #append the nearby station code return m
def compute_circle(myVelfield, center, radius): close_stations = [] rad_distance = [] lon, lat = [], [] for station_vel in myVelfield: mydist = haversine.distance([center[1], center[0]], [station_vel.nlat, station_vel.elon]) if mydist <= radius: rad_distance.append(mydist) lon.append(station_vel.elon) lat.append(station_vel.nlat) close_stations.append(station_vel.name) print("Returning %d stations within %.3f km of %.4f, %.4f" % (len(close_stations), radius, center[0], center[1])) return close_stations, lon, lat, rad_distance
def distance_from_north_pole(values, gifts): distances = [] latitude_num = [] longitude_num = [] for index, gift_id in enumerate(values['GiftId']): current_points = (values['Latitude'][index], values['Longitude'][index]) distance_to_north_pole = distance(current_points, north_pole) print distance_to_north_pole distances.append(distance_to_north_pole) latitude_num.append(int(current_points[0])) longitude_num.append(int(current_points[1])) gifts['distance_to_np'] = distances gifts['latitude_num'] = latitude_num gifts['longitude_num'] = longitude_num
def find_close_by(areas, values, index, clusters, close_by): current_points = (values['Latitude'][index], values['Longitude'][index]) gift = { 'GiftId': values['GiftId'][index], 'Latitude': values['Latitude'][index], 'Longitude': values['Longitude'][index] } for index, area in enumerate(areas[str(close_by)]): if distance(current_points, (area[0]['Latitude'], area[0]['Longitude'])) <= close_by: area.append(gift) clusters[str(close_by)].append(index) return clusters[str(close_by)].append(len(areas)) areas[str(close_by)].append([gift])
def plan_route(current_place, places, weights, total_weight, total_distance, route): while len(places) > 0: the_place = shortest_path(current_place,places) total_weight += weights[the_place[0]] # taking into account sleight weight - 10kg so total_weight of gifts must be <= 990 if total_weight <= 990: current_place = the_place[0] route.append(current_place) places.remove(current_place) total_distance += the_place[1] else: total_distance += distance(current_place, np) current_place = np total_weight = 0 print total_distance return (route, total_distance, current_place, total_weight)
def cross_track_pos(target_lon, target_lat, nearrange_lon, nearrange_lat, heading_cartesian): # Given the heading of a plane and the coordinates of one near-range point # Get the cross-track position of point in a coordinate system centered at (nearrange_lon, nearrange_lat) with given heading distance = haversine.distance((target_lat, target_lon), (nearrange_lat, nearrange_lon)) compass_bearing = haversine.calculate_initial_compass_bearing( (nearrange_lat, nearrange_lon), (target_lat, target_lon)) # this comes as CW from north theta = bearing_to_cartesian(compass_bearing) # the angle of the position vector in cartesian coords # heading_cartesian is the angle between the east unit vector and the flight direction x0 = distance * np.cos(np.deg2rad(theta)) y0 = distance * np.sin(np.deg2rad(theta)) # in the east-north coordinate systeem x_prime, y_prime = rotate_vector_by_angle(x0, y0, heading_cartesian) return y_prime
def get_ranked_items(request): try: username = request.GET["username"] except KeyError: return HttpResponseBadRequest(error_json("username required but not provided")) try: user = User.objects.get(username=username) except User.DoesNotExist: return HttpResponseBadRequest(error_json('user "%s" not found in database'%username)) lat = request.GET.get("lat", None) lon = request.GET.get("lon", None) if lat == None and lon != None: return HttpResponseBadRequest(error_json('lon parameter provided without lat parameter')) if lat != None and lon == None: return HttpResponseBadRequest(error_json('lat parameter provided without lon parameter')) if lat != None: try: lat = float(lat) except ValueError: return HttpResponseBadRequest(error_json('lat parameter invalid')) try: lon = float(lon) except ValueError: return HttpResponseBadRequest(error_json('lon parameter invalid')) page = int(request.GET.get("page", 1)) count = int(request.GET.get("size", 50)) max_distance = float(request.GET.get("radius", 10)) #km from haversine import distance from ml import ML try: rankings = ML.get(username) except KeyError: rankings = fakedict(0) if lat != None: items = [ (rankings[x.locu_id], x) for x in MenuItem.objects.all().select_related('venue') if distance((lat,lon), (x.venue.lat, x.venue.lon)) <= max_distance and x.locu_id in rankings] else: items = [ (rankings[x.locu_id], x) for x in MenuItem.objects.all() if x.locu_id in rankings] #from random import shuffle #shuffle(items) items.sort(reverse=True) if lat != None: output = [ item_to_json_dict(i[1], distance((lat,lon), (i[1].venue.lat, i[1].venue.lon)), ranking=i[0]) for i in items[count*(page-1):count*page] ] else: output = [ item_to_json_dict(i[1], ranking=i[0]) for i in items[count*(page-1):count*page] ] return HttpResponse(json.dumps(output, indent=4))
def create_los_rdr_geo_from_ground_ann_file(ann_file, x_axis, y_axis): # Make los.rdr.geo given .ann file from JPL website's UAVSAR interferograms and the ground-range sample points. # x-axis and y-axis are the x and y arrays where los vectors will be extracted on a corresponding grid. near_angle, far_angle, heading = jpl_uav_read_write.get_nearrange_farrange_heading_angles( ann_file) heading_cartesian = bearing_to_cartesian(heading) # CCW from east print("Heading is %f degrees CW from north" % heading) print("Cartesian Heading is %f" % heading_cartesian) # Get the upper and lower left corners, so we can compute the length of the across-track extent in km ul_lon, ul_lat, ll_lon, ll_lat = jpl_uav_read_write.get_ground_range_left_corners( ann_file) cross_track_max = haversine.distance((ll_lat, ll_lon), (ul_lat, ul_lon)) # in km # Get the azimuth angle for the pixels looking up to the airplane # My own documentation says CCW from north, even though that's really strange. azimuth = heading_cartesian - 90 # 90 degrees to the right of the airplane heading (for the look vector from ground to plane) azimuth = cartesian_to_ccw_from_north(azimuth) # degrees CCW from North print("azimuth from ground to plane is:", azimuth) [X, Y] = np.meshgrid(x_axis, y_axis) (ny, nx) = np.shape(X) grid_az = azimuth * np.ones(np.shape(X)) grid_inc = np.zeros(np.shape(X)) print("Computing incidence angles for all pixels") for i in range(ny): for j in range(nx): xtp = cross_track_pos(X[i, j], Y[i, j], ll_lon, ll_lat, heading_cartesian) # THIS WILL HAVE TO CHANGE FOR ASCENDING AND DESCENDING inc = incidence_angle_trig(xtp, cross_track_max, near_angle, far_angle) grid_inc[i, j] = inc # Finally, write the 2 bands for los.rdr.geo isce_read_write.write_isce_unw(grid_inc, grid_az, nx, ny, "FLOAT", 'los.rdr.geo') return
def filtered_coordinates(self, point, list): from brewerytrip.models import Geocode from brewerytrip.models import Beer coord_list = [] for var in list: beers = Beer.objects.beer_count(var.id) if beers > 0: # remove breweries if they have 0 beers in them coord = Geocode.objects.coordinates(var.id) dis = distance(point, coord) if coord[0] >= point[0] and coord[1] >= point[1]: # Latitude dir = 'NE' elif coord[0] <= point[0] and coord[1] >= point[1]: dir = 'SE' elif coord[0] >= point[0] and coord[1] <= point[1]: # Longitude dir = 'NW' else: dir = 'SW' coord_list.append([var.id, beers, dis, dir, coord]) return coord_list
def load_graph(self): self.cur.execute( 'select n1.id, n1.lat, n1.lon, n2.id, n2.lat, n2.lon, name, roadtype from nodes as n1, nodes as n2, edges where n1.id=node1 and n2.id=node2 and n1.lon between {0} and {1} and n2.lon between {0} and {1} and n1.lat between {2} and {3} and n2.lat between {2} and {3};' .format(self.lonmin, self.lonmax, self.latmin, self.latmax)) nexttuple = self.cur.fetchone() while nexttuple is not None: dist = distance((float(nexttuple[1]), float(nexttuple[2])), (float(nexttuple[4]), float(nexttuple[5]))) self.rn.add_edge(nexttuple[0], nexttuple[3], weight=dist, name=nexttuple[6], speed_limit=self.find_speed_limit(nexttuple[7]), t=dist / self.find_speed_limit(nexttuple[7])) self.rn.node[nexttuple[0]]['lon'] = str(nexttuple[2]) self.rn.node[nexttuple[0]]['lat'] = str(nexttuple[1]) self.rn.node[nexttuple[3]]['lon'] = str(nexttuple[5]) self.rn.node[nexttuple[3]]['lat'] = str(nexttuple[4]) nexttuple = self.cur.fetchone()
def get_cumulative_distances(graph): vs_lon_lats = np.rollaxis( np.array([[ graph.vs[e.source]["lon_lat"][::-1], graph.vs[e.target]["lon_lat"][::-1] ] for e in graph.es]), 1) vs_lat_lons = np.roll(vs_lon_lats, 1, axis=-1) # exchange lon and lat del vs_lon_lats # print(np.shape(vs_lon_lats)) # raise KeyboardInterrupt("bla") dists = hav.distance(vs_lat_lons[0], vs_lat_lons[1]) # ** DIST_POWER # dists = list(map(lambda n: hav.distance([self.graph.vs[new_v]["lat"], self.graph.vs[new_v]["lon"]], [n["lat"], n["lon"]], radius=1), next_neighbors)) assert len(graph.es) == len(dists) graph.vs["cumuDist"] = 0 for e, dis in zip(graph.es, dists): graph.vs[e.source]["cumuDist"] += dis graph.vs[e.target]["cumuDist"] += dis return np.array(graph.vs["cumuDist"])
def adjust_by_reference_stations(names, coords, slope_obj): # How do we adjust the verticals for large-scale drought signatures? reference_station = 'P208' coord_box = [-123, -121, 39, 42] eq_coords = [-124.81, 40.53] radius = 250 max_radius = 350 reference_type = 'radius' # options = 'radius','box','station' new_slope_obj = [] background_slopes_before = [] background_slopes_after = [] for i in range(len(names)): if reference_type == 'station': if names[i] == reference_station: background_slopes_before.append(slope_obj[i][0]) background_slopes_after.append(slope_obj[i][1]) elif reference_type == 'box': if coords[i][0] > coord_box[0] and coords[i][0] < coord_box[1]: if coords[i][1] > coord_box[2] and coords[i][1] < coord_box[3]: background_slopes_before.append(slope_obj[i][0]) background_slopes_after.append(slope_obj[i][1]) elif reference_type == 'radius': mydistance = haversine.distance([coords[i][1], coords[i][0]], [eq_coords[1], eq_coords[0]]) if mydistance > radius and mydistance < max_radius: background_slopes_before.append(slope_obj[i][0]) background_slopes_after.append(slope_obj[i][1]) vert_reference_before = np.nanmean(background_slopes_before) vert_reference_after = np.nanmean(background_slopes_after) print("Vert slope before: %f " % vert_reference_before) print("Vert slope after: %f " % vert_reference_after) for i in range(len(slope_obj)): new_slope_obj.append([ slope_obj[i][0] - vert_reference_before, slope_obj[i][1] - vert_reference_after ]) return new_slope_obj
def get_nearest_pixel_in_raster(raster_lon, raster_lat, target_lon, target_lat): # Take a raster and find the grid location closest to the target location dist = np.zeros(np.shape(raster_lon)) lon_shape = np.shape(raster_lon) for i in range(lon_shape[0]): for j in range(lon_shape[1]): mypt = [raster_lat[i][j], raster_lon[i][j]] dist[i][j] = haversine.distance((target_lat, target_lon), mypt) minimum_distance = np.nanmin(dist) if minimum_distance < 0.25: # if we're inside the domain. idx = np.where(dist == np.nanmin(dist)) i_found = idx[0][0] j_found = idx[1][0] print(raster_lon[i_found][j_found], raster_lat[i_found][j_found]) else: i_found = -1 j_found = -1 # error codes return i_found, j_found
def compute_prem_load(station_array, load_array, disk_radius, greensfunc, max_distance): # For a given station and a given set of loads, # Compute the loading on a PREM earth structure # Max distance is in degrees total_disp_x = []; total_disp_y = []; total_disp_v = []; for i in range(len(station_array.lon)): # for each station... # Take a set of rectangular loads and tile them into disk loads. CircleLoads = get_disks(load_array, disk_radius, max_distance, station_array.lon[i], station_array.lat[i]); print("Number of circles: %d" % len(CircleLoads.lon)); disk_sum_v = 0; disk_sum_x = 0; disk_sum_y = 0; for j in range(np.size(CircleLoads.lon)): dist_from_center = haversine.distance([CircleLoads.lat[j], CircleLoads.lon[j]], [station_array.lat[i], station_array.lon[i]]); # reported in km azimuth = haversine.calculate_initial_compass_bearing((station_array.lat[i], station_array.lon[i]), (CircleLoads.lat[j], CircleLoads.lon[j])); myindex = find_closest_greens(greensfunc.km, dist_from_center); # find the nearest answer in the Green's functions vdisp = greensfunc.vload[myindex]; # in m hdisp = greensfunc.hload[myindex]; # in m [xdisp, ydisp] = az_decompose(hdisp, (station_array.lat[i], station_array.lon[i]), ( CircleLoads.lat[j], CircleLoads.lon[j])); # Decompose radial into x and y components disk_sum_v = disk_sum_v + vdisp * CircleLoads.pressure[ j] * 1000 / 9.81; # THIS SHOULD BE DIVIDED BY 9.81! the PREM code already has it. Result in mm disk_sum_x = disk_sum_x + xdisp * CircleLoads.pressure[ j] * 1000 / 9.81; # THIS SHOULD BE DIVIDED BY 9.81! the PREM code already has it. Result in mm disk_sum_y = disk_sum_y + ydisp * CircleLoads.pressure[ j] * 1000 / 9.81; # THIS SHOULD BE DIVIDED BY 9.81! the PREM code already has it. Result in mm total_disp_x.append(disk_sum_x); total_disp_y.append(disk_sum_y); total_disp_v.append(disk_sum_v); return total_disp_x, total_disp_y, total_disp_v; # should exactly match length of input station_array fields.
def drive_naive(rn, s, t, ev, battery_procent): shortest_path = nx.shortest_path(rn, s, t, 't') path = [] node = s closest_cs = None for i in range(0, len(shortest_path) - 1): node = shortest_path[i] edge = rn.edge[node][shortest_path[i + 1]] energy_used = edge['weight'] * ev.consumption_rate(edge['speed_limit']) path.append(node) if ev.curbat - energy_used < ev.battery_capacity * battery_procent: possible_cs = inRange(rn, shortest_path[i], t, ev) length = float('inf') for CS in possible_cs: lengthToCS = distance( (float(rn.node[node]['lat']), float(rn.node[node]['lon'])), (float(rn.node[CS]['lat']), float(rn.node[CS]['lon']))) if length > lengthToCS: length = lengthToCS closest_cs = CS return path, node, closest_cs ev.curbat -= energy_used return path, node, closest_cs
def recount_distances(point, area): for var in area: var[2] = distance(point, var[4]) return area
def read_data(): tree = ET.parse('map.osm') root = tree.getroot() # Get all nodes for child in root.findall('node'): lon = child.get("lon") lat = child.get("lat") nodeID = child.get("id") nodePosMap[nodeID] = [lat, lon] # Get all ways wayID = -1 name = "DEFAULT" jCount = 0 for child in root.findall('way'): wayID = -1 name = "DEFAULT" tigerName = "DEFAULT" isHighway = False roadType = "DEFAULT" isNamePresent = False uid = child.get("uid") wayID = child.get("id") for tag in child.getiterator('tag'): k = tag.get('k') if('name' == k): name = (tag.get('v')) isNamePresent = True if('highway' == k): isHighway = True roadType = tag.get('v') if('tiger:name_base' == k): tigerName = (tag.get('v')) isNamePresent = True if(isHighway == False or isNamePresent == False): continue if(name == "DEFAULT"): name = tigerName length = 0 firstNode = True; earlierPos = [0,0] segmentList = [] for nodeRef in child.getiterator('nd'): internalNodeID = nodeRef.get('ref') position = nodePosMap.get(internalNodeID, INVALID_LOC) if(nodeToWayMap.get(internalNodeID, NOT_PRESENT) == NOT_PRESENT): nodeToWayMap[internalNodeID] = wayID else: junctionMap[jCount] = (wayID, nodeToWayMap[internalNodeID], internalNodeID) jCount = jCount + 1 if firstNode == True: earlierPos = position firstNode = False else: x1 = float(earlierPos[0]) y1 = float(earlierPos[1]) x2 = float(position[0]) y2 = float(position[1]) length = length + haversine.distance([x1,y1], [x2,y2]) segmentList.append([x1,y1, x2,y2]) earlierPos = position wayMap[wayID] = (name, segmentList, length, roadType)
droid = android.Android() try: me = droid.getIntent().result[u'extras'][u'%LOC'].split(',') except: notif = "androPyTL - Error\n" notif += "GPS position missing" droid.notify("androPyTL", notif) sys.exit(1) me = [float(me[0]), float(me[1])] stations = httpJSON(URL_STATIONS) n_station_idx = 0 for idx in range(len(stations)): stations[idx]['distance'] = haversine.distance(me, [float(stations[idx]['latitude']), float(stations[idx]['longitude'])]) if stations[idx]['distance'] <= stations[n_station_idx]['distance']: n_station_idx = idx station = stations[n_station_idx] notif = station['name'] + ":\n" horaires = httpJSON(URL_HORAIRES.replace('STATION_ID', station['id'])) horaires_tri = sorted(horaires, key=lambda k: k['time_sort']) for horaire in horaires_tri: notif += horaire['line'] + ">" + horaire['destination'] + ": " + horaire['time'] + "\n" droid.notify("androPyTL", notif)
def add_geo_check(db): # Open a connection to geolocation db reader = geoip2.database.Reader('../collection/GeoLite2-City.mmdb') # Define EC2 locations if 'JP' in db: ec2_reg = 'JP' ec2_lat = 35.689506 ec2_lon = 139.6917 elif 'IE' in db: ec2_reg = 'IE' ec2_lat = 53.347778 ec2_lon = -6.259722 else: ec2_reg = 'US' ec2_lat = 39.0900 ec2_lon = -77.6400 c = 299.792458 # speed of light in vacuum - km/ms n = 1.52 # reference index of refraction for fiber v = c/n # speed of light in fiber - km/ms # Open connection to database con = sqlite3.connect(db) cur = con.cursor() cur_loop = con.cursor() # Add a new column for results try: cur.execute("ALTER TABLE http_requests ADD COLUMN in_us BOOLEAN") cur.execute("ALTER TABLE http_responses ADD COLUMN in_us BOOLEAN") cur.execute("ALTER TABLE traceroutes ADD COLUMN min_rtt REAL") cur.execute("ALTER TABLE traceroutes ADD COLUMN geo_check BOOLEAN") con.commit() except sqlite3.OperationalError: pass # calc minimum rtts for every ip in traceroute counter = 0 cur_loop.execute("SELECT DISTINCT ip, time FROM traceroutes") for ip, time in cur_loop.fetchall(): # Get lat/lon for this IP from maxmind try: response = reader.city(ip) except geoip2.errors.AddressNotFoundError: continue # Do country check if city is not available if ec2_reg == 'US' and response.country.iso_code == 'US': # Assume okay lat = ec2_lat lon = ec2_lon elif response.city.name is None and response.country.iso_code == 'US': if ec2_reg == 'US': # Assume okay lat = ec2_lat lon = ec2_lon elif ec2_reg == 'JP': # Use Portland lat = 45.52 lon = -122.681944 elif ec2_reg == 'IE': # Use NYC lat = 40.7127 lon = -74.0059 else: # Use returned lat/lon lat = response.location.latitude lon = response.location.longitude if lat == None or lon == None: continue # Time of flight as the crow flies distance = haversine.distance((ec2_lat, ec2_lon), (lat, lon)) #in km rtt = 2 * float(distance)/v passed = float(time) > rtt # Update table with results of check cur.execute("UPDATE traceroutes SET geo_check = ?, min_rtt = ?, \ country = ?, iso_code = ? WHERE ip = ? AND time = ?", (passed, rtt, response.country.name, response.country.iso_code, ip, time)) counter += 1 if counter > 0 and counter % 100 == 0: con.commit() print "[GEOIPCHECK -- Trace] - " + str(counter) + " added..." con.commit() if ec2_reg != 'US': cur.execute("UPDATE http_requests SET in_us = 1 WHERE host IN \ (SELECT DISTINCT hostname FROM traceroutes \ WHERE iso_code = 'US' AND geo_check = 1)") cur.execute("UPDATE http_responses SET in_us = 1 WHERE host IN \ (SELECT DISTINCT hostname FROM traceroutes \ WHERE iso_code = 'US' AND geo_check = 1)") cur.execute("UPDATE http_requests SET in_us = 0 WHERE host NOT IN \ (SELECT DISTINCT hostname FROM traceroutes \ WHERE iso_code = 'US' AND geo_check = 1)") cur.execute("UPDATE http_responses SET in_us = 0 WHERE host NOT IN \ (SELECT DISTINCT hostname FROM traceroutes \ WHERE iso_code = 'US' AND geo_check = 1)") else: cur.execute("UPDATE http_requests SET in_us = 0 WHERE host IN \ (SELECT DISTINCT hostname FROM traceroutes \ WHERE iso_code != 'US' AND geo_check = 1)") cur.execute("UPDATE http_responses SET in_us = 0 WHERE host IN \ (SELECT DISTINCT hostname FROM traceroutes \ WHERE iso_code != 'US' AND geo_check = 1)") cur.execute("UPDATE http_requests SET in_us = 1 WHERE host NOT IN \ (SELECT DISTINCT hostname FROM traceroutes \ WHERE iso_code != 'US' AND geo_check = 1)") cur.execute("UPDATE http_responses SET in_us = 1 WHERE host NOT IN \ (SELECT DISTINCT hostname FROM traceroutes \ WHERE iso_code != 'US' AND geo_check = 1)") con.commit() con.close()
import FOVCalc, TreeDB, GoogleDirections, StreetviewImages, haversine, TrunkRecognition coordinates = TreeDB.getCoordinates() for i in range(0,10): imageCoords = GoogleDirections.getImageCoordinates(coordinates[i]) distance = haversine.distance(coordinates[i], imageCoords) bearing = haversine.initialBearing(imageCoords,coordinates[i]) image = StreetviewImages.getImage(imageCoords,bearing,-15) # trunkWidth = TrunkRecognition.getWidthOfTrunk(image) # diameter = FOVCalc.calculateWidth(trunkWidth,600,30,distance) # print(diameter)
def isInRange(self, other): if (haversine.distance(self,other) <= 150): return True return False
def test_distance(self): seattle = (47.6097, -122.3331) paradise_rainier = (46.9153, -121.74) self.assertEqual(distance(seattle, paradise_rainier), 89.24660555660384)
def test_routePlanner(self): self.assertAlmostEqual(20015.086796, distance(northPole, southPole))
lon = (d.Decimal(list[5][0]+list[5][1]+list[5][2]) + d.Decimal(list[5][3:len(list[5])])/60) #flips lat/long if list[4].upper() == "S": lat = -1 * lat if list[6].upper() == "W": lon = -1 * lon currentPosition = lat,lon s.close() #valve is open #Compares distance to port and asks to close if too close open = True for x in listOfPorts: port = d.Decimal(x[0]),d.Decimal(x[1]) distanceToPort = haversine.distance(currentPosition,port) if distanceToPort < d.Decimal(x[2]): open = False # if distanceToPort > d.Decimal(x[2]): # open = True # print x[3],distanceToPort #opens valve if open == True and lastState != "True": print "Opening Valve" lastState = "True" valve.open_evsco_valve() #closes valve if open == False and lastState != "False": print "Closing Valve"
def greedy_star(home): try: total_list = get_list( home, 1000) # 1000km radius is maximum distance we can reach measured_list = list_distance( home, total_list ) # Count distances, directions; general list with coordinates layer, dir = layer_and_direction( measured_list ) # Get a sense of which direction and layer is the best target if layer == 0: # Area for searching. From 0 to 450 set_area = list( sorted(filter(lambda x: x[3] == dir and 0 <= x[2] < 450, measured_list), key=itemgetter(1), reverse=True)) if (len(set_area) < 50): set_area = list( sorted(filter(lambda x: 0 <= x[2] < 700, measured_list), key=itemgetter(1), reverse=True)) elif layer == 1: # Area for searching. From 0 to 700 set_area = list( sorted(filter(lambda x: x[3] == dir and 0 <= x[2] < 700, measured_list), key=itemgetter(1), reverse=True)) if (len(set_area) < 50): list( sorted(filter(lambda x: 0 <= x[2] < 900, measured_list), key=itemgetter(1), reverse=True)) else: # Area for searching. From 350 to 1000 set_area = list( sorted(filter(lambda x: x[3] == dir and 350 <= x[2] <= 1000, measured_list), key=itemgetter(1), reverse=True)) if (len(set_area) < 50): list( sorted(filter(lambda x: 100 <= x[2] <= 1000, measured_list), key=itemgetter(1), reverse=True)) if (len(set_area) > 0): max_beers = 0 home_var = [0, 0, 0, 'HOME', home] max_km = 250 max_route = [] for x in range(15, 19): # Reset copy of area area = deepcopy(set_area) km = 2000 # Fuel capacity at 2000km point = home # Starting point changed = True route = [(home_var[0], home_var[1], 0)] collected_beers = 0 # Check if you are able to travel to the next point and then be able to go home while km - distance(point, area[0][4]) - distance( area[0][4], home) >= 0 and changed: changed = False detour = detour_list(point, area[0][4], area[0][2], area, x) queue = make_queue( point, area[0], km - distance(point, area[0][4]) - distance(area[0][4], home), detour, max_km) total = 0 for var in queue: km_dist = distance(point, var[4]) total += km_dist collected_beers += var[1] area.remove(var) point = var[4] route.append((var[0], var[1], km_dist)) changed = True km -= total area = recount_distances(point, area) if km - distance(point, home) >= 0: total = 0 home_var[2] = distance(point, home) detour = detour_list(point, home, home_var[2], area, x) queue = make_queue(point, home_var, km - home_var[2], detour, km - home_var[2]) for var in queue: km_dist = distance(point, var[4]) total += km_dist collected_beers += var[1] point = var[4] route.append((var[0], var[1], km_dist)) km -= total if max_beers < collected_beers: max_beers = collected_beers max_route.clear() max_route = route print(">>>") # print("Finished with %dkm left. Collected %s. %s degree" % (km, collected_beers, x)) else: print("Error, couldn't get to home") return max_route else: return 0 except: print("Error occured at greedy_star") return 0
def shortestRoute(list): shortestRoute = map(lambda x: distance(northPole, x), list) overallDistance = reduce(lambda x,y: x + y, shortestRoute) ## return shortest & overall return (shortestRoute, overallDistance)
def main(url, save_dir): if type(url) is str: if url.endswith('.html'): url = url.replace('.html', '.xml') tds_url = 'https://opendap.oceanobservatories.org/thredds/dodsC' c = Crawl(url, select=[".*ncml"]) datasets = [os.path.join(tds_url, x.id) for x in c.datasets] elif url.endswith('.xml'): tds_url = 'https://opendap.oceanobservatories.org/thredds/dodsC' c = Crawl(url, select=[".*ncml"]) datasets = [os.path.join(tds_url, x.id) for x in c.datasets] elif url.endswith('.nc') or url.endswith('.ncml'): datasets = [url] elif os.path.exists(url): datasets = glob.glob(url + '/*.nc') else: print 'Unrecognized input. Input must be a string of the file location(s) or list of file(s)' elif type(url) is list: datasets = url data = [] for dataset in datasets: logging.info('Processing {}'.format(str(dataset))) try: print 'Opening file: {}'.format(dataset) with xr.open_dataset(dataset, mask_and_scale=False) as ds: qc_df = parse_qc(ds) qc_vars = [x for x in qc_df.keys() if not 'test' in x] qc_df = qc_df.reset_index() deployment = np.unique(ds['deployment'].data)[0] variables = ds.data_vars.keys() variables = eliminate_common_variables(variables) variables = [x for x in variables if not 'qc' in x] # remove qc variables, because we don't care about them ref_des = '{}-{}-{}'.format(ds.subsite, ds.node, ds.sensor) qc_data = request_qc_json(ref_des) # grab data from the qc database ref_des_dict = get_parameter_list(qc_data) deploy_info = get_deployment_information(qc_data, deployment) # Gap test. Get a list of gaps gap_list = test_gaps(qc_df) # Deployment Variables deploy_start = str(deploy_info['start_date']) deploy_stop = str(deploy_info['stop_date']) deploy_lon = deploy_info['longitude'] deploy_lat = deploy_info['latitude'] # Deployment Time data_start = ds.time_coverage_start data_stop = ds.time_coverage_end start_test = [str(deploy_start), str(data_start)] stop_test = [str(deploy_stop), str(data_stop)] # Deployment Distance data_lat = np.unique(ds['lat'])[0] data_lon = np.unique(ds['lon'])[0] dist_calc = distance((deploy_lat, deploy_lon), (data_lat, data_lon)) if dist_calc < .5: # if distance is less than .5 km dist = True else: dist = False dist_test = '{} [{} km]'.format(dist, dist_calc) # Unique times time = ds['time'] len_time = time.__len__() len_time_unique = np.unique(time).__len__() if len_time == len_time_unique: time_test = True else: time_test = False for v in variables: print v # Availability test if v in ref_des_dict[ds.stream]: available = True else: available = False if ds[v].dtype == np.dtype('S64') or ds[v].dtype == np.dtype('datetime64[ns]') or 'time' in v: # this will skip most engineering/system variables because they are strings # ['ref_des', 'stream', 'deployment', 'start', 'stop', 'distance_from_deploy_<=.5km', # 'time_unique', 'variable', 'availability', 'all_nans', 'global_range_test', 'min', 'max', # 'fill_test', 'fill_value', 'gaps', 'global_range', 'stuck_value', 'spike_test']) data.append((ref_des, ds.stream, deployment, start_test, stop_test, dist_test, time_test, v, available, None, None, None, None, None, None, None, None, None, None)) continue else: var_data = ds[v].data # NaN test. Make sure the parameter is not all NaNs nan_test = np.all(np.isnan(var_data)) if not nan_test or available is False: # Global range test [g_min, g_max] = get_global_ranges(ds.subsite, ds.node, ds.sensor, v) try: ind = reject_outliers(var_data, 3) min = np.nanmin(var_data[ind]) max = np.nanmax(var_data[ind]) except TypeError: min = None max = None if g_min is not None: if min >= g_min: if max <= g_max: gr_result = True else: gr_result = False else: gr_result = False else: gr_result = None # Fill Value test try: fill_value = ds[v]._FillValue fill_test = np.any(var_data == ds[v]._FillValue) except AttributeError: fill_value = 'n/a' fill_test = 'n/a' data_tuple = (ref_des, ds.stream, deployment, start_test, stop_test, dist_test, time_test, v, available, nan_test, gr_result, [g_min, min], [g_max, max], fill_test, fill_value, gap_list) if v in qc_vars: temp_list = [] tests = ['global_range_test', 'dataqc_stuckvaluetest', 'dataqc_spiketest'] for test in tests: var = '{}_{}'.format(v, test) group_var = 'group_{}'.format(var) try: qc_df[group_var] = qc_df[var].diff().cumsum().fillna(0) except KeyError as e: logging.warn('Error: P') temp_list.append('DNR') continue tdf = qc_df.groupby([group_var, var])['time'].agg(['first', 'last']) tdf = tdf.reset_index().drop([group_var], axis=1) tdf = tdf.loc[tdf[var] == False].drop(var, axis=1) tdf['first'] = tdf['first'].apply(lambda x: x.strftime('%Y-%m-%d %H:%M:%S')) tdf['last'] = tdf['last'].apply(lambda x: x.strftime('%Y-%m-%d %H:%M:%S')) if tdf.empty: temp_list.append([]) else: temp_list.append(map(list, tdf.values)) temp_tuple = data_tuple + tuple(temp_list) data.append(temp_tuple) else: temp_tuple = data_tuple + ('n/a', 'n/a', 'n/a') data.append(temp_tuple) else: data.append((ref_des, ds.stream, deployment, start_test, stop_test, dist_test, time_test, v, available, nan_test, 'n/a', 'n/a', 'n/a', 'n/a', 'n/a', gap_list, 'n/a', 'n/a', 'n/a')) except Exception as e: logging.warn('Error: Processing failed due to {}.'.format(str(e))) raise df = pd.DataFrame(data, columns=['ref_des', 'stream', 'deployment', 'start', 'stop', 'distance_from_deploy_<=.5km', 'time_unique', 'variable', 'availability', 'all_nans', 'global_range_test', 'min[global,data]', 'max[global,data]', 'fill_test', 'fill_value', 'gaps', 'global_range', 'stuck_value', 'spike_test']) df.to_csv(os.path.join(save_dir, '{}-{}-{}-{}-process_on_{}.csv'.format(ds.subsite, ds.node, ds.sensor, ds.stream, dt.now().strftime('%Y-%m-%dT%H%M00'))), index=False)
loc = slp.flatten()[slp.argmin()] xmin = x.flatten()[slp.argmin()] ymin = y.flatten()[slp.argmin()] latc = lats.flatten()[slp.argmin()] lonc = lons.flatten()[slp.argmin()] points = (ymin,xmin) geo_pts = (latc,lonc) latlocs.append(latc) lonlocs.append(lonc) xlocs.append(xmin) ylocs.append(ymin) grbs.close() indx+=1 dl+=dt.timedelta(hours=timestep) dist = np.zeros(len(latlocs)) for i in xrange(1,len(latlocs)): dist[i] = haversine.distance(lonlocs[i-1],latlocs[i-1],lonlocs[i],latlocs[i]) non = np.where(dist>1000)[0] for i in non: xlocs[i] = np.nan ylocs[i] = np.nan m.drawcoastlines(linewidth=0.8,color='lightgrey') m.plot(xlocs,ylocs,'ro-',linewidth=1.4) plt.show()