Skip to content

Commit

Permalink
replace shp with gpkg for AOI operations. natcap#1551
Browse files Browse the repository at this point in the history
  • Loading branch information
davemfish committed Feb 25, 2025
1 parent f067992 commit 830a5cf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
41 changes: 19 additions & 22 deletions src/natcap/invest/recreation/recmodel_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
}
},
"outputs": {
"PUD_results.shp": {
"PUD_results.gpkg": {
"about": gettext(
"Results of photo-user-days aggregations in the AOI."),
"geometries": spec_utils.POLYGONS,
Expand All @@ -230,7 +230,7 @@
}
}
},
"TUD_results.shp": {
"TUD_results.gpkg": {
"about": gettext(
"Results of twitter-user-days aggregations in the AOI."),
"geometries": spec_utils.POLYGONS,
Expand Down Expand Up @@ -283,7 +283,7 @@
}
}
},
"regression_data.shp": {
"regression_data.gpkg": {
"created_if": "compute_regression",
"about": gettext(
"AOI polygons with all the variables needed to compute a regression, "
Expand Down Expand Up @@ -328,7 +328,7 @@
"server. If these results are used in publication this hash "
"should be included with the results for reproducibility.")
},
"scenario_results.shp": {
"scenario_results.gpkg": {
"created_if": "scenario_predictor_table_path",
"about": gettext(
"Results of scenario, including the predictor data used in the "
Expand All @@ -354,7 +354,7 @@
"intermediate": {
"type": "directory",
"contents": {
"aoi.shp": {
"aoi.gpkg": {
"about": gettext(
"Copy of the input AOI, gridded if applicable."),
"fields": {},
Expand Down Expand Up @@ -396,11 +396,11 @@
}


# These are the expected extensions associated with an ESRI Shapefile
# as part of the ESRI Shapefile driver standard, but some extensions
# like .prj, .sbn, and .sbx, are optional depending on versions of the
# format: http://www.gdal.org/drv_shapefile.html
_ESRI_SHAPEFILE_EXTENSIONS = ['.prj', '.shp', '.shx', '.dbf', '.sbn', '.sbx']
# # These are the expected extensions associated with an ESRI Shapefile
# # as part of the ESRI Shapefile driver standard, but some extensions
# # like .prj, .sbn, and .sbx, are optional depending on versions of the
# # format: http://www.gdal.org/drv_shapefile.html
# _ESRI_SHAPEFILE_EXTENSIONS = ['.prj', '.shp', '.shx', '.dbf', '.sbn', '.sbx']

# Have 5 seconds between timed progress outputs
LOGGER_TIME_DELAY = 5
Expand All @@ -409,18 +409,18 @@
SCENARIO_RESPONSE_ID = 'pr_UD_EST'

_OUTPUT_BASE_FILES = {
'pud_results_path': 'PUD_results.shp',
'pud_results_path': 'PUD_results.gpkg',
'pud_monthly_table_path': 'PUD_monthly_table.csv',
'tud_results_path': 'TUD_results.shp',
'tud_results_path': 'TUD_results.gpkg',
'tud_monthly_table_path': 'TUD_monthly_table.csv',
'regression_vector_path': 'regression_data.shp',
'scenario_results_path': 'scenario_results.shp',
'regression_vector_path': 'regression_data.gpkg',
'scenario_results_path': 'scenario_results.gpkg',
'regression_summary': 'regression_summary.txt',
'regression_coefficients': 'regression_coefficients.csv',
}

_INTERMEDIATE_BASE_FILES = {
'local_aoi_path': 'aoi.shp',
'local_aoi_path': 'aoi.gpkg',
'compressed_aoi_path': 'aoi.zip',
'pud_compressed_userdays_path': 'pud_userdays.zip',
'tud_compressed_userdays_path': 'tud_userdays.zip',
Expand Down Expand Up @@ -642,12 +642,10 @@ def execute(args):
def _copy_aoi_no_grid(source_aoi_path, dest_aoi_path):
"""Copy a shapefile from source to destination"""
aoi_vector = gdal.OpenEx(source_aoi_path, gdal.OF_VECTOR)
driver = gdal.GetDriverByName('ESRI Shapefile')
driver = gdal.GetDriverByName('GPKG')
local_aoi_vector = driver.CreateCopy(
dest_aoi_path, aoi_vector)
gdal.Dataset.__swig_destroy__(local_aoi_vector)
local_aoi_vector = None
gdal.Dataset.__swig_destroy__(aoi_vector)
aoi_vector = None


Expand Down Expand Up @@ -778,16 +776,15 @@ def _grid_vector(vector_path, grid_type, cell_size, out_grid_vector_path):
cell_size (float): dimensions of the grid cell in the projected units
of ``vector_path``; if "square" then this indicates the side length,
if "hexagon" indicates the width of the horizontal axis.
out_grid_vector_path (string): path to the output ESRI shapefile
vector that contains a gridded version of ``vector_path``, this file
should not exist before this call
out_grid_vector_path (string): path to the output Geopackage
vector that contains a gridded version of ``vector_path``.
Returns:
None
"""
LOGGER.info("gridding aoi")
driver = gdal.GetDriverByName('ESRI Shapefile')
driver = gdal.GetDriverByName('GPKG')
if os.path.exists(out_grid_vector_path):
driver.Delete(out_grid_vector_path)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_recreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ def test_square_grid(self):
from natcap.invest.recreation import recmodel_client

out_grid_vector_path = os.path.join(
self.workspace_dir, 'square_grid_vector_path.shp')
self.workspace_dir, 'square_grid_vector_path.gpkg')

recmodel_client._grid_vector(
os.path.join(SAMPLE_DATA, 'andros_aoi.shp'), 'square', 20000.0,
Expand All @@ -1110,7 +1110,7 @@ def test_hex_grid(self):
from natcap.invest.recreation import recmodel_client

out_grid_vector_path = os.path.join(
self.workspace_dir, 'hex_grid_vector_path.shp')
self.workspace_dir, 'hex_grid_vector_path.gpkg')

recmodel_client._grid_vector(
os.path.join(SAMPLE_DATA, 'andros_aoi.shp'), 'hexagon', 20000.0,
Expand Down Expand Up @@ -1147,7 +1147,7 @@ def test_existing_gridded_aoi_shapefiles(self):
from natcap.invest.recreation import recmodel_client

out_grid_vector_path = os.path.join(
self.workspace_dir, 'hex_grid_vector_path.shp')
self.workspace_dir, 'hex_grid_vector_path.gpkg')

recmodel_client._grid_vector(
os.path.join(SAMPLE_DATA, 'andros_aoi.shp'), 'hexagon', 20000.0,
Expand Down

0 comments on commit 830a5cf

Please sign in to comment.