Search code examples
pythonarcgisarcpy

ArcGIS 10.5 python script integration to 'script/tool' not outputting data


In ArcGIS 10.5 I have a python script that works well in the python window, however, I am trying to integrate it in a tool, mainly to make use of the arcpy.GetParametersAsText() input feature for the input and output file names/paths.

The basic intent of the script is to output a number of attribute fields from a particular feature class into a very specific format for a very specific text file (.PTS survey line file for WinFrog if anyone knows it).

Initially, it was failing due to a Unicode error when trying to .write() to the file object created with .open(). As you can see below I have tried numerous things to ensure that the file object 'outfile' is indeed a regular file object, and not Unicode, that is only defined by a path in the from of a string.

Anyway, now the script will run and complete 'successfully', creating the file in the directory indicated in the tool/script input. But now it is not populated with any data! Again the script works every time when running from the python window any help would be awesome. Fully understand this is user error and that I'm likely overlooking something simple.

Script below, sorry for any display issues, new to Stack Overflow.

import arcp

fcpath = str(arcpy.GetParameterAsText(0))
fc = "r\"" + fcpath + "\""
fields = ["LineName", "Lat_SOL" , "Lon_SOL", "Lat_EOL", "Lon_EOL", "Northing_SOL", "Easting_SOL", "Northing_EOL", "Easting_EOL", "Shape_Length"]
filelocation = str(arcpy.GetParameterAsText(1))
outfile = open("r\"" + filelocation + ".PTS", 'w')

with arcpy.da.SearchCursor(fc, fields) as cursor:
    for row in cursor:
        r1 = ("0,{0},0,0.000,0.000,1,2,65280,0,0.200,0,0,1.000,1,0\n".format(row[0]))
        r2 = ("1,{0},{1},0.0m,0.0m,{2},{3},0.000\n".format(row[1], row[2], row[5], row[6]))
        r3 = ("1,{0},{1},0.0m,0.0m,{2},{3},{4}\n".format(row[3], row[4], row[7], row[8], row[9]))

        outfile.write(r1 + r2 + r3)

Solution

  • In your toolbox parameters, set the parameter 0 datatype as a FeatureClass and the parameter 1 as a File. Both must be inputs.

    import arcpy
    
    fcpath = arcpy.GetParameterAsText(0)
    fields = ["LineName", "Lat_SOL" , "Lon_SOL", "Lat_EOL", "Lon_EOL", "Northing_SOL", "Easting_SOL", "Northing_EOL", "Easting_EOL", "Shape_Length"]
    filelocation = arcpy.GetParameterAsText(1)
    outfile = open(filelocation, 'w')
    
        with arcpy.da.SearchCursor(fc, fields) as cursor:
            for row in cursor:
                r1 = ("0,{0},0,0.000,0.000,1,2,65280,0,0.200,0,0,1.000,1,0\n".format(row[0]))
                r2 = ("1,{0},{1},0.0m,0.0m,{2},{3},0.000\n".format(row[1], row[2], row[5], row[6]))
                r3 = ("1,{0},{1},0.0m,0.0m,{2},{3},{4}\n".format(row[3], row[4], row[7], row[8], row[9]))
    
                outfile.write(r1 + r2 + r3)