04 healpix
Working with HEALPix in Vgrid DGGS¶
Full Vgrid DGGS documentation is available at vgrid document.
To work with Vgrid DGGS directly in GeoPandas and Pandas, please use vgridpandas. Full Vgridpandas DGGS documentation is available at vgridpandas 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.
In [ ]:
Copied!
# %pip install vgrid --upgrade
# %pip install vgrid --upgrade
latlon2healpix¶
In [ ]:
Copied!
from vgrid.conversion.latlon2dggs import latlon2healpix
lat = 10.775276
lon = 180.0
res = 0
healpix_id = latlon2healpix(lat, lon, res)
healpix_id
from vgrid.conversion.latlon2dggs import latlon2healpix
lat = 10.775276
lon = 180.0
res = 0
healpix_id = latlon2healpix(lat, lon, res)
healpix_id
Out[ ]:
5
HEALPix to Polygon¶
In [2]:
Copied!
from vgrid.conversion.dggs2geo.healpix2geo import healpix2geo
healpix_geo = healpix2geo(healpix_id)
healpix_geo
from vgrid.conversion.dggs2geo.healpix2geo import healpix2geo
healpix_geo = healpix2geo(healpix_id)
healpix_geo
Out[2]:
HEALPix to GeoJSON¶
In [3]:
Copied!
from vgrid.conversion.dggs2geo.healpix2geo import healpix2geojson
healpix_geojson = healpix2geojson(healpix_id)
healpix_geojson
from vgrid.conversion.dggs2geo.healpix2geo import healpix2geojson
healpix_geojson = healpix2geojson(healpix_id)
healpix_geojson
Out[3]:
{'type': 'FeatureCollection',
'features': [{'type': 'Feature',
'geometry': {'type': 'Polygon',
'coordinates': (((90.0, 41.81031489577859),
(45.0, 0.0),
(90.0, -41.81031489577861),
(134.99999999999994, 0.0),
(90.0, 41.81031489577859)),)},
'properties': {'healpix': '5',
'resolution': 0,
'center_lat': -7.105427357601001e-15,
'center_lon': 89.99999999999997,
'avg_edge_len': 6462621.584028445,
'cell_area': 50810264447065.44,
'cell_perimeter': 25850486.33611378}}]}
Vector to HEALPix¶
(Multi)Point to HEALPix¶
In [4]:
Copied!
from vgrid.conversion.vector2dggs.vector2healpix import vector2healpix
import geopandas as gpd
file_path = (
"https://raw.githubusercontent.com/opengeoshub/vopendata/main/shape/point.geojson"
)
gdf = gpd.read_file(file_path)
point_to_healpix = vector2healpix(
gdf,
# resolution=9,
topology=True,
output_format="gpd",
)
point_to_healpix.plot()
from vgrid.conversion.vector2dggs.vector2healpix import vector2healpix
import geopandas as gpd
file_path = (
"https://raw.githubusercontent.com/opengeoshub/vopendata/main/shape/point.geojson"
)
gdf = gpd.read_file(file_path)
point_to_healpix = vector2healpix(
gdf,
# resolution=9,
topology=True,
output_format="gpd",
)
point_to_healpix.plot()
Processing features: 100%|██████████| 12/12 [00:00<00:00, 3906.52it/s]
Out[4]:
<Axes: >
(Multi)Polyline to HEALPix¶
In [8]:
Copied!
from vgrid.conversion.vector2dggs.vector2healpix import vector2healpix
file_path = (
"https://raw.githubusercontent.com/opengeoshub/vopendata/main/shape/polyline2.geojson"
)
polyline_to_healpix = vector2healpix(
file_path,
resolution=18,
output_format="gpd",
)
polyline_to_healpix.plot(edgecolor="white")
from vgrid.conversion.vector2dggs.vector2healpix import vector2healpix
file_path = (
"https://raw.githubusercontent.com/opengeoshub/vopendata/main/shape/polyline2.geojson"
)
polyline_to_healpix = vector2healpix(
file_path,
resolution=18,
output_format="gpd",
)
polyline_to_healpix.plot(edgecolor="white")
Processing features: 100%|██████████| 1/1 [03:55<00:00, 235.19s/it]
Out[8]:
<Axes: >
(Multi)Polygon to HEALPix¶
In [ ]:
Copied!
from vgrid.conversion.vector2dggs.vector2healpix import vector2healpix
file_path = (
"https://raw.githubusercontent.com/opengeoshub/vopendata/main/shape/polygon2.geojson"
)
vector_to_healpix = vector2healpix(
file_path,
resolution=18,
compact=False,
predicate="intersects",
output_format="gpd",
)
vector_to_healpix.plot(edgecolor="white")
from vgrid.conversion.vector2dggs.vector2healpix import vector2healpix
file_path = (
"https://raw.githubusercontent.com/opengeoshub/vopendata/main/shape/polygon2.geojson"
)
vector_to_healpix = vector2healpix(
file_path,
resolution=18,
compact=False,
predicate="intersects",
output_format="gpd",
)
vector_to_healpix.plot(edgecolor="white")
Processing features: 0%| | 0/1 [00:00<?, ?it/s]
HEALPix Compact¶
In [ ]:
Copied!
from vgrid.conversion.dggscompact.healpixcompact import healpixcompact
healpix_compacted = healpixcompact(
vector_to_healpix, healpix_id="healpix", output_format="gpd"
)
healpix_compacted.plot(edgecolor="white")
from vgrid.conversion.dggscompact.healpixcompact import healpixcompact
healpix_compacted = healpixcompact(
vector_to_healpix, healpix_id="healpix", output_format="gpd"
)
healpix_compacted.plot(edgecolor="white")
HEALPix Expand¶
In [ ]:
Copied!
from vgrid.conversion.dggscompact.healpixcompact import healpixexpand
healpix_expanded = healpixexpand(
vector_to_healpix, resolution=11, output_format="gpd"
)
healpix_expanded.plot(edgecolor="white")
from vgrid.conversion.dggscompact.healpixcompact import healpixexpand
healpix_expanded = healpixexpand(
vector_to_healpix, resolution=11, output_format="gpd"
)
healpix_expanded.plot(edgecolor="white")
Raster to HEALPix¶
In [13]:
Copied!
from vgrid.utils.io import download_file
import rasterio
from rasterio.plot import show
raster_url = (
"https://raw.githubusercontent.com/opengeoshub/vopendata/main/raster/rgb.tif"
)
raster_file = download_file(raster_url)
src = rasterio.open(raster_file, "r")
print(src.meta)
show(src)
from vgrid.utils.io import download_file
import rasterio
from rasterio.plot import show
raster_url = (
"https://raw.githubusercontent.com/opengeoshub/vopendata/main/raster/rgb.tif"
)
raster_file = download_file(raster_url)
src = rasterio.open(raster_file, "r")
print(src.meta)
show(src)
rgb.tif already exists. Skip downloading. Set overwrite=True to overwrite.
{'driver': 'GTiff', 'dtype': 'uint8', 'nodata': None, 'width': 240, 'height': 147, 'count': 3, 'crs': CRS.from_wkt('GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]'), 'transform': Affine(2.6640125000199077e-06, 0.0, 106.708118755,
0.0, -2.6640136054383103e-06, 10.812568272)}
Out[13]:
<Axes: >
In [23]:
Copied!
from vgrid.conversion.raster2dggs.raster2healpix import raster2healpix
raster_to_healpix = raster2healpix(
raster_file,
method="binning", # nearest, binning
stats="mean",
output_format="gpd",
)
# Visualize the output
import folium
m = folium.Map(tiles="CartoDB positron", max_zoom=28)
healpix_layer = folium.GeoJson(
raster_to_healpix,
style_function=lambda x: {
"fillColor": f"rgb({x['properties']['band_1']}, {x['properties']['band_2']}, {x['properties']['band_3']})",
"fillOpacity": 1,
"color": "black",
"weight": 1,
},
popup=folium.GeoJsonPopup(
fields=["healpix", "resolution", "band_1", "band_2", "band_3", "cell_area"],
aliases=[
"HEALPix ID",
"Resolution",
"Band 1",
"Band 2",
"Band 3",
"Area (m²)",
],
style="""
background-color: white;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
""",
),
).add_to(m)
m.fit_bounds(healpix_layer.get_bounds())
# Display the map
m
from vgrid.conversion.raster2dggs.raster2healpix import raster2healpix
raster_to_healpix = raster2healpix(
raster_file,
method="binning", # nearest, binning
stats="mean",
output_format="gpd",
)
# Visualize the output
import folium
m = folium.Map(tiles="CartoDB positron", max_zoom=28)
healpix_layer = folium.GeoJson(
raster_to_healpix,
style_function=lambda x: {
"fillColor": f"rgb({x['properties']['band_1']}, {x['properties']['band_2']}, {x['properties']['band_3']})",
"fillOpacity": 1,
"color": "black",
"weight": 1,
},
popup=folium.GeoJsonPopup(
fields=["healpix", "resolution", "band_1", "band_2", "band_3", "cell_area"],
aliases=[
"HEALPix ID",
"Resolution",
"Band 1",
"Band 2",
"Band 3",
"Area (m²)",
],
style="""
background-color: white;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
""",
),
).add_to(m)
m.fit_bounds(healpix_layer.get_bounds())
# Display the map
m
Cell size: 0.08638527081938627 m2 Nearest HEALPix resolution determined: 24 Method: binning Stats: mean
Binning raster blocks to HEALPix: 100%|██████████| 14/14 [00:00<00:00, 21.66 block/s] Converting raster to HEALPix: 100%|██████████| 19948/19948 [00:02<00:00, 9379.17 cells/s]
Out[23]:
Make this Notebook Trusted to load map: File -> Trust Notebook