This is a static archive of our old OpenStreetMap Help Site. Please post any new questions and answers at community.osm.org.

comparisons in lua called from osm2pgsql

1

In a lua style file called from osm2pgsql, the following code works exactly as expected - the comparison of the two numbers works as expected:

-- ----------------------------------------------------------------------------
-- Big peaks
-- ----------------------------------------------------------------------------
   if ( keyvalues["natural"] == "peak" ) then
      keyvalues["name"] = "before"

      if ( keyvalues["ele"] == nil ) then
         keyvalues["name"] = "elenil"
      else
         keyvalues["name"] = "elenotnil"
         if ( tonumber( keyvalues["ele"] ) == 900 ) then
            keyvalues["name"] = "bigpeak"
         else
            keyvalues["name"] = "notabigpeak"
         end
      end
   end

However, if I change the comparison to the following:

    if ( tonumber( keyvalues["ele"] ) > 900 ) then

then the following error occurs:

Using lua based tag processing pipeline with script /home/renderaccount/src/SomeoneElse-style/style.lua
Using projection SRS 3857 (Spherical Mercator)
Setting up table: planet_osm_point
Osm2pgsql failed due to ERROR: SELECT * FROM planet_osm_point LIMIT 0 failed: ERROR:  relation "planet_osm_point" does not exist
LINE 1: SELECT * FROM planet_osm_point LIMIT 0

I'd expect this to work, because the "tonumber" seems to work OK and this page suggests to me that ">" ought to work there. osm2pgsql is "osm2pgsql version 0.96.0 (64 bit id space)", lua according to "dpkg -l" seems to be 5.1 or 5.2.

Any ideas?

asked 25 Oct '19, 22:04

SomeoneElse's gravatar image

SomeoneElse ♦
36.9k71370866
accept rate: 16%

edited 25 Oct '19, 22:05


One Answer:

5

tonumber() returns nil when the string parameter is not something that can be converted into a number. You can compare nil against a number with the == operator but not with >:

me@machine:~$ lua -e 'print(tonumber("1m"))' nil me@machine:~$ lua -e 'print(tonumber("1m") == 1)' false me@machine:~$ lua -e 'print(tonumber("1m") > 1)' lua: (command line):1: attempt to compare number with nil

answered 26 Oct '19, 09:04

lonvia's gravatar image

lonvia
6.2k25789
accept rate: 40%

3

Often the easiest way to get round this is by adding an or 0, so (tonumber(keyvalues["ele"]) or 0)>900.

(26 Oct '19, 11:03) Richard ♦

Source code available on GitHub .