SQL Server Spatial - STIntersection

  • I am having an issue with STIntersection. I want to intesect a polygon with a linestring an have it return the cut linestrings. I assumed STIntersection would do this and it will in certain situations. What I am running into as in the example below is that instead of keeping the integrity of the original linestrings, STIntersection is creating a new linestring for the clipped area and also for each time a line intersects another line. So instead of getting 5 new linestrings in a Multilinestring geography, I am getting 20 representing each intersection of the linestrings with themselves. I really need a way to only cut the linestring where it intersects the polygon and not create a linestring for each intersection against itself. I tried to figure a way to reassemble them, but I cannot tell which pieces should go together. ESRI can handle this and so far I haven't found anything that I can do in ESRI that I could not do in SQL Spatial.

    Thanks in advance for any help or advice.

    declare @POLYGON geography

    declare @LINESTRING geography

    set @POLYGON = geography::STGeomFromText('POLYGON ((3.4293073891935575 56.665143418074329, 3.4186112463339011 56.670796145703463, 3.4074210497749329 56.676152105069448, 3.395763821883333 56.6811982646792, 3.3836677560585464 56.685922341716854, 3.3711621477806868 56.690312832864493, 3.3582773225608427 56.694359043211691, 3.3450445609810724 56.698051113171722, 3.3314960210228053 56.701380043328655, 3.3176646578930211 56.70433771714422, 3.3035841415676708 56.706916921459893, 3.2892887722809538 56.709111364734895, 3.2748133941975 56.710915692968015, 3.2601933075119414 56.7123255032568, 3.2454641792268695 56.713337354955279, 3.2306619528656695 56.71394877839748, 3.2158227573811824 56.714158281161446, 3.20098281552452 56.713965351855627, 3.1861783519406206 56.713370461416453, 3.1714455012582783 56.712375061913377, 3.1568202164424273 56.710981582864918, 3.1423381776753252 56.709193425076286, 3.1280347020310897 56.70701495201677, 3.1139446542046794 56.70445147876174, 3.1001023585520233 56.701509258531559, 3.0865415126925551 56.698195466866316, 3.073295102918904 56.694518183482117, 3.0603953216511233 56.69048637186124, 3.04787348716445 56.686109856634829, 3.0357599658104149 56.681399298822576, 3.0240840969411535 56.676366169000254, 3.012874120736055 56.67102271847083, 3.0021571091185115 56.665381948520583, 2.9919588999386639 56.659457577846609, 2.9823040345855336 56.653264008246154, 2.973215699179153 56.646816288663288, 2.9647156694800696 56.640130077691374, 2.9568242596401886 56.633221604634329, 2.9495602749052541 56.626107629231974, 2.9429409683655421 56.618805400157925, 2.9369820018375936 56.611332612401256, 2.9316974109460419 56.603707363644375, 2.9270995744610118 56.59594810975193, 2.9231991879331152 56.588073619486778, 2.9200052416548661 56.5801029285696, 2.9175250029644264 56.572055293199767, 2.9157640028950511 56.563950143155182, 2.9147260271614197 56.55580703458871, 2.9144131114623195 56.54764560263834, 2.9148255410678807 56.53948551396774, 2.9159618546488235 56.531346419352623, 2.9178188522949182 56.523247906427223, 2.9203916076602088 56.515209452703409, 2.9236734841634164 56.507250378973559, 2.9276561551633735 56.4993898032057, 2.9323296280213964 56.491646595037722, 2.9376822719550661 56.484039330974511, 2.9437008495810848 56.47658625038963, 2.9503705520385903 56.469305212429859, 2.9576750375785781 56.462213653918354, 2.9655964734999007 56.455328548348682, 2.9741155813076343 56.448666366058873, 2.9832116849653878 56.442243035671225, 2.9928627621094273 56.436073906879912, 3.0030454980891959 56.430173714665209, 3.0137354753850834 56.424556478619756, 3.024906846178145 56.419235676754752, 3.0365327683785304 56.414224001006986, 3.048585349184711 56.409533401124051, 3.0610357111406761 56.405175057055828, 3.0738540601590087 56.401159353164, 3.0870097553612377 56.397495854301049, 3.1004713805860185 56.394193283806167, 3.1142068174150674 56.391259503461335, 3.1281833195662361 56.388701495446988, 3.1423675885027404 56.386525346332157, 3.1567258501073088 56.384736233130489, 3.1712239322697577 56.3833384114487, 3.1858273432364483 56.38233520575055, 3.2005013505699105 56.381729001754962, 3.2152110605669244 56.381521240982636, 3.2299214979833208 56.381712417461969, 3.2445976859136914 56.382302076600183, 3.2592047256742709 56.383288816222041, 3.2737078765371903 56.384670289774114, 3.2880726351643377 56.386443211688423, 3.3022648145890794 56.388603364895339, 3.3162506225940911 56.3911456104712, 3.3299967393336507 56.394063899402283, 3.3434703940487704 56.397351286442365, 3.356639440723733 56.400999946037167, 3.3694724325327354 56.405001190284892, 3.3819386949256693 56.409345488897657, 3.3940083972024166 56.414022491124939, 3.40565262242552 56.419021049595841, 3.41684343552179 56.4243292460328, 3.4275539494241882 56.42993441878555, 3.4377583891063819 56.435823192130307, 3.4474321533635863 56.44198150727437, 3.45655187419488 56.448394655003383, 3.4650954736439457 56.455047309904074, 3.4730422179573286 56.461923566091308, 3.48037276892179 56.469006974364937, 3.487069232245207 56.476280580718061, 3.4931152028487005 56.48372696611451, 3.4984958069414134 56.491328287449981, 3.5031977407535515 56.499066319608126, 3.5072093058079021 56.506922498518954, 3.5105204406152741 56.514877965124413, 3.5131227486849848 56.522913610152557, 3.5150095227477567 56.531010119599046, 3.5161757650952268 56.539148020812071, 3.5166182039476088 56.547307729074056, 3.5163353057690534 56.55546959457169, 3.5153272834587197 56.563613949643262, 3.5135961003547331 56.571721156190812, 3.5111454699977958 56.579771653142835, 3.5079808516115003 56.587746003852168, 3.5041094412670568 56.595624943312416, 3.4995401587114432 56.603389425075889, 3.4942836298496731 56.611020667755305, 3.4883521648840192 56.618500200991377, 3.4817597321256026 56.62580991076922, 3.4745219275066122 56.632932083966296, 3.4666559398346579 56.639849452015987, 3.4581805118441489 56.646545233572482, 3.449115897113225 56.653003176063407, 3.43948381292849 56.659207596019776, 3.4293073891935575 56.665143418074329))', 4326)

    set @LINESTRING = geography::STGeomFromText('LINESTRING (5.5973166666666669 58.92883333333333, 5.5860166666666666 58.92625, 5.5431166666666662 58.9217, 5.523533333333333 58.892616666666669, 5.504083333333333 58.863266666666668, 5.4654666666666669 58.8392, 5.4279666666666664 58.8154, 5.3912333333333331 58.7925, 5.3546166666666668 58.76958333333333, 5.3203166666666668 58.7458, 5.2917 58.719883333333335, 5.2632833333333338 58.69251666666667, 5.2357666666666667 58.664883333333336, 5.2087166666666667 58.63805, 5.1827833333333331 58.611083333333333, 5.1574833333333334 58.5834, 5.132366666666667 58.55595, 5.1058166666666667 58.528466666666667, 5.0795 58.501233333333332, 5.05285 58.473883333333333, 5.0265166666666667 58.44636666666667, 4.9989333333333335 58.418983333333337, 4.9722166666666663 58.393466666666669, 4.9455166666666663 58.368366666666667, 4.9185 58.343033333333331, 4.8899 58.317283333333336, 4.8600166666666667 58.29078333333333, 4.8303333333333338 58.2635, 4.8021833333333337 58.235616666666665, 4.7775333333333334 58.210016666666668, 4.7528500000000005 58.1847, 4.7277166666666668 58.15978333333333, 4.7030166666666666 58.1354, 4.6779833333333336 58.110483333333335, 4.65235 58.08605, 4.6089833333333337 58.034816666666664, 4.5846333333333336 58.009966666666664, 4.4019 57.836283333333334, 4.3755666666666668 57.810966666666666, 4.29825 57.7348, 4.2723333333333331 57.709183333333335, 4.2463333333333333 57.683733333333336, 4.22025 57.65826666666667, 4.1941166666666669 57.632583333333336, 4.1683833333333329 57.60693333333333, 4.1428833333333337 57.58125, 4.11775 57.555183333333332, 4.0926166666666663 57.52945, 4.0676 57.503816666666665, 4.0422333333333329 57.47815, 4.01685 57.452683333333333, 3.99135 57.427433333333333, 3.9656666666666665 57.402116666666664, 3.9400833333333334 57.377, 3.9142166666666665 57.351833333333332, 3.8881666666666668 57.326866666666668, 3.8621 57.301916666666664, 3.8360666666666665 57.277166666666666, 3.810083333333333 57.252633333333335, 3.7841166666666668 57.2281, 3.75835 57.203666666666663, 3.7327 57.179283333333331, 3.7070166666666666 57.1552, 3.6814333333333336 57.131133333333331, 3.6555833333333334 57.10715, 3.6300333333333334 57.08335, 3.2284166666666665 56.548733333333331, 3.2232666666666665 56.546183333333332, 3.2177666666666669 56.54845, 3.23455 56.55916666666667, 3.2298166666666668 56.559283333333333, 3.2124666666666668 56.553233333333331, 3.2140166666666667 56.536616666666667, 3.2364666666666668 56.521033333333335, 3.2199666666666666 56.54345, 3.2206333333333332 56.54475, 3.2166166666666665 56.5337, 3.2176833333333335 56.4181, 3.2202333333333333 56.41895, 3.2347 56.402816666666666, 3.24895 56.389733333333332, 3.25415 56.3835, 3.2564166666666665 56.380833333333335, 3.2621333333333333 56.381266666666669, 3.26735 56.381183333333333, 3.2731 56.381466666666668, 3.2715166666666669 56.3849, 3.27415 56.37965, 3.2761 56.37853333333333, 3.2675666666666667 56.378066666666669, 3.2650166666666669 56.376633333333331, 3.2616166666666668 56.381483333333335, 3.2533 56.378666666666668, 3.2457833333333332 56.376116666666668, 3.24885 56.376316666666668, 3.2393666666666667 56.38133333333333, 3.2397 56.37851666666667, 3.2411666666666665 56.37545, 3.2462666666666666 56.364416666666664, 3.2253333333333334 56.545966666666665, 3.21505 56.563983333333333, 3.2134333333333331 56.553666666666665, 3.22545 56.544933333333333, 3.2181333333333333 56.54815, 3.2911 56.583016666666666, 3.2850666666666668 56.542866666666669, 3.2835166666666664 56.527483333333336, 3.283 56.519933333333334, 3.2825333333333333 56.512866666666667, 3.21895 56.5476, 3.2117166666666668 56.54485, 3.3095499999999998 56.62795, 3.32665 56.641766666666669, 3.30435 56.63388333333333, 3.2691333333333334 56.610233333333333, 3.2308 56.585183333333333, 3.2100666666666666 56.566033333333337, 3.2102333333333335 56.564666666666668, 3.18685 56.54781666666667, 3.1955666666666667 56.543533333333336, 3.2032 56.54295, 3.2304666666666666 56.527633333333334, 3.2371666666666665 56.5187, 3.2393833333333335 56.517983333333333, 3.2468166666666667 56.516166666666663, 3.2298833333333334 56.539566666666666, 3.21135 56.54848333333333, 3.2364166666666665 56.53705, 3.2376833333333335 56.557916666666664, 3.2416166666666668 56.5537, 3.2339833333333332 56.549633333333333, 3.2331 56.54885, 3.26125 56.558033333333334, 3.2715333333333332 56.560216666666669, 3.27915 56.562, 3.2829166666666669 56.565233333333332, 3.2820666666666667 56.567933333333336, 3.2633333333333332 56.5755, 3.2705666666666668 56.582233333333335, 3.2709 56.580916666666667, 3.26615 56.58005, 3.2642166666666665 56.57755, 3.2619666666666669 56.575516666666665, 3.2495 56.5654, 3.21675 56.55833333333333, 3.1614 56.53455, 3.1814666666666667 56.53425, 3.2033333333333331 56.53585, 3.2245166666666667 56.536366666666666, 3.2460833333333334 56.5385, 3.2647166666666667 56.585033333333335, 3.21665 56.544066666666666, 3.2207666666666666 56.544200000000004, 3.2149166666666669 56.556933333333333, 3.22195 56.55766666666667, 3.2241333333333335 56.55915, 3.2271 56.563966666666666, 3.22975 56.56816666666667, 3.2292666666666667 56.564966666666663, 3.22985 56.56315, 3.23005 56.5609, 3.2293833333333333 56.55898333333333, 3.2285666666666666 56.556616666666663, 3.2528333333333332 56.549233333333333, 3.2724333333333333 56.467483333333334, 3.274 56.437583333333336, 3.2553 56.378616666666666, 3.23885 56.5397, 3.2315833333333335 56.544733333333333, 3.22975 56.5462, 3.2253833333333333 56.54465, 3.2282 56.547, 3.4930833333333333 56.9171, 3.5147333333333335 56.9432, 3.5362999999999998 56.969, 3.5585166666666668 56.9954, 3.5800833333333335 57.021433333333334, 3.6016333333333335 57.04761666666667, 3.62355 57.073966666666664, 3.6454166666666667 57.100616666666667, 3.66775 57.127366666666667, 3.6893833333333332 57.154, 3.71165 57.180733333333336, 3.7349333333333332 57.20725, 3.7611499999999998 57.233066666666666, 3.7868166666666667 57.258783333333334, 3.8127666666666666 57.284683333333334, 3.8383833333333333 57.310333333333332, 3.8638666666666666 57.335783333333332, 3.88985 57.36163333333333, 3.9161333333333332 57.387383333333332, 3.94155 57.412916666666668, 3.9677 57.43845, 3.9938666666666665 57.4641, 4.0198666666666663 57.48975, 4.045633333333333 57.5153, 4.0710833333333332 57.540666666666667, 4.0964666666666663 57.5664, 4.12255 57.591966666666664, 4.1485666666666665 57.617233333333331, 4.1749666666666663 57.64306666666667, 4.2013 57.668466666666667, 4.2271666666666663 57.693483333333333, 4.2532833333333331 57.7186, 4.2793666666666663 57.743783333333333, 4.4644666666666666 57.9133, 4.4914666666666667 57.93716666666667, 4.51815 57.960966666666664, 4.5451166666666669 57.984833333333334, 4.5717166666666671 58.008766666666666, 4.5973833333333332 58.03295, 4.6212666666666671 58.05683333333333, 4.6442833333333331 58.080966666666669, 4.6689 58.105, 4.691816666666667 58.129783333333336, 4.71415 58.15465, 4.734633333333333 58.179033333333336, 4.7527333333333335 58.204016666666668, 4.7701166666666666 58.22975, 4.7886666666666668 58.2559, 4.8092333333333332 58.281383333333331, 4.830283333333333 58.3065, 4.8521 58.33175, 4.8741166666666667 58.356716666666664, 4.8970333333333329 58.381116666666664, 4.921 58.4052, 4.9451666666666672 58.42985, 4.9706166666666665 58.454283333333336, 4.9948 58.478183333333334, 5.015366666666667 58.5027, 5.03575 58.5289, 5.0606333333333335 58.555016666666667, 5.0892833333333334 58.580216666666665, 5.1181166666666664 58.605766666666668, 5.1484333333333332 58.6312, 5.1807333333333334 58.656033333333333, 5.2141833333333336 58.68035, 5.2447333333333335 58.701866666666668, 5.2617666666666665 58.715866666666663, 5.2897333333333334 58.734583333333333, 5.3170833333333336 58.7534, 5.3563833333333335 58.77706666666667, 5.4008666666666665 58.803666666666665, 5.45125 58.82865, 5.4961 58.849683333333331, 5.5255166666666664 58.876066666666667, 5.5421333333333331 58.90725, 5.5577333333333332 58.9275, 5.5837 58.926016666666669, 5.592483333333333 58.927266666666668, 5.5940833333333337 58.927633333333333, 5.5972333333333335 58.92881666666667)', 4326)

    select @POLYGON.STIntersection(@LineString) as Intersection,

    @POLYGON.STIntersection(@LineString).STAsText() as IntersectionText,

    @POLYGON.STIntersection(@LineString).STNumGeometries() as GeometryCount

  • I didn't think you could do STIntersection with geography data types. I changed your sample to all geometry data types and it dropped to 17 line segments that could be the correct output.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

  • Thanks for the input.

    The correct count should be 5. You can do STIntersection with geography. I use it often and with alot of success. A geography is essentially a geometry with a projection(SRID) applied. My issue is that the linestrings returned represent everywhere a linestring crosses itself and also intersects the intersecting polygon. I want to keep by linestrings as they are even though they cross over each other. I just want them cut where they intersect the polygon.

  • Interesting. All of my work with spatial has been web20/mobile stuff, which is predominantly "I am here, who/what is near me" type of stuff. Points and distances.

    It doesn't seem to want to do the right thing does it. Short of breaking the strings at each end point and iterating over them I am at a loss. Have you tried the MSDN forms or even better file a Connect item or contact MS directly?

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

  • I thought I would have a go at this with a simplified example. I don't seem to get the behaviour that you are getting? What version of SQL are you using. I've tried it on 2012 RTM

    declare @p geometry = Geometry::STGeomFromText('LINESTRING (0 5, 10 5, 10 10, 5 10, 5 0, 10 0, 0 9)',0)

    declare @l geometry = Geometry::STGeomFromText('POLYGON ((4 4, 6 4, 6 6, 4 6, 4 4))',0)

    select @l union all select @p

    select @l.STIntersection(@p) as Intersection, @l.STIntersection(@p).MakeValid().ToString()

    select @p.STIntersection(@l) as Intersection, @p.STIntersection(@l).MakeValid().ToString()

    GO

    declare @p geography = geography::STGeomFromText('LINESTRING (0 5, 10 5, 10 10, 5 10, 5 0, 10 0, 0 9)',4326)

    declare @l geography = geography::STGeomFromText('POLYGON ((4 4, 6 4, 6 6, 4 6, 4 4))',4326)

    select @l union all select @p

    select @l.STIntersection(@p) as Intersection, @l.STIntersection(@p).MakeValid().ToString()

    select @p.STIntersection(@l) as Intersection, @p.STIntersection(@l).MakeValid().ToString()

    GO

    It could also be the complexity of you line string?

  • I have the EXACT same issue.  Did you come up with a solution?

    Thanks
    Maurice

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply