Skip to content

Commit

Permalink
cut towards 0.3 with Z7 etc
Browse files Browse the repository at this point in the history
  • Loading branch information
allixender committed Nov 19, 2024
1 parent b1a5dcd commit bbdd391
Show file tree
Hide file tree
Showing 11 changed files with 386 additions and 144 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ poetry.lock

tests/metafile_*
tests/dggrid_*
tests/temp_*

docs/source/generated/
test_script.py
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import shapely
from dggrid4py import DGGRIDv7

# create an inital instance that knows where the dggrid tool lives, configure temp workspace and log/stdout output
dggrid_instance = DGGRIDv7(executable='<path_to>/dggrid', working_dir='.', capture_logs=False, silent=False)
dggrid_instance = DGGRIDv7(executable='<path_to>/dggrid', working_dir='.', capture_logs=False, silent=False, tmp_geo_out_legacy=False, debug=False)


# global ISEA4T grid at resolution 5 into GeoDataFrame to Shapefile
gdf1 = dggrid_instance.grid_cell_polygons_for_extent('ISEA4T', 5)
Expand Down Expand Up @@ -69,13 +70,22 @@ gdf6.to_file('from_seqnums_isea7h_5.shp')
gdf7 = dggrid_instance.grid_cell_polygons_for_extent('ISEA7H', 3, split_dateline=True)
gdf7.to_file('global_isea7h_3_interrupted.shp')

gdf_z1 = dggrid_instance.grid_cell_polygons_for_extent('IGEO7', 5, clip_geom=est_bound, output_address_type='Z7_STRING')
print(gdf_z1.head(3))

df_z1 = dggrid_instance.guess_zstr_resolution(gdf_z1['name'].values, 'IGEO7', input_address_type='Z7_STRING')
print(df_z1.head(3))

df_q2di = dggrid_instance.address_transform(gdf_z1['name'].values, 'IGEO7', 5, input_address_type='Z7_STRING', output_address_type='Q2DI')
print(df_q2di.head(3))

df_tri = dggrid_instance.address_transform(gdf_z1['name'].values, 'IGEO7', 5, input_address_type='Z7_STRING', output_address_type='PROJTRI')
print(df_tri.head(3))

```

## TODO:

- sample vector values into dggs cells (aka binning)
- sample raster values into dggs cells

- get parent_for_cell_id at coarser resolution
- get children_for_cell_id at finer resolution

Expand Down
2 changes: 1 addition & 1 deletion dggrid4py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@

from .dggrid_runner import DGGRIDv7, Dggs, dgselect, dggs_types

__version__ = "0.2.9"
__version__ = "0.3.0"
362 changes: 279 additions & 83 deletions dggrid4py/dggrid_runner.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dggrid4py/interrupt.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def get_geom_coords(geometry):

wkt = geometry.wkt
wkt = wkt.replace("POLYGON ((","")
wkt = wkt.replace("POLYGON ))","")
wkt = wkt.replace("))","")
wkt = wkt.split(", ")

return wkt
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ pandas
fiona
shapely
geopandas
dggrid4py==0.2.8
dggrid4py==0.3.0

4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
copyright = '2024, Alexander Kmoch'
author = "Alexander Kmoch"

release = '0.2.8'
version = '0.2.8'
release = '0.3.0'
version = '0.3.0'

# -- General configuration

Expand Down
26 changes: 16 additions & 10 deletions docs/source/dggrid4py/dggrid4py.DGGRIDv7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,28 @@
.. autosummary::

~DGGRIDv7.__init__
~DGGRIDv7.cells_for_geo_points
~DGGRIDv7.dgapi_grid_gen
~DGGRIDv7.dgapi_grid_stats
~DGGRIDv7.dgapi_grid_transform
~DGGRIDv7.dgapi_point_value_binning
~DGGRIDv7.dgapi_pres_binning
~DGGRIDv7.is_runnable
~DGGRIDv7.check_gdal_support
~DGGRIDv7.post_process_split_dateline
~DGGRIDv7.run

~DGGRIDv7.grid_cell_polygons_for_extent
~DGGRIDv7.grid_cell_centroids_for_extent
~DGGRIDv7.grid_cell_polygons_from_cellids
~DGGRIDv7.grid_cell_centroids_from_cellids
~DGGRIDv7.grid_cellids_for_extent
~DGGRIDv7.grid_stats_table
~DGGRIDv7.is_runnable
~DGGRIDv7.check_gdal_support
~DGGRIDv7.post_process_split_dateline
~DGGRIDv7.run

~DGGRIDv7.dgapi_grid_gen
~DGGRIDv7.dgapi_grid_stats
~DGGRIDv7.dgapi_grid_transform
~DGGRIDv7.dgapi_point_value_binning
~DGGRIDv7.dgapi_pres_binning

~DGGRIDv7.cells_for_geo_points
~DGGRIDv7.guess_zstr_resolution
~DGGRIDv7.address_transform




Expand Down
21 changes: 15 additions & 6 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ comfortable geopython libraries, like shapely and geopandas
- grid_cellids_for_extent(): get_all_indexes/cell_ids for dggs at
resolution (clip or world)
- cells_for_geo_points(): poly_outline for point/centre at resolution
- address_transform(): conversion betwenn cell_id address types, like SEQNUM, Z7, or Q2DI

.. code:: python
Expand All @@ -35,7 +36,7 @@ comfortable geopython libraries, like shapely and geopandas
from dggrid4py import DGGRIDv7
# create an inital instance that knows where the dggrid tool lives, configure temp workspace and log/stdout output
dggrid_instance = DGGRIDv7(executable='<path_to>/dggrid', working_dir='.', capture_logs=False, silent=False)
dggrid_instance = DGGRIDv7(executable='<path_to>/dggrid', working_dir='.', capture_logs=False, silent=False, tmp_geo_out_legacy=False, debug=False)
# global ISEA4T grid at resolution 5 into GeoDataFrame to Shapefile
gdf1 = dggrid_instance.grid_cell_polygons_for_extent('ISEA4T', 5)
Expand Down Expand Up @@ -77,14 +78,22 @@ comfortable geopython libraries, like shapely and geopandas
gdf7 = dggrid_instance.grid_cell_polygons_for_extent('ISEA7H', 3, split_dateline=True)
gdf7.to_file('global_isea7h_3_interrupted.shp')
TODO
----
gdf_z1 = dggrid_instance.grid_cell_polygons_for_extent('IGEO7', 5, clip_geom=est_bound, output_address_type='Z7_STRING')
print(gdf_z1.head(3))
df_z1 = dggrid_instance.guess_zstr_resolution(gdf_z1['name'].values, 'IGEO7', input_address_type='Z7_STRING')
print(df_z1.head(3))
Contributions are welcome:
df_q2di = dggrid_instance.address_transform(gdf_z1['name'].values, 'IGEO7', 5, input_address_type='Z7_STRING', output_address_type='Q2DI')
print(df_q2di.head(3))
- sample vector values into dggs cells (aka binning)
df_tri = dggrid_instance.address_transform(gdf_z1['name'].values, 'IGEO7', 5, input_address_type='Z7_STRING', output_address_type='PROJTRI')
print(df_tri.head(3))
TODO
----

- sample raster values into dggs cells
Contributions are welcome

- get parent_for_cell_id at coarser resolution

Expand Down
90 changes: 55 additions & 35 deletions examples/demo_highlevel_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,46 @@ def highlevel_grid_gen_and_transform(dggrid_instance):
est_bound = shapely.geometry.box(20.2,57.00, 28.4,60.0 )

gdf1 = dggrid_instance.grid_cell_polygons_for_extent('ISEA4T', 5, clip_geom=est_bound)
# print(gdf1.head())
gdf1.to_file('/tmp/est_shape_isea4t_5.shp')
print(gdf1.head(3))
# gdf1.to_file('/tmp/est_shape_isea4t_5.shp')

gdf2 = dggrid_instance.grid_cell_polygons_for_extent('ISEA7H', 5, clip_geom=est_bound)
# print(gdf2.head())
gdf2.to_file('/tmp/est_shape_isea7h_5.shp')
print(gdf2.head(3))
# gdf2.to_file('/tmp/est_shape_isea7h_5.shp')

gdf2_a = dggrid_instance.grid_cell_polygons_for_extent('ISEA7H', 6, clip_geom=est_bound)
# print(gdf2_a.head())
gdf2_a.to_file('/tmp/est_shape_isea7h_6.shp')
print(gdf2_a.head(3))
# gdf2_a.to_file('/tmp/est_shape_isea7h_6.shp')

gdf3 = dggrid_instance.grid_cell_polygons_for_extent('ISEA7H', 8, clip_geom=est_bound)
gdf3 = dggrid_instance.grid_cell_polygons_for_extent('ISEA7H', 8, clip_geom=est_bound, output_address_type='Z7')
# print(gdf3.head())
gdf3.to_file('/tmp/est_shape_isea7h_8.shp')
# gdf3.to_file('/tmp/est_shape_isea7h_8.shp')

gdf3_a = dggrid_instance.grid_cell_polygons_for_extent('ISEA7H', 9, clip_geom=est_bound)
# print(gdf3.head())
gdf3_a.to_file('/tmp/est_shape_isea7h_9.shp')
gdf3_a = dggrid_instance.grid_cell_polygons_for_extent('ISEA3H', 9, clip_geom=est_bound, output_address_type='Z3_STRING')
print(gdf3.head(3))
# gdf3_a.to_file('/tmp/est_shape_isea7h_9.shp')

gdf_centroids = dggrid_instance.grid_cell_centroids_for_extent(dggs_type='ISEA7H', resolution=4, mixed_aperture_level=None, clip_geom=None)

df1 = dggrid_instance.grid_stats_table('ISEA7H', 8)
# print(df1.head(8))
df1.to_csv('/tmp/eisea7h_8_stats.csv', index=False)
df1 = dggrid_instance.grid_stats_table('ISEA7H', 20)
print(df1.head(8))
# df1.to_csv('/tmp/eisea7h_8_stats.csv', index=False)

df2 = dggrid_instance.grid_cellids_for_extent('ISEA7H', 5, clip_geom=est_bound)
# print(df2)
df2.to_csv('/tmp/est_isea7h_5_gridgen_from_seqnums.csv', index=False, header=None)
df2 = dggrid_instance.grid_cellids_for_extent('ISEA7H', 5, clip_geom=est_bound, output_address_type='SEQNUM')
print(df2.head(3))
# df2.to_csv('/tmp/est_isea7h_5_gridgen_from_seqnums.csv', index=False, header=None)

cell_list_est = df2[0].values
# # print(cell_list_est)
# # print(cell_list_est.shape)

gdf4 = dggrid_instance.grid_cell_polygons_from_cellids(cell_list_est, 'ISEA7H', 5)
# print(gdf4.head())
gdf4.to_file('/tmp/from_seqnums_isea7h_5.shp')
print(gdf4.head(3))
# gdf4.to_file('/tmp/from_seqnums_isea7h_5.shp')

gdf4 = dggrid_instance.grid_cell_polygons_from_cellids(cell_list_est, 'ISEA7H', 5, clip_subset_type='SEQNUMS', input_address_type='SEQNUM')
print(gdf4.head(3))
# gdf4.to_file('/tmp/from_seqnums_isea7h_5.shp')

gdf4['centroid_geo'] = gdf4['geometry'].centroid

Expand All @@ -71,16 +75,29 @@ def highlevel_grid_gen_and_transform(dggrid_instance):
geodf_points_wgs84 = gpd.GeoDataFrame(tgeo, geometry='geometry', crs=4326)

gdf5 = dggrid_instance.cells_for_geo_points(geodf_points_wgs84, False, 'ISEA7H', 5)
print(gdf5.head())
gdf5.to_file('/tmp/polycells_from_points_isea7h_5.shp')
print(gdf5.head(3))
# gdf5.to_file('/tmp/polycells_from_points_isea7h_5.shp')

gdf6 = dggrid_instance.cells_for_geo_points(geodf_points_wgs84, True, 'ISEA7H', 5)
print(gdf6.head())
gdf6.to_file('/tmp/geopoint_cellids_from_points_isea7h_5.shp')
print(gdf6.head(3))
# gdf6.to_file('/tmp/geopoint_cellids_from_points_isea7h_5.shp')

# v0.2.6 API update split at dateline for cartesian GIS tools
gdf7 = dggrid_instance.grid_cell_polygons_for_extent('ISEA7H', 3, split_dateline=True)
gdf7.to_file('/tmp/global_isea7h_3_interrupted.shp')
print(gdf7.head(3))
# gdf7.to_file('/tmp/global_isea7h_3_interrupted.shp')

gdf_z1 = dggrid_instance.grid_cell_polygons_for_extent('IGEO7', 5, clip_geom=est_bound, output_address_type='Z7_STRING')
print(gdf_z1.head(3))

df_z1 = dggrid_instance.guess_zstr_resolution(gdf_z1['name'].values, 'IGEO7', input_address_type='Z7_STRING')
print(df_z1.head(3))

df_q2di = dggrid_instance.address_transform(gdf_z1['name'].values, 'IGEO7', 5, input_address_type='Z7_STRING', output_address_type='Q2DI')
print(df_q2di.head(3))

df_tri = dggrid_instance.address_transform(gdf_z1['name'].values, 'IGEO7', 5, input_address_type='Z7_STRING', output_address_type='PROJTRI')
print(df_tri.head(3))


def highlevel_grid_stats(dggrid_instance):
Expand All @@ -96,7 +113,8 @@ def highlevel_grid_stats(dggrid_instance):
try:
print(f"{gridtype} - {15}")
df = dggrid_instance.grid_stats_table(dggs_type=gridtype, resolution=15, mixed_aperture_level=mixed_aperture_level, )
df.to_csv(f"/tmp/{gridtype}_{15}_stats.csv", index=False)
print(df.head(10))
# df.to_csv(f"/tmp/{gridtype}_{15}_stats.csv", index=False)
except ValueError as ex:
print(ex)
pass
Expand All @@ -108,18 +126,20 @@ def highlevel_grid_stats(dggrid_instance):

# DGGRID from https://github.com/sahrk/DGGRID
# with a /tmp dir, e.g. on Linux/Mac
dggrid = DGGRIDv7(executable='/usr/local/bin/dggrid', working_dir='/tmp', capture_logs=True, silent=False)
dggrid_path = '/usr/local/bin/dggrid'

highlevel_grid_gen_and_transform(dggrid)
import os

highlevel_grid_stats(dggrid)
if not os.environ['DGGRID_PATH'] is None:
dggrid_path = os.environ['DGGRID_PATH']

debug_mode = False
if not os.environ['DGGRID_DEBUG'] is None:
debug_mode = True if str(os.environ['DGGRID_DEBUG']).lower() == 'true' else False

"""
TODO:
dggrid = DGGRIDv7(executable=dggrid_path, working_dir='/tmp', capture_logs=True, silent=False)

- get parent_for_cell_id at coarser resolution
- get children_for_cell_id at finer resolution
highlevel_grid_gen_and_transform(dggrid)

# highlevel_grid_stats(dggrid)

- sample raster values into s2 dggs cells
- sample vector values into s2 dggs cells
"""
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dggrid4py"
version = "0.2.9"
version = "0.3.0"
description = "a Python library to run highlevel functions of DGGRIDv7 and v8"
authors = ["Alexander Kmoch <[email protected]>",
"Wai Tik Chan <[email protected]>",
Expand Down

0 comments on commit bbdd391

Please sign in to comment.