@@ -147,11 +147,17 @@ int main(int argc, char *argv[])
147147 pqxx::work txn{ pgc };
148148
149149 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, "
151152 " hstore_to_json(name) as name, hstore_to_json(extratags) as extra, "
152153 " COALESCE(address->'housenumber',housenumber) AS housenumber, postcode, ST_X(centroid) as "
153154 " 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 " ;
155161
156162 // load primary hierarchy
157163 {
@@ -180,17 +186,22 @@ int main(int argc, char *argv[])
180186 + " where linked_place_id IS NOT NULL and ST_Intersects(ST_GeomFromGeoJSON($1), "
181187 " geometry) order by admin_level" ,
182188 border);
183- size_t count = 0 ;
189+ size_t count = 0 ;
190+ size_t failed = 0 ;
184191 for (const pqxx::row &row : r)
185192 {
186193 ++count;
187194 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++;
189197 if (count % printout_step == 0 )
190198 std::cout << " Imported linked records: " << count
191199 << " ; Root elements: " << hierarchy.get_root_count ()
192200 << " ; Missing parents: " << hierarchy.get_missing_count () << std::endl;
193201 }
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;
194205 }
195206
196207 // find missing parents for root nodes
@@ -210,6 +221,8 @@ int main(int argc, char *argv[])
210221 {
211222 std::cerr << " Missing parent with ID " << parent << " . Stopping import\n " ;
212223 hierarchy.print_root_with_parent_id (parent);
224+ std::cerr << " \n SQL:\n " << base_query + " where place_id=" << parent << " \n " ;
225+
213226 return -1 ;
214227 }
215228
0 commit comments