Skip to main content

Py2DM

A Python module for reading and writing SMS 2DM mesh files.

PyPI - License GitHub Workflow Status GitHub Workflow Status Coveralls github branch CodeFactor Grade PyPI Read the Docs


  • Support for all standard 2DM element types
  • Optional C++ parser extensions
  • Designed with large meshes (>10 million cells) in mind
  • Python version 3.6+*
  • Fully type annotated

*Python versions 3.6 and 3.7 require external packages. The the installation section for details.

The documentation for this project is hosted at Read the Docs.

Basic usage

The following sections cover basic use cases to illustrate the Py2DM interface.

For detailed instructions, performance considerations and advanced use cases, please refer to the documentation.

Reading mesh files

The py2dm.Reader class provides multiple interfaces for extracting mesh data.

For iterator-based access, the .iter_nodes(), .iter_elements() and .iter_node_strings() methods are available. These allow specifying the range of elements to retrieve. Alternatively, the .elements, .nodes, and node_strings properties provide a shorthand for the corresponding iterator's default values.

import py2dm

with py2dm.Reader('path/to/mesh.2dm') as mesh:
    for node in mesh.iter_nodes():
        if node.id % 10 == 0:
            print(node)

# The above will print the following:
'<Node #10: (1200.0, 200.0, 20.0)>'
'<Node #20: (1120.0, 220.0, 10.0)>'
...

To access a given element or node by its unique ID, use the .element() and .node() method respectively:

import py2dm

with py2dm.Reader('path/to/mesh.2dm') as mesh:
    for element in mesh.elements:
        coords = [mesh.node(n).pos for n in element.nodes]
        print(f'Element #{element.id} coordinates:\n'
              f'\t{coords}')

# The above will print the following:
'Element #1 coordinates:'
'    [(1.0, 2.0, 2.0), (2.0, 2.0, 1.5), (2.0, 1.0, 1.75)]'
'Element #2 coordinates:'
'    [(2.0, 2.0, 1.5), (2.0, 1.0, 1.75), (1.0, 1.0, 1.25)]'
...

Writing mesh files

The py2dm.Writer class provides the node(), element(), and node_string() factory methods to add new geometries to the mesh. The factories for nodes and elements will return the ID they were assigned.

with py2dm.Writer('path/to/mesh.2dm') as mesh:

    # Nodes can be instantiated first and added later
    my_node = py2dm.Node(1, -5.0, -5.0, 0.1)
    mesh.node(my_node)

    # Alternatively, you can use the Writer.node() method as a factory
    mesh.node(2, -5.0, 5.0, 0.2)

    # Specifying a negative ID will auto-select it based on the number
    # of existing nodes in the mesh
    mesh.node(-1, 5.0, -5.0, 0.3)
    mesh.node(-1, 5.0, 5.0, 0.2)

    # Similarly, elements can also be created separately or via the
    # factory method
    my_element = py2dm.Element3T(1, 1, 2, 3)
    mesh.element(my_element)

    # Here too you can use a negative value to auto-select an ID
    mesh.element('E3T', -1, 2, 4, 3)

Format support

The 2DM standard has been extended several times by different parties over the years. This led to the original 2DM format specification no longer matching SMS' own implementation, or those of other software packages such as TUFLOW or BASEMENT.

Py2DM attempts to strike a balance of supporting these custom format variants without breaking compatibility with the original standard.

Notable deviations from the 2DM standard

  • The maximum ID limit of 999'999 is not enforced.

  • Floating point values may be used as material IDs by default.

    You can set the allow_float_matid flag to False to quietly discard floating point materials in the mesh:

    with py2dm.Reader('mesh.2dm', allow_float_matid=False) as mesh:
      ...
    
  • Zero-based indices are supported if the zero_index flag is set upon reader instantiation:

    with py2dm.Reader('mesh.2dm', zero_index=True) as mesh:
      my_node = mesh.node(0)  # This would normally cause an error
    

More information on the various 2DM dialects can be found on the Subformats page of the project documentation.

Installation

Py2DM is available on PyPI and can be installed with pip:

python -m pip install --upgrade py2dm

Requirements

Py2DM is written for Python 3.8 and up and requires no additional packages on this version.

For Python versions 3.6 and 3.7 (notably the ones used by QGIS 3 as of writing this), two additional packages are required to provide functionality that was not yet available in the standard library at the time.

The above packages are only required for Python versions 3.6 and 3.7, with Python 3.8+, no third-party dependencies are needed.

Contributing

If you have encountered any bugs or performance issues, please do get in touch via the repository issues.

Similarly, any information on additional subformats or software-specific caveats is highly appreciated.

Metadata

Release files for py2dm 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py2dm 0.2.2
File Size Uploaded
py2dm-0.2.2.tar.gz 33.3 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for py2dm 0.2.2
File
py2dm-0.2.2-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details
py2dm-0.2.2-cp39-cp39-win_amd64.whl CPython 3.9 CPython 3.9 Windows x86-64 Details
py2dm-0.2.2-cp38-cp38-win_amd64.whl CPython 3.8 CPython 3.8 Windows x86-64 Details
py2dm-0.2.2-cp37-cp37m-win_amd64.whl CPython 3.7 CPython 3.7 pymalloc Windows x86-64 Details
py2dm-0.2.2-cp36-cp36m-win_amd64.whl CPython 3.6 CPython 3.6 pymalloc Windows x86-64 Details

Total release size: 256.3 kB

Release files / py2dm-0.2.2.tar.gz

Download URL py2dm-0.2.2.tar.gz
Size 33.3 kB
Tags Source
SHA-256 checksum
How to use checksums
fa16812292abb8162a046089324b568fdd654198d10fd8430df031f8b1eb3209
BLAKE2b-256 checksum
How to use checksums
02d704bea5ab081d0e99770540e6ed66ebe723e5304abe6bfd17c7b11533ee58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.10

Release files / py2dm-0.2.2-cp310-cp310-win_amd64.whl

Download URL py2dm-0.2.2-cp310-cp310-win_amd64.whl
Size 44.6 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
fa78f71ce1e78e91f5cc4ba51b4861e2cd7587685f5324224609403f396ec304
BLAKE2b-256 checksum
How to use checksums
92cdad6ab5e39eccad16b705dbe45719d5256862cf2d238aad23d5aabf083327
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release files / py2dm-0.2.2-cp39-cp39-win_amd64.whl

Download URL py2dm-0.2.2-cp39-cp39-win_amd64.whl
Size 44.6 kB
Tags CPython 3.9 Windows x86-64
SHA-256 checksum
How to use checksums
3c288940f9b31c44e9988b1c47e6720cbee51e1d83bf2f37701e0d6cfb7e52bf
BLAKE2b-256 checksum
How to use checksums
8d0c0eda7a868d6a45511c6c822795910b6af665dd8c768f7a7f022f66a99541
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / py2dm-0.2.2-cp38-cp38-win_amd64.whl

Download URL py2dm-0.2.2-cp38-cp38-win_amd64.whl
Size 44.6 kB
Tags CPython 3.8 Windows x86-64
SHA-256 checksum
How to use checksums
8c39b943f5665cc8cccd2f5d3eac7f06e965378a6378b28a508f2453362d97a6
BLAKE2b-256 checksum
How to use checksums
c99ffc52b1585e5e1ef0c749d292aefdfda4270a6efced2eedb41be5eb4ffbaa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.10

Release files / py2dm-0.2.2-cp37-cp37m-win_amd64.whl

Download URL py2dm-0.2.2-cp37-cp37m-win_amd64.whl
Size 44.6 kB
Tags CPython 3.7 CPython 3.7 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
9802d0159bc19ac64058c535ba016d2e65b687df51288d6649a1a4593a149c5a
BLAKE2b-256 checksum
How to use checksums
fa24a0e2944a41eb219fca4967377edc72ab70529dea2b34ee149caa3d3821ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.7.9

Release files / py2dm-0.2.2-cp36-cp36m-win_amd64.whl

Download URL py2dm-0.2.2-cp36-cp36m-win_amd64.whl
Size 44.7 kB
Tags CPython 3.6 CPython 3.6 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
6e7ff4065885b732b20b17d80f831e919a682693ad0e64ac8bbfed8ee973b794
BLAKE2b-256 checksum
How to use checksums
65f0128050a6991aa35f106d0a82ab26f9691d6ff24e7f761ef43d075585157d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.3 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.12 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.8

Release history Release notifications | RSS feed

This release

0.2.2 This release

6 release files

0.2.1

5 release files

0.1.1

2 release files

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page