Skip to content

Commit 705dc58

Browse files
authored
Auto-detect grib files with .grb extension. (#1712)
Let binarize accept also the 'threshold' keyword.
1 parent 789d9a6 commit 705dc58

File tree

5 files changed

+16
-12
lines changed

5 files changed

+16
-12
lines changed

src/gdal.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1713,7 +1713,7 @@ end
17131713
nbd = (find_in_kwargs(kw, [:band])[1] !== nothing) ? kw[:band] : 1
17141714
bd = getband(dataset, nbd)
17151715
ds = Gdal.create(Gdal.getdriver("MEMORY"))
1716-
layer = createlayer(name = "poligonized", dataset=ds, geom=Gdal.wkbPolygon)
1716+
layer = createlayer(name="polygonized", dataset=ds, geom=Gdal.wkbPolygon)
17171717
addfielddefn!(layer, "Px", OFTString, nwidth = 32)
17181718
#isfloat ? GDALPolygonize(bd.ptr, mask, layer.ptr, 0, options, progress, C_NULL) :
17191719
#GDALPolygonize( bd.ptr, mask, layer.ptr, 0, options, progress, C_NULL)

src/gdal_tools.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ Its natural use is to digitize masks images.
121121
- `max_area`: Maximum area in m2 for a polygon to be retained.
122122
- `simplify`: Apply the Douglas-Peucker line simplification algorithm to the poligons. Provide a tolerence
123123
in meters. For example: `simplify=0.5`. But be warned that this is a risky option since a too large tolerance
124-
can lead to loss of otherwise good polygons. A good rule of thumb is to use the cell size for the tolerance.
125-
And in fact that is what we do when using `simplify=:auto`.
124+
can lead to loss of otherwise good polygons. A good rule of thumb is to use the cell size for the tolerance.
125+
And in fact that is what we do when using `simplify=:auto`.
126126
- `sort`: If true, will sort polygons by pixel count. Default is the order that GDAL decides internally.
127127
"""
128128
function polygonize(data::GItype; gdataset=nothing, kwargs...)

src/gmtreadwrite.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ function guess_T_from_ext(fname::String; write::Bool=false, text_only::Bool=fals
456456
(!write && (ext == "jp2" || ext == "tif" || ext == "tiff") && (!isfile(fname) && !startswith(fname, "/vsi") &&
457457
!occursin("https:", fname) && !occursin("http:", fname) && !occursin("ftps:", fname) && !occursin("ftp:", fname))) &&
458458
error("File $fname does not exist.")
459-
if (findfirst(isequal(ext), ["grd", "nc", "nc=gd"]) !== nothing) out = " -Tg";
459+
if (findfirst(isequal(ext), ["grd", "nc", "nc=gd", "grb", "grib2"]) !== nothing) out = " -Tg";
460460
elseif (findfirst(isequal(ext), ["dat", "txt", "csv", "isf"]) !== nothing) out = " -Td";
461461
elseif (findfirst(isequal(ext), ["jpg", "jpeg", "png", "bmp", "webp"]) !== nothing) out = " -Ti";
462462
elseif (findfirst(isequal(ext), ["arrow", "arrows", "shp", _kml, "kmz", "json", "feather", "fgb", "geojson", "gmt", "gpkg", "gpx", "gml", "ipc", "parquet", "sqlite"]) !== nothing) out = " -To";

src/img_funs.jl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,20 @@ end
6363

6464
# ---------------------------------------------------------------------------------------------------
6565
"""
66-
Ibw = binarize(I::GMTimage, threshold::Int=0; band=1, revert=false) -> GMTimage
66+
Ibw = binarize(I::GMTimage, threshold::Int=0; band=1, , threshold::Int=0, revert=false) -> GMTimage
6767
6868
Convert an image to a binary image (black-and-white) using a threshold.
6969
7070
### Args
7171
- `I::GMTimage`: input image of type UInt8.
72-
- `threshold::Int`: A number in the range [0 255]. If the default (`nothing`) is maintained,
73-
the threshold is computed using the ``isodata`` method.
72+
- `threshold::Int`: A number in the range [0 255]. If the default (0) is kept and the keyword
73+
`threshold` is not used, then the threshold is computed using the ``isodata`` method.
7474
7575
### Kwargs
7676
- band: If the `I` image has more than one band, use `band` to specify which one to binarize.
77+
By default the first band is used.
78+
- `threshold`: Alternative keyword argument to the positional `threshold` argument. Meaning, one can either
79+
use the `binarize(I, =??)` or `binarize(I, threshold=??)`.
7780
- `revert`: If `true`, values below the threshold are set to 255, and values above the threshold are set to 0.
7881
7982
### Return
@@ -88,13 +91,14 @@ grdimage(I, figsize=6)
8891
grdimage!(Ibw, figsize=6, xshift=6.1, show=true)
8992
```
9093
"""
91-
function binarize(I::GMTimage, threshold::Int=0; band=1, revert=false)::GMTimage
92-
thresh = (threshold == 0) ? isodata(I, band=band) : threshold
94+
function binarize(I::GMTimage, thresh::Int=0; band=1, threshold::Int=0, revert=false)::GMTimage
95+
(thresh == 0 && threshold > 0) && (thresh = threshold)
96+
_tresh = (thresh == 0) ? isodata(I, band=band) : thresh
9397
img = zeros(UInt8, size(I, 1), size(I, 2))
9498
if revert
95-
t = I.layout[3] == 'B' ? (view(I.image, :, :, band) .< thresh) : (slicecube(I, band).image .< thresh)
99+
t = I.layout[3] == 'B' ? (view(I.image, :, :, band) .< _tresh) : (slicecube(I, band).image .< _tresh)
96100
else
97-
t = I.layout[3] == 'B' ? (view(I.image, :, :, band) .> thresh) : (slicecube(I, band).image .> thresh)
101+
t = I.layout[3] == 'B' ? (view(I.image, :, :, band) .> _tresh) : (slicecube(I, band).image .> _tresh)
98102
end
99103
img[t] .= 255
100104
return mat2img(img, I)

src/lepto_funs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ A new `GMTimage` grayscale image `I` with the edge detection, edges are brighter
967967
### Example
968968
969969
Let us vectorize the rice grains in the image "rice.png". The result is not perfect because
970-
the grains in the image's edge are not closed and therefore not filled. And those get poligonized
970+
the grains in the image's edge are not closed and therefore not filled. And those get polygonized
971971
twice (in and outside) making the red lines look thicker (but they are not, they are just doubled).
972972
973973
```julia

0 commit comments

Comments
 (0)