'In Bigquery, how to determine if a point is just outside a geographic region?
I have a geographic table that contains polygons that represent US coastal waters. I want to find the geopoints that fall just outside these regions, say that are just outside by 2 miles. I'm testing the DWithin function, but that is displaying points within my polygons.
with base_tbl1 as (SELECT ais.*, ST_GEOGPOINT(lon, lat) as geo_point
FROM `AIS_positions` as ais)
SELECT *
FROM (
SELECT a.*, ST_DWITHIN(US.geometry, a.geo_point, 0) as test
FROM base_tbl1 as a,
`US_EEZ` as US
)
where test = True
Solution 1:[1]
ST_DWITHIN means "distance between geographies is within X meters", so that includes both regions outside and within the polygon itself.
There are several approaches you can use, two I can think of:
- simply exclude the inner points:
WHERE ST_DWITHIN(US.geometry, a.geo_point, 2 * 1609) AND
NOT ST_INTERSECTS(US.geometry, a.geo_point)
- buffer the US polygon by 2 miles, subtract the original polygon - this gives you approximated 2 miles buffer zone without polygons itself. Note that buffer is an expensive operation, so I would probably do it once and save the result to be used in following queries:
ST_DIFFERENCE(ST_BUFFER(geometry, 2 * 1609), geometry)
You can then test if a point falls into this buffer zone with ST_INTERSECTS check.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Michael Entin |
