Skip to content

Commit 34c9cef

Browse files
committed
Check compatibility of geometry and column before trying to COPY
1 parent 3eebee3 commit 34c9cef

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/output-flex.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,32 @@ static void write_json(json_writer_type *writer, lua_State *lua_state,
494494
}
495495
}
496496

497+
static bool is_compatible(geom::geometry_t const &geom,
498+
table_column_type type) noexcept
499+
{
500+
switch (type) {
501+
case table_column_type::geometry:
502+
return true;
503+
case table_column_type::point:
504+
return geom.is_point();
505+
case table_column_type::linestring:
506+
return geom.is_linestring();
507+
case table_column_type::polygon:
508+
return geom.is_polygon();
509+
case table_column_type::multipoint:
510+
return geom.is_point() || geom.is_multipoint();
511+
case table_column_type::multilinestring:
512+
return geom.is_linestring() || geom.is_multilinestring();
513+
case table_column_type::multipolygon:
514+
return geom.is_polygon() || geom.is_multipolygon();
515+
case table_column_type::geometrycollection:
516+
return geom.is_collection();
517+
default:
518+
break;
519+
}
520+
return false;
521+
}
522+
497523
void output_flex_t::write_column(
498524
db_copy_mgr_t<db_deleter_by_type_and_id_t> *copy_mgr,
499525
flex_table_column_t const &column)
@@ -673,6 +699,12 @@ void output_flex_t::write_column(
673699
auto const *const geom = unpack_geometry(lua_state(), -1);
674700
if (geom && !geom->is_null()) {
675701
auto const type = column.type();
702+
if (!is_compatible(*geom, type)) {
703+
throw std::runtime_error{
704+
"Geometry data for geometry column '{}'"
705+
" has the wrong type ({})."_format(
706+
column.name(), geometry_type(*geom))};
707+
}
676708
bool const wrap_multi =
677709
(type == table_column_type::multipoint ||
678710
type == table_column_type::multilinestring ||

tests/bdd/flex/geometry-linestring.feature

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,32 @@ Feature: Creating linestring features from way
3636
| 20 | 1, 2, 3 | 1, 2, 3 | 1, 2, 3 |
3737
| 21 | 4, 5 | 4, 5 | 4, 5 |
3838

39+
Scenario:
40+
Given the grid
41+
| 1 | 2 |
42+
And the OSM data
43+
"""
44+
w20 Thighway=motorway Nn1,n2
45+
"""
46+
And the lua style
47+
"""
48+
local lines = osm2pgsql.define_way_table('osm2pgsql_test_lines', {
49+
{ column = 'geom', type = 'polygon', projection = 4326 },
50+
})
51+
52+
function osm2pgsql.process_way(object)
53+
if object.tags.highway == 'motorway' then
54+
lines:insert({
55+
geom = object:as_linestring(),
56+
})
57+
end
58+
end
59+
60+
"""
61+
Then running osm2pgsql flex fails
62+
63+
And the error output contains
64+
"""
65+
Geometry data for geometry column 'geom' has the wrong type (LINESTRING).
66+
"""
67+

0 commit comments

Comments
 (0)