-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch.py
executable file
·78 lines (65 loc) · 1.71 KB
/
fetch.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/python3
# Workaround for pdal readers.tindex is not streamable
# and uses lots of memory if there are large source tiles.
from osgeo import ogr
import os
import re
import subprocess
import sys
tindex_ds = ogr.Open(sys.argv[1])
tidx_lyr = tindex_ds.GetLayer('tindex');
out = sys.argv[2]
os.mkdir(out+".tmp")
bounds = re.match("\(\[(\d+),(\d+)\],\[(\d+),(\d+)\]\)", sys.argv[3])
minx = int(bounds[1])
maxx = int(bounds[2])
miny = int(bounds[3])
maxy = int(bounds[4])
ring = ogr.Geometry(ogr.wkbLinearRing)
ring.AddPoint(minx,miny)
ring.AddPoint(maxx,miny)
ring.AddPoint(maxx,maxy)
ring.AddPoint(minx,maxy)
ring.AddPoint(minx,miny)
poly = ogr.Geometry(ogr.wkbPolygon)
poly.AddGeometry(ring)
tidx_lyr.ResetReading()
tidx_lyr.SetSpatialFilter(poly)
idx = 0
tmpfiles = []
for t in tidx_lyr:
idx = idx + 1
laz = t.GetField('location')
laz_out = out+".tmp/"+str(idx)+"-"+os.path.basename(laz)+".las"
bounds = '([{0:6.0f},{1:6.0f}],[{2:7.0f},{3:7.0f}])'.format(
minx,
maxx,
miny,
maxy
)
print("Reading "+laz)
subprocess.run([
"pdal","pipeline",
"fetch.pipeline.json",
"--readers.las.filename="+laz,
"--filters.crop.bounds="+bounds,
"--writers.las.filename="+laz_out,
"--writers.las.offset_x="+str(minx),
"--writers.las.offset_y="+str(miny),
"--writers.las.offset_z=0"
])
tmpfiles.append(laz_out)
print("Merging into "+out)
subprocess.run([
"pdal","merge",
] + tmpfiles + [ out ])
#] + tmpfiles + [ out+".tmp/out.las" ])
#print("Removing duplicate points "+out)
#subprocess.run([
# "pdal","translate",
# out+".tmp/out.las", out,
# "--filter=filters.sample", "--filters.sample.radius=0.001"
#])
print("Removing temp files.")
print("\t"+out+".tmp")
subprocess.run(["rm", "-rf", out+".tmp"])