Samuel . E . Zih
All posts

· 10 min read · Samuel . E . Zih

Place Containment and Tiered Query Expansion in Intercity Route Search

GeoNames records coordinates and population but no containment relation, so a trip to a suburb was invisible to a search for its city. Why radius matching fails — distance is symmetric, containment is not — and how a seeded child→parent hierarchy with labelled exact, via, within and hub result tiers widens route search without misleading the passenger.

  • Engineering
  • Zoomaa
  • Search
  • Geodata
  • Gazetteer

Abstract

Intercity route search matches a trip when its endpoints equal the places searched. That misses trips to places contained in the searched place — a suburb of the destination city — because the place catalog (GeoNames: 95,351 rows, 14,202 in Ghana) stores coordinates and population but no containment relation, and the official administrative hierarchy disagrees with how travellers use place names. This post derives that relation, shows why the obvious substitute — a distance radius — is wrong (distance is symmetric; containment is not), and describes the model used instead: a one-level child→parent link on each place, seeded once by a hub heuristic (nearest place of 150,000+ within 25 km, at least five times larger) and correctable by hand. Search then widens in labelled tiers — exact, via, within, hub — with the distance printed on every non-exact result and exact matches ranked first under every sort order.

The problem

Somebody put a bus on Zoomaa running from Accra to Santase. Somebody else searched for a bus from Accra to Kumasi. They got nothing.

Santase is in Kumasi. It is a suburb, about four kilometres from the centre — near enough that the difference is a short taxi ride. The bus was going almost exactly where the passenger wanted to go, the app had both facts in its database, and it still returned an empty list.

This is the story of fixing that, and of the two or three places where the obvious fix is wrong in a way that would put somebody on the wrong bus.

A bus loading at O.A station. The passenger and the bus are both in the right place; the software is the part that does not know it.

The database does not know

Zoomaa's place catalog comes from GeoNames, the open geographic dataset. 95,351 rows in production, 14,202 of them in Ghana. Each row carries a name, coordinates, a population, a region, a country, and a list of alternate spellings.

What it does not carry is the relationship. There is no field anywhere in that dataset that says Santase is in Kumasi.

My first instinct was that this must be an administrative fact I could look up. It is not, and the example that taught me is Ejisu — another town everyone in Ghana will tell you is in Kumasi. Administratively Ejisu sits in Ejisu-Juaben Municipal. Kumasi sits in Kumasi Metropolitan. By the official hierarchy the two are unrelated, and anything built on that hierarchy would confidently return nothing.

“In Kumasi” is a travel fact, not an administrative one. No dataset has it, because it is about how people move rather than how a country is divided.

So the relationship has to be derived. That is the first real decision: you are not looking this up, you are making it, and you had better be able to correct it later.

The obvious fix, and why it strands people

The obvious fix is distance. Both places have coordinates. Santase is four kilometres from Kumasi. Just return trips whose destination is within some radius of what was searched.

I nearly built that. It is wrong, and here is the reason:

Distance is symmetric. “Inside” is not.

Ejisu is eighteen kilometres from Kumasi, which means Kumasi is eighteen kilometres from Ejisu. A radius rule cannot tell those two situations apart, so it does both:

  • Search Kumasi, see a bus to Ejisu. Useful. The bus stops short of the city, the passenger finishes the last stretch, and everyone understands the trade.

  • Search Ejisu, see a bus to Kumasi. Also fine, actually — a big terminal has onward transport.

  • Search Ejisu, see a bus to some other village eighteen kilometres the other side of Kumasi. Now somebody is standing at a roadside with no way to get where they were going.

That third case is what a radius gives you for free, and it is the one that hurts a real person on a real morning. Distance tells you two places are near each other. It does not tell you that one of them is the place a traveller means when they say the other.

The last four kilometres are usually solvable. The last forty are a second journey. (Photo: Lariwenga, CC BY-SA 4.0, via Wikimedia Commons.)

What the relationship actually is

The shape I settled on is borrowed from Who's On First, the open gazetteer behind the Pelias geocoder. Every place record carries all of its ancestors as named fields and declares what kind of place it is:

Ashanti

region — country: GH

Kumasi

city — region: Ashanti

Santase

town — region: Ashanti, city: Kumasi

Four levels: country, region, city, town. The child points up. The parent stores nothing about its children.

That last sentence is the part people argue with, so: the alternative is storing a list of children on the parent. Kumasi would carry 583 entries. That document gets rewritten in full every time a single town is added, two simultaneous edits race each other, and it grows forever. A pointer on the child is one small indexed field, and it answers who is inside Kumasi? just as fast — you query for it instead of reading it.

Written once, readable from both ends. Like a birth certificate: the child's record names the parent, and nobody keeps a list of children on the parent, but you can still find all of someone's children by looking through the certificates.

Not a graph

The question I got asked, and it is a fair one: should this be a graph, with real traversal?

The test I use:

If you know the depth when you design it, you have columns, not a graph.

Country, region, city, town. Four levels, decided up front, and it will never be forty. Every query is one hop. A graph database earns its keep when the depth is unknown and the traversal is the question — shortest path, friend-of-a-friend, how is A connected to B.

There is a place in Zoomaa where that will be true, and it is not this one. “Can I get from Accra to Ho with one transfer?” is genuine pathfinding over a route network. Different feature, different tool. Worth knowing where the line is so you do not reach across it out of enthusiasm.

With the hierarchy in place, a corridor search can answer in more than one way.

The town inside the city

Search Accra to Kumasi, and a bus to Santase counts — because Santase sits inside Kumasi. The bus falls short of the destination by four kilometres.

The stop along the way

This one I had missed entirely. A bus running Accra to Kumasi that stops at Santase on the way was invisible to anyone searching for Santase, even though it would take them there directly. The stops were in the database the whole time; search simply never looked at them.

It is the better half of the feature, and the safer one: the bus genuinely goes there.

Every trip already carries its stops — here, Kpong and HO station on an Accra–Ho run. Search now matches against them, so a bus is findable by where it passes through, not only by where it terminates.

Saying which is which

Widening a search is easy. Widening it honestly is the actual work, because these four results are not the same promise:

exact

runs between the two places searched

via

stops at a searched place on its way somewhere else

within

serves a town inside the searched city — falls short

hub

serves the city above the searched town — overshoots

Each one is labelled on the card, and anything that is not exact says so above everything else, with the distance printed: “Santase · 4 km from Kumasi”.

Not “nearby”. The number. Four kilometres is a taxi; twenty-four is a second journey, and the passenger is the one who knows which of those they can absorb today.

Kejetia, Kumasi. If your bus stops four kilometres from here you are fine. The app's job is to tell you it is four and not forty.

Exact always wins

A near miss never outranks a bus that actually goes there — in any sort mode. That sounds obvious until you try it with cheapest, where the bus that stops twenty kilometres short is, of course, the cheap one. The cheapest row is exactly the one somebody taps without reading a label.

So the ordering is: tier first, then whatever sort they chose, then distance. It is applied in the reverse of that order in code, with stable sorts, so each pass survives the next. Getting it backwards — sorting by tier first — does nothing at all, because the sort that follows simply undoes it.

Label, do not override

One rule runs through all of this. If somebody explicitly picks cheapest and the cheap bus stops twenty-four kilometres out, it stays at the top of its tier, with the twenty-four kilometres printed on it.

Quietly reordering their choice because I think I know better is worse than telling them the truth and letting them decide. Every hard call in this feature resolved the same way.

The part I got wrong

I argued firmly that the relationship must only ever be read downwards. Search a city, see its towns. Search a town, never see the city — because a bus to Kumasi does not stop in Ejisu, and offering it sells somebody a bus that never comes near them.

I was overcorrecting, and the objection that changed my mind was one sentence:

We do not promise them anything. We say “no direct matches — these are nearby.”

Which is right. The danger was never the direction. It was unlabelled mixing — a near miss sitting in the list looking like an answer. Once the section is honest about what it is and the distance is on the card, the passenger has everything they need.

And that direction is arguably the safer one. A bus that overshoots to a big terminal leaves you somewhere with onward transport. A bus that falls short leaves you in a village.

Circle, Accra. Overshooting to a terminal like this is recoverable; being dropped short of a village is not. (Photo: Joris Darlington Quarshie, CC BY-SA 4.0, via Wikimedia Commons.)

The relationship has to be created, so it is inferred from the two things the catalog does have — geography and size:

A town belongs to the nearest hub (population 150,000+) within 25km, provided that hub is at least five times its size.

The size test is not decoration. Without it two neighbouring towns adopt each other, and worse, a hub becomes the child of a bigger hub down the road. Tema must never end up inside Accra — a bus to Accra is not a bus to Tema, and that one rule is the difference between a useful widening and a wrong one at the busiest corridor in the country.

I ran it at 45km first. It produced 4,455 links and filed 950 towns inside Obuasi, a mining town of 179,000. That is a region, not a city. At 25km it produced 2,337 links, still caught Santase at four kilometres and Ejisu at eighteen, and halved the noise. Kumasi ended up with 583 children.

And critically, the radius is a seeding parameter, not a search one. It runs once to create links. No search ever consults it — which is exactly how the symmetry problem is kept out of the live path.

The links are then editable by hand, because a heuristic over coordinates will be wrong somewhere and the people running the platform know things the coordinates do not.

What it looks like now

Accra to Kumasi, on the live API:

Accra → Kumasi

exact

Accra → Kumasi

exact

Accra → Santase

nearby

Accra → Santase

nearby

Two buses that go there, then two that go nearly there, in that order, each saying which it is.

Accra to Kumasi. The corridor a passenger types is a simple thing to say and a surprisingly layered thing to answer.

What I would tell myself at the start

  • The data will not contain the thing you need. Coordinates and populations are facts. “In Kumasi” is a judgement, and judgements have to be derived, stored, and made correctable.

  • Check the direction of every relationship. Distance looked like the answer for about an hour. It is symmetric, and the asymmetry is the entire point.

  • Widening a search is a promise. Every extra result is the app saying “this might be what you meant.” If it cannot say how confident it is, it should not say anything.

  • Tune against the real catalog, not a guess. 45km and 25km are both defensible numbers right up until you run them and see 950 towns inside a mining town.

The whole thing is one indexed field, one seeding script and a sort order. The hard part was never the code. It was working out what “in Kumasi” means precisely enough to write it down.

More from the blog