Skip to content

Conversation

@joto
Copy link
Collaborator

@joto joto commented Oct 30, 2025

Ways can have a single node (or multiple nodes at the same location) in which case we can't build a proper linestring. This doesn't happen very often and is always an error in the data. This adds the functionality to flex Lua code to run as_point() on a way geometry which creates a point geometry from the first node.

This is mostly intended for the use case where we want to create a point from invalid ways to help fix them. It can also be used if you just need any point in that linestring for a rough position or so.

@lonvia
Copy link
Collaborator

lonvia commented Oct 31, 2025

The cleaner solution would be to have a num_nodes() and get_node(N) function. Then we wouldn't need to get into a discussion if the first, middle or last point should be returned by this function. What do you think? Too complicated?

@joto
Copy link
Collaborator Author

joto commented Oct 31, 2025

I think that's a different use case and we should solve that differently. We'll need some function to get nodes from ways and members from relations, but not just their geometries, but the whole object. We don't need num_nodes(), because we have nodes and can get the length with #. Similarly for members of a relation. But we need either a get_node(N) as you say of a more general get_node(ID) plus the same for relations. But that's a whole can of worms we open up there, because we need to access the database for that which makes everything slower and all that. We could have a as_point(N) for ways, but this would not extend well for relations, so I am hesitant to do that.

@joto
Copy link
Collaborator Author

joto commented Nov 1, 2025

Force pushed new version: as_point() can now take an index argument when called for a way which specifies which member node to take the location from. Index is 1-based, negative indexes count from the back.

So :as_point(), :as_point(nil), :as_point(0), :as_point(1) will return the first node, :as_point(2) the second node and so on. :as_point(-1) will return the last node.

If the index is out of range a null geometry will be returned. If the parameter is not a number an error will be generated.

This allows the as_point() function to be called for ways. The function
has a single (optional) parameter specifying which member node to create
the point from. The index parameter is 1-based as usual in Lua. Negative
indexes count from the back,

:as_point(), :as_point(nil), :as_point(0), :as_point(1) will return the
first node, :as_point(2) the second node and so on. :as_point(-1) will
return the last node.

If the index is out of range a null geometry will be returned. If the
parameter is not a number an error will be generated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants