Ejemplo n.º 1
0
def multiscalar(geoDF,populationCensusTif):
    geoDF=geoDF.to_crs(communityArea_CRS)
    plt.figure()
    geoDF.plot(column='Gonorrhea in Females',cmap='OrRd',figsize=(10, 10),legend=True,scheme='quantiles') # scheme='quantiles'
    # geoplot.polyplot(geoDF,figsize=(20, 20))
    # pc=gpd.read_file(populationCensus)
    # print(pc.columns)
    # print(pc.crs)
    # pc.to_file(os.path.join(dataRoot,"populationCensus_save.shp"))
    # Feature_to_Raster(os.path.join(dataRoot,"populationCensus_save.shp"), os.path.join(dataRoot,"popuC_1.tif"), 20, field_name=['CENSUS_BLO'], NoData_value=-9999)
    
    geoDF_sample=zonal_stats(geoDF,populationCensusTif,stats="count min mean max median")
    geoDF_sample_df=pd.DataFrame(geoDF_sample)
    geoDF_sample_df.rename(columns={"count":"popuCount", "min":"popuMin", "mean":"popuMean", "max":"popuMax", "median":"popuMedian"},inplace=True)
    geoDF_merge=geoDF.merge(geoDF_sample_df,left_on=geoDF.index, right_on=geoDF_sample_df.index)
    
    groups_list = ['Cancer (All Sites)','Lung Cancer','Tuberculosis','Gonorrhea in Females']
    geoDF_merge[groups_list]=geoDF_merge[groups_list].fillna(0)
    geoDF_merge["sumGroup"]=geoDF_merge[groups_list].sum(axis=1)
    for i in range(len(groups_list)):
        geoDF_merge['comp_' + groups_list[i]] = geoDF_merge[groups_list[i]] /geoDF_merge["sumGroup"]  #input_df['popuMean']
    df_pts=geoDF_merge.copy()
    df=df_pts
    
    w_queen = Queen.from_dataframe(df)
    w_rook = Rook.from_dataframe(df)
    w_kernel_1k = Kernel.from_dataframe(df_pts, bandwidth=2500)
    w_kernel_2k = Kernel.from_dataframe(df_pts, bandwidth=3000)

    #01-show spatial weights structure
    fig, ax = plt.subplots(1,4, figsize=(16,4))
    plot_spatial_weights(w_queen, df, ax=ax[0])
    ax[0].set_title('queen')
    
    plot_spatial_weights(w_rook, df, ax=ax[1])
    ax[1].set_title('rook')
    
    plot_spatial_weights(w_kernel_1k, df, ax=ax[2])
    ax[2].set_title('kernel 1k')
    
    plot_spatial_weights(w_kernel_2k, df, ax=ax[3])
    ax[3].set_title('kernel 2k')

    #02-show local environment
    def plot_local_environment(w, ax):
        from segregation.spatial.spatial_indexes import _build_local_environment
        d = _build_local_environment(df, groups_list, w)
        d['geometry'] = df.geometry
        d = gpd.GeoDataFrame(d)
        d.plot('Lung Cancer', k=6, scheme='quantiles', ax=ax)
        ax.axis('off')
    
    plt.figure()
    fig, axs = plt.subplots(1,4, figsize=(16,4))
    for i, wtype in enumerate([w_queen, w_rook, w_kernel_1k, w_kernel_2k]):
        plot_local_environment(w=wtype, ax=axs[i])

    #03-different local environments result in different segregation statistics
    #aspatial
    multiInfo=MultiInformationTheory(df, groups_list).statistic
    print("aspatial:",multiInfo)
    
    #rook neighborhood
    rookNeighborhood=SpatialInformationTheory(df, groups_list, w=w_rook).statistic
    print("rook neighborhood:",rookNeighborhood)

    #queen neighborhood
    queenNeighbor=SpatialInformationTheory(df, groups_list, w=w_queen).statistic
    print("queen neighborhood:",queenNeighbor)

    # 1 kilometer kernel distance neighborhood
    kernelDisNeighbor_a=SpatialInformationTheory(df, groups_list, w=w_kernel_1k).statistic
    print( "kernel distance neighborhood_A:",kernelDisNeighbor_a)
    
    # 2 kilometer kernel distance neighborhood
    kernelDisNeighbor_b=SpatialInformationTheory(df, groups_list, w=w_kernel_2k).statistic
    print("kernel distance neighborhood_B:",kernelDisNeighbor_b)
    
    distances = [1000.,2000.,3000.,4000.,5000.] # note these are floats [1000.,2000.,3000.,4000.,5000.]
    euclidian_profile = compute_segregation_profile(df_pts, groups=groups_list, distances=distances)
    print("euclidian_profile",euclidian_profile)
    
    #04-local street network can have a big impact on -
    df = df.to_crs(epsg=4326)
    # net = get_osm_network(df)
    # net.save_hdf5('dc_network.h5') #it can take awhile to download a street network, so you can save and read it back in using pandana
    dc_networkPath=r"C:\Users\richi\omen-richiebao\omen-code\Chicago_code\Chicago Health_spatioTemporalAnalysis\data\dc_network.h5"
    net = Network.from_hdf5(dc_networkPath)
    # net = Network.from_hdf5('dc_network.h5')
    #three different segregation profiles
    network_linear_profile = compute_segregation_profile(df_pts, groups=groups_list, network=net, distances=distances)
    network_exponential_profile = compute_segregation_profile(df_pts, groups=groups_list, network=net, distances=distances, decay='exp', precompute=False)
  
    plt.figure()
    fig, ax = plt.subplots(figsize=(12,8))
    ax.scatter(euclidian_profile.keys(), euclidian_profile.values(), c='green', label='euclidian exp')
    ax.plot(list(euclidian_profile.keys()), list(euclidian_profile.values()), c='green') #TypeError: float() argument must be a string or a number, not 'dict_keys'
    
    ax.scatter(network_linear_profile.keys(), network_linear_profile.values(), c='red', label='net linear')
    ax.plot(list(network_linear_profile.keys()), list(network_linear_profile.values()), c='red')
    
    ax.scatter(network_exponential_profile.keys(), network_exponential_profile.values(), c='blue', label='net exp')
    ax.plot(list(network_exponential_profile.keys()), list(network_exponential_profile.values()), c='blue')
    
    plt.xlabel('meters')
    plt.ylabel('SIT')
    
    plt.legend()
    plt.show()
Ejemplo n.º 2
0
def greedy(
    gdf,
    strategy="balanced",
    balance="count",
    min_colors=4,
    sw="queen",
    min_distance=None,
    silence_warnings=True,
    interchange=False,
):
    """
    Color GeoDataFrame using various strategies of greedy (topological) colouring.

    Attempts to color a GeoDataFrame using as few colors as possible, where no
    neighbours can have same color as the feature itself. Offers various strategies
    ported from QGIS or implemented within networkX for greedy graph coloring.

    ``greedy`` will return pandas.Series representing assinged color codes.

    Parameters
    ----------
    gdf : GeoDataFrame
        GeoDataFrame
    strategy : str (default 'balanced')
        Determine coloring strategy. Options are ``'balanced'`` for algorithm based on
        QGIS Topological coloring. It is aiming for a visual balance, defined by the
        balance parameter.

        Other options are those supported by networkx.greedy_color:

        * ``'largest_first'``
        * ``'random_sequential'``
        * ``'smallest_last'``
        * ``'independent_set'``
        * ``'connected_sequential_bfs'``
        * ``'connected_sequential_dfs'``
        * ``'connected_sequential'`` (alias for the previous strategy)
        * ``'saturation_largest_first'``
        * ``'DSATUR'`` (alias for the previous strategy)

        For details see
        https://networkx.github.io/documentation/stable/reference/algorithms/generated/networkx.algorithms.coloring.greedy_color.html

    balance : str (default 'count')
        If strategy is ``'balanced'``, determine the method of color balancing.

        * ``'count'`` attempts to balance the number of features per each color.
        * ``'area'`` attempts to balance the area covered by each color.
        * ``'centroid'`` attempts to balance the distance between colors based on the distance between centroids.
        * ``'distance'`` attempts to balance the distance between colors based on the distance between geometries. Slower than ``'centroid'``, but more precise.

        ``'centroid'`` and ``'distance'`` are significantly slower than other especially
        for larger GeoDataFrames.

        Apart from ``'count'``, all require CRS to be projected (not in degrees) to ensure
        metric values are correct.

    min_colors: int (default 4)
        If strategy is ``'balanced'``, define the minimal number of colors to be used.

    sw : 'queen', 'rook' or libpysal.weights.W (default 'queen')
        If min_distance is None, one can pass ``'libpysal.weights.W'`` object denoting neighbors
        or let greedy to generate one based on ``'queen'`` or ``'rook'`` contiguity.

    min_distance : float
        Set minimal distance between colors.

        If min_distance is not None, slower algorithm for generating spatial weghts is used
        based on intersection between geometries. Min_distance is then used as a tolerance
        of intersection.

    silence_warnings : bool (default True)
        Silence libpysal warnings when creating spatial weights.

    interchange : bool (defaul False)
        Use the color interchange algorithm (applicable for networkx strategies)

        For details see
        https://networkx.github.io/documentation/stable/reference/algorithms/generated/networkx.algorithms.coloring.greedy_color.html

    Examples
    --------
    Default:

    >>> gdf['greedy_colors'] = greedy(gdf)

    Balanced by area:

    >>> gdf['balanced_area'] = greedy(gdf, strategy='balanced',
    >>>                               balance='area')

    Using rook adjacency:

    >>> gdf['rook_adjacency'] = greedy(gdf, sw='rook')

    Adding minimal distance between colors:

    >>> gdf['min_distance'] = greedy(gdf, min_distance=100)

    Using different coloring strategy:

    >>> gdf['smallest_last'] = greedy(gdf, strategy='smallest_last')


    Returns
    -------
    color : pd.Series
        pandas.Series representing assinged color codes
    """
    if min_distance is not None:
        sw = _geos_sw(gdf,
                      tolerance=min_distance,
                      silence_warnings=silence_warnings)

    if not isinstance(sw, W):
        if sw == "queen":
            sw = Queen.from_dataframe(gdf,
                                      ids=gdf.index.to_list(),
                                      silence_warnings=silence_warnings)
        elif sw == "rook":
            sw = Rook.from_dataframe(gdf,
                                     ids=gdf.index.to_list(),
                                     silence_warnings=silence_warnings)

    if strategy == "balanced":
        return pd.Series(
            _balanced(gdf, sw, balance=balance, min_colors=min_colors))

    elif strategy in STRATEGIES:
        color = nx.greedy_color(sw.to_networkx(),
                                strategy=strategy,
                                interchange=interchange)
        color = pd.Series(color).sort_index()
        color.index = gdf.index
        return color

    else:
        raise ValueError("{} is not a valid strategy.".format(strategy))
Ejemplo n.º 3
0
import sys
import os
import libpysal
import geopandas
from libpysal.weights import Queen, Rook, KNN
import matplotlib.pyplot as plt

sys.path.append(os.path.abspath('..'))

libpysal.examples.available()
libpysal.examples.explain('mexico')
pth = libpysal.examples.get_path("mexicojoin.shp")
gdf = geopandas.read_file(pth)
ax = gdf.plot(edgecolor='grey', facecolor='w')
ax.set_axis_off()
w_rook = Rook.from_dataframe(gdf)
f, ax = w_rook.plot(gdf,
                    ax=ax,
                    edge_kws=dict(color='r', linestyle=':', linewidth=1),
                    node_kws=dict(marker=''))
ax.set_axis_off()
gdf.head()
w_queen = Queen.from_dataframe(gdf)
plt.show()

ax = gdf.plot(edgecolor='grey', facecolor='w')
f, ax = w_queen.plot(gdf,
                     ax=ax,
                     edge_kws=dict(color='r', linestyle=':', linewidth=1),
                     node_kws=dict(marker=''))
ax.set_axis_off()