01 h3
H3Pandas key features¶
You can try out vgridpandas by using the cloud-computing platforms below without having to install anything on your computer:
Install vgridpandas¶
In [1]:
Copied!
# %pip install vgridpandas
# %pip install vgridpandas
Latlong to H3¶
In [2]:
Copied!
import pandas as pd
from vgridpandas import h3pandas
df = pd.read_csv('https://github.com/uber-web/kepler.gl-data/raw/master/nyctrips/data.csv')
df = df.rename({'pickup_longitude': 'lon', 'pickup_latitude': 'lat'}, axis=1)[['lon', 'lat', 'passenger_count']]
df = df.head(100)
resolution = 8
df = df.h3.latlon2h3(resolution)
df.head()
import pandas as pd
from vgridpandas import h3pandas
df = pd.read_csv('https://github.com/uber-web/kepler.gl-data/raw/master/nyctrips/data.csv')
df = df.rename({'pickup_longitude': 'lon', 'pickup_latitude': 'lat'}, axis=1)[['lon', 'lat', 'passenger_count']]
df = df.head(100)
resolution = 8
df = df.h3.latlon2h3(resolution)
df.head()
Out[2]:
lon | lat | passenger_count | h3_res | |
---|---|---|---|---|
h3 | ||||
882a100d2dfffff | -73.993896 | 40.750111 | 1 | 8 |
882a100d2bfffff | -73.976425 | 40.739811 | 1 | 8 |
882a100d63fffff | -73.968704 | 40.754246 | 5 | 8 |
882a100e25fffff | -73.863060 | 40.769581 | 5 | 8 |
882a10089bfffff | -73.945541 | 40.779423 | 1 | 8 |
H3 to geo boundary¶
In [3]:
Copied!
df = df.h3.h32geo()
df.head()
df = df.h3.h32geo()
df.head()
Out[3]:
lon | lat | passenger_count | h3_res | geometry | |
---|---|---|---|---|---|
h3 | |||||
882a100d2dfffff | -73.993896 | 40.750111 | 1 | 8 | POLYGON ((-73.98804 40.75427, -73.99442 40.753... |
882a100d2bfffff | -73.976425 | 40.739811 | 1 | 8 | POLYGON ((-73.9748 40.74405, -73.98118 40.743,... |
882a100d63fffff | -73.968704 | 40.754246 | 5 | 8 | POLYGON ((-73.9689 40.75743, -73.97528 40.7563... |
882a100e25fffff | -73.863060 | 40.769581 | 5 | 8 | POLYGON ((-73.86237 40.77082, -73.86875 40.769... |
882a10089bfffff | -73.945541 | 40.779423 | 1 | 8 | POLYGON ((-73.94629 40.78183, -73.95267 40.780... |
(Multi)Linestring/ (Multi)Polygon to H3¶
In [4]:
Copied!
import geopandas as gpd
from vgridpandas import h3pandas
gdf = gpd.read_file('https://raw.githubusercontent.com/opengeoshub/vopendata/refs/heads/main/shape/polygon.geojson')
resolution = 10
gdf_polyfill = gdf.h3.polyfill(resolution, compact = True, predicate = "largest_overlap", explode = True)
gdf_polyfill.head()
gdf_polyfill = gdf_polyfill.h3.h32geo("h3")
gdf_polyfill.plot(edgecolor = "white")
import geopandas as gpd
from vgridpandas import h3pandas
gdf = gpd.read_file('https://raw.githubusercontent.com/opengeoshub/vopendata/refs/heads/main/shape/polygon.geojson')
resolution = 10
gdf_polyfill = gdf.h3.polyfill(resolution, compact = True, predicate = "largest_overlap", explode = True)
gdf_polyfill.head()
gdf_polyfill = gdf_polyfill.h3.h32geo("h3")
gdf_polyfill.plot(edgecolor = "white")
Out[4]:
<Axes: >
H3 point binning¶
In [5]:
Copied!
import pandas as pd
import geopandas as gpd
from vgridpandas import h3pandas
resolution = 10
df = pd.read_csv("https://raw.githubusercontent.com/opengeoshub/vopendata/refs/heads/main/csv/dist1_pois.csv")
# df = gpd.read_file("https://raw.githubusercontent.com/opengeoshub/vopendata/refs/heads/main/shape/dist1_pois.geojson")
df.head()
stats = "count"
df_bin = df.h3.h3bin(resolution=resolution, stats = stats,
# numeric_column="confidence",
# category_column="category",
return_geometry=True)
df_bin.plot(
column=stats, # numeric column to base the colors on
cmap='Spectral_r', # color scheme (matplotlib colormap)
legend=True,
linewidth=0.2 # boundary width (optional)
)
import pandas as pd
import geopandas as gpd
from vgridpandas import h3pandas
resolution = 10
df = pd.read_csv("https://raw.githubusercontent.com/opengeoshub/vopendata/refs/heads/main/csv/dist1_pois.csv")
# df = gpd.read_file("https://raw.githubusercontent.com/opengeoshub/vopendata/refs/heads/main/shape/dist1_pois.geojson")
df.head()
stats = "count"
df_bin = df.h3.h3bin(resolution=resolution, stats = stats,
# numeric_column="confidence",
# category_column="category",
return_geometry=True)
df_bin.plot(
column=stats, # numeric column to base the colors on
cmap='Spectral_r', # color scheme (matplotlib colormap)
legend=True,
linewidth=0.2 # boundary width (optional)
)
Out[5]:
<Axes: >