# Creating shapefiles with objects of type polyline from SQL data

**URL:** <https://community.thinkgeo.com/t/creating-shapefiles-with-objects-of-type-polyline-from-sql-data/266>\
**Category:** WinForms\
**Created:** [March 2, 2009, 5:58pm UTC](https://community.thinkgeo.com/t/creating-shapefiles-with-objects-of-type-polyline-from-sql-data/266 "2009-03-02T17:58:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/s/f19dbf/32.png) [@Steve](https://community.thinkgeo.com/u/Steve)\
**Post date:** [March 2, 2009, 5:58pm UTC](https://community.thinkgeo.com/t/creating-shapefiles-with-objects-of-type-polyline-from-sql-data/266/1 "2009-03-02T17:58:52Z")

</div>

Scenario:&nbsp;I've&nbsp;got&nbsp;a&nbsp;layer&nbsp;called&nbsp;secserv&nbsp;of&nbsp;type&nbsp;ESRI(03)&nbsp;polyline.&nbsp;Behind&nbsp;this&nbsp;layer&nbsp;I&nbsp;have&nbsp;sql&nbsp;SDE&nbsp;data&nbsp;that&nbsp;is&nbsp;storing&nbsp;a&nbsp;binary&nbsp;field&nbsp;(Shape)&nbsp;along&nbsp;with&nbsp;xmin,ymin,xmax,ymax&nbsp;(bounding&nbsp;box)&nbsp;for&nbsp;each&nbsp;record.&nbsp;Because&nbsp;my&nbsp;polyline&nbsp;features&nbsp;don't&nbsp;have&nbsp;vertices&nbsp;for&nbsp;each&nbsp;contiguous&nbsp;part,&nbsp;I'd&nbsp;like&nbsp;to&nbsp;be&nbsp;able&nbsp;to&nbsp;extract&nbsp;the&nbsp;necessary&nbsp;information&nbsp;from&nbsp;the&nbsp;SHAPE&nbsp;field&nbsp;in&nbsp;order&nbsp;to&nbsp;rebuild&nbsp;the&nbsp;secserv&nbsp;layer&nbsp;when&nbsp;necessary.&nbsp;I&nbsp;don't&nbsp;even&nbsp;know&nbsp;if&nbsp;this&nbsp;is&nbsp;possible&nbsp;but&nbsp;I&nbsp;figured&nbsp;you&nbsp;guys&nbsp;would.&nbsp;What&nbsp;I&nbsp;have&nbsp;done&nbsp;is&nbsp;read&nbsp;the&nbsp;data&nbsp;from&nbsp;the&nbsp;shape&nbsp;field&nbsp;into&nbsp;a&nbsp;byte&nbsp;array&nbsp;and&nbsp;by&nbsp;that&nbsp;I've&nbsp;been&nbsp;able&nbsp;to&nbsp;follow&nbsp;the&nbsp;ESRI&nbsp;layout.&nbsp;I&nbsp;just&nbsp;don't&nbsp;know&nbsp;what&nbsp;to&nbsp;do&nbsp;with&nbsp;the&nbsp;data.

SQL&nbsp;example&nbsp;of&nbsp;data&nbsp;in&nbsp;SHAPE&nbsp;field:

0x03000000C46ECD652304244162EECB0213951A41C32F07CCB5042441C8A089952E971A41010000000200000000000000C46ECD652304244162EECB0213951A41C32F07CCB5042441C8A089952E971A4100

This&nbsp;is&nbsp;basically&nbsp;what&nbsp;you&nbsp;guys&nbsp;are&nbsp;writing&nbsp;for&nbsp;each&nbsp;record&nbsp;in&nbsp;a&nbsp;polyline&nbsp;shapefile,&nbsp;right?

If&nbsp;there&nbsp;is&nbsp;another&nbsp;way&nbsp;to&nbsp;generate&nbsp;shapefiles&nbsp;from&nbsp;Data&nbsp;without&nbsp;vertices,&nbsp;please&nbsp;let&nbsp;me&nbsp;know.

---

<div class="post-metadata">

**Author:** ![Ben](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/ben/32/25720_2.png) [@Ben](https://community.thinkgeo.com/u/Ben)\
**Post date:** [March 2, 2009, 8:54pm UTC](https://community.thinkgeo.com/t/creating-shapefiles-with-objects-of-type-polyline-from-sql-data/266/2 "2009-03-02T20:54:31Z")

</div>

&nbsp;

Steve,

Besides&nbsp;vertices,&nbsp;you&nbsp;can&nbsp;also&nbsp;get&nbsp;a&nbsp;shape&nbsp;from&nbsp;WKT&nbsp;or&nbsp;WKB.&nbsp;Here&nbsp;is&nbsp;some&nbsp;codes&nbsp;where&nbsp;you&nbsp;can&nbsp;find&nbsp;how&nbsp;to&nbsp;use&nbsp;WKT/WKB&nbsp;in&nbsp;Map&nbsp;Suite&nbsp;3.0.

I&nbsp;tried&nbsp;the&nbsp;Hex&nbsp;String&nbsp;you&nbsp;provided&nbsp;and&nbsp;seems&nbsp;that's&nbsp;not&nbsp;a&nbsp;standard&nbsp;WKB,&nbsp;instead&nbsp;it's&nbsp;following&nbsp;the&nbsp;Record&nbsp;format&nbsp;of&nbsp;a&nbsp;Shape&nbsp;File.&nbsp;We&nbsp;don't&nbsp;support&nbsp;this&nbsp;format&nbsp;but&nbsp;with&nbsp;the&nbsp;specification&nbsp;here&nbsp;([esri.com/library/whitepapers/pdfs/shapefile.pdf&nbsp;](http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf)page&nbsp;7),&nbsp;you&nbsp;can&nbsp;easily&nbsp;support&nbsp;it&nbsp;by&nbsp;yourself.

`  private void Form1_Load(object sender, EventArgs e)        {           // Get WKB            LineShape lineShape = new LineShape("LINESTRING(0 0,1 1,1 2)");            byte[] lineShapeWKB = lineShape.GetWellKnownBinary();            // Get the hex string of the lineShape's WKB            StringBuilder sb = new StringBuilder();            for (int i = 0; i < lineShapeWKB.Length; i++)            {                sb.Append(lineShapeWKB[i].ToString("X2"));            }            string lineShapeWKBHexString = sb.ToString();            // Just a test, you can see the return value lineShape2 is identical to lineShape            byte[] lineShapeWKB2 = GetBytesFromHexString(lineShapeWKBHexString);            BaseShape lineShape2 = BaseShape.CreateShapeFromWellKnownData(lineShapeWKB2);            // Try to get the shape from the WKB you provide            string userWKBHexString = "03000000C46ECD652304244162EECB0213951A41C32F07CCB5042441C8A089952E971A41010000000200000000000000C46ECD652304244162EECB0213951A41C32F07CCB5042441C8A089952E971A4100";            byte[] userWKB = GetBytesFromHexString(userWKBHexString);            // throws an exception here that the input WKB is not valid            BaseShape shape = BaseShape.CreateShapeFromWellKnownData(userWKB);  }   private byte[] GetBytesFromHexString(string hexString)        {            int length = hexString.Length;            if (length % 2 != 0)            {                throw new ArgumentException("The input hexString is not correct");            }            byte[] results = new byte[length / 2];            for (int i = 0; i < hexString.Length; )            {                string subString = hexString.Substring(i, 2);                results[i / 2] = byte.Parse(subString, System.Globalization.NumberStyles.HexNumber);                i += 2;            }            return results;        }`

Let&nbsp;me&nbsp;know&nbsp;if&nbsp;you&nbsp;have&nbsp;any&nbsp;issues.

Thanks,

Ben
