0

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, even if its just identifying the problem. 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)

Update per MikeWatt's suggestions, still not working.

import arcpy, os

fcpath = arcpy.GetParameterAsText(0)
fields = ["LineName", "Lat_SOL" , "Lon_SOL", "Lat_EOL", "Lon_EOL", "Northing_SOL", "Easting_SOL", "Northing_EOL", "Easting_EOL", "Shape_Length"]
outfile = open(arcpy.GetParameterAsText(1) + ".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)



outfile.close()

latest attempt to get anything into the output file. Still nothing. Very strange again as this script works no worries when copied straight into the ArcGIS python window.

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"]
outfile = open(arcpy.GetParameterAsText(1) + ".PTS", 'w')


outfile.write("TEST\n" + "Test\n" + "TEST\n")



outfile.close()

The attached ScreenGrab is the Parameters window of the 'Add Script' tool. I could be messing this aspect of it up as well.

Add script parameter window, perhaps this aspect of the setup is messin git all up?

7
  • 1
    The way you're building the paths doesn't look to be valid-- you don't need to include quotes within the path string itself, nor should you include an "r" at the beginning. That's only if you want to define a raw string literal within the script, which you aren't doing since it comes from the output of GetParameterAsText(). Also it's good practice to explicitly close the file after writing to ensure it gets flushed to disk, either with outfile.close() or using a with block to open it.
    – mikewatt
    Feb 20, 2019 at 18:37
  • 1
    Use os.path.join to build paths
    – BERA
    Feb 20, 2019 at 18:47
  • So the paths seem to be working, at least for the output file, as the file is being created in the right directory. However still no data being parsed and populated. Taking mikewatt's recommendations here is that script now, still not working. This is more or less how it was initially, the "r"-s were an attempt to make sure ArcPython (different from reg. python) would read the path when I was struggling with the Unicode error described. EDIT: script added to original post as I could not add here.* Feb 20, 2019 at 18:54
  • Try writing something manually, for example outfile.write("test\n"+"test2\n"+"test3\n") instead of the cursor.
    – BERA
    Feb 20, 2019 at 19:13
  • Make sure you're actually entering your loop by printing row
    – mikewatt
    Feb 20, 2019 at 19:14

1 Answer 1

0

Thanks for everyone's answers. Below is the code I got to work for this particular issue.

import arcpy, os
from datetime import datetime

#This script takes lineplans created in Arcgis, or any polyline file, with the appropraite attributes and outputs 
#a properly formatted PTS file for WinFrog.
#This scipt outputs to a static directory here on our vessel's network, idealy the output location would be an
#input also, however that proved to be a stumbling block when actually setting up the parameters in the arc tool setup.
#this works for us, but it would be easy to make 'outfile' a parameter and output it. Thats been an Arc problem though, not
#a python issue.


#set feature class input for ArcGIS tool.
fc = arcpy.GetParameterAsText(0)

#Create date object for naming scheme.
date_object = datetime.now()

#parse out the input feature classes name from the path, I think you can do this more easily with os.Path utilites, but I am still learning.
infile = str(fc)
tmp = infile.split("\\")
filetmp = tmp[-1].split(".")
outfilename = filetmp[0]

#set up fields for search cursor
fields = ["LineName", "Lat_SOL" , "Lon_SOL", "Lat_EOL", "Lon_EOL", "Northing_SOL", "Easting_SOL", "Northing_EOL", "Easting_EOL", "Shape_Length"]

#setup outfile object.
outfile = open("A:\\J18350-TGS-WestAfrica_Leg_2\\FromVessel\\Cad\\LinePlan_Modifications\\" + outfilename + date_object.strftime('_%Y%m%d') + ".PTS" ,'w')

#use search cursor to parse desired fields and populate strings needed to create PTS file.
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)

#close output file.
outfile.close()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.