Skip to content

Commit 5fc53c8

Browse files
authored
Auto-crop referenced images to the minimum set by the polygon boundingbox. (#1591)
* Docstrings * Call the getsize instead of repeating it. * Fix bug when determining if arg2_is_table is true * Auto-crop referenced images to the minimum set by the polygon boundingbox.
1 parent 768bec9 commit 5fc53c8

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

src/grdcut.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ end
9393
"""
9494
crop(arg::GItype; kw...)
9595
96-
Crop a subregion of a grid (GMTgrid) or a image (GMTimage). The subregion is specified with the
97-
``limits`` or ``region`` keyword; the specified range must not exceed the range of the input.
98-
This function differs from ``grdcut`` in the sense that it doesn't call the GMT lib and works only on
99-
in-memory array (i.e., no disk files).
96+
Crop a subregion of a grid (GMTgrid) or a image (GMTimage).
97+
98+
The subregion is specified with the ``limits`` or ``region`` keyword; the specified range must not
99+
exceed the range of the input. This function differs from ``grdcut`` in the sense that it doesn't
100+
call the GMT lib and works only on in-memory array (i.e., no disk files).
100101
101102
### Returns
102103
A grid or an image, depending on the input type, plus two 1x2 matrices with the indices of the cropped zone.

src/grdimage.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ function common_insert_R!(d::Dict, O::Bool, cmd0, I_G; is3D=false)
187187
end
188188
(opt_R != "") && (CTRL.pocket_R[1] = " -R" * opt_R)
189189
end
190-
function isimgsize(GI)
191-
width, height = (GI.layout != "" && GI.layout[2] == 'C') ? (size(GI,2), size(GI,1)) : (length(GI.x), length(GI.y)) .- GI.registration
190+
function isimgsize(GI)::Bool
191+
width, height = getsize(GI)
192192
(GI.range[2] - GI.range[1]) == width && (GI.range[4] - GI.range[3]) == height
193193
end
194194

src/grdtrack.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interpolated values added as (one or more) new columns.
77
When using two numeric inputs and no G option, the order of the x,y and grid is not important.
88
That is, both of this will work: ``D = grdtrack([0 0], Grid);`` or ``D = grdtrack(Grid, [0 0]);``
99
10-
To see the documentation type: ``@? grdtrack``
10+
To see the documentation, type: ``@? grdtrack``
1111
"""
1212
function grdtrack(cmd0::String="", arg1=nothing, arg2=nothing; kwargs...)
1313

@@ -27,7 +27,7 @@ function grdtrack(cmd0::String="", arg1=nothing, arg2=nothing; kwargs...)
2727
# Because we allow arg1 and arg2 to either exist or not and also contain data & grid in any order
2828
if (arg1 !== nothing && arg2 !== nothing)
2929
arg1_is_grid, arg1_is_fv = isa(arg1, GMTgrid), isa(arg1, GMTfv)
30-
arg2_is_table = (isa(arg2, Array) || isa(arg2, GMTdataset) || isa(arg1, GMTfv))
30+
arg2_is_table = (isa(arg2, Array) || isa(arg2, GMTdataset) || isa(arg2, GMTfv))
3131
if (arg2_is_table && (arg1_is_grid || arg1_is_fv)) # Swap the arg1, arg2
3232
arg1, arg2 = arg2, arg1
3333
end
@@ -103,4 +103,4 @@ function parse_G_grdtrk(d::Dict, symbs::Vector{<:Symbol}, cmd::String, arg1, arg
103103
end
104104

105105
# ---------------------------------------------------------------------------------------------------
106-
grdtrack(arg1, arg2=nothing; kw...) = grdtrack("", arg1, arg2; kw...)
106+
grdtrack(arg1, arg2=nothing; kw...) = isa(arg2, String) ? grdtrack(arg2, arg1; kw...) : grdtrack("", arg1, arg2; kw...)

src/solids.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,12 +725,33 @@ viz(FV)
725725
"""
726726
function flatfv(I::Union{GMTimage, AbstractString}; shape=:n, level=0.0)::GMTfv
727727

728+
function crop_if_possible(I::Union{GMTimage, AbstractString}, shape)
729+
# If the image is referenced crop it to the 'shape's bounding box
730+
if isa(I, AbstractString)
731+
D = grdinfo(I, C=1)
732+
isnoref = (D[2] - D[1] == D[9] && D[7] == 1) && (D[4] - D[3] == D[10] && D[8] == 1)
733+
else
734+
isnoref = isimgsize(I) # A function from grdimage.jl
735+
end
736+
isnoref && return I # A plain image with no coords
737+
738+
x = extrema(view(shape, :, 1)) # xx minmax
739+
y = extrema(view(shape, :, 2))
740+
isa(I, AbstractString) && (x[1] < D[1] || x[2] > D[2] || y[1] < D[3] || y[2] > D[4]) &&
741+
error("The 'shape' is outside the image.")
742+
isa(I, GMTimage) && (x[1] < I.range[1] || x[2] > I.range[2] || y[1] < I.range[3] || y[2] > I.range[4]) &&
743+
error("The 'shape' is outside the image.")
744+
745+
return isa(I, AbstractString) ? gmtread(I, R=(x[1], x[2], y[1], y[2]), V=:q) : crop(I, R=(x[1], x[2], y[1], y[2]))
746+
end
747+
728748
function forceRGB(I)::GMTimage{UInt8, 3}
729749
I_ = isa(I, GMTimage) ? I : gmtread(I)::GMTimage
730750
size(I_, 3) == 1 && (I_ = ind2rgb(I_))
731751
return I_
732752
end
733753

754+
I = crop_if_possible(I, shape)
734755
_I = forceRGB(I)
735756
n_cols::Int, n_rows::Int = getsize(_I) # Works for both 'regular' and GDAL transposed images
736757

@@ -784,6 +805,7 @@ function flatfv(I::Union{GMTimage, AbstractString}; shape=:n, level=0.0)::GMTfv
784805
end
785806

786807
FV = surf2fv(X, Y, Z, type=:quad, mask=masca)
808+
copyrefA2B!(_I, FV)
787809
n_colors = doMask ? sum(masca) : (n_rows * n_cols)
788810
cor = Vector{String}(undef, n_colors)
789811

0 commit comments

Comments
 (0)