Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8639795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:06:38+00:00 2026-06-12T11:06:38+00:00

I wrote a code to read *.las file in Python. *las file are special

  • 0

I wrote a code to read *.las file in Python. *las file are special ascii file where each line is x,y,z value of points.

My function read N. number of points and check if they are inside a polygon with points_inside_poly.

I have the following questions:

  1. When I arrive at the end of the file I get this message: LASException: LASError in "LASReader_GetPointAt": point subscript out of range because the number of points is under the chunk dimension. I cannot figure how to resolve this problem.
  2. a = [file_out.write(c[m]) for m in xrange(len(c))] I use a = in order to avoid video print. Is it correct?
  3. In c = [chunk[l] for l in index] I create a new list c because I am not sure that replacing a new chunk is the smart solution (ex: chunk = [chunk[l] for l in index]).
  4. In a statement if else...else I use pass. Is this the right choice?

Really thank for help. It’s important to improve listen suggestions from expertise!!!!

import shapefile
import numpy
import numpy as np
from numpy import nonzero
from liblas import file as lasfile
from shapely.geometry import Polygon
from matplotlib.nxutils import points_inside_poly  


# open shapefile (polygon)
sf = shapefile.Reader(poly)
shapes = sf.shapes()
# extract vertices
verts = np.array(shapes[0].points,float)

# open las file
f = lasfile.File(inFile,None,'r') # open LAS
# read "header"
h = f.header

# create a file where store the points
file_out = lasfile.File(outFile,mode='w',header= h)


chunkSize = 100000
for i in xrange(0,len(f), chunkSize):
    chunk = f[i:i+chunkSize]

    x,y = [],[]

    # extraxt x and y value for each points
    for p in xrange(len(chunk)):
        x.append(chunk[p].x)
        y.append(chunk[p].y)

    # zip all points 
    points = np.array(zip(x,y))
    # create an index where are present the points inside the polygon
    index = nonzero(points_inside_poly(points, verts))[0]

    # if index is not empty do this otherwise "pass"
    if len(index) != 0:
        c = [chunk[l] for l in index] #Is It correct to create a new list or i can replace chunck?
        # save points
        a = [file_out.write(c[m]) for m in xrange(len(c))] #use a = in order to avoid video print. Is it correct?
    else:
        pass #Is It correct to use pass?

f.close()
file_out.close()

code proposed by @Roland Smith and changed by Gianni

f = lasfile.File(inFile,None,'r') # open LAS
h = f.header
# change the software id to libLAS
h.software_id = "Gianni"
file_out = lasfile.File(outFile,mode='w',header= h)
f.close()
sf = shapefile.Reader(poly) #open shpfile
shapes = sf.shapes()
for i in xrange(len(shapes)):
    verts = np.array(shapes[i].points,float)
    inside_points = [p for p in lasfile.File(inFile,None,'r') if pnpoly(p.x, p.y, verts)]
    for p in inside_points:
        file_out.write(p)
f.close()
file_out.close()

i used these solution:
1) reading f = lasfile.File(inFile,None,’r’) and after the read head because i need in the *.las output file
2) close the file
3) i used inside_points = [p for p in lasfile.File(inFile,None,’r’) if pnpoly(p.x, p.y, verts)] instead of

with lasfile.File(inFile, None, 'r') as f:
...     inside_points = [p for p in f if pnpoly(p.x, p.y, verts)]
...     

because i always get this error message

Traceback (most recent call last):
File “”, line 1, in
AttributeError: _exit_

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:06:40+00:00Added an answer on June 12, 2026 at 11:06 am

    Regarding (1):

    First, why are you using chunks? Just use the lasfile as an iterator (as shown in the tutorial), and process the points one at a time. The following should get write all the points inside the polygon to the output file, by using the pnpoly function in a list comprehension instead of points_inside_poly.

    from liblas import file as lasfile
    import numpy as np
    from matplotlib.nxutils import pnpoly
    
    with lasfile.File(inFile, None, 'r') as f:
        inside_points = (p for p in f if pnpoly(p.x, p.y, verts))
        with lasfile.File(outFile,mode='w',header= h) as file_out:
            for p in inside_points:
                file_out.write(p)
    

    The five lines directly above should replace the whole big for-loop. Let’s go over them one-by-one:

    • with lasfile.File(inFile…: Using this construction means that the file will be closed automatically when the with block finishes.
    • Now comes the good part, the generator expression that does all the work (the part between ()). It iterates over the input file (for p in f). Every point that is inside the polygon (if pnpoly(p.x, p.y, verts)) is added to the generator.
    • We use another with block for the output file
    • and all the points (for p in inside_points, this is were the generator is used)
    • are written to the output file (file_out.write(p))

    Because this method only adds the points that are inside the polygon to the list, you don’t waste memory on points that you don’t need!

    You should only use chunks if the method shown above doesn’t work.
    When using chunks you should handle the exception properly. E.g:

    from liblas import LASException
    
    chunkSize = 100000
    for i in xrange(0,len(f), chunkSize):
        try:
            chunk = f[i:i+chunkSize]
        except LASException:
            rem = len(f)-i
            chunk = f[i:i+rem]
    

    Regarding (2): Sorry, but I fail to understand what you are trying to accomplish here. What do you mean by “video print”?

    Regarding (3): since you are not using the original chunk anymore, you can re-use the name. Realize that in python a “variable” is just a nametag.

    Regarding (4): you aren’t using the else, so leave it out completely.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote some code to read a file in my Java Servlet class. (I'm
New to programming. I wrote a Python code (with help from stackoverflow) to read
I wrote the following code to read the content of a file: #include <ifstream>
I have wrote a code that can read an excel 2007 file using Microsoft
I wrote the following piece of code to read a text file inside of
I wrote some code to read a text file from C drive directly given
I wrote some code to read in a text file and to return an
Hi I wrote a code to read from the database and its working. but
I am trying to write some simple code which will read a text file
I have been trying to write some code in Scala to read a file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.