sql-serverdatabasegisogc

Remove Geometry data that is outside of a polygon


I am creating a SQL Server table that takes data from a OS MasterMap layer if it sits within the SITES_TEST layer.

First I am using STIntersects to get the OS MM data into an ASSETS layer.

INSERT INTO ASSETS(GEOMETRY, THEMES) 
    (select b.GEOMETRY, b.THEMES from 
          SITES_TEST a,
          MM_TOPO b
          where a.geometry.STBuffer(1).STIntersects(b.geometry) = 1 AND  (b.THEMES ='Land' or b.THEMES ='Roads Tracks And Paths')) 

The Blue boundary is my site layer and in the background OS MasterMap. Blue boundary is my site layer

After the query above is run in SQL Server it returns the overlapping data as well as the contained data. I get that I could use STContains but then that leaves out data that goes both within and outside the boundary.

enter image description here

I was hoping I would be able to run an UPDATE on the ASSETS table using STDifference.

UPDATE ASSETS(GEOMETRY) 
        (select b.GEOMETRY from 
              SITES_TEST a,
              MM_TOPO b
              where a.geometry.STDifference(b.geometry)=1) 

But I think I am going about it the wrong way as this is returning a boolean error.

Invalid operator for data type. Operator equals equal to, type equals geometry.

Summary: I am trying to remove geometry that is outside of another geometry. The first picture shows a blue polygon, then the SQL script is run which results in the second picture which shows data in red that sits outside the blue boundary polygon from the first picture. I want to remove the data that is now outside the blue polygon.


Solution

  • Instead of just asking for geometries that intersect the polygon of interest, which returns all of the intersecting geometry (as you found out) you want just the parts of the geometry that intersect your polygon of interest. So something like:

    INSERT INTO ASSETS(GEOMETRY, THEMES) 
        (select b.GEOMETRY.STIntersection(a.geometry, b.THEMES from 
              SITES_TEST a,
              MM_TOPO b
              where a.geometry.STBuffer(1).STIntersects(b.geometry) = 1 AND  (b.THEMES ='Land' or b.THEMES ='Roads Tracks And Paths'))