Skip to content

Instantly share code, notes, and snippets.

@zeffii
Last active August 29, 2016 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/5dc7dd44899b7ae36dab73a2c235cd4f to your computer and use it in GitHub Desktop.
Save zeffii/5dc7dd44899b7ae36dab73a2c235cd4f to your computer and use it in GitHub Desktop.
blender modified asc reader importer handles garbage values (-9999 -> 100 )
import bpy
import time
import csv
A = time.time()
# dfile = r"C:\Users\dealga\Desktop\Archive\SU8606_DSM_1M.asc"
dfile = r"C:\Users\zeffi\Downloads\rubber\LIDAR-DTM-2M-NZ24\nz2146_DTM_2m.asc"
getval = lambda i: int(next(i).split()[1])
with open(dfile) as ofile:
ncols = getval(ofile)
nrows = getval(ofile)
xllcorner = getval(ofile)
yllcorner = getval(ofile)
cellsize = getval(ofile)
NODATA_value = getval(ofile)
print(ncols, nrows, xllcorner, yllcorner, cellsize, NODATA_value)
# this will read the rest
verts = []
add_vert = verts.append
# asc_reader = csv.reader(ofile, delimiter=' ')
ni = nrows #? +1 -1
nj = ncols #? +1 -1
# ni = 500
# nj = 500
for i, row in enumerate(ofile):
row_data = row.strip().split(' ')
row_data = [float(d) for d in row_data]
if i >= ni:
break
for j in range(int(ncols)):
if j >= nj:
break
# uk survey has a lot of garbage data in some .asc files
datum = 100 if (row_data[j] == -9999) else row_data[j]
z = float(datum) / 60
x = j * 0.01 # cell x width
y = i * 0.01 # cell y width
add_vert((x,y,z))
print('done')
print('last vertex:', verts[-1])
B = time.time()
total_time = B-A
print('total_time:', total_time)
faces = []
add_face = faces.append
# generate_edges, i = verts y, j = verts x
total_range = ((ni-1) * (nj))
indices = []
for i in range(total_range):
if not ((i+1) % nj == 0):
add_face([i, i+nj, i+nj+1, i+1])
# do your own error handling
mesh_data = bpy.data.meshes.new("LIDAR_mesh_data3")
mesh_data.from_pydata(verts, [], faces)
mesh_data.update()
LIDAR_object = bpy.data.objects.new("LIDAR_Object3", mesh_data)
scene = bpy.context.scene
scene.objects.link(LIDAR_object)
LIDAR_object.select = True
import bpy
import time
import csv
A = time.time()
# dfile = r"C:\Users\dealga\Desktop\Archive\SU8606_DSM_1M.asc"
dfile = r"C:\Users\zeffi\Downloads\rubber\LIDAR-DTM-2M-NZ24\nz2141_DTM_2m.asc"
getval = lambda i: int(next(i).split()[1])
with open(dfile) as ofile:
ncols = getval(ofile)
nrows = getval(ofile)
xllcorner = getval(ofile)
yllcorner = getval(ofile)
cellsize = getval(ofile)
NODATA_value = getval(ofile)
print(ncols, nrows, xllcorner, yllcorner, cellsize, NODATA_value)
# this will read the rest
verts = []
add_vert = verts.append
asc_reader = csv.reader(ofile, skipinitialspace=True, delimiter=' ')
ni = nrows #? +1 -1
nj = ncols #? +1 -1
for i, row in enumerate(asc_reader):
if i >= ni:
break
for j in range(int(ncols)):
if j >= nj:
break
datum = 100 if (row[j] == str(NODATA_value)) else row[j]
z = float(datum) / 60
x = j * 0.01 # cell x width
y = i * 0.01 # cell y width
add_vert((x,y,z))
print('done')
print('last vertex:', verts[-1])
B = time.time()
total_time = B-A
print('total_time:', total_time)
faces = []
add_face = faces.append
# generate_edges, i = verts y, j = verts x
total_range = ((ni-1) * (nj))
indices = []
for i in range(total_range):
if not ((i+1) % nj == 0):
add_face([i, i+nj, i+nj+1, i+1])
# do your own error handling
mesh_data = bpy.data.meshes.new("LIDAR_mesh_data3")
mesh_data.from_pydata(verts, [], faces)
mesh_data.update()
LIDAR_object = bpy.data.objects.new("LIDAR_Object3", mesh_data)
scene = bpy.context.scene
scene.objects.link(LIDAR_object)
LIDAR_object.select = True
@zeffii
Copy link
Author

zeffii commented Aug 29, 2016

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment