05 dggal
DGGALPandas key features¶
You can try out vgridpandas by using the cloud-computing platforms below without having to install anything on your computer:
Full VgridPandas DGGS documentation is available at vgridpandas document.
To work with Vgrid in Python or CLI, use vgrid package. Full Vgrid DGGS documentation is available at vgrid document.
To work with Vgrid DGGS in QGIS, install the Vgrid Plugin.
To visualize DGGS in Maplibre GL JS, try the vgrid-maplibre library.
For an interactive demo, visit the Vgrid Homepage.
Install vgridpandas¶
Uncomment the following line to install vgridpandas.
In [ ]:
Copied!
# %pip install vgridpandas
# %pip install vgridpandas
Import dggalpandas¶
In [ ]:
Copied!
from vgridpandas import dggalpandas
from vgridpandas import dggalpandas
Latlon to DGGAL¶
Pandas¶
In [ ]:
Copied!
import pandas as pd
dggs_type = "isea9r"
df = pd.read_csv('https://raw.githubusercontent.com/opengeoshub/vopendata/main/csv/housing.csv')
resolution = 4
df = df.dggal.latlon2dggal(dggs_type, resolution, lat_col='lat', lon_col='lon')
df
import pandas as pd
dggs_type = "isea9r"
df = pd.read_csv('https://raw.githubusercontent.com/opengeoshub/vopendata/main/csv/housing.csv')
resolution = 4
df = df.dggal.latlon2dggal(dggs_type, resolution, lat_col='lat', lon_col='lon')
df
Out[ ]:
| lon | lat | housing_median_age | total_rooms | total_bedrooms | population | households | median_income | median_house_value | ocean_proximity | dggal_isea9r | dggal_isea9r_res | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | -122.23 | 37.88 | 41 | 880 | 129.0 | 322 | 126 | 8.3252 | 452600 | NEAR BAY | E0-1091 | 4 |
| 1 | -122.22 | 37.86 | 21 | 7099 | 1106.0 | 2401 | 1138 | 8.3014 | 358500 | NEAR BAY | E0-1091 | 4 |
| 2 | -122.24 | 37.85 | 52 | 1467 | 190.0 | 496 | 177 | 7.2574 | 352100 | NEAR BAY | E0-1091 | 4 |
| 3 | -122.25 | 37.85 | 52 | 1274 | 235.0 | 558 | 219 | 5.6431 | 341300 | NEAR BAY | E0-1091 | 4 |
| 4 | -122.25 | 37.85 | 52 | 1627 | 280.0 | 565 | 259 | 3.8462 | 342200 | NEAR BAY | E0-1091 | 4 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 20635 | -121.09 | 39.48 | 25 | 1665 | 374.0 | 845 | 330 | 1.5603 | 78100 | INLAND | E0-1041 | 4 |
| 20636 | -121.21 | 39.49 | 18 | 697 | 150.0 | 356 | 114 | 2.5568 | 77100 | INLAND | E0-1041 | 4 |
| 20637 | -121.22 | 39.43 | 17 | 2254 | 485.0 | 1007 | 433 | 1.7000 | 92300 | INLAND | E0-1041 | 4 |
| 20638 | -121.32 | 39.43 | 18 | 1860 | 409.0 | 741 | 349 | 1.8672 | 84700 | INLAND | E0-1041 | 4 |
| 20639 | -121.24 | 39.37 | 16 | 2785 | 616.0 | 1387 | 530 | 2.3886 | 89400 | INLAND | E0-1041 | 4 |
20640 rows × 12 columns
GeoPandas¶
In [ ]:
Copied!
import geopandas as gpd
df = gpd.read_file('https://raw.githubusercontent.com/opengeoshub/vopendata/main/shape/housing.geojson')
resolution = 4
df = df.dggal.latlon2dggal(dggs_type, resolution)
df
import geopandas as gpd
df = gpd.read_file('https://raw.githubusercontent.com/opengeoshub/vopendata/main/shape/housing.geojson')
resolution = 4
df = df.dggal.latlon2dggal(dggs_type, resolution)
df
Out[ ]:
| longitude | latitude | housing_median_age | total_rooms | total_bedrooms | population | households | median_income | median_house_value | ocean_proximity | geometry | dggal_isea9r | dggal_isea9r_res | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | -122.23 | 37.88 | 41.0 | 880.0 | 129.0 | 322.0 | 126.0 | 8.3252 | 452600.0 | NEAR BAY | POINT (-122.23 37.88) | E0-1091 | 4 |
| 1 | -122.22 | 37.86 | 21.0 | 7099.0 | 1106.0 | 2401.0 | 1138.0 | 8.3014 | 358500.0 | NEAR BAY | POINT (-122.22 37.86) | E0-1091 | 4 |
| 2 | -122.24 | 37.85 | 52.0 | 1467.0 | 190.0 | 496.0 | 177.0 | 7.2574 | 352100.0 | NEAR BAY | POINT (-122.24 37.85) | E0-1091 | 4 |
| 3 | -122.25 | 37.85 | 52.0 | 1274.0 | 235.0 | 558.0 | 219.0 | 5.6431 | 341300.0 | NEAR BAY | POINT (-122.25 37.85) | E0-1091 | 4 |
| 4 | -122.25 | 37.85 | 52.0 | 1627.0 | 280.0 | 565.0 | 259.0 | 3.8462 | 342200.0 | NEAR BAY | POINT (-122.25 37.85) | E0-1091 | 4 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 20635 | -121.09 | 39.48 | 25.0 | 1665.0 | 374.0 | 845.0 | 330.0 | 1.5603 | 78100.0 | INLAND | POINT (-121.09 39.48) | E0-1041 | 4 |
| 20636 | -121.21 | 39.49 | 18.0 | 697.0 | 150.0 | 356.0 | 114.0 | 2.5568 | 77100.0 | INLAND | POINT (-121.21 39.49) | E0-1041 | 4 |
| 20637 | -121.22 | 39.43 | 17.0 | 2254.0 | 485.0 | 1007.0 | 433.0 | 1.7000 | 92300.0 | INLAND | POINT (-121.22 39.43) | E0-1041 | 4 |
| 20638 | -121.32 | 39.43 | 18.0 | 1860.0 | 409.0 | 741.0 | 349.0 | 1.8672 | 84700.0 | INLAND | POINT (-121.32 39.43) | E0-1041 | 4 |
| 20639 | -121.24 | 39.37 | 16.0 | 2785.0 | 616.0 | 1387.0 | 530.0 | 2.3886 | 89400.0 | INLAND | POINT (-121.24 39.37) | E0-1041 | 4 |
20640 rows × 13 columns
DGGAL to geo¶
In [ ]:
Copied!
df = df.dggal.dggal2geo(dggs_type)
df.plot(edgecolor='white')
df = df.dggal.dggal2geo(dggs_type)
df.plot(edgecolor='white')
Out[ ]:
<Axes: >
(Multi)Polygon to DGGAL¶
In [ ]:
Copied!
import geopandas as gpd
gdf = gpd.read_file('https://raw.githubusercontent.com/opengeoshub/vopendata/main/shape/polygon.geojson')
resolution = 10
gdf_polyfill = gdf.dggal.polyfill(dggs_type, resolution, compact=True, predicate='intersects', explode=False)
gdf_polyfill = gdf_polyfill.dggal.dggal2geo(dggs_type)
gdf_polyfill.plot(edgecolor='white')
import geopandas as gpd
gdf = gpd.read_file('https://raw.githubusercontent.com/opengeoshub/vopendata/main/shape/polygon.geojson')
resolution = 10
gdf_polyfill = gdf.dggal.polyfill(dggs_type, resolution, compact=True, predicate='intersects', explode=False)
gdf_polyfill = gdf_polyfill.dggal.dggal2geo(dggs_type)
gdf_polyfill.plot(edgecolor='white')
Out[ ]:
<Axes: >
DGGAL binning¶
Pandas¶
In [ ]:
Copied!
import pandas as pd
resolution = 5
df = pd.read_csv('https://raw.githubusercontent.com/opengeoshub/vopendata/main/csv/housing.csv')
stats = 'max'
numeric_col = 'median_house_value'
df_bin = df.dggal.dggalbin(dggs_type, resolution,
lat_col='lat', lon_col='lon', stats=stats, numeric_col=numeric_col)
df_bin.plot(column=f'{numeric_col}_{stats}', cmap='Spectral_r', legend=True, linewidth=0.2)
import pandas as pd
resolution = 5
df = pd.read_csv('https://raw.githubusercontent.com/opengeoshub/vopendata/main/csv/housing.csv')
stats = 'max'
numeric_col = 'median_house_value'
df_bin = df.dggal.dggalbin(dggs_type, resolution,
lat_col='lat', lon_col='lon', stats=stats, numeric_col=numeric_col)
df_bin.plot(column=f'{numeric_col}_{stats}', cmap='Spectral_r', legend=True, linewidth=0.2)
Out[ ]:
<Axes: >
GeoPandas¶
In [ ]:
Copied!
import geopandas as gpd
resolution = 5
df = gpd.read_file('https://raw.githubusercontent.com/opengeoshub/vopendata/main/shape/housing.geojson')
stats = 'max'
numeric_col = 'median_house_value'
df_bin = df.dggal.dggalbin(dggs_type, resolution, stats=stats, numeric_col=numeric_col)
df_bin.plot(column=f'{numeric_col}_{stats}', cmap='Spectral_r', legend=True, linewidth=0.2)
df_bin
import geopandas as gpd
resolution = 5
df = gpd.read_file('https://raw.githubusercontent.com/opengeoshub/vopendata/main/shape/housing.geojson')
stats = 'max'
numeric_col = 'median_house_value'
df_bin = df.dggal.dggalbin(dggs_type, resolution, stats=stats, numeric_col=numeric_col)
df_bin.plot(column=f'{numeric_col}_{stats}', cmap='Spectral_r', legend=True, linewidth=0.2)
df_bin
Out[ ]:
| dggal_isea9r | median_house_value_max | geometry | |
|---|---|---|---|
| 0 | F0-8536 | 122400.0 | POLYGON ((-124.37085 41.87418, -124.36945 41.8... |
| 1 | F0-862A | 152700.0 | POLYGON ((-124.03073 41.78422, -124.02937 41.7... |
| 2 | F0-871C | 62800.0 | POLYGON ((-124.28748 41.30204, -124.2861 41.29... |
| 3 | F0-871D | 71100.0 | POLYGON ((-123.99036 41.49796, -123.98903 41.4... |
| 4 | F0-871E | 65400.0 | POLYGON ((-123.69148 41.69206, -123.69018 41.6... |
| ... | ... | ... | ... |
| 410 | F0-B0F9 | 159900.0 | POLYGON ((-115.84571 32.65528, -115.84419 32.6... |
| 411 | F0-B0FA | 146300.0 | POLYGON ((-115.59144 32.81524, -115.5899 32.80... |
| 412 | F0-B0FC | 57500.0 | POLYGON ((-115.08432 33.13493, -115.08272 33.1... |
| 413 | F0-B1F0 | 25000.0 | POLYGON ((-114.78372 32.99478, -114.78217 32.9... |
| 414 | F0-B2E3 | 56100.0 | POLYGON ((-114.73816 32.69557, -114.73668 32.6... |
415 rows × 3 columns