-
Notifications
You must be signed in to change notification settings - Fork 0
/
geotiff2rhino_example.py
61 lines (47 loc) · 2.02 KB
/
geotiff2rhino_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Example script for loading a GeoTIFF into Rhino as a PointCloud object
#
# Author: Dan Furie
# https://github.com/djfurie/GeoTIFF2Rhino
import GeoTIFF2Rhino
import rhinoscriptsyntax as rs
from System.Drawing import Color
def main():
tif_path = rs.OpenFileName('Choose TIF file', filter='TIF Files|*.tif', extension='.tif')
if not tif_path:
print 'No TIF file path given!'
return
tfw_path = rs.OpenFileName('Choose TFW file', filter='TFW Files|*.tfw', extension='.tfw')
if not tfw_path:
print 'No TFW file path given!'
return
tiff_file = GeoTIFF2Rhino.TIFFFile(tif_path)
tfw_file = GeoTIFF2Rhino.TFWFile(tfw_path)
start_x = rs.GetInteger('Upper Left X Pixel Coordinate', 0, 0, tiff_file.tiff_ImageWidth)
start_y = rs.GetInteger('Upper Left Y Pixel Coordinate', 0, 0, tiff_file.tiff_ImageHeight)
end_x = rs.GetInteger('Lower Right X Pixel Coordinate', tiff_file.tiff_ImageWidth, 0, tiff_file.tiff_ImageWidth)
end_y = rs.GetInteger('Lower Right Y Pixel Coordinate', tiff_file.tiff_ImageHeight, 0, tiff_file.tiff_ImageHeight)
# Figure out the center of our slice of land
center_x = (start_x + end_x) / 2
center_y = (start_y + end_y) / 2
(center_x, center_y) = tfw_file.pixel_to_world(center_x, center_y)
print center_x, center_y
# Sample all of the pixels in the desired region of the TIF
# and generate a point cloud from them
v = []
for y in range(start_y, end_y):
for x in range(start_x, end_x):
z1 = tiff_file.get_pixel_val(x, y)
# Only use valid pixel data
if z1 != 32767:
(x1, y1) = tfw_file.pixel_to_world(x, y)
#Recenter the points about the origin
x1 -= center_x
y1 -= center_y
v.append(x1)
v.append(y1)
v.append(z1)
pc_layer = rs.AddLayer('Imported GeoTIFF PointCloud', Color.SandyBrown)
rs.CurrentLayer(pc_layer)
rs.AddPointCloud(v)
if __name__ == "__main__":
main()