Search code examples
sqlsql-serverpolygongeography

SQL-Server Geography Remove an overlap


I'm trying to remove an overlap with the SQL-Server 2012 but i have no clue how to do it.

Example:

CREATE TABLE #temp(
[Geo] [geography] NOT NULL)
INSERT INTO #temp
VALUES (geography::STGeomFromText('POLYGON((0 0, 0.1 0, 0.1 0.1, 0 0.1,0 0))', 4326))
INSERT INTO #temp
VALUES (geography::STGeomFromText('POLYGON((0.05 0.05, 0.15 0.05, 0.15 0.15, 0.05 0.15, 0.05 0.05 ))', 4326))
SELECT * FROM #temp

My idea would be to use the STIntersection() function to get the overlapping polygon and then somehow to delete it from one of the polygons.


Solution

  • As per the comments above, STDifference will do the trick. That is, given two geography instances a & b that may or may not have an overlap, setting a = a.STDifference(b) will remove any area from a that intersects with b.