def fill_empty_adjacent_ids(test, Selected_station_configuration):
    test.loc[test['up_station_id'].isnull(), 'up_station_id'] = test['down_station_id'].shift(1)
    test.loc[test['down_station_id'].isnull(), 'down_station_id'] = test['up_station_id'].shift(-1)

    # ffill and backfill station ids
    test['up_station_id_ffill'] = test['up_station_id'].fillna(method='ffill', axis=0)
    test['up_station_id_backfill'] = test['up_station_id'].fillna(method='backfill', axis=0)

    test.loc[test['up_station_id_ffill'].isnull(), 'up_station_id_ffill'] = test['up_station_id_backfill']
    test.loc[test['up_station_id_backfill'].isnull(), 'up_station_id_backfill'] = test['up_station_id_ffill']

    # check the distance between segment start with ffill id, backfill id.
    test = test.join(Selected_station_configuration[['station_id', 'longitude', 'latitude']].set_index('station_id'), on='up_station_id_ffill', rsuffix='_ffill')
    test = test.join(Selected_station_configuration[['station_id', 'longitude', 'latitude']].set_index('station_id'), on='up_station_id_backfill', rsuffix='_backfill')

    test['up_distance_ffill'] = distance(test, 'StartLong', 'StartLat', 'longitude', 'latitude')
    test['up_distance_backfill'] = distance(test, 'StartLong', 'StartLat', 'longitude_backfill', 'latitude_backfill')

    # fill the up_station_id and down_station_id based on the condition of up_distance_ffill and up_distance_backfill
    test['up_station_id'] = np.where(test['up_distance_ffill'] <= test['up_distance_backfill'], test['up_station_id_ffill'], test['up_station_id_backfill'])
    test.loc[test['down_station_id'].isnull(), 'down_station_id'] = test['up_station_id'].shift(-1)
    test.loc[test['down_station_id'].isnull(), 'down_station_id'] = test['up_station_id']

    # exclude additional variables
    test = test.drop(['up_station_id_ffill', 'up_station_id_backfill', 'longitude', 'latitude', 'longitude_backfill', 'latitude_backfill', 'up_distance_ffill', 'up_distance_backfill'], axis=1)
    test = test.join(Selected_station_configuration[['station_id', 'id7']].set_index('station_id'), on='up_station_id', rsuffix='_up')
    test = test.join(Selected_station_configuration[['station_id', 'id7']].set_index('station_id'), on='down_station_id', rsuffix='_down')

    # identify the segment type, internal (0) or external (1)
    test['internal_external'] = np.where(test['Tmc'].str.contains('|'.join(['N', 'P'])), 0, 1)

    return test
Пример #2
0
def getNearest(loc, data, n):
    if (n == 'all'):
        result = []
        for k, v in iter(data.items()):
            result.append(distance(loc, (v['location']['lat'], v['location']['lng'])))
        #print('Returning ' + str(result))
        return result

    h = []
    if (0):
        #print('dists')
        #startTime = time.time()
        dists = []
        for k, v in iter(data.items()):
            dists.append(distance(loc, (v['location']['lat'], v['location']['lng'])))
        #print('elapsedTime = ' + str(time.time() - startTime))

        #print('heap')
        #startTime = time.time()
        for d in dists:
            #heapq.heappush(h, distance(loc, (v['location']['lat'], v['location']['lng'])))
            heapq.heappush(h, d)
        #print('elapsedTime = ' + str(time.time() - startTime))
    else:
        for k, v in iter(data.items()):
            heapq.heappush(h, distance(loc, (v['location']['lat'], v['location']['lng'])))

    return [heapq.heappop(h) for i in range(n)]
Пример #3
0
def __lies_between(A, B, C):
    def distance(A, B):
        return math.sqrt((A[0] - B[0])**2 + (A[1] - B[1])**2)

    a = distance(B, C)
    b = distance(C, A)
    c = distance(A, B)
    return a**2 + b**2 >= c**2 and a**2 + c**2 >= b**2
Пример #4
0
def search(text, long, lat, page):
    lst_dict = []
    add = normal(text)
    lst_text = split_tag(add)
    for i in coll.find():
        tag = str(i['store_name']) + " " + str(i['store_address'])
        tag = normal(tag)
        lst_tag = split_tag(tag)
        p = len(set(lst_text) & set(lst_tag))
        if (p == len(lst_text)):
            i['_id'] = str(i['_id'])
            lst_dict.append(i)
    if not lst_dict:
        for i in coll.find():
            tag = str(i['store_name']) + " " + str(i['store_address'])
            tag = normal(tag)
            lst_tag = split_tag(tag)
            p = len(set(lst_text) & set(lst_tag))
            lst_cat = split_tag(i['category'].lower())
            p_cat = len(set(lst_text) & set(lst_cat))
            if (p_cat > 0 or p >= 2):
                i['_id'] = str(i['_id'])
                lst_dict.append(i)
    for i in lst_dict:
        i['km'] = distance(i['longitude'], i['latitude'], long, lat)
    lst_dict = sorted(lst_dict, key=lambda d: d['km'])
    if page is None:
        return lst_dict
    else:
        return lst_dict[0:50]
Пример #5
0
 def sum_of_distances(individual):
     broken_bots = [position[bot] for bot in individual["path"]]
     return sum([
         distance(bot_1, bot_2)
         for (bot_1,
              bot_2) in zip(broken_bots, broken_bots[1:] + [broken_bots[0]])
     ])
Пример #6
0
def main():
    home = {"lng": -92.019850, "lat": 30.213028}

    start = time.time()
    locations = []
    tries = 0
    while len(locations) < 10:
        tries += 1
        print("Try {}: fetching locations".format(tries))
        r = query()
        for location in r:
            location["dist"] = distance(home, location)
            location["href"] = permalink(location)
        r.sort(key=operator.itemgetter("dist"))
        r = itertools.takewhile(lambda x: x["dist"] < 100000, r)
        r = list(r)
        locations.extend(r)
        print("Try {}: found {} locatings within 100km, have {}".format(
            tries, len(r), len(locations)))

    dur = time.time() - start
    print("Took {:,.0f}s to find {} locations within 100km".format(
        dur, len(locations)))

    locations.sort(key=operator.itemgetter("dist"))
    for location in locations:
        print("{0[dist]: =10,.0f}m {0[formatted_address]} {0[href]}".format(
            location))
Пример #7
0
def node_distance(node1, node2, min_distance, max_distance):
    """[summary] Calulcates whether a naptan node distance in meters, using
    geodesic formulate

    Arguments:
        node1 {[type]} -- [description]
        node2 {[type]} -- [description]
        min_distance -- the min distance in meters that should be between two
        nodes.
        max_dist {[float]} -- the max distance in meters that can be between
        the nodes

    Returns:
        Distance [str] -- [description]
    """

    lat_n1, lon_n1 = node1.Latitude, node1.Longitude
    lat_n2, lon_n2 = node2.Latitude, node2.Longitude
    distance = distance((lat_n1.values[0], lon_n1.values[0]),
                        (lat_n2.values[0], lon_n2.values[0])).meters

    if (distance >= max_distance):
        return ('Nodes are too far away.')
    if (distance <= min_distance):
        return ('Nodes are too close.')
    else:
        return (f'Stop is within; {distance:.2f} meters.')
Пример #8
0
def cate(long, lat, page):
    lstcat = predict()
    lst = []
    for i in coll.find({}, {'_id': 0}):
        if (i['category'].lower() in lstcat):
            i['km'] = distance(i['longitude'], i['latitude'], long, lat)
            lst.append(i)
    lst = sorted(lst, key=lambda d: d['km'])
    return lst[(page - 1) * 15:page * 15 - 1]
Пример #9
0
def parser(client, userdata, message):
    global uid
    global p
    print(message.payload)
    if message.topic == "/user/cancel":
        payload = message.payload
        payload = str(payload)
        p.uuid = str(p.uuid)
        p.euid = str(p.euid)
        print(payload)
        if str(payload.split("::")[0]) == str(p.uuid) and str(
                payload.split("::")[1]) == str(p.euid):
            try:
                p.stop()
                # magic.gpio.display("Event resolved")
                # time.sleep(5)
                # magic.gpio.display("")
                p = None
            except:
                pass
    elif message.topic == "/user/events":
        try:
            payload = json.loads(message.payload)
        except:
            payload = json.loads(str(message.payload, "utf8"))
        if not distance(
            [self_lat, self_long],
                payload["location"]) > conf["l" + str(payload["level"])]:
            m = MapGenerator(
                [{
                    "lat": self_lat,
                    "long": self_long,
                    "style": "round",
                    "color": "",
                    "size": "",
                    "content": ""
                }, {
                    "lat": payload["location"][0],
                    "long": payload["location"][1],
                    "color": ["rd", "or", "gn"][payload["level"] - 1],
                    "style": "pm2",
                    "size": "l",
                    "content": ""
                }], {
                    "lang": "ru_RU",
                    "width": 320 if THIS_PI else 650,
                    "height": 240 if THIS_PI else 450,
                    "type": "map"
                }).get_file()
            if THIS_PI:
                p = PicDisplayerFbi(m)
            else:
                p = PicDisplayerPygame(m)
            p.euid = payload["euid"]
            p.uuid = payload["uuid"]
Пример #10
0
def neighbor_dectection_with_longlat(latitude, longitude, data):
    distances = []
    long_ = float(longitude)
    lat_ = float(latitude)
    for i in range(len(data)):
        if (data.loc[i].latitude != -1 and data.loc[i].longitude != -1):
            distances.append(
                distance(lat_, long_, data.loc[i].latitude,
                         data.loc[i].longitude))
    distances = np.array(distances)
    return data.loc[np.argsort(distances)[:30]]
Пример #11
0
def dist_to_segment(x,
                    ab,
                    distance=geopy.distance.great_circle
                    ) -> Tuple[float, float]:
    # Compute lengths
    xa = distance(x, ab[0]).m
    xb = distance(x, ab[1]).m
    ab = distance(ab[0], ab[1]).m
    if (ab == 0): return (xa, 0)
    # Heron's formula for the area-squared
    s = (xa + xb + ab) / 2
    AA = s * (s - xa) * (s - xb) * (s - ab)
    # Height
    h = math.sqrt(max(0, AA)) / ab * 2
    # From base edngpoints to base of the height
    ah = math.sqrt(max(0, xa**2 - h**2))
    bh = math.sqrt(max(0, xb**2 - h**2))
    # Distance and relative coordinate
    (d, t) = max([(bh, (xa, 0)), (ah, (xb, 1)), (ab, (h, ah / ab))])[1]
    return (d, t)
Пример #12
0
def get_distance_weight(where_query, poi):
    where_coordinates = YandexAPI().get_coordinates(where_query)
    poi_coordinates = MongoDB().get_poi_by_id(str(poi['_id']))
    dist = distance(float(where_coordinates['lat']), float(where_coordinates['lon']), float(poi_coordinates['lat']), float(poi_coordinates['lon']))
    if WEIGHT.FIRST_DISTANCE_RANGE_MAX <= dist <= WEIGHT.FIRST_DISTANCE_RANGE_MIN:
        return distance_range(WEIGHT.FIRST_DISTANCE_POINT_MIN, WEIGHT.FIRST_DISTANCE_POINT_MAX, WEIGHT.FIRST_DISTANCE_RANGE_MIN, WEIGHT.FIRST_DISTANCE_RANGE_MAX, dist)
    elif WEIGHT.SECOND_DISTANCE_RANGE_MAX <= dist <= WEIGHT.SECOND_DISTANCE_RANGE_MIN:
        return distance_range(WEIGHT.SECOND_DISTANCE_POINT_MIN, WEIGHT.SECOND_DISTANCE_POINT_MAX, WEIGHT.SECOND_DISTANCE_RANGE_MIN, WEIGHT.SECOND_DISTANCE_RANGE_MAX, dist)
    elif WEIGHT.THIRD_DISTANCE_RANGE_MAX <= dist <= WEIGHT.THIRD_DISTANCE_RANGE_MIN:
        return distance_range(WEIGHT.THIRD_DISTANCE_POINT_MIN, WEIGHT.THIRD_DISTANCE_POINT_MAX, WEIGHT.THIRD_DISTANCE_RANGE_MIN, WEIGHT.THIRD_DISTANCE_RANGE_MAX, dist)
    else:
        return 0
Пример #13
0
def reprojection_error(X):
    global min_err, best_h

    # Camera Matrix and Distortion Coefficients
    f_x, f_y, c_x, c_y, k1, k2, p1, p2 = X
    mtx = np.array([[f_x, 0, c_x], [0, f_y, c_y], [0, 0, 1]], dtype=np.float64)
    dist = np.array([[k1, k2, p1, p2]], dtype=np.float64)
    # Undistort detection points # https://stackoverflow.com/questions/22027419/bad-results-when-undistorting-points-using-opencv-in-python
    undistorted_points = cv2.undistortPoints(distorted_points.reshape(
        -1, 1, 2),
                                             mtx,
                                             dist,
                                             P=mtx)
    # Compute Homography
    choices = np.random.randint(0, len(distorted_points), size=10)
    h, status = cv2.findHomography(undistorted_points[choices],
                                   gps_points[choices], cv2.RANSAC)

    if type(h) != type(None):
        # Compute New Projections # https://stackoverflow.com/questions/55055655/how-to-use-cv2-perspectivetransform-to-apply-homography-on-a-set-of-points-in
        gps_projections = cv2.perspectiveTransform(undistorted_points, h)
        gps_projections = gps_projections.reshape(-1, 2)
        # Compute Average Projection Distance Error

        ERR = []
        for i in range(len(gps_projections)):
            ERR.append(distance(gps_projections[i], gps_points[i]))
        RMSE = (sum([err**2 for err in ERR]) / len(ERR))**.5
        if RMSE < min_err:
            min_err = RMSE
            best_camera_params["Homography"] = h
            best_camera_params["Distortion Coefficients"] = dist
            best_camera_params["Intrinsic Matrix"] = mtx

            fig = Figure()
            canvas = FigureCanvas(fig)
            ax = fig.gca()
            ax.scatter(gps_projections[:, 0],
                       gps_projections[:, 1],
                       c=ERR / max(ERR),
                       cmap="Reds")
            ax.scatter(gps_points[:, 0], gps_points[:, 1],
                       marker="x")  #,c=colors,cmap="bwr")

            canvas.print_figure("output.png")
            frame = plt.imread("output.png")
            image_without_alpha = frame[:, :, :3]
            frame = np.uint8(255 * image_without_alpha)
            out.write(frame)
        return RMSE
    else:
        return 10e10
Пример #14
0
def latStepsFromLngSteps(bounds, lng_steps):
    xstart = bounds['southwest']['lng']
    xend = bounds['northeast']['lng']
    ystart = bounds['southwest']['lat']
    yend = bounds['northeast']['lat']

    xdist = distance((ystart, xstart), (ystart, xend))
    ydist = distance((ystart, xstart), (yend, xstart))
    lat_steps = int((ydist / xdist) * lng_steps)

    #print('lng_steps = ' + str(lng_steps))
    #print('lat_steps = ' + str(lat_steps))
    #print('aspect ratio = ' + str(ydist / xdist))
    #print('xdist = ' + str(xdist))
    #print('xincr = ' + str(xdist / lng_steps))
    #print('ydist = ' + str(ydist))
    #print('yincr = ' + str(ydist / lat_steps))

    #xstep = (xend - xstart) / lng_steps
    #ystep = xstep * xdist / ydist

    return lat_steps
def finder(ll, lats, longs, APIs, pad, max_dist=0.05):
    """
    Determine which lats and longs are within the criteria
    distance of ll to be included in the same pad
    The function is designed for use in a recursive program
    :input ll: latitude and longitude of a point to be considered
    :input lats: a sorted array of latitudes
    :input longs: an array of longitudes associated with lats
    :input APIs: an array of API values of the wells
    :input pad: a list of wells grouped into pads.
    :input max_dist: maximum distance to the nearest well to be considered
    a pad (km)
    :return: none
    """
    pad.append(ll)
    if len(lats) < 1:
        return
    minind = binary_find_min(ll[0], lats)
    # preliminary fast culling of distant wells
    if crude_lat_dist(ll[0], lats[minind]) > max_dist * 10:
        return
    else:
        cond = []
        temp = minind
        while temp >= 0 and \
                crude_lat_dist(ll[0], lats[temp]) <= max_dist * 10:
            cond.append(temp)
            temp -= 1
        temp = minind + 1
        while temp < len(lats) and \
                crude_lat_dist(ll[0], lats[temp]) <= max_dist * 10:
            cond.append(temp)
            temp += 1
        cond.sort(reverse=True)
        # final selection of nearby wells to be included in the pad
        la = [lats[l] for l in cond]
        lo = [longs[l] for l in cond]
        distances = distance(np.array(list(zip(la, lo))), ll)
        cond2 = np.where(distances < max_dist)[0]
        if len(cond) == 0:
            return
        winners = []
        for c2 in cond2:
            winners.append([lats[cond[c2]], longs[cond[c2]], APIs[cond[c2]]])
            del (lats[cond[c2]])
            del (longs[cond[c2]])
            del (APIs[cond[c2]])
        for w in winners:
            finder(w, lats, longs, APIs, pad)
Пример #16
0
def user(user_id, long, lat):
    transList = list(db['tran'].find({'user_id': Int64(user_id)}, {'_id': 0}))
    merList = []
    for i in transList:
        mer = coll.find_one({'store_id': Int64(i['store_id'])}, {
            '_id': 0,
            '': 0
        })
        if mer is not None:
            mer['amount'] = i['amount']
            mer['km'] = distance(float(mer['longitude']),
                                 float(mer['latitude']), long, lat)
            merList.append(mer)

    return merList
Пример #17
0
def create_distance_matrix(locations):
    """Compute distance matrix for given locations.

    :param locations: a sequence of cordinates
    :type location: sequence of pairs of floats
    :returns: a matrix M of shape len(locations) x len(locations) such that
     M[i, j] is a distance between i-th and j-th location.
    :rtype: numpy.ndarray
    """
    size = len(locations)
    result = numpy.zeros((size, size))
    for i in range(size):
        for j in range(i + 1, size):
            result[i, j] = result[j, i] = distance(locations[i], locations[j])
    return result
Пример #18
0
def main(args):
    if not args.output[-5:] == '.html':
        exit('ERROR Output file must be .html')

    heatmap_data = []

    for gpx_file in get_gpx_files(args):
        if not args.quiet:
            print('Reading {}'.format(gpx_file))

        with open(gpx_file, 'r') as file:
            last_point = None
            for line in file:
                if '<trkpt' in line:
                    r = re.findall('[-]?[0-9]*[.]?[0-9]+', line)

                    point = [float(r[0]), float(r[1])]
                    if last_point is not None and distance(point, last_point) < args.skip_distance:
                        continue
                    heatmap_data.append(point)
                    last_point = point

    if not args.quiet:
        print('Loaded {} trackpoints'.format(len(heatmap_data)))

    fmap = Map(tiles = 'CartoDB positron' if args.light_map else 'CartoDB dark_matter',
               location=args.center,
               zoom_start=11,
               prefer_canvas = True,
               max_zoom = HEATMAP_MAXZOOM)

    HeatMap(heatmap_data,
            radius = args.radius,
            blur = args.blur,
            gradient = HEATMAP_GRAD['light' if args.light_map else 'dark'],
            min_opacity = args.min_opacity,
            max_val = args.max_val).add_to(fmap)

    if args.center is None:
        fmap.fit_bounds(fmap.get_bounds())

    fmap.save(args.output)

    if not args.quiet:
        print('Saved {}'.format(args.output))

    if not args.quiet:
        webbrowser.open(args.output, new = 2, autoraise = True)
Пример #19
0
def inst_velocity(dataset, since, until):
    """
    Calculating instantaneous velocity since some moment of time until another moment of time. Velocity is computed as distance change divided by time change.
    :param dataset: List of pandas.DataFrames, each corresponding to one moment of time. Each of these dataframes should have columns: "Lines", "Lon", "VehicleNumber", "Time", "Lat", "Brigade".
    :param since: index of list(dataset). It corresponds to the moment of time to start measuring velocity.
    :param until: index of list(dataset). It correspons to the moment of time to end measuring velocity.
    :return: dataframe containing two columns: VehicleNumber and Velocity.
    """
    dist = distance(dataset, since, until)
    t_diff = time_difference(dataset, since, until)
    merged = pd.merge(dist, t_diff, on=["VehicleNumber"], how="inner")
    merged["Velocity"] = np.NaN
    updated = (merged["Time_difference"] != 0)
    merged["Velocity"][updated] = 3600 * (merged["Distance"][updated] /
                                          merged["Time_difference"][updated])
    result = merged[["VehicleNumber", "Velocity"]]
    return result
Пример #20
0
 def from_csv_record(row, mgc):
     ''' create a pipe from csv '''
     pid = int(float(row['LINEID']))
     f_junction = int(row['FRNODE'])
     t_junction = int(row['TONODE'])
     diameter = float(row['DIAMETER']) * .0254
     f_node = (float(row['FRNODE_Y']), float(row['FRNODE_X']))
     t_node = (float(row['TONODE_Y']), float(row['TONODE_X']))
     length = distance(f_node, t_node)
     pipe = Pipe({
         '_id': pid,
         'f_junction': f_junction,
         't_junction': t_junction,
         'diameter': diameter,
         'length': length
     })
     return pipe
Пример #21
0
def wkmeans(values, k=None, centroids=None, steps=200):
    # initialize k points randomly
    centroids = values[np.random.choice(np.arange(len(values), ), k, False)]

    for step in range(max(steps, 1)):
        # compute distance between each pair of the two collections of inputs
        dists = scipy.spatial.distance.cdist(centroids, values,
                                             lambda u, v: distance(u, v)**2)
        # closest centroid to each point
        clusters = np.argmin(dists, axis=0)

        new_centroids = cluster_centroids(values, clusters, k)

        if np.array_equal(new_centroids, centroids):
            break
        centroids = new_centroids

    return clusters, centroids
Пример #22
0
def parser(client, userdata, message):
    global uid
    global p
    print(message.payload)
    if message.topic == "/user/cancel":
        payload = message.payload
        payload = str(payload)
        p.uuid = str(p.uuid)
        p.euid = str(p.euid)
        print(payload)
        if str(payload.split("::")[0]) == str(p.uuid) and str(payload.split(
                "::")[1]) == str(p.euid):
            try:
                p.stop()
                # magic.gpio.display("Event resolved")
                # time.sleep(5)
                # magic.gpio.display("")
                p = None
            except:
                pass
    elif message.topic == "/user/events":
        try:
            payload = json.loads(message.payload)
        except:
            payload = json.loads(str(message.payload, "utf8"))
        if not distance([self_lat, self_long],
                        payload["location"]) > conf["l" +
                str(payload["level"])]:
            m = MapGenerator([{"lat": self_lat, "long": self_long,
                               "style": "round", "color": "", "size": "",
                               "content": ""}, {"lat": payload["location"][0],
                                                "long": payload["location"][1],
                                                "color": ["rd","or","gn"][payload["level"]-1], "style": "pm2",
                                                "size": "l", "content": ""}],
                             {"lang": "ru_RU", "width": 320 if THIS_PI else 650, "height": 240 if THIS_PI else 450,
                              "type": "map"}).get_file()
            if THIS_PI:
                p = PicDisplayerFbi(m)
            else:
                p = PicDisplayerPygame(m)
            p.euid = payload["euid"]
            p.uuid = payload["uuid"]
Пример #23
0
def update_text(selectedData):
    s = ""
    di = ""
    try:
        lat1 = selectedData['points'][0]['lat']
        lon1 = selectedData['points'][0]['lon']
        lat2 = selectedData['points'][1]['lat']
        lon2 = selectedData['points'][1]['lon']
        di = distance(lat1, lon1, lat2, lon2)
    except (TypeError, IndexError) as e:
        pass

    try:
        return html.H3('{} is within a {:.1f} mile radius from {}'.format(
            selectedData['points'][0]['text'], di,
            selectedData['points'][1]['text']))
    except (TypeError, IndexError) as e:
        return html.H3(
            "Select two points on the map with the selection tool to see the distance."
        )
Пример #24
0
def check_polygon_lengths(polygon):
    """[summary] Get the longest side of a given polygon, in meters

    Arguments:
        polygon {[shapely.geometry.polygon.Polygon]} -- [the polygon of the 
        given area, ]

    Raises:
        NotImplemented: [description]

    Returns:
        [type] -- [description]
    """
    # get minimum bounding box around polygon
    box = polygon.minimum_rotated_rectangle
    # get coordinates of polygon vertices
    x, y = box.exterior.coords.xy
    # get length of bounding box longest edge
    edge_length = distance(
        Point(x[0], y[0]).distance(Point(x[1], y[1])),
        Point(x[1], y[1]).distance(Point(x[2], y[2]))).meters
    return edge_length
    raise NotImplementedError
Пример #25
0
 def __score__(self, nb_occ, latitude, longitude, prefix):
     if nb_occ == 1:
         self.score = 1
     else:
         if nb_occ >= 10:
             first_score = 0.1
         else:
             first_score = 1 / nb_occ
         self.score = first_score
         if latitude is not None:
             self.score += getScoreOfDist(
                 (1 - first_score) / 3,
                 distance(latitude, longitude, self.latitude,
                          self.longitude))
             self.score += getScoreOfPop((1 - first_score) / 3,
                                         self.population)
             self.score += getScoreOfNameVsPrefix((1 - first_score) / 3,
                                                  self.name, prefix)
         else:
             self.score += getScoreOfPop((1 - first_score) / 2,
                                         self.population)
             self.score += getScoreOfNameVsPrefix((1 - first_score) / 2,
                                                  self.name, prefix)
         self.score = round(self.score, 1)
Пример #26
0
def least_distance(locations):
    if len(locations) == 1:
        return 0
    elif len(locations) == 2:
        return distance(locations[0], locations[1])
    elif len(locations) == 3:
        d = []
        d[0] = distance(locations[0], locations[1])
        d[1] = distance(locations[2], locations[1])
        d[2] = distance(locations[2], locations[0])
        d.sort()
        return d[0] + d[1]
    elif len(locations) == 4:
        l = list(itertools.permutations(locations))
        d = []
        for i in range(len(l)):
            d[i] = distance(l[i][0], l[i][1]) + distance(
                l[i][2], l[i][1]) + distance(l[i][2], l[i][3])
        d.sort()
        return d[0]

    else:
        return 0
        "name": name,
        "mass": mass,
        "fall": fall,
        "year": year(date),
        "location": location(lat, lng)
    }

def distance(location):
    def inner(meteor):
        """
        Distance between a location and a meteor
        """
        return geopy.distance.vincenty((location.latitude, location.longitude), meteor['location'])
    return inner

def get_location():
    """
    Find the user's location
    """
    city=raw_input('Which city are you in?: ')
    country=raw_input('Which country are you in?: ')
    return geolocator.geocode(city + ' ' + country)

with open('Meteorite_Landings.csv') as f:
    meteors = [meteor(line) for line in f.readlines()]

location = get_location()
meteor = min(meteors, key=distance(location))
print('the nearest meteor to you was {0} in {1}'.format(meteor['name'], meteor['year']))
webbrowser.open('https://www.google.co.uk/maps/place/{0[0]},{0[1]}'.format(meteor['location']))
def deltax(esp, variance):
    maximum = distance(esp - variance)
    minimum = distance(esp + variance)
    return (maximum - minimum) / 2
def trilateration(point_list, gateway_list, ref_point, filter):

    #filter: tuple(inner_filter,outer_filter)
    #read all point and gateway data
    data = trilat_extract_info(point_list, gateway_list, ref_point)

    #create a zero reference point to be used for the circle coordinate system
    total_lat = total_lon = c = 0
    for gtw in data:
        total_lat += float(gtw['Lat'])
        total_lon += float(gtw['Lon'])
        c += 1
    zero_point = total_lat / c, total_lon / c
    #print(zero_point)
    #print(data)

    gtw_pairs = []
    intersect_points = []

    #for every pair of gateways:
    for gtw1 in data:
        for gtw2 in data:
            if gtw1['EUI'] != gtw2['EUI']:
                #Every pair exists twice. Filter.
                gtw_pairs.append({'EUI1': gtw1['EUI'], 'EUI2': gtw2['EUI']})
                #if the inverse pair does not exist yet
                if {'EUI1': gtw2['EUI'], 'EUI2': gtw1['EUI']} not in gtw_pairs:
                    #print("Gateway pair: "+gtw1['EUI']+ " + "+gtw2['EUI'])
                    '''
					#testing my own functions against geopy. Test ok, almost corresponds to great circle
					p1_geopy = geopy.distance.great_circle((gtw1['Lat'],gtw1['Lon']),zero_point).km * 1000
					p1_lat = coord_to_m('lat',zero_point[0]-gtw1['Lat'],gtw1['Lat'])
					p1_lon = coord_to_m('lon',zero_point[1]-gtw1['Lon'],gtw1['Lat'])
					p1_homemade = math.sqrt(p1_lat*p1_lat+p1_lon*p1_lon)
					print("Geopy distance: " + str(p1_geopy))
					print("Own function dist: " + str(p1_homemade))
					
					#Test: conversion to refcoords and back --> works
					print("Reference point: " + str(gtw1['Lat'])+", "+str(gtw1['Lon']))
					banana = latlon_to_ref((gtw1['Lat'],gtw1['Lon']),zero_point)
					print("Point in reference coordinates: "+str(banana))
					avocado = ref_to_latlon(banana,zero_point)
					print("Conversion back to lat/lon: " + str(avocado))
					'''

                    #build rings with sigma_rings time the variance
                    for param in range(0, 61):
                        sigma_ring = 0.1 * param - 3

                        if distance(gtw1['ESP-mean']) < distance(
                                gtw2['ESP-mean']):
                            closer_gateway = gtw1['EUI']
                            further_gateway = gtw2['EUI']
                            closer_distance = distance(gtw1['ESP-mean'])
                            closer_variance = distance(gtw1['ESP-var'])
                        else:
                            closer_gateway = gtw2['EUI']
                            further_gateway = gtw1['EUI']
                            closer_distance = distance(gtw2['ESP-mean'])
                            closer_variance = distance(gtw2['ESP-var'])

                        mean_distance = distance(
                            (gtw1['ESP-mean'] + gtw2['ESP-mean']) / 2)
                        mean_variance = distance(
                            (gtw1['ESP-var'] + gtw2['ESP-var']) / 2)

                        circle1 = latlon_to_ref(
                            (gtw1['Lat'], gtw1['Lon']), zero_point) + (
                                distance(gtw1['ESP-mean'] +
                                         sigma_ring * gtw1['ESP-var']), )
                        circle2 = latlon_to_ref(
                            (gtw2['Lat'], gtw2['Lon']), zero_point) + (
                                distance(gtw2['ESP-mean'] +
                                         sigma_ring * gtw2['ESP-var']), )
                        p1_refsystem, p2_refsystem = circle_intersection(
                            circle1, circle2)
                        if p1_refsystem != None:
                            p1_latlon = ref_to_latlon(p1_refsystem, zero_point)
                            p2_latlon = ref_to_latlon(p2_refsystem, zero_point)

                            #check validity of the point (within 3 sigma of every gateway)
                            valid1 = valid2 = True
                            for g in data:
                                #for every gateway, if distance point-gateway is bigger than dist+3sigma, point is invalid
                                #only one gateway not fitting into the criteria is setting it to false.

                                #outside circle
                                if geopy.distance.vincenty(
                                        p1_latlon,
                                    (g['Lat'], g['Lon'])).km * 1000 > distance(
                                        g['ESP-mean'] -
                                        filter[1] * g['ESP-var']):
                                    valid1 = False
                                if geopy.distance.vincenty(
                                        p2_latlon,
                                    (g['Lat'], g['Lon'])).km * 1000 > distance(
                                        g['ESP-mean'] -
                                        filter[1] * g['ESP-var']):
                                    valid2 = False

                                #inside circle
                                if geopy.distance.vincenty(
                                        p1_latlon,
                                    (g['Lat'], g['Lon'])).km * 1000 < distance(
                                        g['ESP-mean'] +
                                        filter[0] * g['ESP-var']):
                                    valid1 = False
                                if geopy.distance.vincenty(
                                        p2_latlon,
                                    (g['Lat'], g['Lon'])).km * 1000 < distance(
                                        g['ESP-mean'] +
                                        filter[0] * g['ESP-var']):
                                    valid2 = False

                            if (valid1):
                                intersect_points.append({
                                    'Ref':
                                    ref_point,
                                    'Gateways':
                                    (closer_gateway, further_gateway),
                                    'Intersection':
                                    p1_latlon,
                                    'Closer distance':
                                    closer_distance,
                                    'Closer variance':
                                    closer_variance,
                                    'Mean distance':
                                    mean_distance,
                                    'Mean variance':
                                    mean_variance,
                                    'Sigma ring':
                                    sigma_ring
                                })
                            if (valid2):
                                intersect_points.append({
                                    'Ref':
                                    ref_point,
                                    'Gateways':
                                    (closer_gateway, further_gateway),
                                    'Intersection':
                                    p2_latlon,
                                    'Closer distance':
                                    closer_distance,
                                    'Closer variance':
                                    closer_variance,
                                    'Mean distance':
                                    mean_distance,
                                    'Mean variance':
                                    mean_variance,
                                    'Sigma ring':
                                    sigma_ring
                                })

    return intersect_points
Пример #30
0
        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('')


# Kuala Lumpur --> Bangkok --> Beijing --> Seoul --> Tokyo --> Taipei --> Hong Kong --> Jakarta --> Kuala Lumpur
# 13930.557 km

# path = [0, 2, 6, 7, 5, 3, 4, 1, 0]
path = [0, 2, 6, 7, 3, 4, 1, 5, 0]
#path = [0, 3, 7, 6, 2, 1, 5, 4, 0]
print(len(path)-1)

def distance(path):
    sum = 0.0
    for y in range (len(path)-1):
        sum = sum + distanceCountry[path[y]][path[y+1]]
    return sum

print("The distance is ", end='')
print(distance(path))

# Kuala Lumpur --> Bangkok --> Beijing --> Seoul --> Taipei --> Hong Kong --> Jakarta --> Tokyo --> Kuala Lumpur
# The distance is 22059.093961229577
# [0, 1, 2, 3, 5, 6, 7, 4, 0]



# Sentiment path
# Kuala Lumpur --> Taipei --> Seoul --> Beijing --> Bangkok --> Jakarta --> Tokyo --> Hong Kong --> Kuala Lumpur
# [0, 3, 7, 6, 2, 1, 5, 4, 0]
Пример #31
0
from itertools import chain
import geopy.distance
distance = geopy.distance.vincenty    

# shapefile
baseFile = 'data/Oakland_parcels_queried/Oakland_parcels_queried'
errorFile = 'data/Oakland_parcels_errors/Oakland_parcels_errors'

# compute map boundscenter = (37.8058428, -122.2399758)        # (lat, long), Armenian Church
#center = geopy.Point(37.8058428, -122.2399758)        # (lat, long), Armenian Church
center = geopy.Point(37.79539889, -122.21547850)        # (lat, long), rough geometric center of entire dataset
#center = geopy.Point(37.759987, -122.221965)        # (lat, long), rough geometric center of error dataset
radius = 8.5                       # in km
#ur = distance(kilometers=radius).destination(center, +45)
#ll = distance(kilometers=radius).destination(center, -135)
ur = distance(kilometers=radius*2**0.5).destination(center, +45)
ll = distance(kilometers=radius*2**0.5).destination(center, -135)
ur = (ur.longitude, ur.latitude)
ll = (ll.longitude, ll.latitude)

extra = 0.01           # padding
coords = list(chain(ll, ur))
w, h = coords[2] - coords[0], coords[3] - coords[1]

m = Basemap(
    projection='tmerc',
    lon_0=-122.,
    lat_0=37.,
    ellps = 'WGS84',
    llcrnrlon=coords[0] - extra * w,
    llcrnrlat=coords[1] - extra * h,
Пример #32
0
    for segment in track.segments:
        for point in segment.points:
            tudo.append([point.latitude, point.longitude, point.time])
#print(tudo)
c = 0
d = 0
week = timedelta(weeks=1)
week = week.total_seconds()
for point in tudo:
    tempo = point[2]
    point[2] = (
        point[2] -
        datetime.combine(get_first_dow(2018, point[2].isocalendar()[1]),
                         datetime.min.time())).total_seconds()
    p1 = [point[0], point[1]]
    if distance(p1, casa) < 500:
        if (c != 1):
            if DEBUG:
                print('\n\n')
                print("++chegou a casa")
                print(tempo)
            casaF.append(point)
        if d == 1:
            time = (point[2] - deiF[-1][2])
            if DEBUG:
                print('duracao: ', time)
            if time < 0:
                if DEBUG:
                    print("--saiu do dei")
                    print(week + time)
                deiF.append(week + time)
Пример #33
0
filename = 'Alameda County Parcel Boundaries.geojson'
r = 0.5  # in km

with open(filename, 'r') as fid:
    # store complete data fields within a certain distance of tempCenter

    objects = ijson.items(fid, 'features.item')
    data = {}

    #    for (i, o) in zip(range(5000), objects) :
    for (i, o) in enumerate(objects):
        if i % 1000 == 0:
            if i % 10000 == 0:
                print('{}: {} records so far'.format(i, len(data)))
            else:
                print(i, end=' ')

        pos = o['geometry']['coordinates'][0][0][
            0]  # get (long, lat) of first vertex
        # would be better to get centroid of the polygon
        pos = (pos[1], pos[0])  # switch to (lat, long)
        d = '{}'.format(distance(pos, center))
        d = float(d[:-3])  # convert to a number that can be compared
        if d < 0.25:
            data[round(d, 6)] = o  # 6th decimal place in lat,long is 0.1 m
#            print('Record {} is {} km away'.format(i, d))

print('{}: {} records so far'.format(i, len(data)))

tempout.close()
import geopy
import geopy.distance
distance = geopy.distance.vincenty    

center = (37.8058428, -122.2399758)        # (lat, long), Armenian Church
center = (center[1], center[0])

data1 = {}
#for (i, (k, v)) in zip(range(10), data.items()) :
for (k, v) in data.items() :
    c = shp.shape(v['geometry'])
#    v['centroid'] = c.centroid.wkt
#    d = '{}'.format(distance(v['centroid'], center))       # distance can take a wkt format!
    
    v['centroid'] = (c.centroid.x, c.centroid.y)
    d = '{}'.format(distance(v['centroid'], center))
    
    d = float(d[:-3])           # convert to a number that can be compared
#    print('Old distance: {} km, new distance: {} km'.format(k, round(d,6)))
    print('Difference = {:6.2f} m'.format(abs(k-round(d,6))*1000))
    data1[round(d,6)] = v
    
data = data1
del data1


#%% How to save key to file
import pickle 

apifile = open('../private/API_keys.pkl', 'wb')
a = pickle.Pickler(apifile)
Пример #35
0
filename = 'Alameda County Parcel Boundaries.geojson'
r = 0.5             # in km


with open(filename, 'r') as fid:
    # store complete data fields within a certain distance of tempCenter

    objects = ijson.items(fid, 'features.item')
    data = {}

#    for (i, o) in zip(range(5000), objects) :
    for (i, o) in enumerate(objects) :
        if i%1000 == 0:
            if i%10000 == 0 :
                print('{}: {} records so far'.format(i, len(data)))
            else :
                print(i, end=' ')

        pos = o['geometry']['coordinates'][0][0][0]           # get (long, lat) of first vertex
            # would be better to get centroid of the polygon
        pos = (pos[1], pos[0])  # switch to (lat, long)
        d = '{}'.format(distance(pos, center))
        d = float(d[:-3])           # convert to a number that can be compared
        if d < 0.25 :
            data[round(d,6)] = o    # 6th decimal place in lat,long is 0.1 m
#            print('Record {} is {} km away'.format(i, d))
            
print('{}: {} records so far'.format(i, len(data)))
             
tempout.close()
Пример #36
0
from mpl_toolkits.basemap import Basemap
from shapely.geometry import Point, Polygon, MultiPoint, MultiPolygon
from shapely.prepared import prep
from descartes import PolygonPatch
import fiona
from itertools import chain
import geopy.distance
distance = geopy.distance.vincenty

baseFile = 'data/Oakland_parcels/parcels'
#baseFile = 'data/Oakland_parcels_queried/Oakland_parcels_queried'  # radius of 500 m

# compute map boundscenter = (37.8058428, -122.2399758)        # (lat, long), Armenian Church
center = geopy.Point(37.8058428, -122.2399758)  # (lat, long), Armenian Church
radius = 4  # in km
ur = distance(kilometers=radius).destination(center, +45)
ll = distance(kilometers=radius).destination(center, -135)
ur = (ur.longitude, ur.latitude)
ll = (ll.longitude, ll.latitude)

extra = 0.01  # padding
coords = list(chain(ll, ur))
w, h = coords[2] - coords[0], coords[3] - coords[1]

m = Basemap(projection='tmerc',
            lon_0=-122.,
            lat_0=37.,
            ellps='WGS84',
            llcrnrlon=coords[0] - extra * w,
            llcrnrlat=coords[1] - extra * h,
            urcrnrlon=coords[2] + extra * w,
roadnet_data = pd.read_excel(f_path + 'Dec 9 2016 Roadnet Export Route 24 STL for comparison.xlsx', header=0, sheetname='Sheet')

LOC,ADD,CITY,STATE,ZIP = roadnet_data['Location'].astype(str), roadnet_data['Address Line 1'].astype(str), roadnet_data['City'].astype(str), 'MO', roadnet_data['Postal Code'].astype(str)
roadnet_data['Street'] = ADD
roadnet_data['ExtendedAddress'] = LOC +', '+ ADD +', '+ CITY +', '+ STATE +', '+ ZIP +', '+ 'USA'

roadnet_coordinates = roadnet_data['Coordinate'].astype(str)
lat = [r.split(',')[0] for r in roadnet_coordinates]
roadnet_data['Latitude'] = lat = [re.sub('[(]', '', l.strip()) for l in lat]

lon = [r.split(',')[-1] for r in roadnet_coordinates]
roadnet_data['Longitude'] = lon = [re.sub('[)]', '', l.strip()) for l in lon]


print('Create distance matrix between both dataframes.')
[distance(re.sub('[,()]',' ',r), re.sub('[,()]',' ',r))  for r in roadnet_coordinates]



stop_candidates.merge(roadnet_data, on='Latitude')







print('Conduct fuzzy match.')
FUZZY = difflib.Differ()
matches = list(FUZZY.compare(stop_candidates.Street, roadnet_data.Street))
pp = pprint.PrettyPrinter(indent=4)