Post

Python & Google Maps API: Find a Manor

I’ve lived in East London for over twenty years, but always within the same couple of square miles. I lack knowledge about Greater London and its surrounding counties. When it comes to finding somewhere to buy a house, then I’m at a bit of a disadvantage.

Wouldn’t it been nice if I could start by finding all the stations approximately one hour or less travel time from the tube station nearest to my office? Then I could plot those stations on a map and get a view of my best chances of finding an area to live in. At some point I could take it further and get more data about that area. Average house prices, average income, any schools or hospitals? Prisons? Sewage Works? What if it could connect to RightMove or Zoopla and pull a list of houses currently for Sale in that area?

Anyway, baby-steps.

FindAManor consists of build-station-dataset, a set of scripts that can generate a list of TFL and/or National Rail stations within a specified distance or travel time (or both) from a selected origin station. The list is generated as JSON, but can be transformed into a CSV file, to be loaded into Google maps which will show configurable markers for each station.

To run it, you’ll need a Google MAPS API key and, yes, this means enabling yourself for billing because it ain’t free, but Google do give you £125/$200 per month credit to play with and, for this project, that’s more than enough and even provides plenty of head-room for testing and repeated runs. I have not been charged a penny while developing these scripts and I still have about £60 left for this month.

The scripts use station data taken from TFL and Trainline. I wanted to minimise usage of Google Maps (and therefore cost) so I calculate initial distances between my origin and all stations using the GeoPy library. This also meant that, once distances have been calculated, I could apply the first filter for free which cuts down drastically on the required calls to Google. For instance, it’s unlikely I’ll be commuting from Aberdeen, so getting rid of that before asking Google to calculate Travel Time was pretty useful. Ditto TFL and fare-zones - A house in Zone 1 is probably a little out of my price range, so it was useful to be able to filter that out early, too.

Yeah, it’s a bit fiddly, but I designed it specifically to be worked through in chunks to avoid repeatedly accessing the Google API on each run and allow a certain amount of check-pointing. Given it’s not something I’d run often - stations don’t tend to get knocked down or built very frequently, and I don’t tend to buy houses that often - it doesn’t really bother me. I suppose it could be stuck into a shell script if you wanted to run the whole thing at once.

The reason the station scripts are in a sub-directory is because I might add some more tools eventually if I ever get around to looking at the RightMove or Zoopla API. It would be nice to pull out average house prices and houses for sale in the areas around the stations.

The README.md contains a run through of an example from Charing Cross. You’d be surprised how far you can get in 50 minutes.

/assets/img/find-a-manor/google-maps-screen-shot.png

This post is licensed under CC BY 4.0 by the author.