sibartlett / Geo

A geospatial library for .NET
https://www.nuget.org/packages/Geo/
GNU Lesser General Public License v3.0
176 stars 39 forks source link

WkbReader does not read multipolygons properly #14

Closed ghost closed 9 years ago

ghost commented 9 years ago

According to specification WKB format, each polygon in multipolygon has its endiannes and object type (polygon), but the implementation of WkbReader seems to ignore this fact. It assumes these two fields does not exist and reads number of rings straight away, so actual polygon data is garbage (ring count, coordinates...)

http://edndoc.esri.com/arcsde/9.0/general_topics/wkb_representation.htm

WKBPolygon { byte byteOrder; uint32 wkbType; // 3 uint32 numRings; LinearRing rings[numRings]; }

wkbMultiPolygon {
byte byteOrder;
uint32 wkbType; // 6 uint32 num_wkbPolygons; WKBPolygon wkbPolygons[num_wkbPolygons]; }

I'm using data from here http://www.naturalearthdata.com/downloads/ (SQLite database)

This quick test seems to fix the issue (and I guess there could also be a problem with other multi* objects):

private MultiPolygon ReadMultiPolygon(WkbBinaryReader reader, WkbDimensions dimensions) { var pointsCount = (int)reader.ReadUInt32(); var polygons = new List(); for (var i = 0; i < pointsCount; i++) { reader.ReadBytes(1); // dump byteOrder from stream reader.ReadUInt32(); // dump wkbType from stream polygons.Add(ReadPolygon(reader, dimensions)); } return new MultiPolygon(polygons); }

Thanks!

Regards, Mladen

sibartlett commented 9 years ago

Sorry, that I took a while to fix this!

I have fixed both reading and writing WKB geometries. New release has been pushed to NuGet.

ghost commented 9 years ago

No problem man. Thanks!