Skip to main content

Python module for 3D tiles format

Project description

https://secure.travis-ci.org/Oslandia/py3dtiles.png https://badge.fury.io/py/py3dtiles.svg

py3dtiles

Python module to manage 3DTiles format.

For now, only the Point Cloud and the Batched 3D Model specifications are supported.

py3dtiles is distributed under LGPL2 or later.

Install

From sources

To use py3dtiles from sources:

$ git clone https://github.com/Oslandia/py3dtiles
$ cd py3dtiles
$ virtualenv -p /usr/bin/python3 venv
$ . venv/bin/activate
(venv)$ pip install -e .
(venv)$ python setup.py install

If you wan to run unit tests:

(venv)$ pip install nose
(venv)$ nosetests
...

Specifications

Generic Tile

The py3dtiles module provides some classes to fit into the specification:

  • Tile with a header TileHeader and a body TileBody

  • TileHeader represents the metadata of the tile (magic value, version, …)

  • TileBody contains varying semantic and geometric data depending on the the tile’s type

Moreover, a utility class TileReader is available to read a tile file as well as a simple command line tool to retrieve basic information about a tile: py3dtiles_info. We also provide a utility to generate a tileset from a list of 3D models in WKB format or stored in a postGIS table.

How to use py3dtiles_info

Here is an example on how to retrieve basic information about a tile, in this case pointCloudRGB.pnts:

$ py3dtiles_info tests/pointCloudRGB.pnts
Tile Header
-----------
Magic Value:  pnts
Version:  1
Tile byte length:  15176
Feature table json byte length:  148
Feature table bin byte length:  15000

Feature Table Header
--------------------
{'POSITION': {'byteOffset': 0}, 'RGB': {'byteOffset': 12000}, 'POINTS_LENGTH': 1000, 'RTC_CENTER': [1215012.8828876738, -4736313.051199594, 4081605.22126042]}

First point
-----------
{'Z': -0.17107764, 'Red': 44, 'X': 2.19396, 'Y': 4.4896851, 'Green': 243, 'Blue': 209}

How to use export_tileset

Two export modes are available, the database export or the directory export. They both transform all the geometries provided in .b3dm files, along with a tileset.json file which organizes them.

The directory export will use all the .wkb files in the provided directory. Warning: the coordinates are read as floats, not doubles. Make sure to offset the coordinates beforehand to reduce their size. Afterwards, you can indicate in the command line the offset that needs to be applied to the tileset so it is correctly placed. Usage example:

$ export_tileset -d my_directory -o 10000 10000 0

The database export requires a user name, a database name, the name of the table and its column that contains the geometry and (optionaly) the name of the column that contains the object’s ID. Usage example:

$ export_tileset -D database -t my_city -c geom -i id -u oslandia

Point Cloud

Points Tile Format: https://github.com/AnalyticalGraphicsInc/3d-tiles/tree/master/TileFormats/PointCloud

In the current implementation, the Pnts class only contains a FeatureTable (FeatureTableHeader and a FeatureTableBody, which contains features of type Feature).

How to read a .pnts file

>>> from py3dtiles import TileReader
>>> from py3dtiles import Pnts
>>>
>>> filename = 'tests/pointCloudRGB.pnts'
>>>
>>> # read the file
>>> tile = TileReader().read_file(filename)
>>>
>>> # tile is an instance of the Tile class
>>> tile
<py3dtiles.tile.Tile>
>>>
>>> # extract information about the tile header
>>> th = tile.header
>>> th
<py3dtiles.tile.TileHeader>
>>> th.magic_value
'pnts'
>>> th.tile_byte_length
15176
>>>
>>> # extract the feature table
>>> ft = tile.body.feature_table
>>> ft
<py3dtiles.feature_table.FeatureTable
>>>
>>> # display feature table header
>>> ft.header.to_json()
{'RTC_CENTER': [1215012.8828876738, -4736313.051199594, 4081605.22126042],
'RGB': {'byteOffset': 12000}, 'POINTS_LENGTH': 1000, 'POSITION': {'byteOffset': 0}}
>>>
>>> # extract positions and colors of the first point
>>> f = ft.feature(0)
>>> f
<py3dtiles.feature_table.Feature>
>>> f.positions
{'Y': 4.4896851, 'X': 2.19396, 'Z': -0.17107764}
>>> f.colors
{'Green': 243, 'Red': 44, 'Blue': 209}

How to write a .pnts file

To write a Point Cloud file, you have to build a numpy array with the corresponding data type.

>>> from py3dtiles import Feature
>>> import numpy as np
>>>
>>> # create the numpy dtype for positions with 32-bit floating point numbers
>>> dt = np.dtype([('X', '<f4'), ('Y', '<f4'), ('Z', '<f4')])
>>>
>>> # create a position array
>>> position = np.array([(4.489, 2.19, -0.17)], dtype=dt)
>>>
>>> # create a new feature from a uint8 numpy array
>>> f = Feature.from_array(dt, position.view('uint8'))
>>> f
<py3dtiles.feature_table.Feature>
>>> f.positions
{'Y': 2.19, 'X': 4.489, 'Z': -0.17}
>>>
>>> # create a tile directly from our feature. None is for "no colors".
>>> t  = Pnts.from_features(dt, None, [f])
>>>
>>> # the tile is complete
>>> t.body.feature_table.header.to_json()
{'POINTS_LENGTH': 1, 'POSITION': {'byteOffset': 0}}
>>>
>>> # to save our tile as a .pnts file
>>> t.save_as("mypoints.pnts")

Batched 3D Model

Batched 3D Model Tile Format: https://github.com/AnalyticalGraphicsInc/3d-tiles/tree/master/TileFormats/Batched3DModel

How to read a .b3dm file

>>> from py3dtiles import TileReader
>>> from py3dtiles import B3dm
>>>
>>> filename = 'tests/dragon_low.b3dm'
>>>
>>> # read the file
>>> tile = TileReader().read_file(filename)
>>>
>>> # tile is an instance of the Tile class
>>> tile
<py3dtiles.tile.Tile>
>>>
>>> # extract information about the tile header
>>> th = tile.header
>>> th
<py3dtiles.b3dm.B3dmHeader>
>>> th.magic_value
'b3dm'
>>> th.tile_byte_length
47246
>>>
>>> # extract the glTF
>>> gltf = tile.body.glTF
>>> gltf
<py3dtiles.gltf.GlTF>
>>>
>>> # display gltf header's asset field
>>> gltf.header['asset']
{'premultipliedAlpha': True, 'profile': {'version': '1.0', 'api': 'WebGL'}, 'version': '1.0', 'generator': 'OBJ2GLTF'}

How to write a .b3dm file

To write a Batched 3D Model file, you have to import the geometry from a wkb file containing polyhedralsurfaces or multipolygons.

>>> import numpy as np
>>> from py3dtiles import GlTF, TriangleSoup
>>>
>>> # load a wkb file
>>> wkb = open('tests/building.wkb', 'rb').read()
>>>
>>> # define the geometry's bouding box
>>> box = [[-8.75, -7.36, -2.05], [8.80, 7.30, 2.05]]
>>>
>>> # define the geometry's world transformation
>>> transform = np.array([
...             [1, 0, 0, 1842015.125],
...             [0, 1, 0, 5177109.25],
...             [0, 0, 1, 247.87364196777344],
...             [0, 0, 0, 1]], dtype=float)
>>> transform = transform.flatten('F')
>>>
>>> # use the TriangleSoup helper class to transform the wkb into arrays
>>> # of points and normals
>>> ts = TriangleSoup.from_wkb_multipolygon(wkb)
>>> positions = ts.getPositionArray()
>>> normals = ts.getNormalArray()
>>> # generate the glTF part from the binary arrays.
>>> # notice that from_binary_arrays accepts array of geometries
>>> # for batching purposes.
>>> geometry = { 'position': positions, 'normal': normals, 'bbox': box }
>>> gltf = GlTF.from_binary_arrays([geometry], transform)
>>>
>>> # create a b3dm tile directly from the glTF.
>>> t = B3dm.from_glTF(glTF)
>>>
>>> # to save our tile as a .b3dm file
>>> t.save_as("mymodel.b3dm")

Third party assets

Dragon model from Analytical Graphics Inc.’s 3d-tiles samples

Earcut-python by Joshua Skelton

ISC License

Copyright (c) 2016, Mapbox

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

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

py3dtiles-1.0.2.tar.gz (39.3 kB view details)

Uploaded Source

Built Distribution

py3dtiles-1.0.2-py3-none-any.whl (29.2 kB view details)

Uploaded Python 3

File details

Details for the file py3dtiles-1.0.2.tar.gz.

File metadata

  • Download URL: py3dtiles-1.0.2.tar.gz
  • Upload date:
  • Size: 39.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for py3dtiles-1.0.2.tar.gz
Algorithm Hash digest
SHA256 f81dcff5808dedd525923f5eea0c14b00c9ac1faa9eadc7cb4e03b99ec3f54dd
MD5 f9022788413d22f7cf7984d168745092
BLAKE2b-256 a6892fe235de11eb17b10400db71e6bf4ee68bc73e24b9fa0f07b985ef9dab34

See more details on using hashes here.

File details

Details for the file py3dtiles-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for py3dtiles-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 72d03fc726f4c1d670e4b5055fb8cdadc55767625f75590ed1485f7227792a41
MD5 8043eb96ac74d2ee70f521e385753259
BLAKE2b-256 f0ff831472a4908823b0d8ab737c666c973de2e1bc8e454126e24878c0e2cf96

See more details on using hashes here.

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