示例#1
0
def make_hexmap(MAREA=None, d=300):
    '''Takes a shapefile and fills it with hexagons of diameter d.
    
    Args: 
        MAREA (GeoDataFrame) : Polygon of boundaries of a given city.
        d : desired hexagon diameter
    
    Returns:
        gdf (GeoDataFrame) : Hexagon grid GeoDataFrame
    '''

    xmin, ymin, xmax, ymax = MAREA.total_bounds  # lat-long of 2 corners
    #East-West extent of Toronto = 42193 metres
    EW = haversine((xmin, ymin), (xmax, ymin))
    # North-South extent of Toronto = 30519 metres
    NS = haversine((xmin, ymin), (xmin, ymax))
    # diameter of each hexagon in the grid = 900 metres
    d = 300
    # horizontal width of hexagon = w = d* sin(60)
    w = d * np.sin(np.pi / 3)
    # Approximate number of hexagons per row = EW/w
    n_cols = int(EW / w) + 1
    # Approximate number of hexagons per column = NS/d
    n_rows = int(NS / d) + 1

    # Make hexagons
    w = (xmax - xmin) / n_cols  # width of hexagon
    d = w / np.sin(np.pi / 3)  #diameter of hexagon
    array_of_hexes = []
    for rows in range(0, n_rows):
        hcoord = np.arange(xmin, xmax, w) + (rows % 2) * w / 2
        vcoord = [ymax - rows * d * 0.75] * n_cols
        for x, y in zip(hcoord, vcoord):  #, colors):
            hexes = RegularPolygon((x, y),
                                   numVertices=6,
                                   radius=d / 2,
                                   alpha=0.2,
                                   edgecolor='k')
            verts = hexes.get_path().vertices
            trans = hexes.get_patch_transform()
            points = trans.transform(verts)
            array_of_hexes.append(Polygon(points))
            #ax.add_patch(hexes)

    # make final geodataframe
    hex_grid = gpd.GeoDataFrame({'geometry': array_of_hexes},
                                crs={'init': 'epsg:4326'})
    MAREA_hex = gpd.overlay(hex_grid, MAREA)
    #MAREA_hex = hex_grid
    MAREA_hex = gpd.GeoDataFrame(MAREA_hex, geometry='geometry')
    MAREA_hex = MAREA_hex.reset_index()

    return MAREA_hex
def hex_bins(osm_buffer_gpkg_path, study_area, gdf_osm_destinations_clipped):

    boundary = gpd.read_file(osm_buffer_gpkg_path, layer="urban_study_region")
    gdf_boundary = boundary["geometry"]

    xmin, ymin, xmax, ymax = gdf_boundary.total_bounds  # lat-long of 2 corners
    xmin -= 500
    xmax += 500
    ymin -= 500
    ymax += 500
    # East-West extent of urban_study_region
    EW = xmax - xmin
    # North-South extent of urban_study_region
    NS = ymax - ymin
    # Hexagon bins diameter should equal 500 meters
    d = 500
    # horizontal width of hexagon = w = d* sin(60)
    w = d * np.sin(np.pi / 3)
    # Approximate number of hexagons per row = EW/w
    n_cols = int(EW / d) + 1
    # Approximate number of hexagons per column = NS/d
    n_rows = int(NS / w) + 1

    w = (xmax - xmin) / n_cols  # width of hexagon
    d = w / np.sin(np.pi / 3)  # diameter of hexagon 500 meters
    array_of_hexes = []

    # +1 added to n_rows since the range function runs from 0 through (n-1), and the number of rows of hexgons plotted
    # was one less than the expcted number of rows.
    for rows in range(0, n_rows + 1):
        hcoord = np.arange(xmin, xmax, w) + (rows % 2) * w / 2
        vcoord = [ymax - rows * d * 0.75] * n_cols
        for x, y in zip(hcoord, vcoord):
            hexes = RegularPolygon((x, y),
                                   numVertices=6,
                                   radius=d / 2,
                                   alpha=0.2,
                                   edgecolor="k")
            verts = hexes.get_path().vertices
            trans = hexes.get_patch_transform()
            points = trans.transform(verts)
            array_of_hexes.append(Polygon(points))

    # turn study_area polygon into gdf with correct CRS
    gdf_boundary = gpd.GeoDataFrame(geometry=[study_area],
                                    crs=gdf_osm_destinations_clipped.crs)
    gdf_boundary = gpd.GeoDataFrame(gdf_boundary)

    hex_grid = gpd.GeoDataFrame({"geometry": array_of_hexes})
    hex_grid_clipped = gpd.overlay(hex_grid, gdf_boundary)
    hex_grid_clipped = gpd.GeoDataFrame(hex_grid_clipped, geometry="geometry")

    return gdf_boundary, hex_grid_clipped
示例#3
0
n_rows = int(NS / d) + 10

# Make a hexagonal grid to cover the entire area
from matplotlib.patches import RegularPolygon

ax = TIME_EDGES.boundary.plot(edgecolor='black', figsize=(20, 60))
w = (xmax - xmin) / n_cols  # width of hexagon
d = w / np.sin(np.pi / 3)  # diameter of hexagon
array_of_hexes = []
for rows in range(0, n_rows):
    hcoord = np.arange(xmin, xmax, w) + (rows % 2) * w / 2
    vcoord = [ymax - rows * d * 0.75] * n_cols
    for x, y in zip(hcoord, vcoord):  # , colors):
        hexes = RegularPolygon((x, y), numVertices=6, radius=d / 2, alpha=0.2, edgecolor='k')
        verts = hexes.get_path().vertices
        trans = hexes.get_patch_transform()
        points = trans.transform(verts)
        array_of_hexes.append(Polygon(points))
        ax.add_patch(hexes)
ax.set_xlim([xmin, xmax])
ax.set_ylim([ymin, ymax])

hex_grid = gpd.GeoDataFrame({'geometry': array_of_hexes}, crs={'init': 'epsg:4326'})
# hex_grid = gpd.GeoDataFrame({'geometry':array_of_hexes},crs={'init':'epsg:3035'})
# hex_grid.plot()


#############################################################################################
# create basemap
ave_LAT = 37.53988692816245
ave_LON = 15.044971594798902