Nearest neighbor using BQ Scripting

We’ve already discussed Nearest Neighbors problem:

The general idea is to start with small search radius, and increase it if we fail to find anything. Since that article, we got BigQuery Scripting, so let’s use it to do this. This time we’ll solve single neighbor search — find nearest neighbor for a specific point, rather than finding neighbors for a set of points.

For demo purposes, we’ll search nearest road in US Tiger database. Note it is not a very good road dataset for actual use, but fine for demo. The search with a specific radius will look like this:

Where search_r is search radius, and center is the point for which we are trying to find a neighbor. We’ll start with search radius of 100 m and increase it 10x each time if we don’t find anything with current radius. The procedure will look like this:

You can call it like this:

Hi, I'm TL of BigQuery GIS project. Posting small recipes and various notes for BQ GIS users.

Hi, I'm TL of BigQuery GIS project. Posting small recipes and various notes for BQ GIS users.