SQL Server 2008 Spatial - Converting from Ink Strokes to geometries

The following snippet will convert an strok collected using an Ink Canvas in WPF to a LINSTRING geometry.

The first uses the Well Known Text format WKT.

//Text representation of a line is LINESTRING (x1 y1,x2 y2 x3 y3....)

//create an string array to store the points

string[] pnts = new string[e.Stroke.StylusPoints.Count];

 

for (int i = 0; i < e.Stroke.StylusPoints.Count; i++)

{

  StylusPoint p = e.Stroke.StylusPoints[i];

  pnts[i] = string.Format("{0} {1}", p.X, p.Y);

}

 

string geomText = String.Format("LINESTRING({0})", String.Join(",", pnts));

 

SqlGeometry strk = SqlGeometry.Parse(geomText);

And this uses the Well Known Binary format WKB

MemoryStream ms = new MemoryStream ();

BinaryWriter bw = new BinaryWriter(ms);

 

//we need to write some geometry information before the points.

//the byte order, the geometry type and the number of points.

bw.Write((Byte)1);

bw.Write((UInt32)2);

bw.Write((UInt32)e.Stroke.StylusPoints.Count);

 

foreach (StylusPoint p in e.Stroke.StylusPoints)

}

   bw.Write((double)p.X);

   bw.Write((double)p.Y);

}

           

SqlGeometry strk = SqlGeometry.STGeomFromWKB (new SqlBytes(ms),0);

bw.Close();

ms.Close();



-
Published 04 January 2008 17:34 by simonsabin

Comments

No Comments