Skip to content

Commit cef5546

Browse files
committed
✨ Add ASC and TIFF file handling
1 parent 978cfc3 commit cef5546

5 files changed

Lines changed: 134 additions & 8 deletions

File tree

src/FastPointQuery.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ function __init__()
3636
end
3737
end
3838

39-
include(joinpath(@__DIR__, "fileio/asc2ply.jl"))
39+
include(joinpath(@__DIR__, "fileio/asc.jl" ))
4040
include(joinpath(@__DIR__, "fileio/geojson.jl"))
41-
include(joinpath(@__DIR__, "fileio/ply.jl"))
42-
include(joinpath(@__DIR__, "fileio/stl.jl"))
43-
include(joinpath(@__DIR__, "fileio/tiff2ply.jl"))
41+
include(joinpath(@__DIR__, "fileio/ply.jl" ))
42+
include(joinpath(@__DIR__, "fileio/stl.jl" ))
43+
include(joinpath(@__DIR__, "fileio/tiff.jl" ))
4444

45-
include(joinpath(@__DIR__, "utils.jl"))
46-
include(joinpath(@__DIR__, "polygon.jl"))
45+
include(joinpath(@__DIR__, "utils.jl" ))
46+
include(joinpath(@__DIR__, "polygon.jl" ))
4747
include(joinpath(@__DIR__, "polyhedron.jl"))
4848

4949
export get_resource, res_dir

src/fileio/asc.jl

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#==========================================================================================+
2+
| TABLE OF CONTENTS: |
3+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4+
| - readasc |
5+
+==========================================================================================#
6+
7+
export readasc
8+
9+
"""
10+
readasc(filename::String)
11+
12+
Description:
13+
---
14+
Reads an ASCII grid file (.asc) and returns the pixel coordinates and values as a 3D array
15+
with shape (3, N), where N is the number of pixels.
16+
17+
Example:
18+
---
19+
```julia
20+
xyz = readasc("example.asc")
21+
```
22+
"""
23+
function readasc(filename::String)
24+
isfile(filename) || throw(ArgumentError("file not found: $filename"))
25+
endswith(filename, ".asc") || error("filename must be a .asc file")
26+
27+
@pyexec """
28+
def py_tmp(asc_path, rasterio, np):
29+
with rasterio.open(asc_path) as src:
30+
band = src.read(1)
31+
transform = src.transform
32+
nodata = src.nodata
33+
H, W = band.shape
34+
35+
# 获取像素索引网格
36+
rows, cols = np.indices((H, W))
37+
rows = rows.ravel()
38+
cols = cols.ravel()
39+
40+
# 提取高程并处理 nodata 为 NaN
41+
zs = band.ravel().astype(np.float32)
42+
if nodata is not None:
43+
zs[zs == nodata] = np.nan
44+
45+
# 转换像素坐标 → 实际地理坐标 (x, y),单位与原始数据一致(可能是米或经纬度)
46+
xs, ys = rasterio.transform.xy(transform, rows, cols)
47+
xs = np.array(xs)
48+
ys = np.array(ys)
49+
50+
# 拼成 (N, 3) 的数组
51+
xyz = np.column_stack([xs, ys, zs])
52+
return xyz.T
53+
""" => py_tmp
54+
55+
return py2ju(Array, py_tmp(filename, rasterio, np))
56+
end

src/fileio/asc2ply.jl

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/fileio/tiff.jl

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#==========================================================================================+
2+
| TABLE OF CONTENTS: |
3+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4+
| - readtiff |
5+
+==========================================================================================#
6+
7+
export readtiff
8+
9+
"""
10+
readtiff(filename::String, dst_crs::String="EPSG:3857")
11+
12+
Description:
13+
---
14+
Reads a GeoTIFF file and returns the pixel coordinates and values as a 3D array
15+
with shape (3, N), where N is the number of pixels. `dst_crs` specifies the destination
16+
coordinate reference system (CRS) for the output coordinates. The default CRS is "EPSG:3857"
17+
(Web Mercator).
18+
19+
Example:
20+
---
21+
```julia
22+
xyz = readtiff("example.tiff")
23+
#or
24+
xyz = readtiff("example.tif")
25+
```
26+
"""
27+
function readtiff(filename::String; dst_crs::String="EPSG:3857")
28+
isfile(filename) || throw(ArgumentError("file not found: $filename"))
29+
if endswith(filename, ".tif")
30+
filename *= "f" # ensure .tiff extension
31+
end
32+
endswith(filename, ".tiff") || error("filename must be a .tif file")
33+
34+
@pyexec """
35+
def py_tmp(tif_path, dst_crs, rasterio, np):
36+
with rasterio.open(tif_path) as src:
37+
transform, width, height = rasterio.warp.calculate_default_transform(
38+
src.crs, dst_crs, src.width, src.height, *src.bounds)
39+
kwargs = src.meta.copy()
40+
kwargs.update({
41+
'crs': dst_crs,
42+
'transform': transform,
43+
'width': width,
44+
'height': height
45+
})
46+
data = np.empty((height, width), dtype=src.dtypes[0])
47+
rasterio.warp.reproject(
48+
source=rasterio.band(src, 1),
49+
destination=data,
50+
src_transform=src.transform,
51+
src_crs=src.crs,
52+
dst_transform=transform,
53+
dst_crs=dst_crs,
54+
resampling=rasterio.warp.Resampling.bilinear
55+
)
56+
nodata = kwargs.get('nodata', None)
57+
H, W = data.shape
58+
rows, cols = np.indices((H, W))
59+
rows = rows.ravel()
60+
cols = cols.ravel()
61+
zs = data.ravel().astype(np.float32)
62+
if nodata is not None:
63+
zs[zs == nodata] = np.nan
64+
xs, ys = rasterio.transform.xy(transform, rows, cols)
65+
xs = np.array(xs)
66+
ys = np.array(ys)
67+
xyz = np.column_stack([xs, ys, zs])
68+
return xyz.T
69+
""" => py_tmp
70+
71+
return py2ju(Array, py_tmp(filename, dst_crs, rasterio, np))
72+
end

src/fileio/tiff2ply.jl

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)