-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
58 lines (51 loc) · 2.43 KB
/
script.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
import arcpy
import os
import ConversionUtils
def fName(path):
bname = os.path.basename(path).split('.')[0]
for x in bname:
if x in ['-','&']:
bname = bname.replace(x,'_')
return bname
def cleanUp(feature):
arcpy.Delete_management(feature)
def gpxtoPolygon(gpxFiles, name_desc_col, outputFeature, computeArea='', areaUnit=''):
try:
polygons={}
gpxFiles = ConversionUtils.SplitMultiInputs(gpxFiles)
for gpxfile in gpxFiles:
arcpy.GPXtoFeatures_conversion(gpxfile, r'in_memory\tempFile{}'.format(fName(gpxfile)))
with arcpy.da.SearchCursor(r'in_memory\tempFile{}'.format(fName(gpxfile)),['Shape@XYZ',name_desc_col]) as sc:
for row in sc:
if row[1] not in polygons.keys():
polygons[row[1]] = arcpy.Array(arcpy.Point(row[0][0],row[0][1]))
else:
polygons[row[1]].append(arcpy.Point(row[0][0],row[0][1]))
cleanUp(r'in_memory\tempFile{}'.format(fName(gpxfile)))
arcpy.CreateFeatureclass_management(os.path.dirname(outputFeature), os.path.basename(outputFeature),'POLYGON','','','',arcpy.SpatialReference(4326))
arcpy.AddField_management(outputFeature, name_desc_col, 'TEXT')
with arcpy.da.InsertCursor(outputFeature,['Shape@',name_desc_col]) as ic:
for key,value in polygons.items():
plot = arcpy.Polygon(value, arcpy.SpatialReference(4326))
ic.insertRow((plot, key))
if computeArea == 'true':
fieldName = 'Area_'
count = 0
while len(fieldName) != 10:
fieldName+=areaUnit[count]
count+=1
arcpy.AddField_management(outputFeature, fieldName, 'DOUBLE')
arcpy.CalculateField_management(outputFeature, fieldName, '!shape.area@{}!'.format(areaUnit.lower()),'PYTHON')
except arcpy.ExecuteError as err:
arcpy.AddError(err)
try:
if arcpy.Exists(r'in_memory\tempFile{}'.format(fName(gpxfile))):
arcpy.Delete_management(r'in_memory\tempFile{}'.format(fName(gpxfile)))
except arcpy.ExecuteError as err2:
arcpy.AddError(err2)
finally:
arcpy.AddMessage('Cleaning up.................')
del(polygons) #clean up
if __name__=='__main__':
args = tuple(arcpy.GetParameterAsText(i)for i in range(arcpy.GetArgumentCount()))
gpxtoPolygon(*args)