@@ -147,11 +147,17 @@ int main(int argc, char *argv[])
147
147
pqxx::work txn{ pgc };
148
148
149
149
const std::string base_query
150
- = " select place_id, linked_place_id, parent_place_id, country_code, class, type, "
150
+ = " select place_id, linked_place_id, parent_place_resolved as parent_place_id, country_code, "
151
+ " class, type, "
151
152
" hstore_to_json(name) as name, hstore_to_json(extratags) as extra, "
152
153
" COALESCE(address->'housenumber',housenumber) AS housenumber, postcode, ST_X(centroid) as "
153
154
" longitude, ST_Y(centroid) as latitude, osm_id "
154
- " from placex " ;
155
+ " from placex pl left join lateral "
156
+ " (with recursive prec as (select place_id, linked_place_id "
157
+ " from placex where pl.parent_place_id=placex.place_id union select p.place_id, "
158
+ " p.linked_place_id from placex p join prec on p.place_id=prec.linked_place_id) select "
159
+ " place_id as parent_place_resolved from prec where linked_place_id is null limit 1) as "
160
+ " pres on true " ;
155
161
156
162
// load primary hierarchy
157
163
{
@@ -180,17 +186,22 @@ int main(int argc, char *argv[])
180
186
+ " where linked_place_id IS NOT NULL and ST_Intersects(ST_GeomFromGeoJSON($1), "
181
187
" geometry) order by admin_level" ,
182
188
border);
183
- size_t count = 0 ;
189
+ size_t count = 0 ;
190
+ size_t failed = 0 ;
184
191
for (const pqxx::row &row : r)
185
192
{
186
193
++count;
187
194
std::shared_ptr<HierarchyItem> item = std::make_shared<HierarchyItem>(row);
188
- hierarchy.add_linked_item (item);
195
+ if (!hierarchy.add_linked_item (item))
196
+ failed++;
189
197
if (count % printout_step == 0 )
190
198
std::cout << " Imported linked records: " << count
191
199
<< " ; Root elements: " << hierarchy.get_root_count ()
192
200
<< " ; Missing parents: " << hierarchy.get_missing_count () << std::endl;
193
201
}
202
+ std::cout << " Imported linked records: " << count << " / failed to import: " << failed
203
+ << " ; Root elements: " << hierarchy.get_root_count ()
204
+ << " ; Missing parents: " << hierarchy.get_missing_count () << std::endl;
194
205
}
195
206
196
207
// find missing parents for root nodes
@@ -210,6 +221,8 @@ int main(int argc, char *argv[])
210
221
{
211
222
std::cerr << " Missing parent with ID " << parent << " . Stopping import\n " ;
212
223
hierarchy.print_root_with_parent_id (parent);
224
+ std::cerr << " \n SQL:\n " << base_query + " where place_id=" << parent << " \n " ;
225
+
213
226
return -1 ;
214
227
}
215
228
0 commit comments