-
Notifications
You must be signed in to change notification settings - Fork 0
/
NASISpedons_Extract_Pedons_from_NASIS.py
1672 lines (1277 loc) · 78 KB
/
NASISpedons_Extract_Pedons_from_NASIS.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#-------------------------------------------------------------------------------
# Name: NASISpedons_Extract_Pedons_from_NASIS
#
# Author: Adolfo.Diaz
# e-mail: [email protected]
# phone: 608.662.4422 ext. 216
#
# Author: Jason.Nemecek
# e-mail: [email protected]
# phone: 608.662.4422 ext. 190
#
# Created: 7/04/2016
# Last Modified: 5/18/2017
# Copyright: (c) Adolfo.Diaz 2016
#-------------------------------------------------------------------------------
## ===================================================================================
class ExitError(Exception):
pass
## ===================================================================================
def AddMsgAndPrint(msg, severity=0):
# prints message to screen if run as a python script
# Adds tool message to the geoprocessor
#
#Split the message on \n first, so that if it's multiple lines, a GPMessage will be added for each line
try:
print msg
#for string in msg.split('\n'):
#Add a geoprocessing message (in case this is run as a tool)
if severity == 0:
arcpy.AddMessage(msg)
elif severity == 1:
arcpy.AddWarning(msg)
elif severity == 2:
arcpy.AddError("\n" + msg)
except:
pass
## ===================================================================================
def errorMsg():
try:
exc_type, exc_value, exc_traceback = sys.exc_info()
theMsg = "\t" + traceback.format_exception(exc_type, exc_value, exc_traceback)[1] + "\n\t" + traceback.format_exception(exc_type, exc_value, exc_traceback)[-1]
AddMsgAndPrint(theMsg,2)
except:
AddMsgAndPrint("Unhandled error in errorMsg method", 2)
pass
### ===================================================================================
def setScratchWorkspace():
""" This function will set the scratchWorkspace for the interim of the execution
of this tool. The scratchWorkspace is used to set the scratchGDB which is
where all of the temporary files will be written to. The path of the user-defined
scratchWorkspace will be compared to existing paths from the user's system
variables. If there is any overlap in directories the scratchWorkspace will
be set to C:\TEMP, assuming C:\ is the system drive. If all else fails then
the packageWorkspace Environment will be set as the scratchWorkspace. This
function returns the scratchGDB environment which is set upon setting the scratchWorkspace"""
try:
AddMsgAndPrint("\nSetting Scratch Workspace")
scratchWK = arcpy.env.scratchWorkspace
# -----------------------------------------------
# Scratch Workspace is defined by user or default is set
if scratchWK is not None:
# dictionary of system environmental variables
envVariables = os.environ
# get the root system drive
if envVariables.has_key('SYSTEMDRIVE'):
sysDrive = envVariables['SYSTEMDRIVE']
else:
sysDrive = None
varsToSearch = ['ESRI_OS_DATADIR_LOCAL_DONOTUSE','ESRI_OS_DIR_DONOTUSE','ESRI_OS_DATADIR_MYDOCUMENTS_DONOTUSE',
'ESRI_OS_DATADIR_ROAMING_DONOTUSE','TEMP','LOCALAPPDATA','PROGRAMW6432','COMMONPROGRAMFILES','APPDATA',
'USERPROFILE','PUBLIC','SYSTEMROOT','PROGRAMFILES','COMMONPROGRAMFILES(X86)','ALLUSERSPROFILE']
""" This is a printout of my system environmmental variables - Windows 7
-----------------------------------------------------------------------------------------
ESRI_OS_DATADIR_LOCAL_DONOTUSE C:\Users\adolfo.diaz\AppData\Local\
ESRI_OS_DIR_DONOTUSE C:\Users\ADOLFO~1.DIA\AppData\Local\Temp\6\arc3765\
ESRI_OS_DATADIR_MYDOCUMENTS_DONOTUSE C:\Users\adolfo.diaz\Documents\
ESRI_OS_DATADIR_COMMON_DONOTUSE C:\ProgramData\
ESRI_OS_DATADIR_ROAMING_DONOTUSE C:\Users\adolfo.diaz\AppData\Roaming\
TEMP C:\Users\ADOLFO~1.DIA\AppData\Local\Temp\6\arc3765\
LOCALAPPDATA C:\Users\adolfo.diaz\AppData\Local
PROGRAMW6432 C:\Program Files
COMMONPROGRAMFILES : C:\Program Files (x86)\Common Files
APPDATA C:\Users\adolfo.diaz\AppData\Roaming
USERPROFILE C:\Users\adolfo.diaz
PUBLIC C:\Users\Public
SYSTEMROOT : C:\Windows
PROGRAMFILES : C:\Program Files (x86)
COMMONPROGRAMFILES(X86) : C:\Program Files (x86)\Common Files
ALLUSERSPROFILE : C:\ProgramData
------------------------------------------------------------------------------------------"""
bSetTempWorkSpace = False
""" Iterate through each Environmental variable; If the variable is within the 'varsToSearch' list
list above then check their value against the user-set scratch workspace. If they have anything
in common then switch the workspace to something local """
for var in envVariables:
if not var in varsToSearch:
continue
# make a list from the scratch and environmental paths
varValueList = (envVariables[var].lower()).split(os.sep) # ['C:', 'Users', 'adolfo.diaz', 'AppData', 'Local']
scratchWSList = (scratchWK.lower()).split(os.sep) # [u'C:', u'Users', u'adolfo.diaz', u'Documents', u'ArcGIS', u'Default.gdb', u'']
# remove any blanks items from lists
if '' in varValueList: varValueList.remove('')
if '' in scratchWSList: scratchWSList.remove('')
# First element is the drive letter; remove it if they are
# the same otherwise review the next variable.
if varValueList[0] == scratchWSList[0]:
scratchWSList.remove(scratchWSList[0])
varValueList.remove(varValueList[0])
# obtain a similarity ratio between the 2 lists above
#sM = SequenceMatcher(None,varValueList,scratchWSList)
# Compare the values of 2 lists; order is significant
common = [i for i, j in zip(varValueList, scratchWSList) if i == j]
if len(common) > 0:
bSetTempWorkSpace = True
break
# The current scratch workspace shares 1 or more directory paths with the
# system env variables. Create a temp folder at root
if bSetTempWorkSpace:
AddMsgAndPrint("\tCurrent Workspace: " + scratchWK,0)
if sysDrive:
tempFolder = sysDrive + os.sep + "TEMP"
if not os.path.exists(tempFolder):
os.makedirs(tempFolder,mode=777)
arcpy.env.scratchWorkspace = tempFolder
AddMsgAndPrint("\tTemporarily setting scratch workspace to: " + arcpy.env.scratchGDB,1)
else:
packageWS = [f for f in arcpy.ListEnvironments() if f=='packageWorkspace']
if arcpy.env[packageWS[0]]:
arcpy.env.scratchWorkspace = arcpy.env[packageWS[0]]
AddMsgAndPrint("\tTemporarily setting scratch workspace to: " + arcpy.env.scratchGDB,1)
else:
AddMsgAndPrint("\tCould not set any scratch workspace",2)
return False
# user-set workspace does not violate system paths; Check for read/write
# permissions; if write permissions are denied then set workspace to TEMP folder
else:
arcpy.env.scratchWorkspace = scratchWK
if arcpy.env.scratchGDB == None:
AddMsgAndPrint("\tCurrent scratch workspace: " + scratchWK + " is READ only!",0)
if sysDrive:
tempFolder = sysDrive + os.sep + "TEMP"
if not os.path.exists(tempFolder):
os.makedirs(tempFolder,mode=777)
arcpy.env.scratchWorkspace = tempFolder
AddMsgAndPrint("\tTemporarily setting scratch workspace to: " + arcpy.env.scratchGDB,1)
else:
packageWS = [f for f in arcpy.ListEnvironments() if f=='packageWorkspace']
if arcpy.env[packageWS[0]]:
arcpy.env.scratchWorkspace = arcpy.env[packageWS[0]]
AddMsgAndPrint("\tTemporarily setting scratch workspace to: " + arcpy.env.scratchGDB,1)
else:
AddMsgAndPrint("\tCould not set any scratch workspace",2)
return False
else:
AddMsgAndPrint("\tUser-defined scratch workspace is set to: " + arcpy.env.scratchGDB,0)
# No workspace set (Very odd that it would go in here unless running directly from python)
else:
AddMsgAndPrint("\tNo user-defined scratch workspace ",0)
sysDrive = os.environ['SYSTEMDRIVE']
if sysDrive:
tempFolder = sysDrive + os.sep + "TEMP"
if not os.path.exists(tempFolder):
os.makedirs(tempFolder,mode=777)
arcpy.env.scratchWorkspace = tempFolder
AddMsgAndPrint("\tTemporarily setting scratch workspace to: " + arcpy.env.scratchGDB,1)
else:
packageWS = [f for f in arcpy.ListEnvironments() if f=='packageWorkspace']
if arcpy.env[packageWS[0]]:
arcpy.env.scratchWorkspace = arcpy.env[packageWS[0]]
AddMsgAndPrint("\tTemporarily setting scratch workspace to: " + arcpy.env.scratchGDB,1)
else:
return False
arcpy.Compact_management(arcpy.env.scratchGDB)
return arcpy.env.scratchGDB
except:
# All Failed; set workspace to packageWorkspace environment
try:
packageWS = [f for f in arcpy.ListEnvironments() if f=='packageWorkspace']
if arcpy.env[packageWS[0]]:
arcpy.env.scratchWorkspace = arcpy.env[packageWS[0]]
arcpy.Compact_management(arcpy.env.scratchGDB)
return arcpy.env.scratchGDB
else:
AddMsgAndPrint("\tCould not set scratchWorkspace. Not even to default!",2)
return False
except:
errorMsg()
return False
## ================================================================================================================
def tic():
""" Returns the current time """
return time.time()
## ================================================================================================================
def toc(_start_time):
""" Returns the total time by subtracting the start time - finish time"""
try:
t_sec = round(time.time() - _start_time)
(t_min, t_sec) = divmod(t_sec,60)
(t_hour,t_min) = divmod(t_min,60)
if t_hour:
return ('{} hour(s): {} minute(s): {} second(s)'.format(int(t_hour),int(t_min),int(t_sec)))
elif t_min:
return ('{} minute(s): {} second(s)'.format(int(t_min),int(t_sec)))
else:
return ('{} second(s)'.format(int(t_sec)))
except:
errorMsg()
## ================================================================================================================
def splitThousands(someNumber):
""" will determine where to put a thousands seperator if one is needed.
Input is an integer. Integer with or without thousands seperator is returned."""
try:
return re.sub(r'(\d{3})(?=\d)', r'\1,', str(someNumber)[::-1])[::-1]
except:
errorMsg()
return someNumber
## ================================================================================================================
def getBoundingCoordinates(feature):
""" This function will return WGS coordinates in Lat-Long format that will be passed over to
the 'WEB_EXPORT_PEDON_BOX_COUNT' report. The coordinates are generated by creating
a minimum bounding box around the input features. The box is then converted to vertices
and the SW Ycoord, NE Ycoord, SW Xcoord and NE Ycoord are return in that order.
Geo-Processing Environments are set to WGS84 in order to return coords in Lat/Long."""
try:
""" Determine if features are a subset of selected polygons OR the entire dataset
is being used. This was necessary b/c the output Coordinate System
environmental variable was not being honored if a selected set is being used.
Export selected set to a temporary feature class otherwise continue"""
arcpy.SetProgressorLabel("Calculating bounding coordinates of input features")
featurePath = arcpy.Describe(feature).catalogPath
totalPolys = int(arcpy.GetCount_management(featurePath).getOutput(0))
selectedPolys = int(arcpy.GetCount_management(feature).getOutput(0))
bExport = False
if selectedPolys < totalPolys:
envelopeFeature = arcpy.CreateScratchName("envelopeFeature",data_type="FeatureClass", workspace=scratchWS)
arcpy.CopyFeatures_management(feature,envelopeFeature)
AddMsgAndPrint("\nCalculating bounding coordinates for " + splitThousands(selectedPolys) + " feature(s)",0)
bExport = True
else:
envelopeFeature = feature
AddMsgAndPrint("\nCalculating bounding coordinates of input features",0)
""" Set Projection and Geographic Transformation environments in order
to post process everything in WGS84. This will force all coordinates
to be in Lat/Long"""
inputSR = arcpy.Describe(feature).spatialReference # Get Spatial Reference of input features
inputDatum = inputSR.GCS.datumName # Get Datum name of input features
if inputSR == "Unkown":
AddMsgAndPrint("\n\tInput layer needs a spatial reference defined to determine bounding envelope",2)
return False
if inputDatum == "D_North_American_1983":
arcpy.env.geographicTransformations = "WGS_1984_(ITRF00)_To_NAD_1983"
elif inputDatum == "D_North_American_1927":
arcpy.env.geographicTransformations = "WGS_1984_(ITRF00)_To_NAD_1927"
elif inputDatum == "D_NAD_1983_2011":
arcpy.env.geographicTransformations = "WGS_1984_(ITRF00)_To_NAD_1983_2011"
elif inputDatum == "D_WGS_1984":
arcpy.env.geographicTransformations = ""
else:
AddMsgAndPrint("\n\tGeo Transformation of Datum could not be set",2)
AddMsgAndPrint("\tTry Projecting input layer to WGS 1984 Coordinate System",2)
return False, False, False, False
# Factory code for WGS84 Coordinate System
arcpy.env.outputCoordinateSystem = arcpy.SpatialReference(4326)
""" ------------ Create Minimum Bounding Envelope of features ------------"""
envelope = arcpy.CreateScratchName("envelope",data_type="FeatureClass",workspace=scratchWS)
envelopePts = arcpy.CreateScratchName("envelopePts",data_type="FeatureClass",workspace=scratchWS)
# create minimum bounding geometry enclosing all features
arcpy.MinimumBoundingGeometry_management(envelopeFeature,envelope,"ENVELOPE","ALL","#","MBG_FIELDS")
if int(arcpy.GetCount_management(envelope).getOutput(0)) < 1:
AddMsgAndPrint("\n\tFailed to create minimum bounding area. \n\tArea of interest is potentially too small",2)
return False
arcpy.FeatureVerticesToPoints_management(envelope, envelopePts, "ALL")
""" ------------ Get X and Y coordinates from envelope ------------"""
coordList = []
with arcpy.da.SearchCursor(envelopePts,['SHAPE@XY']) as cursor:
for row in cursor:
if abs(row[0][0]) > 0 and abs(row[0][1]) > 0:
# Don't add duplicate coords; Last coord will also be the starting coord
if not row[0] in coordList:
coordList.append(row[0])
# Reset output Coord Sys Environment
arcpy.env.outputCoordinateSystem = ""
# Delete temp spatial files
for tempFile in [envelope,envelopePts]:
if arcpy.Exists(tempFile):
arcpy.Delete_management(tempFile)
if bExport:
arcpy.Delete_management(envelopeFeature)
if len(coordList) == 4:
AddMsgAndPrint("\tBounding Box Coordinates:")
AddMsgAndPrint("\t\tSouth Latitude: " + str(coordList[0][1]))
AddMsgAndPrint("\t\tNorth Latitude: " + str(coordList[2][1]))
AddMsgAndPrint("\t\tEast Longitude: " + str(coordList[0][0]))
AddMsgAndPrint("\t\tWest Longitude: " + str(coordList[2][0]))
return coordList[0][1],coordList[2][1],coordList[0][0],coordList[2][0]
else:
AddMsgAndPrint("\tCould not get Latitude-Longitude coordinates from bounding area",2)
return False
except:
for tempFile in [envelope,envelopePts]:
if arcpy.Exists(tempFile):
arcpy.Delete_management(tempFile)
errorMsg()
return False
## ================================================================================================================
def getWebPedonNumberSum(coordinates):
""" This function will send the bounding coordinates to the 'Web Pedon Number SUM' NASIS report
and return the number of pedons within the bounding coordinates. Pedons include regular
NASIS pedons and LAB pedons. Example of URL sent to the NASIS report would be:
https://nasis.sc.egov.usda.gov/NasisReportsWebSite/limsreport.aspx?report_name=WEB_ANALYSIS_PC_PEDON_NUMBER_SUM&Lat1=43.6425577303&Lat2=43.9828939095&Long1=-89.5997555233&Long2=-89.167551308"""
try:
AddMsgAndPrint("\nDetermining if there are any pedons within the bounding coordinates")
arcpy.SetProgressorLabel("Determining if there are any pedons within the bounding coordinates")
# Open a network object using the URL with the search string already concatenated
URL = r'https://nasis.sc.egov.usda.gov/NasisReportsWebSite/limsreport.aspx?report_name=WEB_ANALYSIS_PC_PEDON_NUMBER_SUM' + coordinates
""" -------------------------------------- Try connecting to NASIS to read the report ------------------------"""
try:
theReport = urlopen(URL).readlines()
except:
try:
AddMsgAndPrint("\t2nd attempt at requesting data")
theReport = urlopen(URL).readlines()
except:
try:
AddMsgAndPrint("\t3rd attempt at requesting data")
theReport = urlopen(URL).readlines()
except URLError, e:
if hasattr(e, 'reason'):
AddMsgAndPrint("\n\t" + URL)
AddMsgAndPrint("\tURL Error: " + str(e.reason), 2)
elif hasattr(e, 'code'):
AddMsgAndPrint("\n\t" + URL)
AddMsgAndPrint("\t" + e.msg + " (errorcode " + str(e.code) + ")", 2)
return False
except socket.timeout, e:
AddMsgAndPrint("\n\t" + URL)
AddMsgAndPrint("\tServer Timeout Error", 2)
return False
except socket.error, e:
AddMsgAndPrint("\n\t" + URL)
AddMsgAndPrint("\tNASIS Reports Website connection failure (Socket Error)", 2)
return False
except httplib.BadStatusLine:
AddMsgAndPrint("\n\t" + URL)
AddMsgAndPrint("\tNASIS Reports Website connection failure (Bad Status Line)", 2)
return False
except:
errorMsg()
return False
""" -------------------------------------- Read the NASIS report ---------------------------------"""
bValidRecord = False # boolean that marks the starting point of the mapunits listed in the project
# iterate through the report until a valid record is found
for theValue in theReport:
theValue = theValue.strip() # removes whitespace characters
# Iterating through the lines in the report
if bValidRecord:
if theValue == "STOP": # written as part of the report; end of lines
break
else:
try:
return int(theValue)
except:
continue
else:
if theValue.startswith('<div id="ReportData">START'):
bValidRecord = True
except:
errorMsg()
return False
## ================================================================================================================
def getWebExportPedon(coordinates):
""" This function will send the bounding coordinates to the 'Web Export Pedon Box' NASIS report
and return a list of pedons within the bounding coordinates. Pedons include regular
NASIS pedons and LAB pedons. Each record in the report will contain the following values:
Row_Number,upedonid,peiid,pedlabsampnum,Longstddecimaldegrees,latstddecimaldegrees,Undisclosed Pedon
24|S1994MN161001|102861|94P0697|-93.5380936|44.0612717|'Y'
A dictionary will be returned containing something similar:
{'102857': ('S1954MN161113A', '40A1694', '-93.6499481', '43.8647194','Y'),
'102858': ('S1954MN161113B', '40A1695', '-93.6455002', '43.8899956','N')}
theURL = r' #getPedonIDURL = r'https://nasis.sc.egov.usda.gov/NasisReportsWebSite/limsreport.aspx?report_name=WEB_EXPORT_PEDON_BOX_COUNT&Lat1=44.070820&Lat2=44.596950&Long1=-91.166274&Long2=-90.311911'"""
try:
AddMsgAndPrint("\nRequesting a list of pedonIDs from NASIS using the above bounding coordinates")
arcpy.SetProgressorLabel("Requesting a list of pedons from NASIS")
URL = r'https://nasis.sc.egov.usda.gov/NasisReportsWebSite/limsreport.aspx?report_name=WEB_EXPORT_PEDON_BOX_COUNT' + coordinates
# Open a network object using the URL with the search string already concatenated
startTime = tic()
#AddMsgAndPrint("\tNetwork Request Time: " + toc(startTime))
""" -------------------------------------- Try connecting to NASIS to read the report ------------------------"""
try:
theReport = urlopen(URL).readlines()
except:
try:
AddMsgAndPrint("\t2nd attempt at requesting data")
theReport = urlopen(URL).readlines()
except:
try:
AddMsgAndPrint("\t3rd attempt at requesting data")
theReport = urlopen(URL).readlines()
except URLError, e:
if hasattr(e, 'reason'):
AddMsgAndPrint("\n\t" + URL)
AddMsgAndPrint("\tURL Error: " + str(e.reason), 2)
elif hasattr(e, 'code'):
AddMsgAndPrint("\n\t" + URL)
AddMsgAndPrint("\t" + e.msg + " (errorcode " + str(e.code) + ")", 2)
return False
except socket.timeout, e:
AddMsgAndPrint("\n\t" + URL)
AddMsgAndPrint("\tServer Timeout Error", 2)
return False
except socket.error, e:
AddMsgAndPrint("\n\t" + URL)
AddMsgAndPrint("\tNASIS Reports Website connection failure", 2)
return False
except httplib.BadStatusLine:
AddMsgAndPrint("\n\t" + URL)
AddMsgAndPrint("\tNASIS Reports Website connection failure", 2)
return False
""" -------------------------------------- Read the NASIS report ------------------------------------"""
totalPedonCnt = 0
labPedonCnt = 0
undisclosed = 0
bValidRecord = False # boolean that marks the starting point of the mapunits listed in the project
arcpy.SetProgressor("step", "Reading NASIS Report: 'WEB_EXPORT_PEDON_BOX_COUNT'", 0, len(theReport), 1)
# iterate through the report until a valid record is found
for theValue in theReport:
theValue = theValue.strip() # removes whitespace characters
# Iterating through the lines in the report
if bValidRecord:
if theValue == "STOP": # written as part of the report; end of lines
break
# Found a valid project record i.e. -- SDJR - MLRA 103 - Kingston silty clay loam, 1 to 3 percent slopes|400036
else:
theRec = theValue.split("|")
if len(theRec) != 7:
AddMsgAndPrint("\tNASIS Report: Web Export Pedon Box is not returning the correct amount of values per record",2)
return False
# Undisclosed Record; Reject this record
if theRec[6] == "Y":
undisclosed+=1
totalPedonCnt += 1
continue
rowNumber = theRec[0]
userPedonID = theRec[1]
pedonID = theRec[2]
longDD = theRec[4]
latDD = theRec[5]
# Lab sample or not
if theRec[3] == 'Null' or theRec[3] == '':
labSampleNum = None
else:
labSampleNum = theRec[3]
labPedonCnt += 1
if not pedonDict.has_key(pedonID):
pedonDict[pedonID] = (userPedonID,labSampleNum,longDD,latDD)
totalPedonCnt += 1
else:
if theValue.startswith('<div id="ReportData">START'):
bValidRecord = True
arcpy.SetProgressorPosition()
#Resets the progressor back to its initial state
arcpy.ResetProgressor()
if len(pedonDict) == 0:
AddMsgAndPrint("\tThere were no pedons found in this area; Try using a larger extent",1)
return False
else:
#AddMsgAndPrint("\tThere are a total of " + splitThousands(totalPedonCnt) + " pedons found in this area:")
AddMsgAndPrint("\tThere are " + splitThousands(totalPedonCnt) + " within this layer:")
AddMsgAndPrint("\t\tLAB Pedons: " + splitThousands(labPedonCnt))
AddMsgAndPrint("\t\tUndisclosed: " + splitThousands(undisclosed))
AddMsgAndPrint("\t\tNASIS Pedons: " + splitThousands((totalPedonCnt - labPedonCnt) - undisclosed))
return True
except:
errorMsg()
return False
## ================================================================================================================
def filterPedonsByFeature(feature):
""" This function will temporarily plot out the pedons in order to determine which pedons fall completely
within the user's AOI. Once determined, the extra pedons will be removed from the pedonDict"""
try:
AddMsgAndPrint("\nSelecting pedons that intersect with " + arcpy.Describe(feature).Name + " Layer",0)
arcpy.SetProgressorLabel("Selecting pedons that intersect with " + arcpy.Describe(feature).Name + " Layer")
#arcpy.env.outputCoordinateSystem = arcpy.SpatialReference(4326)
# Make a copy of the user-input features - this is just in case there is a selected set
aoiFeature = arcpy.CreateScratchName("aoiFeature",data_type="FeatureClass", workspace=scratchWS)
arcpy.CopyFeatures_management(feature,aoiFeature)
# Create a temp point feature class to digitize ALL of the pedons within the bounding box first
tempPoints = arcpy.CreateScratchName("tempPoints",data_type="FeatureClass", workspace=scratchWS)
# Factory code for WGS84 Coordinate System
spatial_reference = arcpy.SpatialReference(4326)
#spatial_reference = arcpy.Describe(feature).spatialReference
arcpy.CreateFeatureclass_management(scratchWS, os.path.basename(tempPoints), "POINT", "#", "DISABLED", "DISABLED", spatial_reference)
peiidFld = "peiid"
arcpy.AddField_management(tempPoints,peiidFld,"LONG")
# Initiate the insert cursor object using the peiid and XY values
cursor = arcpy.da.InsertCursor(tempPoints,[peiidFld,'SHAPE@XY'])
for pedon in pedonDict:
xValue = float(pedonDict[pedon][2])
yValue = float(pedonDict[pedon][3])
newRow = [pedon,(xValue,yValue)]
cursor.insertRow(newRow)
del cursor
tempPointsPRJ = arcpy.CreateScratchName("tempPointsPRJ",data_type="FeatureClass", workspace=scratchWS)
arcpy.Project_management(tempPoints,tempPointsPRJ,arcpy.Describe(feature).spatialReference)
arcpy.SetProgressorLabel("Selecting pedons that intersect with " + arcpy.Describe(feature).Name + " Layer") # Some odd reason 'tempPointsPRJ' stays frozen in the progress bar.
# Select all of the pedons within the user's AOI
tempPointsLYR = arcpy.CreateScratchName("tempPointsLYR",data_type="FeatureClass", workspace=scratchWS)
arcpy.MakeFeatureLayer_management(tempPointsPRJ,tempPointsLYR)
#AddMsgAndPrint("\tThere are " + str(int(arcpy.GetCount_management("tempPoints_LYR").getOutput(0))) + " pedons in the layer",2)
arcpy.SelectLayerByLocation_management(tempPointsLYR,"INTERSECT",aoiFeature, "","NEW_SELECTION")
pedonsWithinAOI = int(arcpy.GetCount_management(tempPointsLYR).getOutput(0))
# There are pedons within the user's AOI
if pedonsWithinAOI > 0:
AddMsgAndPrint("\tThere are " + splitThousands(pedonsWithinAOI) + " pedons within this layer",0)
# Make a copy of the user-input features - this is just in case there is a selected set
selectedPedons = arcpy.CreateScratchName("selectedPedons",data_type="FeatureClass", workspace=scratchWS)
arcpy.CopyFeatures_management(tempPointsLYR,selectedPedons)
# Create a new list of pedonIDs from the selected set; pedonIDs are converted to strings in order
# to compare against the pedonDict()
selectedPedonsList = [str(row[0]) for row in arcpy.da.SearchCursor(selectedPedons, (peiidFld))]
# Make a copy of pedonDict b/c it cannot change during iteration
pedonDictCopy = pedonDict.copy()
# delete any pedon from the original pedonDict that is not in the selected set.
labPedonCnt = 0
for pedon in pedonDictCopy:
if pedon not in selectedPedonsList:
del pedonDict[pedon]
else:
if not pedonDict[pedon][1] is None:
labPedonCnt+=1
AddMsgAndPrint("\t\tLAB Pedons: " + splitThousands(labPedonCnt))
AddMsgAndPrint("\t\tNASIS Pedons: " + splitThousands(pedonsWithinAOI - labPedonCnt))
for layer in (aoiFeature,tempPoints,tempPointsPRJ,tempPointsLYR,selectedPedons):
if arcpy.Exists(layer):
arcpy.Delete_management(layer)
del pedonDictCopy,selectedPedons,selectedPedonsList
return pedonsWithinAOI
else:
AddMsgAndPrint("\tThere are NO pedons that are completely within your AOI. EXITING! \n",2)
exit()
except arcpy.ExecuteError:
AddMsgAndPrint(arcpy.GetMessages(2),2)
return False
except:
AddMsgAndPrint("Unhandled exception (filterPedonsByFeature). EXITING!", 2)
errorMsg()
return False
## ================================================================================================================
def createPedonFGDB():
"""This Function will create a new File Geodatabase using a pre-established XML workspace
schema. All Tables will be empty and should correspond to that of the access database.
Relationships will also be pre-established.
Return false if XML workspace document is missing OR an existing FGDB with the user-defined
name already exists and cannot be deleted OR an unhandled error is encountered.
Return the path to the new Pedon File Geodatabase if everything executes correctly."""
try:
AddMsgAndPrint("\nCreating New Pedon File Geodatabase",0)
arcpy.SetProgressorLabel("Creating New Pedon File Geodatabase")
# pedon xml template that contains empty pedon Tables and relationships
# schema and will be copied over to the output location
pedonXML = os.path.dirname(sys.argv[0]) + os.sep + "NASISpedons_XMLWorkspace.xml"
localPedonGDB = os.path.dirname(sys.argv[0]) + os.sep + "NasisPedonsTemplate.gdb"
# Return false if pedon fGDB template is not found
if not arcpy.Exists(localPedonGDB):
AddMsgAndPrint("\t" + os.path.basename(localPedonGDB) + " FGDB template was not found!",2)
return False
newPedonFGDB = os.path.join(outputFolder,GDBname + ".gdb")
if arcpy.Exists(newPedonFGDB):
try:
arcpy.Delete_management(newPedonFGDB)
AddMsgAndPrint("\t" + GDBname + ".gdb already exists. Deleting and re-creating FGDB\n",1)
except:
AddMsgAndPrint("\t" + GDBname + ".gdb already exists. Failed to delete\n",2)
return False
# copy template over to new location
AddMsgAndPrint("\tCreating " + GDBname + ".gdb with NCSS Pedon Schema 7.3")
arcpy.Copy_management(localPedonGDB,newPedonFGDB)
""" ------------------------------ Code to use XML Workspace -------------------------------------------"""
## # Return false if xml file is not found
## if not arcpy.Exists(pedonXML):
## AddMsgAndPrint("\t" + os.path.basename(pedonXML) + " Workspace document was not found!",2)
## return False
##
## # Create empty temp File Geodatabae
## arcpy.CreateFileGDB_management(outputFolder,os.path.splitext(os.path.basename(newPedonFGDB))[0])
##
## # set the pedon schema on the newly created temp Pedon FGDB
## AddMsgAndPrint("\tImporting NCSS Pedon Schema 7.3 into " + GDBname + ".gdb")
## arcpy.ImportXMLWorkspaceDocument_management(newPedonFGDB, pedonXML, "DATA", "DEFAULTS")
arcpy.UncompressFileGeodatabaseData_management(newPedonFGDB)
arcpy.RefreshCatalog(outputFolder)
AddMsgAndPrint("\tSuccessfully created: " + GDBname + ".gdb")
return newPedonFGDB
except arcpy.ExecuteError:
AddMsgAndPrint(arcpy.GetMessages(2),2)
return False
except:
AddMsgAndPrint("Unhandled exception (createFGDB)", 2)
errorMsg()
return False
## ===============================================================================================================
def getTableAliases(pedonFGDBloc):
# Retrieve physical and alias names from MDSTATTABS table and assigns them to a blank dictionary.
# Stores physical names (key) and aliases (value) in a Python dictionary i.e. {chasshto:'Horizon AASHTO,chaashto'}
# Fieldnames are Physical Name = AliasName,IEfilename
try:
arcpy.SetProgressorLabel("Gathering Table and Field aliases")
# Open Metadata table containing information for other pedon tables
theMDTable = pedonFGDBloc + os.sep + "MetadataTable"
arcpy.env.workspace = pedonFGDBloc
# Establishes a cursor for searching through field rows. A search cursor can be used to retrieve rows.
# This method will return an enumeration object that will, in turn, hand out row objects
if not arcpy.Exists(theMDTable):
return False
tableList = arcpy.ListTables("*")
tableList.append("pedon")
nameOfFields = ["TablePhysicalName","TableLabel"]
for table in tableList:
# Skip any Metadata files
if table.find('Metadata') > -1: continue
expression = arcpy.AddFieldDelimiters(theMDTable,"TablePhysicalName") + " = '" + table + "'"
with arcpy.da.SearchCursor(theMDTable,nameOfFields, where_clause = expression) as cursor:
for row in cursor:
# read each table record and assign 'TablePhysicalName' and 'TableLabel' to 2 variables
physicalName = row[0]
aliasName = row[1]
# i.e. {phtexture:'Pedon Horizon Texture',phtexture}; will create a one-to-many dictionary
# As long as the physical name doesn't exist in dict() add physical name
# as Key and alias as Value.
if not tblAliases.has_key(physicalName):
tblAliases[physicalName] = aliasName
del physicalName,aliasName
del theMDTable,tableList,nameOfFields
return True
except arcpy.ExecuteError:
AddMsgAndPrint(arcpy.GetMessages(2),2)
return False
except:
AddMsgAndPrint("Unhandled exception (GetTableAliases)", 2)
errorMsg()
return False
## ===============================================================================================================
def createEmptyDictOfTables():
# Create a new dictionary called pedonGDBtablesDict that will contain every table in the newly created
# pedonFGDB above as a key. Individual records of tables will be added as values to the table keys.
# These values will be in the form of lists. This dictionary will be populated using the results of
# the WEB_AnalysisPC_MAIN_URL_EXPORT NASIS report. Much faster than opening and closing cursors every
# a request is made to nasis. URL report contents will be parsed out to this dictionary.
# {'area': [],'areatype': [],'basalareatreescounted': [],'beltdata': [],'belttransectsummary': []........}
try:
arcpy.env.workspace = pedonFGDB
tables = arcpy.ListTables()
tables.append(arcpy.ListFeatureClasses('pedon','Point')[0]) ## pedon is a feature class and gets excluded by the ListTables function
# Create dictionary where keys will be tables and values will be later populated
# {'area': [],'areatype': [],'basalareatreescounted': [],'beltdata': [],'belttransectsummary': []........}
pedonGDBtablesDict = dict()
for table in tables:
# Skip any Metadata files
if table.find('Metadata') > -1: continue
pedonGDBtablesDict[str(table)] = []
del tables
return pedonGDBtablesDict
except:
AddMsgAndPrint("\nUnhandled exception (GetTableAliases)\n", 2)
errorMsg()
exit()
## ===============================================================================================================
def createTableFieldLookuup():
# This function will create a dictionary that will contain table:number of fields in order
# to double check that the values from the web report are correct. This was added b/c there
# were text fields that were getting disconnected in the report and being read as 2 lines
# and Jason couldn't address this issue in NASIS. This also serves as a QC against values
# have an incorrect number of values relative to the available fields. This has been a problem
# with values having a pipe (|) which causes a problem with splitting values.
try:
arcpy.env.workspace = pedonFGDB
tableInfoDict = dict() # contains all valid tables and the number of fields that it contains i.e. petext:11
validTables = arcpy.ListTables("*")
validTables.append('pedon')
for table in validTables:
# Skip any Metadata files
if table.find('Metadata') > -1: continue
uniqueFields = arcpy.Describe(os.path.join(pedonFGDB,table)).fields
numOfValidFlds = 0
for field in uniqueFields:
if not field.type.lower() in ("oid","geometry","FID"):
numOfValidFlds +=1
# Add 2 more fields to the pedon table for X,Y
if table == 'pedon':
numOfValidFlds += 2
tableInfoDict[table] = numOfValidFlds
del uniqueFields;numOfValidFlds
return tableInfoDict
except:
AddMsgAndPrint("\nUnhandled exception (createTableFieldLookup)\n", 2)
errorMsg()
exit()
## ===============================================================================================================
def parsePedonsIntoLists():
""" This function will parse pedons into manageable chunks that will be sent to the 2nd URL report.
There is an inherent URL character limit of 2,083. The report URL is 123 characters long which leaves 1,960 characters
available. I arbitrarily chose to have a max URL of 1,860 characters long to avoid problems. Most pedonIDs are about
6 characters. This would mean an average max request of 265 pedons at a time.
This function returns a list of pedon lists"""
#1860 = 265
try:
arcpy.SetProgressorLabel("Determining the number of requests to send the server")
# Total Count
i = 1
listOfPedonStrings = list() # List containing pedonIDstring lists; individual lists are comprised of about 265 pedons
pedonIDstr = ""
for pedonID in pedonDict:
# End of pedon list has been reached
if i == len(pedonDict):
pedonIDstr = pedonIDstr + str(pedonID)
listOfPedonStrings.append(pedonIDstr)
# End of pedon list NOT reached
else:
# Max URL length reached - retrieve pedon data and start over
if len(pedonIDstr) > 1860:
pedonIDstr = pedonIDstr + str(pedonID)
listOfPedonStrings.append(pedonIDstr)
## reset the pedon ID string to empty
pedonIDstr = ""
i+=1
# concatenate pedonID to string and continue
else:
pedonIDstr = pedonIDstr + str(pedonID) + ",";i+=1
numOfPedonStrings = len(listOfPedonStrings) # Number of unique requests that will be sent
if not numOfPedonStrings:
AddMsgAndPrint("\n\t Something Happened here.....WTF!",2)
exit()
else:
return listOfPedonStrings,numOfPedonStrings
except:
AddMsgAndPrint("Unhandled exception (createFGDB)", 2)
errorMsg()
exit()
## ================================================================================================================
def getPedonHorizon(pedonList):
# To view a sample output report go to:
# https://nasis.sc.egov.usda.gov/NasisReportsWebSite/limsreport.aspx?report_name=WEB_AnalysisPC_MAIN_URL_EXPORT&pedonid_list=14542
try:
if numOfPedonStrings > 1:
tab = "\t\t"
else:
tab = "\t"
"""----------------------------------- Open a network object --------------------------------"""
''' Open a network object using the URL with the search string already concatenated.
As soon as the url is opened it needs to be read otherwise there will be a socket
error raised. Experienced this when the url was being opened before the above
dictionary was created. Bizarre'''
URL = r'https://nasis.sc.egov.usda.gov/NasisReportsWebSite/limsreport.aspx?report_name=WEB_AnalysisPC_MAIN_URL_EXPORT&pedonid_list=' + pedonList
requestStartTime = tic()
try:
theReport = urlopen(URL).readlines()
except:
try:
AddMsgAndPrint(tab + "2nd attempt at requesting data - 15 second pause")
time.sleep(15)
theReport = urlopen(URL).readlines()
except:
try:
AddMsgAndPrint(tab + "3rd attempt at requesting data - 30 second pause")
time.sleep(30)
theReport = urlopen(URL).readlines()
except: