I am trying to get my first python/arcpy script going, but I am stuck with implementing it into ArcGIS Pro. If my script is running inside the ArcGIS Pro Python-Window everything is executed well and a new Layer is added to the workspace. But if I try to use it as a tool via geoprocessing with editable input and output the script still executes without any errors but no new layer with the desired data appears inside the GDB/workspace. I need to implement it as a tool to be able to use it as part of an existing ModelBuilder process.
This is the attribute table of the layer, which I want to edit with the script:
FID | Shape | object | drain |
---|---|---|---|
1 | point | 44_6547/2 | [55.27517, -142.08384] ; [-71.56405, 5.49818] |
2 | point | 44_6543/6 | [45.58417, -50.15136] ; [-40.11681, 5.49818] ; [-28.57185, 128.38152] ; [-48.65803, -78.15412] |
3 | point | 44_6894/5 | [9.51578, -20.82284] ; [3.92250, 78.47350]; [-12.93206, 130.76959] |
4 | point | 44_6594/2 | [33.01124, -85.81893] |
This is my arcpy script:
import arcpy
# I WANT SOME KIND OF THIS TO WORK (LIKE PREPARED STATEMENTS):
# input_layer = arcpy.GetParameterAsText(0)
# output_layer = arcpy.GetParameterAsText(1)
# THIS IS WORKING:
input_layer = 'C:/Users/... local path .../layer/testLayer.shp'
workspace = arcpy.env.workspace
new_layer = arcpy.CreateFeatureclass_management(workspace,'_GetLatLng','POINT')
arcpy.AddField_management(new_layer, 'object', 'STRING', field_length = 255)
arcpy.AddField_management(new_layer, 'lat', 'FLOAT', field_length = 50)
arcpy.AddField_management(new_layer, 'lng', 'FLOAT', field_length = 50)
fields_insert = ['object', 'lat', 'lng']
insert_cursor = arcpy.da.InsertCursor(new_layer, fields_insert)
fields_search = ['Shape', 'object', 'drain']
search_cursor = arcpy.da.SearchCursor(input_layer, fields_search)
for x in search_cursor:
a = x[2].split(' ; ')
for i in a:
print(i)
e = i.split(', ')
lat = e[0].lstrip('[')
lng = e[1].rstrip(']')
insert_cursor.insertRow((x[1], float(lat), float(lng)))
del search_cursor, insert_cursor
This is how I tried to setup the tool in ArcGIS Pro:
Updated code part after comment from @gotchula
inFC = arcpy.GetParameterAsText(0)
outFC = arcpy.GetParameterAsText(1)
workspace = arcpy.env.workspace
spatial_ref = arcpy.Describe(inFC).spatialReference
testedFC = arcpy.ValidateTableName(outFC, workspace)
newFC = arcpy.CreateFeatureclass_management(workspace, testedFC, 'POINT', spatial_ref)
But now I receive an error message about the newFC
line.
My desired input type is Feature-Class in File-Geodatabase
and my output could either be a feature-class
or feature-layer
. Does this has to match with the datatypes I declare in the tool properties? Because if I declare Feature-Class
instead of Feature-Layer
there appear no choices in the list when running the tool...