Skip to content

Instantly share code, notes, and snippets.

@chadcooper
Created April 2, 2013 00:29
Show Gist options
  • Save chadcooper/5288986 to your computer and use it in GitHub Desktop.
Save chadcooper/5288986 to your computer and use it in GitHub Desktop.
Takes an input raster (Esri file geodatabase raster in this case) and translates it into a text file for import into Vue Infinite. See my gist called load-terrain.py, as it's related.
def translate_z(in_val, no_data, max_elev, min_elev):
""" Takes elevation Z values from raster and converts them to their
equivalent value on a scale of 0 to 1, where 0 is the minimum elevation
value of the raster dataset and 1 is the maximum. First add the min_z
absolute value to raster to bring lowest elevation value back up to zero.
"""
if in_val == no_data:
new_val = 0
else:
new_val = ((in_val - min_elev)/(max_elev - min_elev)) * (1 - 0) + 0
return new_val
def raster_to_text(in_raster):
"""Write out a raster to text file for Vue"""
out_text_name = str(in_raster).split("\\")[-1] + ".txt"
out_text_dir = os.path.split(os.path.split(in_raster)[0])[0]
out_text = os.path.join(out_text_dir, out_text_name)
fo = open(out_text,"w")
# Slurp elevation values into a numpy array
pt_array = arcpy.RasterToNumPyArray(in_raster)
p = pt_array.tolist()
# Reverse the list, because for whatever reason, Vue flip-flops the nested
# array values, which will give you a mirror image of your actual terrain
p.reverse()
# Get properties of the raster to use below
rp = get_raster_props(str(in_raster))
new_list = []
for each in p:
new_inner_list = []
for every in each:
new_inner_list.append(translate_z(every, rp["no_data"],
rp["max_elev"],
rp["min_elev"]))
fo.write(str(new_inner_list).strip('[]') + "\n")
fo.close()
return out_text_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment