Home > Software design >  When writing a SQL Server geography value, the shell of a polygon must be oriented counter-clockwise
When writing a SQL Server geography value, the shell of a polygon must be oriented counter-clockwise

Time:03-24

Using Net Topology suite I am creating geometries and attempting to insert them into the database.

I'm getting this error on some of the Polygons/Multipolygons when attempting to write them to the database:

"When writing a SQL Server geography value, the shell of a polygon must be oriented counter-clockwise."

Looking online, I found the following solution, this is reversing some of the Polygon's coordinates but I'm still getting the error:

var geoFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);
Polygon polygon = geoFactory.CreatePolygon(coordinateArray);

if (!polygon.Shell.IsCCW)
   polygon = (Polygon)polygon.Reverse();

For MultiPolygons, I am then converting the array of Polygons to MultiPolygons using the geoFactory.CreateMultiPolygon(GetPolygonArray(geoItems)) function.

I am then trying to insert them into the database as Geometry types using Entity Framework Core.

EDIT:

I've managed to locate the MULTIPOLYGON that is causing the issue:

MULTIPOLYGON (((-5.477231502532959 50.21693420410156, -5.477231979370117 50.21693420410156, -5.477231502532959 50.21693420410156, -5.477231502532959 50.21693420410156)), ((-5.476067543029785 50.216758728027344, -5.476065635681152 50.216773986816406, -5.476064682006836 50.21678161621094, -5.476067066192627 50.21684646606445, -5.476050853729248 50.21684646606445, -5.476036071777344 50.216854095458984, -5.476024150848389 50.21685791015625, -5.476018905639648 50.21685791015625, -5.476008892059326 50.21686553955078, -5.476000785827637 50.21687316894531, -5.4759931564331055 50.216880798339844, -5.475985527038574 50.21688461303711, -5.475971221923828 50.21689224243164, -5.475956439971924 50.21689987182617, -5.475951194763184 50.21689987182617, -5.47593879699707 50.21690368652344, -5.475925445556641 50.21690368652344, -5.475910663604736 50.2169075012207, -5.475896835327148 50.21690368652344, -5.475858688354492 50.21690368652344, -5.475827217102051 50.21690368652344, -5.475808620452881 50.21691131591797, -5.475790023803711 50.216915130615234, -5.475773811340332 50.2169189453125, -5.475744247436523 50.216922760009766, -5.475719928741455 50.216922760009766, -5.475719451904297 50.216922760009766, -5.475703716278076 50.216922760009766, -5.475687026977539 50.2169189453125, -5.475674629211426 50.2169189453125, -5.47564697265625 50.216915130615234, -5.475618362426758 50.216915130615234, -5.4755353927612305 50.2169189453125, -5.475491523742676 50.216915130615234, -5.475334644317627 50.216949462890625, -5.475363254547119 50.21697235107422, -5.475406169891357 50.217018127441406, -5.475433349609375 50.21705627441406, -5.475474834442139 50.217105865478516, -5.475507736206055 50.21717071533203, -5.475545406341553 50.217227935791016, -5.475546836853027 50.21723175048828, -5.4755659103393555 50.21725845336914, -5.475589275360107 50.21729278564453, -5.475616455078125 50.21735763549805, -5.475639820098877 50.2174072265625, -5.4756669998168945 50.217472076416016, -5.475675106048584 50.217506408691406, -5.475677490234375 50.2175407409668, -5.475678443908691 50.21760940551758, -5.475724697113037 50.21763229370117, -5.475792407989502 50.21767044067383, -5.47599458694458 50.217777252197266, -5.47599458694458 50.2177734375, -5.475991249084473 50.21776580810547, -5.4759745597839355 50.217750549316406, -5.475966930389404 50.217742919921875, -5.475960731506348 50.21773147583008, -5.475956916809082 50.21772003173828, -5.475956439971924 50.21771240234375, -5.475956916809082 50.21770095825195, -5.475959300994873 50.21769332885742, -5.4759626388549805 50.21768569946289, -5.475971698760986 50.217674255371094, -5.475978851318359 50.21766662597656, -5.475983619689941 50.2176628112793, -5.476004123687744 50.217655181884766, -5.476006984710693 50.217655181884766, -5.476009368896484 50.217655181884766, -5.476032733917236 50.217647552490234, -5.476041793823242 50.217647552490234, -5.4760637283325195 50.21764373779297, -5.476070404052734 50.2176399230957, -5.476140022277832 50.21763610839844, -5.47618293762207 50.217628479003906, -5.476196765899658 50.21762466430664, -5.476210594177246 50.217620849609375, -5.476224422454834 50.21761703491211, -5.476232528686523 50.217613220214844, -5.476273059844971 50.21760177612305, -5.4764180183410645 50.217525482177734, -5.476437568664551 50.217529296875, -5.476502418518066 50.21754455566406, -5.476541519165039 50.21754837036133, -5.476622581481934 50.217559814453125, -5.476663589477539 50.21754837036133, -5.476677894592285 50.21754455566406, -5.47672700881958 50.217533111572266, -5.476751327514648 50.217525482177734, -5.476791858673096 50.21751403808594, -5.4767985343933105 50.217498779296875, -5.476830959320068 50.2174186706543, -5.476834297180176 50.2174072265625, -5.4768500328063965 50.21733474731445, -5.476853370666504 50.217315673828125, -5.476869106292725 50.21722412109375, -5.476870059967041 50.217220306396484, -5.476863861083984 50.21720504760742, -5.476816177368164 50.21706008911133, -5.4768385887146 50.21705627441406, -5.476830959320068 50.21703338623047, -5.4768571853637695 50.21702575683594, -5.476879596710205 50.21702194213867, -5.476896286010742 50.21699905395508, -5.476908206939697 50.21699905395508, -5.476908206939697 50.21699523925781, -5.476908206939697 50.21699523925781, -5.476906776428223 50.21693420410156, -5.4769062995910645 50.2169303894043, -5.476919174194336 50.21692657470703, -5.476965427398682 50.216915130615234, -5.476965427398682 50.2169189453125, -5.476965427398682 50.21692657470703, -5.476965427398682 50.2169303894043, -5.476966381072998 50.21695327758789, -5.476967811584473 50.21698760986328, -5.477108001708984 50.21696472167969, -5.477083206176758 50.2169075012207, -5.477156162261963 50.216896057128906, -5.4771599769592285 50.21690368652344, -5.477160930633545 50.2169075012207, -5.477216720581055 50.21689987182617, -5.477219104766846 50.2169075012207, -5.477231502532959 50.21693420410156, -5.477252006530762 50.2169303894043, -5.477239608764648 50.21690368652344, -5.477142333984375 50.21683883666992, -5.476948261260986 50.21685028076172, -5.476930141448975 50.216861724853516, -5.476810932159424 50.216888427734375, -5.4767985343933105 50.216888427734375, -5.476660251617432 50.2169189453125, -5.4766387939453125 50.2169075012207, -5.476625442504883 50.21690368652344, -5.476612091064453 50.216896057128906, -5.476449489593506 50.216819763183594, -5.47625732421875 50.216773986816406, -5.47622013092041 50.216773986816406, -5.476205825805664 50.21677017211914, -5.476178169250488 50.21677017211914, -5.476138114929199 50.216766357421875, -5.476120948791504 50.216766357421875, -5.476109981536865 50.21676254272461, -5.476098537445068 50.21676254272461, -5.476093292236328 50.21676254272461, -5.476067543029785 50.216758728027344)))

CodePudding user response:

The first polygon of your sample is collapsed (Geometry.Area == 0). I assume that it causes the issue you are seeing. Where does it come from, can you drop it?

  • Related