Skip to content

Commit

Permalink
added functions to output a new geodatabase to make re-runs easier
Browse files Browse the repository at this point in the history
  • Loading branch information
tkchafin committed Apr 30, 2024
1 parent 0a5667f commit 0454886
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
Binary file added src/resistnet/data/test.gpkg
Binary file not shown.
15 changes: 12 additions & 3 deletions src/resistnet/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self):
try:
options, r = getopt.getopt(
sys.argv[1:],
'hp:g:s:s:T:P:G:s:m:i:c:t:F:d:D:f:b:C:v:V:Aa:o:Xj:n:',
'hp:g:s:s:T:P:G:s:m:i:c:t:F:d:D:f:b:C:v:V:Aa:o:Xj:n:O:',
["shp=", "help", "input=", "genmat=", "shapefile=",
"seed=", "procs=", "maxPop=", "maxpop=", "maxgen=", "maxGen=",
"size=", "popsize=", "mutpb=", "indpb=", "cxpb=", "tourn=",
Expand All @@ -30,7 +30,7 @@ def __init__(self):
"fixShape", "max_gens=", "max_gen=", "max_pop=", "varFile=",
"tournsize=", "tournSize=", "tSize=", "threads=",
"minWeight=", "min_weight=", "infer_origin=", "origin=",
"sizes=", "allSymmetric"
"sizes=", "allSymmetric", "gdf_out="
]
)
except getopt.GetoptError as err:
Expand All @@ -56,6 +56,7 @@ def __init__(self):
self.fitmetric = "aic"
self.network = None
self.predicted = False
self.output_driver = "GPKG"
self.inmat = None
self.shapefile = None
self.network = None
Expand Down Expand Up @@ -168,6 +169,12 @@ def __init__(self):
self.fitmetric = arg.lower()
elif opt in ('b', 'burn'):
self.burnin = int(arg)
elif opt in ("O", "gdf_out"):
arg_upper = str(arg).upper()
if arg_upper not in ["GPKG", "SHP", "GDB"]:
self.display_help(f"Invalid option {arg_upper} for \
option <--gdf_out>")
self.output_driver = arg_upper
elif opt == "dist_col":
self.dist_col = arg
elif opt == "infer":
Expand Down Expand Up @@ -288,7 +295,9 @@ def display_help(self, message=None):
" -t, --procs: Number of parallel processors\n"
" -X, --noPlot: Turn off plotting\n"
" -o, --out: Output file prefix\n"
" -h, --help: Displays help menu\n\n"
" -h, --help: Displays help menu\n"
" -O, --gdf_out : Output driver for annotated geodataframe \
(options \"SHP\", \"GPKG\", \"GDB\")\n\n"

"Aggregation options:\n"
" --edge_agg: Method for combining variables across segments\n"
Expand Down
28 changes: 28 additions & 0 deletions src/resistnet/resistance_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import traceback
import itertools
import pandas as pd
import geopandas as gpd
import pyogrio
import numpy as np
from sortedcontainers import SortedDict
import seaborn as sns
Expand Down Expand Up @@ -702,6 +704,32 @@ def parse_input_gen_mat(self, out=None):
)
sys.exit()

# NOTE: Later add option to join the resistance values
# Probably faster to just use momepy.nx_to_gdf...
def write_geodataframe(self, output_prefix, output_driver):
# extract original geodatabase
geoDF = pyogrio.read_dataframe(self.shapefile)
mask = geoDF[self.reachid_col].isin(list(self._edge_order))
geoDF = geoDF.loc[mask]

# write with user-provided output engine
gpd.options.io_engine = "pyogrio"
extension = {
"SHP": ".shp",
"GPKG": ".gpkg",
"GDB": ".gdb"
}.get(output_driver.upper(), ".gpkg") # Default to .gpkg
if output_driver.upper() == "SHP":
output_driver = "ESRI Shapefile"

output_path = f"{output_prefix}{extension}"

if output_driver == 'GDB' and not os.path.exists(output_path):
os.makedirs(output_path)

geoDF.to_file(output_path, driver=output_driver.upper())


def build_incidence_matrix(self, edge_id, out=None):
"""
Builds an incidence matrix for the network based on Dijkstra's
Expand Down

0 comments on commit 0454886

Please sign in to comment.