首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python:使用模块laspy的列表理解问题

Python:使用模块laspy的列表理解问题
EN

Stack Overflow用户
提问于 2012-10-10 01:57:04
回答 3查看 955关注 0票数 1

最近我明白了使用列表理解的巨大优势。我正在处理存储在一个特殊格式的*.las文件中的几百万个点(x,y,z)。在python中,有两种方法可以使用此格式:

代码语言:javascript
复制
Liblas module  [http://www.liblas.org/tutorial/python.html][1] (in a C++/Python)
laspy module [http://laspy.readthedocs.org/en/latest/tut_part_1.html][2] (pure Python)

我在使用liblas时遇到了几个问题,我希望测试laspy。

在liblas中,我可以这样使用列表理解:

代码语言:javascript
复制
from liblas import file as lasfile
f = lasfile.File(inFile,None,'r') # open LAS
points = [(p.x,p.y) for p in f] # read in list comprehension

在laspy中,我无法描绘如何做同样的事情:

代码语言:javascript
复制
from laspy.file import File
f = file.File(inFile, mode='r')
f
<laspy.file.File object at 0x0000000013939080>
(f[0].X,f[0].Y)
(30839973, 696447860)

我尝试了几种组合,如下:

代码语言:javascript
复制
points = [(p.X,p.Y) for p in f]

但我得到了这样的信息

代码语言:javascript
复制
Traceback (most recent call last):
 File "<interactive input>", line 1, in <module>
AttributeError: Point instance has no attribute 'x'

我尝试使用大写字母而不是大写字母,因为Python区分大小写:

代码语言:javascript
复制
>>> [(p.x,p.y) for p in f]
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
AttributeError: Point instance has no attribute 'x'
>>> [(p.X,p.Y) for p in f]
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
AttributeError: Point instance has no attribute 'X'

这是在交互提示中:

代码语言:javascript
复制
C:\Python27>python.exe
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> from laspy.file import File
>>> inFile="C:\\04-las_clip_inside_area\\Ku_018_class.las"
>>> f = File(inFile, None, 'r')
>>> f
<laspy.file.File object at 0x00000000024D5E10>
>>> points = [(p.X,p.Y) for p in f]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Point instance has no attribute 'X'
>>>

列表后面的print p是:

代码语言:javascript
复制
print dir(p) 
['__doc__', '__init__', '__module__', 'make_nice', 'pack', 'packer', 'reader', 'unpacked']

在循环格式中,我总是有相同的错误

代码语言:javascript
复制
>>> for p in f:
...     print dir(p)
...     print p.X,p.Y
...     
['__doc__', '__init__', '__module__', 'make_nice', 'pack', 'packer', 'reader', 'unpacked']
Traceback (most recent call last):
  File "<interactive input>", line 3, in <module>
AttributeError: Point instance has no attribute 'X'

使用nneonneo建议的代码

代码语言:javascript
复制
import numpy as np
for p in f:
...   points = np.array([f.X, f.Y]).T  

我可以存储在一个数组中

代码语言:javascript
复制
points
array([[ 30839973, 696447860],
       [ 30839937, 696447890],
       [ 30839842, 696447832],
       ..., 
       [ 30943795, 695999984],
       [ 30943695, 695999922],
       [ 30943960, 695999995]])

但是错过了创建列表理解的方法

代码语言:javascript
复制
points = [np.array(p.X,p.Y).T for p in f]
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
AttributeError: Point instance has no attribute 'X'

提前感谢您的帮助。吉安尼

EN

回答 3

Stack Overflow用户

发布于 2012-10-10 02:01:58

Python区分大小写。看起来你需要x属性,但它应该是一个大写的X

票数 3
EN

Stack Overflow用户

发布于 2012-10-10 02:26:43

试一试

代码语言:javascript
复制
import numpy as np
...
points = np.array([f.X, f.Y]).T
票数 1
EN

Stack Overflow用户

发布于 2012-10-10 02:27:39

看起来Point有一个可以显示更多属性的make_nice()方法。

代码语言:javascript
复制
for p in f: p.make_nice()

现在你的列表comp应该可以工作了(X和Y大写--见下面的注释)。

代码语言:javascript
复制
[(p.X,p.Y) for p in f]

注:此答案未经测试。它是基于阅读laspy.util.Point的源代码的。

相关来源:

代码语言:javascript
复制
def make_nice(self):
    '''Turn a point instance with the bare essentials (an unpacked list of data)
    into a fully populated point. Add all the named attributes it possesses, 
    including binary fields.
    '''
    i = 0
    for dim in self.reader.point_format.specs: 
            self.__dict__[dim.name] = self.unpacked[i]
            i += 1

    # rest of method snipped
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12805699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档