Skip to content

Commit 5081ded

Browse files
committed
Account for parent ids that are links to some other items
1 parent 8fcb76f commit 5081ded

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

importer/src/hierarchy.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,19 @@ void Hierarchy::add_item(std::shared_ptr<HierarchyItem> &item)
4141
}
4242
}
4343

44-
void Hierarchy::add_linked_item(std::shared_ptr<HierarchyItem> &item)
44+
bool Hierarchy::add_linked_item(std::shared_ptr<HierarchyItem> &item)
4545
{
4646
hindex linked = item->linked_id();
4747
auto tolink = m_items.find(linked);
4848
if (tolink == m_items.end())
4949
{
5050
std::cout << "Failed to find linked object " << linked << " required by " << item->id()
51-
<< ". Skipping linkage.";
52-
return;
51+
<< ". Skipping linkage.\n";
52+
return false;
5353
}
5454

5555
tolink->second->add_linked(item);
56+
return true;
5657
}
5758

5859
void Hierarchy::cleanup()

importer/src/hierarchy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Hierarchy
1919
~Hierarchy();
2020

2121
void add_item(std::shared_ptr<HierarchyItem> &item);
22-
void add_linked_item(std::shared_ptr<HierarchyItem> &item);
22+
bool add_linked_item(std::shared_ptr<HierarchyItem> &item);
2323
void set_country(const std::string &country, hindex id);
2424
void cleanup();
2525
void finalize();

importer/src/main.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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 << "\nSQL:\n" << base_query + "where place_id=" << parent << "\n";
225+
213226
return -1;
214227
}
215228

0 commit comments

Comments
 (0)