-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from GlobalFishingWatch/22-dateline
Handle fishing events crossing the dateline
- Loading branch information
Showing
3 changed files
with
72 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#standardSQL | ||
|
||
# Utility Functions | ||
|
||
|
||
# Get the longitude of the antipode for a given longitude | ||
# | ||
# This funtion transforms the longitude to the meridian on the opposite side of the world | ||
# This is useful for doing operations with data that spans the anti-meridian | ||
# | ||
# Apply the function again to the transformed value to return to the original meridian | ||
# | ||
# Example | ||
# anti_lon(0.0) = 0.0 | ||
# anti_lon(90.0) = -90.0 | ||
# anti_lon(-90.0) = 90.0 | ||
# anti_lon(-179.0) = 1.0 | ||
# anti_lon(1.0) = -179.0 | ||
# | ||
CREATE TEMPORARY FUNCTION anti_lon ( lon FLOAT64 ) | ||
AS ( | ||
IF (lon < 0, 180.0 + lon, (180 - lon) * -1) | ||
); | ||
|
||
|
||
# Convert a lat,lon pont in a GEOGRAPHY, such as created with ST_GEOGPOINT(lon, lat) | ||
# Retrns a STRUCT(lon, lat) | ||
# | ||
# It seems ridiculous that we have to convert to json and then parse it to do this, but bigquery | ||
# does not provide any other way to get the lat/lon out of a GEOGRAPHY | ||
|
||
CREATE TEMPORARY FUNCTION geopoint_to_struct (pt GEOGRAPHY) | ||
AS ( | ||
STRUCT( | ||
CAST(JSON_EXTRACT_SCALAR(ST_ASGEOJSON(pt), "$['coordinates'][0]") AS FLOAT64) as lon, | ||
CAST(JSON_EXTRACT_SCALAR(ST_ASGEOJSON(pt), "$['coordinates'][1]") AS FLOAT64) as lat | ||
) | ||
); |