-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdal_ExtractHeightsfromLAS_python.py
More file actions
284 lines (243 loc) · 8.96 KB
/
pdal_ExtractHeightsfromLAS_python.py
File metadata and controls
284 lines (243 loc) · 8.96 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
""" Using the preformated JSON pipeline,
loop through a python script to download chunked LAS files based on a fishnet geometry file """
import geopandas as gpd
import pandas as pd
import numpy as np
import shapely.geometry
import pdal, json, requests, urllib.parse, geojson, mercantile, tempfile
from tqdm import tqdm
from urllib.request import urlopen
#############################################################################################
# AOIName
name = "Boston"
print(name)
# Define Output Directory
outputdirectory = r"S:\GCMC\Data\LiDAR\{}LAS".format(name)
## Define Lidar Source
lidarsource = "http://usgs-lidar-public.s3.amazonaws.com/MA_CentralEastern_1_2021/ept.json" # Boston
# ## 3DEP Lidar Footprints
# projecturl = "https://index.nationalmap.gov/arcgis/rest/services/3DEPElevationIndex/MapServer/24/query?"
# params = {"workunit": "MA_CentralEastern_1_2021"}
# projecturl = projecturl + urllib.parse.urlencode(params)
# response = requests.get(url=projecturl)
# data = response.text
# gdf_temp = gpd.read_file(data)
bounds = [
[
[-71.19045, 42.3799],
[-71.19, 42.38069],
[-71.18870, 42.38068],
[-71.18901, 42.37966],
]
]
######--------------- Read in the Building Footprints
#########Define our area of interest, a LIDAR project (AOI)
aoi_geom = {
"coordinates": bounds,
"type": "Polygon",
}
aoi_shape = shapely.geometry.shape(aoi_geom)
minx, miny, maxx, maxy = aoi_shape.bounds
aoi_gdf = gpd.GeoSeries(aoi_shape, crs=4326)
output_fn = "example_building_footprints.geojson"
# ###############Determine which tiles intersect our AOI
quad_keys = set()
for tile in list(mercantile.tiles(minx, miny, maxx, maxy, zooms=9)):
quad_keys.add(int(mercantile.quadkey(tile)))
quad_keys = list(quad_keys)
print(f"The input area spans {len(quad_keys)} tiles: {quad_keys}")
# ########Step 3 - Download the building footprints for each tile that intersects our AOI and crop the results
df = pd.read_csv(
"https://minedbuildings.blob.core.windows.net/global-buildings/dataset-links.csv"
)
idx = 0
combined_rows = []
geodf = gpd.GeoDataFrame(columns=["id", "geometry"], geometry="geometry", crs=4326)
with tempfile.TemporaryDirectory() as tmpdir:
# Download the GeoJSON files for each tile that intersects the input geometry
tmp_fns = []
for quad_key in tqdm(quad_keys):
rows = df[df["QuadKey"] == quad_key]
if rows.shape[0] == 1:
url = rows.iloc[0]["Url"]
df2 = pd.read_json(url, lines=True)
df2["geometry"] = df2["geometry"].apply(shapely.geometry.shape)
gdf = gpd.GeoDataFrame(df2, crs=4326)
elif rows.shape[0] > 1:
raise ValueError(f"Multiple rows found for QuadKey: {quad_key}")
else:
raise ValueError(f"QuadKey not found in dataset: {quad_key}")
geodf = pd.concat([geodf, gdf])
### Clip Footprints to AOI
geodf = geodf.cx[
aoi_gdf.total_bounds[0] : aoi_gdf.total_bounds[2],
aoi_gdf.total_bounds[1] : aoi_gdf.total_bounds[3],
]
# #Build the GeoDataFrame
geodf["properties"].apply(
lambda x: x.update(
{
"minHAG": np.nan,
"minHAG25": np.nan,
"maxHAG": np.nan,
"maxHAG25": np.nan,
"meanHAG": np.nan,
"meanHAG25": np.nan,
"medHAG": np.nan,
"medHAG25": np.nan,
"stdevHAG25": np.nan,
"q1HAG": np.nan,
"q1HAG25": np.nan,
"q3HAG": np.nan,
"q3HAG25": np.nan,
"ground": np.nan,
"heightobs": np.nan,
"heightobs25": np.nan,
}
)
)
# print(geodf)
gdf = geodf.to_crs(3857)
print(gdf)
##################
gdf["innerbuffer"] = gdf.geometry.buffer(-1)
gdf["fpbuffer"] = gdf.geometry.buffer(3)
#############################################################################################
# change the global options that Geopandas inherits from
pd.set_option("display.max_columns", None)
# row = gpd.GeoDataFrame(gdf.iloc[[0]],crs=3857)
# for count, row in gdf.iterfeatures():
def calcHeight(x):
row = gpd.GeoDataFrame(pd.DataFrame(x).transpose(), crs=3857)
row["fpbuffer"] = gpd.GeoSeries(row["fpbuffer"], crs=3857)
row["innerbuffer"] = gpd.GeoSeries(row["innerbuffer"], crs=3857)
footprintWKT = row.geometry.to_wkt()
# print("FootprintWKT",footprintWKT)
# print("FootprintWKT: ",str(footprintWKT))
bufferbounds = (
[row["fpbuffer"].total_bounds[0], row["fpbuffer"].total_bounds[2]],
[row["fpbuffer"].total_bounds[1], row["fpbuffer"].total_bounds[3]],
)
innerbounds = (
[row["innerbuffer"].total_bounds[0], row["innerbuffer"].total_bounds[2]],
[row["innerbuffer"].total_bounds[1], row["innerbuffer"].total_bounds[3]],
)
# print("The buffer bounds: ", bufferbounds)
## Using the standard EPT LAS pipeline format, reprojecting the output to the same EPSG as the AOI
rawLAS = json.dumps(
[
{
"type": "readers.ept",
"filename": lidarsource,
"bounds": str(bufferbounds),
},
# {
# "filename":outputdirectory+"\\"+name+"rawtestlasAOI_{}.las".format("a"+str(1))
# }
]
)
rawpipeline = pdal.Pipeline(rawLAS)
rawpipeline.execute()
rawarray = rawpipeline.arrays
# print(rawarray)
rawarray = np.ma.masked_where(
rawarray[0][["Classification"]]["Classification"] != 2, rawarray[0][["Z"]]["Z"]
).filled(np.nan)
## Run Pipeline on feature
LAStiles = json.dumps(
[
{
"type": "readers.ept",
"filename": lidarsource,
"bounds": str(bufferbounds),
},
{
"type": "filters.hag_nn",
"allow_extrapolation": "true",
},
{
"type": "filters.expression",
"expression": "(!(Classification ==2 || Classification ==3 || Classification ==4 || Classification ==5 || Classification ==7 || Classification ==18) && HeightAboveGround>1)",
},
{"type": "filters.crop", "polygon": footprintWKT.iloc[0]},
# {
# "filename":outputdirectory+"\\"+name+"testlasAOI_{}.las".format("a"+str(count))
# }
]
)
pipeline = pdal.Pipeline(LAStiles)
pipeline.execute()
arrays = pipeline.arrays
############Perform calculations on array and append to feature
if arrays[0][["HeightAboveGround"]]["HeightAboveGround"].size > 0:
top25array = np.ma.masked_where(
arrays[0][["HeightAboveGround"]]["HeightAboveGround"]
< np.percentile(arrays[0][["HeightAboveGround"]]["HeightAboveGround"], 25),
arrays[0][["HeightAboveGround"]]["HeightAboveGround"],
).filled(np.nan)
heightarray = arrays[0][["HeightAboveGround"]]["HeightAboveGround"]
groundarray = rawarray
# Calculate Sample Size
heightobs = np.sum(~np.isnan(heightarray))
heightobs25 = np.sum(~np.isnan(top25array))
# Calculate Min/Max Values
meanground = np.nanmean(groundarray)
minHAG = np.nanmin(heightarray)
min25HAG = np.nanmin(top25array)
maxHAG = np.nanmax(heightarray)
max25HAG = np.nanmax(top25array)
# Calculate Mean Values
meanHAG = np.nanmean(heightarray)
mean25HAG = np.nanmean(top25array)
meanZ = np.nanmean(arrays[0][["Z"]]["Z"])
# Calculate Standard Deviations
stdHAG = np.nanstd(heightarray)
std25HAG = np.nanstd(top25array)
# Calculate Median Values
medHAG = np.nanmedian(heightarray)
med25HAG = np.nanmedian(top25array)
# Calculate Quartile 1
q3HAG, q1HAG = np.nanpercentile(heightarray, [75, 25])
q325HAG, q125HAG = np.nanpercentile(top25array, [75, 25])
else:
heightobs = np.nan
heightobs25 = np.nan
meanground = np.nan
minHAG = np.nan
min25HAG = np.nan
maxHAG = np.nan
max25HAG = np.nan
meanHAG = np.nan
mean25HAG = np.nan
medHAG = np.nan
med25HAG = np.nan
stdHAG = np.nan
std25HAG = np.nan
q1HAG = np.nan
q125HAG = np.nan
q3HAG = np.nan
q325HAG = np.nan
return {
"minHAG": minHAG,
"min25HAG": min25HAG,
"maxHAG": maxHAG,
"max25HAG": max25HAG,
"meanHAG": meanHAG,
"mean25HAG": mean25HAG,
"medHAG": medHAG,
"med25HAG": med25HAG,
"stdHAG": stdHAG,
"std25HAG": std25HAG,
"q1HAG": q1HAG,
"q125HAG": q125HAG,
"q3HAG": q3HAG,
"q325HAG": q325HAG,
"meanground": meanground,
"heightobs": heightobs,
"heightobs25": heightobs25,
}
gdf.apply(lambda x: x["properties"].update(calcHeight(x)), axis=1)
gdf.reset_index(inplace=True)
gdf = gdf.join(pd.json_normalize(gdf.pop("properties")))
gdf = gdf.drop(columns=["innerbuffer", "fpbuffer"])
gdf.to_file(r"S:\GCMC\_Code\temp\building.shp")