{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f5a936e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                 name           geometry   lon   lat\n",
      "0  244146921975218925  POINT (20.5 57.5)  20.5  57.5\n",
      "1  244880873744371191      POINT (21 58)  21.0  58.0\n"
     ]
    }
   ],
   "source": [
    "\n",
    "from dggrid4py import tool, DGGRIDv8\n",
    "\n",
    "# create an initial instance that knows where the dggrid tool lives, configure temp workspace and log/stdout output\n",
    "# if you have \n",
    "\n",
    "dggrid_exec = tool.get_portable_executable(\".\")\n",
    "\n",
    "dggrid_instance = DGGRIDv8(\n",
    "    executable=dggrid_exec,\n",
    "    working_dir=\".\",\n",
    "    capture_logs=True,\n",
    "    silent=True,\n",
    "    has_gdal=False,\n",
    "    tmp_geo_out_legacy=True,\n",
    "    debug=False,\n",
    ")\n",
    "\n",
    "import geopandas\n",
    "import shapely\n",
    "points = [shapely.Point(20.5, 57.5), shapely.Point(21.0, 58.0)]\n",
    "geodf_points_wgs84 = geopandas.GeoDataFrame({'name': ['A', 'B']}, geometry=points, crs='EPSG:4326')\n",
    "gdf4 = dggrid_instance.cells_for_geo_points(geodf_points_wgs84, True,'IGEO7', 20, \n",
    "                                            output_address_type='Z7')\n",
    "                                            # output_address_type='HIERNDX',\n",
    "                                            # output_hier_ndx_system='Z3', output_hier_ndx_form ='DIGIT_STRING')\n",
    "print(gdf4.head())\n",
    "\n",
    "\n",
    "# global ISEA4T grid at resolution 5 into GeoDataFrame to Shapefile\n",
    "# gdf1 = dggrid_instance.grid_cell_polygons_for_extent('ISEA3H', 0, split_dateline=True)\n",
    "# gdf1 = gdf1.set_crs(\"EPSG:4326\")      # or gdf1.set_crs(4326)\n",
    "# print(gdf1.head())\n",
    "    # gdf1.to_file('isea4t_5.geojson')\n",
    "# gdf_centroids = dggrid_instance.grid_cell_centroids_for_extent(dggs_type='ISEA7H', resolution=4, mixed_aperture_level=None, clip_geom=None)\n",
    "\n",
    "# # clip extent\n",
    "# clip_bound = shapely.geometry.box(20.2,57.00, 28.4,60.0)\n",
    "\n",
    "# # ISEA7H grid at resolution 9, for extent of provided WGS84 rectangle into GeoDataFrame to Shapefile\n",
    "# gdf3 = dggrid_instance.grid_cell_polygons_for_extent('ISEA7H', 9, clip_geom=clip_bound)\n",
    "# print(gdf3.head())\n",
    "# gdf3.to_file('grids/est_shape_isea7h_9.shp')\n",
    "\n",
    "# # generate cell and areal statistics for a ISEA7H grids from resolution 0 to 8 (return a pandas DataFrame)\n",
    "# df1 = dggrid_instance.grid_stats_table('ISEA7H', 8)\n",
    "# print(df1.head(8))\n",
    "# df1.to_csv('isea7h_8_stats.csv', index=False)\n",
    "\n",
    "# # generate the DGGS grid cells that would cover a GeoDataFrame of points, return Polygons with cell IDs as GeoDataFrame\n",
    "# points = [shapely.Point(20.5, 57.5), shapely.Point(21.0, 58.0)]\n",
    "# geodf_points_wgs84 = geopandas.GeoDataFrame({'name': ['A', 'B']}, geometry=points, crs='EPSG:4326')\n",
    "# gdf4 = dggrid_instance.cells_for_geo_points(geodf_points_wgs84, False, 'ISEA7H', 5)\n",
    "# print(gdf4.head())\n",
    "# gdf4.to_file('polycells_from_points_isea7h_5.shp')\n",
    "\n",
    "# # generate the DGGS grid cells that would cover a GeoDataFrame of points, return cell IDs added as column to the points GDF\n",
    "# gdf5 = dggrid_instance.cells_for_geo_points(geodf_points_wgs84=geodf_points_wgs84, cell_ids_only=True, dggs_type='ISEA4H', resolution=8)\n",
    "# print(gdf5.head())\n",
    "# gdf5.to_file('geopoint_cellids_from_points_isea4h_8.shp')\n",
    "\n",
    "# # generate DGGS grid cell polygons based on 'cell_id_list' (a list or np.array of provided cell_ids)\n",
    "# gdf6 = dggrid_instance.grid_cell_polygons_from_cellids(cell_id_list=[1, 4, 8], dggs_type='ISEA7H', resolution=5)\n",
    "# print(gdf6.head())\n",
    "# gdf6.to_file('from_seqnums_isea7h_5.shp')\n",
    "\n",
    "# # v0.2.6 API update split at dateline for cartesian GIS tools\n",
    "# gdf7 = dggrid_instance.grid_cell_polygons_for_extent('ISEA7H', 3, split_dateline=True)\n",
    "# gdf7.to_file('global_isea7h_3_interrupted.shp')\n",
    "\n",
    "# gdf_z1 = dggrid_instance.grid_cell_polygons_for_extent('IGEO7', 5, clip_geom=clip_bound, output_address_type='Z7_STRING')\n",
    "# print(gdf_z1.head(3))\n",
    "\n",
    "# df_z1 = dggrid_instance.guess_zstr_resolution(gdf_z1['name'].values, 'IGEO7', input_address_type='Z7_STRING')\n",
    "# print(df_z1.head(3))\n",
    "\n",
    "# df_q2di = dggrid_instance.address_transform(gdf_z1['name'].values, 'IGEO7', 5, input_address_type='Z7_STRING', output_address_type='Q2DI')\n",
    "# print(df_q2di.head(3))\n",
    "\n",
    "# df_tri = dggrid_instance.address_transform(gdf_z1['name'].values, 'IGEO7', 5, input_address_type='Z7_STRING', output_address_type='PROJTRI')\n",
    "# print(df_tri.head(3))\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
