Skip to content

Commit d664442

Browse files
authored
Let polygonlevels also join with approximate matches (startswith, endswith, contains) (#1792)
* Keep only two chars of the states codes. * Fix bug/typo in var name * Let polygonlevels also join with approximate matches (startswith, endswith, contains)
1 parent cc89f6a commit d664442

3 files changed

Lines changed: 22 additions & 22 deletions

File tree

src/gmt_main.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ function get_dataset(API::Ptr{Nothing}, object::Ptr{Nothing})::GDtype
690690
Darr[seg_out].header = hdrstr
691691
else
692692
(DCWnames && (ind = findfirst(" Segment", hdrstr)) !== nothing) &&
693-
(Darr[seg_out].attrib["CODE"] = hdrstr[4:4+codelen-1]; Darr[seg_out].attrib["NAME"] = hdrstr[7+codelen-2:ind[1]-1])
693+
(Darr[seg_out].attrib["CODE"] = hdrstr[4+codelen-2:4+codelen-1]; Darr[seg_out].attrib["NAME"] = hdrstr[7+codelen-2:ind[1]-1])
694694
end
695695
end
696696
if (seg == 1)

src/psxy.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,7 @@ function make_color_column(d::Dict, cmd::String, opt_i::String, len_cmd::Int, N_
12711271
warn1 = string("Probably color column in '", the_kw, "' has incorrect dims (", length(mz), " vs $n_rows). Ignoring it.")
12721272
warn2 = "Plotting with color table requires adding one more column to the dataset but your 'incols'
12731273
option didn't do it, so you won't get what you expect. Try incols=\"0-1,1\" for 2D or \"=0-2,2\" for 3D plots"
1274-
(!no_m) ? @warn(warn1) : @warn(warn2)
1274+
(!no_mz) ? @warn(warn1) : @warn(warn2)
12751275
return cmd, arg1, arg2, N_args, true
12761276
end
12771277

src/spatial_funs.jl

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ or
55
66
zvals = polygonlevels(D::GDtype, idvals::GMTdataset; kw...) -> Vector{Float64}
77
8-
Create a vector with `zvals` to use in `plot` and where length(zvals) == length(D)
8+
Create a vector with `zvals` to use in `plot` when creating choropleth maps and where length(zvals) == length(D)
99
1010
The elements of `zvals` are made up from the `vals`.
1111
@@ -24,6 +24,9 @@ The elements of `zvals` are made up from the `vals`.
2424
- `attrib` or `att`: Select which attribute to use when matching with contents of the `ids` strings.
2525
- `nocase` or `insensitive`: Perform a case insensitive comparision between the contents of
2626
`ids` and the attribute specified with `attrib`. Default compares as case sensistive.
27+
- `starts,ends,contains`: Sometimes the attribute value is only part of the string in `ids`.
28+
Use one of these options to specify how to match. E.g. `ends=true` selects all attributes
29+
that ends with a match in `ids`. Default is the exact match.
2730
- `repeat`: Replicate the previously known value until it finds a new segment ID for the case
2831
when a polygon have no attributes (may happen for the islands in a country).
2932
@@ -48,28 +51,25 @@ end
4851
function polygonlevels(D::Vector{<:GMTdataset}, user_ids::Vector{<:AbstractString}, vals::Vector{<:Real}; kw...)::Vector{Float64}
4952
@assert((n_user_ids = length(user_ids)) == length(vals))
5053
((att = find_in_kwargs(kw, [:att :attrib])[1]) === nothing) && error("Must provide the `attribute` NAME.")
51-
nocase = (find_in_kwargs(kw, [:nocase :insensitive])[1] === nothing) ? true : false
52-
repeat = (find_in_kwargs(kw, [:repeat])[1] === nothing) ? false : true
54+
nocase = !is_in_kwargs(kw, [:nocase :insensitive]) # i.e. case insensitive
55+
repeat = is_in_kwargs(kw, [:repeat])
56+
ends = is_in_kwargs(kw, [:ends])
57+
starts = is_in_kwargs(kw, [:starts])
58+
contains_ = is_in_kwargs(kw, [:contains])
59+
fun = starts ? startswith : ends ? endswith : contains_ ? contains : Base.:(==)
60+
exact = !(starts || ends || contains_)
5361

5462
n_seg = length(D)
5563
zvals = fill(NaN, n_seg)
56-
if (nocase)
57-
for m = 1:n_seg
58-
isempty(D[m].attrib) && (repeat && (zvals[m] = zvals[m-1]); continue)
59-
for k = 1:n_user_ids
60-
if (D[m].attrib[att] == user_ids[k])
61-
zvals[m] = vals[k]; break
62-
end
63-
end
64-
end
65-
else
66-
for m = 1:n_seg
67-
isempty(D[m].attrib) && (repeat && (zvals[m] = zvals[m-1]); continue)
68-
t = lowercase(D[m].attrib[att])
69-
for k = 1:n_user_ids
70-
if (t == lowercase(user_ids[k]))
71-
zvals[m] = vals[k]; break
72-
end
64+
65+
for m = 1:n_seg
66+
isempty(D[m].attrib) && (repeat && (zvals[m] = zvals[m-1]); continue)
67+
t = (nocase) ? D[m].attrib[att] : lowercase(D[m].attrib[att])
68+
for k = 1:n_user_ids
69+
to_comp = (nocase) ? user_ids[k] : lowercase(user_ids[k])
70+
condition = (exact) ? (t == to_comp) : fun(t, to_comp)
71+
if (condition)
72+
zvals[m] = vals[k]; break
7373
end
7474
end
7575
end

0 commit comments

Comments
 (0)