Saturday, 10 August 2019

numpy - Write array to CSV inside arc script tool (works in pycharm, doesn't work in arc)



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



Answer



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...