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"
         keyvalues["name"] = "elenotnil"
         if ( tonumber( keyvalues["ele"] ) == 900 ) then
            keyvalues["name"] = "bigpeak"
            keyvalues["name"] = "notabigpeak"

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 ♦
accept rate: 15%

edited 25 Oct '19, 22:05

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

permanent link

answered 26 Oct '19, 09:04

lonvia's gravatar image

accept rate: 41%


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 ♦
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 25 Oct '19, 22:04

question was seen: 502 times

last updated: 26 Oct '19, 11:03

powered by OSQA