I wrote this function to read Las file and save a shapefile. The function creates a shapefile with 8 fields. What i wish insert a parse element in the function in order to select the fields i wish to save LAS2SHP(inFile,outFile=None,parse=None). if None all fields are saved. if parse is
parse=”irn” the fields intensity, return_number, and number_of_returns are saved. following the legend
"i": p.intensity,
"r": p.return_number,
"n": p.number_of_returns,
"s": p.scan_direction,
"e": p.flightline_edge,
"c": p.classification,
"a": p.scan_angle,
I wrote a solution if….ifelse….else really code consuming (and not elegant). Thanks for all helps and suggestions for saving code
thanks in advance
Gianni
here the original function in python
import shapefile
from liblas import file as lasfile
def LAS2SHP(inFile,outFile=None):
w = shapefile.Writer(shapefile.POINT)
w.field('Z','C','10')
w.field('Intensity','C','10')
w.field('Return','C','10')
w.field('NumberRet','C','10')
w.field('ScanDir','C','10')
w.field('FlightEdge','C','10')
w.field('Class','C','10')
w.field('ScanAngle','C','10')
for p in lasfile.File(inFile,None,'r'):
w.point(p.x,p.y)
w.record(float(p.z),float(p.intensity),float(p.return_number),float(p.number_of_returns),float(p.scan_direction),float(p.flightline_edge),float(p.classification),float(p.scan_angle))
if outFile == None:
inFile_path, inFile_name_ext = os.path.split(os.path.abspath(inFile))
inFile_name = os.path.splitext(inFile_name_ext)[0]
w.save("{0}\\{1}.shp".format(inFile_path,inFile_name))
else:
w.save(outFile)
Perhaps try something like this:
for key in parseloops through the letters inparse. For example,if
parse = 'irn'then key loops through the valuesi,r,n.pattris a dict.pattr[key]is the name of the associatedattribute. For example,
pattr['i']is"intensity".getattr(p, pattr[key])is the value of thepattr[key]attributein
p. For example,getattr(p, "intensity")isp.intensity. It is the way to get attribute values when you know the name of the attribute as a string, (e.g.pattr[key]).The
*inw.record(*pdata)unpackspdatabefore sending the arguments on tow.record. For example,w.record(*[1,2,3])is equivalent tow.record(1,2,3). It is the way one sends an arbitrary number of arguments to a function.For example,