Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expression Editor: Replace Map Function Does Not Match Two-Digit Numeric Key to Value Properly #42940

Closed
Saijin-Naib opened this issue Apr 24, 2021 · 4 comments · Fixed by #42941
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter!

Comments

@Saijin-Naib
Copy link

Describe the bug
In a replace(map()) function, the replacing/mapping does not function properly when the key is numeric and more than one digit.

For instance, if the key were to be 1 and the value Paved - Asphalt, it will work properly, but if the key were to be 11 and the value Curb - Stone, it will simply put Paved - AsphaltPaved - Asphalt.

How to Reproduce

replace("CAMPRS_SHOULDER_ID",map('0','NULL','1','Paved - Asphalt','2','Gravel','3','Earth','4','Vegetation','5','None','6','Curb - Asphalt','7','Curb - Concrete','8','Curb - Granite','9','Paved - Concrete','10','Gutter - Concrete','11','Curb - Stone','12','Gutter - Paved'))

QGIS and OS versions

QGIS version
3.18.2-Zürich
QGIS code revision
515138c
Compiled against Qt
5.15.2
Running against Qt
5.15.2
Compiled against GDAL/OGR
3.2.2
Running against GDAL/OGR
3.2.2
Compiled against GEOS
3.9.1-CAPI-1.14.2
Running against GEOS
3.9.1-CAPI-1.14.2
Compiled against SQLite
3.35.2
Running against SQLite
3.35.2
Compiled against PDAL
2.2.0
Running against PDAL
2.2.0 (git-version: 24dd45)
PostgreSQL Client Version
13.0
SpatiaLite Version
5.0.1
QWT Version
6.1.3
QScintilla2 Version
2.11.5
Compiled against PROJ
8.0.0
Running against PROJ
Rel. 8.0.0, March 1st, 2021
OS Version
Windows 10 Version 2009
Active python plugins
CalculateGeometry;
cartography_tools;
ClipMultipleLayers;
MemoryLayerSaver;
project_colors_dock;
slyr;
toggle_labels_widget;
zoom_level;
db_manager;
MetaSearch;
processing

Additional context
Example screenshot:
image

@Saijin-Naib Saijin-Naib added the Bug Either a bug report, or a bug fix. Let's hope for the latter! label Apr 24, 2021
@roya0045
Copy link
Contributor

that's because 11 is also 1 1. You would need to do this in two steps, or increment your index by 10 to have them all in double digit, and you would still have issue wit longer int.

i'm not sure how much of this is a bug.

@Saijin-Naib
Copy link
Author

It seems really unintuitive, especially if the key is being quoted, as a user would expect that to be a literal match to '11', not 1 1.

@agiudiceandrea
Copy link
Contributor

agiudiceandrea commented Apr 24, 2021

@Saijin-Naib the map function produces a dictionary in which the keys are alphabetically ordered, so the key '1' comes before the key '11' and, thus, the replace function firstly replaces any occurrence of '1' in the string and afterwards it tries to replace any occurrence of '11' which at this point are no longer present in the string.
I think you could use the replace function with two arrays so you can control the order in which the replacements are performed.
Anyway it seems the documentation does not make this behaviour explicit.

@Saijin-Naib
Copy link
Author

Saijin-Naib commented Apr 25, 2021

Not actually a bug, but me mis-understanding how this function works, and possibly a tricky default implementation.

Being closed by this awesome PR: #42941

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants