Replicating the ST_LineLocatePoint function from PostGIS in Google BigQuery

Replicating the ST_LineLocatePoint function from PostGIS in Google BigQuery

PostGIS has a function ST_LineLocatePoint which is really useful in multiple use cases including snapping lines to roads (common in mobility use cases, or for cleaning GPS data) as I've previously shown here. Unfortunately Google BigQuery does not yet have this function, however it is easy to replicate!

ST_LineLocatePoint returns the distance of the closest point along a line as a fraction of it's length as shown in the diagram above. To replicate this, we can use the following SQL:

No alt text provided for this image

To make this more repeatable, we can turn it into a persistent UDF (User Defined Function) ready for re-use:

No alt text provided for this image

Which can then be used like any other function:

No alt text provided for this image

Code available as a Github Gist here - enjoy!

Reza G.

Building the Future of Traffic Intelligence and Mobility Solutions

2y

Thanks ! However, it only works for a simple LineString with only one segment. If you have a more complex LineString, the result would be wrong.

Like
Reply

To view or add a comment, sign in

More articles by Simon Wrigley

  • Is Cambridge the North?

    In England the term "North South divide" refers to the cultural, economic, and social differences between the North and…

    6 Comments
  • Exploring London Underground station coverage using a spatial index

    After feeling inspired by my colleague Helen McKenzie's post on which London Underground stations you can walk between…

    4 Comments
  • Open Street Map Footprints

    Following on from my previous post on the OSM edit history of the United Kingdom, I was inspired by Gareth Bathers…

    2 Comments
  • Advanced Spatial SQL: Window Functions

    Window functions allow you to calculate statistics from a dataset whilst preserving the original row count (as opposed…

    1 Comment
  • How to query OpenStreetMap data using Google BigQuery.

    OpenStreetMap is an incredible resource of open source geographic data and Google have made the entire planet dataset…

    1 Comment
  • Exploring Rescues at Sea using Spatial SQL & CARTO.

    The Royal National Lifeboat Institution (RNLI) is an institution beloved by many in the United Kingdom who provide…

    2 Comments
  • Spatial SQL: Should you ever GROUP BY geometry/geography?

    TLDR: Probably not, unless you are sure of what you are doing! In spatial analysis, it is common to use aggregate…

    8 Comments

Insights from the community

Others also viewed

Explore topics