-
Notifications
You must be signed in to change notification settings - Fork 0
/
CM_XYZ_to_TIN.py
388 lines (337 loc) · 15.1 KB
/
CM_XYZ_to_TIN.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
# CM_XYZ_to_TIN
# Script to import channel mapping data and create TIN
# September 9, 2016
# Paul Grams
#
# modified from XYZ_v10.py which started as XYZ_FIST_FGDB_Folder_v10.py by Tim Andrews
#
import glob
import re
import sys
import string
import os
import arcpy
from arcpy import env
# Turn on history logging so that a history log file is written
arcpy.LogHistory = True
# Obtain a license for the 3D Analyst extension
arcpy.CheckOutExtension("3D")
# Local variables...
# GUI variables
# set as parameters in toolbox in this order #Display Name: Data Type
inYear = arcpy.GetParameterAsText(0) #Year data collected: string
inSegment = arcpy.GetParameterAsText(1) #Segment Name: string
inMbFolder = arcpy.GetParameterAsText(2) #Input MB Folder Folder
inSbFolder = arcpy.GetParameterAsText(3) #Input SB Folder: Folder
inTopoPts_shape = arcpy.GetParameterAsText(4) #Input topo points: Shapefile
inTopoBRK_shape = arcpy.GetParameterAsText(5) #Input topo breaklines: Shapefile
inTopoWE_shape = arcpy.GetParameterAsText(6) #Input topo WE: Shapefile
workspaceGDB = arcpy.GetParameterAsText(7) #Output Geodatabase: Workspace or Feature Dataset (must be geodatabase!)
aggregation_distance = arcpy.GetParameterAsText(8) #Aggragation Distance: string (default is 3)
outTinFolder = arcpy.GetParameterAsText(9) #Output folder for TIN: Workspace or Feature Dataset (must be folder!)
TinDelineationLength = arcpy.GetParameterAsText(10) #TIN Delineation Length: string (default is 6)
EliminationArea = arcpy.GetParameterAsText(11) #Multbeam data hole elimination threshold (default is 500)
## Note on Outputs
# The output TINs go in the output tin folder
# All other outputs go in the output geodatabase
# These include all the individual mb, sb, and topo point files and merged point files.
# AllPts_Seg_xxx_YYYY_merge has all mb, sb, and topo points in one file with the following fields:
# POINT_X, POINT_Y, POINT_Z, SOURCE_2014
# SOURCE_2014 has MB for multibeam data, SB for singlebeam data, and TOPO for topo data
#
# MB_Seg_xxx_YYYY_boundary is boundary of multibeam data coverage
# created using aggradation with specified aggragation distance
# MB_Seg_xxx_YYYY_bdy_elim has holes removed using eliminate with
# elimination size hardwired in at 500 m in line 217
# were inputs, changed to fixed
fileSuffix = "xyz"
#
inMbWS = inMbFolder
inSbWS = inSbFolder
# copy input shapefiles to feature classes in geodatabase
## convert shapefile to feature class
outTopoPtsFC = str(workspaceGDB + "\\" + "TopoPts_Seg_" + inSegment + "_" + inYear)
arcpy.CopyFeatures_management(inTopoPts_shape, outTopoPtsFC)
inTopoPts = os.path.basename(outTopoPtsFC)
#
outTopoBrkFC = str(workspaceGDB + "\\" + "TopoBrk_Seg_" + inSegment + "_" + inYear)
arcpy.CopyFeatures_management(inTopoBRK_shape, outTopoBrkFC)
inTopoBRK = os.path.basename(outTopoBrkFC)
#
outTopoWeFC = str(workspaceGDB + "\\" + "TopoWe_Seg_" + inSegment + "_" + inYear)
arcpy.CopyFeatures_management(inTopoWE_shape, outTopoWeFC)
inTopoWE = os.path.basename(outTopoWeFC)
#
arcpy.AddMessage(" ")
arcpy.AddMessage("Input Multibeam Folder Path is: " + inMbWS)
arcpy.AddMessage("Input Singlebeam Folder Path is: " + inSbWS)
arcpy.AddMessage("Input Topography points: " + inTopoPts)
arcpy.AddMessage("Input Topography breaklines: " + inTopoBRK)
arcpy.AddMessage("Input Topography water edge: " + inTopoWE)
arcpy.AddMessage(" ")
#
arcpy.AddMessage("Workspace Geodatabase is: " + workspaceGDB)
arcpy.AddMessage(" ")
arcpy.AddMessage("Input File Suffix is: " + fileSuffix)
arcpy.AddMessage(" ")
# spatial reference hardwired in
# Use the State Plane AZ Central projection file as input to the SpatialReference class
prjFile = r"C:\arcgis\Desktop10.0\Coordinate Systems\Projected Coordinate Systems\State Plane\NAD 1983 (Meters)\NAD 1983 StatePlane Arizona Central FIPS 0202 (Meters).prj"
spatialRef = arcpy.SpatialReference(prjFile)
# Set the output Coordinate System
arcpy.env.outputCoordinateSystem = spatialRef
arcpy.AddMessage("Output Spatial Reference was set to " + str(prjFile))
arcpy.AddMessage(" ")
# Set environment settings
env.workspace = str(workspaceGDB)
env.overwriteOutput = True
#############################################################
#############################################################
#
# process multibeam inputs
# Get all the XYZ files in the input folder as a Glob
DataType = "MB"
FieldValue = '"MB"'
inputs = str(inMbWS+"\*" + str(fileSuffix))
arcpy.AddMessage("Inputs with wildcard are: " + inputs)
arcpy.AddMessage(" ")
theFiles = glob.glob(inputs)
FClist = []
# Loop through the input XYZ files
for i in theFiles:
#inXYZfull = str(i).replace("\\","/")
inXYZfull = str(i)
inXYZ = os.path.split(inXYZfull)[1]
inPath = os.path.split(inXYZfull)[0]
#output = os.path.join(out_workspace, fc)
arcpy.AddMessage("Input XYZ File is: " + inXYZfull)
noextName = str(inXYZ[:-4])
# arcpy.AddMessage("Input XYZ File without extension is: " + noextName)
# arcpy.AddMessage("")
shortFCName = str("pt_" + noextName)
# arcpy.AddMessage("Short Output FC Name: " + shortFCName)
# arcpy.AddMessage("Short Output GRID Name: " + outRasterName)
outFCNameFull = str(workspaceGDB + "\\" + shortFCName)
# arcpy.AddMessage("Full Output FC Name: " + outFCNameFull)
# arcpy.AddMessage(" ")
try:
# Input file format
inFormat = "XYZ"
# Geometry of the output feature class
outType = "POINT"
# Multiplier applied to the input z values
zFactor = 1
# The character used to represent a decimal for floating point numbers
decSep = "DECIMAL_POINT" # Specifies the decimal delimiter
# Process: creating a feature class using an ASCII input file
# ASCII3DToFeatureClass_3d (input, in_file_type, out_feature_class, out_geometry_type, {z_factor},
# {input_coordinate_system}, average_point_spacing, {file_suffix}, {decimal_separator})
arcpy.ASCII3DToFeatureClass_3d(inXYZfull, inFormat, outFCNameFull, outType, zFactor, spatialRef, "#", fileSuffix, decSep)
arcpy.AddMessage("Created XYZ Point Feature Class: " + outFCNameFull)
# Add X,Y, Z Fields
arcpy.AddXY_management(outFCNameFull)
arcpy.AddMessage("Added X,Y,Z fields to " + outFCNameFull)
arcpy.AddMessage(" ")
# Add field for data type
#AddField_management (in_table, field_name, field_type, {field_precision}, {field_scale}, {field_length},
# {field_alias}, {field_is_nullable}, {field_is_required}, {field_domain})
FieldName = str("Source_" + inYear)
#FieldValue = '"MB"'
arcpy.AddField_management(outFCNameFull, FieldName, "TEXT")
arcpy.CalculateField_management(outFCNameFull, FieldName, FieldValue, "PYTHON_9.3")
#
#FClist.append(outFCNameFull)
FClist = FClist + [outFCNameFull]
arcpy.AddMessage("list of feature class files... ")
arcpy.AddMessage(FClist)
arcpy.AddMessage(" ")
except:
# If an error occurred while running the tool, print the error messages.
arcpy.AddMessage("failed to create mb point feature classes")
print arcpy.GetMessages()
try:
# merge
#
outPointMerge = str(workspaceGDB + "\\" + DataType +"_Seg_" + inSegment + "_" + inYear + "_merge")
arcpy.Merge_management(FClist, outPointMerge)
arcpy.AddMessage("created merged point file...")
arcpy.AddMessage(outPointMerge)
#MBpointMerge = outPointMerge
MBpointMerge = os.path.basename(outPointMerge)
arcpy.AddMessage("short name...")
arcpy.AddMessage(MBpointMerge)
except:
# If an error occurred while running a tool, then print the messages.
arcpy.AddMessage("did not create merged point file: " + outPointMerge)
print arcpy.GetMessages()
try:
outMBboundary = str(workspaceGDB + "\\" + DataType + "_Seg_" + inSegment + "_" + inYear + "_boundary")
fc = outPointMerge
# Process: Aggregate Points
arcpy.AggregatePoints_cartography(fc, outMBboundary, aggregation_distance)
arcpy.AddMessage("Created Polygons from XYZ points...")
arcpy.AddMessage(outMBboundary)
except:
# If an error occurred while running a tool, then print the messages.
arcpy.AddMessage("Did Not Create Polygons from XYZ points...")
arcpy.AddMessage(outMBboundary)
print arcpy.GetMessages()
try:
outMBboundaryElim = str(workspaceGDB + "\\" + DataType + "_Seg_" + inSegment + "_" + inYear + "_bdy_elim")
EliminationAreaExpression = str(EliminationArea + " SquareMeters")
arcpy.EliminatePolygonPart_management(outMBboundary, outMBboundaryElim, "AREA", "500 SquareMeters", part_area_percent="0", part_option="ANY")
arcpy.AddMessage("Eliminated holes in polygon...")
arcpy.AddMessage(outMBboundaryElim)
arcpy.AddMessage(" ")
except:
# If an error occurred while running a tool, then print the messages.
arcpy.AddMessage("Did Not eliminate holes in polygon...")
arcpy.AddMessage(" ")
print arcpy.GetMessages()
#
#############################################################
#############################################################
#
# process singlebeam inputs
# Get all the XYZ files in the input folder as a Glob
DataType = "SB"
FieldValue = '"SB"'
inputs = str(inSbWS+"\*" + str(fileSuffix))
arcpy.AddMessage("Inputs with wildcard are: " + inputs)
arcpy.AddMessage(" ")
theFiles = glob.glob(inputs)
FClist = []
# Loop through the input XYZ files
for i in theFiles:
#inXYZfull = str(i).replace("\\","/")
inXYZfull = str(i)
inXYZ = os.path.split(inXYZfull)[1]
inPath = os.path.split(inXYZfull)[0]
#output = os.path.join(out_workspace, fc)
arcpy.AddMessage("Input XYZ File is: " + inXYZfull)
noextName = str(inXYZ[:-4])
# arcpy.AddMessage("Input XYZ File without extension is: " + noextName)
# arcpy.AddMessage("")
shortFCName = str("pt_" + noextName)
# arcpy.AddMessage("Short Output FC Name: " + shortFCName)
# arcpy.AddMessage("Short Output GRID Name: " + outRasterName)
outFCNameFull = str(workspaceGDB + "\\" + shortFCName)
# arcpy.AddMessage("Full Output FC Name: " + outFCNameFull)
# arcpy.AddMessage(" ")
try:
# Input file format
inFormat = "XYZ"
# Geometry of the output feature class
outType = "POINT"
# Multiplier applied to the input z values
zFactor = 1
# The character used to represent a decimal for floating point numbers
decSep = "DECIMAL_POINT" # Specifies the decimal delimiter
# Process: creating a feature class using an ASCII input file
# ASCII3DToFeatureClass_3d (input, in_file_type, out_feature_class, out_geometry_type, {z_factor},
# {input_coordinate_system}, average_point_spacing, {file_suffix}, {decimal_separator})
arcpy.ASCII3DToFeatureClass_3d(inXYZfull, inFormat, outFCNameFull, outType, zFactor, spatialRef, "#", fileSuffix, decSep)
arcpy.AddMessage("Created XYZ Point Feature Class: " + outFCNameFull)
# Add X,Y, Z Fields
arcpy.AddXY_management(outFCNameFull)
arcpy.AddMessage("Added X,Y,Z fields to " + outFCNameFull)
arcpy.AddMessage(" ")
# Add field for data type
#AddField_management (in_table, field_name, field_type, {field_precision}, {field_scale}, {field_length},
# {field_alias}, {field_is_nullable}, {field_is_required}, {field_domain})
FieldName = str("Source_" + inYear)
arcpy.AddField_management(outFCNameFull, FieldName, "TEXT")
arcpy.CalculateField_management(outFCNameFull, FieldName, FieldValue, "PYTHON_9.3")
#
#FClist.append(outFCNameFull)
FClist = FClist + [outFCNameFull]
arcpy.AddMessage("list of feature class files... ")
arcpy.AddMessage(FClist)
arcpy.AddMessage(" ")
except:
# If an error occurred while running the tool, print the error messages.
print arcpy.GetMessages()
# Merge SB points
outPointMerge = str(workspaceGDB + "\\" + DataType + "_Seg_" + inSegment + "_" + inYear + "_merge")
outPointMergeErase = str(workspaceGDB + "\\" + DataType + "_Seg_" + inSegment + "_" + inYear + "_merge_erase")
try:
arcpy.Merge_management(FClist, outPointMerge)
arcpy.AddMessage("created merged point file...")
arcpy.AddMessage(outPointMerge)
arcpy.Erase_analysis(outPointMerge, outMBboundary, outPointMergeErase)
#SBpointMerge = outPointMergeErase
SBpointMerge = os.path.basename(outPointMergeErase)
arcpy.AddMessage("short name...")
arcpy.AddMessage(SBpointMerge)
arcpy.AddMessage(" ")
except:
# If an error occurred while running a tool, then print the messages.
arcpy.AddMessage("did not create merged point file: " + outPointMerge)
print arcpy.GetMessages()
#
#############################################################
#############################################################
#
## Process topo data
# add fields
#
FieldName = str("POINT_X")
arcpy.AddField_management(inTopoPts, FieldName, "DOUBLE")
FieldValue = str("!EASTING!")
arcpy.CalculateField_management(inTopoPts, FieldName, FieldValue, "PYTHON_9.3")
#
FieldName = str("POINT_Y")
arcpy.AddField_management(inTopoPts, FieldName, "DOUBLE")
FieldValue = str("!NORTHING!")
arcpy.CalculateField_management(inTopoPts, FieldName, FieldValue, "PYTHON_9.3")
#
FieldName = str("POINT_Z")
arcpy.AddField_management(inTopoPts, FieldName, "DOUBLE")
FieldValue = str("!ELEVATION!")
arcpy.CalculateField_management(inTopoPts, FieldName, FieldValue, "PYTHON_9.3")
#
FieldName = str("Source_" + inYear)
arcpy.AddField_management(inTopoPts, FieldName, "TEXT")
FieldValue = '"TOPO"'
arcpy.CalculateField_management(inTopoPts, FieldName, FieldValue, "PYTHON_9.3")
# delete fields
dropFields = ["PT_ID", "GIS_KEY", "NORTHING", "EASTING", "ELEVATION", "DESCRIPTIO"]
arcpy.DeleteField_management(inTopoPts, dropFields)
#
#############################################################
#############################################################
#
## Merge mb points, sb points, and topo points and create list for making TIN
#
AllPtsMerge = str(workspaceGDB + "\\" + "AllPts_Seg_" + inSegment + "_" + inYear + "_merge")
try:
MergeList = [MBpointMerge, SBpointMerge, inTopoPts]
arcpy.Merge_management(MergeList, AllPtsMerge)
in_features = str(MBpointMerge + " POINT_Z masspoints; " + SBpointMerge + " POINT_Z masspoints; " + inTopoPts + " POINT_Z masspoints;" + inTopoBRK + " <None> softline;" + inTopoWE + " <None> softline")
arcpy.AddMessage("input features for tin...")
arcpy.AddMessage(in_features)
except:
# assuming that not having SB points is cause for fail
MergeList = [MBpointMerge, inTopoPts]
arcpy.Merge_management(MergeList, AllPtsMerge)
in_features = str(MBpointMerge + " POINT_Z masspoints; " + inTopoPts + " POINT_Z masspoints;" + inTopoBRK + " <None> softline;" + inTopoWE + " <None> softline")
arcpy.AddMessage("did not create merged points with all files...")
print arcpy.GetMessages()
arcpy.AddMessage("created without SB points")
print arcpy.GetMessages()
#
#############################################################
#############################################################
#
## Create TIN
#
out_tin = str(outTinFolder + "\\" + "Seg_" + inSegment + "_" + inYear + "_tin")
try:
arcpy.CreateTin_3d(out_tin, spatialRef, in_features, False)
except arcpy.ExecuteError:
print arcpy.GetMessages()
#
arcpy.DelineateTinDataArea_3d(out_tin, TinDelineationLength, "PERIMETER_ONLY")
# copy TIN for editing
copy_tin = str(outTinFolder + "\\" + "Seg_" + inSegment + "_" + inYear + "_tin_EDT")
arcpy.CopyTin_3d(out_tin, copy_tin)