Skip to content

Commit 02f0ed6

Browse files
committed
Fix area calculation in compatible.lua
This was totally broken with the switch from add_row() to insert(). Also implements the "reproject_area" switch now. Fixes #2017
1 parent 03d9bb8 commit 02f0ed6

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

flex-config/compatible.lua

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ local hstore_match_only = false
4040
-- hstore column.
4141
local hstore_column = nil
4242

43+
-- If this is set, area calculations are always in Mercator coordinates units
44+
-- irrespective of the srid setting.
45+
-- Set this to true if you used --reproject-area before.
46+
local reproject_area = false
47+
4348
-- There is some very old specialized handling of route relations in osm2pgsql,
4449
-- which you probably don't need. This is disabled here, but you can enable
4550
-- it by setting this to true. If you don't understand this, leave it alone.
@@ -332,12 +337,8 @@ local function gen_columns(text_columns, with_hstore, area, geometry_type)
332337

333338
add_column('z_order', 'int')
334339

335-
if area ~= nil then
336-
if area then
337-
add_column('way_area', 'area')
338-
else
339-
add_column('way_area', 'real')
340-
end
340+
if area then
341+
add_column('way_area', 'real')
341342
end
342343

343344
if hstore_column then
@@ -360,13 +361,13 @@ local tables = {}
360361
tables.point = osm2pgsql.define_table{
361362
name = prefix .. '_point',
362363
ids = { type = 'node', id_column = 'osm_id' },
363-
columns = gen_columns(point_columns, hstore or hstore_all, nil, 'point')
364+
columns = gen_columns(point_columns, hstore or hstore_all, false, 'point')
364365
}
365366

366367
tables.line = osm2pgsql.define_table{
367368
name = prefix .. '_line',
368369
ids = { type = 'way', id_column = 'osm_id' },
369-
columns = gen_columns(non_point_columns, hstore or hstore_all, false, 'linestring')
370+
columns = gen_columns(non_point_columns, hstore or hstore_all, true, 'linestring')
370371
}
371372

372373
tables.polygon = osm2pgsql.define_table{
@@ -378,7 +379,7 @@ tables.polygon = osm2pgsql.define_table{
378379
tables.roads = osm2pgsql.define_table{
379380
name = prefix .. '_roads',
380381
ids = { type = 'way', id_column = 'osm_id' },
381-
columns = gen_columns(non_point_columns, hstore or hstore_all, false, 'linestring')
382+
columns = gen_columns(non_point_columns, hstore or hstore_all, true, 'linestring')
382383
}
383384

384385
local z_order_lookup = {
@@ -570,6 +571,20 @@ local function add_line(output, geom, roads)
570571
end
571572
end
572573

574+
local function compute_geom_and_area(geom)
575+
local area, projected_geom
576+
577+
projected_geom = geom:transform(srid)
578+
579+
if reproject_area and srid ~= 3857 then
580+
area = geom:transform(3857):area()
581+
else
582+
area = projected_geom:area()
583+
end
584+
585+
return projected_geom, area
586+
end
587+
573588
function osm2pgsql.process_way(object)
574589
if clean_tags(object.tags) then
575590
return
@@ -628,7 +643,9 @@ function osm2pgsql.process_way(object)
628643
end
629644

630645
if polygon and object.is_closed then
631-
output.way = object:as_polygon()
646+
local pgeom, area = compute_geom_and_area(object:as_polygon())
647+
output.way = pgeom
648+
output.way_area = area
632649
tables.polygon:insert(output)
633650
else
634651
add_line(output, object:as_linestring(), roads)
@@ -730,12 +747,17 @@ function osm2pgsql.process_relation(object)
730747

731748
if make_boundary or make_polygon then
732749
local geom = object:as_multipolygon()
750+
733751
if multi_geometry then
734-
output.way = geom
752+
local pgeom, area = compute_geom_and_area(geom)
753+
output.way = pgeom
754+
output.way_area = area
735755
tables.polygon:insert(output)
736756
else
737757
for sgeom in geom:geometries() do
738-
output.way = sgeom
758+
local pgeom, area = compute_geom_and_area(sgeom)
759+
output.way = pgeom
760+
output.way_area = area
739761
tables.polygon:insert(output)
740762
end
741763
end

0 commit comments

Comments
 (0)