-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_package.py
183 lines (154 loc) · 5.76 KB
/
export_package.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
"""
One of three scripts called by the Create gSSURGO File Geodatabase tool
from the RSS SSURGO Export Tool arctoolbox
This tool creates a SSURGO folder structure and exports the raster
as a geoTIFF.
Created on: 09/19/2024
@author: Alexander Stum
@maintainer: Alexander Stum
@title: GIS Specialist & Soil Scientist
@organization: National Soil Survey Center, USDA-NRCS
@email: [email protected]
@modified 09/19/2024
@by: Alexnder Stum
@version: 1.1
# ---
The orginal tool this is base off of is from the ArcMap Desktop toolbox
ArcGIS Desktop Build RSS gdb: Open Source Export. The tool creates
SSURGO Download structured directory package. It copies the contents of
tabular folder and creates a spatial sub directory and exports the raster
in the RSS database as geoTIFF.
Numpy Dostring format
"""
import arcpy
import os
import sys
import traceback
import shutil
def pyErr(func: str = None) -> str:
"""When a python exception is raised, this funciton
formats the traceback message.
Parameters
----------
func : str
The function that raised the python error exception
Returns
-------
str
Formatted python error message
"""
try:
etype, exc, tb = sys.exc_info()
tbinfo = traceback.format_tb(tb)[0]
tbinfo = '\t\n'.join(tbinfo.split(','))
msgs = (f"PYTHON ERRORS:\nIn function: {func}"
f"\nTraceback info:\n{tbinfo}\nError Info:\n\t{exc}")
return msgs
except:
return "Error in pyErr method"
def arcpyErr(func: str) -> str:
"""When an arcpy by exception is raised, this function formats the
message returned by arcpy.
Parameters
----------
func : str
The function that raised the arcpy error exception
Returns
-------
str
Formatted arcpy error message
"""
try:
etype, exc, tb = sys.exc_info()
line = tb.tb_lineno
msgs = (f"ArcPy ERRORS:\nIn function: {func}\non line: {line}"
f"\n\t{arcpy.GetMessages(2)}\n")
return msgs
except:
return "Error in arcpyErr method"
def main(args: list[str, str, str, int, str]) ->str:
"""This function packages the tif version of the RSS with the accompnaying
textfiles in a SSURGO export folder structure.
Parameters
----------
args : list[str, str, str, int, str]
The arguments sent from main:
- Path of the newly created RSS FGDB
- Path of the input tabular directory with textefiles
- State abbreviation
- Fiscal year of publication
- Name of the MURASTER feature in the FGDB
Returns
-------
str
If successful, returns the path of the newly created export
directory. Otherwise returns an empty string.
"""
try:
v = '1.1'
arcpy.AddMessage(f"\nExport Package, {v = !s}")
gdb_p = args[0] # input RSS gdb
input_p = args[1] # input tablular folder
st = args[2] # State
fy = args[3] # fiscal year of publication
raster_n = args[4] # MURASTER name
# Make export directory
out_p = os.path.dirname(gdb_p)
export_p = f"{out_p}/RSS_{st}"
os.mkdir(export_p)
# Add spatial and taubular sub directories
dirs = ['spatial', 'tabular']
for d in dirs:
os.mkdir(f"{export_p}/{d}")
# Export MURASTER as tif
out_r = f"{export_p}/spatial/{raster_n}.tif"
arcpy.management.CopyRaster(
f"{gdb_p}/{raster_n}", out_r, None, None, None, None, None,
"32_BIT_UNSIGNED"
)
# Copy over tabular textfiles from source
tab_out = f"{export_p}/tabular"
tabs_req = [
'ccancov.txt', 'ccrpyd.txt', 'cdfeat.txt', 'cecoclas.txt',
'ceplants.txt', 'cerosnac.txt', 'cfprod.txt', 'cfprodo.txt',
'cgeomord.txt', 'chaashto.txt', 'chconsis.txt', 'chdsuffx.txt',
'chfrags.txt', 'chorizon.txt', 'chpores.txt', 'chstr.txt',
'chstrgrp.txt', 'chtexgrp.txt', 'chtexmod.txt', 'chtext.txt',
'chtextur.txt', 'chunifie.txt', 'chydcrit.txt', 'cinterp.txt',
'cmonth.txt', 'comp.txt', 'cpmat.txt', 'cpmatgrp.txt',
'cpwndbrk.txt', 'crstrcts.txt', 'csfrags.txt', 'csmoist.txt',
'csmorgc.txt', 'csmorhpp.txt', 'csmormr.txt', 'csmorss.txt',
'cstemp.txt', 'ctext.txt', 'ctreestm.txt', 'ctxfmmin.txt',
'ctxfmoth.txt', 'ctxmoicl.txt', 'distimd.txt', 'distlmd.txt',
'distmd.txt', 'lareao.txt', 'legend.txt', 'ltext.txt',
'mapunit.txt', 'msdomdet.txt', 'msdommas.txt', 'msidxdet.txt',
'msidxmas.txt', 'msrsdet.txt', 'msrsmas.txt', 'mstab.txt',
'mstabcol.txt', 'muaggatt.txt', 'muareao.txt', 'mucrpyd.txt',
'mutext.txt', 'sacatlog.txt', 'sainterp.txt', 'sdvalgorithm.txt',
'sdvattribute.txt', 'sdvfolder.txt', 'sdvfolderattribute.txt',
'version.txt'
]
in_contents = os.listdir(input_p)
for f in os.scandir(input_p):
if f.is_file() and f.name in in_contents:
shutil.copy(f.path, f"{tab_out}/{f.name}")
tabs_req.remove(f.name)
if tabs_req:
arcpy.AddWarning(
f"\tThe following text files were not copied over to {tab_out}:"
)
for t in tabs_req:
arcpy.AddWarning(f"\t\t{t}")
return export_p
except arcpy.ExecuteError:
func = sys._getframe().f_code.co_name
arcpy.AddError(arcpyErr(func))
return ''
except:
func = sys._getframe().f_code.co_name
arcpy.AddError(pyErr(func))
return ''
if __name__ == '__main__':
main(*sys.argv[1:])