1

I have an arcpy script in which I am writing an array to a CSV (for later use in the script to make a routeing layer).

I've tried a couple iterations, and all work like a charm in PyCharm (can't pass up a pun, no matter how weak). But when I convert to script tool and run in the catalog in ArcMap 10.6, it doesn't write the CSV. (Other numpy operations work fine.) In my multiple scripting attempts, the CSV file is opened and headers are written fine; it poops out on writing the array.

When I check x.shape in PyCharm, it shows the right dimensions (91L, 4L) When I check x.shape in Arc python shell, it shows the wrong dimensions [0, 4]

I don't understand why ArcGIS sees the array as empty.

nom_rup = arcpy.GetParameterAsText(0)
route_offset_km = arcpy.GetParameterAsText(1)
route_ptsSpacing_km = arcpy.GetParameterAsText(2)
eqk_name = arcpy.GetParameterAsText(3)
target_epsg = arcpy.GetParameterAsText(4)
outpath = arcpy.GetParameterAsText(5)


# in arc script tool, fields defined as follows:
# nom_rup > shapefile [note not needed for this self-contained snippet; so can browse to any SHP]
# route_offset_km > double, I entered 5
# route_ptsSpacing_km > double, I entered 1
# eqk_name > string [note not needed for this self-contained snippet; so can enter abc123]
# target_epsg > double [note not needed for this self-contained snippet; so can enter 12345]
# outpath > folder, I browsed to my output folder

rup_len_km = 89.932 # this is calculated elsewhere in the script, so I define it here in this self-contained snippet

start = 0
import numpy
stop1 = (numpy.ceil(rup_len_km) + 1) * 1000
step = route_ptsSpacing_km * 1000

start = float(start)
stop1 = float(stop1)
step = float(step)

# Execute
descritizings = numpy.arange(start, stop1, step)

# Clean up
del start, stop1, step

off1 = numpy.full((len(descritizings)), route_offset_km * 1000)
off2 = numpy.full((len(descritizings)), route_offset_km * -1000)
rt = numpy.ones((len(descritizings)))

route_table = numpy.array([descritizings, off1, rt, off2])
route_table = route_table.transpose()


outFile = outpath + '\\' + '_RouteTable.csv'
numpy.savetxt(outFile, route_table, fmt = '%d', delimiter = ',', header = "LOCATION, OFFSET1, ROUTE, OFFSET2", comments = '')

if I use

off1 = numpy.ones((len(descritizings))) * route_offset_km * 1000
off2 = numpy.ones((len(descritizings))) * route_offset_km * -1000

instead, as suggested in a comment below, here's the error I get (note line 170 is from the longer script but corresponds to the line that begins with "off1..."

enter image description here

  • I also tried the following to write the csv file: <<<<< heads = ['LOCATION', 'OFFSET1', 'ROUTE', 'OFFSET2'] import csv # writing to csv file with open(outFile, 'w') as csvfile: # creating a csv writer object csvwriter = csv.writer(csvfile) # writing the fields csvwriter.writerow(heads) # writing the data rows csvwriter.writerows(route_table) >>>>> when running it through the arc script, it still produces a CSV as shown above (i.e., writes headers but that's all) (EDIT, I don't know why this is showing up collapsed and hard to read, apologies) – AlexS1 Sep 11 '18 at 18:55
  • the script is 500 lines long, I assumed no one would want to read through the first 215 lines or the last 260 lines. Editing now based on more troubleshooting. – AlexS1 Sep 11 '18 at 22:46
  • edited; works in pycharm. if I add print route_table.shape it shows correct dimensions in pycharm. if I add arcpy.AddMessage(route_table.shape) it shows incorrect (empty) dimensions in arc – AlexS1 Sep 11 '18 at 23:08
  • Gotcha, thanks. Just recognized that in my arc tool, I am entering these values... so, it has been edited again. Maybe the issue is I am entering some values as "double" in the arc tool script? not sure what other type to use? thx – AlexS1 Sep 11 '18 at 23:28
  • true that!! awesome, let me give it a whirl--when I have luck, I'll let you know and you maybe you can convert the comment to an answer so I can upvote it – AlexS1 Sep 11 '18 at 23:42
1

GetParameterAsText returns the parameter values "as text".

Use

var = int(arcpy.GetParameterAsText(index))

and/or

var = float(arcpy.GetParameterAsText(index))

to convert the numeric parameter text to the appropriate type of number.

Replace var with your actual variable name as appropriate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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