Skip to main content

Point cloud data processing

Project description

PDAL Python support allows you to process data with PDAL into Numpy arrays. It provides a PDAL extension module to control Python interaction with PDAL. Additionally, you can use it to fetch schema and metadata from PDAL operations.

Installation

PyPI

PDAL Python support is installable via PyPI:

pip install PDAL

GitHub

The repository for PDAL’s Python extension is available at https://github.com/PDAL/python

Python support released independently from PDAL itself as of PDAL 1.7.

Usage

Simple

Given the following pipeline, which simply reads an ASPRS LAS file and sorts it by the X dimension:

json = """
{
  "pipeline": [
    "1.2-with-color.las",
    {
        "type": "filters.sort",
        "dimension": "X"
    }
  ]
}"""

import pdal
pipeline = pdal.Pipeline(json)
count = pipeline.execute()
arrays = pipeline.arrays
metadata = pipeline.metadata
log = pipeline.log

Programmatic Pipeline Construction

The previous example specified the pipeline as a JSON string. Alternatively, a pipeline can be constructed by creating Stage instances and piping them together. For example, the previous pipeline can be specified as:

pipeline = pdal.Reader("1.2-with-color.las") | pdal.Filter.sort(dimension="X")

Stage Objects

  • A stage is an instance of pdal.Reader, pdal.Filter or pdal.Writer.

  • A stage can be instantiated by passing as keyword arguments the options applicable to the respective PDAL stage. For more on PDAL stages and their options, check the PDAL documentation on Stage Objects.

    • The filename option of Readers and Writers as well as the type option of Filters can be passed positionally as the first argument.

    • The inputs option specifies a sequence of stages to be set as input to the current stage. Each input can be either the string tag of another stage, or the Stage instance itself.

  • The Reader, Filter and Writer classes come with static methods for all the respective PDAL drivers. For example, pdal.Filter.head() is a shortcut for pdal.Filter(type="filters.head"). These methods are auto-generated by introspecting pdal and the available options are included in each method’s docstring:

>>> help(pdal.Filter.head)
Help on function head in module pdal.pipeline:

head(**kwargs)
    Return N points from beginning of the point cloud.

    user_data: User JSON
    log: Debug output filename
    option_file: File from which to read additional options
    where: Expression describing points to be passed to this filter
    where_merge='auto': If 'where' option is set, describes how skipped points should be merged with kept points in standard mode.
    count='10': Number of points to return from beginning.  If 'invert' is true, number of points to drop from the beginning.
    invert='false': If true, 'count' specifies the number of points to skip from the beginning.

Pipeline Objects

A pdal.Pipeline instance can be created from:

  • a JSON string: Pipeline(json_string)

  • a sequence of Stage instances: Pipeline([stage1, stage2])

  • a single Stage with the Stage.pipeline method: stage.pipeline()

  • nothing: Pipeline() creates a pipeline with no stages.

  • joining Stage and/or other Pipeline instances together with the pipe operator (|):

    • stage1 | stage2

    • stage1 | pipeline1

    • pipeline1 | stage1

    • pipeline1 | pipeline2

Every application of the pipe operator creates a new Pipeline instance. To update an existing Pipeline use the respective in-place pipe operator (|=):

# update pipeline in-place
pipeline = pdal.Pipeline()
pipeline |= stage
pipeline |= pipeline2

Reading using Numpy Arrays

The following more complex scenario demonstrates the full cycling between PDAL and Python:

  • Read a small testfile from GitHub into a Numpy array

  • Filters the array with Numpy for Intensity

  • Pass the filtered array to PDAL to be filtered again

  • Write the final filtered array to a LAS file and a TileDB array via the TileDB-PDAL integration using the TileDB writer plugin

import pdal

data = "https://github.com/PDAL/PDAL/blob/master/test/data/las/1.2-with-color.las?raw=true"

pipeline = pdal.Reader.las(filename=data).pipeline()
print(pipeline.execute())  # 1065 points

# Get the data from the first array
# [array([(637012.24, 849028.31, 431.66, 143, 1,
# 1, 1, 0, 1,  -9., 132, 7326, 245380.78254963,  68,  77,  88),
# dtype=[('X', '<f8'), ('Y', '<f8'), ('Z', '<f8'), ('Intensity', '<u2'),
# ('ReturnNumber', 'u1'), ('NumberOfReturns', 'u1'), ('ScanDirectionFlag', 'u1'),
# ('EdgeOfFlightLine', 'u1'), ('Classification', 'u1'), ('ScanAngleRank', '<f4'),
# ('UserData', 'u1'), ('PointSourceId', '<u2'),
# ('GpsTime', '<f8'), ('Red', '<u2'), ('Green', '<u2'), ('Blue', '<u2')])
arr = pipeline.arrays[0]

# Filter out entries that have intensity < 50
intensity = arr[arr["Intensity"] > 30]
print(len(intensity))  # 704 points

# Now use pdal to clamp points that have intensity 100 <= v < 300
pipeline = pdal.Filter.range(limits="Intensity[100:300)").pipeline(intensity)
print(pipeline.execute())  # 387 points
clamped = pipeline.arrays[0]

# Write our intensity data to a LAS file and a TileDB array. For TileDB it is
# recommended to use Hilbert ordering by default with geospatial point cloud data,
# which requires specifying a domain extent. This can be determined automatically
# from a stats filter that computes statistics about each dimension (min, max, etc.).
pipeline = pdal.Writer.las(
    filename="clamped.las",
    offset_x="auto",
    offset_y="auto",
    offset_z="auto",
    scale_x=0.01,
    scale_y=0.01,
    scale_z=0.01,
).pipeline(clamped)
pipeline |= pdal.Filter.stats() | pdal.Writer.tiledb(array_name="clamped")
print(pipeline.execute())  # 387 points

# Dump the TileDB array schema
import tiledb
with tiledb.open("clamped") as a:
    print(a.schema)

Executing Streamable Pipelines

Streamable pipelines (pipelines that consist exclusively of streamable PDAL stages) can be executed in streaming mode via Pipeline.iterator(). This returns an iterator object that yields Numpy arrays of up to chunk_size size (default=10000) at a time.

import pdal
pipeline = pdal.Reader("test/data/autzen-utm.las") | pdal.Filter.range(limits="Intensity[80:120)")
for array in pipeline.iterator(chunk_size=500):
    print(len(array))
# or to concatenate all arrays into one
# full_array = np.concatenate(list(pipeline))

Pipeline.iterator() also takes an optional prefetch parameter (default=0) to allow prefetching up to to this number of arrays in parallel and buffering them until they are yielded to the caller.

If you just want to execute a streamable pipeline in streaming mode and don’t need to access the data points (typically when the pipeline has Writer stage(s)), you can use the Pipeline.execute_streaming(chunk_size) method instead. This is functionally equivalent to sum(map(len, pipeline.iterator(chunk_size))) but more efficient as it avoids allocating and filling any arrays in memory.

Accessing Mesh Data

Some PDAL stages (for instance filters.delaunay) create TIN type mesh data.

This data can be accessed in Python using the Pipeline.meshes property, which returns a numpy.ndarray of shape (1,n) where n is the number of Triangles in the mesh.

If the PointView contains no mesh data, then n = 0.

Each Triangle is a tuple (A,B,C) where A, B and C are indices into the PointView identifying the point that is the vertex for the Triangle.

Meshio Integration

The meshes property provides the face data but is not easy to use as a mesh. Therefore, we have provided optional Integration into the Meshio library.

The pdal.Pipeline class provides the get_meshio(idx: int) -> meshio.Mesh method. This method creates a Mesh object from the PointView array and mesh properties.

Simple use of the functionality could be as follows:

import pdal

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

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

Advanced Mesh Use Case

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 psycopg2
import io

pl = (
    pdal.Reader(".../python/test/data/1.2-with-color.las")
    | pdal.Filter.splitter(length=1000)
    | pdal.Filter.delaunay()
)
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()
https://github.com/PDAL/python/workflows/Build/badge.svg

Requirements

  • PDAL 2.3+

  • Python >=3.7

  • Pybind11 (eg pip install pybind11[global])

  • Numpy (eg pip install numpy)

  • Packaging (eg pip install packaging)

  • scikit-build (eg pip install scikit-build)

Changes

3.0.0

2.3.5

2.3.0

  • PDAL Python support 2.3.0 requires PDAL 2.1+. Older PDAL base libraries likely will not work.

  • Python support built using scikit-build

  • readers.numpy and filters.python are installed along with the extension.

  • Pipeline can take in a list of arrays that are passed to readers.numpy

  • readers.numpy now supports functions that return arrays. See https://pdal.io/stages/readers.numpy.html for more detail.

2.0.0

  • PDAL Python extension is now in its own repository on its own release schedule at https://github.com/PDAL/python

  • Extension now builds and works under PDAL OSGeo4W64 on Windows.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PDAL-3.0.2.tar.gz (82.8 kB view details)

Uploaded Source

Built Distributions

PDAL-3.0.2-cp39-cp39-win_amd64.whl (127.8 kB view details)

Uploaded CPython 3.9 Windows x86-64

PDAL-3.0.2-cp39-cp39-macosx_10_15_x86_64.whl (134.0 kB view details)

Uploaded CPython 3.9 macOS 10.15+ x86-64

PDAL-3.0.2-cp38-cp38-win_amd64.whl (128.8 kB view details)

Uploaded CPython 3.8 Windows x86-64

PDAL-3.0.2-cp38-cp38-macosx_10_15_x86_64.whl (133.9 kB view details)

Uploaded CPython 3.8 macOS 10.15+ x86-64

PDAL-3.0.2-cp37-cp37m-win_amd64.whl (129.6 kB view details)

Uploaded CPython 3.7m Windows x86-64

PDAL-3.0.2-cp37-cp37m-macosx_10_15_x86_64.whl (132.5 kB view details)

Uploaded CPython 3.7m macOS 10.15+ x86-64

File details

Details for the file PDAL-3.0.2.tar.gz.

File metadata

  • Download URL: PDAL-3.0.2.tar.gz
  • Upload date:
  • Size: 82.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for PDAL-3.0.2.tar.gz
Algorithm Hash digest
SHA256 fbad878ccab49833a8c7fc6982f8cd98dc9421cc592a1cf762113655806bac15
MD5 bebd05d5556d0d192753b280e3b558a4
BLAKE2b-256 5153c3c7f24a8931c2e9bac642e305527fc8b08c4f3c469269bf130817707aa5

See more details on using hashes here.

Provenance

File details

Details for the file PDAL-3.0.2-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: PDAL-3.0.2-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 127.8 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for PDAL-3.0.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 18694999fc65c5ed6c44f2b690fb5954ddcfa3d9f4e3df0026b07fd7779872b3
MD5 562ec601943a1d04c8bcff00c629d93a
BLAKE2b-256 430d77b760c654cbfb95bb17cfb754b9286705808f52a1d7dd6670dffad081ea

See more details on using hashes here.

Provenance

File details

Details for the file PDAL-3.0.2-cp39-cp39-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: PDAL-3.0.2-cp39-cp39-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 134.0 kB
  • Tags: CPython 3.9, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for PDAL-3.0.2-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 ddc37ca2fb745251848a815da25f82b6e20fc049cc9750d7b54c72399e423cc1
MD5 3eef0adbb84c177183796aa6d6f032a2
BLAKE2b-256 59e3e21d8a62d6d3f621a12a8149dfb7cc9f993860a79a0ff13708dbfbb5da03

See more details on using hashes here.

Provenance

File details

Details for the file PDAL-3.0.2-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: PDAL-3.0.2-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 128.8 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for PDAL-3.0.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 b8bb9d308139aa526cb6cb7abe7262a4945609b3174024ebf7f35a68bb6a3233
MD5 bfbe082868fdbd35f82e94c74d75a2ef
BLAKE2b-256 42f94908fdedd7586296745c3054da51bf009d689476467ba3ac7437f17b8612

See more details on using hashes here.

Provenance

File details

Details for the file PDAL-3.0.2-cp38-cp38-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: PDAL-3.0.2-cp38-cp38-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 133.9 kB
  • Tags: CPython 3.8, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for PDAL-3.0.2-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 4ceb5bd3e422dae62f4eff2d08a0ea1163aca1c7ebf12b34e39b729eef8c3710
MD5 5761474185642e9289917657e25034e7
BLAKE2b-256 ca0a5e7daf2570de4a2261b84916895d8fa16e4a4524496840b767b3eacdfaf6

See more details on using hashes here.

Provenance

File details

Details for the file PDAL-3.0.2-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: PDAL-3.0.2-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 129.6 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for PDAL-3.0.2-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 e3d6ca31d53324aaa324457d2acef9a367deede856bee3da00488a2125f2f004
MD5 61f7892f7853c08bc35fb4bbf2482190
BLAKE2b-256 ce018b88a5d3b06578815fb7468c29a49026273ff1c3c91e7a9977fa1de6176c

See more details on using hashes here.

Provenance

File details

Details for the file PDAL-3.0.2-cp37-cp37m-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: PDAL-3.0.2-cp37-cp37m-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 132.5 kB
  • Tags: CPython 3.7m, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for PDAL-3.0.2-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 086c7181ba125fc836a45fe88b91b055a98b7ce453dcab888d90826b5a4ff264
MD5 27961a0213360c532e70140393c2bff5
BLAKE2b-256 4227639b7d8975c2205e00d1332a721ce5cf742eb5a50f87f645507076b1f6e3

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page