Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample code code to illustrate how to call MakeValid
/*
SQL code for SQL Server from
https://stackoverflow.com/questions/66496778/sql-statement-including-makevalid-crushing-the-server
*/
/*
C# version using SqlGeography instead of SqlGeometry
Requires the Microsoft.SqlServer.Types assembly
Install-Package Microsoft.SqlServer.Types -Version 14.0.1016.290
*/
void MakeValidCall()
{
// Sample set of lines that were generated from GPS data
var wkt = "MULTILINESTRING ((-78.063088844296 39.0923128276227, -78.0629933704493 39.0924209049056, -78.0628537704575 39.0925804994165, -78.0626985415764 39.0927655959674, -78.0625776031863 39.0929093443343, -78.0624772451994 39.0930271271681, -78.0624404735616 39.093069429965, -78.06242230244 39.0931060149791, -78.062417726422 39.0931460260255, -78.0624230534316 39.0931855915373, -78.0624414338606 39.0932160425133, -78.0624617321928 39.0932363736011, -78.0624857549401 39.0932547887746, -78.0625955308645 39.0933174085555, -78.0628091268828 39.0934413211804, -78.0623615448858 39.0939008841112, -78.0618945628718 39.0943812788732, -78.0621603492116 39.094522628407, -78.0625166974101 39.0947133754383, -78.0627689030194 39.0948489021124, -78.0630246488892 39.0949866567258, -78.0632038915344 39.0950820335129, -78.0632954936423 39.095126900709, -78.0633505648608 39.0951567951925, -78.0635673364237 39.0952755726356, -78.063682189626 39.0953377527927), (-78.0636821896416 39.0953377528011, -78.063757905942 39.0953787442108, -78.0639711453292 39.0954941852792, -78.0641092846603 39.0955700222676, -78.0644398539051 39.0957485791264, -78.064783336432 39.0959347898128, -78.06514128453 39.096126566234, -78.0651655623989 39.0961381922841, -78.0654659698097 39.0958486496622, -78.0653479527409 39.0956841997381, -78.0648892954147 39.0954718713677, -78.0645701123162 39.0955967701995, -78.0644398539051 39.0957485791264, -78.0641092846603 39.0955700222676, -78.0639711453292 39.0954941852792, -78.063757905942 39.0953787442108, -78.0635673364393 39.095275572644, -78.0633505648764 39.0951567952009, -78.0632954936579 39.0951269007174, -78.06320389155 39.0950820335213, -78.0630246489048 39.0949866567342, -78.062768903035 39.0948489021208, -78.0625166974257 39.0947133754467, -78.0621603492272 39.0945226284154, -78.0618945628874 39.0943812788816, -78.0617054459606 39.0942782571458, -78.0615781367185 39.094207817123, -78.0613182406321 39.0940655626988, -78.0611270926144 39.0939594435658, -78.0609317701255 39.0938510242263, -78.0607352124531 39.0937393369615, -78.0605369866093 39.0936266643798, -78.0603537707155 39.0935248163208, -78.0602511013634 39.0934677492167), (-78.0602511013634 39.0934677492167, -78.0600742383553 39.0933694413094, -78.0604932584179 39.0928866824054, -78.0608162054575 39.0930709352344, -78.0610014102213 39.0931742989685, -78.0612692959238 39.093305320502, -78.061566316686 39.0934705247655, -78.0621024968246 39.0937608119971, -78.0622390120017 39.0938294697379, -78.0623615449014 39.0939008841196, -78.0618945628874 39.0943812788816, -78.0617054459606 39.0942782571458, -78.0615781367185 39.094207817123, -78.0613182406321 39.0940655626988, -78.0611270926144 39.0939594435658, -78.0609317701255 39.0938510242263, -78.0607352124531 39.0937393369615, -78.0605369866093 39.0936266643798, -78.0603537707155 39.0935248163208, -78.0603424456547 39.0935185213164), (-78.060342445643 39.0935185213101, -78.0600742383436 39.0933694413031, -78.0604932584062 39.0928866823991, -78.0608162054458 39.0930709352281, -78.0610014102096 39.0931742989622, -78.0612692959121 39.0933053204957, -78.0615663166743 39.0934705247592, -78.0621024968129 39.0937608119908, -78.06223901199 39.0938294697316, -78.0623615448897 39.0939008841133, -78.0618945628757 39.0943812788753, -78.0621603492155 39.0945226284091, -78.062516697414 39.0947133754404, -78.0627689030233 39.0948489021145, -78.0630246488931 39.0949866567279, -78.0632038915383 39.095082033515, -78.0632954936462 39.0951269007111, -78.0633505648647 39.0951567951946, -78.0635673364276 39.0952755726377, -78.0637579059303 39.0953787442045, -78.0639711453175 39.0954941852729, -78.0641092846486 39.0955700222613, -78.0644398538934 39.0957485791201, -78.0641672852732 39.0960441025072, -78.0636611852219 39.0965910448198), (-78.063661185218 39.0965910448177, -78.0636370870122 39.0966170875257, -78.0630339329776 39.0962933813165, -78.0621979068753 39.0958332797234, -78.0619819914069 39.0960652526533, -78.0619241515807 39.0961326818238, -78.0618918077389 39.0961804325525, -78.0618661401763 39.0962282083471, -78.0618577957256 39.0962822921137, -78.0618508382737 39.0963280558047, -78.0618559182467 39.0963707428539, -78.0618703344103 39.0964155450904, -78.0618861115217 39.0964561900775, -78.0619152375981 39.0964979254134, -78.0619444328449 39.0965282139131, -78.0619829777571 39.096558536561, -78.062052145339 39.0966029963687, -78.0634884796131 39.0973721601295, -78.0637108737313 39.0971400255744, -78.0637686336075 39.0970732038992, -78.0637898392381 39.0970442332728, -78.0638072674529 39.0970078001921, -78.0638219225684 39.0969557157526, -78.0638298699638 39.0969065851623, -78.0638283122851 39.0968484820183, -78.0638161769439 39.0968007672974, -78.0637944736466 39.0967545061364, -78.063758437729 39.0967074480832, -78.0637194975581 39.0966663375332, -78.0636871267668 39.0966453618572, -78.0636370870122 39.0966170875257, -78.0636920082121 39.0965577341202), (-78.0636920082121 39.0965577341202, -78.0638980790433 39.0963350337855), (-78.0638980790446 39.0963350337862, -78.0641672852706 39.0960441025058, -78.0644398538908 39.0957485791187, -78.0647833364177 39.0959347898051, -78.0651412845157 39.0961265662263, -78.0651655623846 39.0961381922764, -78.0654659697954 39.0958486496545, -78.0653479527266 39.0956841997304, -78.0648892954004 39.09547187136, -78.0645701123019 39.0955967701918, -78.0644398538908 39.0957485791187, -78.0641672852706 39.0960441025058, -78.0636370870135 39.0966170875264, -78.0636871267681 39.0966453618579, -78.0637194975594 39.0966663375339, -78.0637584377303 39.0967074480839, -78.0637944736479 39.0967545061371, -78.0638161769452 39.0968007672981, -78.0638283122864 39.096848482019, -78.0638298699651 39.096906585163, -78.0638219225697 39.0969557157533, -78.0638072674542 39.0970078001928, -78.0637898392394 39.0970442332735, -78.0637686336088 39.0970732038999, -78.0637108737326 39.0971400255751, -78.0634884796144 39.0973721601302, -78.0634166790704 39.0973337110302), (-78.0634166790938 39.0973337110428, -78.0624792296026 39.0968317033473), (-78.0624792296026 39.0968317033473, -78.0621042503451 39.0966308991288), (-78.0621042503425 39.0966308991274, -78.0620521453611 39.0966029963806, -78.0619829777792 39.0965585365729, -78.061944432867 39.096528213925, -78.0619152376202 39.0964979254253, -78.0618861115438 39.0964561900894, -78.0618703344324 39.0964155451023, -78.0618559182688 39.0963707428658, -78.0618508382958 39.0963280558166, -78.0618577957477 39.0962822921256, -78.0618661401984 39.096228208359, -78.061891807761 39.0961804325644, -78.0619241516028 39.0961326818357, -78.061981991429 39.0960652526652, -78.0621979068974 39.0958332797353, -78.0630339329997 39.0962933813284, -78.0636370870343 39.0966170875376, -78.0636871267889 39.0966453618691, -78.0637194975802 39.0966663375451, -78.0637584377511 39.0967074480951, -78.0637944736687 39.0967545061483, -78.063816176966 39.0968007673093, -78.0638283123072 39.0968484820302, -78.0638298699859 39.0969065851742, -78.0638219225905 39.0969557157644, -78.063807267475 39.097007800204, -78.0637898392602 39.0970442332847, -78.0637686336296 39.0970732039111, -78.0637108737534 39.0971400255863, -78.0634884796352 39.0973721601414, -78.0622917404204 39.0967313009596), (-78.0622917404204 39.0967313009596, -78.0620521453611 39.0966029963806, -78.0619829777792 39.0965585365729, -78.061944432867 39.096528213925, -78.0619152376202 39.0964979254253, -78.0618861115438 39.0964561900894, -78.0618703344324 39.0964155451023, -78.0618559182688 39.0963707428658, -78.0618508382958 39.0963280558166, -78.0618577957477 39.0962822921256, -78.0618661401984 39.096228208359, -78.061891807761 39.0961804325644, -78.0619241516028 39.0961326818357, -78.061981991429 39.0960652526652, -78.0621979068974 39.0958332797353, -78.0630339329997 39.0962933813284, -78.0636370870343 39.0966170875376, -78.0641672852914 39.096044102517, -78.0644398539116 39.0957485791299, -78.0641092846668 39.0955700222711, -78.0639711453357 39.0954941852827, -78.0637579059485 39.0953787442143, -78.0635673364458 39.0952755726475, -78.0633505648829 39.0951567952044, -78.0632954936644 39.0951269007209, -78.0632038915565 39.0950820335248, -78.0630246489113 39.0949866567377, -78.0627689030415 39.0948489021243, -78.0625166974322 39.0947133754502, -78.0623137733994 39.0946047536911), (-78.0623137733851 39.0946047536834, -78.0621603492194 39.0945226284112, -78.0618945628796 39.0943812788774, -78.061888890917 39.0943781889316), (-78.061888890917 39.0943781889316, -78.0617054459528 39.0942782571416, -78.0615781367107 39.0942078171189, -78.0613182406243 39.0940655626946, -78.0611270926066 39.0939594435616, -78.0609317701177 39.0938510242221, -78.0607352124453 39.0937393369573, -78.0605369866015 39.0936266643756, -78.0603537707077 39.0935248163166, -78.0600742383475 39.0933694413052, -78.0604932584101 39.0928866824012, -78.0608162054497 39.0930709352302, -78.0610014102135 39.0931742989643, -78.061269295916 39.0933053204978, -78.0615663166782 39.0934705247613, -78.0621024968168 39.0937608119929, -78.0622390119939 39.0938294697337, -78.0623615448936 39.0939008841154, -78.0618945628796 39.0943812788774, -78.0621603492194 39.0945226284112, -78.0625166974179 39.0947133754425, -78.0627689030272 39.0948489021166, -78.063024648897 39.09498665673, -78.0632038915422 39.0950820335171, -78.0632954936501 39.0951269007132, -78.0633505648686 39.0951567951967, -78.0635673364315 39.0952755726398, -78.0637579059342 39.0953787442066, -78.0639711453214 39.095494185275, -78.0641092846525 39.0955700222634, -78.0644398538973 39.0957485791222, -78.0641672852771 39.0960441025093, -78.06363708702 39.0966170875299, -78.0630339329854 39.0962933813207, -78.0621979068831 39.0958332797276, -78.0621805909577 39.0958241843748), (-78.062180590959 39.0958241843755, -78.0616206090586 39.0955300516351, -78.0621979068844 39.0958332797283, -78.061981991416 39.0960652526582, -78.0619241515898 39.0961326818287, -78.061891807748 39.0961804325574, -78.0618917260013 39.0961805838438), (-78.0618917260117 39.0961805838494, -78.0618661401958 39.0962282083576, -78.0618577957451 39.0962822921242, -78.0618508382932 39.0963280558152, -78.0618559182662 39.0963707428644, -78.0618703344298 39.0964155451009, -78.0618861115412 39.096456190088, -78.0619152376176 39.0964979254239, -78.0619444328644 39.0965282139236, -78.0619829777766 39.0965585365715, -78.0620521453585 39.0966029963792, -78.0634884796326 39.09737216014, -78.0637108737508 39.0971400255849, -78.063768633627 39.0970732039097, -78.0637898392576 39.0970442332833, -78.0638072674724 39.0970078002026, -78.0638219225879 39.096955715763, -78.0638298699833 39.0969065851728, -78.0638283123046 39.0968484820288, -78.0638161769634 39.0968007673079, -78.0637944736661 39.0967545061469, -78.0637584377485 39.0967074480937, -78.0637194975776 39.0966663375437, -78.0636871267863 39.0966453618677, -78.0636370870317 39.0966170875362, -78.0630339329971 39.096293381327, -78.0621979068948 39.0958332797339, -78.0621805900711 39.0958241843811), (-78.0621805900711 39.0958241843811, -78.061620609069 39.0955300516407, -78.0621979068948 39.0958332797339, -78.0630339329971 39.096293381327, -78.0636370870317 39.0966170875362, -78.0641672852888 39.0960441025156, -78.064439853909 39.0957485791285, -78.0641092846642 39.0955700222697, -78.0639711453331 39.0954941852813, -78.0637579059459 39.0953787442129, -78.0635673364432 39.0952755726461, -78.0633505648803 39.095156795203, -78.0632954936618 39.0951269007195, -78.0632038915539 39.0950820335234, -78.0630246489087 39.0949866567363, -78.0627689030389 39.0948489021229, -78.0625166974296 39.0947133754488, -78.0621603492311 39.0945226284175, -78.0618945628913 39.0943812788837, -78.0617054459645 39.0942782571479, -78.061321075719 39.094762278193, -78.06117171643 39.0949581928028, -78.0611668089336 39.0949645908619), (-78.0611668089323 39.0949645908612, -78.0609227959584 39.0952826908637, -78.0608782619782 39.0953392384941, -78.0608407240774 39.0953850090718, -78.060819789738 39.0954078863319, -78.0607980307452 39.0954240093091, -78.0607754336243 39.0954354026171, -78.0607554685671 39.095441404635, -78.0607268446489 39.0954466990153, -78.0606973861958 39.0954465895581, -78.0606688080916 39.0954444568864, -78.060639408029 39.0954348950704, -78.060603123278 39.0954178811002, -78.0599165840255 39.095049383042, -78.0595054754256 39.0948371970775, -78.0594492552619 39.0948081321096), (-78.0594492552541 39.0948081321054, -78.0590503242162 39.0946018903349, -78.0588067801629 39.0944747238768, -78.0586265790152 39.0943747998927, -78.058662242132 39.0943616292758, -78.0587202032307 39.0943388925086, -78.0588941314428 39.0942634564754, -78.0589635541463 39.0942316659499, -78.0590346342414 39.0941972246871, -78.0591063817849 39.0941612467963, -78.0591711000112 39.0941266109824, -78.0592624775402 39.0940737805097, -78.059423526606 39.0939658145222, -78.0595277491453 39.0938935412623, -78.0595912196117 39.0938476156364, -78.0596474577418 39.0938004664022, -78.059706574074 39.0937490537822, -78.0597932695837 39.0936726110627, -78.0598449245092 39.0936215120883, -78.0598954843883 39.0935698964531, -78.0599705548 39.093492897175, -78.0600742383501 39.0933694413066, -78.0603537707103 39.093524816318, -78.0605369866041 39.093626664377, -78.0607352124479 39.0937393369587, -78.0609317701203 39.0938510242235, -78.0611270926092 39.093959443563, -78.0613182406269 39.094065562696, -78.0615781367133 39.0942078171202, -78.0617054459554 39.094278257143, -78.0618945628822 39.0943812788788, -78.062160349222 39.0945226284126, -78.0625166974205 39.0947133754439, -78.0627689030298 39.094848902118, -78.0630246488996 39.0949866567314, -78.0632038915448 39.0950820335185, -78.0632954936527 39.0951269007146, -78.0636763667567 39.0947529812103, -78.063979456537 39.0946417743417, -78.0645934164061 39.0949256008234))";
// Using SqlGeometry instead of SqlGeography will cause MakeValid() to fail
var g =
Microsoft.SqlServer.Types.SqlGeography.STGeomFromText(
new System.Data.SqlTypes.SqlChars(wkt), 4326);
// Next line will return false
Console.WriteLine($"Before MakeValid(), IsValid: {g.STIsValid()}");
// The equivalent SqlGeometry call with SQL server would crash the SQL Server connection after 3 minutes
// The instance "f" will be valid
var f = g.MakeValid();
// Next line will return true
Console.WriteLine($"After MakeValid(), IsValid: {f.STIsValid()}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment