Select to view content in your preferred language

Using a Variable for the Class Code parameter in Change LAS Class Codes Arcpy Script

1044
6
Jump to solution
01-31-2023 02:14 PM
twangtx
Occasional Contributor

Hello, 

So I've been racking my brain as to why this isn't working. Basically I have a script to process LiDAR .las data and I have a step to "Change LAS Class Code". And eventually I want to have this in a GP Script Tool or as a Notebook where my coworker(s) just need to put in the inputs.

Per the documentation for the Change LAS Class Code tool, the input format for "class_codes" can be

 For example, a current class code of 5 can be changed to 25 by specifying "5 2" or [[5, 2]]. Likewise, a change to the synthetic class flag can be made by adding the keyword for the desired modification ("5 2 SET" or [[5, 2, "SET"]]). Multiple changes can be specified as a semicolon-delimited string (for example, "5 2; 8 3; 1 4") or as a list of lists (for example, [[5, 2], [8, 3], [1, 4]]).  https://pro.arcgis.com/en/pro-app/latest/tool-reference/3d-analyst/change-las-class-codes.htm 

But when I put in the Input() or arcpy.GetParameterAsText to use as a variable, in the same way the documentation has the change class codes formatted, the script will fail. But when I hard code it into the Geoprocessing step (ex: class_code = [[1,0]]) it works just fine.

Example Code

inputClassCodeChange = input("..."): [[0,1]]

arcpy.ddd.ChangeLasClassCodes(in_las_dataset = lasdsets, class_codes = inputClassCodeChange)

^ This will not work and fails.
 
But hard coding it in like, below, works just fine.
arcpy.ddd.ChangeLasClassCodes(in_las_dataset = lasdsets, class_codes = [[0,1]])
 
I'm trying to figure out what I'm missing. I've tried adding quotes, and some many combinations. 
 
I may just be dumb but any help y'all can give will be greatly apprciated.
 
Thank you
 
Thomas
 
 

 

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

If you want to keep the [[1,2]] format, you can parse it out with json.

import json

inputClassCodeChange = input()

rawString = inputClassCodeChange
rawStringType = type(rawString)

# String to list
res = json.loads(rawString)
resType = type(res)

 

JeffK_0-1675254941098.png

View solution in original post

6 Replies
DanPatterson
MVP Esteemed Contributor

python 'input' uses strings 

did you try the "5 2; 8 3; 1 4" option and parse the input accordingly instead?


... sort of retired...
0 Kudos
twangtx
Occasional Contributor

I tried that as classcode = input("5 2; 8 3; 1 4"), then arcpy.ddd.ChangeLasClassCodes(in_las_dataset = lasdsets, class_codes = classcode), but it would fail.

If you mean like parsing out each individual class change then putting it in a list for the script to loop through each one. I started down that path, but the documentation makes it seem like I should just be able to insert that string, as is, in the parameters and it should work.

Thanks.

0 Kudos
leandroduartebr
New Contributor

teste

0 Kudos
leandroduartebr
New Contributor

teste2

0 Kudos
by Anonymous User
Not applicable

If you want to keep the [[1,2]] format, you can parse it out with json.

import json

inputClassCodeChange = input()

rawString = inputClassCodeChange
rawStringType = type(rawString)

# String to list
res = json.loads(rawString)
resType = type(res)

 

JeffK_0-1675254941098.png

twangtx
Occasional Contributor

Thank you!

I have gotten this to work (or at least not fail)!

One more library of tools I need to learn...

0 Kudos