GenericMappingTools / gmt

The Generic Mapping Tools
https://www.generic-mapping-tools.org
Other
843 stars 352 forks source link

gmtconvert -S search string option does not search for exact match #6385

Closed evangowan closed 2 years ago

evangowan commented 2 years ago

Description of the problem Please forgive me if there is already a way around this issue and that I have misunderstood the documentation.

I have a GMT/OGR formatted text file that contains polygons of specific regions for a series of maps I am creating. Each region has a location name, which is in the header of each segment. I have been using the gmtconvert command to extract the specific segment for each region, but I have run into problems when part of the name of one location is in another location. The gmt convert -Svarname=value option searches for any instance of "value" in "varname", rather than searching for the exact value of "value".

Below is a script that replicates the problem I am having.

Full script that generated the error

#! /bin/bash

cat << END_CAT > temp.gmt
# @VGMT1.0 @GPOLYGON
# @R-152.77272029/169.128951621/-23.2377812569/81.9940671284              
# @Je4326
# @Jp"+proj=longlat +datum=WGS84 +no_defs"
# @Jw"GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],AUTHORITY[\"EPSG\",\"4326\"]]"
# @Nid|location
# @Tdouble|string
# FEATURE_DATA
>
# @D|West_Gulf_Of_Riga
# @P
22.1120169091186 57.7710424992848
23.2040767172597 57.9283770234075
23.4930241573252 57.629100183232
23.4606231249008 57.3312000795529
23.1172608994629 56.8525065167987
22.0778143510082 57.4635298952077
22.1120169091186 57.7710424992848
>
# @D|Riga
# @P
23.4606231249008 57.3312000777763
24.8006762845239 57.4253968551489
24.670067386146 57.0026169511991
24.082984467369 56.7662371812125
23.3597174711204 56.7601754061888
23.1172608994629 56.8525065149645
23.4606231249008 57.3312000777763
END_CAT

echo ------------------------------------------
echo  ------ outputs West_Gulf_Of_Riga polygon ------ 
echo ------------------------------------------
location=West_Gulf_Of_Riga
echo ${location}

gmt convert temp.gmt -Slocation=${location}

echo ------------------------------------------
echo ------ outputs both polygons, but I only want the Riga polygon ------ 
echo ------------------------------------------
location=Riga
echo ${location}

gmt convert temp.gmt -Slocation=${location}

System information

PaulWessel commented 2 years ago

The -S only allowed for a pattern match, i.e., any substring that matches the given pattern would be found, as you learned. To allow for the more specific exact match I have added the modifier +e which you would now use to only get Riga, for example. Hope you are able to build and test the branch in #6391.

PaulWessel commented 2 years ago

Now merged into master so no need to select a branch for testing.

evangowan commented 2 years ago

Dear Paul,

Thank you for looking into the issue. I tested the script with the code from the master branch. When I added the +e option, for instance:

gmt convert temp.gmt -Slocation=Riga+e

There is no output.

However, using the +e option on the "West_Gulf_Of_Riga" command still outputs the polygon for "West_Gulf_Of_Riga".

In fact, I actually ran into other problems when running the script, where it is outputting all of the polygons even though there was no text match. below is a modified script that demonstrates the problem.

#! /bin/bash

cat << END_CAT > temp.gmt
# @VGMT1.0 @GPOLYGON
# @R-152.77272029/169.128951621/-23.2377812569/81.9940671284              
# @Je4326
# @Jp"+proj=longlat +datum=WGS84 +no_defs"
# @Jw"GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],AUTHORITY[\"EPSG\",\"4326\"]]"
# @Nid|location
# @Tdouble|string
# FEATURE_DATA
>
# @D|West_Gulf_Of_Riga
# @P
22.1120169091186 57.7710424992848
23.2040767172597 57.9283770234075
23.4930241573252 57.629100183232
23.4606231249008 57.3312000795529
23.1172608994629 56.8525065167987
22.0778143510082 57.4635298952077
22.1120169091186 57.7710424992848
>
# @D|Riga
# @P
23.4606231249008 57.3312000777763
24.8006762845239 57.4253968551489
24.670067386146 57.0026169511991
24.082984467369 56.7662371812125
23.3597174711204 56.7601754061888
23.1172608994629 56.8525065149645
23.4606231249008 57.3312000777763
>
# @D|west
# @P
23.4606231249008 57.3312000777763
24.8006762845239 57.4253968551489
24.670067386146 57.0026169511991
24.082984467369 56.7662371812125
23.3597174711204 56.7601754061888
23.1172608994629 56.8525065149645
23.4606231249008 57.3312000777763
END_CAT

echo ------------------------------------------
echo  ------ outputs West_Gulf_Of_Riga polygon ------ 
echo ------------------------------------------
location=West_Gulf_Of_Riga
echo ${location}

gmt convert temp.gmt -Slocation=${location}+e

echo ------------------------------------------
echo ------ should output Riga polygon, but it outputs nothing ------ 
echo ------------------------------------------
location=Riga
echo ${location}

gmt convert temp.gmt -Slocation=${location}+e

echo ------------------------------------------
echo ------ should output the west polygon, but it outputs everything ------ 
echo ------ it works fine in GMT 6.3.0                                ------ 
echo ------------------------------------------
location=west
echo ${location}
gmt convert temp.gmt -Slocation=${location}

echo ------------------------------------------
echo ------ should output nothing, but it outputs everything ------ 
echo ------ it works fine in GMT 6.3.0                                ------ 
echo ------------------------------------------
location=output
echo ${location}
gmt convert temp.gmt -Slocation=${location}

echo ------------------------------------------
echo ------ this string, for some reason, does not output any polygon ------ 
echo ------------------------------------------

location=should_not_output
echo ${location}
gmt convert temp.gmt -Slocation=${location}
evangowan commented 2 years ago

Dear Paul,

I tried the text script again, and it seems that everything is working now. Thank you for fixing this!

Cheers, Evan