示例#1
0
def getRegions(point, regiondf):

    res = 0

    for index, row in regiondf.iterrows():
        print(row['geometry'])
        p = MultiPolygon(row['geometry'])
        print(p)
        if p.contains(point):
            res = row["name"]
            break
        print(res)
    return res
示例#2
0
def print_closest_dis():
    # Load data
    df = pd.read_csv(
        'https://raw.githubusercontent.com/trendct/dunkin-donuts-ct/master/dunkindonuts.csv'
    )
    ma_dunkin_donuts = df.loc[df['state'] == 'MA']
    dunkin_donuts_coordinates = ma_dunkin_donuts[['lng', 'lat']].to_numpy()

    with urlopen(
            'https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json'
    ) as response:
        counties = json.load(response)
        df = pd.json_normalize(counties['features'])
        ma_geodata = pd.json_normalize(counties['features'])[
            df['properties.STATE'] == '25']['geometry.coordinates'].to_numpy()
        ma_coordinates = flatten(ma_geodata)

    # Create Voronoi and Polygons
    vor = Voronoi(dunkin_donuts_coordinates)
    polygons = [Polygon(county) for county in ma_coordinates]
    polygon = MultiPolygon(polygons)

    biggestDistanceYet = 0
    bestVertex = []
    closest_dunkin_coord = []

    # Calculate best vertex
    for vertex in vor.vertices:
        closest_dunkin_branch = closest_dunkin(vertex,
                                               dunkin_donuts_coordinates)
        minDis = distance.cdist([vertex], [closest_dunkin_branch])
        if biggestDistanceYet < minDis and polygon.contains(Point(vertex)):
            biggestDistanceYet = minDis
            bestVertex = vertex
            closest_dunkin_coord = closest_dunkin_branch

    print(
        "The furthest away you can get from a dunkin donuts is {} km at the coordinates {}. "
        "The closest dunkin donuts would be at {}.".format(
            haversine_np(bestVertex, closest_dunkin_coord), bestVertex,
            closest_dunkin_coord))

    # Plot everything
    fig = voronoi_plot_2d(vor, show_vertices=False, line_width=0)
    ax = fig.gca()
    ax.add_patch(
        plt.Circle(bestVertex,
                   biggestDistanceYet,
                   color='g',
                   fill=False,
                   linewidth=2))
    plt.plot(bestVertex[0], bestVertex[1], 'o')
    plt.ylim(40.5, 43)
    for a in polygons:
        x, y = a.exterior.xy
        plt.plot(x, y)
    ax.set_aspect('equal')
    ax.set_ylabel('Latitude')
    ax.set_xlabel('Longitude')

    plt.show()
    fig.savefig('vonoroi.png')