Skip to content

ts.insertsite is missing lat-long trigger #13

@ecgrimm

Description

@ecgrimm

ts.insertsite is missing the trigger to insert the lat-long columns. Here is the SQL Server version.

CREATE TRIGGER [NDB].[TR_Sites_BBox] ON [NDB].[Sites]
AFTER INSERT, UPDATE
AS
  IF (UPDATE(geog))
    BEGIN
	  -- for points, populate box columns with coords of points
      IF EXISTS (SELECT * FROM inserted WHERE geog.STGeometryType() = 'POINT')
        BEGIN
		  UPDATE 
			NDB.Sites 
		  SET 
			LatitudeNorth = i.geog.Lat,
			LatitudeSouth = i.geog.Lat,
			LongitudeEast = i.geog.Long,
			LongitudeWest = i.geog.Long
		  FROM 
			NDB.Sites s JOIN 
			INSERTED i ON s.SiteID = i.SiteID
		  WHERE
			i.geog.STGeometryType() = 'POINT'
		END

	  -- for polygons, populate box columns from poly envelopes
      IF EXISTS (SELECT * FROM inserted WHERE geog.STGeometryType() = 'POLYGON')
	    BEGIN
		  UPDATE 
			NDB.Sites 
		  SET 
			LatitudeNorth = geometry::STGeomFromWKB(i.geog.STAsBinary(), 4326).STEnvelope().STPointN(3).STY,
			LatitudeSouth = geometry::STGeomFromWKB(i.geog.STAsBinary(), 4326).STEnvelope().STPointN(1).STY,
			LongitudeEast = geometry::STGeomFromWKB(i.geog.STAsBinary(), 4326).STEnvelope().STPointN(3).STX,
			LongitudeWest = geometry::STGeomFromWKB(i.geog.STAsBinary(), 4326).STEnvelope().STPointN(1).STX
		  FROM 
			NDB.Sites s JOIN 
			INSERTED i ON s.SiteID = i.SiteID
		  WHERE
			i.geog.STGeometryType() = 'POLYGON'
		END
	END
GO

ALTER TABLE [NDB].[Sites] ENABLE TRIGGER [TR_Sites_BBox]
GO

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions