Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading polygons from MS SQL cause "shell is empty but holes are not" #407

Closed
tcihak-fqa opened this issue May 6, 2020 · 8 comments
Closed

Comments

@tcihak-fqa
Copy link

Description

I have some simple polygons stored in a MS SQL (geography column) that the database says are valid.
However, NetTopology cannot load them. The exception ("shell is empty but holes are not") is thrown.

Geography value
0xE6100000022405000000DB909E2D6D6F474001000020175A444014E5081BA266474001000020175A444014E5081BA26647400100002037494440DB909E2D6D6F47400100002037494440DB909E2D6D6F474001000020175A444001000000010000000001000000FFFFFFFF0000000003

WKT
POLYGON ((40.703830718994148 46.870519354271629, 40.703830718994148 46.801822070449049, 40.571994781494148 46.801822070449049, 40.571994781494148 46.870519354271629, 40.703830718994148 46.870519354271629))

System Configuration

NetTopology Suite 3.1.3
EF Core 3.1.3
.NET Core 3.1

@kludcev
Copy link

kludcev commented May 21, 2020

Same problem here.

Geography:
0xE610000002240B0000003934D83FED413CC06F01A529002B634039C0F417DE403CC0D14A3D7E122B6340FFCAEEA178403CC0DF9C86C23A2B634043501E89F9403CC06D2EE069612B634014F8FD237E413CC0102CDEB36C2B6340E51277F245423CC0C197CF7B6F2B6340E82EC71655433CC05D4E37275D2B6340C602148ABA433CC051FCEDE2342B634032FE38A639433CC0C66A943B0E2B6340410C810DB5423CC0206D96F1022B63403934D83FED413CC06F01A529002B634001000000010000000002000000FFFFFFFF0000000006000000000000000003

WKT:
MULTIPOLYGON (((153.34376985767037 -28.257526388439349, 153.34600746127265 -28.253388879064207, 153.35092283526953 -28.251840706633626, 153.35564130578442 -28.253807611370814, 153.35701936142914 -28.255831002716221, 153.35735884232966 -28.258879808493379, 153.35512123872732 -28.263017104748457, 153.3502058647305 -28.264565114865739, 153.3454873942157 -28.2625984086173, 153.34410933857089 -28.260575145726992, 153.34376985767037 -28.257526388439349)))

geography.IsValidDetailed() :
24400: Valid

System Configuration

.NET Core 3.1
NetTopology Suite 2.0.0.
EF Core 3.1.3

@kludcev
Copy link

kludcev commented May 21, 2020

Never mind my previous message, I figured my problem out. It was caused by clockwise ring orientation.
Possible solutions in case if someone is interested:
SQL:

Update Table_name 
set Shape = Shape.ReorientObject()
where Shape.EnvelopeAngle() > 90

C#:

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

@tcihak-fqa
Copy link
Author

Thanks @kludcev. I ran the SQL query to fix the shapes.

@airbreather
Copy link
Member

airbreather commented Jul 4, 2020

@alanmachadouk
Copy link

For me the SQL @kludcev provided didn't fix the issues in my case as I had a mixture of good and bad orientations and there was no way to tell which was which (the bad records returned angle < 90). Had thousands of records, the error kept appearing. I had geography columns as well.

I finally came up with the SQL that worked for me and fixed the issue, hope it helps someone.

Please test first as this was done on a very simple polygon.

UPDATE <table>
   SET [Boundary] = CONVERT(geography, CONVERT(geometry, [Boundary].STAsText()).MakeValid().STAsText()).MakeValid()
FROM <Table>

The extra MakeValid() at the end may be over kill but hey ho

@SheldonHage
Copy link

I've been struggling with this issue ('shell is empty but holes are not') and if only (...if...only...) there was a way to map a SQL Server geography column to an EF type that could then map back to SQL Server. By mapping a Geography column to an NTS Geometry type, I am then unable to use C# to fix the geography and update. At least I don't know of a way. Is there one?

@FObermaier
Copy link
Member

You need to use a GeometryFactoryEx to create your geometries. For it you can define the expected orientation for exterior rings. The orientation of the interior rings is opposite, except when the orientation of exterior rings is not explicit.

@callynch
Copy link

I am trying to write an empty polygon to an SQL Server geography column, and none of the above worked for me. Both emptyPoly.Shell.IsCCW and emptyPoly.Reverse().Shell.IsCCW return false. Even if I use GeometryFactoryEx with an explicit ring orientation of CCW, the orientation still reports as clockwise and SQL Server will not accept it.

Can anyone tell me how I can construct an empty polygon and then write it to SQL Server?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants