MeshToGeotiff - A fast Python algorithm to convert a 3D mesh into a GeoTIFF

Overview

MeshToGeotiff - A fast Python algorithm to convert a 3D mesh into a GeoTIFF

Python class for converting (very fast) 3D Meshes/Surfaces to Raster DEMs (as Geotiff) or regular point cloud grids.

  • Supports gridding overlapping surfaces (e.g. highest, lowest, or first result)
  • Supports output to regular x,y,z grid
  • Supports output to Geotiff DEMs
  • Supports point colour averaging (and outputting colour raster to TIF with heightmap)

Example of output

Motivation

I worked on a project that used polygonal meshes and wanted to integrate parts that relied on raster computations. There was a need to shift between the two paradigms without having to wait too long for conversion.

I couldn't find anything fast enough in Python to seamlessly transition between mesh and rasters. This uses Numba for parallel loops and has been heavily optimised for computation speed (intended to compete with c++ benchmarks for similar computations).

The benchmarks below indicate speeds expected on an average PC (at least for 3D processing purposes).

Installation Instructions

With pip

Requires rasterio (which also needs gdal). These libraries are easier installed from pre-compiled wheels.

You will need rasterio and gdal. The easiest way to install these will be a pre-compiled versions for your platform from:

Python 3.7, Windows, amd64:

python -m pip install https://download.lfd.uci.edu/pythonlibs/w6tyco5e/GDAL-3.3.3-cp37-cp37m-win_amd64.whl
python -m pip install https://download.lfd.uci.edu/pythonlibs/w6tyco5e/rasterio-1.2.10-cp37-cp37m-win_amd64.whl

Python 3.8, Windows, amd64:

python -m pip install https://download.lfd.uci.edu/pythonlibs/w6tyco5e/GDAL-3.3.3-cp38-cp38-win_amd64.whl
python -m pip install https://download.lfd.uci.edu/pythonlibs/w6tyco5e/rasterio-1.2.10-cp38-cp38-win_amd64.whl

Python 3.9, Windows, amd64:

python -m pip install https://download.lfd.uci.edu/pythonlibs/w6tyco5e/GDAL-3.3.3-cp39-cp39-win_amd64.whl
python -m pip install https://download.lfd.uci.edu/pythonlibs/w6tyco5e/rasterio-1.2.10-cp39-cp39-win_amd64.whl

With those satisfied it should be fine to pip install this:

python -m pip install git+https://github.com/jeremybutlermaptek/mesh_to_geotiff

or

python -m pip install https://github.com/jeremybutlermaptek/mesh_to_geotiff/raw/main/dist/mesh_to_geotiff-0.1.0-py3-none-any.whl

When running examples, if you see this error: ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject

Update numpy to 1.2 and/or reinstall:

python -m pip install --upgrade --force-reinstall numpy

To create a wheel for this:

python setup.py bdist_wheel

Usage

See examples:

from mesh_to_geotiff import MeshObject, MeshToGeotiff
import trimesh
mesh = trimesh.load_mesh("input_mesh.obj", "obj")
# Not providing colours will default to green.. colours are optional
mesh_object = MeshObject(points=mesh.vertices, point_colours=None, facets=mesh.faces)
grid_surface = MeshToGeotiff(verbose=True)

print("Calculating grid")
print("Note: First time running requires numba to compile and cache - may add 15sec overhead once")
grid_surface.compute_grid(mesh_object, grid_spacing=1.0)

print("Creating tif outputs")
# Exporting RGB map is optional, shown for example purposes
saved_as_heights, save_as_rgba = grid_surface.save_geotiff("export_heightmap.tif", "export_rgbmap.tif")
print(f"Saved DEM to: {saved_as_heights}")
print(f"Saved RGB to: {save_as_rgba}")

valid_xyzs = grid_surface.grid_points[grid_surface.null_mask]
print(valid_xyzs)

Benchmarks

Note: Upon the first run, Numba must compile/cache. This can add 15~ seconds to the first-time run that will disappear after that. Benchmarks are after this has happened once.

Test surface:

  • Points: 194,114
  • Facets: 384,874
  • Surface area: 728,550m² (Bounding: approx 1,240m x 970m)

Initial surface

The output DEM looks like this: DEM tif output

Benchmark PC:

  • CPU: AMD Ryzen 5 3600
  • RAM: 64gb
  • GPU: GTX1060
  • OS: Windows 10 x64

Gridding to 1m:

  • Total time to grid: 0.82sec
  • Time to save geotiff: 0.06sec
  • Tif size: 1.6mb
  • Total raster cells: 1,185,532

1m grid

Gridding to 0.5m:

  • Total time to grid: 0.97sec
  • Time to save geotiff: 0.13sec
  • Tif size: 5.76mb
  • Total raster cells: 4,735,248

0.5m grid

Gridding to 0.1m:

  • Total time to grid: 4.93sec
  • Time to save geotiff: 6.3sec
  • Tif size: 75mb
  • Total raster cells: 118,259,020

0.1m grid

Gridding to 0.05m:

  • Total time to grid: 19.99sec
  • Time to save geotiff: 24.5sec
  • Tif size: 223mb
  • Total raster cells: 472,973,166

0.05m grid

You might also like...
Use Convolutional Recurrent Neural Network to recognize the Handwritten line text image without pre segmentation into words or characters. Use CTC loss Function to train.
Use Convolutional Recurrent Neural Network to recognize the Handwritten line text image without pre segmentation into words or characters. Use CTC loss Function to train.

Handwritten Line Text Recognition using Deep Learning with Tensorflow Description Use Convolutional Recurrent Neural Network to recognize the Handwrit

Turn images of tables into CSV data. Detect tables from images and run OCR on the cells.
Turn images of tables into CSV data. Detect tables from images and run OCR on the cells.

Table of Contents Overview Requirements Demo Modules Overview This python package contains modules to help with finding and extracting tabular data fr

Tools for manipulating and evaluating the hOCR format for representing multi-lingual OCR results by embedding them into HTML.

hocr-tools About About the code Installation System-wide with pip System-wide from source virtualenv Available Programs hocr-check -- check the hOCR f

RepMLP: Re-parameterizing Convolutions into Fully-connected Layers for Image Recognition

RepMLP RepMLP: Re-parameterizing Convolutions into Fully-connected Layers for Image Recognition Released the code of RepMLP together with an example o

Detect the mathematical formula from the given picture and the same formula is extracted and converted into the latex code
Detect the mathematical formula from the given picture and the same formula is extracted and converted into the latex code

Mathematical formulae extractor The goal of this project is to create a learning based system that takes an image of a math formula and returns corres

Tools for manipulating and evaluating the hOCR format for representing multi-lingual OCR results by embedding them into HTML.

hocr-tools About About the code Installation System-wide with pip System-wide from source virtualenv Available Programs hocr-check -- check the hOCR f

WACV 2022 Paper - Is An Image Worth Five Sentences? A New Look into Semantics for Image-Text Matching
WACV 2022 Paper - Is An Image Worth Five Sentences? A New Look into Semantics for Image-Text Matching

Is An Image Worth Five Sentences? A New Look into Semantics for Image-Text Matching Code based on our WACV 2022 Accepted Paper: https://arxiv.org/pdf/

Slice a single image into multiple pieces and create a dataset from them
Slice a single image into multiple pieces and create a dataset from them

OpenCV Image to Dataset Converter Slice a single image of Persian digits into mu

This is a passport scanning web service to help you scan, identify and validate your passport created with a simple and flexible design and ready to be integrated right into your system!
This is a passport scanning web service to help you scan, identify and validate your passport created with a simple and flexible design and ready to be integrated right into your system!

Passport-Recogniton-System This is a passport scanning web service to help you scan, identify and validate your passport created with a simple and fle

Owner
null
Simple SDF mesh generation in Python

Generate 3D meshes based on SDFs (signed distance functions) with a dirt simple Python API.

Michael Fogleman 1.1k Jan 8, 2023
Python bindings for JIGSAW: a Delaunay-based unstructured mesh generator.

JIGSAW: An unstructured mesh generator JIGSAW is an unstructured mesh generator and tessellation library; designed to generate high-quality triangulat

Darren Engwirda 26 Dec 13, 2022
Convert Text-to Handwriting Using Python

Convert Text-to Handwriting Using Python Description In this project we'll use python library that's "pywhatkit" for converting text to handwriting. t

null 8 Nov 19, 2022
This tool will help you convert your text to handwriting xD

So your teacher asked you to upload written assignments? Hate writing assigments? This tool will help you convert your text to handwriting xD

Saurabh Daware 4.2k Jan 7, 2023
This is used to convert a string to an Image with Handwritten Characters.

Text-to-Handwriting-using-python This is used to convert a string to an Image with Handwritten Characters. text_to_handwriting(string: str, save_to: s

Akashdeep Mahata 3 Aug 15, 2022
Convert PDF/Image to TXT using EasyOcr - the best OCR engine available!

PDFImage2TXT - DOWNLOAD INSTALLER HERE What can you do with it? Convert scanned PDFs to TXT. Convert scanned Documents to TXT. No coding required!! In

Hans Alemão 2 Feb 22, 2022
This can be use to convert text in a file to handwritten text.

TextToHandwriting This can be used to convert text to handwriting. Clone this project or download the code. Run TextToImage.py give the filename of th

Ashutosh Mahapatra 2 Feb 6, 2022
A Python script to capture images from multiple webcams at once and save them into your local machine

Capturing multiple images at once from Webcam Using OpenCV Capture multiple image by accessing the webcam of your system and save it to your machine.

Fazal ur Rehman 2 Apr 16, 2022
huoyijie 1.2k Dec 29, 2022
An interactive interface for using OpenCV's GrabCut algorithm for image segmentation.

Interactive GrabCut An interactive interface for using OpenCV's GrabCut algorithm for image segmentation. Setup Install dependencies: pip install nump

Jason Y. Zhang 16 Oct 10, 2022