10 qtm
Working with QTM 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.
# %pip install vgrid --upgrade
latlon2qtm¶
from vgrid.conversion.latlon2dggs import latlon2qtm
lat = 10.775276
lon = 106.706797
res = 14
qtm_id = latlon2qtm(lat, lon, res)
qtm_id
'42012323131211'
QTM to Polygon¶
from vgrid.conversion.dggs2geo.qtm2geo import qtm2geo
qtm_geo = qtm2geo(qtm_id)
qtm_geo
QTM to GeoJSON¶
from vgrid.conversion.dggs2geo.qtm2geo import qtm2geojson
qtm_geojson = qtm2geojson(qtm_id)
# qtm_geojson
Vector to QTM¶
from vgrid.conversion.vector2dggs.vector2qtm import vector2qtm
file_path = "https://raw.githubusercontent.com/opengeoshub/vopendata/main/shape/polygon2.geojson"
vector_to_qtm = vector2qtm(
file_path,
resolution=17,
compact=True,
topology=False,
predicate="intersects",
output_format="gpd",
)
# Visualize the output
vector_to_qtm.plot(edgecolor="white")
Processing features: 100%|██████████| 1/1 [00:00<00:00, 2.61it/s]
<Axes: >
QTM Compact¶
from vgrid.conversion.dggscompact import qtmcompact
qtm_compacted = qtmcompact(vector_to_qtm, qtm_id="qtm", output_format="gpd")
qtm_compacted.plot(edgecolor="white")
<Axes: >
QTM Expand¶
from vgrid.conversion.dggscompact import qtmexpand
qtm_expanded = qtmexpand(
vector_to_qtm, resolution=18, qtm_id="qtm", output_format="gpd"
)
qtm_expanded.plot(edgecolor="white")
<Axes: >
QTM Binning¶
from vgrid.binning.qtmbin import qtmbin
file_path = (
"https://raw.githubusercontent.com/opengeoshub/vopendata/main/csv/dist1_pois.csv"
)
stats = "count"
qtm_bin = qtmbin(
file_path,
resolution=16,
stats=stats,
# numeric_field="confidence",
# category="category",
output_format="gpd",
)
qtm_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)
)
Generating QTM DGGS: 100%|██████████| 16/16 [00:00<00:00, 65.29it/s]
<Axes: >
Raster to QTM¶
Download and open raster¶
# %pip install folium
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)
WARNING [rasterio._env:368 open()] CPLE_AppDefined in PROJ: proj_create_from_database: Cannot find proj.db
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["World Geodetic System 1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST]]'), 'transform': Affine(2.6640125000199077e-06, 0.0, 106.708118755,
0.0, -2.6640136054383103e-06, 10.812568272)}
<Axes: >
Convert raster to QTM¶
from vgrid.conversion.raster2dggs.raster2qtm import raster2qtm
raster_to_qtm = raster2qtm(raster_file, resolution=23, output_format="gpd")
# Visualize the output
import folium
m = folium.Map(tiles="CartoDB positron", max_zoom=28)
qtm_layer = folium.GeoJson(
raster_to_qtm,
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=["qtm", "band_1", "band_2", "band_3"],
aliases=["QTM ID", "Band 1", "Band 2", "Band 3"],
style="""
background-color: white;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
""",
),
).add_to(m)
m.fit_bounds(qtm_layer.get_bounds())
# Display the map
m
Converting raster to QTM: 100%|██████████| 998/998 [00:01<00:00, 982.06 cells/s]
QTM Generator¶
from vgrid.generator.qtmgrid import qtmgrid
qtm_grid = qtmgrid(resolution=3, output_format="gpd")
# qtm_grid = qtmgrid(resolution=18,bbox=[106.699007, 10.762811, 106.717674, 10.778649],output_format="gpd")
qtm_grid.plot(edgecolor="white")
qtm_grid.to_file("qtm.geojson")
Generating QTM DGGS: 100%|██████████| 3/3 [00:00<00:00, 27.52it/s]
QTM Inspect¶
from vgrid.stats.qtmstats import qtminspect
resolution = 6
qtm_inspect = qtminspect(resolution)
qtm_inspect.head()
Generating QTM DGGS: 100%|██████████| 6/6 [00:05<00:00, 1.02it/s]
| qtm | resolution | center_lat | center_lon | avg_edge_len | cell_area | cell_perimeter | geometry | crossed | norm_area | ipq | zsc | cvh | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 6 | 45.0 | -135.0 | 1.000756e+07 | 6.375820e+13 | 3.002269e+07 | POLYGON ((-180 0, -90 0, -90 90, -180 90, -180... | False | 512.5 | 0.888888 | 0.882057 | 1.0 |
| 1 | 2 | 6 | 45.0 | -45.0 | 1.000756e+07 | 6.375820e+13 | 3.002269e+07 | POLYGON ((-90 0, 0 0, 0 90, -90 90, -90 0)) | False | 512.5 | 0.888888 | 0.882057 | 1.0 |
| 2 | 3 | 6 | 45.0 | 45.0 | 1.000756e+07 | 6.375820e+13 | 3.002269e+07 | POLYGON ((0 0, 90 0, 90 90, 0 90, 0 0)) | False | 512.5 | 0.888888 | 0.882057 | 1.0 |
| 3 | 4 | 6 | 45.0 | 135.0 | 1.000756e+07 | 6.375820e+13 | 3.002269e+07 | POLYGON ((90 0, 180 0, 180 90, 90 90, 90 0)) | False | 512.5 | 0.888888 | 0.882057 | 1.0 |
| 4 | 5 | 6 | -45.0 | -135.0 | 1.000756e+07 | 6.375820e+13 | 3.002269e+07 | POLYGON ((-180 -90, -90 -90, -90 0, -180 0, -1... | False | 512.5 | 0.888888 | 0.882057 | 1.0 |
QTM Normalized Area Histogram¶
from vgrid.stats.qtmstats import qtm_norm_area_hist
qtm_norm_area_hist(qtm_inspect)
Distribution of QTM Area Distortions¶
from vgrid.stats.qtmstats import qtm_norm_area
qtm_norm_area(qtm_inspect)
QTM IPQ Compactness Histogram¶
Isoperimetric Inequality (IPQ) Compactness (suggested by Osserman, 1978):
$$C_{IPQ} = \frac{4 \pi A}{p^2}$$ The range of the IPQ compactness metric is [0,1].
A circle represents the maximum compactness with a value of 1.
As shapes become more irregular or elongated, their compactness decreases toward 0.
from vgrid.stats.qtmstats import qtm_compactness_ipq_hist
qtm_compactness_ipq_hist(qtm_inspect)
Distribution of QTM IPQ Compactness¶
from vgrid.stats.qtmstats import qtm_compactness_ipq
qtm_compactness_ipq(qtm_inspect)
QTM Convex hull Compactness Histogram:¶
$$C_{CVH} = \frac{A}{A_{CVH}}$$
The range of the convex hull compactness metric is [0,1].
As shapes become more concave, their convex hull compactness decreases toward 0.
from vgrid.stats.qtmstats import qtm_compactness_cvh_hist
qtm_compactness_cvh_hist(qtm_inspect)
Distribution of QTM Convex hull Compactness¶
from vgrid.stats.qtmstats import qtm_compactness_cvh
qtm_compactness_cvh(qtm_inspect)
QTM Statistics¶
Characteristic Length Scale (CLS - suggested by Ralph Kahn): the diameter of a spherical cap of the same cell's area
from vgrid.stats import qtmstats
qtmstats("km")
| resolution | number_of_cells | avg_edge_len_km | avg_cell_area_km2 | cls_km | |
|---|---|---|---|---|---|
| 0 | 1 | 8 | 12134.383462 | 6.375820e+07 | 9209.090165 |
| 1 | 2 | 32 | 6067.191731 | 1.593955e+07 | 4528.782056 |
| 2 | 3 | 128 | 3033.595866 | 3.984888e+06 | 2255.434481 |
| 3 | 4 | 512 | 1516.797933 | 9.962219e+05 | 1126.612534 |
| 4 | 5 | 2048 | 758.398966 | 2.490555e+05 | 563.168635 |
| 5 | 6 | 8192 | 379.199483 | 6.226387e+04 | 281.567127 |
| 6 | 7 | 32768 | 189.599742 | 1.556597e+04 | 140.781415 |
| 7 | 8 | 131072 | 94.799871 | 3.891492e+03 | 70.390439 |
| 8 | 9 | 524288 | 47.399935 | 9.728730e+02 | 35.195186 |
| 9 | 10 | 2097152 | 23.699968 | 2.432182e+02 | 17.597589 |
| 10 | 11 | 8388608 | 11.849984 | 6.080456e+01 | 8.798794 |
| 11 | 12 | 33554432 | 5.924992 | 1.520114e+01 | 4.399397 |
| 12 | 13 | 134217728 | 2.962496 | 3.800285e+00 | 2.199698 |
| 13 | 14 | 536870912 | 1.481248 | 9.500713e-01 | 1.099849 |
| 14 | 15 | 2147483648 | 0.740624 | 2.375178e-01 | 0.549925 |
| 15 | 16 | 8589934592 | 0.370312 | 5.937945e-02 | 0.274962 |
| 16 | 17 | 34359738368 | 0.185156 | 1.484486e-02 | 0.137481 |
| 17 | 18 | 137438953472 | 0.092578 | 3.711216e-03 | 0.068741 |
| 18 | 19 | 549755813888 | 0.046289 | 9.278040e-04 | 0.034370 |
| 19 | 20 | 2199023255552 | 0.023144 | 2.319510e-04 | 0.017185 |
| 20 | 21 | 8796093022208 | 0.011572 | 5.798775e-05 | 0.008593 |
| 21 | 22 | 35184372088832 | 0.005786 | 1.449694e-05 | 0.004296 |
| 22 | 23 | 140737488355328 | 0.002893 | 3.624234e-06 | 0.002148 |
| 23 | 24 | 562949953421312 | 0.001447 | 9.060586e-07 | 0.001074 |