%matplotlib inline
import geopandas as gpd
import contextily as ctx
import matplotlib.pyplot as plt
from shapely.geometry import Polygon
google = "http://www.google.cn/maps/vt?lyrs=s@189&gl=cn&x=tileX&y=tileY&z=tileZ"
Primero, vamos a cargar el grid con celdas donde hay algún edificio:
db = gpd.read_file('../../01 AUDES/grid/grid.shp')
db.head()
index | N. Buildin | geometry | |
---|---|---|---|
0 | 0 | 4.0 | POLYGON ((122907.233 4858659.138, 122907.233 4... |
1 | 1 | 1.0 | POLYGON ((123107.233 4858659.138, 123107.233 4... |
2 | 2 | 2.0 | POLYGON ((123307.233 4858659.138, 123307.233 4... |
3 | 3 | 5.0 | POLYGON ((123507.233 4858659.138, 123507.233 4... |
4 | 4 | 1.0 | POLYGON ((123107.233 4858459.138, 123107.233 4... |
La celda con más edificios puede ser identificada seleccionando la fila con el mayor valor en la columna N. Buildings
:
densest = db.loc[db['N. Buildin'] == db['N. Buildin'].max(), ]
densest
index | N. Buildin | geometry | |
---|---|---|---|
667547 | 667547 | 506.0 | POLYGON ((290107.233 4444859.138, 290107.233 4... |
def plot_cell(densest, figsize=(9, 9), ax=None):
if ax is None:
f, ax = plt.subplots(1, figsize=figsize)
densest.to_crs(epsg=3857)\
.plot(facecolor='none', edgecolor='red', linewidth=5, ax=ax)
ctx.add_basemap(ax, zoom=18, url=google,
attribution='Imagery by Google Tile Map Service')
minX, maxX, minY, maxY = ax.axis()
p = densest.to_crs(epsg=3857).iloc[0].geometry
shade = Polygon(shell=((minX, minY), (maxX, minY),
(maxX, maxY), (minX, maxY),
(minX, minY)),
holes=[tuple(p.exterior.coords)])
gpd.GeoSeries(shade, crs={'init': 'epsg:3857'}).plot(ax=ax, color='k', alpha=0.5)
ax.set_axis_off()
return None
plot_cell(densest)
Usando la misma lógica, podemos tambien explorar el top 9 (encaja mejor en una matriz 3x3):
top9 = db.sort_values('N. Buildin', ascending=False)\
.head(10)
top9
index | N. Buildin | geometry | |
---|---|---|---|
667547 | 667547 | 506.0 | POLYGON ((290107.233 4444859.138, 290107.233 4... |
1300607 | 1300607 | 452.0 | POLYGON ((278707.233 4001259.138, 278707.233 4... |
662988 | 662988 | 433.0 | POLYGON ((244107.233 4447259.138, 244107.233 4... |
1269302 | 1269302 | 433.0 | POLYGON ((438107.233 4065259.138, 438107.233 4... |
53 | 53 | 431.0 | POLYGON ((107907.233 4854659.138, 107907.233 4... |
653453 | 653453 | 418.0 | POLYGON ((228507.233 4453459.138, 228507.233 4... |
663047 | 663047 | 418.0 | POLYGON ((309107.233 4447259.138, 309107.233 4... |
1259582 | 1259582 | 412.0 | POLYGON ((414907.233 4068459.138, 414907.233 4... |
629542 | 629542 | 409.0 | POLYGON ((443507.233 4469659.138, 443507.233 4... |
1284000 | 1284000 | 408.0 | POLYGON ((325507.233 4050259.138, 325507.233 4... |
Y las imagenes en un grid:
f, axs = plt.subplots(3, 3, figsize=(12, 12))
axs = axs.flatten()
for i in range(axs.shape[0]):
plot_cell(top9.iloc[[i], :], ax=axs[i])
plt.subplots_adjust(wspace=0, hspace=0)
plt.savefig('top9.png', dpi=300)
plt.show()
! du -h top9.png
14M top9.png