Example #1
0
def nearer_station(location, stations):
    ''' Takes a location (Point) object and a tuple of two Station objects'''
    distance1 = distance.distance((location.y, location.x), (stations[0].latitude, stations[0].longitude))
    distance2 = distance.distance((location.y, location.x), (stations[1].latitude, stations[1].longitude))
    if (distance1 < distance2):
        return stations[0]
    return stations[1]
Example #2
0
def find_similar_listings(latitude, longitude, area):
    # find similar listings for comparison (max 5)
    # need to weigh distance and area
    # should add parking to the db
    # units at exact address should be given priority
    #    print('Locating apartments within about')
    #    print(distance.distance((latitude,longitude),(latitude+0.0025,longitude))) # 0.0025 should be replaced by some statistical measure
    conn = sqlite3.connect("/home/bram/Documents/craiglist_crawler/apartments.db")
    c = conn.cursor()
    c.execute(
        "SELECT * FROM apartments WHERE latitude < ? AND latitude > ? AND longitude < ? AND longitude > ?",
        (latitude + 0.0025, latitude - 0.025, longitude + 0.0025, longitude - 0.0025),
    )
    costs = []
    links = []
    for (date, link, description, lat, long, address, available, price, a, neighbourhood) in c.fetchall():
        links.append(link)
        if a and area:
            costs.append(distance.distance((lat, long), (latitude, longitude)).meters + abs(area - a))
        else:
            costs.append(
                distance.distance((lat, long), (latitude, longitude)).meters + 200
            )  # 200 should be replaced by some statistical measure
    min_list = sorted(zip(links, costs), key=lambda t: t[1])
    return [i[0] for i in min_list[:5]]  # return top five links
Example #3
0
 def get_user_event_distance(self, user, event, is_search_eachtime=False):
     g = geocoders.GeoNames()
     user_event_distance = 0
     if user['location'] is not None and event['latitude'] is not None and event['longitude'] is not None:
             #不超过400  
         user_event_distance = 400
         event_coordinate = (event['latitude'], event['longitude'])
         if 'latitude' in user and is_search_eachtime == False:
             user_coordinate = (user['latitude'], user['longitude'])
             d = distance.distance(user_coordinate, event_coordinate).miles
             if d < user_event_distance:
                 user_event_distance = d
             else:
                 user_event_distance = user_event_distance
         else:
                 #多个距离选最近的
             results = g.geocode(user['location'], exactly_one = False)
             closest_index = 0
             for i, (_, user_coordinate) in enumerate(results):
                 d = distance.distance(user_coordinate, event_coordinate).miles
                 if d < user_event_distance:
                     user_event_distance = d
                     closest_index = i
                     self.db.user.update({'id' : user['id']}, {'$set' : {'latitude' : results[closest_index][1][0], 'longitude': results[closest_index][1][1]}})
     return user_event_distance
 def getLocationDistance(self, user, event, isSearchEachTime=False):
     g = geocoders.GeoNames()
     ueDistance = 0
     if user['location'] is not None and event['latitude'] is not None and event['longitude'] is not None:
         try:
             #if distance more than 300, consider as got an ERROR coordinate  
             ueDistance = 300
             eventCoordinate = (event['latitude'], event['longitude'])
             if 'latitude' in user and isSearchEachTime == False:
                 userCoordinate = (user['latitude'], user['longitude'])
                 d = distance.distance(userCoordinate, eventCoordinate).miles
                 ueDistance = d if d < ueDistance else ueDistance
             else:
                 #if retrieved multiple locations, choose the closest one
                 results = g.geocode(user['location'], exactly_one = False)
                 closestIndex = 0
                 for i, (_, userCoordinate) in enumerate(results):
                     d = distance.distance(userCoordinate, eventCoordinate).miles
                     if d < ueDistance:
                         ueDistance = d
                         closestIndex = i
                 self.db.user.update({'id' : user['id']}, {'$set' : {'latitude' : results[closestIndex][1][0], 
                                                                     'longitude' : results[closestIndex][1][1]}})
         except Exception as e:
             print e
     return ueDistance
Example #5
0
def nearer_station(geometry, stations):
    ''' Return the nearest Station object[s] 
        from a given Location object. '''
    distance1 = distance.distance((geometry.y, geometry.x), (stations[0].latitude, stations[0].longitude))
    distance2 = distance.distance((geometry.y, geometry.x), (stations[1].latitude, stations[1].longitude))
    if (distance1 < distance2):
        return stations[0]
    return stations[1]
Example #6
0
def loc_translate(gps_coords):
    global left_corner_of_area
    (lat, long, elev) = left_corner_of_area
    (y, x, z) = gps_coords
    
    #TODO: how does elev data come out of mesh?
    
    yCoord = round(distance((lat,0), (y,0)).m / 2.5)
    xCoord = round(distance((lat,long), (lat,x)).m / 2.5)
    zCoord = (elev - z) / 2.5
    return [xCoord, yCoord, zCoord]
def calculateDistance(latlon):
    length = len(latlon)

    # need at minimum 2 points
    if length < 2: return 0

    dist = distance.distance(latlon[0], latlon[1])
    for i in range(1, length-1):
        dist += distance.distance(latlon[i], latlon[i+1])

    return dist
Example #8
0
    def test_should_accept_iterations_constructor_kwarg(self):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            # `iterations` kwarg is a legacy from Vincenty.
            self.assertEqual(distance(132, iterations=20).km, 132)
            # `iterations` is not a valid arg of the base Distance class,
            # so it should raise a warning.
            self.assertEqual(1, len(w))

            self.assertEqual(distance(132).km, 132)
            self.assertEqual(1, len(w))
Example #9
0
def calc_distance(value, arg, unit="km"):

    if not isinstance(value, Waypoint.location):
        raise TypeError("First value is not a location")
    if not isinstance(arg, Waypoint.location):
        raise TypeError("Argument is not a location")

    if unit == "km":
        return distance(value.get_lat_long(), arg.get_lat_long()).km
    elif unit == "mi":
        return distance(value.get_lat_long(), arg.get_lat_long()).mi
Example #10
0
def base_cmp_by_proximity(current, previous, coords):
	"""Compare method for two objects with latitude and longitude in comparison to a lat/long pair."""
	from geopy import distance as dist
	
	current_distance = dist.distance(current.coords_tuple, coords).feet
	previous_distance = dist.distance(previous.coords_tuple, coords).feet
	if current_distance < previous_distance:
		return -1
	elif current_distance == previous_distance:
		return 0
	else:
		return 1
Example #11
0
def VenuesNearLocation (request):
	if request.method == 'GET':
		zipcode = request.GET.get('zipcode', None)
		lat = request.GET.get('lat', None)
		long = request.GET.get('long', None)
		radius = request.GET.get('radius', '1.0')

		log_messages = []

		if not lat or not long:
			if zipcode:
				g = geocoders.GoogleV3()
				place, (lat, long) = g.geocode(zipcode)
			else:
				response = json.dumps({'status': 'missing params', })
				return HttpResponse(response, mimetype="application/json")

		all_venues = Venue.objects.all()
		user_point = (float(lat), float(long))#Point(str(lat)+";"+str(long)) #37.228272, -80.42313630000001 (Buruss)
		log_messages.append('using radius: ' + str(radius))
		log_messages.append('user_point: ' + str(user_point))
		log_messages.append('all venues: '+ str(all_venues))

		log_messages.append('starting loop')
		nearby_venues = []
		for venue in all_venues:
			venue_point = (float(venue.latitude), float(venue.longitude))#Point(str(venue.latitude)+";"+str(venue.longitude))
			log_messages.append('first venue point: ' + str(venue_point))
			distance_between = float(distance.distance(venue_point, user_point).miles)
			log_messages.append('distance between user and venue: ' + str(distance_between))
			if float(distance.distance(venue_point, user_point).miles) < float(radius):
				log_messages.append('successfully adding')
				nearby_venues.append(venue)

		open_nearby_venues = []
		for venue in nearby_venues:
			weekday = convert_current_UTC_to_venue_local_datetime(venue).weekday()
			try:
				venue_weekday = VenueOpeningHours.objects.get(venue=venue, weekday=weekday)
			except VenueOpeningHours.DoesNotExist:
				continue
			if venue_weekday.open_hour < convert_current_UTC_to_venue_local_time(venue) < venue_weekday.close_hour\
						and not venue_weekday.closed:
				open_nearby_venues.append(venue)

		# if len(nearby_venues) == 0:
		# 	message = 'No venues near (' + str(lat) + ', ' + str(long) +') within radius of ' + str(radius) + ' given user point ' + str(user_point)
		# 	response = json.dumps({'status': str(log_messages), })
		# 	return HttpResponse(response, mimetype="application/json")

		json_serializer = serializers.get_serializer("json")()
		response = json_serializer.serialize(open_nearby_venues, ensure_ascii=False)
		return HttpResponse(response, mimetype="application/json")
Example #12
0
def get_gains(dist=70):
    """Returns lat and lon gain

    Gain is space between circles.
    """
    start = Point(*bounds.center)
    base = dist * sqrt(3)
    height = base * sqrt(3) / 2
    dis_a = distance(meters=base)
    dis_h = distance(meters=height)
    lon_gain = dis_a.destination(point=start, bearing=90).longitude
    lat_gain = dis_h.destination(point=start, bearing=0).latitude
    return abs(start.latitude - lat_gain), abs(start.longitude - lon_gain)
Example #13
0
def predicted_cost(graph, current, goal):
    """Calculates h(n) based on shortest as-the-crow-flies path

    Params:
    graph - The graph containing where a path is being planned through
    current - The node the heuristic is calculated for
    goal - The goal of the search
    """
    cattrs = graph.node_attributes(current)
    gattrs = graph.node_attributes(goal)
    return min(gdistance.distance(cattrs['start_point'], gattrs['start_point']).m,
               gdistance.distance(cattrs['start_point'], gattrs['end_point']).m,
               gdistance.distance(cattrs['end_point'], gattrs['start_point']).m,
               gdistance.distance(cattrs['end_point'], gattrs['end_point']).m)
Example #14
0
def get_scan_area():
    """Returns the square kilometers for configured scan area"""
    lat1 = config.MAP_START[0]
    lat2 = config.MAP_END[0]
    lon1 = config.MAP_START[1]
    lon2 = config.MAP_END[1]
    p1 = Point(lat1, lon1)
    p2 = Point(lat1, lon2)
    p3 = Point(lat1, lon1)
    p4 = Point(lat2, lon1)

    width = distance.distance(p1, p2).kilometers
    height = distance.distance(p3, p4).kilometers
    area = int(width * height)
    return area
Example #15
0
 def nearby_posts(cls, lat, long, dist):
     nearby_posts = []
     d = distance.distance(miles=dist)
     rough_distance = Decimal(str(units.degrees(arcminutes=d.nm) * 2))
     lat_lower = Decimal(lat) - rough_distance
     lat_upper = Decimal(lat) + rough_distance
     long_lower = Decimal(long) - rough_distance
     long_upper = Decimal(long) + rough_distance
     posts = db.session.query(Post).filter(Post.lat.between(lat_lower, lat_upper))
     posts = posts.filter(Post.long.between(long_lower, long_upper)).order_by('id desc')
     for post in posts:
         exact_distance = distance.distance((post.lat, post.long), (lat, long))
         if exact_distance.miles <= dist:
             nearby_posts.append(post)
     return nearby_posts
Example #16
0
 def assert_expected(expected):
     found = False
     for r in results['features']:
         passed = True
         properties = None
         if 'geocoding' in r['properties']:
             properties = r['properties']['geocoding']
         else:
             properties = r['properties']
         failed = properties['failed'] = []
         for key, value in expected.items():
             value = str(value)
             if not compare_values(str(properties.get(key)), value):
                 # Value is not like expected. But in the case of
                 # coordinate we need to handle the tolerance.
                 if key == 'coordinate':
                     coord = r['geometry']['coordinates']
                     lat, lon, max_deviation = map(float, value.split(","))
                     deviation = distance(
                         Point(lat, lon),
                         Point(coord[1], coord[0])
                     )
                     if int(deviation.meters) <= int(max_deviation):
                         continue  # Continue to other properties
                     failed.append('distance')
                 passed = False
                 failed.append(key)
         if passed:
             found = True
     if not found:
         raise SearchException(
             params=params,
             expected=expected,
             results=results
         )
Example #17
0
def km_from(state, lat2, long2):
    if state == "VIC":
        #port melbourne
        from_coord = (-37.823786, 144.928443)
    if state == "NSW":
        #erskine park
        from_coord = (-33.81955, 150.80243)
    if state == "ACT":
        #hume
        from_coord = (-35.392175,149.162993)
    if state == "QLD":
        #Richlands
        from_coord = (-27.5796767, 152.9514717)
    if state == "WA":
        #kewdale
        from_coord = (-31.9817, 115.952682)
    if state == "SA":
        #wingfield
        from_coord = (-34.839407,138.574849)
    if state == "NT":
        #WINNELLIE
        from_coord = (-12.429465,130.87759)
    if state == "TAS":
        #hobart
        from_coord = (-42.8763329, 147.3259806)
    return distance.distance(from_coord, (lat2, long2)).km    
Example #18
0
def traverseTree(root, locationVal, nearEvents):
    '''
    Binary tree traversal
    '''
    key = root.keys()
    # Terminal Condition --> reach the leaf
    tag = False
    if type(root[key[0]][0]) == type(0) or type(root[key[0]][0]) == type(0.0):
        for key in root.keys():
            if distance.distance([locationVal[1], locationVal[0]], [root[key][1], root[key][0]]).miles <= mileThreshold:
                nearEvents.append(key)
                tag = True
            '''# Test
            if math.sqrt((locationVal[0]-root[key][0])**2 + (locationVal[1]-root[key][1])**2) <= mileThreshold:
                nearEvents.append(key)
                tag = True'''
        return tag

    if len(key) != 1:
        print 'Not tree structure.'
        sys.exit(1)

    key = root.keys()[0]
    tag_left = traverseTree(root[key][0], locationVal, nearEvents)
    tag_right = traverseTree(root[key][1], locationVal, nearEvents)
    if tag_left or tag_right:
        return True
    return False
Example #19
0
    def _get_distance(self):
        from haystack.utils.geo import Distance

        if self._distance is None:
            # We didn't get it from the backend & we haven't tried calculating
            # it yet. Check if geopy is available to do it the "slow" way
            # (even though slow meant 100 distance calculations in 0.004 seconds
            # in my testing).
            if geopy_distance is None:
                raise SpatialError("The backend doesn't have 'DISTANCE_AVAILABLE' enabled & the 'geopy' library could not be imported, so distance information is not available.")

            if not self._point_of_origin:
                raise SpatialError("The original point is not available.")

            if not hasattr(self, self._point_of_origin['field']):
                raise SpatialError("The field '%s' was not included in search results, so the distance could not be calculated." % self._point_of_origin['field'])

            po_lng, po_lat = self._point_of_origin['point'].get_coords()
            location_field = getattr(self, self._point_of_origin['field'])

            if location_field is None:
                return None

            lf_lng, lf_lat  = location_field.get_coords()
            self._distance = Distance(km=geopy_distance.distance((po_lat, po_lng), (lf_lat, lf_lng)).km)

        # We've either already calculated it or the backend returned it, so
        # let's use that.
        return self._distance
Example #20
0
def compute_error_dist():
    for ip, loc in pl_loc_map2.iteritems():
	loc2=pl_loc_map.get(ip,'N/A')
	if loc2 == 'N/A':
	  continue
	err_dist=(distance.distance(loc,loc2).miles)*1.60934
	print ip +"\t"+str(err_dist)+"\t"+loc[0]+"\t"+loc[1]+"\t"+loc2[0]+"\t"+loc2[1];
Example #21
0
def smart_map_point(s):
  '''Map point s to closets point on route conscious of nextStopID'''
  #Calculate range of indexes of points to check
  half = int(len(points) / 3)
  lat, lng, nextStopID = s
  print(s)
  destination_index = stops[nextStopID]
  if (destination_index - half) >= 0: #chuck to check is in higher half
    check = [i for i in range(destination_index - half, destination_index)]
  else: #chunk to check dips below 0
    check = [i for i in range(0, destination_index)]
    delta = -(destination_index - half)
    check.extend( [i for i in range(len(points) - delta, len(points))])
  check.extend([i % len(points) for i in range(destination_index,
    destination_index + 3)])

  #Actual search
  running_min = float("inf")
  point_index = None #index of closest point on map
  for i in check:
    dist = distance(points[i], s).miles
    if dist < running_min:
      running_min = dist
      point_index = i
  return point_index
Example #22
0
def create_markers(track):
    marker_spacing = calculate_marker_spacing(track)

    last_pos = None
    d = 0.0
    last_km_counter = -1
    count = 0
    info_points = {}
    positions = track.positions
    last_info_point = positions[0]

    for p in positions:
        if last_pos != None:
            d = d + distance.distance((p.latitude, p.longitude), (last_pos.latitude, last_pos.longitude)).kilometers

        current_kilometer = int(d / marker_spacing) * marker_spacing
        if current_kilometer > last_km_counter:
            info_points[count] = {
                "distance": current_kilometer,
                "total_time": str(p.time - positions[0].time),
                "pace": "%s %s"
                % (
                    track.activity.format_speed(
                        current_kilometer - last_km_counter, (p.time - last_info_point.time).seconds
                    ),
                    track.activity.get_speed_format_display(),
                ),
            }
            last_info_point = p
            last_km_counter = current_kilometer

        last_pos = p
        count = count + 1

    return info_points
Example #23
0
def create_graph(input_path):
  # Create new graph
  G = nx.DiGraph()
  # To grab points from WKT linestring
  geopoint_re = re.compile("(-?\d+.\d+) (-?\d+.\d+)")
  # Open input csv file
  with open(input_path) as csvfile:
    reader = csv.DictReader(csvfile)
    for i, row in enumerate(reader):
      if(i%10000==0):
        print i
      wkt_string = row['WKT']
      bidirectional = (row['DIR_CODE'] == 'B')
      forward = bidirectional or (row['DIR_CODE'] == 'F')
      points = re.findall(geopoint_re, wkt_string)
      # If r.e. found some points in string
      if points:
          line = [geopy.Point(float(point[1]), float(point[0])) for point in points]
      # If its a reverse line, reverse points in line list
      if (not forward):
        line.reverse()
      # Calc dist between line nodes, and add as weights to edges
      for i in range(len(line)-1):
        dist = distance.distance(line[i], line[i+1]).m
        G.add_edge(str(line[i]), str(line[i+1]), weight=dist)
        # IF bidirectional line, add reversed pair
        if bidirectional:
          G.add_edge(str(line[i+1]), str(line[i]), weight=dist)
  return G
Example #24
0
def searchNearestNeighbor(locationVal, kdTree, searchPath, depth, nearEvents):
    dim = depth%dimNum
    #print kdTree
    key = kdTree.keys()
    # Terminal Condition --> reach the leaf
    #print type(kdTree[key[0]][0])
    if type(kdTree[key[0]][0]) == type(0) or type(kdTree[key[0]][0]) == type(0.0):
        for key in kdTree.keys():
            if distance.distance([locationVal[1], locationVal[0]], [kdTree[key][1], kdTree[key][0]]).miles <= mileThreshold:
                nearEvents.append(key)
            '''# Test
            if math.sqrt((locationVal[0]-kdTree[key][0])**2 + (locationVal[1]-kdTree[key][1])**2) <= mileThreshold:
                nearEvents.append(key)'''
        return

    # Tree's interior node should meet the tree structure
    if len(key) != 1:
        print 'Not tree structure.'
        sys.exit(1)

    key = key[0]
    # Iterative Search
    depth += 1
    if locationVal[dim] < key:
        searchPath.append(kdTree[key][1])  # Note: store the reverse search direction of point.
        searchNearestNeighbor(locationVal, kdTree[key][0], searchPath, depth, nearEvents)
    else:
        searchPath.append(kdTree[key][0])
        searchNearestNeighbor(locationVal, kdTree[key][1], searchPath, depth, nearEvents)
Example #25
0
def get_relevant_producers(sensors, lat, lng, radius, max):
    relevant_producers = []
    current = 0
    centre = Point(lat, lng)

    now = datetime.datetime.now()

    for producer in producers.values():

        if ((now - producer.access_time).total_seconds() > constants.HEARTBEAT_RATE_OUTDATED):
            print "Removing producer " + producer.producer_id
            print "Removing broker " + str(producer.broker_pid)

            os.kill(producer.broker_pid, signal.SIGKILL) # kill broker associated with the producer
            producers.pop(producer.producer_id)
            if (enablePickle): 
                pickle.dump(producers, open( "producers.p", "wb" ))
            continue

        if (current < max):
            prod_location = Point(producer.lat, producer.lng)
            distance = geopy_distance.distance(centre, prod_location).miles
            if set(sensors).issubset(set(producer.sensors)) and distance <= radius:
                relevant_producers.append(producer)
                current += 1
        else:
            break

    return relevant_producers
Example #26
0
def calc_distances_to_stations(start,town):
	"""Calculates distance as the crow flies from starting point to all other station postcodes in statute miles."""
	from geopy import geocoders
	from geopy import distance

	#get town lat, long:
	g=geocoders.GoogleV3()
	place,(start_lat,start_long)=g.geocode(town) 

	#read in station_postcodes_definitive.csv
	postcodes_data=pd.read_csv('station_postcodes_definitive.csv')
	# add column with just first half of postcode
	postcodes_data['postcode1'] = postcodes_data['postcode'].apply(lambda x: x.split(' ')[0])


	#read in uk-postcodes.csv
	lat_long_data = pd.read_csv('uk-postcodes.csv')
	#drop unncessary x,y columns. 
	lat_long_data = lat_long_data.drop(['x','y'], 1)

	#combine distance and price data into one data frame
	dist_data = pd.merge(postcodes_data, lat_long_data, on='postcode1', how='left')
	dist_data = dist_data.drop('postcode1',1)

	#calculate the distance between the start and the destination station for each destination
	dist_data['distance_miles'] = dist_data.apply(lambda x: distance.distance((start_lat,start_long),(x['lat'],x['long'])).miles, axis=1)
	#as a sanity check, remove values that are more than 700 miles away. too far for UK railway station
	dist_data['distance_miles'] = dist_data.apply(lambda x: x['distance_miles'] if x['distance_miles'] < 700. else float('NaN'), axis=1)

	#write to file
	file_name = 'distances_from_'+start+'.csv'
	dist_data.to_csv(file_name, sep=',')

	return dist_data
Example #27
0
def findFoodTrucksbyLocation(latitude, longitude, radius, limit):
    """
            Function to find the list of foodtrucks(max = "limit")
            within a circle of radius = "radius" from ("latitude", 
    """
    latitude = Decimal(latitude)
    longitude = Decimal(longitude)
    radius = Decimal(radius)
    limit = int(limit)

    # calculate a bounding box using radius to retrieve trucks within approx.
    # distance
    lat_offset = Decimal(geopy.units.degrees(
        arcminutes=geopy.units.nautical(miles=float(radius))))
    lon_offset = Decimal(geopy.units.degrees(arcminutes=geopy.units.nautical(
        miles=float(radius) / math.cos(int(latitude)))))

    north = latitude + lat_offset
    south = latitude - lat_offset
    east = longitude - lon_offset
    west = longitude + lon_offset

    # convert queryset to list so we can add a distance field
    foodtrucks = list(Foodtruck.objects.filter(latitude__range=(
        south, north)).filter(longitude__range=(east, west)))

    # remove trucks that are too far
    for truck in list(foodtrucks):
        dist = distance((truck.latitude, truck.longitude),
                        (float(latitude), float(longitude))).miles
        if dist > radius:
            foodtrucks.remove(truck)
        else:
            truck.distance = dist
    return foodtrucks[:limit]
Example #28
0
def near_stations(location):
    ''' finds the closet station from an arbitrary point'''
    distances = []
    stations = transform(Station.objects.all())
    for station in stations:
        distances.append(distance.distance((location.geometry.y, location.geometry.x), (station.latitude, station.longitude)))
    return distances    
Example #29
0
def near(latitude, longitude, radius):
    db = get_db()

    # get list of songs
    songs = db.find_songs({})
    songs = [i for i in songs]

    print "LOCATION:", latitude, longitude

    # loop through every song
    results = []
    for song in songs:
        # loop through "total" array
        for user in song.get("total", []):
            x = str(latitude) + "," + str(longitude)
            y = (
                str(user["location"]["latitude"])
                + ","
                + str(user["location"]["longitude"])
            )

            if len(x) > 1 and len(y) > 1:  # 1 because of comma
                p1 = Point(x)
                p2 = Point(y)

                # check to see if song is within radius
                dist = distance.distance(p1, p2).kilometers
                if dist <= float(radius):
                    song.pop("_id")
                    results.append(song)
                    break

    return jsonify({"songs": results})
def nearby_restaurants():
    user_loc = [float(request.args.get('lat')), \
                float(request.args.get('lon'))]
    establishments = g.db.establishments.find({
        'location': {
            '$near': user_loc
        }
    }, limit=10)

    establishments = [dict(item) for item in establishments]

    for e in establishments:
        e['dist'] = distance.distance(user_loc, e['location']).miles
        inspections = g.db.inspections.find(
            spec={'id_establishments': e['id']},
            sort=[
                ('date.year', -1),
                ('date.month', -1),
                ('date.day', -1)
            ],
            limit=1
        )
        if inspections.count() > 0:
            e['latest_inspection'] = inspections.next()

    establishments.sort(key=lambda e: e['dist'])

    return json.dumps(establishments, default=json_util.default)
Example #31
0
 def flight_distance(self, flight_id: str):
     departure_airport = airports.latlon[self.departure_airport[flight_id]]
     arrival_airport = airports.latlon[self.arrival_airport[flight_id]]
     return distance(departure_airport, arrival_airport).m
Example #32
0
            alt_ff.append(alt_f)

            # lat_teste_ff.append(lat_teste_f)
            # lon_teste_ff.append(lon_teste_f)

    distances_pp = []
    distances_pp_f = []

    for j in range(len(lon_rz) - 1):

        # Defining cordinates of two points to messure distance
        coordinates0 = (lat_rz[j], lon_rz[j])
        coordinates1 = (lat_rz[j + 1], lon_rz[j + 1])

        # Calculating haversine distance between two points in nautical miles
        distance_pp = float(distance(coordinates0, coordinates1).nm)

        # Storing calculated point to point distances into a vector
        distances_pp.append(distance_pp)

        # Sum of point to point distances to obtain total distance of a flight
        distance_real = sum(distances_pp)

print(distance_real)

lat_ff = np.asarray(lat_ff)
lon_ff = np.asarray(lon_ff)

#PLOT JFK INTL AIRPORT
# Plot station positions and names into the map
# again we have to compute the projection of our lon/lat values
Example #33
0
def getDistance(tuples):
    tot_dist = 0.0
    for i in range(len(tuples) - 1):
        tot_dist += distance(tuples[i], tuples[i + 1]).km
    return tot_dist
Example #34
0
    def plan(self, r_col, r_mod3, dist_min):

        job = get_current_job()
        print('Starting task')

        all_info = self.plan_file.copy()
        all_info['COPCI_NEAREST_DIST'] = 0
        all_info['CORSI_NEAREST_DIST'] = 0
        all_info['AREA_TYPE'] = all_info.apply(
            lambda x: self.map2poly(x, all_info), axis=1)
        all_info['AREA_TYPE'] = all_info['AREA_TYPE'].fillna("border")
        all_info['AREA_TYPE'] = all_info['AREA_TYPE'].apply(
            lambda x: x.lower())

        # split plan file to df of existing cells and cells to plan
        cell2plan = all_info[all_info['PCI'] == -1].reset_index(drop=True)
        lte_cell_info = all_info[all_info['PCI'] != -1].reset_index(drop=True)

        for i in range(cell2plan.shape[0]):

            # calculate distance
            lat = cell2plan.iloc[i]['LAT']
            lng = cell2plan.iloc[i]['LNG']
            direction = cell2plan.iloc[i]['DIRECTION']

            # determine pci&rsi range
            if cell2plan.iloc[i]['AREA_TYPE'].lower() == 'inner':
                if cell2plan.iloc[i]['SITE_TYPE'].lower() == 'macro':
                    pci_range = self.pci_inner_macro
                    rsi_range = self.rsi_inner_macro
                elif cell2plan.iloc[i]['SITE_TYPE'].lower() == 'micro':
                    pci_range = self.pci_inner_micro
                    rsi_range = self.rsi_inner_micro
                else:
                    pci_range = self.pci_inner_pico
                    rsi_range = self.rsi_inner_pico
            elif cell2plan.iloc[i]['AREA_TYPE'].lower() == 'outer':
                if cell2plan.iloc[i]['SITE_TYPE'].lower() == 'macro':
                    pci_range = self.pci_outer_macro
                    rsi_range = self.rsi_outer_macro
                elif cell2plan.iloc[i]['SITE_TYPE'].lower() == 'micro':
                    pci_range = self.pci_outer_micro
                    rsi_range = self.rsi_outer_micro
                else:
                    pci_range = self.pci_outer_pico
                    rsi_range = self.rsi_outer_pico
            else:
                pci_range = self.pci_border
                rsi_range = self.rsi_border

            # convert cell lat lng to plane coordinate
            utm_proj = Proj(init="epsg:32647", proj='utm')
            x, y = utm_proj(lng, lat)

            # define cell center
            x_cov = x + r_mod3 * np.sin(direction / 180 * np.pi)
            y_cov = y + r_mod3 * np.cos(direction / 180 * np.pi)
            lng_cov, lat_cov = utm_proj(x_cov, y_cov, inverse=True)

            # calculate distance between cell and the other cells
            lte_cell_info['DISTANCE'] = lte_cell_info.apply(
                lambda col: distance.distance((col['LAT'], col['LNG']),
                                              (lat, lng)),
                axis=1)
            lte_cell_info['DISTANCE'] = lte_cell_info['DISTANCE'].apply(
                lambda x: x.km)

            # filter only cells within the collision free zone
            lte_cell_filtered_1 = lte_cell_info[
                lte_cell_info['DISTANCE'] < r_col / 1000]

            # create df for PCI reused
            pci_dict = dict(lte_cell_filtered_1['PCI'].value_counts())

            pci_lst = [
                pci_dict[x] if x in pci_dict.keys() else 0 for x in pci_range
            ]

            pci_df = pd.DataFrame(pci_lst, index=pci_range)
            pci_df.reset_index(inplace=True)
            pci_df.columns = ['PCI', 'COUNT']
            pci_df['MOD3_GROUP'] = pci_df['PCI'].apply(lambda x: int(x % 3))

            # create df for RSI reused
            rsi_dict = dict(lte_cell_filtered_1[lte_cell_filtered_1['RSI'] %
                                                6 == 0]['RSI'].value_counts())

            rsi_lst = [
                rsi_dict[x] if x in rsi_dict.keys() else 0 for x in rsi_range
            ]

            rsi_df = pd.DataFrame(rsi_lst, index=rsi_range)
            rsi_df.reset_index(inplace=True)
            rsi_df.columns = ['RSI', 'COUNT']

            # INSERT CONDITION FOR A CELL WITH THE SPECIFIED MOD3 GROUP HERE!!!
            if not pd.isna(cell2plan.iloc[i]['SPECIFIED_MOD3_GROUP']):
                least_ol_mod3_g = int(
                    cell2plan.iloc[i]['SPECIFIED_MOD3_GROUP'])
            else:
                # filter only cells within the mod3 conflict zone
                lte_cell_filtered_2 = lte_cell_filtered_1[
                    lte_cell_filtered_1['DISTANCE'] < 3 * r_mod3 / 1000]

                if lte_cell_filtered_2.shape[0] > 0:
                    # calculate cell center to cell center distance
                    lte_cell_filtered_2['X'], lte_cell_filtered_2[
                        'Y'] = utm_proj(np.array(lte_cell_filtered_2['LNG']),
                                        np.array(lte_cell_filtered_2['LAT']))
                    lte_cell_filtered_2['DIRECTION'] = lte_cell_filtered_2[
                        'DIRECTION'].apply(lambda x: float(x))
                    lte_cell_filtered_2['X_COV'] = lte_cell_filtered_2.apply(
                        lambda col: (col['X'] + r_mod3 * np.sin(col[
                            'DIRECTION'] / 180 * np.pi))
                        if (col['DIRECTION'] != -1) else col['X'],
                        axis=1)
                    lte_cell_filtered_2['Y_COV'] = lte_cell_filtered_2.apply(
                        lambda col: (col['Y'] + r_mod3 * np.cos(col[
                            'DIRECTION'] / 180 * np.pi))
                        if (col['DIRECTION'] != -1) else col['Y'],
                        axis=1)
                    lte_cell_filtered_2['LNG_COV'], lte_cell_filtered_2[
                        'LAT_COV'] = utm_proj(
                            np.array(lte_cell_filtered_2['X_COV']),
                            np.array(lte_cell_filtered_2['Y_COV']),
                            inverse=True)
                    lte_cell_filtered_2.drop(['X', 'Y', 'X_COV', 'Y_COV'],
                                             axis=1,
                                             inplace=True)
                    lte_cell_filtered_2[
                        'CELL2CELL_DIST'] = lte_cell_filtered_2.apply(
                            lambda col: distance.distance(
                                (col['LAT_COV'], col['LNG_COV']),
                                (lat_cov, lng_cov)),
                            axis=1)
                    lte_cell_filtered_2[
                        'CELL2CELL_DIST'] = lte_cell_filtered_2[
                            'CELL2CELL_DIST'].apply(lambda x: x.km)

                    # calculate the overlapped area of each PCImod3 grp
                    lte_cell_filtered_2['OL_AREA'] = lte_cell_filtered_2.apply(
                        lambda x: self.circle_ol_area(x, r_mod3), axis=1)
                    lte_cell_filtered_2['MOD3_GROUP'] = lte_cell_filtered_2[
                        'PCI'].apply(lambda x: int(x % 3))

                    # find the PCI group that fits
                    mod3_g_ol = lte_cell_filtered_2.groupby(
                        'MOD3_GROUP').sum()['OL_AREA']
                    ol_arr = np.zeros(3, )
                    for j in range(3):
                        if j in mod3_g_ol.index:
                            ol_arr[j] = mod3_g_ol[j]

                    least_ol_mod3_g = ol_arr.argmin()
                else:
                    least_ol_mod3_g = -1

            # find the PCI with min reused and largest nearest reused distance
            if least_ol_mod3_g != -1:
                # in case where threre is overlapped area
                pci_cnt_sorted = np.sort(pci_df[
                    pci_df['MOD3_GROUP'] == least_ol_mod3_g]['COUNT'].unique())
                for j in range(len(pci_cnt_sorted)):
                    pci_min_reused = pci_cnt_sorted[j]
                    potential_pci = pci_df[
                        (pci_df['MOD3_GROUP'] == least_ol_mod3_g)
                        & (pci_df['COUNT'] == pci_min_reused)]
                    if pci_min_reused == 0:
                        chosen_pci = potential_pci['PCI'].min()
                        best_nearest_pci_dist = r_col / 1000
                        break
                    else:
                        pci_nearest_dist = []
                        for index, row in potential_pci.iterrows():
                            pci_nearest_dist.append(lte_cell_filtered_1[
                                lte_cell_filtered_1['PCI'] == row['PCI']]
                                                    ['DISTANCE'].min())

                        potential_pci['NEAREST_DIST'] = pci_nearest_dist
                        best_nearest_pci_dist = potential_pci[
                            'NEAREST_DIST'].max()
                        if best_nearest_pci_dist > dist_min / 1000:
                            chosen_pci = potential_pci[
                                potential_pci['NEAREST_DIST'] ==
                                best_nearest_pci_dist]['PCI']
                            break
                        else:
                            continue
                else:
                    chosen_pci = np.int64(999)
            else:
                # in case where there is no overlapped area
                pci_cnt_sorted = np.sort(pci_df['COUNT'].unique())
                for j in range(len(pci_cnt_sorted)):
                    pci_min_reused = pci_cnt_sorted[j]
                    potential_pci = pci_df[pci_df['COUNT'] == pci_min_reused]
                    if pci_min_reused == 0:
                        chosen_pci = potential_pci['PCI'].min()
                        best_nearest_pci_dist = r_col / 1000
                        break
                    else:

                        pci_nearest_dist = []
                        for index, row in potential_pci.iterrows():
                            pci_nearest_dist.append(lte_cell_filtered_1[
                                lte_cell_filtered_1['PCI'] == row['PCI']]
                                                    ['DISTANCE'].min())

                        potential_pci['NEAREST_DIST'] = pci_nearest_dist
                        best_nearest_pci_dist = potential_pci[
                            'NEAREST_DIST'].max()
                        if best_nearest_pci_dist > dist_min / 1000:
                            chosen_pci = potential_pci[
                                potential_pci['NEAREST_DIST'] ==
                                best_nearest_pci_dist]['PCI']
                            break
                        else:
                            continue
                else:
                    chosen_pci = np.int64(999)

            if isinstance(chosen_pci, np.int64):
                cell2plan['PCI'].iloc[i] = chosen_pci
            else:
                cell2plan['PCI'].iloc[i] = chosen_pci.iloc[0]

            cell2plan['COPCI_NEAREST_DIST'].iloc[i] = best_nearest_pci_dist

            # find the RSI with min reused and largest nearest reused distance
            rsi_cnt_sorted = np.sort(rsi_df['COUNT'].unique())
            for j in range(len(rsi_cnt_sorted)):

                rsi_min_reused = rsi_cnt_sorted[j]
                potential_rsi = rsi_df[rsi_df['COUNT'] == rsi_min_reused]
                if rsi_min_reused == 0:
                    chosen_rsi = potential_rsi['RSI'].min()
                    best_nearest_rsi_dist = r_col / 1000
                    break
                else:
                    rsi_nearest_dist = []
                    for index, row in potential_rsi.iterrows():
                        rsi_nearest_dist.append(
                            lte_cell_filtered_1[lte_cell_filtered_1['RSI'] ==
                                                row['RSI']]['DISTANCE'].min())
                    potential_rsi['NEAREST_DIST'] = rsi_nearest_dist
                    best_nearest_rsi_dist = potential_rsi['NEAREST_DIST'].max()
                    if best_nearest_rsi_dist > dist_min / 1000:
                        chosen_rsi = potential_rsi[
                            potential_rsi['NEAREST_DIST'] ==
                            best_nearest_rsi_dist]['RSI']
                        break
                    else:
                        continue
            else:
                chosen_rsi = np.int64(999)

            if isinstance(chosen_rsi, np.int64):
                cell2plan['RSI'].iloc[i] = chosen_rsi
            else:
                cell2plan['RSI'].iloc[i] = chosen_rsi.iloc[0]

            cell2plan['CORSI_NEAREST_DIST'].iloc[i] = best_nearest_rsi_dist

            # drop DISTANCE col from cell info df
            lte_cell_info.drop('DISTANCE', axis=1, inplace=True)

            # write each planned cell to cell info
            lte_cell_info = lte_cell_info.append(cell2plan.iloc[i],
                                                 ignore_index=True)

            job.meta['progress'] = 100.0 * i / cell2plan.shape[0]
            job.save_meta()

        cell2plan['PLAN_DATE'] = pd.datetime.today().date()
        file_name = f'output/plan_result_4g_{datetime.datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d-%H-%M-%S")}.xlsx'
        cell2plan.to_excel(file_name, index=False)
        job.meta['progress'] = 100.0
        job.meta['file_name'] = file_name
        job.save_meta()
Example #35
0
                (local_temperature['year'] == y)
                & (local_temperature['month'] == m)]
            ym_precipitation = local_precipitation[
                (local_precipitation['year'] == y)
                & (local_precipitation['month'] == m)]

            # Distance
            center_position = prefecture[prefecture['pref_id'] == preid]
            center_position = (center_position['latitude'].iloc[0],
                               center_position['longtitude'].iloc[0])

            ym_temperature_distance = np.zeros(ym_temperature.shape[0])
            for rows in range(ym_temperature.shape[0]):
                sta_position = (ym_temperature['latitude'].iloc[rows],
                                ym_temperature['longtitude'].iloc[rows])
                ym_temperature_distance[rows] = geodist.distance(
                    center_position, sta_position).km
            #ym_temperature['D']=ym_temperature_distance

            # Weighted
            sum_dessq = np.sum((1 / ym_temperature_distance)**2)
            ym_temperature_weight = (1 /
                                     ym_temperature_distance**2) / sum_dessq

            #Create new data

            new_data[new_rows][0] = preid
            new_data[new_rows][1] = y
            new_data[new_rows][2] = m
            for w in range(ym_temperature.shape[0]):
                new_data[new_rows][
                    3] = new_data[new_rows][3] + ym_temperature_weight[
Example #36
0
iterator = 0
for i in range(0, len(file3[2])):
    x = lat3_2[i]
    y = lng3_2[i]
    if LAT_LOW <= x <= LAT_UP and LNG_LOW <= y <= LNG_UP:
        pp.create_bus(net,
                      index=id3_2[i],
                      vn_kv=10.,
                      name='bus' + str(id3_2[i]),
                      geodata=(x, y),
                      type='b')
        for start_bus in net.trafo['lv_bus']:
            it = id3_1.index(start_bus)
            (start_x, start_y) = (lat3_1[it], lng3_1[it])
            geodata = [(start_x, start_y), (x, y)]
            length = gd.distance(geodata[0], geodata[1]).km
            pp.create_line(net,
                           from_bus=start_bus,
                           to_bus=id3_2[i],
                           name='line' + str(iterator),
                           length_km=length,
                           std_type='149-AL1/24-ST1A 110.0',
                           geodata=geodata)
            pp.create_switch(net,
                             bus=start_bus,
                             element=id3_2[i],
                             et="b",
                             closed=False,
                             type="CB",
                             name="switch" + str(i))
            #           pp.create_load()
Example #37
0
 def distance(self):
     if self.from_ and self.to:
         return distance(self.from_.location()[1], self.to.location()[1])
     return 0
Example #38
0
    c[c == 8] = '#FF9100'
    c[c == 9] = '#FF0000'
    c[c == 10] = '#D20000'
    c = pd.Series(c.iloc[:, 0])
    return c


#Retrieve GeoJSON from USGS (All, Past 7 Days)
url = 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson'
df = pd.read_json(url, typ='table')
#Keep only features
df = json_normalize(df.features)
anc = (61.216667, -149.9)  #Anchorage Latitude, Longitude
#Calculate distance to each quake
latlon = df['geometry.coordinates'].apply(lambda x: (x[1], x[0]))
dist = latlon.apply(lambda x: distance.distance(anc, x).miles)
df.insert(0, 'dist', dist)
df = df.where(df.dist <= 100)  #Plot only quakes within 100 miles
df = df.dropna(how='all')
#Convert to local time
df['properties.time'] = df['properties.time'] + df['properties.tz'] * 60 * 1000
df['properties.time'] = pd.to_datetime(df['properties.time'], unit='ms')
#Count total
tot = str(df.shape[0])
#Plot
fig = plt.figure()
fig.set_size_inches(16 * .75, 9 * .75)
ax = fig.add_subplot(111)
c = colors(df['properties.mmi'])
c = c.ravel()
ax.scatter(df['properties.time'].values,
Example #39
0
 def distance(self, lat, lon):
     coordsPtA = (self.lat, self.lon)
     coordsPtB = (lat,lon)
     return distance.distance(coordsPtA, coordsPtB).km
Example #40
0
def lambda_handler(event, context):

    try:

        riderType = event['pathParameters']["riderType"]
        riderId = event['pathParameters']["riderId"]

        body = json.loads(event['body'])

        if(riderType=="riders"):
            pk = "PS#" + riderId
            riderTypeId = "passengerId"
            locationText = "currentLocation"
            locationN = str(body["currentLocation"]["N"])
            locationW = str(body["currentLocation"]["W"])
            timestampType = "lastActive"
        elif(riderType == "drivers"):
            pk = "DR#" + riderId
            riderTypeId = "driverId"
            locationText = "updatedLocation"
            locationN = str(body["updatedLocation"]["N"])
            locationW = str(body["updatedLocation"]["W"])
            timestampType = "createdAt"
        
        locationId = str(uuid.uuid4())
        now = datetime.now() + timedelta(hours=8)
        timestamp = now.strftime("%Y-%m-%dT%H-%M-%S+8000")

        response = client.update_item(
            TableName = dbName,
            Key = {
                "PK": {
                    "S": pk
                },
                "SK": {
                    "S": "#PROFILE#" + riderId
                }
            },
            UpdateExpression="SET #loc=:loc, #riderTypeId=:riderId, locationId=:locationId, #ts=:ts",
            ExpressionAttributeNames={
                "#loc": "location",
                "#riderTypeId": riderTypeId,
                "#ts": "timestamp"
            },
            ExpressionAttributeValues={
                ":loc": {
                    "M": {
                        "Longitude" : {
                            "S" : locationN
                        },
                        "Latitude" : {
                            "S" : locationW
                        }
                    }
                },
                ":riderId": {
                    "S": riderId
                },
                ":locationId": {
                    "S": locationId
                },
                ":ts": {
                    "S": timestamp
                }
            }
        )

        if(riderType == "drivers"):
            stateUpdateResponse = client.query(
                TableName = dbName,
                IndexName = dbIndex,
                KeyConditionExpression = "driverIdKey= :pk",
                ExpressionAttributeValues= {
                    ":pk": {
                        "S": pk
                    }
                },
                Limit=1
            )

            stateDate = stateUpdateResponse["Items"][0]["stateDate"]["S"]
            state = stateDate.split("#")[0]
            date = stateDate.split("#")[1]
            pk = stateUpdateResponse["Items"][0]["PK"]["S"]
            sk = stateUpdateResponse["Items"][0]["SK"]["S"]

            if(state == "accepted"):
                bookingLocation = stateUpdateResponse["Items"][0]["bookingLocation"]["M"]
                bookingLocationN = bookingLocation["Longitude"]["S"]
                bookingLocationW = bookingLocation["Latitude"]["S"]
                dist = distance((locationN, locationW), (bookingLocationN, bookingLocationW)).m
                if(dist <= 20.0):
                    inProgressResponse = client.update_item(
                        TableName = dbName,
                        Key = {
                            "PK": {
                                "S": pk
                            },
                            "SK": {
                                "S": sk
                            }
                        },
                        UpdateExpression="SET #state=:state, stateDate=:stateDate",
                        ExpressionAttributeNames={
                            "#state": "state"
                        },
                        ExpressionAttributeValues={
                            ":state": {
                                "S": "in_progress"
                            },
                            ":stateDate": {
                                "S": "in_progress#"+date
                            }
                        }
                    )
                    
            if(state == "in_progress"):
                targetLocation = stateUpdateResponse["Items"][0]["targetLocation"]["M"]
                targetLocationN = targetLocation["Longitude"]["S"]
                targetLocationW = targetLocation["Latitude"]["S"]
                dist = distance((locationN, locationW), (targetLocationN, targetLocationW)).m
                if(dist <= 20.0):
                    completeSuccessResponse = client.update_item(
                        TableName = dbName,
                        Key = {
                            "PK": {
                                "S": pk
                            },
                            "SK": {
                                "S": sk
                            }
                        },
                        UpdateExpression="SET #state=:state REMOVE stateDate",
                        ExpressionAttributeNames={
                            "#state": "state"
                        },
                        ExpressionAttributeValues={
                            ":state": {
                                "S": "complete_success"
                            }
                        }
                    )

        return {
            "statusCode": 200,
            "body": json.dumps({
                "locationId": locationId,
                locationText : {
                    "N": locationN,
                    "W": locationW
                },
                timestampType: timestamp
            })
        }

    except:
        return {
            "statusCode": 400,
            "body": "Bad request"
        }
Example #41
0
    def read_nodes(self,
                   nodes: overpy.Node,
                   cumulative_distances: bool = True) -> dict:
        """
        Method for reading overpy.Node nodes and generating a
        TCXFile/GPXFile like dictionary of objects.\n
        Args:
            nodes (list):
                list of overpy.Node objects
            cumulative_distances (bool):
                If set to True, distance equals previous point
                distance + distance between the nodes,
                else tells actual distance between two points.

        Returns: dictionary of nodes.
            {
                'activity_type': str,
                'positions': [...],
                'altitudes': [...],
                'distances': [...],
                'total_distance': float
            }
        """
        activity_type = 'Overpy nodes'

        positions = []
        altitudes = []
        distances = []
        total_distance = None

        nodes = list(map(self.__map_payload, nodes))
        elevation_identification = ElevationIdentification(
            open_elevation_api=self.open_elevation_api, positions=nodes)
        altitudes = elevation_identification.fetch_elevation_data(
            payload_formatting=False)

        node: overpy.node
        prevNode = nodes[0]

        for i in range(len(nodes)):
            node = nodes[i]
            positions.append((node['latitude'], node['longitude']))
            if i != 0:
                flat_distance = distance.distance(
                    (node['latitude'], node['longitude']),
                    (prevNode['latitude'], prevNode['longitude']),
                ).meters
                euclidean_distance = math.sqrt(flat_distance**2 +
                                               abs(altitudes[i] -
                                                   altitudes[i - 1])**2)
                if (cumulative_distances):
                    distances.append(euclidean_distance + distances[-1])
                else:
                    distances.append(euclidean_distance)
            else:
                distances.append(0)
            prevNode = node
        try:
            if cumulative_distances:
                total_distance = distances[-1]
            else:
                total_distance = sum(distances)
        except BaseException:
            total_distance = None

        interpreted_way = {
            'activity_type': activity_type,
            'positions': positions,
            'altitudes': altitudes,
            'distances': distances,
            'total_distance': total_distance,
        }

        return interpreted_way
Example #42
0
def geosearch(request):
    """
    Returns geocoded results in MERCATOR projection
    First tries coordinates, then a series of geocoding engines
    """
    from geopy import distance, geocoders
    from geopy.point import Point
    import json
    if request.method != 'GET':
        return HttpResponse('Invalid http method; use GET', status=405)

    try:
        txt = str(request.GET['search'])
    except:
        return HttpResponseBadRequest()

    searchtype = lat = lon = None
    place = txt
    try:
        p = Point(txt)
        lat, lon, altitude = p
        searchtype = 'coordinates'
    except:
        pass  # not a point

    centerloc = Point("45.54 N 120.64 W")
    max_dist = 315  # should be everything in WA and Oregon

    searches = [
        # geocoders.GeoNames(),
        # geocoders.OpenMapQuest(),
        geocoders.Nominatim(),
        # geocoders.Yahoo(app_id=settings.APP_NAME),
        # geocoders.Bing(api_key=settings.BING_API_KEY),
        # these are tried in reverse order, fastest first
        # TODO thread them and try them as they come in.
    ]

    while not (searchtype and lat and lon):  # try a geocoder
        try:
            g = searches.pop()
        except IndexError:
            break  # no more search engines left to try

        try:
            for p, loc in g.geocode(txt, exactly_one=False):
                d = distance.distance(loc, centerloc).miles
                if d < max_dist:
                    # TODO maybe compile these and return the closest to map center?
                    # print g, p, loc
                    place = p
                    lat = loc[0]
                    lon = loc[1]
                    max_dist = d
                else:
                    pass
            searchtype = g.__class__.__name__
        except:
            pass

    if searchtype and lat and lon:  # we have a winner
        from django.contrib.gis.geos import GEOSGeometry
        cntr = GEOSGeometry('SRID=4326;POINT(%f %f)' % (lon, lat))
        cntr.transform(settings.GEOMETRY_DB_SRID)
        cntrbuf = cntr.buffer(settings.POINT_BUFFER)
        extent = cntrbuf.extent
        loc = {
            'status': 'ok',
            'search': txt,
            'place': place,
            'type': searchtype,
            'extent': extent,
            'latlon': [lat, lon],
            'center': (cntr[0], cntr[1]),
        }
        json_loc = json.dumps(loc)
        return HttpResponse(json_loc,
                            content_type='application/json',
                            status=200)
    else:
        loc = {
            'status': 'failed',
            'search': txt,
            'type': None,
            'extent': None,
            'center': None,
        }
        json_loc = json.dumps(loc)
        return HttpResponse(json_loc,
                            content_type='application/json',
                            status=404)
Example #43
0
print("")

# 3. Get Distance

print("The distance of the city(3 decimal place):")
print("%15s|" % (''), end='')
for i in range(len(locationList)):
    print("%15s|" % (locationList[i].name), end='')
print('')

distanceCountry = [[0] * len(locationList) for i in range(len(locationList))]

for i in range(len(locationList)):
    print("%-15s|" % locationList[i].name, end='')
    for j in range(len(locationList)):
        distanceCountry[i][j] = distance.distance(
            locationList[i].coordinate, locationList[j].coordinate).km
        print("%12.3f km|" % distance.distance(locationList[i].coordinate,
                                               locationList[j].coordinate).km,
              end='')
    print('')


# function of travel salesman person using Held-Karp algorithm
def TSP(location):
    n = len(location)

    # initial value of 0 to all other points
    A = {}
    for i, cost in enumerate(
            location[0]
        [1:]):  # w[0][1:] = take length start from [0][1 afterward]
Example #44
0
def choose():
    execute('DROP TABLE IF EXISTS `entity-geocode`')
    for entityRecord in [row['rowid'] for row in select('rowid FROM `scraped`')]:
        d = select('''
  `latitude-geocode`, `longitude-geocode`
FROM `branch_address`
JOIN `geocode` ON (
  `branch_address`.`address-input` = `geocode`.`address-input`
) WHERE (
  `address-column` = "province-country" AND
  `service` = "nominatim" AND
  `entityRecord` = ?
)''', entityRecord)
        if 0 == len(d):
            # No province
            d2 = select('''
  `entityRecord`, `latitude-geocode`, `longitude-geocode`
FROM `branch_address`
JOIN `geocode` ON (
  `branch_address`.`address-input` = `geocode`.`address-input`
) WHERE (
  `address-column` = "full-address" AND
  `service` = "google" AND
  `entityRecord` = ?
)''', entityRecord)
            if len(d2) == 0:
                continue
            row = d2[0]

            if row['latitude-geocode'] == None:
                kilometers_from_country = None
            else:
                kilometers_from_country = distance.distance(
                    (row['latitude-geocode'], row['longitude-geocode']),
                    COUNTRY_COORDS
                ).km

            if kilometers_from_country < MAX_KM_FROM_COUNTRY:
                best_address = {
                    "entityRecord": row['entityRecord'],
                    "service": "google",
                    "address-column": "full-address"
                }
            else:
                best_address = {
                    "entityRecord": row['entityRecord'],
                    "service": None,
                    "address-column": None
                }

        else:
            # Yes province

            # This should be pretty accurate, but not that precise.
            province_coords = (d[0]['latitude-geocode'], d[0]['longitude-geocode'])

            best_address = {
                "entityRecord": entityRecord,
                "service": "nominatim",
                "address-column": "province-country"
            }
            # Ordered from worst to best.
            for address_type, service in [
                ('postcode-country', 'nominatim'),
                ('postcode-country', 'google'),
                ('postcode-country', 'geonames'),
                ('full-address', 'google'),
                ('town-country', 'nominatim'),
                ('town-country', 'google'),
                ('town-country', 'geonames'),
                ('town-province-country', 'nominatim'),
                ('town-province-country', 'google'),
                ('town-province-country', 'geonames')
            ]:
                d2 = select('''
  `entityRecord`, `address-column`, `service`, `latitude-geocode`, `longitude-geocode`
FROM `branch_address`
JOIN `geocode` ON
  `branch_address`.`address-input` = `geocode`.`address-input`
WHERE (
  `address-column` = ? AND
  `service` = ? AND
  `entityRecord` = ?
)''', [address_type, service, entityRecord])
                if len(d2) == 0:
                    continue
                row = d2[0]
    
                if row['latitude-geocode'] == None:
                    kilometers_from_province = None
                else:
                    kilometers_from_province = distance.distance(
                        (row['latitude-geocode'], row['longitude-geocode']),
                        province_coords
                    ).km
    
                if kilometers_from_province < MAX_KM_FROM_PROVINCE:
                    best_address = {
                        "entityRecord": row['entityRecord'],
                        "service": service,
                        "address-column": address_type
                    }

        save(['entityRecord'], best_address, 'branch_best-address', verbose = False)
Example #45
0
def get_range(min, max):
    # normalize the points to the same axis value
    print(min[1], max[1])
    dist = distance.distance(min, max).km
    return dist
Example #46
0
def get_distance(lat, lng):
    init_date = datetime.datetime(2020, 4, 22)
    current_date = datetime.datetime.today()

    euc = partial(euclidean, lat, lng)

    while current_date > init_date:
        # current_date = current_date - datetime.timedelta(days=1)

        try:
            url = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/{}"

            current_date_format = current_date.strftime("%m-%d-%Y")

            filename = "{}.csv".format(current_date_format)
            if not os.path.exists("files"):
                os.mkdir("files")
            filename_to_saved = "files/" + filename

            if not os.path.exists(filename_to_saved):
                current_location = (lat, lng)
                url = url.format(filename)

                r = requests.head(url)

                if r.status_code == 200:

                    filename, headers = urllib.request.urlretrieve(
                        url, filename=filename_to_saved)

                    mycsv = csv.reader(open(filename))
                    dct = {}
                    mycsv = list(mycsv)[1:]
                    for lines in mycsv:
                        if lines[0] is not "FIPS" and lines[1] is not "Admin2":
                            dct[lines[11]] = gd.distance(
                                current_location,
                                (lines[5], lines[6])).kilometers

                    # distance = []
                    # for k in dct.keys():
                    #     if len(dct[k][0]):
                    #         d = euc(float(dct[k][0]), float(dct[k][0]))
                    #         distance.append((k, d))
                    #
                    distance = sorted(dct.items(), key=lambda x: x[1])
                    nearest_distance = distance[0][1]
                    return nearest_distance
                else:
                    current_date = current_date - datetime.timedelta(days=1)
            else:
                current_location = (lat, lng)
                mycsv = csv.reader(open(filename_to_saved))
                dct = {}
                mycsv = list(mycsv)[1:]
                for lines in mycsv:
                    if lines[0] is not "FIPS" and lines[1] is not "Admin2":
                        if len(lines[5]) and len(lines[6]):
                            dct[lines[11]] = gd.distance(
                                current_location,
                                (lines[5], lines[6])).kilometers

                # distance = []
                # for k in dct.keys():
                #     if len(dct[k][0]):
                #         d = euc(float(dct[k][0]), float(dct[k][0]))
                #         distance.append((k, d))
                #
                distance = sorted(dct.items(), key=lambda x: x[1])
                nearest_distance = distance[0][1]
                return nearest_distance
        except Exception as e:
            print(e)
            return 0.0
    return 0.0
def illustrator(n: int):
    '''funkcja rysująca jedynie położenie samolotu i punktu zrzutu narfa i wody. Przyjmowany argument to ilość punktów
       jakie ma odczytać z pliku'''
    target, bound, point, f, bcg = bcg_gen()

    merge = Image.new("RGBA", (900, 900))
    draw = ImageDraw.Draw(merge)

    prev_cords = (0, 0)  # anti fuckup
    new_dest2 = (0, 0)  # anti fuckup
    main_cords = []
    cords = []  # potrzebne do wyliczania funkcji liniowej
    nerf_drop = []
    water_drop = []
    for i in range(0, n):  # wczytuje n linijek z pliku
        prev_dest = new_dest2  # zapamietaj poprzednia iteracje
        new_cord = get_cord(f)  # pobierz koordynate z pliku (f)
        new_dest2 = CalcPoint(bound, new_cord[1],
                              new_cord[0])  # przeksztalc koordynate na punkt
        main_cords.append(new_cord)
        cords.append(new_dest2)

    area_instance = StrefaZrzutu()
    '''Tutaj ważne jest by prędkość wiatru była wynikiem odejmowania prędkośći samolotu z pixa i prędkości z gps'''
    for j in range(len(cords) - 2):
        _a, _b = f_liniowa_coeff(cords[j], cords[j + 1])
        nerf_drop_cord = area_instance.wspolrzedne_zrzutu(xs=cords[j + 1][0],
                                                          ys=cords[j + 1][1],
                                                          vs=30,
                                                          hs=30,
                                                          a=_a,
                                                          vw=5,
                                                          B=0,
                                                          m=0.132,
                                                          s=0.01935,
                                                          cords=cords,
                                                          j=j)
        nerf_drop.append(nerf_drop_cord)

        water_drop_cord = area_instance.wspolrzedne_zrzutu(xs=cords[j + 1][0],
                                                           ys=cords[j + 1][1],
                                                           vs=30,
                                                           hs=30,
                                                           a=_a,
                                                           vw=5,
                                                           B=0,
                                                           m=0.52,
                                                           s=0.01175,
                                                           cords=cords,
                                                           j=j)
        water_drop.append(water_drop_cord)

        water = open_clap(target[0], target[1], water_drop_cord[0],
                          water_drop_cord[1], 15)
        nerf = open_clap(target[0], target[1], nerf_drop_cord[0],
                         nerf_drop_cord[1], 15)

        if water is True and nerf is True:
            print('Otwieram klapę')

    #merge.alpha_composite(plane.rotate( CalcAngle(new_dest2,target) ),dest=tuple(i-25 for i in new_dest2))  # rysuje samolot obrocony o wyliczony kat miedzy dwoma punktami
    for k in range(len(water_drop)):
        draw_circle(cords[k], 2, "white", draw)  # narysuj kolko
        draw_circle(water_drop[k], 2, "red", draw)  # wskaz miejsce zrzutu wody
        draw_circle(nerf_drop[k], 2, "yellow",
                    draw)  # wskaz miejsce zrzutu nerfa
        draw.line(target + cords[k], fill=20)  # narysuj linie ()
    #print(drop[k], cords[k], k, main_cords[k])

    # merge.alpha_composite(plane.rotate(CalcAngle(new_dest2,target) ),dest=tuple(i-25 for i in new_dest2))  # rysuje samolot obrocony o kat
    # draw.line(target+new_dest2,fill=60)     # rysuje linie z targetu do samolotu
    draw.text((10, 10),
              "ANGLE: " + str(CalcAngle(new_dest2, target)),
              fill=(255, 255, 0))
    draw.text(
        (10, 25),
        "DISTANCE: " +
        str(distance.distance((point['lat'], point['lon']), new_cord).meters),
        fill=(255, 255, 0))

    #merge.alpha_composite(draw)
    front = merge.save("static/imagetestx2D.png")  # zapisz wartstwy do pliku
    return front
Example #48
0
def main(file):
    """
    Runs the main program.
    :param file: the file
    :return: stops, left_turn, right_turn
    """
    GPGGA_data, GPRMC_data = set_gps_data(file)
    GPSData = format_gps_data(GPRMC_data, GPGGA_data)

    pd.set_option('display.max_columns', 20)
    change_in_direction = GPSData["angle"].values
    change_in_direction = np.array(change_in_direction).astype(float)  # convert elements to float
    change_in_direction[1:] = change_in_direction[1:] - change_in_direction[:-1]
    for idx, direction in enumerate(change_in_direction):
        # loops through the angle column to eliminate very large directional changes
        # because signs cause huge direction changes when crossing an axis
        # (i.e from 1 to 359 is only 2 degrees in practice)
        diff = direction % 360
        result = diff if diff < 180 else 360 - diff
        sign = 1 if (0 <= direction <= 180) or (-180 >= direction >= -360) else -1
        change_in_direction[idx] = result * sign
    change_in_direction[0] = change_in_direction[1]
    change_in_direction[-1] = change_in_direction[-2]
    GPSData["angle difference"] = change_in_direction
    left_turns = GPSData[np.logical_and(GPSData["angle difference"] < -5, GPSData["speed"] > 3)]
    left_turns = left_turns[np.logical_and(left_turns["angle difference"] > -8, left_turns["speed"] < 25)]
    right_turns = GPSData[np.logical_and(GPSData["angle difference"] > 5, GPSData["speed"] > 3)]
    right_turns = right_turns[np.logical_and(right_turns["angle difference"] < 8, right_turns["speed"] < 25)]
    right_turn_list = []
    points_to_delete = set()
    for row in right_turns.iterrows():  # there's probably a more efficient way to convert the column to a list
        right_turn_list.append([row[1][2], row[1][1]])
    for idx in range(len(right_turn_list)):
        for j in range(idx + 1, len(right_turn_list)):
            dist = distance.distance(right_turn_list[idx], right_turn_list[j]).m
            if dist < 8:  # multiple consecutive points where the car is not moving are removed
                points_to_delete.add(j)
    right_turn_list = [right_turn_list[i] for i in range(len(right_turn_list)) if i not in points_to_delete]

    points_to_delete = set()
    left_turn_list = []
    for row in left_turns.iterrows():
        left_turn_list.append([row[1][2], row[1][1]])
    for idx in range(len(left_turn_list)):
        for j in range(idx + 1, len(left_turn_list)):
            dist = distance.distance(left_turn_list[idx], left_turn_list[j]).m
            if dist < 8:  # multiple consecutive points where the car is not moving are removed
                points_to_delete.add(j)
    left_turn_list = [left_turn_list[i] for i in range(len(left_turn_list)) if i not in points_to_delete]

    GPSData["speedavg"] = GPSData["speed"].rolling(10, center=True).mean()
    stops = GPSData[np.logical_and(GPSData["speed"] > 9, GPSData["speed"] < 12)]
    stops = stops[np.logical_and(stops["speedavg"] > 9, stops["speedavg"] < 12)]
    stopping_points = []
    for row in stops.iterrows():
        stopping_points.append([row[1][2], row[1][1]])
    points_to_delete = set()
    for idx in range(len(stopping_points)):
        for j in range(idx + 1, len(stopping_points)):
            dist = distance.distance(stopping_points[idx], stopping_points[j]).m
            if dist < 15:  # multiple consecutive points where the car is not moving are removed
                points_to_delete.add(j)
    new_stopping_list = [stopping_points[i] for i in range(len(stopping_points)) if i not in points_to_delete]
    coordinates = ""
    for row in GPSData.iterrows():
        if pd.notnull(row[1][0]):
            coordinates += f"{row[1][2]},{row[1][1]},0.0\n"

    return new_stopping_list, left_turn_list, right_turn_list
Example #49
0
    def add_to_data_list(self, navigate_data):
        self.DistanceFromLastPoint = 0

        if navigate_data is None:
            return

        if not self.check_date_time(navigate_data):
            return

        if len(navigate_data.PDOP) == 0 or len(navigate_data.HDOP) == 0 or len(navigate_data.VDOP) == 0:
            return

        if navigate_data.LatitudeValue == 0 or navigate_data.LongitudeValue == 0:
            return

        if self.LastNavigateData is not None:
            last_point = (self.LastNavigateData.LatitudeValue, self.LastNavigateData.LongitudeValue)
            current_point = (navigate_data.LatitudeValue, navigate_data.LongitudeValue)
            self.DistanceFromLastPoint = distance.distance(last_point, current_point).m

        self.TotalDistance += self.DistanceFromLastPoint

        try:
            self.PDOPList.append(navigate_data.PDOP)
            self.HDOPList.append(navigate_data.HDOP)
            self.VDOPList.append(navigate_data.VDOP)
            self.LatitudeList.append(navigate_data.LatitudeValue)
            self.LongitudeList.append(navigate_data.LongitudeValue)
            self.AltitudeList.append(float(navigate_data.Altitude))
        except ValueError as e:
            print("ValueError:", e)
            return

        # (55, 168, 218), (187, 249, 112), (255, 255, 0), (113, 130, 36), (113, 174, 38), (255, 255, 255)
        value = int(navigate_data.FixQuality)
        if value == 0:
            self.FixQuality_0 = self.FixQuality_0 + 1;
            self.MarkColor = (0.5, 0.5, 0.5)  # remark = "Invalid"
        elif value == 1:
            self.FixQuality_1 = self.FixQuality_1 + 1;
            self.MarkColor = (0.22, 0.67, 0.872)  # remark = "SPS"
        elif value == 2:
            self.FixQuality_2 = self.FixQuality_2 + 1;
            self.MarkColor = (0.733, 0.976, 0.439)  # remark = "Differential"
        elif value == 3:
            self.FixQuality_3 = self.FixQuality_3 + 1;
            self.MarkColor = (1.0, 1.0, 0)  # remark = "PPS"
        elif value == 4:
            self.FixQuality_4 = self.FixQuality_4 + 1;
            self.MarkColor = (0.443, 0.509, 0.141)  # remark = "RTK Fixed"
        elif value == 5:
            self.FixQuality_5 = self.FixQuality_5 + 1;
            self.MarkColor = (0.443, 0.682, 0.149)  # remark = "RTK Float"
        elif value == 6:
            self.FixQuality_6 = self.FixQuality_6 + 1;
            self.MarkColor = (1.0, 1.0, 1.0)  # remark = "Estimated"

        self.ColorList.append(self.MarkColor)

        self.LastNavigateData = navigate_data

        self.write_to_file(navigate_data)