[Tile-serving] [osm2pgsql-dev/osm2pgsql] Flex: call `:get_bbox()` on geometry, not on parameter table (object) (Issue #2219)
Marek Dorda
notifications at github.com
Mon Aug 12 13:32:58 UTC 2024
This is not a bug report, but rather a feature request, or a request for advice.
According to your [documentation](https://osm2pgsql.org/doc/manual.html#processing-callbacks), method `:get_bbox()` is available for the parameter table (object). Which works great for the most cases. But I find it very limiting when I need to work with multipolygons.
I need to add centroid and bboxes to all (multi)polygons. It seems to be very straightforward:
```lua
function Place:get_geometry_bbox()
if (self.object.type == 'node') then
return nil
end
local xmin, ymin, xmax, ymax = self.object:get_bbox()
if xmin == nil then
return nil
end
return 'BOX(' .. tostring(xmin) .. ' ' .. tostring(ymin) .. ',' .. tostring(xmax) .. ' ' .. tostring(ymax) .. ')'
end
function Place:get_geometry_loc()
return self.geometry:centroid()
end
```
and then just call it when writting:
```lua
function Place:write_row(k, v, save_extra_mains)
-- some logic
place_table:insert{
-- more props
geometry_bbox = self:get_geometry_bbox(),
geometry_loc = self:get_geometry_loc()
}
-- some logic
return 1
end
```
But it gets tricky when you need to work a bit more complex with multipolygons. Specifically: to get a centroid and bbox only from the largest part of the multipolygon.
It is easy for centroid:
```lua
function Place:get_geometry_loc()
if not self.geometry:geometry_type() == 'MULTIPOLYGON' then
return self.geometry:centroid()
end
-- for multipolygon return centroid of the biggest geometry part
local max_geometry = nil
local max_geometry_area = nil
for g in self.geometry:geometries() do
local geometry_area = g:spherical_area()
if max_geometry_area == nil or max_geometry_area < geometry_area then
max_geometry = g
max_geometry_area = geometry_area
end
end
return max_geometry:centroid();
end
```
But it cannot be done for bbox, becasue it is not a geometry method.
I admit that I have a very specific requirement, but purely from the logic of things, `:get_bbox()` seems to me to be a geometry thing and I would expect this method to be directly on geometry. Similar to `:centroid()` or `:area()` ([documentation](https://osm2pgsql.org/doc/manual.html#geometry-objects-in-lua)). It looks pretty inconsistent to me that way.
Did I miss something? Is there any way to achieve this?
Thank you so much!
--
Reply to this email directly or view it on GitHub:
https://github.com/osm2pgsql-dev/osm2pgsql/issues/2219
You are receiving this because you are subscribed to this thread.
Message ID: <osm2pgsql-dev/osm2pgsql/issues/2219 at github.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstreetmap.org/pipermail/tile-serving/attachments/20240812/db084bb7/attachment.htm>
More information about the Tile-serving
mailing list