...
 
Commits (2)
__author__ = "Cedrick COPOL"
__version__ = "1.0"
import sys
import numpy as np
import matplotlib.pyplot as plt
def make_image(data, outputname, size=(2400, 1220)):
fig = plt.figure()
# Size is in pixel "dimension"
dpi = fig.get_dpi()
fig.set_size_inches(size[0]/float(dpi), size[1]/float(dpi))
# Remove axis and fill the space
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)
ax.imshow(data, cmap="gray", origin="lower")
plt.savefig(outputname, dpi=dpi)
def asc2fmt(filename, outputname, fmt="png", cellsize=None, widthIRL=400):
"""Convert asc (lidar ASCII format)
Parameters
----------
filename: str
input .asc file
outputname: str
output file without extension
fmt: str
output extension without "."
cellsize: int or float
size of the cell in meter
widthIRL: int
width in mm of the final stl. If you choose to output a png, this script will
print some values to set in Cura to have this width.
"""
_formats = ["png", "vts"]
if not fmt in _formats:
raise ValueError("fmt ({}) must be in {}".format(fmt, _formats))
#xmin, xmaxii, ymin, ymax = limits
with open(filename, "r") as txt:
data = [line.strip().split() for line in txt.readlines()]
dim = data[:6]
# Number of points
npy, npx = [int(d[1]) for d in data[:2]]
# Origin coordinates (Actually these can be ignored in our case)
Ox, Oy = [float(d[1]) for d in data[2:4]]
if cellsize is None:
cellsize = float(data[4][1])
# Topography
data = np.array(data[6:], dtype="float64", order="F")
if fmt == "vts":
#To export data stored in a rectilinear grid (AKA irregular Cartesian grid):
from evtk.hl import gridToVTK
#X = [Ox + i*cellsize for i in range(npx)]
#Y = [Oy + j*cellsize for j in range(npy)]
X = np.linspace(Ox, Ox + npx*cellsize, npx, dtype="float64")
Y = np.linspace(Oy, Oy + npy*cellsize, npy, dtype="float64")
x = np.zeros((npx,npy,1))
y = np.zeros((npx,npy,1))
z = data.flatten().reshape(npx, npy, 1)
for i, Xi in enumerate(X):
x[i,:,0] = Xi
for j, Yj in enumerate(Y):
y[:,j,0] = Yj
print(gridToVTK(outputname, x, y, z, pointData = {"elevation" : z.flatten(order="F")}))
elif fmt == "png":
xsize = (npx-1)*cellsize
ysize = (npy-1)*cellsize
hsize = data.max() - data.min()
print("dim:", xsize, ysize, hsize)
# Image dimension (pixel)
pixel_width = 1200.
pixel_scale = pixel_width/xsize
print("scale 1 pix = {} m".format(pixel_scale))
pixel_depth = pixel_scale * ysize
make_image(data, outputname+".png", size=(pixel_width, pixel_depth))
# dimension cura en mm
scale = widthIRL/xsize
width = scale*xsize
depth = scale*ysize
height = scale*hsize
print("---------------------------")
print("Importer l'image dans Cura.")
print("Pour un modèle de {} mm, mettre les paramètres suivants :".format(widthIRL))
print("height (mm) {:.2f}".format(height))
print("Base (mm) 1.0")
print("Width (mm) {:.2f}".format(width))
print("Depth (mm) {:.2f}".format(depth))
print(" Lighter is higher")
elif fmt == "stl":
try:
import stl_tools
except:
raise ImportError("stl_tools not found")
#stl_tools.numpy2stl
if __name__ == "__main__":
asc2fmt("lidar_souf_5m_ll.asc", "Soufriere", fmt="png", cellsize=5, widthIRL=400)
#asc2fmt("lidar_souf_5m_ll.asc", "Soufriere_limit", fmt="png", cellsize=5, widthIRL=400)
#asc2fmt("lidar_souf_5m_ll.asc", "Soufriere", xmax=2500, fmt="vts", cellsize=5)
import matplotlib.pyplot as plt
import numpy as np
def make_image(data, outputname, size=(2400, 1220)):
fig = plt.figure()
# Set size to 2400x1220 pixels
dpi = fig.get_dpi()
fig.set_size_inches(size[0]/float(dpi), size[1]/float(dpi))
#fig.set_size_inches(size)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)
ax.imshow(data, cmap="gray", origin="lower")
plt.savefig(outputname, dpi=dpi)
with open("lidar_souf_5m_ll.asc", "r") as txt:
data = [line.strip().split() for line in txt.readlines()]
dim = data[:6]
npy, npx = [int(d[1]) for d in data[:2]]
Ox, Oy = [float(d[1]) for d in data[2:4]]
#cellsize = float(data[4][1])
cellsize = 5
data = np.array(data[6:], dtype="float64")
nbvertices = npx*npy
nbrectangles = (npx-1)*(npy-1)
# print(data.max(), data.min(), data.max()-data.min())
#
# X = np.array([Ox + i*cellsize for i in range(npx)])
# Y = np.array([Oy + j*cellsize for j in range(npx)])
#
xsize = (npx-1)*cellsize
ysize = (npy-1)*cellsize
hsize = data.max() - data.min()
pixel_width = 1200.
pixel_scale = pixel_width/xsize
pixel_depth = pixel_scale * ysize
print(pixel_width, pixel_depth)
make_image(data.transpose(), "Soufriere.png", size=(pixel_width, pixel_depth))
cote_mm = 400
scale = cote_mm/xsize
# dimension cura en mm
width = scale*xsize
depth = scale*ysize
height = scale*hsize
print("Importer l'image dans Cura.")
print("Pour un modèle de {} mm, mettre les paramètres suivants :".format(cote_mm))
print("height (mm) {:.2f}".format(height))
print("Base (mm) 1.0")
print("Width (mm) {:.2f}".format(width))
print("Depth (mm) {:.2f}".format(depth))
print(" Lighter is higher")