NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

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


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

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 ♦

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "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:

×263
×5
×1

question asked: 25 Oct '19, 22:04

question was seen: 2,078 times

last updated: 26 Oct '19, 11:03

NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum