Virtual Earth & SQL Server 2008 - Part 4: Integrating SQL Server 2008 and Virtual Earth (6/6)

Johannes Kebeck's Blog
03-22-2008, 08:13 AM
<h2>Inserting data into SQL Server 2008</h2> <p>In the previous parts we have first been looking on the database side and how we can use various mechanisms to load and extract data and we even implemented a geocoder in the database. Then we moved on and investigated various ways to retrieve data from SQL Server 2008 and visualize them in Virtual Earth. In this final part we will now focus on using Virtual Earth as a simple data editor which allows us to write data back to SQL Server 2008. <p>We will implement the data editor in form of a query builder. In the user interface we will have some drawing tools which allow us to draw points, lines, polygons and circles on the map and to store them temporarily in our browser window. Each of the temporarily stored geographies will have a checkbox next to it and when we check these boxes we will dynamically create a SQL statement which automatically determines the appropriate feature-type. The SQL-statement will be displayed in a textarea and we will be able to manually edit it. <p>Once we click on the image with the SQL Server logo we will create an AJAX-call which sends the SQL-statement via HTTP-Post request to a HTTP handler and this handler will actually execute the statement. <p><a href="http://blufiles.storage.msn.com/y1pLfDzppuxasgPm72cNO2kzgN4VB-a1kD5MP7P-jn2AhFNx999zGQ14ccgnmyVtCu8gvGSFMTb4Fg?PARTNER=WRI TER">

http://blufiles.storage.msn.com/y1pLfDzppuxasgWQquQZMiRZNaAbTz5BrThUjhwyS5sFtPv9oE OsbKX3Ww9jrR2yqAh912IvTcBZto?PARTNER=WRITER
</a> <p>Let’s start with a fresh table in our SQL Server. We create a table with a column of type <strong>geography</strong>. <pre><span style="color:blue">CREATE TABLE </span>[FeatureDemo]
(
[id] [int] <span style="color:blue">IDENTITY</span>(1,1) <span style="color:blue">NOT NULL</span>,
[Geom] geography <span style="color:blue">NULL</span>,
<span style="color:blue">CONSTRAINT </span>[PK_FeatureDemo] <span style="color:blue">PRIMARY KEY CLUSTERED
</span>(
[id] <span style="color:blue">ASC
</span>)
) <span style="color:blue">ON </span>[PRIMARY]</pre><a href="http://11011.net/software/vspaste"></a>
<p>In our ASP.NET page we first add a new accordion pane. This pane first displays a notice which reminds you that drawings which shall be added as a <b>geography</b> data type to the database must be drawn counter-clockwise. It also hosts buttons to draw various types of geographies and textboxes which will display the latitude and longitude under the mouse-cursor.
<p>In the “Geometry”-section it provides 2 links: delete all drawings and reset the SQL statement in the textarea. Finally there is an empty DIV-element in which we will temporarily store our drawing objects with a checkbox next to them:<pre><span style="color:blue"><</span><span style="color:#a31515">cc1</span><span style="color:blue">:</span><span style="color:#a31515">AccordionPane </span><span style="color:red">ID</span><span style="color:blue">="paneQueryBuilder" </span><span style="color:red">runat</span><span style="color:blue">="server">
<</span><span style="color:#a31515">Header</span><span style="color:blue">></span>Query Builder<span style="color:blue"></</span><span style="color:#a31515">Header</span><span style="color:blue">>
<</span><span style="color:#a31515">Content</span><span style="color:blue">>
<</span><span style="color:#a31515">b</span><span style="color:blue">></span>Drawing Tools<span style="color:blue"></</span><span style="color:#a31515">b</span><span style="color:blue">><</span><span style="color:#a31515">br </span><span style="color:blue">/><</span><span style="color:#a31515">br </span><span style="color:blue">/>
<</span><span style="color:#a31515">i</span><span style="color:blue">></span>Please make sure, you draw the polygon counter-clockise<span style="color:blue"></</span><span style="color:#a31515">i</span><span style="color:blue">><</span><span style="color:#a31515">br </span><span style="color:blue">/><</span><span style="color:#a31515">br </span><span style="color:blue">/>
<</span><span style="color:#a31515">input </span><span style="color:red">id</span><span style="color:blue">="btnPoint" </span><span style="color:red">type</span><span style="color:blue">="button" </span><span style="color:red">value</span><span style="color:blue">="Point" </span><span style="color:red">onclick</span><span style="color:blue">="Draw('point')" </span><span style="color:red">style</span><span style="color:blue">="width: 125px;" />
<</span><span style="color:#a31515">input </span><span style="color:red">id</span><span style="color:blue">="btnPolyline" </span><span style="color:red">type</span><span style="color:blue">="button" </span><span style="color:red">value</span><span style="color:blue">="Polyline" </span><span style="color:red">onclick</span><span style="color:blue">="Draw('polyline')" </span><span style="color:red">style</span><span style="color:blue">="width: 125px;" /><</span><span style="color:#a31515">br </span><span style="color:blue">/>
<</span><span style="color:#a31515">input </span><span style="color:red">id</span><span style="color:blue">="btnPolygon" </span><span style="color:red">type</span><span style="color:blue">="button" </span><span style="color:red">value</span><span style="color:blue">="Polygon" </span><span style="color:red">onclick</span><span style="color:blue">="Draw('polygon')" </span><span style="color:red">style</span><span style="color:blue">="width: 125px;" />
<</span><span style="color:#a31515">input </span><span style="color:red">id</span><span style="color:blue">="btnCircle" </span><span style="color:red">type</span><span style="color:blue">="button" </span><span style="color:red">value</span><span style="color:blue">="Circle" </span><span style="color:red">onclick</span><span style="color:blue">="Draw('circle')" </span><span style="color:red">style</span><span style="color:blue">="width: 125px" /><</span><span style="color:#a31515">br </span><span style="color:blue">/><</span><span style="color:#a31515">br </span><span style="color:blue">/>
<</span><span style="color:#a31515">a </span><span style="color:red">style</span><span style="color:blue">="font-family:Courier New"></span>Lat:<span style="color:red">&nbsp;</span><span style="color:blue"></</span><span style="color:#a31515">a</span><span style="color:blue">><</span><span style="color:#a31515">input </span><span style="color:red">id</span><span style="color:blue">="txtLat" </span><span style="color:red">type</span><span style="color:blue">="text" </span><span style="color:red">disabled</span><span style="color:blue">="disabled" </span><span style="color:red">style</span><span style="color:blue">="width: 210px" </span><span style="color:red">class</span><span style="color:blue">="TxtBox" /><</span><span style="color:#a31515">br </span><span style="color:blue">/>
<</span><span style="color:#a31515">a </span><span style="color:red">style</span><span style="color:blue">="font-family:Courier New"></span>Lon:<span style="color:red">&nbsp;</span><span style="color:blue"></</span><span style="color:#a31515">a</span><span style="color:blue">><</span><span style="color:#a31515">input </span><span style="color:red">id</span><span style="color:blue">="txtLon" </span><span style="color:red">type</span><span style="color:blue">="text" </span><span style="color:red">disabled</span><span style="color:blue">="disabled" </span><span style="color:red">style</span><span style="color:blue">="width: 210px" </span><span style="color:red">class</span><span style="color:blue">="TxtBox" /><</span><span style="color:#a31515">br </span><span style="color:blue">/><</span><span style="color:#a31515">br </span><span style="color:blue">/>
<</span><span style="color:#a31515">b</span><span style="color:blue">></span>Geometries<span style="color:blue"></</span><span style="color:#a31515">b</span><span style="color:blue">><</span><span style="color:#a31515">br </span><span style="color:blue">/>
<</span><span style="color:#a31515">a </span><span style="color:red">href</span><span style="color:blue">='javascript:DeleteAll()'></span>Delete All<span style="color:red">&nbsp;</span><span style="color:blue"></</span><span style="color:#a31515">a</span><span style="color:blue">>
<</span><span style="color:#a31515">a </span><span style="color:red">href</span><span style="color:blue">='javascript:ResetQuery()'></span>Reset SQL-Query<span style="color:blue"></</span><span style="color:#a31515">a</span><span style="color:blue">><</span><span style="color:#a31515">br </span><span style="color:blue">/>
<</span><span style="color:#a31515">div </span><span style="color:red">id</span><span style="color:blue">="divGeom"></</span><span style="color:#a31515">div</span><span style="color:blue">>
</</span><span style="color:#a31515">Content</span><span style="color:blue">>
</</span><span style="color:#a31515">cc1</span><span style="color:blue">:</span><span style="color:#a31515">AccordionPane</span><span style="color:blue">></span></pre><a href="http://11011.net/software/vspaste"></a>
<p>We will also need some more DIV-elements for the interaction with our geographies as well as the visualization and the editing of our SQL statement. Below you find the ASP.NET sources for a textarea where the SQL statement will be displayed, a clickable image which allows us to execute the AJAX-call as well as 2 elements which display the length of polylines and polygons while we draw as well as a dialog which allows us to enter the title and description of a geography.<pre><span style="color:blue"><</span><span style="color:#a31515">textarea </span><span style="color:red">id</span><span style="color:blue">="txtSQL" </span><span style="color:red">rows</span><span style="color:blue">="2" </span><span style="color:red">style</span><span style="color:blue">="position:absolute; left:300px; bottom:5px; width:300px; height:94px" ></</span><span style="color:#a31515">textarea</span><span style="color:blue">>
<</span><span style="color:#a31515">img </span><span style="color:red">src</span><span style="color:blue">="IMG/Katmai.png" </span><span style="color:red">alt</span><span style="color:blue">="Execute SQL-Statement" </span><span style="color:red">style</span><span style="color:blue">="position:absolute; bottom:5px; right:5px; cursor:pointer;" </span><span style="color:red">onclick</span><span style="color:blue">="SqlInsert();"/>
<</span><span style="color:#a31515">div </span><span style="color:red">id</span><span style="color:blue">='divDistance' </span><span style="color:red">style</span><span style="color:blue">="position:absolute; left:10px; top:800px; visibility:hidden;" </span><span style="color:red">class</span><span style="color:blue">="TxtBox"</span>;<span style="color:blue">></</span><span style="color:#a31515">div</span><span style="color:blue">>
<</span><span style="color:#a31515">div </span><span style="color:red">id</span><span style="color:blue">='divShapeInfo' </span><span style="color:red">style</span><span style="color:blue">="position:absolute; left:10px; top:850px; visibility:hidden;" </span><span style="color:red">class</span><span style="color:blue">="TxtBox"</span>;<span style="color:blue">>
<</span><span style="color:#a31515">b</span><span style="color:blue">><</span><span style="color:#a31515">u</span><span style="color:blue">></span>Please enter the details:<span style="color:blue"></</span><span style="color:#a31515">u</span><span style="color:blue">></</span><span style="color:#a31515">b</span><span style="color:blue">><</span><span style="color:#a31515">br </span><span style="color:blue">/><</span><span style="color:#a31515">br </span><span style="color:blue">/>
<</span><span style="color:#a31515">a</span><span style="color:blue">></span>ID<span style="color:red">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>:<span style="color:red">&nbsp;</span><span style="color:blue"></</span><span style="color:#a31515">a</span><span style="color:blue">><</span><span style="color:#a31515">input </span><span style="color:red">id</span><span style="color:blue">="txtShapeID" </span><span style="color:red">type</span><span style="color:blue">="text" </span><span style="color:red">disabled</span><span style="color:blue">="disabled" </span><span style="color:red">style</span><span style="color:blue">="width: 150px" /><</span><span style="color:#a31515">br </span><span style="color:blue">/>
<</span><span style="color:#a31515">a</span><span style="color:blue">></span>Title<span style="color:red">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>:<span style="color:red">&nbsp;</span><span style="color:blue"></</span><span style="color:#a31515">a</span><span style="color:blue">><</span><span style="color:#a31515">input </span><span style="color:red">id</span><span style="color:blue">="txtShapeTitle" </span><span style="color:red">type</span><span style="color:blue">="text" </span><span style="color:red">style</span><span style="color:blue">="width: 150px" /><</span><span style="color:#a31515">br </span><span style="color:blue">/>
<</span><span style="color:#a31515">a</span><span style="color:blue">></span>Description:<span style="color:red">&nbsp;</span><span style="color:blue"></</span><span style="color:#a31515">a</span><span style="color:blue">><</span><span style="color:#a31515">input </span><span style="color:red">id</span><span style="color:blue">="txtShapeDetails" </span><span style="color:red">type</span><span style="color:blue">="text" </span><span style="color:red">style</span><span style="color:blue">="width: 150px" /><</span><span style="color:#a31515">br </span><span style="color:blue">/><</span><span style="color:#a31515">br </span><span style="color:blue">/>
<</span><span style="color:#a31515">input </span><span style="color:red">id</span><span style="color:blue">="btnShapeInfo" </span><span style="color:red">type</span><span style="color:blue">="button" </span><span style="color:red">value</span><span style="color:blue">="Set" </span><span style="color:red">onclick</span><span style="color:blue">="SetInfo()" </span><span style="color:red">style</span><span style="color:blue">="width: 260px"/><</span><span style="color:#a31515">br </span><span style="color:blue">/>
</</span><span style="color:#a31515">div</span><span style="color:blue">></span></pre>
<p>If we click a button to draw a geography we change the cursor to a crosshair and attach an onclick- and an onmousemove-event to the map. While we move the mouse we continuously update the latitude and longitude in the textboxes on the accordion pane. When we click with the left mouse-button we add a point to our temporary geography. If the geography is a polyline or polygon we will also determine the length of the line. For a circle the value in the divDistance will be the radius.
<p><a href="http://gkxu3a.blu.livefilestore.com/y1psLOdC392akruH7qetJzPF1V1ebyR5sVrEspjAMPh4Bkb91Z 5edfitN0gNu48KbeFs0-74BuaQotQK7HhTLLP9A?PARTNER=WRITER">

http://gkxu3a.blu.livefilestore.com/y1psLOdC392akqCy22_Uqtlr3G8xf_zALGeihHJeWBft4s_q45 WlR2uTaRFnM9wHfbtdVTLKdDRnMsX95EEMqmDjA?PARTNER=WR ITER
</a>
<p>To finish the drawing we will hit the right mouse-button. This will detach the map-events and also bring up the divShapeInfo so that we can enter a title and a description. The distance will automatically be added to the shape.
<p><a href="http://gkxu3a.blu.livefilestore.com/y1pYZrCEXzy6WGZmw0QomwlkcrMzsM_YTVnQ0WTB7tO2oZPc2p EOgBCSzT4jo6o0zL3uqy7aUar1DwybLqc4oCRQXMU2pMAgXZd? PARTNER=WRITER">

http://gkxu3a.blu.livefilestore.com/y1pYZrCEXzy6WEU0RdHWSRkYYBTSHd3F7hwvzCLDeVZY8Pv14o dmR96lm04hucCXk7QdICg5xQKdndC2nLsBnFgMV3EVbKP-WWU?PARTNER=WRITER
</a>
<p>When we click on “Set” in the dialogue above we will add the geography to our divGeom in the accordion pane. Next to the geography we have a checkbox and a JavaScript-function will update the textarea txtSQL with a statement appropriate for the selected geometries. For example if we have a polygon the statement will reflect that, if we have 2 polygons the statement will show a MULTIPOLYGON and if we have 2 polygons and a point the statement will show a GEOMETRYCOLLECTION.
<p><a href="http://gkxu3a.blu.livefilestore.com/y1psLOdC392akre99boSLnpFTx6Qw3tTuX7t3BQPRIu1Wp2csO Mt9oU0vM6YuMg2Vr6K_GSi9bM2JjTa1TL7a4aHw?PARTNER=WR ITER">
http://gkxu3a.blu.livefilestore.com/y1pYZrCEXzy6WEuKU8TWPHLlv8nkY74BOd2TbbjCaBD_wxtk3G C2h_sWoc0eI7WADsZ9RXt3vQt1yQQjZTBLGwnGwNXS5LS35r3? PARTNER=WRITER
</a>
<p>The drawing part on top of Virtual Earth leverages a couple of events which can be attached to the Virtual Earth Map and I published the complete code on <a href="http://www.viavirtualearth.com/Wiki/Drawing-Tool+for+VE+v5.ashx">Via Virtual Earth</a>. So I don’t want to go into detail for this part.
<p>[I just noticed that this sample has been removed so now you'll find it <a href="http://cid-42e1f70205ec8a96.skydrive.live.com/self.aspx/Public/VE-Drawing.zip">here</a>].
<p>The major difference between the code on “Via Virtual Earth” and the code in this sample application is that we create an additional checkbox with an onclick-event attached. Once we activate the checkbox we call a function to update the SQL statement in the textarea:<pre><span style="color:blue">function </span>SetInfo()
{
myCurrentShape.SetTitle(document.getElementById(<span style="color:#a31515">"txtShapeTitle"</span>).value);
myCurrentShape.SetDescription(document.getElementB yId(<span style="color:#a31515">"txtShapeDetails"</span>).value + <span style="color:#a31515">"<br><a href='javascript:Delete(\"" </span>+ myCurrentShape.GetID() + <span style="color:#a31515">"\")'>Delete</a>"</span>);

<font color="#ff0000"><strong>document.getElementById(<span style="color:#a31515">"divGeom"</span>).innerHTML = document.getElementById(<span style="color:#a31515">"divGeom"</span>).innerHTML + <br> <span style="color:#a31515">"<input id='" </span>+ myCurrentShape.GetID() + <span style="color:#a31515">"' type='checkbox' onclick='UpdateSQLQuery(" </span>+ <br> myCurrentShape.GetID() + <span style="color:#a31515">")' />" </span>+ document.getElementById(<span style="color:#a31515">"txtShapeTitle"</span>).value + <span style="color:#a31515">"<br>"</span>;
</strong></font>
myPoints = <span style="color:blue">new </span>Array();
tempPoints = <span style="color:blue">null</span>;
myDistance = 0;
tempDistance = 0;

document.getElementById(<span style="color:#a31515">"divShapeInfo"</span>).style.visibility = <span style="color:#a31515">"hidden"</span>;
document.getElementById(<span style="color:#a31515">"txtShapeTitle"</span>).value = <span style="color:#a31515">""</span>;
document.getElementById(<span style="color:#a31515">"txtShapeDetails"</span>).value = <span style="color:#a31515">""</span>;
}</pre><a href="http://11011.net/software/vspaste"></a>
<p>Updating the SQL-statement in the textarea requires first to determine the types of the VEShape-objects which have been activated.<pre><span style="color:blue">function </span>UpdateSQLQuery(id)
{
<span style="color:blue">var </span>currentShape = slDrawing.GetShapeByID(id.id);
<span style="color:blue">switch </span>(currentShape.GetType())
{
<span style="color:blue">case </span><span style="color:#a31515">"Point"</span>:
pointCount++;
<span style="color:blue">var </span>tempArray = currentShape.GetPoints();
<span style="color:blue">for</span>(<span style="color:blue">var </span>j = 0; j < (tempArray.length); j++)
{
pointArray.push(tempArray[j].Latitude + <span style="color:#a31515">" " </span>+ tempArray[j].Longitude);
}
<span style="color:blue">break</span>;
<span style="color:blue">case </span><span style="color:#a31515">"Polyline"</span>:
polylineCount++;
<span style="color:blue">var </span>tempArray = currentShape.GetPoints();
<span style="color:blue">var </span>dummy = <span style="color:#a31515">""</span>;
<span style="color:blue">for</span>(<span style="color:blue">var </span>j = 0; j < (tempArray.length); j++)
{
dummy = dummy + tempArray[j].Latitude + <span style="color:#a31515">" " </span>+ tempArray[j].Longitude + <span style="color:#a31515">", "</span>;
}
dummy = dummy.substr(0, dummy.length - 2);
polylineArray.push(dummy);
<span style="color:blue">break</span>;
<span style="color:blue">case </span><span style="color:#a31515">"Polygon"</span>:
polygonCount++;
<span style="color:blue">var </span>tempArray = currentShape.GetPoints();
<span style="color:blue">var </span>dummy = <span style="color:#a31515">""</span>;
<span style="color:blue">for</span>(<span style="color:blue">var </span>j = 0; j < (tempArray.length); j++)
{
dummy = dummy + tempArray[j].Latitude + <span style="color:#a31515">" " </span>+ tempArray[j].Longitude + <span style="color:#a31515">", "</span>;
}
dummy = dummy.substr(0, dummy.length - 2);
polygonArray.push(dummy);
<span style="color:blue">break</span>;
}</pre>
<p>Now we determine which type of geography in SQL Server 2008 matches our selection.<pre><span style="color:blue">if </span>(pointCount == 1 && polylineCount == 0 && polygonCount == 0) geomType = <span style="color:#a31515">"POINT"</span>;
<span style="color:blue">else if </span>(pointCount > 1 && polylineCount == 0 && polygonCount == 0) geomType = <span style="color:#a31515">"MULTIPOINT"</span>;
<span style="color:blue">else if </span>(pointCount == 0 && polylineCount == 1 && polygonCount == 0) geomType = <span style="color:#a31515">"LINESTRING"</span>;
<span style="color:blue">else if </span>(pointCount == 0 && polylineCount > 1 && polygonCount == 0) geomType = <span style="color:#a31515">"MULTILINESTRING"</span>;
<span style="color:blue">else if </span>(pointCount == 0 && polylineCount == 0 && polygonCount == 1) geomType = <span style="color:#a31515">"POLYGON"</span>;
<span style="color:blue">else if </span>(pointCount == 0 && polylineCount == 0 && polygonCount > 1) geomType = <span style="color:#a31515">"MULTIPOLYGON"</span>;
<span style="color:blue">else </span>geomType = <span style="color:#a31515">"GEOMETRYCOLLECTION"</span>;</pre><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a>
<p>Finally we create our SQL-statement.<pre> <span style="color:blue">switch </span>(geomType)
{
<span style="color:blue">case </span><span style="color:#a31515">"POINT"</span>:
sqlBuilder = <span style="color:#a31515">"INSERT INTO FeatureDemo (Geom) VALUES (geography::STGeomFromText('POINT (" </span>+ pointArray[0] + <span style="color:#a31515">")', 4326))"</span>;
document.getElementById(<span style="color:#a31515">"txtSQL"</span>).value = sqlBuilder;
<span style="color:blue">break</span>;
<span style="color:blue">case </span><span style="color:#a31515">"MULTIPOINT"</span>:
<span style="color:blue">var </span>dummy = <span style="color:#a31515">""</span>;
<span style="color:blue">for</span>(<span style="color:blue">var </span>j = 0; j < (pointArray.length); j++)
{
dummy = dummy + pointArray[j] + <span style="color:#a31515">", "</span>;
}
dummy = dummy.substr(0, dummy.length - 2);
sqlBuilder = <span style="color:#a31515">"INSERT INTO FeatureDemo (Geom) VALUES (geography::STGeomFromText('MULTIPOINT (" </span>+ dummy + <span style="color:#a31515">")', 4326))"</span>;
document.getElementById(<span style="color:#a31515">"txtSQL"</span>).value = sqlBuilder;
<span style="color:blue">break</span>;
<span style="color:blue">case </span><span style="color:#a31515">"LINESTRING"</span>:
sqlBuilder = <span style="color:#a31515">"INSERT INTO FeatureDemo (Geom) VALUES (geography::STGeomFromText('LINESTRING (" </span>+ polylineArray[0] + <span style="color:#a31515">")', 4326))"</span>;
document.getElementById(<span style="color:#a31515">"txtSQL"</span>).value = sqlBuilder;
<span style="color:blue">break</span>;
<span style="color:blue">case </span><span style="color:#a31515">"MULTILINESTRING"</span>:
<span style="color:blue">var </span>dummy = <span style="color:#a31515">""</span>;
<span style="color:blue">for</span>(<span style="color:blue">var </span>j = 0; j < (polylineArray.length); j++)
{
dummy = dummy + <span style="color:#a31515">"(" </span>+ polylineArray[j] + <span style="color:#a31515">"), "</span>;
}
dummy = dummy.substr(0, dummy.length - 2);
sqlBuilder = <span style="color:#a31515">"INSERT INTO FeatureDemo (Geom) VALUES (geography::STGeomFromText('MULTILINESTRING (" </span>+ dummy + <span style="color:#a31515">")', 4326))"</span>;
document.getElementById(<span style="color:#a31515">"txtSQL"</span>).value = sqlBuilder;
<span style="color:blue">break</span>;
<span style="color:blue">case </span><span style="color:#a31515">"POLYGON"</span>:
sqlBuilder = <span style="color:#a31515">"INSERT INTO FeatureDemo (Geom) VALUES (geography::STGeomFromText('POLYGON ((" </span>+ polygonArray[0] + <span style="color:#a31515">"))', 4326))"</span>;
document.getElementById(<span style="color:#a31515">"txtSQL"</span>).value = sqlBuilder;
<span style="color:blue">break</span>;
<span style="color:blue">case </span><span style="color:#a31515">"MULTIPOLYGON"</span>:
<span style="color:blue">var </span>dummy = <span style="color:#a31515">""</span>;
<span style="color:blue">for</span>(<span style="color:blue">var </span>j = 0; j < (polygonArray.length); j++)
{
dummy = dummy + <span style="color:#a31515">"((" </span>+ polygonArray[j] + <span style="color:#a31515">")), "</span>;
}
dummy = dummy.substr(0, dummy.length - 2);
sqlBuilder = <span style="color:#a31515">"INSERT INTO FeatureDemo (Geom) VALUES (geography::STGeomFromText('MULTIPOLYGON (" </span>+ dummy + <span style="color:#a31515">")', 4326))"</span>;
document.getElementById(<span style="color:#a31515">"txtSQL"</span>).value = sqlBuilder;
<span style="color:blue">break</span>;
<span style="color:blue">case </span><span style="color:#a31515">"GEOMETRYCOLLECTION"</span>:
<span style="color:blue">var </span>dummy = <span style="color:#a31515">""</span>;
<span style="color:blue">if </span>(pointArray.length > 0)
{
<span style="color:blue">for</span>(<span style="color:blue">var </span>j = 0; j < (pointArray.length); j++)
{
dummy = dummy + <span style="color:#a31515">"POINT(" </span>+ pointArray[j] + <span style="color:#a31515">"), "</span>;
}
}
<span style="color:blue">if </span>(polylineArray.length > 0)
{
<span style="color:blue">for</span>(<span style="color:blue">var </span>j = 0; j < (polylineArray.length); j++)
{
dummy = dummy + <span style="color:#a31515">"LINESTRING(" </span>+ polylineArray[j] + <span style="color:#a31515">"), "</span>;
}
}
<span style="color:blue">if </span>(polygonArray.length > 0)
{
<span style="color:blue">for</span>(<span style="color:blue">var </span>j = 0; j < (polygonArray.length); j++)
{
dummy = dummy + <span style="color:#a31515">"POLYGON((" </span>+ polygonArray[j] + <span style="color:#a31515">")), "</span>;
}
}
dummy = dummy.substr(0, dummy.length - 2);
sqlBuilder = <span style="color:#a31515">"INSERT INTO FeatureDemo (Geom) VALUES (geography::STGeomFromText('GEOMETRYCOLLECTION (" </span>+ dummy + <span style="color:#a31515">")', 4326))"</span>;
document.getElementById(<span style="color:#a31515">"txtSQL"</span>).value = sqlBuilder;
<span style="color:blue">break</span>;
}
}</pre><a href="http://11011.net/software/vspaste"></a>
<p>Now that we have drawn our geographies and dynamically created our SQL-statement we only need to execute it. As in previous examples we will use an AJAX-call but unlike in the previous examples we will use an HTTP-Post request since the SQL-statements with all the coordinates can become quite long.<pre><span style="color:blue">function </span>SqlInsert()
{
<span style="color:green">//Get the appropriate XMLHTTP object for the browser
</span><span style="color:blue">var </span>xmlhttp = GetXmlHttp();

<span style="color:green">//if we have a valid XMLHTTP object
</span><span style="color:blue">if </span>(xmlhttp)
{
xmlhttp.open(<span style="color:#a31515">"POST"</span>, <span style="color:#a31515">"./Insert.ashx"</span>, <span style="color:blue">false</span>);
xmlhttp.setRequestHeader(<span style="color:#a31515">"Content-Type"</span>,<span style="color:#a31515">"application/x-www-form-urlencoded"</span>);
<span style="color:green">//set the callback
</span>xmlhttp.onreadystatechange = <span style="color:blue">function</span>()
{
<span style="color:blue">if </span>(xmlhttp.readystate ==4) <span style="color:green">//4 is a success
</span>{
<span style="color:green">//server code creates JavaScript "on the fly" for us to
//execute using eval()
</span><span style="color:blue">var </span>result = xmlhttp.responseText
eval(result);
}
}
xmlhttp.send(<span style="color:#a31515">"sql=" </span>+ document.getElementById(<span style="color:#a31515">"txtSQL"</span>).value);
}
}</pre><a href="http://11011.net/software/vspaste"></a>
<p>The HTTP-handler which is being called by the AJAX-call is even simpler than anything we had before. We just fetch the SQL-statement from the, set up the database connection and execute the statement. A message will be returned to the AJAX-call to indicate success or possible problems with the statement.<pre><span style="color:green">'Fetch URL-parameter
</span><span style="color:blue">Dim </span>mySQL1 <span style="color:blue">As String </span>= context.Request.Params(<span style="color:#a31515">"sql"</span>)

<span style="color:green">'Prepare database
</span><span style="color:blue">Dim </span>settings <span style="color:blue">As </span>ConnectionStringSettings = ConfigurationManager.ConnectionStrings(<span style="color:#a31515">"SpatialDB"</span>)

<span style="color:blue">Dim </span>myConn1 <span style="color:blue">As New </span>SqlConnection(settings.ConnectionString)
myConn1.Open()
<span style="color:blue">Dim </span>myCMD1 <span style="color:blue">As New </span>SqlCommand(mySQL1, myConn1)

<span style="color:blue">Try
</span>myCMD1.ExecuteNonQuery()
context.Response.Write(<span style="color:#a31515">"alert('SQL-Statement Executed');"</span>)
<span style="color:blue">Catch </span>ex <span style="color:blue">As </span>Exception
context.Response.Write(<span style="color:#a31515">"alert(" </span>+ <span style="color:#a31515">"""" </span>+ ex.Message + <span style="color:#a31515">"""" </span>+ <span style="color:#a31515">");"</span>)
<span style="color:blue">End Try</span></pre><a href="http://11011.net/software/vspaste"></a>
<p>All right, we’re done. Run your page and test it.
<p><a href="http://gkxu3a.blu.livefilestore.com/y1psLOdC392akqeyrUGBe4HPu4FFP6awMvq9wQs74dGAURtUCa X5lfm8X6TJojpfVfS0PI_FPgAc3YES4skSFICwA?PARTNER=WR ITER">
http://gkxu3a.blu.livefilestore.com/y1psLOdC392akq9xcC0dbyy0Nz1Uwha-INGGlBjL6r4OZ_wnraqJwEi-P1_1E-vfdDliOdVnyhm2-bR7I7xZTxxkA?PARTNER=WRITER</a>
<p>You can also retrieve the results within SQL Server Management Studio... <pre><span style="color:blue">SELECT </span>Geom.STAsText() <span style="color:blue">FROM </span>FeatureDemo
<span style="color:green">---------------------------------------
</span>POINT (51.461166621237787 -0.9271699190139665)
MULTIPOINT ((51.461149909867657 -0.9271699190139665), (51...))
POLYGON ((51.461380526235004 -0.92682123184203891, 51...))
MULTIPOLYGON (((51.461380526235004 -0...))((...)))</pre>
<p>…and do further analysis like determining the size of the area which is covered by a polygon.<pre><span style="color:blue">SELECT </span>Geom.STArea() <span style="color:blue">FROM </span>FeatureDemo <span style="color:blue">WHERE </span>ID = 4
<span style="color:green">--------------------------------------------------
</span>2532.6 square meter</pre><a href="http://11011.net/software/vspaste"></a>
<p>Note: Polygons must be drawn counter clockwise and each polygon figure must be within a single, logical hemisphere. Individual spatial objects do not need to be within the same logical hemisphere in order to run spatial operations between them.
<p>*
<div style="padding-right:0px;display:inline;padding-left:0px;padding-bottom:0px;margin:0px;padding-top:0px">Technorati Tags: <a href="http://technorati.com/tags/Virtual Earth" rel=tag>Virtual Earth</a>,<a href="http://technorati.com/tags/SQL Server 2008" rel=tag>SQL Server 2008</a></div><img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=4819404664324524694&page=RSS%3a+Virtual+Earth+%26+SQL+Server+2008+-+Part+4%3a+Integrating+SQL+Server+2008+and+Virtual +Earth+(6%2f6)&referrer=" width="1px" height="1px" border="0" alt=""><img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&NA=1149&PI=73329&RF=&DI=3919&PS=85545&TP=johanneskebeck.spaces.live.com&GT1=JohannesKebeck">

Click here to view the article. (http://JohannesKebeck.spaces.live.com/Blog/cns!42E1F70205EC8A96!3704.entry)

 
Web mp2kmag.com
mapforums.com