Skip to content

Commit ed0c0ef

Browse files
committed
Store -S/--style setting in properties and use for append
1 parent 9c6ed68 commit ed0c0ef

File tree

9 files changed

+262
-25
lines changed

9 files changed

+262
-25
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: 1 addition & 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;

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
@@ -667,11 +667,6 @@ int main(int argc, char *argv[])
667667
return 2;
668668
}
669669

670-
if (style.empty()) {
671-
log_error("Need --style/-S option");
672-
return 2;
673-
}
674-
675670
if (jobs < 1 || jobs > 32) {
676671
log_error("The --jobs/-j parameter must be between 1 and 32.");
677672
return 2;
@@ -710,6 +705,14 @@ int main(int argc, char *argv[])
710705
properties_t properties{conninfo, schema};
711706
properties.load();
712707

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

src/options.hpp

Lines changed: 2 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{};
@@ -186,6 +186,7 @@ struct options_t
186186
bool pass_prompt = false;
187187

188188
bool output_backend_set = false;
189+
bool style_set = false;
189190
}; // struct options_t
190191

191192
#endif // OSM2PGSQL_OPTIONS_HPP

src/osm2pgsql.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ static void store_properties(properties_t *properties, options_t const &options)
115115
properties->set_int("db_format", options.middle_database_format);
116116
properties->set_string("output", options.output_backend);
117117

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+
}
126+
118127
properties->store();
119128
}
120129

@@ -261,6 +270,38 @@ static void check_output(properties_t const &properties, options_t *options)
261270
options->output_backend, output);
262271
}
263272

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+
264305
// This is called in "append" mode to check that the command line options are
265306
// compatible with the properties stored in the database.
266307
static void check_and_update_properties(properties_t *properties,
@@ -272,6 +313,7 @@ static void check_and_update_properties(properties_t *properties,
272313
check_prefix(*properties, options);
273314
check_db_format(*properties, options);
274315
check_output(*properties, options);
316+
check_and_update_style_file(properties, options);
275317
}
276318

277319
// If we are in append mode and the middle nodes table isn't there, it probably
@@ -291,6 +333,20 @@ static void check_for_nodes_table(options_t const &options)
291333
}
292334
}
293335

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+
294350
int main(int argc, char *argv[])
295351
{
296352
try {
@@ -317,6 +373,7 @@ int main(int argc, char *argv[])
317373
if (properties.load()) {
318374
check_and_update_properties(&properties, &options);
319375
} else {
376+
check_and_set_style(&options);
320377
check_for_nodes_table(options);
321378
}
322379

@@ -334,6 +391,7 @@ int main(int argc, char *argv[])
334391
}
335392
}
336393
} else {
394+
check_and_set_style(&options);
337395
store_properties(&properties, options);
338396
auto const finfo = run(options);
339397
store_data_properties(&properties, finfo);

tests/bdd/regression/properties.feature

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,46 @@ Feature: Updates to the test database with properties check
7979
Different output specified on command line
8080
"""
8181

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+

tests/bdd/regression/timestamps.feature

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Feature: Timestamps in properties table should reflect timestamps in input file
99
"""
1010
When running osm2pgsql pgsql
1111

12-
Then table osm2pgsql_properties has 9 rows
12+
Then table osm2pgsql_properties has 10 rows
1313
And table osm2pgsql_properties contains
1414
| property | value |
1515
| attributes | false |
@@ -30,7 +30,7 @@ Feature: Timestamps in properties table should reflect timestamps in input file
3030
"""
3131
When running osm2pgsql pgsql
3232

33-
Then table osm2pgsql_properties has 7 rows
33+
Then table osm2pgsql_properties has 8 rows
3434
Then table osm2pgsql_properties contains
3535
| property | value |
3636
| attributes | false |
@@ -51,7 +51,7 @@ Feature: Timestamps in properties table should reflect timestamps in input file
5151
When running osm2pgsql pgsql with parameters
5252
| --create | --slim |
5353

54-
Then table osm2pgsql_properties has 9 rows
54+
Then table osm2pgsql_properties has 10 rows
5555
And table osm2pgsql_properties contains
5656
| property | value |
5757
| attributes | false |
@@ -72,7 +72,7 @@ Feature: Timestamps in properties table should reflect timestamps in input file
7272
When running osm2pgsql pgsql with parameters
7373
| --append | --slim |
7474

75-
Then table osm2pgsql_properties has 9 rows
75+
Then table osm2pgsql_properties has 10 rows
7676
And table osm2pgsql_properties contains
7777
| property | value |
7878
| attributes | false |
@@ -95,7 +95,7 @@ Feature: Timestamps in properties table should reflect timestamps in input file
9595
When running osm2pgsql pgsql with parameters
9696
| --create | --slim |
9797

98-
Then table osm2pgsql_properties has 9 rows
98+
Then table osm2pgsql_properties has 10 rows
9999
And table osm2pgsql_properties contains
100100
| property | value |
101101
| attributes | false |
@@ -116,7 +116,7 @@ Feature: Timestamps in properties table should reflect timestamps in input file
116116
When running osm2pgsql pgsql with parameters
117117
| --append | --slim |
118118

119-
Then table osm2pgsql_properties has 9 rows
119+
Then table osm2pgsql_properties has 10 rows
120120
And table osm2pgsql_properties contains
121121
| property | value |
122122
| attributes | false |
@@ -139,7 +139,7 @@ Feature: Timestamps in properties table should reflect timestamps in input file
139139
When running osm2pgsql pgsql with parameters
140140
| --create | --slim |
141141

142-
Then table osm2pgsql_properties has 7 rows
142+
Then table osm2pgsql_properties has 8 rows
143143
And table osm2pgsql_properties contains
144144
| property | value |
145145
| attributes | false |
@@ -158,7 +158,7 @@ Feature: Timestamps in properties table should reflect timestamps in input file
158158
When running osm2pgsql pgsql with parameters
159159
| --append | --slim |
160160

161-
Then table osm2pgsql_properties has 8 rows
161+
Then table osm2pgsql_properties has 9 rows
162162
And table osm2pgsql_properties contains
163163
| property | value |
164164
| attributes | false |

tests/bdd/regression/update.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Feature: Updates to the test database
2222
And table planet_osm_line has 3274 rows
2323
And table planet_osm_roads has 380 rows
2424
And table planet_osm_polygon has 4277 rows
25-
And table osm2pgsql_properties has 12 rows
25+
And table osm2pgsql_properties has 13 rows
2626

2727
Examples:
2828
| param1 | param2 | param3 |

0 commit comments

Comments
 (0)