PDAL / python

PDAL's Python Support
Other
116 stars 34 forks source link

Add Meshes to the pdal API #82

Closed runette closed 3 years ago

runette commented 3 years ago

Adds the ability to access face data from meshes in the Python API:

All of the above is working on my builds and does not introduce any new dependencies. However, I also wanted to create a way to create a useable Mesh object easily.

NOTE - where there is no mesh attached to the PointView, get_meshes will return an ndarray with 0 rows to ensure that the correspondence between arrays and meshes can be retained.

One good way of doing that is using the Meshio package - which allows the Mesh object to be re-used by a wide range of other packages.

So - I have also included the following :

Usage

The simplest usage would look like the following:

import pdal

...
pl = pdal.Pipeline(pipeline)
pl.execute()

mesh = pl.get_meshio(0)
mesh.write('test.obj')

NOTE that since we are getting back a vector of meshes - we can do something that was not easy before and was actually the use-case that I needed to solve:

USE-CASE : Take a LiDAR map, create a mesh from the ground points, split into tiles and store the tiles in PostGIS.

(example using 1.2-with-color.las and not doing the ground classification for clarity)

import pdal
import json
import psycopg2
import io

pipe = [
     '.../python/test/data/1.2-with-color.las',
     {"type":  "filters.splitter", "length": 1000}, 
     {"type":  "filters.delaunay"}
]

pl = pdal.Pipeline(json.dumps(pipe))
pl.execute()

conn = psycopg(%CONNNECTION_STRING%)
buffer = io.StringIO

for idx in range(len(pl.meshes)):
    m =  pl.get_meshio(idx)
    if m:
        m.write(buffer,  file_format = "wkt")
        with conn.cursor() as curr:
           curr.execute(
               "INSERT INTO %table-name% (mesh) VALUES (ST_GeomFromEWKT(%(ewkt)s)", 
               { "ewkt": buffer.getvalue()}
           )

conn.commit()
conn.close()
buffer.close()

This creates 24 tile meshes in PostGIS

runette commented 3 years ago

Can you verify that it doesn't leak PDAL objects?

I am not aware of any leaks and tried to follow the existing Array structure as closely as possible - but as usual I don't know what I don't know!

hobu commented 3 years ago

Please add a test that does filters.delaunay on https://github.com/PDAL/python/blob/master/test/data/1.2-with-color.las and tests that you get what you expect. With that, I think we're good to merge.

runette commented 3 years ago

Please add a test that does filters.delaunay on https://github.com/PDAL/python/blob/master/test/data/1.2-with-color.las and tests that you get what you expect. With that, I think we're good to merge.

Will do - probably tomorrow

runette commented 3 years ago

@hobu Added tests and documentation

hobu commented 3 years ago

2.4.0 pushed https://pypi.org/project/PDAL/