Skip to main content

Read and write BlueKenue ASCII files (.i2s, .i3s, .t3s)

Project description

bluekenue

Read and write BlueKenue ASCII files (.i2s, .i3s, .t3s) from Python.

bluekenue ships a small, typed, dependency-light I/O layer for the EnSim 1.0 text format used by BlueKenue (and the wider TELEMAC ecosystem):

Format Purpose Reader Writer
.i2s 2D line set (open or closed) read_i2s write_i2s
.i3s 3D line set (cross-sections, …) read_i3s write_i3s
.t3s 2D triangular scalar mesh read_t3s write_t3s

All three preserve the full header (projection block, multi-attribute specs, ISO-8859-1 encoding, CRLF line endings) on read and write — round-tripping a real-world BlueKenue file does not silently drop metadata.

Installation

pip install python-bluekenue

The distribution is named python-bluekenue on PyPI; the import name is bluekenue (a common pattern, like python-dateutilimport dateutil).

Optional extras:

pip install "python-bluekenue[geometry]"    # adds shapely
pip install "python-bluekenue[projection]"  # adds pyproj
pip install "python-bluekenue[geometry,projection]"  # both

Quickstart

Read a 2D line set (.i2s)

from bluekenue import read_i2s

line_set = read_i2s("emprise.i2s")
print(line_set.header.name, [a.name for a in line_set.header.attributes])
for poly in line_set.polylines:
    print(poly.n_points, "points,", "closed" if poly.is_closed else "open")
    print(poly.coordinates)  # numpy array, shape (n, 2)

Read a 3D line set (.i3s)

from bluekenue import read_i3s

line_set = read_i3s("outline.i3s")
print(line_set.polylines[0].coordinates.shape)  # (n_points, 3)

Read a triangular mesh (.t3s)

from bluekenue import read_t3s

mesh = read_t3s("mesh.t3s")
print(mesh.n_nodes, "nodes,", mesh.n_triangles, "triangles")
print(mesh.nodes)            # (n_nodes, 2)  — X, Y
print(mesh.node_attributes)  # (n_nodes, n_attributes)
print(mesh.triangles)        # (n_triangles, 3) — 1-based BlueKenue indices

Write a synthetic mesh

import numpy as np
from bluekenue import Attribute, BlueKenueHeader, Mesh, write_t3s

header = BlueKenueHeader(
    file_type="t3s",
    name="demo",
    attributes=[Attribute(name="BOTTOM", type="double", units="m")],
)
nodes = np.array([[0, 0], [1, 0], [1, 1], [0, 1]], dtype=float)
triangles = np.array([[1, 2, 3], [1, 3, 4]], dtype=np.int32)  # 1-based
node_attrs = np.array([[0.0], [1.0], [2.0], [1.0]])

write_t3s("demo.t3s", Mesh(header, nodes, triangles, node_attrs))

Convert polylines to shapely

Polyline2D / Polyline3D and LineSet2D / LineSet3D expose to_shapely() / from_shapely() (extra [geometry]).

from bluekenue import read_i2s

line_set = read_i2s("emprise.i2s")
geom = line_set.to_shapely()      # GeometryCollection of LineStrings / LinearRings
ring = line_set.polylines[0].to_shapely()  # LinearRing if closed, LineString otherwise

from shapely.geometry import Polygon
from bluekenue import Polyline2D

polygon = Polygon([(0, 0), (10, 0), (10, 10), (0, 10)])
poly = Polyline2D.from_shapely(polygon)  # uses the exterior ring, returns Polyline2D

If shapely is not installed, methods raise a clear ImportError telling you exactly which extra to install.

Convert the BlueKenue projection to PROJ4 / pyproj

The :Projection block of a BlueKenue file is essentially a PROJ4 string split into named keys. Projection.to_proj4() is pure Python; to_crs / from_crs need pyproj (install with pip install bluekenue[projection]).

from bluekenue import read_t3s

mesh = read_t3s("mesh.t3s")
print(mesh.header.projection.to_proj4())
# +proj=lcc +lon_0=3 +lat_1=49 +lat_2=44 +lat_0=46.5 +x_0=700000 +y_0=6600000 +ellps=GRS80 +units=m +no_defs

crs = mesh.header.projection.to_crs()      # pyproj.CRS

Projection.from_crs is flexible — it accepts anything you can already feed to pyproj plus QGIS' QgsCoordinateReferenceSystem (duck-typed, no QGIS dependency required):

from bluekenue import Projection

proj = Projection.from_crs(2154, name="LambertConformal")          # EPSG int
proj = Projection.from_crs("EPSG:2154", name="LambertConformal")    # EPSG string
proj = Projection.from_crs("+proj=lcc +lat_0=46.5 ...")             # PROJ4 string
proj = Projection.from_crs(pyproj.CRS.from_epsg(2154))              # pyproj.CRS
proj = Projection.from_crs(qgs_layer.crs(), name="LambertConformal")  # QGIS CRS

Development

git clone https://gitlab.com/isl-ingenierie/modules-python/python-bluekenue.git
cd python-bluekenue
pip install -e ".[dev,geometry,projection]"
pre-commit install
pytest

Releasing

See CHANGELOG.md for the version history (format: Keep a Changelog 1.1.0, versions follow Semantic Versioning 2.0.0).

The release flow:

  1. Move entries from ## [Unreleased] to a new ## [VERSION] - YYYY-MM-DD section in CHANGELOG.md.
  2. Commit and tag (git tag VERSION && git push origin VERSION).
  3. GitLab CI uploads to PyPI / TestPyPI via Trusted Publisher (OIDC, no API token stored as a CI variable) and creates a GitLab Release whose body is the matching CHANGELOG section.

The leading v is optional (SemVer 2.0 does not include it; it is purely a Git tag convention). Both 0.1.0 and v0.1.0 work; hatch-vcs strips the v when computing the package version.

Tag pattern Target
0.1.0a1, v0.1.0rc2, 0.1.0.dev3 TestPyPI (pypi-test job)
0.1.0, v1.2.3 PyPI prod (pypi job)
git tag v0.1.0
git push origin v0.1.0

License

BSD 3-Clause.

The library was inspired by the BlueKenue I/O code in pyteltools; credits to its authors for documenting the format.

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

python_bluekenue-0.1.0.tar.gz (98.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_bluekenue-0.1.0-py3-none-any.whl (24.0 kB view details)

Uploaded Python 3

File details

Details for the file python_bluekenue-0.1.0.tar.gz.

File metadata

  • Download URL: python_bluekenue-0.1.0.tar.gz
  • Upload date:
  • Size: 98.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for python_bluekenue-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d5c603ace19475b1433cb9df52b2cbd0e85d5ea5dd0c5e89227203c43016449e
MD5 8a66d1504c848195d30837c51624edae
BLAKE2b-256 f3ef00d829fbbcced4efe701f0f4019a6e19e339d3d49600537680bf73768046

See more details on using hashes here.

File details

Details for the file python_bluekenue-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_bluekenue-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a3f37133ed15c53812c106facfe5f3c20da7d929da56b2394b599cb2cf01c52a
MD5 07316e1c1ea7d56a00978198980b815a
BLAKE2b-256 b7ee6cda9638822ea76a95c81256310cdc659f689a2460f2e4fdd24be24998dc

See more details on using hashes here.

Supported by

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