STUnion Resulting in huge number of vertices

Unanswered STUnion Resulting in huge number of vertices

  • Monday, April 30, 2012 2:51 PM
     
      Has Code

    Hello,

    I am trying to merge to polygons using STUnion method. The resulting polygon seems to have a vertex where huge number of vertices are stacked over each other. Here is the code I am using to merge-

    DECLARE @Polygon1 geometry  
    SET @Polygon1 = geometry::STPolyFromText('POLYGON ((-10553853.400672335 4002307.7175941337, -10553858.803142548 4002348.6219978333, -10553912.122381078 4002360.8854242279, -10553931.955673218 4002310.851436615, -10553853.400672335 4002307.7175941337))',3857)
    
    DECLARE @Polygon2 geometry  
    SET @Polygon2 = geometry::STPolyFromText('POLYGON ((-10553858.204683276 4002344.09157344, -10553835.215061188 4002355.7880477905, -10553899.708805084 4002368.6270828247, -10553900.671458066 4002358.2518135007, -10553858.803119659 4002348.6220970154, -10553858.803115845 4002348.6220932007, -10553858.80311203 4002348.6220932007, -10553858.803108215 4002348.6220932007, -10553858.8031044 4002348.622089386, -10553858.803100586 4002348.622089386, -10553858.803096771 4002348.622089386, -10553858.803092957 4002348.6220855713, -10553858.803089142 4002348.6220855713, -10553858.803089142 4002348.6220817566, -10553858.803085327 4002348.6220817566, -10553858.803081512 4002348.6220779419, -10553858.803077698 4002348.6220741272, -10553858.803073883 4002348.6220741272, -10553858.803073883 4002348.6220703125, -10553858.803070068 4002348.6220664978, -10553858.803066254 4002348.6220664978, -10553858.803066254 4002348.6220626831, -10553858.803062439 4002348.6220588684, -10553858.803062439 4002348.6220550537, -10553858.803058624 4002348.6220550537, -10553858.80305481 400
    2348.622051239, -10553858.80305481 4002348.6220474243, -10553858.803050995 4002348.6220397949, -10553858.80304718 4002348.6220321655, -10553858.80304718 4002348.6220283508, -10553858.803043365 4002348.6220207214, -10553858.803043365 4002348.622013092, -10553858.204683276 4002344.09157344))',3857)
    
    
    
    
    
      var firstStandGeom = SqlGeometry.Parse(firstStandDTO.StandAreaPolygonWKT);
                var secondStandGeom = SqlGeometry.Parse(secondStandDTO.StandAreaPolygonWKT);
                double dist = firstStandGeom.STDistance(secondStandGeom).Value;
                double buffer = Constants.SPLIT_MIN_BUFFER;
                if (dist < Constants.SPLIT_MAX_BUFFER)
                    buffer = .01;
                //buffer the two stands and merge them to avoid cases where really close polygons end up being multipolygons
                //then buffer them negatively to offset the buffer done above
                firstStandGeom = firstStandGeom.STBuffer(buffer)
                    .STUnion(secondStandGeom.STBuffer(buffer))
                    .STBuffer(-1 * buffer);
    

    Any help on this will be highly appreciated.

    Thanks

    Apurva


All Replies

  • Monday, April 30, 2012 2:40 PM
     
      Has Code

    Hello,

    I am trying to merge to polygons using STUnion method. The resulting polygon seems to have a vertex where huge number of vertices are stacked over each other. Here is the code I am using to merge-

    DECLARE @Polygon1 geometry  
    SET @Polygon1 = geometry::STPolyFromText('POLYGON ((-10553853.400672335 4002307.7175941337, -10553858.803142548 4002348.6219978333, -10553912.122381078 4002360.8854242279, -10553931.955673218 4002310.851436615, -10553853.400672335 4002307.7175941337))',3857)
    
    DECLARE @Polygon2 geometry  
    SET @Polygon2 = geometry::STPolyFromText('POLYGON ((-10553858.204683276 4002344.09157344, -10553835.215061188 4002355.7880477905, -10553899.708805084 4002368.6270828247, -10553900.671458066 4002358.2518135007, -10553858.803119659 4002348.6220970154, -10553858.803115845 4002348.6220932007, -10553858.80311203 4002348.6220932007, -10553858.803108215 4002348.6220932007, -10553858.8031044 4002348.622089386, -10553858.803100586 4002348.622089386, -10553858.803096771 4002348.622089386, -10553858.803092957 4002348.6220855713, -10553858.803089142 4002348.6220855713, -10553858.803089142 4002348.6220817566, -10553858.803085327 4002348.6220817566, -10553858.803081512 4002348.6220779419, -10553858.803077698 4002348.6220741272, -10553858.803073883 4002348.6220741272, -10553858.803073883 4002348.6220703125, -10553858.803070068 4002348.6220664978, -10553858.803066254 4002348.6220664978, -10553858.803066254 4002348.6220626831, -10553858.803062439 4002348.6220588684, -10553858.803062439 4002348.6220550537, -10553858.803058624 4002348.6220550537, -10553858.80305481 400
    2348.622051239, -10553858.80305481 4002348.6220474243, -10553858.803050995 4002348.6220397949, -10553858.80304718 4002348.6220321655, -10553858.80304718 4002348.6220283508, -10553858.803043365 4002348.6220207214, -10553858.803043365 4002348.622013092, -10553858.204683276 4002344.09157344))',3857)
    
    
    
    
    
      var firstStandGeom = SqlGeometry.Parse(firstStandDTO.StandAreaPolygonWKT);
                var secondStandGeom = SqlGeometry.Parse(secondStandDTO.StandAreaPolygonWKT);
                double dist = firstStandGeom.STDistance(secondStandGeom).Value;
                double buffer = Constants.SPLIT_MIN_BUFFER;
                if (dist < Constants.SPLIT_MAX_BUFFER)
                    buffer = .01;
                //buffer the two stands and merge them to avoid cases where really close polygons end up being multipolygons
                //then buffer them negatively to offset the buffer done above
                firstStandGeom = firstStandGeom.STBuffer(buffer)
                    .STUnion(secondStandGeom.STBuffer(buffer))
                    .STBuffer(-1 * buffer);
    

    Any help on this will be highly appreciated.

    Thanks

    Apurva


  • Monday, April 30, 2012 7:14 PM
    Answerer
     
      Has Code

    I'm not quite sure what the description of vertices "stacked over each other" means, but if you mean that a lot of vertices are created close to each other, this is a by-product of the buffering/un-buffering that you're doing rather than of the union. You can demonstrate this as follows:

    SELECT @Polygon1.STNumPoints(), --5 points, @Polygon2.STNumPoints(), -- 33 points @Polygon1.STUnion(@Polygon2).STNumPoints(), -- 41 points @Polygon1.STBuffer(0.01).STUnion(@Polygon2.STBuffer(0.01)).STBuffer(-0.01).STNumPoints() -- 133 points

    Buffering often increases the number of points in a geometry, because it creates "rounded" edges around what would otherwise be sharp points. If the purpose of buffering is to ensure that close geometries overlap prior to union'ing (as suggested by the comments in your code), you might want to try the BufferWithTolerance() method instead, which creates a simpler buffer.


    twitter: @alastaira blog: http://alastaira.wordpress.com/

  • Monday, April 30, 2012 8:04 PM
     
      Has Code

    Thank you so much Tanoshimi. Yes the purpose of buffer is to make sure that close geometries overlap. Let me try the BufferWithTolerance() method.

    Also...when I try unioning 2 polygons, sometimes the boundary common to the two polygons doesnt dissolve after union. Here is the code-

    DECLARE @Polygon1 geometry  
    SET @Polygon1 = geometry::STPolyFromText('POLYGON ((-11053412.341470713 4227922.3937791167, -11053387.960144043 4227964.0101852417, -11053439.913299561 4227958.0385742187, -11053448.870773315 4227993.2712097168, -11053516.947418213 4227959.2328948975, -11053533.070922852 4227992.6740646362, -11053565.875044087 4227979.0999438437, -11053574.27520752 4227924.2988815308, -11053412.341470713 4227922.3937791167))',3857)
    DECLARE @Polygon2 geometry
    SET @Polygon2 = geometry::STPolyFromText('POLYGON ((-11053376.613838196 4227947.2897109985, -11053359.893234253 4227987.8968811035, -11053408.263542175 4227982.5223999023, -11053412.928718122 4227961.1403410006, -11053387.960090637 4227964.0102767944, -11053387.96005249 4227964.0101318359, -11053397.020150166 4227948.54548392, -11053376.613838196 4227947.2897109985))',3857)
    DECLARE @Polygon3 geometry
    SET @Polygon3=@Polygon1.STUnion(@Polygon2)
     select @Polygon3,
    @Polygon3.STAsText()

    Is there something I am doing wrong?

    Thanks so much again!!

    Apurva

  • Monday, April 30, 2012 8:16 PM
     
      Has Code

    So I am changing my method to this-

      //Merge and update the shapes
                var firstStandGeom = SqlGeometry.Parse(firstStandDTO.StandAreaPolygonWKT);
                var secondStandGeom = SqlGeometry.Parse(secondStandDTO.StandAreaPolygonWKT);
                double dist = firstStandGeom.STDistance(secondStandGeom).Value;
    
    firstStandGeom = firstStandGeom.BufferWithTolerance(dist, 0.001, true).STUnion(secondStandGeom.BufferWithTolerance(dist, Constants.0.001, true)).BufferWithTolerance(-1*dist,0.001, true);
                
                
                SqlGeometry outGeom;
                SpatialUtility.CleanArtifact(firstStandGeom, out outGeom);
    Do the parameters to BufferWithTolerance method look okay? And is the method to remove the buffer (by applying negative distance) is the right way to do things.

    Sorry I am not completely sure and am using this method first time  so...

    Thanks again.

    Apurva

  • Tuesday, May 01, 2012 2:19 PM
     
     

    I have tried BufferWithTolerance with parameters as I specified earlier...it has definitely reduced the multi-vertices case. But we still get them sometimes. Do you suggest using Reduce method to get rid of multiple vertices very close to each other?

    Thanks

  • Tuesday, May 01, 2012 2:36 PM
    Answerer
     
     

    @g.BufferWithTolerance()

    is a bit like calling:

    @g.STBuffer().Reduce()

    The exact values you should supply for the tolerance can only really be determined by trial and error to a level you're happy with, but you shouldn't need to call Reduce() on the result of BufferWithTolerance() as well.


    twitter: @alastaira blog: http://alastaira.wordpress.com/