Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Erik-H-zz
Created March 11, 2022 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Erik-H-zz/018caedd849889d3c478d4a7727af094 to your computer and use it in GitHub Desktop.
Save Erik-H-zz/018caedd849889d3c478d4a7727af094 to your computer and use it in GitHub Desktop.
CREATE view [Model].[vw_GeoIntersects]
as
WITH Coordinates AS (
SELECT
ID
, Latitude
, Longitude
, geometry::Point(Longitude, Latitude, 0) AS LatLon
FROM Ref.MarpolCoordinates
), Intersects AS (
SELECT
ID
, c.Latitude
, c.Longitude
, CAST(c.LatLon.STIntersects(a.AreaCoordinates) AS INT) as inArea
FROM Coordinates c
CROSS JOIN Ref.MarpolAreas a
)
SELECT CAST(ID AS VARCHAR(50)) AS ID
, '' AS AreaCoordinates
, 50 AS Size -- required for Power BI visual
, i.Latitude
, i.Longitude
, i.inArea AS InMarpolArea
FROM Intersects i
UNION ALL
SELECT ma.MarpolAreaName
, CAST(ma.AreaCoordinates AS varchar(max)) AS AreaCoordinates
, 50 AS Size
, NULL
, NULL
, NULL
FROM Ref.MarpolAreas ma
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment