In [1]:
import sys
import os
In [2]:
sys.path.append(os.path.abspath('..'))
import libpysal
In [3]:
libpysal.examples.available()
Out[3]:
In [4]:
libpysal.examples.explain('mexico')
Out[4]:
In [5]:
import geopandas
pth = libpysal.examples.get_path("mexicojoin.shp")
gdf = geopandas.read_file(pth)
from libpysal.weights import Queen, Rook, KNN
In [6]:
%matplotlib inline
import matplotlib.pyplot as plt
In [7]:
ax = gdf.plot()
ax.set_axis_off()
In [8]:
w_rook = Rook.from_dataframe(gdf)
In [9]:
w_rook.n
Out[9]:
In [10]:
w_rook.pct_nonzero
Out[10]:
In [11]:
ax = gdf.plot(edgecolor='grey', facecolor='w')
f,ax = w_rook.plot(gdf, ax=ax,
edge_kws=dict(color='r', linestyle=':', linewidth=1),
node_kws=dict(marker=''))
ax.set_axis_off()
In [12]:
gdf.head()
Out[12]:
In [13]:
w_rook.neighbors[0] # the first location has two neighbors at locations 1 and 22
Out[13]:
In [14]:
gdf['NAME'][[0, 1,22]]
Out[14]:
So, Baja California Norte has 2 rook neighbors: Baja California Sur and Sonora.
Queen neighbors are based on a more inclusive condition that requires only a shared vertex between two states:
In [15]:
w_queen = Queen.from_dataframe(gdf)
In [16]:
w_queen.n == w_rook.n
Out[16]:
In [17]:
(w_queen.pct_nonzero > w_rook.pct_nonzero) == (w_queen.n == w_rook.n)
Out[17]:
In [18]:
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()
In [19]:
w_queen.histogram
Out[19]:
In [20]:
w_rook.histogram
Out[20]:
In [21]:
c9 = [idx for idx,c in w_queen.cardinalities.items() if c==9]
In [22]:
gdf['NAME'][c9]
Out[22]:
In [23]:
w_rook.neighbors[28]
Out[23]:
In [24]:
w_queen.neighbors[28]
Out[24]:
In [25]:
import numpy as np
f,ax = plt.subplots(1,2,figsize=(10, 6), subplot_kw=dict(aspect='equal'))
gdf.plot(edgecolor='grey', facecolor='w', ax=ax[0])
w_rook.plot(gdf, ax=ax[0],
edge_kws=dict(color='r', linestyle=':', linewidth=1),
node_kws=dict(marker=''))
ax[0].set_title('Rook')
ax[0].axis(np.asarray([-105.0, -95.0, 21, 26]))
ax[0].axis('off')
gdf.plot(edgecolor='grey', facecolor='w', ax=ax[1])
w_queen.plot(gdf, ax=ax[1],
edge_kws=dict(color='r', linestyle=':', linewidth=1),
node_kws=dict(marker=''))
ax[1].set_title('Queen')
ax[1].axis('off')
ax[1].axis(np.asarray([-105.0, -95.0, 21, 26]))
Out[25]:
In [26]:
w_knn = KNN.from_dataframe(gdf, k=4)
In [27]:
w_knn.histogram
Out[27]:
In [28]:
ax = gdf.plot(edgecolor='grey', facecolor='w')
f,ax = w_knn.plot(gdf, ax=ax,
edge_kws=dict(color='r', linestyle=':', linewidth=1),
node_kws=dict(marker=''))
ax.set_axis_off()
In [29]:
pth = libpysal.examples.get_path("mexicojoin.shp")
from libpysal.weights import Queen, Rook, KNN
In [30]:
w_queen = Queen.from_shapefile(pth)
In [31]:
w_rook = Rook.from_shapefile(pth)
In [32]:
w_knn1 = KNN.from_shapefile(pth)
The warning alerts us to the fact that using a first nearest neighbor criterion to define the neighbors results in a connectivity graph that has more than a single component. In this particular case there are 2 components which can be seen in the following plot:
In [33]:
ax = gdf.plot(edgecolor='grey', facecolor='w')
f,ax = w_knn1.plot(gdf, ax=ax,
edge_kws=dict(color='r', linestyle=':', linewidth=1),
node_kws=dict(marker=''))
ax.set_axis_off()
The two components are separated in the southern part of the country, with the smaller component to the east and the larger component running through the rest of the country to the west. For certain types of spatial analytical methods, it is necessary to have a adjacency structure that consists of a single component. To ensure this for the case of Mexican states, we can increase the number of nearest neighbors to three:
In [34]:
w_knn3 = KNN.from_shapefile(pth,k=3)
In [35]:
ax = gdf.plot(edgecolor='grey', facecolor='w')
f,ax = w_knn3.plot(gdf, ax=ax,
edge_kws=dict(color='r', linestyle=':', linewidth=1),
node_kws=dict(marker=''))
ax.set_axis_off()
In [36]:
from libpysal.weights import lat2W
In [37]:
w = lat2W(4,3)
In [38]:
w.n
Out[38]:
In [39]:
w.pct_nonzero
Out[39]:
In [40]:
w.neighbors
Out[40]:
In [41]:
rs = libpysal.examples.get_path('map_RS_BR.shp')
In [42]:
import geopandas as gpd
In [43]:
rs_df = gpd.read_file(rs)
wq = libpysal.weights.Queen.from_dataframe(rs_df)
In [44]:
len(wq.islands)
Out[44]:
In [45]:
wq[0]
Out[45]:
In [46]:
wf = libpysal.weights.fuzzy_contiguity(rs_df)
In [47]:
wf.islands
Out[47]:
In [48]:
wf[0]
Out[48]:
In [49]:
plt.rcParams["figure.figsize"] = (20,15)
ax = rs_df.plot(edgecolor='grey', facecolor='w')
f,ax = wq.plot(rs_df, ax=ax,
edge_kws=dict(color='r', linestyle=':', linewidth=1),
node_kws=dict(marker=''))
ax.set_axis_off()
In [53]:
ax = rs_df.plot(edgecolor='grey', facecolor='w')
f,ax = wf.plot(rs_df, ax=ax,
edge_kws=dict(color='r', linestyle=':', linewidth=1),
node_kws=dict(marker=''))
ax.set_title('Rio Grande do Sul: Nonplanar Weights')
ax.set_axis_off()
plt.savefig('rioGrandeDoSul.png')
In [ ]:
In [ ]: