コード例 #1
0
def test_plot_spatial_weights():
    # get data
    gdf = gpd.read_file(examples.get_path('43MUE250GC_SIR.shp'))
    gdf.head()
    # calculate weights
    weights = Queen.from_dataframe(gdf)
    # plot weights
    fig, _ = plot_spatial_weights(weights, gdf)
    plt.close(fig)
    # calculate nonplanar_joins
    wnp = libpysal.weights.util.nonplanar_neighbors(weights, gdf)
    # plot new joins
    fig2, _ = plot_spatial_weights(wnp, gdf)
    plt.close(fig2)
    #customize
    fig3, _ = plot_spatial_weights(wnp,
                                   gdf,
                                   nonplanar_edge_kws=dict(color='#4393c3'))
    plt.close(fig3)
コード例 #2
0
def test_plot_spatial_weights():
    # get data
    gdf = gpd.read_file(examples.get_path('43MUE250GC_SIR.shp'))
    gdf.head()
    # calculate weights
    weights = Queen.from_dataframe(gdf)
    # plot weights
    fig, _ = plot_spatial_weights(weights, gdf)
    plt.close(fig)
    # calculate nonplanar_joins
    wnp = libpysal.weights.util.nonplanar_neighbors(weights, gdf)
    # plot new joins
    fig2, _ = plot_spatial_weights(wnp, gdf)
    plt.close(fig2)
    #customize
    fig3, _ = plot_spatial_weights(wnp,
                                   gdf,
                                   nonplanar_edge_kws=dict(color='#4393c3'))
    plt.close(fig3)
    # plot in existing figure
    fig4, axs = plt.subplots(1, 3)
    plot_spatial_weights(wnp, gdf, ax=axs[0])
    plt.close(fig4)

    # uses a column as the index for spatial weights object
    weights_index = Queen.from_dataframe(gdf, idVariable="CD_GEOCMU")
    fig, _ = plot_spatial_weights(weights_index, gdf, indexed_on="CD_GEOCMU")
    plt.close(fig)
コード例 #3
0
from libpysal.weights.contiguity import Queen
import libpysal
from libpysal import examples
import matplotlib.pyplot as plt
import geopandas as gpd
from splot.libpysal import plot_spatial_weights

examples.explain('tokyo')
gdf = gpd.read_file(examples.get_path('tokyomet262.shp'))
gdf.head()
weight = Queen.from_dataframe(gdf)
plot_spatial_weights(weight, gdf)
plt.show()
wnp = libpysal.weights.util.nonplanar_neighbors(weight, gdf)
plot_spatial_weights(wnp, gdf)
plt.show()
plot_spatial_weights(wnp, gdf, nonplanar_edge_kws=dict(color='#4393c3'))
plt.show()
コード例 #4
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()
コード例 #5
0
tess_c = gpd.read_file(path, layer="tess_c")

blg = pd.concat([blg_s, blg_c])
tess = pd.concat([tess_s, tess_c])


blg = blg.sort_values("uID")
blg.reset_index(inplace=True)
tess = tess.loc[tess["uID"].isin(blg["uID"])]

tess = tess.sort_values("uID")
tess.reset_index(inplace=True)

weights = libpysal.weights.contiguity.Queen.from_dataframe(tess)

f, ax = plt.subplots(figsize=(20, 10))
tess.plot(ax=ax)
plot_spatial_weights(weights, blg, ax=ax)
#plt.savefig(
#    "contiguity_diagram.svg",
#    dpi=300,
#    bbox_inches="tight",
#)


# In[ ]: