-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgdal_utils.jl
197 lines (173 loc) · 5.42 KB
/
gdal_utils.jl
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
# This is testing GDAL/OGR utilities as a library, as has been added with
# https://gdal.org/development/rfc/rfc59.1_utilities_as_a_library.html
# In `gdal_jll_utils.jl` we test the utility executables.
ds_small = GDAL.gdalopen("data/utmsmall.tif", GDAL.GA_ReadOnly)
ds_point =
GDAL.gdalopenex("data/point.geojson", GDAL.GDAL_OF_VECTOR, C_NULL, C_NULL, C_NULL)
# GDALInfo
options = GDAL.gdalinfooptionsnew(["-checksum"], C_NULL)
infostr = GDAL.gdalinfo(ds_small, options)
@test occursin("Checksum=50054", infostr)
GDAL.gdalinfooptionsfree(options)
# GDALTranslate
# resample to a 5×5 ascii grid
optvec = ["-of", "AAIGrid", "-r", "cubic", "-tr", "1200", "1200"]
options = GDAL.gdaltranslateoptionsnew(optvec, C_NULL)
ds_tiny_asc = GDAL.gdaltranslate("data/utmtiny.asc", ds_small, options, C_NULL)
GDAL.gdaltranslateoptionsfree(options)
GDAL.gdalclose(ds_tiny_asc)
@test replace(replace(read("data/utmtiny.asc", String), "\r" => ""), " \n" => "\n") == """
ncols 5
nrows 5
xllcorner 440720.000000000000
yllcorner 3745320.000000000000
cellsize 1200.000000000000
128 126 161 185 193
171 164 175 206 205
127 148 177 205 209
93 114 164 172 181
83 101 140 128 122
"""
# also create a Byte GeoTIFF version for later use
optvec = ["-r", "cubic", "-tr", "1200", "1200", "-ot", "Byte"]
options = GDAL.gdaltranslateoptionsnew(optvec, C_NULL)
ds_tiny = GDAL.gdaltranslate("data/utmtiny.tif", ds_small, options, C_NULL)
GDAL.gdaltranslateoptionsfree(options)
tinydata = UInt8[
128 126 161 185 193
171 164 175 206 205
127 148 177 205 209
93 114 164 172 181
83 101 140 128 122
]
band = GDAL.gdalgetrasterband(ds_tiny, 1)
read_data = zero(tinydata)
GDAL.gdalrasterio(band, GDAL.GF_Read, 0, 0, 5, 5, read_data, 5, 5, GDAL.GDT_Byte, 0, 0)
@test read_data' == tinydata
# GDALWarp
options = GDAL.gdalwarpappoptionsnew(["-t_srs", "EPSG:4326", "-of", "MEM"], C_NULL) # in memory
ds_warped = GDAL.gdalwarp(
"data/utmsmall.mem",
Ptr{GDAL.GDALDatasetH}(C_NULL),
1,
[ds_small],
options,
C_NULL,
)
GDAL.gdalwarpappoptionsfree(options)
GDAL.gdalclose(ds_warped)
# GDALDEMProcessing
# calculate hillshade
options = GDAL.gdaldemprocessingoptionsnew(["-of", "AAIGrid"], C_NULL)
ds_dempr = GDAL.gdaldemprocessing(
"data/utmtiny-hillshade.asc",
ds_tiny,
"hillshade",
C_NULL,
options,
C_NULL,
)
GDAL.gdaldemprocessingoptionsfree(options)
GDAL.gdalclose(ds_dempr)
@test replace(
replace(read("data/utmtiny-hillshade.asc", String), "\r" => ""),
" \n" => "\n",
) == """
ncols 5
nrows 5
xllcorner 440720.000000000000
yllcorner 3745320.000000000000
cellsize 1200.000000000000
NODATA_value 0
0 0 0 0 0
0 183 184 183 0
0 180 182 181 0
0 181 181 177 0
0 0 0 0 0
"""
rm("data/utmtiny-hillshade.asc")
rm("data/utmtiny-hillshade.prj")
# GDALNearblack
# not ascii because it doesn't support `create`
options = GDAL.gdalnearblackoptionsnew(["-of", "GTiff", "-near", "100"], C_NULL)
ds_nearblack = GDAL.gdalnearblack(
"data/utmtiny-nearblack.tif",
Ptr{GDAL.GDALDatasetH}(C_NULL),
ds_tiny,
options,
C_NULL,
)
GDAL.gdalnearblackoptionsfree(options)
# do rasterio to read result
band = GDAL.gdalgetrasterband(ds_nearblack, 1)
read_data = zero(tinydata)
GDAL.gdalrasterio(band, GDAL.GF_Read, 0, 0, 5, 5, read_data, 5, 5, GDAL.GDT_Byte, 0, 0)
@test sum(read_data[1:3, 2:5]) == 0
@test sum(read_data) == 2221
GDAL.gdalclose(ds_nearblack)
rm("data/utmtiny-nearblack.tif")
# GDALGrid
options = GDAL.gdalgridoptionsnew(
["-of", "MEM", "-outsize", "3", "10", "-txe", "100", "100.3", "-tye", "0", "0.1"],
C_NULL,
)
ds_grid = GDAL.gdalgrid("data/point-grid.mem", ds_point, options, C_NULL)
GDAL.gdalgridoptionsfree(options)
geotransform = fill(0.0, 6)
GDAL.gdalgetgeotransform(ds_grid, geotransform)
@test geotransform ≈ [100.0, 0.1, 0.0, 0.1, 0.0, -0.01]
GDAL.gdalclose(ds_grid)
# GDALRasterize
options = GDAL.gdalrasterizeoptionsnew(["-of", "MEM", "-tr", "0.05", "0.05"], C_NULL)
ds_rasterize = GDAL.gdalrasterize(
"data/point-rasterize.mem",
Ptr{GDAL.GDALDatasetH}(C_NULL),
ds_point,
options,
C_NULL,
)
GDAL.gdalrasterizeoptionsfree(options)
geotransform = fill(0.0, 6)
GDAL.gdalgetgeotransform(ds_rasterize, geotransform)
@test geotransform ≈ [99.975, 0.05, 0.0, 0.1143, 0.0, -0.05]
GDAL.gdalclose(ds_rasterize)
# GDALBuildVRT
options = GDAL.gdalbuildvrtoptionsnew([], C_NULL)
ds_buildvrt = GDAL.gdalbuildvrt("data/utmtiny.vrt", 1, [ds_tiny], C_NULL, options, C_NULL)
GDAL.gdalbuildvrtoptionsfree(options)
GDAL.gdalclose(ds_buildvrt)
@test occursin(
"<SourceFilename relativeToVRT=\"1\">utmtiny.tif</SourceFilename>",
read("data/utmtiny.vrt", String),
)
rm("data/utmtiny.vrt")
# GDALVectorTranslate
# convert a GeoJSON to CSV with X and Y columns
options =
GDAL.gdalvectortranslateoptionsnew(["-f", "CSV", "-lco", "GEOMETRY=AS_XY"], C_NULL)
ds_csv = GDAL.gdalvectortranslate(
"data/point.csv",
Ptr{GDAL.GDALDatasetH}(C_NULL),
1,
[ds_point],
options,
C_NULL,
)
GDAL.gdalvectortranslateoptionsfree(options)
GDAL.gdalclose(ds_csv)
dstcsv = """
X,Y,FID,pointname
100,0,2,point-a
100.2785,0.0893,3,point-b
100,0,0,a
100.2785,0.0893,3,b
"""
@test replace(read("data/point.csv", String), "\r" => "") == dstcsv
rm("data/point.csv")
GDAL.gdalclose(ds_small)
GDAL.gdalclose(ds_tiny)
GDAL.gdalclose(ds_point)
rm("data/utmtiny.asc.aux.xml")
rm("data/utmtiny.prj")
rm("data/utmtiny.asc")
rm("data/utmtiny.tif")