I was fascinated by Lauren Leek’s Open Food Map, but it wasn’t much use to me in Vancouver until she kindly open sourced it, at which point I could run it and see what hidden gems there were in Vancouver to check out.
Except $10 of API calls later, things were a little sparse: look at Yaletown, an area full of restaurants:

There are a lot more restaurants in that area than the map shows. Reading through Google’s Places API docs, I thought that was because there’s a maxResultCount parameter that defaults to 20, and as by default Lauren’s code divides up the target area into 1.5 km wide squares, and then searches in 1500m radii around each vertex of the grid, Google will happily provide something but not anything like a complete set of restaurants, especially in dense areas. But this was a misapprehension: the code is using the legacy Places API (which has a higher result max (up to 60) which is still vulnerable to missing restaurants in high-density areas
What to do?
We can just make the grid smaller. This costs more – if we create a grid with 0.5 km squares, we’ll have 9 times as many squares, and so we’ll spend 9 times as much. This is fine if you have an unlimited budget, but gets expensive otherwise.
We can offset this by shrinking the overall area we’re inspecting. In my first run, I tried to get most of the Vancouver area, with a grid running from latitude 49.205 to 49.291 and longitude -123.264 to -123.023. Roughly that’s going to be 0.241 * 110540 (the height of the area in metres) times the length of the area in metres (which is 0.86 * 111,320 * cos(radians(49.248)) (don’t sue me, I’m not a professional geographer), which is…
26,640 * 6,249 = 166.5 million m2 or 166.5 square kilometres, or about 74 1.5 km squares, which gives us 300 API calls.
Now, I don’t care about restaurants in the green or blue areas of this map:

so I can probably constrain things a bit more – rather than brute forcing it to look at every vertex in a grid, I could find points of interest across Vancouver and then search around each of those, but that’s more than my little brain could cope with initially, and would still hit the 20 result constraint Google inflicts on us. So instead, let’s use the initial grid method, but restrict to Yaletown:
Or to be more specific, let’s take the square from the corner of Burrard and Davie (that’s 49.279031, -123.129166) to where the Cambie Bridge crosses Morningside (that’s 49.273494, -123.115017), which according to my arithmetic is a rectangle about 1.5 km high, and 435 metres wide. Chop that up into 500 metre steps first, and see what we get (an enormous Google Cloud Services bill? Hopefully not… (Google Maps Platform core services pricing list tells me I get 1,000 free calls to the API per month, which isn’t much, but hey, it’s the 4th of March, plenty of time to go…)
python src/data_collection/collect_data.py --city-name yaletown-500 --lat-min 49.273 --lat-max 49.279 --lon-min -123.129 --lon-max -123.115 --grid-step 0.5 --radius 500
That hauled back 165 points of interest! Then we run Lauren’s analytic code, and then visualise it:
Better: here’s the 1.5km grid steps:

and here’s the 500m grid steps:

That’s a lot more restaurants (hooray!) but we’re still missing my favourite, the ludicrously named Tasty Indian Bistro. (Pro tip: the Surrey location is better than the Yaletown one, but if you can’t face the drive from East Van, taking the train to Yaletown is much easier).
We could carry on shrinking the grid size, but I don’t want to generate API calls ad infinitum, so in the meantime, let’s look at what the API returns. Of the 165 restaurants Google tells us about … there are 3 7-Elevens, 5 Subways, 3 Tim Hortons, 2 A&Ws and 2 McDonalds. Apparently there’s only 1 KFC, so I guess if you’d already ruled generic fried chicken out of the running for your fancy meal, Google’s almost in agreement with you. Can we solve for this at all?
According to Google, the 7-Elevens have types such as
cafe,convenience_store,establishment,food,liquor_store,point_of_interest,restaurant,store
I did assume I needed to call the Liquor Control Board and grass 7-Eleven up, but apparently they’ve had licenses to sell booze for over a year in B.C. now. So the more you know. But just because there’s some microwaved hot dogs available, I wouldn’t call that a cafe.
So at this point I’m a little sad that the stated approach (“I scraped every single restaurant in Greater London and built a machine-learning model”) doesn’t quite match with what I get (“I asked Google and it gave me what it thought was a reasonable subset of all the places in Vancouver where you can get food, and then I used Lauren’s machine-learning model”). But we can try to amend the code to force Google to not return the things we don’t want.
We could use the optional excludedTypes parameter to try to filter out some of the things we don’t want. But! it doesn’t have any effect because that’s a parameter available in the new Places API, and not the Legacy one.
convenience_store
So the base version of the code generates the Places API call to obtain places within x metres as
params = {"key": API_KEY, "location": f"{lat},{lon}", "radius": radius_m, "type": place_type}
Let’s try tweaking this to add the excludedTypes
params = {"key": API_KEY, "location": f"{lat},{lon}", "radius": radius_m, "type": place_type, "excludedType": "convenience_store"}
To my surprise, running this new code gives us many more points of interest: we’ve gone from 165 to 226! (And this hasn’t excluded the 7-Elevens… which suggests I don’t know quite what I’m doing with the API call code, and need to work on that, as well as raising the worrying suspicion that the API results aren’t deterministic, because you wouldn’t expect to get 37% more restaurants just by making the same requests twice.) My earlier suspicion was slightly wrong though, because I thought the code was calling the current Places API, when in fact it was using the Legacy API, which would have no way to accept the excludedType parameter.
The further good news is that in its inexplicable generosity, this time around Google has returned Tasty Indian Bistro in the results (success!) but because of code to infer the cuisine, Open Food Map thinks it’s French because of the word ‘bistro’ in the name1. Gerry Pack told me more than 20 years ago that no good news is ever as good as you think it is, and no bad news is as bad as you think it is either. [Although perhaps he didn’t consider if what’s good news to some people might be quite bad for others, and thus possibly worse…]
My suspicion on the classification is that the code does it the wrong way round: it starts by looking at the name of the restaurant, then at the editorial summary, and then at reviews. So in the case of Tasty Indian Bistro, you’d hope that the reviews mentioned Indian food and would be more reliable than the title. (Of course, that’s not helped if every review says “Tasty Indian Bistro is great” and the classifier spots the Bistro before the Indian…
Then there’s the fun subject of how to identify different areas in Vancouver, for which Local area boundary — City of Vancouver Open Data Portal may be useful…
1 Just next door is Paratha 2 Pasta, which gets classified as Italian, which is maybe … half right?
