-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuncs_gpkg.py
159 lines (116 loc) · 4.64 KB
/
funcs_gpkg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# -*- coding: utf-8 -*-
"""
@author: Mino Sorribas
"""
import pandas as pd
import geopandas as gpd
#-----------------------------------------------------------------------------
# FUNCTIONS TO INSERT NEW COLUMNS IN BHO TABLE AND EXPORT AS GEOPACKAGE
#-----------------------------------------------------------------------------
def f_dicts_to_bho_gpkg(
gdf_tble_bho,
dict_bho_targets,
tkey = 'cotrecho',
simplify = True,
to_gpkg = True,
to_xlsx = True,
prefix = 'base',
suffix = '',
):
"""
Maps dictionary values into new columns of gdf_tble_bho
then, optional, export .GPKG and/or .XLSX
- each key of dict_bho_targets makes a new column
- each new column (pd.Series) has values mapped (.map) from the
inner target dictionaries {tkey:values}
where 'tkey is the target column name for the mapping
Args:
gdf_tble_bho (gpd.DataFrame) :: drainage table from BHO
dict_bho_targets(dict) :: dictionary of target dicionaries (see Usage)
tkey (string) :: column of gdf_tble_column used to map keys
simplify (bool) :: True to select some columns
'cotrecho','cobacia',
'nucomptrec','nuareacont','nuareamont',
'nutrjus','dedominial','nustrahler',
'nuordemcda','cocursodag','cocdadesag','nudistbact'
to_gpkg (bool) :: True to export geopackage
to_xlsx (bool) :: True to export table in xlsx format
prefix (str) :: <prefix>_mgbbhods_<suffix>.[xlsx/gpkg]
default = 'base'
suffix (str) :: <prefix>_mgbbhods_<suffix>.[xlsx/gpkg]
default = ''
Returns:
gdw (pd.DataFrame) :: gdf_tble_bho with new columns
Notes:
- it won't export .gpkg if there is 'no geometry' (only pd.DataFrame)
Usage:
example:
>> df_tble_bho_new = f_map_dicts_to_bho(df_tble_bho,dict_bho_targets)
where:
dict_bho_targets = {
'mini_t1': {cotrecho: value_mini_1,...,cotrecho_n:value_mini_n},
'mini_t2': {cotrecho: value_mini_1,...,cotrecho_n:value_mini_n},
'mini_t3': {cotrecho: value_mini_1,...,cotrecho_n:value_mini_n},
'q95': {cotrecho: value_q95_1,...cotrecho_n:value_q95_n}
'tipo': {cotrecho: value_tipo_1,...cotrecho_n:value_tipo_n,}
...}
OR
dict_bho_targets = {'mini_t1': dict_bho_mini_t1,
'mini_t2': dict_bho_mini_t2,
...
'tipo': dict_bho_solver,
'q95': dict_bho_q95,
...
}
"""
print(" Updating dataframe and exporting... ")
# make a "working" copy
#gdw = gdf_tble_bho.copy(deep=True)
gdw = gdf_tble_bho
# insert data from dictionaries into dataframe (use)
for k,v in dict_bho_targets.items():
target, dict_bho_target = k,v
print (" - make column {}".format(k),end='')
gdw[target] = gdw[tkey].map(dict_bho_target)
print (" done.")
#TODO: check .loc for caveats/slices
# filenames
file_xlsx = "{}_mgbbhods_{}.xlsx".format(prefix,suffix)
file_gpkg = "{}_mgbbhods_{}.gpkg".format(prefix,suffix)
# check geometry
if 'geometry' in gdw.columns:
flag_geom = True
else:
flag_geom = False
# simplify columns
allcols = list(gdw.columns)
newcols = list(dict_bho_targets.keys())
sel = allcols[:]
if simplify:
sel = ['cotrecho','cobacia',
'nucomptrec','nuareacont','nuareamont',
'nutrjus','dedominial','nustrahler',
'nuordemcda','cocursodag','cocdadesag','nudistbact'
]
sel = sel + ['geometry'] if flag_geom else sel
sel = sel + newcols
# selected columns
gdw = gdw[sel]
# export xlsx
if to_xlsx:
print(" - saving {}...".format(file_xlsx),end='')
if flag_geom:
gdw_xls = gdw.drop('geometry',axis=1)
gdw_xls.to_excel(file_xlsx)
else:
gdw.to_excel(file_xlsx)
print(" done.")
# export gpkg
if to_gpkg:
print(" - saving {}...".format(file_gpkg),end='')
if flag_geom:
gdw.to_file(file_gpkg,driver='GPKG')
print(" done.")
else:
print(" fail: missing geometry.")
return gdw