Skip to content

Commit e340a4c

Browse files
authored
Merge pull request #2032 from joto/additional-properties
Additional properties
2 parents 7d84fb7 + ed0c0ef commit e340a4c

File tree

10 files changed

+328
-27
lines changed

10 files changed

+328
-27
lines changed

docs/osm2pgsql-gen.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,19 @@ mandatory for short options too.
3636
specified.
3737

3838
-S, \--style=FILE
39-
: The Lua config file. Same as for **osm2pgsql**.
39+
: The Lua config file. Same as for **osm2pgsql**. Usually not required
40+
because it is read from the `osm2pgsql_properties` table.
4041

4142
-j, \-jobs=NUM
4243
: Specifies the number of parallel threads used for certain operations.
4344
Setting this to the number of available CPU cores is a reasonable starting
4445
point.
4546

47+
\--middle-schema=SCHEMA
48+
: Database schema where the `osm2pgsql_properties` table is to be found.
49+
Default `public`. Set to the same value as on the `osm2pgsql` command
50+
line.
51+
4652
# HELP/VERSION OPTIONS
4753

4854
-h, \--help

src/command-line-parser.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -514,14 +514,6 @@ static void check_options(options_t *options)
514514
options->expire_tiles_zoom = 0;
515515
}
516516

517-
if (options->output_backend == "flex" ||
518-
options->output_backend == "gazetteer") {
519-
if (options->style == DEFAULT_STYLE) {
520-
throw std::runtime_error{
521-
"You have to set the config file with the -S|--style option."};
522-
}
523-
}
524-
525517
if (options->output_backend == "gazetteer") {
526518
log_warn(
527519
"The 'gazetteer' output is deprecated and will soon be removed.");
@@ -623,6 +615,7 @@ options_t parse_command_line(int argc, char *argv[])
623615
break;
624616
case 'S': // --style
625617
options.style = optarg;
618+
options.style_set = true;
626619
break;
627620
case 'i': // --tablespace-index
628621
options.tblsmain_index = optarg;
@@ -652,6 +645,7 @@ options_t parse_command_line(int argc, char *argv[])
652645
break;
653646
case 'O': // --output
654647
options.output_backend = optarg;
648+
options.output_backend_set = true;
655649
break;
656650
case 'x': // --extra-attributes
657651
options.extra_attributes = true;

src/gen/osm2pgsql-gen.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ This program is EXPERIMENTAL and might change without notice.
8888
Main Options:
8989
-a|--append Run in append mode
9090
-c|--create Run in create mode (default)
91-
-S|--style=FILE The Lua config file (required, same as for osm2pgsql)
91+
-S|--style=FILE The Lua config file (same as for osm2pgsql)
9292
-j|--jobs=NUM Number of parallel jobs (default 1)
9393
--middle-schema=SCHEMA Database schema for middle tables
9494
@@ -669,11 +669,6 @@ int main(int argc, char *argv[])
669669
return 2;
670670
}
671671

672-
if (style.empty()) {
673-
log_error("Need --style/-S option");
674-
return 2;
675-
}
676-
677672
if (jobs < 1 || jobs > 32) {
678673
log_error("The --jobs/-j parameter must be between 1 and 32.");
679674
return 2;
@@ -712,6 +707,14 @@ int main(int argc, char *argv[])
712707
properties_t properties{conninfo, schema};
713708
properties.load();
714709

710+
if (style.empty()) {
711+
style = properties.get_string("style", "");
712+
if (style.empty()) {
713+
log_error("Need --style/-S option");
714+
return 2;
715+
}
716+
}
717+
715718
bool const updatable = properties.get_bool("updatable", false);
716719
genproc_t gen{style, conninfo, append, updatable, jobs};
717720
gen.run();

src/options.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ struct options_t
104104
/// Pg schema to store output tables in.
105105
std::string output_dbschema{"public"};
106106

107-
std::string style{DEFAULT_STYLE}; ///< style file to use
107+
std::string style{}; ///< style file to use
108108

109109
/// Name of the flat node file used. Empty if flat node file is not enabled.
110110
std::string flat_node_file{};
@@ -184,6 +184,9 @@ struct options_t
184184
bool parallel_indexing = true;
185185
bool create = false;
186186
bool pass_prompt = false;
187+
188+
bool output_backend_set = false;
189+
bool style_set = false;
187190
}; // struct options_t
188191

189192
#endif // OSM2PGSQL_OPTIONS_HPP

src/osm2pgsql.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ static void store_properties(properties_t *properties, options_t const &options)
113113
properties->set_bool("updatable", options.slim && !options.droptemp);
114114
properties->set_string("version", get_osm2pgsql_short_version());
115115
properties->set_int("db_format", options.middle_database_format);
116+
properties->set_string("output", options.output_backend);
117+
118+
if (options.style.empty()) {
119+
properties->set_string("style", "");
120+
} else {
121+
properties->set_string(
122+
"style",
123+
boost::filesystem::absolute(boost::filesystem::path{options.style})
124+
.string());
125+
}
116126

117127
properties->store();
118128
}
@@ -241,6 +251,57 @@ static void check_db_format(properties_t const &properties, options_t *options)
241251
options->middle_database_format = format;
242252
}
243253

254+
static void check_output(properties_t const &properties, options_t *options)
255+
{
256+
auto const output = properties.get_string("output", "pgsql");
257+
258+
if (!options->output_backend_set) {
259+
options->output_backend = output;
260+
log_info("Using output '{}' (same as on import).", output);
261+
return;
262+
}
263+
264+
if (options->output_backend == output) {
265+
return;
266+
}
267+
268+
throw fmt_error("Different output specified on command line ('{}')"
269+
" then used on import ('{}').",
270+
options->output_backend, output);
271+
}
272+
273+
static void check_and_update_style_file(properties_t *properties,
274+
options_t *options)
275+
{
276+
auto const style_file_from_import = properties->get_string("style", "");
277+
278+
if (options->style.empty()) {
279+
log_info("Using style file '{}' (same as on import).",
280+
style_file_from_import);
281+
options->style = style_file_from_import;
282+
return;
283+
}
284+
285+
if (style_file_from_import.empty()) {
286+
throw std::runtime_error{"Style file from import is empty!?"};
287+
}
288+
289+
const auto absolute_path =
290+
boost::filesystem::absolute(boost::filesystem::path{options->style})
291+
.string();
292+
293+
if (absolute_path == style_file_from_import) {
294+
log_info("Using style file '{}' (same as on import).",
295+
style_file_from_import);
296+
return;
297+
}
298+
299+
log_info("Using the style file you specified on the command line"
300+
" ('{}') instead of the one used on import ('{}').",
301+
absolute_path, style_file_from_import);
302+
properties->set_string("style", absolute_path, true);
303+
}
304+
244305
// This is called in "append" mode to check that the command line options are
245306
// compatible with the properties stored in the database.
246307
static void check_and_update_properties(properties_t *properties,
@@ -251,6 +312,8 @@ static void check_and_update_properties(properties_t *properties,
251312
check_and_update_flat_node_file(properties, options);
252313
check_prefix(*properties, options);
253314
check_db_format(*properties, options);
315+
check_output(*properties, options);
316+
check_and_update_style_file(properties, options);
254317
}
255318

256319
// If we are in append mode and the middle nodes table isn't there, it probably
@@ -270,6 +333,20 @@ static void check_for_nodes_table(options_t const &options)
270333
}
271334
}
272335

336+
static void check_and_set_style(options_t *options)
337+
{
338+
if (!options->style_set) {
339+
if (options->output_backend == "flex" ||
340+
options->output_backend == "gazetteer") {
341+
throw std::runtime_error{"You have to set the config file "
342+
"with the -S|--style option."};
343+
}
344+
if (options->output_backend == "pgsql") {
345+
options->style = DEFAULT_STYLE;
346+
}
347+
}
348+
}
349+
273350
int main(int argc, char *argv[])
274351
{
275352
try {
@@ -296,6 +373,7 @@ int main(int argc, char *argv[])
296373
if (properties.load()) {
297374
check_and_update_properties(&properties, &options);
298375
} else {
376+
check_and_set_style(&options);
299377
check_for_nodes_table(options);
300378
}
301379

@@ -313,6 +391,7 @@ int main(int argc, char *argv[])
313391
}
314392
}
315393
} else {
394+
check_and_set_style(&options);
316395
store_properties(&properties, options);
317396
auto const finfo = run(options);
318397
store_data_properties(&properties, finfo);

tests/bdd/regression/properties.feature

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ Feature: Updates to the test database with properties check
2626
| --slim | --flat-nodes=x | Database was imported without flat node file |
2727

2828

29+
Scenario: Append without output on null output
30+
When running osm2pgsql null with parameters
31+
| -c |
32+
| --slim |
33+
34+
Given the input file '000466354.osc.gz'
35+
When running osm2pgsql nooutput with parameters
36+
| -a |
37+
| --slim |
38+
Then the error output contains
39+
"""
40+
Using output 'null' (same as on import).
41+
"""
42+
43+
2944
Scenario Outline: Create/append with various parameters
3045
When running osm2pgsql pgsql with parameters
3146
| --slim |
@@ -50,3 +65,60 @@ Feature: Updates to the test database with properties check
5065
| --flat-nodes=x | --flat-nodes=y | Using the flat node file you specified |
5166
| --prefix=abc | | Using prefix 'abc' (same as on import). |
5267

68+
69+
Scenario: Create with different output than append
70+
When running osm2pgsql pgsql with parameters
71+
| --slim |
72+
73+
Given the input file '000466354.osc.gz'
74+
Then running osm2pgsql null with parameters fails
75+
| -a |
76+
| --slim |
77+
And the error output contains
78+
"""
79+
Different output specified on command line
80+
"""
81+
82+
Scenario Outline: Create/append with with null output doesn't need style
83+
When running osm2pgsql null with parameters
84+
| --slim |
85+
86+
Given the input file '000466354.osc.gz'
87+
When running osm2pgsql null with parameters
88+
| -a |
89+
| --slim |
90+
| <param> |
91+
Then the error output contains
92+
"""
93+
<message>
94+
"""
95+
96+
Examples:
97+
| param | message |
98+
| | Using style file '' (same as on import). |
99+
| --style= | Using style file '' (same as on import). |
100+
101+
102+
@config.have_lua
103+
Scenario Outline: Create/append with various style parameters with flex output
104+
When running osm2pgsql flex with parameters
105+
| --slim |
106+
| <param_create> |
107+
108+
Given the input file '000466354.osc.gz'
109+
When running osm2pgsql flex with parameters
110+
| -a |
111+
| --slim |
112+
| <param_append> |
113+
Then the error output contains
114+
"""
115+
<message>
116+
"""
117+
118+
Examples:
119+
| param_create | param_append | message |
120+
| --style={TEST_DATA_DIR}/test_output_flex.lua | | Using style file |
121+
| --style={TEST_DATA_DIR}/test_output_flex.lua | --style={TEST_DATA_DIR}/test_output_flex.lua | Using style file |
122+
| --style={TEST_DATA_DIR}/test_output_flex.lua | --style={TEST_DATA_DIR}/test_output_flex_copy.lua | Using the style file you specified |
123+
124+

0 commit comments

Comments
 (0)