Skip to content

Commit

Permalink
laspy upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
rstijerina committed Nov 21, 2024
1 parent 8245cae commit e59aa0e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
6 changes: 3 additions & 3 deletions geoapi/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ def empty_las_file_path_fixture():
with tempfile.TemporaryDirectory() as temp_dir:
empty_las_file_path = os.path.join(temp_dir, "empty.las")

header = laspy.header.Header()
outfile = laspy.file.File(empty_las_file_path, mode="w", header=header)
outfile.close()
header = laspy.header.LasHeader()
outfile = laspy.LasData(header)
outfile.write(empty_las_file_path)
yield empty_las_file_path


Expand Down
48 changes: 32 additions & 16 deletions geoapi/utils/lidar.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ def _transform_to_geojson(proj4, point: tuple) -> tuple:
"""
input_projection = Proj(proj4)
geojson_default_projection = Proj(init="epsg:4326")
x, y, _ = transform(input_projection, geojson_default_projection, point[0], point[1], point[2], errcheck=True)
x, y, _ = transform(
input_projection,
geojson_default_projection,
point[0],
point[1],
point[2],
errcheck=True,
)
return x, y


Expand All @@ -28,15 +35,15 @@ def getProj4(filePath: str):
:return: str
:raises InvalidCoordinateReferenceSystem
"""
result = subprocess.run([
"pdal",
"info",
filePath,
"--metadata"
], capture_output=True, text=True, check=True)
result = subprocess.run(
["pdal", "info", filePath, "--metadata"],
capture_output=True,
text=True,
check=True,
)
info = json.loads(result.stdout)
try:
proj4 = info['metadata']['srs']['proj4']
proj4 = info["metadata"]["srs"]["proj4"]
if proj4:
return proj4
except KeyError:
Expand All @@ -62,13 +69,22 @@ def get_bounding_box_2d(filePaths: List[str]) -> MultiPolygon:
for input_file in filePaths:
proj4 = getProj4(input_file)

las_file = laspy.file.File(input_file, mode="r-")
min_point = _transform_to_geojson(proj4=proj4, point=tuple(las_file.header.min[:3]))
max_point = _transform_to_geojson(proj4=proj4, point=tuple(las_file.header.max[:3]))
las_file.close()
las_file = laspy.read(input_file)
min_point = _transform_to_geojson(
proj4=proj4, point=tuple(las_file.header.mins[:3])
)
max_point = _transform_to_geojson(
proj4=proj4, point=tuple(las_file.header.maxs[:3])
)

polygons.append(Polygon([min_point,
(max_point[0], min_point[1]),
max_point,
(min_point[0], max_point[1])]))
polygons.append(
Polygon(
[
min_point,
(max_point[0], min_point[1]),
max_point,
(min_point[0], max_point[1]),
]
)
)
return polygons[0] if len(polygons) == 1 else unary_union(polygons)

0 comments on commit e59aa0e

Please sign in to comment.