Skip to content

Commit

Permalink
Fixed pretty serious serialization bug and issues reported in #13
Browse files Browse the repository at this point in the history
  • Loading branch information
dotMorten committed Oct 29, 2018
1 parent c32874d commit 644b7b3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/Microsoft.SqlServer.Types/ShapeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,18 +368,18 @@ public void Write(BinaryWriter bw)
bw.Write(NumPoints); // Number of Points = 0 (no points)
foreach (var p in _vertices)
{
bw.Write(X); //X
bw.Write(Y); //Y
bw.Write(p.X); //X
bw.Write(p.Y); //Y
}
if (_zValues != null)
foreach (var p in _zValues)
foreach (var z in _zValues)
{
bw.Write(Z);
bw.Write(z);
}
if (_mValues != null)
foreach (var p in _mValues)
foreach (var m in _mValues)
{
bw.Write(Z);
bw.Write(m);
}
if (!props.HasFlag(SerializationProps.IsSingleLineSegment) &&
!props.HasFlag(SerializationProps.IsSinglePoint))
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.SqlServer.Types/SqlGeography.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public static SqlGeography STGeomFromText(SqlChars geometryTaggedText, int srid)
{
if (geometryTaggedText.IsNull)
return SqlGeography.Null;
var data = Wkt.WktReader.Parse(geometryTaggedText.ToString());
var data = Wkt.WktReader.Parse(geometryTaggedText.ToString(), Wkt.WktReader.CoordinateOrder.LatLong);
return new SqlGeography(data, srid);
}

Expand All @@ -433,7 +433,7 @@ public static SqlGeography Parse(SqlString s)
{
if (s.IsNull)
return SqlGeography.Null;
var data = Wkt.WktReader.Parse(s.ToString());
var data = Wkt.WktReader.Parse(s.ToString(), Wkt.WktReader.CoordinateOrder.LatLong);
return new SqlGeography(data, 4326);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.SqlServer.Types/SqlGeometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public static SqlGeometry STGeomFromText(SqlChars geometryTaggedText, int srid)
{
if (geometryTaggedText.IsNull)
return SqlGeometry.Null;
var data = Wkt.WktReader.Parse(geometryTaggedText.ToString());
var data = Wkt.WktReader.Parse(geometryTaggedText.ToString(), Wkt.WktReader.CoordinateOrder.XY);
return new SqlGeometry(data, srid);
}

Expand All @@ -432,7 +432,7 @@ public static SqlGeometry Parse(SqlString s)
{
if (s.IsNull)
return SqlGeometry.Null;
var data = Wkt.WktReader.Parse(s.ToString());
var data = Wkt.WktReader.Parse(s.ToString(), Wkt.WktReader.CoordinateOrder.XY);
return new SqlGeometry(data, 0);
}

Expand Down
17 changes: 15 additions & 2 deletions src/Microsoft.SqlServer.Types/Wkt/WktReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ internal class WktReader
List<Figure> _figures;
List<Segment> _segments;
List<Shape> _shapes;
CoordinateOrder _order;

public enum CoordinateOrder
{
XY,
LatLong
}

private WktReader(string str)
{
Expand All @@ -40,9 +47,10 @@ private WktReader(string str)
Current = wkt[0];
}

public static ShapeData Parse(string str)
public static ShapeData Parse(string str, CoordinateOrder order)
{
WktReader reader = new WktReader(str);
reader._order = order;
return reader.ReadShape();
}

Expand Down Expand Up @@ -200,7 +208,12 @@ private void ReadCoordinateCollection()

private void ReadCoordinate()
{
_vertices.Add(new Point(ReadDouble(), ReadDouble()));
var x = ReadDouble();
var y = ReadDouble();
if(_order == CoordinateOrder.XY)
_vertices.Add(new Point(x, y));
else
_vertices.Add(new Point(y, x));
hasZ = ReadOptionalDouble(out double z) || hasZ;
_z.Add(z);
hasM = ReadOptionalDouble(out double m) || hasM;
Expand Down

0 comments on commit 644b7b3

Please sign in to comment.