<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Dave does Data : SQL Server Spatial</title><link>http://blogs.msdn.com/davidlean/archive/tags/SQL+Server+Spatial/default.aspx</link><description>Tags: SQL Server Spatial</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Review: Programming MS SQL Server 2008 – Holistic coverage for an App Developer</title><link>http://blogs.msdn.com/davidlean/archive/2009/10/03/review-programming-ms-sql-server-2008-holistic-coverage-for-an-app-developer.aspx</link><pubDate>Sat, 03 Oct 2009 11:57:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9902710</guid><dc:creator>davele</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/davidlean/comments/9902710.aspx</comments><wfw:commentRss>http://blogs.msdn.com/davidlean/commentrss.aspx?PostID=9902710</wfw:commentRss><wfw:comment>http://blogs.msdn.com/davidlean/rsscomments.aspx?PostID=9902710</wfw:comment><description>&lt;p&gt;Following on from my review of my library. This week, Programming Microsoft SQL Server 2008.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color="#0000ff"&gt;&lt;font size="3"&gt;Summary:&lt;/font&gt; &lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;It appears to me that the authors started this book with the premise “There is a lot in SQL2008 that can dramatically change the way you architect solutions. Many features elegantly remove some of the clunky poor performance approaches we use today” So they set about drawing your attention to those features &amp;amp; mapping it back to the problems you currently experience when writing applications.&lt;/p&gt;  &lt;p&gt;The book is a good read, plenty of code examples &amp;amp; screenshots to help understand the subject quickly (not just to pad out the pages)&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;This book differs from other SQL 2008 books I’ve reviewed in 3 ways :-&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;strong&gt;It takes a holistic approach&lt;/strong&gt;. It doesn’t stop at the Relational database but expands your thinking into       &lt;br /&gt;(i) handling non-relational datatypes; Spatial, Text, Video &amp;amp; Audio streaming, Hierarchies.       &lt;br /&gt;(ii) Using the BI Components for Reporting &amp;amp; Analytics instead of the, usually less efficient approach, of doing it all yourself with TSQL. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;It focuses purely on SQL 2008&lt;/strong&gt;. There is very little here for those on SQL 2005 or earlier. So it is not a “Hey there is much in the engine you will need, a lot was already there, &amp;amp; this new feature extends it” but more a “This new interface now lets you do it in 3 lines &amp;amp; removes that “huge cliff” you had to program around. OR SQL now has a component that does that, you don’t need to develop your own component from scratch.” &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;It is more for those in development teams&lt;/strong&gt;. Except for a small section, Chapter 2, the book assumes you will be comfortable in Visual Studio or at least BI Dev Studio; for writing SQL Reports &amp;amp; the Analytics piece. OR even if you don’t code yourself, you want the knowledge to suggest new approaches to architect the app better. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;a title="Link to MS Press Web Site" href="http://www.mspress.com.au/searchresults.aspx?s=a2V5d29yZA==-I7x1ozBkcPY=&amp;amp;k=cHJvZ3JhbW1pbmcgU1FMIHNlcnZlciAyMDA4-BDlvrVqC//I=" target="_blank"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Photo of Book Cover" border="0" alt="Photo of Book Cover" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/ReviewProgrammingMSSQLServer2008Holistic_BC5B/image_3.png" width="202" height="244" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color="#0000ff" size="3"&gt;Recommended Audience:&lt;/font&gt;&lt;/strong&gt; &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;SQL Leaders&lt;/strong&gt; - Everyone who considers themselves “a database person”, especially if you advise others on how to create more efficient solutions that use SQL Server.&amp;#160; &lt;br /&gt;&lt;strong&gt;Architects&lt;/strong&gt; – more so if you design down to the component level.      &lt;br /&gt;&lt;strong&gt;Developers&lt;/strong&gt; – who write code to interact with Databases, Analytics, Reporting or Data Mining.       &lt;br /&gt;Anyone who wants a good understanding of the new capabilities of SQL 2008 in 1 book, at 951 pages this is quite a reference. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color="#0000ff"&gt;Version&lt;/font&gt;&lt;/strong&gt;: SQL 2008, &amp;amp; SQL 2008 R2 (when it ships in 2010). Not for SQL 2005 &amp;amp; earlier versions – except the Analytics part. If that was all you wanted, you could find another book just dedicated to that.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color="#0000ff"&gt;&lt;font size="3"&gt;Detail:&lt;/font&gt; &lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Like SQL 2008 Internals, this book is written by a team of authors. Handy because it is hard to specialise in everything. Compared to the SQL Internals authors, they must have spent much more time peer reviewing as there is a lot less variation in style between each chapter. Though you can see one author was keen to give a little background while one of the others prefers to dive right in. &lt;/p&gt;  &lt;p&gt;&lt;font size="3"&gt;&lt;font color="#804000"&gt;&lt;strong&gt;&lt;u&gt;Part 1: Core Fundamentals&lt;/u&gt; &lt;/strong&gt;(228 Pages)&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#804000"&gt;This part is great. Buy the book just for Chapter 2 &amp;amp; then flesh out your thinking with the other chapters.&lt;/font&gt; &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Chapter 1: Overview&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Nothing. Just tells you what is in the book.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Chapter 2: T-SQL Enhancements&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#ff00ff"&gt;You’ve gotta read this chapter,&lt;/font&gt; even if you just stand there in the book store, it is pure gold. I could tell you that it has a very nice coverage of CTE’s both recursive &amp;amp; not. Or list the TSQL Features that it covers: PIVOT, UNPIVOT, APPLY, TOP, ROW_NUMBER, OVER, Ranking Functions, Try/CATCH, WAITFOR, DDL Triggers,&amp;#160; Date &amp;amp; Time Datatypes, MERGE Statement, OUTPUT clause, Grouping Sets. But Books Online does that too. What sets this apart is their application of these features. They have put a lot of thought into “Does this new feature empower me to solve a problem I’d been unable to fix before”, then serves up a lot of really useful examples &amp;amp; ideas. If you don’t read this thinking “wow I wish I had that in the last project(s) I worked on”, you must be fresh from school. (or did too many drugs in your youth)&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Chapter 3: Exploring SQL CLR&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;This is not the ultimate reference in SQL CLR, it misses some of the “how to ensure your CLR Designs perform well”. That said it is a great tutorial. It covers all you need to know to write all forms of CLRs; Datatypes, Triggers, Aggregates. How to deploy them. How to use the tools. There are a few landmines in CLR development that you can waste days in. This guides you around them, giving you a methodology that helps you avoid a lot of the newbie pain.&amp;#160;&amp;#160; &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Chapter 4: Server Management&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;How to programmatically access Server Management Objects (SMO). If your app needs to Manage SQL Server, SMO is preferred over sniffing around the System Catalogue with TSQL scripts ie: “SELECT * FROM sys.objects”. It briefly touches Policy-Based Management. But for PBM you’d get more listening to a &lt;a title="Non Tech-ED attendees can pay to download &amp;amp; watch all the Tech-Ed sessions around the world." href="http://www.msteched.com/online/home.aspx" target="_blank"&gt;Tech-Ed&lt;/a&gt; session on the topic.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Chapter 5: Security in SQL Server 2008&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Security is always a pain &amp;amp; is often the cause of a lot of “hard to solve” user problems. Largely because is it boring, not well understood &amp;amp; squeezed into the design when the project is nearly complete. This chapter makes it easier. It covers Authorization, Authentication, Encryption (inc Transparent Data encryption), Keys &amp;amp; Certificate. &lt;/p&gt;    &lt;p&gt;There is a nice section on the new SQL Server Audit Feature. If you commonly use (i) LastModifiedDate &amp;amp; LastModifiedBy columns OR (ii) Triggers that mirror the DML changes into “Audit” tables. This is a must read for you.&amp;#160; &lt;br /&gt;Also a small piece on common ways hackers will hit you.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;font size="3"&gt;&lt;font color="#804000"&gt;&lt;strong&gt;&lt;u&gt;Part 2: Beyond Relational&lt;/u&gt; &lt;/strong&gt;(106 Pages)&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#804000"&gt;This part may permit you to remove large inefficient parts of your app &amp;amp; replace them with easier to maintain much more performant alternatives. It may also get you thinking of new exciting features you can offer, that will excite your users &amp;amp;/or customers.&lt;/font&gt;&amp;#160; &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Chapter 6: XML &amp;amp; the Relational Database&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;While not a lot of recommendations or things to extend your use of this feature. It is a very comprehensive coverage of SQL’s XML capabilities together with a lot of examples. It should be enough to help figure out what isn’t working &amp;amp; get you going.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Chapter 7 Hierarchal Data and the Relational Database&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;This is 25 pages on the HierarchyID datatype. Nothing insightful, it just tells you how to use it with good examples.      &lt;br /&gt;While I’d prefer the HierarchyID datatype to be more I want it to “just work” ie: figure out &amp;amp; maintain the hierarchy without any involvement from me. It is very powerful &amp;amp; does remove a lot of performance issues from apps that use this style of relationship. eg: Org Charts, Bill of Materials. So it is worthwhile you know about it.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Chapter 8: Using Filestream for Unstructured Storage&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;This chapter lives up to its title, with a fairly comprehensive coverage on how to incorporate large files; Video, Audio, Picture &amp;amp; Documents, into SQL much more efficiently using the FILESTEAM feature. &lt;/p&gt;    &lt;p&gt;While a good chapter I was a little disappointed by the narrow focus. SQL 2008 introduced two new features for BLOB handling to compliment the SQL2005’s VARBINARY(MAX) replacement of the IMAGE datatype. &lt;strong&gt;FILESTREAM&lt;/strong&gt; was one of them &amp;amp; &lt;strong&gt;Remote BLOB Storage API&lt;/strong&gt; was the other. It seems they missed the opportunity to cover Remote BLOB’s here or at least to give them a mention &amp;amp; compare / contrast when to use one over the other. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Chapter 9: Geospatial Data types&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;This small chapter contains a reasonable coverage of (i) Spatial Types, Methods &amp;amp; (ii) how to integrate with Virtual Earth. Also a small C# example of how write code that uses the Spatial Types.      &lt;br /&gt;Call me biased but I think my &lt;a title="My articles on SQL 2008 Spatial" href="http://blogs.msdn.com/davidlean/archive/tags/Spatial/default.aspx" target="_blank"&gt;12 Blog Posts&lt;/a&gt; on the topic gives a deeper coverage on using Spatial Types &amp;amp; methods. And &lt;a title="Microsoft Virtual Earth Technology Specialist" href="http://johanneskebeck.spaces.live.com/" target="_blank"&gt;Johannes Kebeck's Blog&lt;/a&gt; is an authority source for the Virtual Earth (Bing Maps) interop. But I’d agree if they hadn’t put something in I’d be complaining that they overlooked it, so I’ll shut-up now.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;font size="3"&gt;&lt;font color="#804000"&gt;&lt;strong&gt;&lt;u&gt;Part 3: Reach Technologies&lt;/u&gt; &lt;/strong&gt;(226 Pages)&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#804000"&gt;This Part is largely a coding guide. Introducing you to features in the development tools &amp;amp; language that will have you up &amp;amp; running quickly. It ensures you are informed &amp;amp; can choose the right technology for you. &lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Chapter 10: The Microsoft Data Access Machine&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;This is a 101 for the coder. It covers all those important things you never hear Microsoft talk about in their events. Creating DataSets, Dataset designer, Table Adaptor, Connection management, DataReader, … all the stuff that is easy to do when someone has shown you &amp;amp; time-consuming to figure out by yourself. &lt;/p&gt;    &lt;p&gt;After ADO.NET, there is similar depth on Language Integrated Query (&lt;strong&gt;LINQ&lt;/strong&gt;) technologies: LINQ to DataSet, LINQ to SQL, The Entity Framework &amp;amp; EDM, Web Services for Data. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Chapter 11: The many facets of Data Binding&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Data Binding can remove a lot of code. This is a good introduction of how to get it working from the different app development environments; Windows Forms (inc LINQ to SQL, Entity Framework), Web with ASP.NET (inc AJAX) &amp;amp; Windows Presentation Foundation (WPF) including XAML &amp;amp; Silverlight.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Chapter 12: Transactions&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;It seems that most people believe that Transactions are unimportant in a relational system &amp;amp; recommend you use the NOLOCK hint to turn them off. ;-)&amp;#160; But if you believe that data corruption is undesirable in your app this chapter may interest you. It covers everything from the basics; ACID Properties &amp;amp; Isolation level. Then looks at the impact of SQL Configuration Options; ie: AutoCommit vs Explicit Transactions vs Batch Scoped &amp;amp; MARS.&amp;#160; Then finishes with good coverage on Distributed Transactions; 2 Phase commits,&amp;#160; Resource Managers. Examples show xtns in your TSQL Code, your .NET app code &amp;amp; your SQL CLR code.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Chapter 13: Developing Occasionally Connected Systems&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;70 pages of really detailed coverage looking at the technology alternatives for distributed occasionally connected apps. The new Sync Services &amp;amp; Change Tracking technologies are explained the compared &amp;amp; contrasted with Merge Replication. This chapter has more than sufficient detail for you to use it as a reference with starting to develop your app using either Merge or CT technologies. It does not overlook that most of these solutions will want to sync with PDA/Mobile phones, so SQL Server Compact v3.5 is featured strongly.&amp;#160;&amp;#160; &lt;br /&gt;&lt;u&gt;Note&lt;/u&gt;: SQL 2008’s Change Tracking (CT) is very different to Change Data Tracking (CDC) though in some apps you could use CT or CDC.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;font size="3"&gt;&lt;font color="#804000"&gt;&lt;strong&gt;&lt;u&gt;Part 4: Business Intelligence&lt;/u&gt; &lt;/strong&gt;(388 Pages)&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#804000"&gt;This Part is largely a tutorial style, guiding people inexperienced in the SQL BI topics thru the options available to them.&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font color="#804000"&gt;I had planned on comparing this to &lt;a href="http://www.mspress.com.au/searchresults.aspx?s=a2V5d29yZA==-I7x1ozBkcPY=&amp;amp;k=TWljcm9zb2Z0wq4gU1FMIFNlcnZlcsKuIDIwMDggQW5hbHlzaXMgU2VydmljZXMgU3RlcCBieSBTdGVw-+1hLYkGLtmA=" target="_blank"&gt;Microsoft SQL Server 2008 Analysis Services Step by Step&lt;/a&gt; but in my generous excitement I gave it away at a SQL User Group meeting. From memory, the content is similar. Of course this book has 3 other parts in it &amp;amp; is more targeted to those writing applications that use the engine. &lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Chapter 14: Data Warehousing&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Much of this chapter is a “What is DW”; comparing Inmon vs Kimbal &amp;amp; similar topic definitions. Change Data Capture gets a very detailed coverage. Whereas features like SPARSE columns, Data Page Compression &amp;amp; Backup Compression are introduced &amp;amp; have 1 example each.&amp;#160; &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Chapter 15: Basic OLAP&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;This is an OLAP 101. It guides you thru using the tools to create a cube. Predominantly it is an introduction to the designers &amp;amp; wizards. Like chapt 14 much of this is applicable to SQL 2005 &amp;amp; perhaps even SQL 2000. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Chapter 16: Advanced OLAP&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;This is not Advanced OLAP, but it is a useful coverage of all the other features beyond creating a basic cube. eg: Dimensions; Member Grouping, Attribute Relationships, Calculations, Names Sets, KPI’s, Actions, Perspectives, Translations &amp;amp; Security. It covers Proactive caching, Partitions, Aggregation Design &amp;amp; other Storage Settings.      &lt;br /&gt;If you have minimal to zero experience in SQL OLAP you will find these 3 chapters have sufficient detail to get you going without getting lost in detail or reading a specialist book on the topic.      &lt;br /&gt;If you want Advanced OLAP get the bible.&amp;#160; &lt;a title="Written by the Microsoft SQL Analysis Development team" href="http://www.amazon.com/Microsoft-Server-2005-Analysis-Services/dp/0672327821" target="_blank"&gt;SQL Server 2005 Analysis Services (not an introductory text)&lt;/a&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Chapter 17 OLAP Queries, Tools and Application Development&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;This covers a multiple of visualisation options.      &lt;br /&gt;(i) Excel as a client tool. This will be superseded when the Gemini Execl add-in is released with SQL 2008 R2.       &lt;br /&gt;(ii) Excel Services for web BI.      &lt;br /&gt;(iii) A tiny glimpse of MDX (Multi-Dimensional eXpression language Analysis Services (SSAS) equivalent to T-SQL )      &lt;br /&gt;(iv) .NET development via ADO MD.NET      &lt;br /&gt;(v) Analysis Management Objects (AMO) this is the SSAS equivalent to SMO.      &lt;br /&gt;(vi) XMLA – XML for Analysis Services.      &lt;br /&gt;(vii) Writing “SQL CLR’s” for Analysis Services. &lt;/p&gt;    &lt;p&gt;That is a lot of ground to cover. It does a good job of making you aware of the technologies, what they can do with 1 or 2 examples. As you can imagine once you get started on a project you’ll need more than just this chapter as your reference. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Chapter 18 Expanding your Business Intelligence with Data Mining&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;This is a nice overview of Data Mining; from methodology you should use, a look at the viewers, creating a mining model &amp;amp; how Nested Tables work thru to reasonable coving of DMX (Data Mining eXtension language). There is a few pages on including it in your code &amp;amp; some mention of the &lt;a title="Get it here" href="http://www.microsoft.com/sqlserver/2008/en/us/data-mining-addins.aspx" target="_blank"&gt;SQL Server 2008 Data Mining Add-ins for Microsoft Office 2007&lt;/a&gt;      &lt;br /&gt; Again a nice succinct chapter but if I were spending any time reading on Dataming I’d get the bible &lt;a href="http://www.amazon.com/gp/product/0470277742?ie=UTF8&amp;amp;tag=sqlserverda09-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;creativeASIN=0470277742"&gt;&lt;u&gt;&lt;strong&gt;Data Mining with Microsoft SQL Server 2008&lt;/strong&gt;&lt;/u&gt;&lt;/a&gt;&amp;#160; see my &lt;a title="I updated this months ago to point to the latest book - unfortunately I discovered today that I&amp;#39;d never posted it, Just after I deleted the updated entry. BUM." href="http://blogs.msdn.com/davidlean/archive/2008/11/06/resource-recommendations-data-mining.aspx" target="_blank"&gt;Blog Post&lt;/a&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Chapter 19: Reporting Services. &lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;This would be best described as a “Tutorial Walk thru” it will get you up &amp;amp; running. It does give some examples on how to incorporate reports into your own code. But you do have to extrapolate from the tutorial into your own work. It is not a reference on all the options you have, nor does it have any “Best Practise” Recommendations. Nor does it contain anything that will expand your thinking into new “not so obvious” applications as does chapter 2 of this book.      &lt;br /&gt;For a more detailed walkthru I’d consider &lt;a href="http://www.mspress.com.au/searchresults.aspx?s=a2V5d29yZA==-I7x1ozBkcPY=&amp;amp;k=U1FMIFNlcnZlciAyMDA4IFJlcG9ydGluZyBTZXJ2aWNlcw==-2xIeSXJ88Vc=" target="_blank"&gt;Microsoft® SQL Server® 2008 Reporting Services Step by Step&lt;/a&gt; its 525 pages swamp the 79 pages of this chapter. But still it is an introductory text &amp;amp; one suitable for Report Authors . I’ve yet to see a book that covered the programmatic aspects &amp;amp;/or extending SQL RS in any great detail. But I’ll keep looking.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color="#0000ff"&gt;If you are too busy to walk to a book store &amp;amp; too lazy to type a search into Bing or Google, the following are links to Online Retailers.&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font color="#0000ff"&gt;Australians:&lt;/font&gt; &lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a title="Scroll to the Bottom of the page." href="http://www.mspress.com.au/products.aspx?c=MTAy-uhUMsvsjojU%3d&amp;amp;s=VU5TSA%3d%3d-FzZ0EOwr4bk%3d&amp;amp;ct=TWljcm9zb2Z0IFNlcnZlcnM%3d-VCDlpFDxTyI%3d&amp;amp;st=U1FMIFNlcnZlciAyMDA4-BmM60mY0QXM%3d" target="_blank"&gt;&lt;strong&gt;MS Press Australia&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;When getting the link I noticed this promotion. &lt;/p&gt;    &lt;h5&gt;So many reasons to shop at the Microsoft Press Online Book Store      &lt;br /&gt;– &lt;font color="#ff00ff" size="3"&gt;40% off all books purchased until October 31&lt;sup&gt;st&lt;/sup&gt; 09 &lt;/font&gt;and...&lt;/h5&gt;    &lt;table border="0" cellspacing="0" cellpadding="0"&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;td valign="top" width="308"&gt;           &lt;ul&gt;             &lt;li&gt;Best &amp;amp; Latest Microsoft Press Books&lt;/li&gt;           &lt;/ul&gt;         &lt;/td&gt;          &lt;td valign="top" width="263"&gt;           &lt;ul&gt;             &lt;li&gt;Express Delivery&lt;/li&gt;           &lt;/ul&gt;         &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="308"&gt;           &lt;ul&gt;             &lt;li&gt;Quick &amp;amp; Easy Ordering&lt;/li&gt;           &lt;/ul&gt;         &lt;/td&gt;          &lt;td valign="top" width="263"&gt;           &lt;ul&gt;             &lt;li&gt;Amazon Price Match Promise&lt;/li&gt;           &lt;/ul&gt;         &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="308"&gt;           &lt;ul&gt;             &lt;li&gt;Regular Promotions&lt;/li&gt;           &lt;/ul&gt;         &lt;/td&gt;          &lt;td valign="top" width="263"&gt;           &lt;ul&gt;             &lt;li&gt;Affiliate Program&lt;/li&gt;           &lt;/ul&gt;         &lt;/td&gt;       &lt;/tr&gt;     &lt;/tbody&gt;&lt;/table&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Rest of World:&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://search.barnesandnoble.com/Programming-Microsoft-SQL-Server-2008/Leonard-Lobel/e/9780735625990/?itm=1&amp;amp;usri=Programming+Microsoft+SQL+Server+2008" target="_blank"&gt;Barnes and Noble&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.amazon.com/Programming-Microsoft-Server-2008-PRO-Developer/dp/0735625999/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1254531193&amp;amp;sr=1-1" target="_blank"&gt;Amazon&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;As always I’m interested in your thoughts. Did you read this far? Was it useful or way too much detail? &lt;/p&gt;  &lt;p&gt;Thanks Dave.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Thought for the post:&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;This took way longer to type up than I thought it would. Then again it took a few weeks to read the book too.&lt;/p&gt;    &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:068fc0a9-cf09-44ea-ad02-d02842f393fe" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/TSQL" rel="tag"&gt;TSQL&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Book+Recommendations" rel="tag"&gt;Book Recommendations&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Book+Reviews" rel="tag"&gt;Book Reviews&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SQL+2008" rel="tag"&gt;SQL 2008&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SQL+Server+2008" rel="tag"&gt;SQL Server 2008&lt;/a&gt;&lt;/div&gt;&lt;/blockquote&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9902710" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/TSQL/default.aspx">TSQL</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server+Spatial/default.aspx">SQL Server Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Recommendations/default.aspx">Recommendations</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Reporting+_2800_SSRS_2900_/default.aspx">SQL Reporting (SSRS)</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Analysis+Services+_2800_SSAS_2900_/default.aspx">SQL Analysis Services (SSAS)</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Training/default.aspx">Training</category></item><item><title>SQL Spatial - How to get Spatial data. (FREE Maps 'n Demographics)</title><link>http://blogs.msdn.com/davidlean/archive/2008/11/14/sql-spatial-how-to-get-spatial-data-free-maps-n-demographics.aspx</link><pubDate>Fri, 14 Nov 2008 00:47:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9067563</guid><dc:creator>davele</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/davidlean/comments/9067563.aspx</comments><wfw:commentRss>http://blogs.msdn.com/davidlean/commentrss.aspx?PostID=9067563</wfw:commentRss><wfw:comment>http://blogs.msdn.com/davidlean/rsscomments.aspx?PostID=9067563</wfw:comment><description>&lt;p&gt;This post lists ways to get spatial maps, some of them are Free. If you are in a hurry jump to the last link, it is so cool. Then check out the Free Map sites.&lt;/p&gt;  &lt;h1&gt;Overview&lt;/h1&gt;  &lt;p&gt;There are 3 ways to get the spatial data for your application:-&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Buy it &lt;/li&gt;    &lt;li&gt;Download it - FREE &lt;/li&gt;    &lt;li&gt;Build it Yourself - FREE but costs time &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;I'd have liked to make this a comprehensive list of every source. But it would huge &amp;amp; I'd never finish it. So mainly I'll walk you thru a couple of options, some links I've found handy &amp;amp; then show you how to get started for Free.&amp;#160; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h1&gt;Comparing the options    &lt;br /&gt;&lt;/h1&gt;  &lt;h2&gt;1a. Buy it (Own the data)&lt;/h2&gt;  &lt;blockquote&gt;   &lt;p&gt;For any large enterprise with a serious need for accurate data, this might be the cheaper than expensive employees wasting time with the &amp;quot;free&amp;quot; stuff. Typically it comes from a Professional Supplier. It is comprehensive &amp;amp; maintained with regular updates, often quarterly or as needed.&lt;/p&gt;    &lt;p&gt;There are advantages in having all the map data &amp;quot;on-site&amp;quot;. Also buying all your data up front may mean the costs are fixed, so easier to forecast &amp;amp; budget. And you can negotiate a price, perhaps discounted based on the number of users you license.      &lt;br /&gt;      &lt;br /&gt;&lt;strong&gt;Note&lt;/strong&gt;: Not all applications require accurate data. Sometimes, for example, thematic maps, you just need to convey the basic idea of the regions you are trying to represent. State boundaries can be &amp;quot;out by miles&amp;quot; &amp;amp; users will still recognise that area as a state. Occasionally this distortion improves readability eg: Moving Hawaii near California &amp;amp; making it much bigger, may make it easier to see how your Sales per population compare with other USA States. Free data could be good here.&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;u&gt;Vendors who sell spatial data&lt;/u&gt;&lt;/p&gt;    &lt;p&gt;&lt;u&gt;&lt;a title="Australian &amp;amp; New Zealand data" href="http://www.mapds.com.au/" target="_blank"&gt;Map Data Sciences (Aust &amp;amp; NZ)&lt;/a&gt;         &lt;br /&gt;&lt;/u&gt;&lt;a title="Lots of Maps &amp;amp; Demographic info, especially about USA" href="http://www.esri.com/products/index.html#6" target="_blank"&gt;ESRI&lt;/a&gt;       &lt;br /&gt;&lt;a href="http://www.mapinfo.com/products/data" target="_blank"&gt;Pitney Bowes MapInfo&lt;/a&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;&lt;font color="#ff00ff"&gt;(Lots of others - I plan to update this post with other vendors in the future.)&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;1b. Buy it (View the data)&lt;/h2&gt;  &lt;blockquote&gt;   &lt;p&gt;For many solutions Online Suppliers are preferable. They are often cheaper, as you only pay for what you've used. ie: if you occasionally zoom to very high detail (normally very expensive) you didn't need to buy the entire country at the resolution, on the off chance that someone might want to look at somewhere in high detail. You don't have to pay &amp;amp; maintain the systems to store &amp;amp; deliver the data.&amp;#160; It can deliver a richer visual experience by complimenting the maps with a vast library of satellite &amp;amp; aerial photography. &lt;/p&gt;    &lt;p&gt;A big bonus is a low entry price. For small businesses &amp;amp; startup projects this means no up-front huge expense. Hopefully your costs grow in line with your revenue. &lt;/p&gt;    &lt;p&gt;&lt;u&gt;Vendors who sell online Shape data&lt;/u&gt;&lt;/p&gt;    &lt;p&gt;&lt;a href="http://www.microsoft.com/virtualearth/" target="_blank"&gt;Virtual Earth&lt;/a&gt;&amp;#160; To use Virtual Earth with SQL see Johannes Kebeck's excellent set of posts &lt;/p&gt;    &lt;ul&gt;     &lt;li&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3529.entry"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 1: Introduction (1/2)&lt;/a&gt; &lt;/li&gt;      &lt;li&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3534.entry"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 1: Introduction (2/2)&lt;/a&gt; &lt;/li&gt;      &lt;li&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3541.entry"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 2: Spatial Data Management in SQL Server 2008 (1/3)&lt;/a&gt; &lt;/li&gt;      &lt;li&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3574.entry"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 2: Spatial Data Management in SQL Server 2008 (2/3)&lt;/a&gt; &lt;/li&gt;      &lt;li&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3589.entry"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 2: Spatial Data Management in SQL Server 2008 (3/3)&lt;/a&gt; &lt;/li&gt;      &lt;li&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3601.entry"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 3: Getting Started with Virtual Earth&lt;/a&gt; &lt;/li&gt;      &lt;li&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3615.entry"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 4: Integrating SQL Server 2008 and Virtual Earth (1/6)&lt;/a&gt; &lt;/li&gt;      &lt;li&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3626.entry"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 4: Integrating SQL Server 2008 and Virtual Earth (2/6)&lt;/a&gt; &lt;/li&gt;      &lt;li&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3631.entry"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 4: Integrating SQL Server 2008 and Virtual Earth (3/6)&lt;/a&gt; &lt;/li&gt;      &lt;li&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3642.entry"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 4: Integrating SQL Server 2008 and Virtual Earth (4/6)&lt;/a&gt; &lt;/li&gt;      &lt;li&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3692.entry"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 4: Integrating SQL Server 2008 and Virtual Earth (5/6)&lt;/a&gt; &lt;/li&gt;      &lt;li&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3704.entry"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 4: Integrating SQL Server 2008 and Virtual Earth (6/6)&lt;/a&gt; &lt;/li&gt;      &lt;li&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3705.entry"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 5: Conclusion&lt;/a&gt; &lt;/li&gt;      &lt;li&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!4504.entry"&gt;FindNearRoute with Virtual Earth 6.1 and SQL Server 2008&lt;/a&gt; &lt;/li&gt;   &lt;/ul&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;Google&lt;/strong&gt; also have an offering. It is not obvious to me how to extract data from it or to use it to display data from SQL. As a result I've not worked with it &amp;amp; can't comment on how easy/hard it is to combine with SQL Spatial. Someone at the SQL User group mentioned that Google put ads randomly on the map. When an ad for their map competitor kept displaying on their page, they decided to redevelop on Virtual Earth. I expect this would only happen on the Free version but I don't know for sure. &lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;2. Download it. FREE&lt;/h2&gt;  &lt;blockquote&gt;   &lt;p&gt;There is a ton of stuff available for free. Especially in USA. If all you want is countries &amp;amp;/or states so you can colour them, usually you can find it. The most common format is ESRI Shapefile. There are free utilities that will load Shapefiles into SQL Server 2008. I use &lt;a title="Nice little ESRI Shapefile Importing utility" href="http://www.sharpgis.net/page/Shape2SQL.aspx" target="_blank"&gt;Morten Nielsen's Shape2SQL&lt;/a&gt;. However I expect that most companies would be better suited by a more robust tool like &lt;a href="http://www.safe.com/solutions/application/SQLserver.php" target="_blank"&gt;Safe Software's FME&lt;/a&gt; which can convert data to/from any format SQL2008 included.&lt;/p&gt;    &lt;p&gt;Free stuff is typically not maintained, but if all you need is a general concept of an area for business reporting, it is often good enough. Outside of USA, if you need real detail; suburbs, postcodes, land parcels &amp;amp; detailed street names, it is unlikely you'll find it for free.      &lt;br /&gt;&amp;#160;&amp;#160; While I've not included links here you can often get highly detailed information from specialist geological sites for free. So if you want to know where the sandstone &amp;amp; granite layers are, these sites are for you.&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;u&gt;&lt;strong&gt;Cool sites for Free Map data&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;    &lt;table cellspacing="0" cellpadding="2" width="618" border="3"&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;td valign="top" width="203"&gt;&lt;strong&gt;Source&lt;/strong&gt;&lt;/td&gt;          &lt;td valign="top" width="409"&gt;&lt;strong&gt;Comment&lt;/strong&gt;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="204"&gt;&lt;a title="Great source of GeoPolitical Boundaries for many countries of the world." href="http://biogeo.berkeley.edu/bgm/gdata.php" target="_blank"&gt;gData&lt;/a&gt;&lt;/td&gt;          &lt;td valign="top" width="409"&gt;This site rocks, Maps with a decent level of detail to most places of the world. &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="205"&gt;&lt;a title="Country boundaries as at 1998" href="http://www.cdc.gov/epiinfo/shape.htm" target="_blank"&gt;Centres for Disease Control &amp;amp; Prevention&lt;/a&gt;&lt;/td&gt;          &lt;td valign="top" width="409"&gt;Also brilliant. Boundaries &amp;amp; State borders of most countries, in a modest amount of detail. Great for thematic maps. As a bonus has &lt;u&gt;heaps&lt;/u&gt; of demographic data related to population. &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="205"&gt;&lt;a title="Brilliant link to tons of sites." href="http://www.dundas.com/Products/Map/NET/Resources/index.aspx" target="_blank"&gt;Dundas Map Resources Page&lt;/a&gt;&lt;/td&gt;          &lt;td valign="top" width="409"&gt;Brilliant site with lots of links to other sites.&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="205"&gt;&lt;a title="Heaps of detailed maps of USA. No Demographic data" href="http://www.census.gov/geo/www/tiger/tgrshp2007/tgrshp2007.html" target="_blank"&gt;US Census Bureau 2007 TIGER/Line Shapefiles&lt;/a&gt;&lt;/td&gt;          &lt;td valign="top" width="409"&gt;More detail than most but only on USA&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="205"&gt;&lt;a title="A large network of global maps &amp;amp; data" href="http://www.geographynetwork.com/" target="_blank"&gt;Geography Network (ESRI)&lt;/a&gt;&lt;/td&gt;          &lt;td valign="top" width="409"&gt;Large network of global maps but with a strength on USA&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="205"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="409"&gt;&amp;#160;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="205"&gt;&lt;a href="http://gos2.geodata.gov/wps/portal/gos" target="_blank"&gt;GeoData.gov U.S. Maps &amp;amp; Data&lt;/a&gt;&lt;/td&gt;          &lt;td valign="top" width="409"&gt;Another US based set of maps.&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="205"&gt;&lt;a title="Detailed maps &amp;amp; data about worlds Plants &amp;amp; other organisms" href="http://www.floramap-ciat.org/ing/climate-grid.htm" target="_blank"&gt;FloraMap&lt;/a&gt;&lt;/td&gt;          &lt;td valign="top" width="409"&gt;Detailed maps &amp;amp; data about worlds Plants &amp;amp; other organisms&lt;/td&gt;       &lt;/tr&gt;     &lt;/tbody&gt;&lt;/table&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;&lt;u&gt;Sample Level of world detail from gData site.          &lt;br /&gt;&lt;/u&gt;&lt;/strong&gt;Sample 1. Major Regions of Australian State of New South Wales       &lt;br /&gt;displayed in SQL Server Management Studio (SSMS)&lt;/p&gt;    &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQLSpatialHowtogetSpatialdata.Mapsnstuff_DE67/gData_NSW.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="148" alt="gData_NSW" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQLSpatialHowtogetSpatialdata.Mapsnstuff_DE67/gData_NSW_thumb.png" width="244" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;    &lt;p&gt;Sample 2. Same map zoomed in to show Major Local Council Regions in Sydney.&lt;/p&gt;    &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQLSpatialHowtogetSpatialdata.Mapsnstuff_DE67/GData_Sydney.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="151" alt="GData_Sydney" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQLSpatialHowtogetSpatialdata.Mapsnstuff_DE67/GData_Sydney_thumb.png" width="244" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;    &lt;p&gt;&lt;u&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;    &lt;p&gt;&lt;u&gt;&lt;strong&gt;Cool sites for Free &amp;amp; Paid Demographic data&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;    &lt;p&gt;Often your data mining &amp;amp;/or mapping insight is enriched by considering additional demographic information. I've found an effective way to find this is to &lt;strong&gt;search &amp;quot;&amp;lt; your Country name&amp;gt; Census&amp;quot;&lt;/strong&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;table cellspacing="0" cellpadding="2" width="618" border="3"&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;td valign="top" width="255"&gt;&lt;strong&gt;Source&lt;/strong&gt;&lt;/td&gt;          &lt;td valign="top" width="356"&gt;&lt;strong&gt;Comment&lt;/strong&gt;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="255"&gt;&lt;a href="http://www.abs.gov.au/websitedbs/D3310114.nsf/Home/Census+Data " target="_blank"&gt;Australian Bureau of Statistics - Census&lt;/a&gt;&lt;/td&gt;          &lt;td valign="top" width="356"&gt;Results form 1996, 2001 &amp;amp; 2006 Census&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="255"&gt;&lt;a href="http://www.stats.govt.nz/census/default.htm" target="_blank"&gt;Statistics New Zealand&lt;/a&gt;&lt;/td&gt;          &lt;td valign="top" width="356"&gt;1996, 2001 &amp;amp; 2006 Census Data&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="255"&gt;&lt;a href="http://www.singstat.gov.sg/stats/stats.html#charts" target="_blank"&gt;Statistics Singapore&lt;/a&gt;&lt;/td&gt;          &lt;td valign="top" width="356"&gt;It appears you need to pay for this. But I've not explored completely.&lt;/td&gt;       &lt;/tr&gt;     &lt;/tbody&gt;&lt;/table&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;3. Build it Yourself - (FREE but costs time)&lt;/h2&gt;  &lt;p&gt;There are a variety of sources that let you capture &amp;amp;/or build your own data. &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#008000"&gt;A) &lt;strong&gt;Encode from a GPS Device&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;I'll document this better in a future post. The concept is that you could have someone with a Mobile GPS (phone) visit all the locations of interest &amp;amp; slowly build up your sales regions or other areas of interest. This approach is so labour intensive &amp;amp; other easier methods exist so I doubt you'd do it. But it is also very valuable approach just to track your equipment, products &amp;amp; people. Then display that info on map. I've seen some very impressive govt &amp;amp; corporate apps where this is a key part of the business solution. &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;a href="http://msdn.microsoft.com/en-us/library/bb158727.aspx" target="_blank"&gt;Creating Applications that Utilize GPS&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;a href="http://msdn.microsoft.com/en-us/library/ms850332.aspx" target="_blank"&gt;GPS Intermediate Driver Reference&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;a href="http://www.trimble.com/gps/dgps-how.shtml" target="_blank"&gt;Trimble's Tutorial on GPS &amp;amp; how it works&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;a href="http://www.msndirect.com/developer/" target="_blank"&gt;MSN Direct &amp;quot;Send to GPS&amp;quot; API&lt;/a&gt;       &lt;br /&gt;&lt;/p&gt;    &lt;p&gt;&lt;font color="#008000"&gt;B) &lt;strong&gt;Bulk GeoCode from Address Data&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;Many Data cleansing providers offer this capability. eg: &lt;a href="http://dataqualitymanager.com/" target="_blank"&gt;DQM&lt;/a&gt; but you can also write your own utilities that use the MapPoint Web service to do it for you. See &lt;a href="http://msdn.microsoft.com/en-us/library/bb545004.aspx" target="_blank"&gt;Precise Geocoding with MapPoint Web Service and Virtual Earth&lt;/a&gt;.&amp;#160; &lt;br /&gt;You can use some of the utilities &amp;amp; sites listed below.&lt;/p&gt;    &lt;p&gt;&lt;u&gt;Bulk Geocode providers&lt;/u&gt;&amp;#160; &lt;/p&gt;    &lt;table cellspacing="0" cellpadding="2" width="622" border="0"&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;td valign="top" width="295"&gt;&lt;font color="#ff00ff"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;          &lt;td valign="top" width="325"&gt;&lt;font color="#ff00ff"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="295"&gt;&lt;font color="#400000"&gt;MapPoint Web Service.&lt;/font&gt;&lt;/td&gt;          &lt;td valign="top" width="325"&gt;MapPoint has a Batch geocoder which lets you geocode in bulk for free, but you do need an account. Batch-geocoding can be accomplished either through a &lt;a href="http://msdn.microsoft.com/en-us/library/cc514453.aspx" target="_blank"&gt;Customer Data Service (CDS) API&lt;/a&gt; or manually through the &lt;a href="https://mappoint-css.live.com/CSCV3/Css.aspx" target="_blank"&gt;Virtual Earth Platform Customer Service Site&lt;/a&gt; (CSS). To test these services. You can sign-up for a free developer account &lt;a href="https://mappoint-css.live.com/MwsSignup/Eval.aspx" target="_blank"&gt;here&lt;/a&gt;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="295"&gt;&lt;a href="https://webgis.usc.edu/Services/Geocode/Default.aspx" target="_blank"&gt;USC (Uni Sth California) - Free Geocoding&lt;/a&gt;&lt;/td&gt;          &lt;td valign="top" width="325"&gt;Appears to be limited to USA Addresses&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="295"&gt;&lt;a title="Supplied by USC" href="https://webgis.usc.edu/Services/Geocode/GeocoderList.aspx" target="_blank"&gt;Large list of Geocoding Software&lt;/a&gt;&lt;/td&gt;          &lt;td valign="top" width="325"&gt;Heaps of tools listed in this index.&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="295"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="325"&gt;&amp;#160;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="295"&gt;&lt;a href="http://geocoder.ca/" target="_blank"&gt;Geocoder.ca&lt;/a&gt;&lt;/td&gt;          &lt;td valign="top" width="325"&gt;their web site also lets you &lt;b&gt;Reverse geocode any US or Canadian latitude &amp;amp; longitude - for &lt;em&gt;free&lt;/em&gt;&lt;/b&gt;. But at 1 point at a time you may need to be patient.&lt;/td&gt;       &lt;/tr&gt;     &lt;/tbody&gt;&lt;/table&gt;    &lt;p&gt;Or use functionality built into many of the professional Geospatial tools. Vendors like ESRI, MapInfo &amp;amp; &lt;a title="Seems to be a very price competitive application" href="http://www.manifold.net/index.shtml" target="_blank"&gt;Manifold&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;&lt;font color="#008000"&gt;B) &lt;strong&gt;Encode from the Web&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;There are a number of map sites where you can get Long Lat info on the point you are looking at. Both &lt;strong&gt;Virtual Earth&lt;/strong&gt; (3D mode) &amp;amp; &lt;strong&gt;Google Maps&lt;/strong&gt;, display Long / Lat / Altitude of any point you hover over. But this would requires a lot of effort to build up any decent set of objects, one point at a time. Sure, easier than driving to each point &amp;amp; using your GPS. But still hard. So ...&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;h2&gt;&lt;font color="#ff00ff"&gt;My favourite &amp;quot;Create your own GIS Data&amp;quot; solution - &lt;/font&gt;&lt;font color="#0000ff"&gt;This is cool!&lt;/font&gt;&lt;/h2&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;Build your own very accurate regions, sales territories, geopolitical boundaries etc &amp;amp; export them directly into SQL 2008. IDV solution have made this brilliant tool &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;The &lt;a title="Make your own Spatial Shapes &amp;amp; export to SQL 2008" href="http://silverlight.idvsolutions.com " target="_blank"&gt;idvSolutions Spatial wiki&lt;/a&gt; site is run by IDV Solutions as a demonstration of the powerful visual experiences that can be created with Silverlight. It uses Virtual Earth for its spatial capability. &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;It allows you to draw Points, Lines &amp;amp; Polygons anywhere in the world, at extremely high detail, then Export your regions to SQL 2008. &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;This could be very handy for small businesses to create their own sales territories &amp;amp; other fairly static regions. Then use them to build thematic maps of various parts of your business. Sure this could be labour intensive &amp;amp; thus expensive if you needed to create a lot of shapes. But for a small family business, there is always a school kid somewhere keen to help out.&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;I&amp;#8217;m not sure how long they will continue to provide this free service. You can use it to map entire countries or tiny sections of a house.&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQLSpatialHowtogetSpatialdata.Mapsnstuff_DE67/NZMap_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="185" alt="NZMap" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQLSpatialHowtogetSpatialdata.Mapsnstuff_DE67/NZMap_thumb.png" width="244" border="0" /&gt;&lt;/a&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;In this example&lt;/strong&gt; I've created the region representing a local Shopping Complex, then included the closest Railway &amp;amp; rail stations. &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;1. Go to the idvSolutions site &amp;amp; see the Virtual Earth background.&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQLSpatialHowtogetSpatialdata.Mapsnstuff_DE67/IDVAerial1.jpg"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="174" alt="IDVAerial1" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQLSpatialHowtogetSpatialdata.Mapsnstuff_DE67/IDVAerial1_thumb.jpg" width="244" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;2. Click &amp;quot;Create New&amp;quot;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;3. Draw your Region, Path or Points of Interest on the map. Give each one a name &amp;amp; description. &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQLSpatialHowtogetSpatialdata.Mapsnstuff_DE67/IDVDrawing2_2.jpg"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="165" alt="IDVDrawing2" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQLSpatialHowtogetSpatialdata.Mapsnstuff_DE67/IDVDrawing2_thumb.jpg" width="244" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;4. Label &amp;amp; Save Drawing. Draw in Map, Aerial or Hybrid View.&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQLSpatialHowtogetSpatialdata.Mapsnstuff_DE67/IDVDrawing3_2.jpg"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="164" alt="IDVDrawing3" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQLSpatialHowtogetSpatialdata.Mapsnstuff_DE67/IDVDrawing3_thumb.jpg" width="244" border="0" /&gt;&lt;/a&gt; &lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQLSpatialHowtogetSpatialdata.Mapsnstuff_DE67/IDVDrawing5_2.jpg"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="150" alt="IDVDrawing5" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQLSpatialHowtogetSpatialdata.Mapsnstuff_DE67/IDVDrawing5_thumb.jpg" width="244" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;5. Click Export Drawing &amp;amp; Select format &amp;quot;SQL Server 2008&amp;quot;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQLSpatialHowtogetSpatialdata.Mapsnstuff_DE67/IDVDrawing6_4.jpg"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="165" alt="IDVDrawing6" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQLSpatialHowtogetSpatialdata.Mapsnstuff_DE67/IDVDrawing6_thumb_1.jpg" width="244" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;6. Run the TSQL it creates to insert the spatial objects you created.&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQLSpatialHowtogetSpatialdata.Mapsnstuff_DE67/IDVDrawing7.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="113" alt="IDVDrawing7" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQLSpatialHowtogetSpatialdata.Mapsnstuff_DE67/IDVDrawing7_thumb.png" width="493" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;/blockquote&gt;  &lt;h4&gt;Thanks&lt;/h4&gt;  &lt;p&gt;Please give feedback. Is this type of info useful? Did it save you time? What was good, What could be better?, Notice any errors? What would you like me to cover? All thoughts, comments, suggestions welcome.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:f25d321e-2865-4b32-a627-57e7e68cf2a6" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/SQL%20Server" rel="tag"&gt;SQL Server&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SQL%20Server%20Spatial" rel="tag"&gt;SQL Server Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Spatial" rel="tag"&gt;Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Free" rel="tag"&gt;Free&lt;/a&gt;,&lt;a href="http://technorati.com/tags/TSQL" rel="tag"&gt;TSQL&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Geo%20Data" rel="tag"&gt;Geo Data&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9067563" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/davidlean/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Spatial/default.aspx">Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/TSQL/default.aspx">TSQL</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server+Spatial/default.aspx">SQL Server Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geometry/default.aspx">Geometry</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Polygon/default.aspx">Polygon</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geography/default.aspx">Geography</category></item><item><title>SQL Spatial - Tips on Storage</title><link>http://blogs.msdn.com/davidlean/archive/2008/11/04/sql-spatial-tips-on-storage.aspx</link><pubDate>Tue, 04 Nov 2008 07:06:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9037104</guid><dc:creator>davele</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/davidlean/comments/9037104.aspx</comments><wfw:commentRss>http://blogs.msdn.com/davidlean/commentrss.aspx?PostID=9037104</wfw:commentRss><wfw:comment>http://blogs.msdn.com/davidlean/rsscomments.aspx?PostID=9037104</wfw:comment><description>&lt;p&gt;This post talks about ways to Store Spatial Reference data in SQL in order to improve performance of your map displays.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;: There are two broad categories of Spatial Data. &lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;strong&gt;Spatial Reference data&lt;/strong&gt;, the &amp;quot;stuff&amp;quot; you draw maps with; Geo-Political Boundaries (States, Postcodes), Rivers, Roads etc. It tends to be changed infrequently &amp;amp; typically refreshed via a batch process. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Spatial Attributes.&lt;/strong&gt; They are properties that helps describe some other data eg: The Location column in a Customer Address record. Or the location of a Service Van at a point in time. This information, these records are quite volatile. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;We will only look at the first group &amp;quot;Spatial Reference data&amp;quot;. The second group is just standard database design &amp;amp; tuning.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Warning&lt;/strong&gt;: The following section outlines some changes you can make to your database schema to improve performance. Perhaps you don't need to. As with all SQL performance tuning it is worth testing to get a baseline. And testing for benefit, before putting hints in your code or racing off creating complexity for operations staff. &lt;/p&gt;  &lt;h1&gt;&amp;#160;&lt;/h1&gt;  &lt;h1&gt;Storing your Spatial Reference Data in SQL    &lt;br /&gt;&lt;/h1&gt;  &lt;h2&gt;1. Consider using a Read-Only filegroup&lt;/h2&gt;  &lt;blockquote&gt;   &lt;p&gt;If you have a lot of data &amp;amp; it is largely static, this can simplify maintenance operations of your database. After you've loaded the data &amp;amp; backed it up once, it doesn't have to be backed up again, until you change it. This reduces the time is takes to run your &amp;quot;normal&amp;quot; backups &amp;amp; also reduces their file size. Restore operations are faster as SQL &amp;quot;knows&amp;quot; that the pages haven't been modified.&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;This is invisible to your application, none of your queries need to change. (Unless they are trying to modify the table in which case, this is unsuitable.)&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;Typically you would update the tables on a READ-ONLY filegroup via a batch job. The batch would, (i) swap the filegroup to READ-WRITE, (ii) change the data, (iii) swap back to READ-ONLY &amp;amp; then (iv) run a BACKUP.&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;Alternatively, you would partition your table over multiple filegroups. Your new or changing rows would be inserted in the RW part of the table &amp;amp; the historical records would remain unchanged on the part of the table residing on the partition located in the Read-Only filegroup.&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Sample code to Manipulate a READ-ONLY Filegroup &amp;gt;==
-- This assumes you've created a database called SpatialDB --
&lt;/span&gt;&lt;span style="color: blue"&gt;USE &lt;/span&gt;SpatialDB
&lt;span style="color: blue"&gt;go

&lt;/span&gt;&lt;span style="color: green"&gt;-- &amp;lt; Add a File &amp;amp; FileGroup the database &amp;gt;--
&lt;/span&gt;&lt;span style="color: blue"&gt;ALTER DATABASE &lt;/span&gt;SpatialDB &lt;span style="color: blue"&gt;ADD &lt;/span&gt;&lt;font color="#0000ff"&gt;FILEGROUP&lt;/font&gt; fgSpatialRO
&lt;span style="color: blue"&gt;go

ALTER DATABASE &lt;/span&gt;SpatialDB 
              &lt;span style="color: blue"&gt;ADD FILE &lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;font color="#0000ff"&gt;NAME&lt;/font&gt; &lt;span style="color: gray"&gt;= &lt;/span&gt;spatialfile1&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: blue"&gt;FILENAME &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'c:\temp\spatialfile1'&lt;/span&gt;&lt;span style="color: gray"&gt;)
              &lt;/span&gt;&lt;span style="color: blue"&gt;TO &lt;/span&gt;&lt;font color="#0000ff"&gt;FILEGROUP&lt;/font&gt; fgSpatialRO
&lt;span style="color: blue"&gt;go
&lt;/span&gt;&lt;span style="color: green"&gt;-- &amp;lt; Create a table and associate it to a filegroup &amp;gt;--
&lt;/span&gt;&lt;span style="color: blue"&gt;CREATE TABLE &lt;/span&gt;dbo&lt;span style="color: gray"&gt;.&lt;/span&gt;Roads &lt;span style="color: gray"&gt;(
   &lt;/span&gt;ID &lt;span style="color: blue"&gt;INT PRIMARY KEY IDENTITY&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;1&lt;span style="color: gray"&gt;,&lt;/span&gt;1&lt;span style="color: gray"&gt;),
   &lt;/span&gt;Geom &lt;span style="color: blue"&gt;INT&lt;/span&gt;&lt;span style="color: green"&gt;-- Normally this would be a Geography type
&lt;/span&gt;&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;ON &lt;/span&gt;fgSpatialRO
&lt;span style="color: blue"&gt;go

&lt;/span&gt;&lt;span style="color: green"&gt;-- Insert 13,000 rows with different identity values
&lt;/span&gt;&lt;span style="color: blue"&gt;INSERT INTO &lt;/span&gt;dbo&lt;span style="color: gray"&gt;.&lt;/span&gt;Roads &lt;span style="color: blue"&gt;VALUES&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;1&lt;span style="color: gray"&gt;)
&lt;/span&gt;&lt;span style="color: blue"&gt;go &lt;/span&gt;13000  

&lt;span style="color: green"&gt;-- ==&amp;lt; Mark the filegroup read-only &amp;gt;==
&lt;/span&gt;&lt;span style="color: blue"&gt;ALTER DATABASE &lt;/span&gt;SpatialDB &lt;font color="#0000ff"&gt;MODIFY FILEGROUP&lt;/font&gt; fgSpatialRO &lt;span style="color: blue"&gt;READ_ONLY &lt;/span&gt;&lt;span style="color: green"&gt;--  swap back with READ_WRITE
&lt;/span&gt;&lt;span style="color: blue"&gt;go

&lt;/span&gt;&lt;span style="color: green"&gt;-- --&amp;lt; Backup the Database &amp;gt;--
-- BACKUP DATABASE SpatialDB .....&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Shared locks &lt;u&gt;&lt;strong&gt;are taken&lt;/strong&gt;&lt;/u&gt; when querying a table located on a Read-Only filegroup. It is a common misconception that they are not. &lt;/p&gt;

  &lt;p&gt;Bonus code. If you'd like to compare the locks taken when a Filegroup is RO vs RW, or&amp;#160; If a Database is RO vs RW use this code. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- run a transaction with repeatable read isolation
&lt;/span&gt;&lt;span style="color: blue"&gt;SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
BEGIN TRAN
SELECT &lt;/span&gt;&lt;span style="color: gray"&gt;* &lt;/span&gt;&lt;span style="color: blue"&gt;from &lt;/span&gt;dbo&lt;span style="color: gray"&gt;.&lt;/span&gt;Roads &lt;span style="color: blue"&gt;where &lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;ID &lt;span style="color: gray"&gt;% &lt;/span&gt;5 &lt;span style="color: gray"&gt;= &lt;/span&gt;0&lt;span style="color: gray"&gt;) AND &lt;/span&gt;ID &lt;span style="color: gray"&gt;BETWEEN &lt;/span&gt;7000 &lt;span style="color: gray"&gt;AND &lt;/span&gt;9000

&lt;span style="color: green"&gt;-- Look at the locks that are being held
&lt;/span&gt;&lt;span style="color: blue"&gt;EXEC &lt;/span&gt;&lt;span style="color: maroon"&gt;sp_lock &lt;/span&gt;&lt;span style="color: magenta"&gt;@@spid

&lt;/span&gt;&lt;span style="color: green"&gt;-- Free the shared locks
&lt;/span&gt;&lt;span style="color: blue"&gt;ROLLBACK TRAN
&lt;/span&gt;&lt;span style="color: green"&gt;--COMMIT TRANSACTION&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;h2&gt;2. Consider using a Read-Only database&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;In addition to the benefits offered by a RO Filegroup, Read-Only databases offer a performance benefit, when you query them almost no locks are taken. This can free memory to used for other purposes, perhaps to hold more data in RAM. Of course, the transaction log never grows &amp;amp; recovery is faster for same reasons as read-only filegroups.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;The Disadvantage is the potential to disrupt user processing when swapping a database from Read-Only to Read-Write or vice versa.. Sure it is only 1 command which runs in milliseconds but All connections to &lt;u&gt;that&lt;/u&gt; database will need to be dropped. This issue can be minimised by being intelligent with your connections. eg: &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
  &lt;ol&gt;
    &lt;li&gt;&amp;quot;Connect to&amp;quot; or &amp;quot;Use&amp;quot; another database, perhaps the one you are using for other data &amp;amp; then refer to the ReadOnly spatial database using a 3 part naming scheme. 
      &lt;br /&gt;eg: &lt;font color="#0000ff"&gt;SELECT&lt;/font&gt; ... &lt;font color="#0000ff"&gt;FROM&lt;/font&gt; &lt;strong&gt;ROSpatialDB&lt;/strong&gt;.Zoom3.Suburbs &lt;/li&gt;

    &lt;li&gt;Don't hold a connection open if you aren't using it. Have an error handler that reconnects &amp;amp; reties the query if you lose the server. &lt;/li&gt;

    &lt;li&gt;Block out a time when the database will be unavailable due to maintenance. Increasingly companies are demanding 7 *24, but maybe few people are looking at maps at 2am Sunday morning. So they will tolerate needing to reconnect if you accidentally disconnect them. &lt;/li&gt;
  &lt;/ol&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- --&amp;lt; Backup the Database &amp;gt;--
-- BACKUP DATABASE SpatialDB .....

-- ==&amp;lt; Sample: How to make a database Read Only &amp;gt;==
-- Ensure you aren't using the Database you're changing or you need to reconnect.
&lt;/span&gt;&lt;span style="color: blue"&gt;USE master
go
&lt;/span&gt;&lt;span style="color: green"&gt;-- Change the database Option
&lt;/span&gt;&lt;span style="color: blue"&gt;ALTER DATABASE &lt;/span&gt;SpatialDB &lt;span style="color: blue"&gt;SET READ_ONLY&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: green"&gt;--ALTER DATABASE SpatialDB SET READ_WRITE;
&lt;/span&gt;&lt;span style="color: blue"&gt;go
&lt;/span&gt;&lt;span style="color: green"&gt;-- Go back to your &amp;quot;original&amp;quot; db
&lt;/span&gt;&lt;span style="color: blue"&gt;USE &lt;/span&gt;SpatialDB
&lt;span style="color: blue"&gt;go&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;font color="#ff0000"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;h2&gt;3. Can't I just get better concurrency &amp;amp; lower memory overhead by using a &amp;quot;With NOLOCK&amp;quot; hint?&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;Maybe, but just like the &amp;quot;Cut off your legs to lose weight&amp;quot; diet, you really want to think it thru. The links below may help your research. But real rule is, look to see if your schema &amp;amp; queries are optimal, then try it as a last resort. Test to see if it makes a difference &amp;amp; if you go with it, document everything; why, the version &amp;amp; build you ran your tests on, etc.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a title="Lubor Kollar has lead part of the SQL Team that developer the SQL Engine, now works on SQL&amp;#39;s High Performance Team" href="http://blogs.msdn.com/sqlcat/archive/2007/02/01/previously-committed-rows-might-be-missed-if-nolock-hint-is-used.aspx" target="_blank"&gt;Previously committed rows might be missed if NOLOCK hint is used&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/11/10/1280.aspx" target="_blank"&gt;Timebomb - The Consistency problem with NOLOCK / READ UNCOMMITTED&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a title="Background on NOLOCK but doesn&amp;#39;t address the problems." href="http://articles.techrepublic.com.com/5100-10878_11-6185492.html" target="_blank"&gt;Using NOLOCK and READPAST table hints in SQL Server&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h1&gt;Common Spatial Data Store Designs. (Background optimising your table schema) 
  &lt;br /&gt;&lt;/h1&gt;

&lt;p&gt;Can I get a better user experience if I pre-compute objects, remove some of the detail &amp;amp; put them in Lower Detail tables? Often the answer is yes. The following may give you some ideas.&lt;/p&gt;

&lt;h2&gt;&lt;font color="#ff00ff"&gt;Option 1. Split your data into Layers&lt;/font&gt;&lt;/h2&gt;

&lt;h3&gt;Option 1a: Create a Table for each layer&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;Each layer of your map; Roads, Rivers, Features, gets its own table. eg: WA.Railways, WA.MainRoads, WA.Suburbs, WA.Rivers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;This is the most common approach. Possibly as was the easiest to create 30 years ago. Possibly because that is how GIS Data vendors sell their data &amp;amp; no-one spent a lot of time thinking about alternatives. Possibly because is easiest way to offer users display flexibility, as users check the option to display/hide a layer, ie: Railways, it is easy to query the relevant table &amp;amp; display it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;An advantage to this that all the spatial data in the table is of the same dimensionality (ie: Railways are typically Linestrings, Countries are polygons), so this may let you optimise your code. 
    &lt;br /&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;Option 1b: Create a Schema for each major region &lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;If you have huge amounts of detail &amp;amp; each business unit is only focused on a limited geographic area (ie State Mgr), it may be worthwhile to create a schema name for each major Region, then create tables for each layer within each schema. eg: Create a schema for each state, containing all roads, land within that state etc. eg: The Roads table would be split into; NSW.Roads, VIC.Roads, WA.Roads ... 
    &lt;br /&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;Option 1c: Create a Schema for each &amp;quot;Zoom&amp;quot; level&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;Storing multiple versions of your map data, each with a reduced level of detail can significantly reduce the load on your system, both for data retrieval &amp;amp; also for client rendering. eg: &lt;strong&gt;zoom1.Roads&lt;/strong&gt; may only contain Major Highways that stretch across the state. &lt;strong&gt;zoom3.Roads&lt;/strong&gt; might be major roads in each city, &lt;strong&gt;zoom5.Roads&lt;/strong&gt; might show all roads but only as lines. Perhaps you might go all the way in to &amp;quot;Zoom9&amp;quot; before you display all the Exit ramps, turning lanes &amp;amp; minute details. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Pre-computing your reference data &amp;amp; storing the same tablename with a different schema makes writing your application simpler when needing to zoom in. Changing the schema name from say, 1 to 9, is cleaner than using conditional code to change table names ie: Highways, MajorRoads, Roads, RoadsWithDetails. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Also makes it simpler to refresh data as it is updated. You could just truncate all tables of that feature &amp;amp; regenerate them all with the new data. NB: You may not need to regenerate ALL zoom levels, it is rare that changes are so significant they are visible beyond the &amp;quot;City&amp;quot; level. 
    &lt;br /&gt;

    &lt;br /&gt;

    &lt;br /&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;Option 1d: Create an extra column for each &amp;quot;Zoom&amp;quot; level&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;This option doesn't have the benefit of reducing the number of rows returned. And has the disadvantage of creating really long rows. But makes sense in situations where your table doesn't have many rows but contains very detailed polygons &amp;amp; lots of other supporting columns. This is typical of thematic maps which mainly look at the same regions coloured with different data. Sometimes you view a dashboard showing N little copies (ie: Violent crimes, Minor Crimes, etc), occasionally you expand one map to the entire screen. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;The advantage is it saves you from maintaining a set of Zoom Tables (like Option 1c). 
    &lt;br /&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;Option 1e: Add a &amp;quot;ZoomLevel&amp;quot; column &amp;amp; duplicate each row.&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;If your row didn't have a lot of other columns or you can normalize your design to put the associated data in a separate table, it probably easier to add an &amp;quot;ZoomLevel&amp;quot; column &amp;amp; duplicate the row pre-computed to a lower resolution. 
    &lt;br /&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;Option 1f: Add a &amp;quot;Reduce by value&amp;quot; column &amp;amp; compute on the fly.&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;Adding a column containing a default or suggested &amp;quot;Reduce by&amp;quot; value to pass to a&amp;#160; .Reduce method, saves the overhead of calculating an appropriate value. This may let you define different values for different rows, saving diskspace. A variation of this approach is to create a &amp;quot;ZoomLevel&amp;quot; table with the appropriate value for each &amp;quot;level&amp;quot; you offer in your app. Of course computing this at query time will hit your CPU harder than if you pre-Reduce the row. 
    &lt;br /&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;Option 1g: Add an &amp;quot;Envelope&amp;quot; column to each object.&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;Especially for Geography types, you may find it useful to store the extreme edges of your shape; Most Left, Most Right, Lowest Lat, Highest Lat. These 4 values can be stored as 4 float columns or combined into one geography column containing a 2 point LineString (Lower Left, Upper Right). This persists the STEnvelope values to help quickly calculate an optional value to Reduce by. Handy if your app offers the users a Pan &amp;amp; Zoom capability. Less useful where a report is just displaying a static, thematic map.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;&lt;font color="#ff00ff"&gt;2. Split your data into Geographic regions&lt;/font&gt;&lt;/h2&gt;

&lt;h2&gt;&amp;#160;&lt;/h2&gt;

&lt;h2&gt;Option 2: Create Tiles.&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;Divide your world up into little sections. Possibly based on a functional unit of analysis (ie: Postcode or Suburb boundaries) OR possibly squares of a certain size, (NB: This is how Virtual Earth / Google Maps do it). &lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Typically you would pre-compute entire &amp;#8220;sections&amp;#8221; of a map perhaps storing them as GeometryCollections. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;u&gt;Advantage&lt;/u&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Very efficient for panning around a map. You can improve speed by pre-fetching just the non-visible area around the edge of the map. And it is easy to filter out details not viewed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;3. Do both&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;Create Layered Tiles, &amp;amp; maybe create a Schema for each Zoom Level.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;Final Thought &lt;/h2&gt;

&lt;p&gt;Source: my young son. Sure it is lame, but if you know some pre-teens &amp;amp; tell it right, it cracks them up.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Two cupcakes are sitting in an oven.&lt;/p&gt;

  &lt;p&gt;One says to the other &amp;quot;It's really hot in here&amp;quot;&lt;/p&gt;

  &lt;p&gt;The other says &amp;quot;Whoa, A talking cupcake!&amp;quot; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;Thanks&lt;/h1&gt;

&lt;p&gt;Please give feedback. Is this series of articles useful? Did it save you time? What was good, What could be better?, Notice any errors? What would you like me to cover? All thoughts, comments, suggestions welcome.&lt;/p&gt;

&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:fe23297c-a24b-40bf-bc03-840615758fa3" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/SQL%20Server" rel="tag"&gt;SQL Server&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SQL%20Server%20Spatial" rel="tag"&gt;SQL Server Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Spatial" rel="tag"&gt;Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/TSQL" rel="tag"&gt;TSQL&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9037104" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Spatial/default.aspx">Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/TSQL/default.aspx">TSQL</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server+Spatial/default.aspx">SQL Server Spatial</category></item><item><title>SQL 2008 - Ways to architect Spatial apps</title><link>http://blogs.msdn.com/davidlean/archive/2008/11/03/sql-2008-ways-to-architect-spatial-apps.aspx</link><pubDate>Mon, 03 Nov 2008 06:32:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9030760</guid><dc:creator>davele</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/davidlean/comments/9030760.aspx</comments><wfw:commentRss>http://blogs.msdn.com/davidlean/commentrss.aspx?PostID=9030760</wfw:commentRss><wfw:comment>http://blogs.msdn.com/davidlean/rsscomments.aspx?PostID=9030760</wfw:comment><description>&lt;p&gt;Adding spatial capability to your app might be simpler than you think. This is not a comprehensive coverage on architecture, but rather just a few ideas to stimulate your thinking. &lt;/p&gt;  &lt;h1&gt;Overview&lt;/h1&gt;  &lt;p&gt;In an N-Tier environment you have one of 3 options :-&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;All spatial processing occurs on the Server(s) - Mid-Tier &amp;amp;/or Backend. &lt;/li&gt;    &lt;li&gt;All spatial processing occurs in the client.&lt;/li&gt;    &lt;li&gt;Both Client &amp;amp; Servers are spatially aware. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;This post concentrates on Option 1. What a DBA can do to add spatial intelligence into an application with minimal changes to your app design. A few stored procedures &amp;amp; away you go.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h1&gt;1. Spatial capability only on Server&lt;/h1&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Concept&lt;/u&gt;&lt;/strong&gt;: The application does not deal with Geometry / Geography data types. &lt;/p&gt;  &lt;p&gt;The rows passed back from the server may be the result of a query that included some spatial aspect.&amp;#160; &lt;br /&gt;If the app needs to show points,&amp;#160; the X,Y values are passed as 2 floating point numbers. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Use examples&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;List all cities within 50km from here.&lt;/li&gt;    &lt;li&gt;List the phone number of the nearest Petrol Station (that delivers fuel :-) ).&lt;/li&gt;    &lt;li&gt;List phone numbers to all Video Stores in this suburb &amp;amp; any suburbs adjacent to this one.&lt;/li&gt;    &lt;li&gt;List Homes for sale in suburb &amp;#8216;S&amp;#8217; &amp;amp; adjoining suburbs.&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;List HousesForSale within 5km of Train, 2km near primary school &amp;amp; 10km Shops in NorthRegion&lt;/li&gt;   &lt;/ul&gt;    &lt;li&gt;List the distances between this point (my customer) &amp;amp; our 3 closest repair centres.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;u&gt;&lt;strong&gt;Advantage&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Doesn't use any new technology. So works with everything:-&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;Mobiles/PDA&amp;#8217;s (Compact Framework), &lt;/li&gt;      &lt;li&gt;SQL CE, older technologies (ODBC drivers)&lt;/li&gt;      &lt;li&gt;Merge Replication &amp;amp;/or Sync Services.&lt;/li&gt;   &lt;/ul&gt;    &lt;li&gt;Ideal for putting spatial apps on mobile phones. Necessary as SQL's Spatial Library is not available for the Compact Framework yet.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;u&gt;&lt;strong&gt;Disadvantage&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Can be verbose.&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;Fine when dealing with a few Points.&lt;/li&gt;      &lt;li&gt;Performance might suck if app requires passing many Shapes each comprising of 1,000&amp;#8217;s of points. You can use WKT for this, but as you'd still have to parse them, it would be easier to send as WKB &amp;amp; write you own parsing routines (or rethink your design)&lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;p&gt;&lt;u&gt;Example 1: Pass X,Y as Numbers&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;App calls a stored procedure with an two floating point numbers &amp;amp; returns the names of the cities closest to that point. NB: This proc depends on you having a Cities table containing the Long/Lats of all the relevant cities or suburbs. (This code is derived from a sample originally developed by my friend &lt;a title="Dr Greg Low" href="http://sqlblog.com/blogs/greg_low/" target="_blank"&gt;Dr Greg Low&lt;/a&gt; for the SQL 2008 Launch Event)&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- Pass (X,Y) as Float &amp;amp; Returns : Name of nearest City &amp;amp; its X,Y Location
&lt;/span&gt;&lt;span style="color: blue"&gt;CREATE PROCEDURE &lt;/span&gt;[dbo]&lt;span style="color: gray"&gt;.&lt;/span&gt;[LocateNearestCityTo]
   @Latitude &lt;span style="color: blue"&gt;float&lt;/span&gt;&lt;span style="color: gray"&gt;,  &lt;/span&gt;@Longitude &lt;span style="color: blue"&gt;float
AS
   &lt;/span&gt;&lt;span style="color: green"&gt;-- Convert the X &amp;amp; Y numbers into a Geography Point
   &lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;&lt;strong&gt;&lt;font color="#ff8040"&gt;@TestLocation&lt;/font&gt;&lt;/strong&gt; &lt;span style="color: blue"&gt;AS GEOGRAPHY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(
                &lt;/span&gt;&lt;span style="color: red"&gt;'POINT (' &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@Longitude&lt;span style="color: gray"&gt;)  + &lt;/span&gt;&lt;span style="color: red"&gt;' ' 
                &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@Latitude&lt;span style="color: gray"&gt;) + &lt;/span&gt;&lt;span style="color: red"&gt;')'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;4326&lt;span style="color: gray"&gt;);

    &lt;/span&gt;&lt;span style="color: green"&gt;-- Return details of the closest City(s)
   &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: magenta"&gt;COALESCE&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;c&lt;span style="color: gray"&gt;.&lt;/span&gt;CityName&lt;span style="color: gray"&gt;,&lt;/span&gt;&lt;span style="color: red"&gt;''&lt;/span&gt;&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;CityName
                  &lt;span style="color: gray"&gt;,&lt;/span&gt;c&lt;span style="color: gray"&gt;.&lt;/span&gt;Location&lt;span style="color: gray"&gt;.&lt;/span&gt;Lat &lt;span style="color: blue"&gt;AS &lt;/span&gt;ToLat
                  &lt;span style="color: gray"&gt;,&lt;/span&gt;c&lt;span style="color: gray"&gt;.&lt;/span&gt;Location&lt;span style="color: gray"&gt;.&lt;/span&gt;Long &lt;span style="color: blue"&gt;AS &lt;/span&gt;ToLong
   &lt;span style="color: blue"&gt;FROM &lt;/span&gt;dbo&lt;span style="color: gray"&gt;.&lt;/span&gt;Cities &lt;span style="color: blue"&gt;AS &lt;/span&gt;c 
   &lt;span style="color: blue"&gt;WHERE &lt;/span&gt;c&lt;span style="color: gray"&gt;.&lt;/span&gt;Location&lt;span style="color: gray"&gt;.&lt;/span&gt;STDistance&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;strong&gt;&lt;font color="#ff8000"&gt;@TestLocation&lt;/font&gt;&lt;/strong&gt;&lt;span style="color: gray"&gt;) = (
                    &lt;/span&gt;&lt;span style="color: green"&gt;-- Find the shortest distance from that point to any city
                    &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT  &lt;/span&gt;&lt;span style="color: magenta"&gt;MIN&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;t&lt;span style="color: gray"&gt;.&lt;/span&gt;Location&lt;span style="color: gray"&gt;.&lt;/span&gt;STDistance&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;strong&gt;&lt;font color="#ff8040"&gt;@TestLocation&lt;/font&gt;&lt;/strong&gt;&lt;span style="color: gray"&gt;))
                    &lt;/span&gt;&lt;span style="color: blue"&gt;FROM &lt;/span&gt;dbo&lt;span style="color: gray"&gt;.&lt;/span&gt;Cities &lt;span style="color: blue"&gt;AS &lt;/span&gt;t
   &lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: blue"&gt;GO&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;u&gt;Example 2: Pass a key as a String, Get back a List&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;This Proc finds the closest Railway station to the suburb name you pass as a parameter.&lt;/p&gt;

&lt;p&gt;Conceptually this shows you could pass a string &amp;amp; get back a set of results. The App developer does not need to know that your stored procedure used the Spatial Methods get the answer.&lt;/p&gt;

&lt;p&gt;In Reality &amp;#8211; I left the included extra information (numbers) in the results. (a) X,Y of Start point, (b) X,Y of RailStation &amp;amp; (c ) Distance between them. This information could be passed to Virtual Earth or some other Map Display to plot the locations &amp;amp; perhaps draw a line between them.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;CREATE PROCEDURE &lt;/span&gt;&lt;span style="color: black"&gt;[dbo].[FindStationNearestTo] @Suburb varchar(100)
&lt;/span&gt;&lt;span style="color: blue"&gt;AS
   &lt;/span&gt;&lt;span style="color: #009900"&gt;-- Lookup the location of the suburb called @Suburb
    &lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;&lt;span style="color: black"&gt;@TestLocation &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: black"&gt;;
    &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: black"&gt;@TestLocation = Location
    &lt;/span&gt;&lt;span style="color: blue"&gt;FROM &lt;/span&gt;&lt;span style="color: black"&gt;dbo.Cities 
    &lt;/span&gt;&lt;span style="color: blue"&gt;WHERE &lt;/span&gt;&lt;span style="color: black"&gt;CityName = @Suburb;

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: magenta"&gt;COALESCE&lt;/span&gt;&lt;span style="color: black"&gt;(c.CityName,'') &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;&lt;span style="color: black"&gt;CityName, 
     &lt;/span&gt;&lt;span style="color: magenta"&gt;COALESCE&lt;/span&gt;&lt;span style="color: black"&gt;(c.Location.STDistance(@TestLocation) / 1000.0,0) &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;&lt;span style="color: black"&gt;Distance,
     @TestLocation.Lat &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;&lt;span style="color: black"&gt;FromLat,      @TestLocation.Long &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;&lt;span style="color: black"&gt;FromLong,
      c.Location.Lat &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;&lt;span style="color: black"&gt;ToLat,     c.Location.Long &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;&lt;span style="color: black"&gt;ToLong 
&lt;/span&gt;&lt;span style="color: blue"&gt;FROM &lt;/span&gt;&lt;span style="color: black"&gt;dbo.Cities &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;&lt;span style="color: black"&gt;c 
&lt;/span&gt;&lt;span style="color: blue"&gt;WHERE &lt;/span&gt;&lt;span style="color: black"&gt;c.Location.STDistance(@TestLocation)
               = (&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: magenta"&gt;MIN&lt;/span&gt;&lt;span style="color: black"&gt;(t.Location.STDistance(@TestLocation))
                  &lt;/span&gt;&lt;span style="color: blue"&gt;FROM &lt;/span&gt;&lt;span style="color: black"&gt;dbo.Cities AS t
                  &lt;/span&gt;&lt;span style="color: blue"&gt;WHERE &lt;/span&gt;&lt;span style="color: black"&gt;t.IsRailStop = 1
                 );
&lt;/span&gt;&lt;/pre&gt;

&lt;h2&gt;Example: Windows Mobile Application&lt;/h2&gt;

&lt;p&gt;It is easy to write .NET Compact Framework applications that talk to a Mobile GPS enabled device. At present, the Microsoft Spatial Types are not part of the Compact Framework, nor are they included in SQL CE 3.5. So how do you create a distributed mobile spatial experience?&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Optionally - Use your GPS to track where you are.&lt;/li&gt;

  &lt;li&gt;Store the Long / Lat data as floating point values in a SQL Server Compact Edition database.&lt;/li&gt;

  &lt;ul&gt;
    &lt;li&gt;Either get values from GPS or via central database.&lt;/li&gt;
  &lt;/ul&gt;

  &lt;li&gt;Use Merge Replication or Sync Services to centralise the information with SQL Server 2008 -Express (or higher edition)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;u&gt;Advantage&lt;/u&gt; &lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The Central Database can be &amp;#8220;Spatially Aware&amp;#8221;. And as nothing else changes this will work with existing infrastructure &amp;amp; applications.&lt;/li&gt;

  &lt;li&gt;Can leverage existing skills of .NET developers &amp;amp; SQL DBA&amp;#8217;s.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;Disadvantage&lt;/u&gt; &lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;No Spatially Aware calculations are done locally on the mobile device.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008WaystoarchitectSpatialapps_14DFE/MobileCE.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="181" alt="MobileCE" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008WaystoarchitectSpatialapps_14DFE/MobileCE_thumb.png" width="297" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Would this work for a commercial application? Sure, this is how &lt;a title="ArcPad is developed under license by MapTel" href="http://www.maptel.com.au/maptel/index.html" target="_blank"&gt;ESRI's ArcPad&lt;/a&gt; works &amp;amp; lots of people use that.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h1&gt;2. All Spatial processing on the Client&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Concept&lt;/u&gt;&lt;/strong&gt;: The map(s) are stored in files with the client. Mainly used for showing where points are located or thematic mapping (colouring in areas) eg: Show suburbs coloured by highest crime rate or Avg Monthly Insurance payout as a percentage of Premiums.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Handy if you aren&amp;#8217;t storing your data in a database. You can still have spatial awareness in your app. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h1&gt;3. Spatial is designed into any or all Tiers. (Client &amp;amp; Backend)&lt;/h1&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Advantage&lt;/strong&gt;&lt;/u&gt;: &lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Passing as spatial data is more network efficient than passing WKT &amp;amp; Numbers.&lt;/li&gt;

  &lt;li&gt;Take advantage of the power of SQL brilliant spatial Index&amp;#8217;s &amp;amp; the use the client to deliver a rich interactive visual experience.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;For code examples see my earlier posts :&lt;a title="Sample 1 of 9 - Hints at how to pass spatial results from Server to Client." href="http://blogs.msdn.com/davidlean/archive/2008/11/01/sql-2008-spatial-samples-part-1-of-9-how-to-learn-sql-spatial.aspx" target="_blank"&gt;SQL 2008 Samples&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Examples&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1. Plotting Data gathered by Mobile users onto a Web Display.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Using Virtual Earth is often cheaper than buying highly detailed data from a professional spatial data supplier &amp;amp; then storing &amp;amp; managing it inside SQL Server. &lt;/p&gt;

  &lt;p&gt;In this example we are capturing input from mobile &amp;amp; desktop devices then using a web interface to render the data. A potential solution for both desktops &amp;amp; the growing number of connected mobile devices (Laptops &amp;amp; Phones)&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008WaystoarchitectSpatialapps_14DFE/WebApp.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="209" alt="WebApp" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008WaystoarchitectSpatialapps_14DFE/WebApp_thumb.png" width="244" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

  &lt;p&gt;There are a ton of good articles written on using Virtual Earth with SQL Server, so I won't duplicate it here. &lt;/p&gt;

  &lt;p&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3529.entry" target="_blank"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 1: Introduction (1/2)&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3534.entry" target="_blank"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 1: Introduction (2/2)&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3541.entry" target="_blank"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 2: Spatial Data Management in SQL Server 2008 (1/3)&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3574.entry" target="_blank"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 2: Spatial Data Management in SQL Server 2008 (2/3)&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3589.entry" target="_blank"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 2: Spatial Data Management in SQL Server 2008 (3/3)&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3601.entry" target="_blank"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 3: Getting Started with Virtual Earth&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3615.entry" target="_blank"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 4: Integrating SQL Server 2008 and Virtual Earth (1/6)&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3626.entry" target="_blank"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 4: Integrating SQL Server 2008 and Virtual Earth (2/6)&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3631.entry" target="_blank"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 4: Integrating SQL Server 2008 and Virtual Earth (3/6)&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3642.entry" target="_blank"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 4: Integrating SQL Server 2008 and Virtual Earth (4/6)&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3692.entry" target="_blank"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 4: Integrating SQL Server 2008 and Virtual Earth (5/6)&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3704.entry" target="_blank"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 4: Integrating SQL Server 2008 and Virtual Earth (6/6)&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!3705.entry" target="_blank"&gt;Virtual Earth &amp;amp; SQL Server 2008 - Part 5: Conclusion&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;a href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!4504.entry" target="_blank"&gt;FindNearRoute with Virtual Earth 6.1 and SQL Server 2008&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;2. Apps for any UI, Spatial data from SQL, Web or flat file, Business data from any source.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Of course the web is for wimps, and everyone secretly aspires to be rich &amp;amp; powerful. So nothing quite matches the visual power &amp;amp; interactivity of a Rich User Interface. As you can see below, whatever the UI experience, however you want to configure your data sources, it is possible. &lt;/p&gt;

  &lt;p&gt;(I know that is thin on details. But as you can see it is just an extension of what I've been talking about in other posts &amp;amp; those I've referred to. So you should be good to go) &lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008WaystoarchitectSpatialapps_14DFE/GIS%20Apps.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="165" alt="GIS Apps" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008WaystoarchitectSpatialapps_14DFE/GIS%20Apps_thumb.png" width="244" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;Thanks&lt;/h1&gt;

&lt;p&gt;Please give feedback. Is this series of articles useful? Did it save you time? What was good, What could be better?, Notice any errors? What would you like me to cover? All thoughts, comments, suggestions welcome.&lt;/p&gt;

&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:1e8133f7-c49d-4e7a-9261-2d4613b508a9" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/SQL%20Server%20Spatial" rel="tag"&gt;SQL Server Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Spatial" rel="tag"&gt;Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Virtual%20Earth" rel="tag"&gt;Virtual Earth&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9030760" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/davidlean/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Spatial/default.aspx">Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server+Spatial/default.aspx">SQL Server Spatial</category></item><item><title>SQL 2008 Spatial Samples, Part 1 of 9 - How to Learn SQL Spatial</title><link>http://blogs.msdn.com/davidlean/archive/2008/11/01/sql-2008-spatial-samples-part-1-of-9-how-to-learn-sql-spatial.aspx</link><pubDate>Sat, 01 Nov 2008 07:40:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9027754</guid><dc:creator>davele</dc:creator><slash:comments>8</slash:comments><comments>http://blogs.msdn.com/davidlean/comments/9027754.aspx</comments><wfw:commentRss>http://blogs.msdn.com/davidlean/commentrss.aspx?PostID=9027754</wfw:commentRss><wfw:comment>http://blogs.msdn.com/davidlean/rsscomments.aspx?PostID=9027754</wfw:comment><description>&lt;p&gt;This post covers how to get started with SQL Spatial methods &amp;amp; the best way to test things for yourself.&lt;/p&gt;  &lt;p&gt;One of the goals in this series of articles is to give you code you can cut, paste &amp;amp; try. I hope you find this handy. &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:B3E14793-948F-49af-A347-D19C374A7C4F:b2b950d3-4d63-4020-b163-ed0ffa1667f8" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt; &lt;script type="text/javascript"&gt;&lt;!--
digg_bodytext = 'This post covers how to get started with SQL Spatial methods &amp;amp; the best way to test things for yourself.';
//--&gt;&lt;/script&gt; &lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;h1&gt;Overview&lt;/h1&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;u&gt;Visual Studio is better than SSMS because:&lt;/u&gt;&lt;/p&gt;    &lt;p&gt;One of the best ways to learn is to play with the product. I've found that the Intellisense support in Visual Studio (VS) is much more mature than that provided in SQL Server Management Studio (SSMS). This is especially true for the SQL Spatial Objects, where the TSQL people don't get offered a drop down list of all the valid methods (spelt &amp;amp; cased correctly) nor does it offer the parameters, their data types etc. &lt;/p&gt;    &lt;p&gt;So I'll describe how a DBA (non-Programmer) can still take advantage of &lt;abbr title="Visual Studio"&gt;VS&lt;/abbr&gt; to write TSQL queries. &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;u&gt;SSMS is better than Visual Studio because:&lt;/u&gt;&lt;/p&gt;    &lt;p&gt;But the reverse is also true. &lt;acronym title="SQL Server Management Studio"&gt;SSMS includes a Spatial Results Tab, which makes it really easy to see what you are doing with the spatial data. Much easier than in Visual Studio where you need to write your own bug free rendering routines, or use some 3rd party map control. &lt;/acronym&gt;&lt;/p&gt;    &lt;p&gt;&lt;acronym title="SQL Server Management Studio"&gt;&lt;u&gt;So use both:&lt;/u&gt;&lt;/acronym&gt;&lt;/p&gt;    &lt;p&gt;&lt;acronym title="SQL Server Management Studio"&gt;Thus both tools compliment each other really well.&lt;/acronym&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;How to do I get get SQL Spatial?&lt;/h2&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;Option 1&lt;/strong&gt;: It is included with SQL Server 2008 (all editions)       &lt;br /&gt;If you've installed any edition of SQL Server 2008, you have everything you need. Clearly SQL Server 2008 Developer Edition is the simplest, but you can download SQL Server 2008 Express &amp;amp; it works too.&lt;/p&gt;   &lt;strong&gt;Option 2&lt;/strong&gt;: Download the libraries &amp;amp; call them within your Visual Studio code. (or whatever development environment you prefer to write .Net managed code)&lt;/blockquote&gt;  &lt;h2&gt;What if I don't have any software?&lt;/h2&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;u&gt;You can get it free&lt;/u&gt;&lt;/p&gt;    &lt;p&gt;If you've installed any edition of SQL Server 2008, you have everything you need. But interestingly you don't need to have a copy of SQL Server in order to use the spatial capabilities. It is also made available as a separately installed library. It is part of the &lt;b&gt;Microsoft SQL Server System CLR Types&lt;/b&gt;&amp;#160; which are free to download &amp;amp; use. They are found on the &lt;a title="Source for the Microsoft SQL Server System CLR Types" href="http://www.microsoft.com/downloads/details.aspx?FamilyId=228DE03F-3B5A-428A-923F-58A033D316E1&amp;amp;displaylang=en"&gt;Microsoft SQL Server 2008 Feature Pack, October 2008&lt;/a&gt;. (Note: This is refreshed every 3-4 months, with new capably, utilities &amp;amp; interoperability tools. So if October 2008 was a while ago I suggest you look for the latest drop of the SQL Server feature pack. Also worth knowing that not everything in the Feature Pack is free, many items require a licensed version of SQL Server in order to be useful. Or at least a SQL Client Access License (CAL) to connect to a server.)&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;So it is possible to install just the System CLR Types &amp;amp; use the spatial data types &amp;amp; methods within objects in your program. You do not need any edition of SQL Server, just keep everything in memory. Perhaps that will be sufficient, for your use. You could even store them in a file as Binary, GML or Text if you wanted. &lt;/p&gt;    &lt;p&gt;But if you do need to persist them to disk I'd suggest at least use the Free edition of SQL, &lt;a title="Download site for the FREE edition of SQL Server 2008" href="http://www.microsoft.com/express/sql/register/"&gt;SQL Server 2008 Express&lt;/a&gt;. Not only does SQL prove to be a better way to store spatial information, it adds Spatial Indexes. These are a key reason for SQL being blindingly fast compared to other relational databases's spatial offerings. see &lt;a title="Comparison of SQL Server, Oracle, DB2, MySQL &amp;amp; PostGre" href="http://www.bostongis.com/PrinterFriendly.aspx?content_name=sqlserver2008_postgis_mysql_compare"&gt;Boston Geographic Information Systems's Cross Compare SQL Server 2008 Spatial RC0, PostgreSQL/PostGIS 1.3-1.4, MySQL 5-6&lt;/a&gt; Note: This paper was written prior to the release of SQL. While they are complementary about it in their feature comparison, they under-state its features. Unfortunately they assumed (incorrectly) that no additional capability would be available in the released product. But still they do mention it is fast. Make sure you create indexes on your spatial data, if you plan on using any of the Spatial &amp;quot;conditional&amp;quot; methods in your code ie: STDisjoint().&lt;/p&gt;    &lt;p&gt;&lt;font color="#0000ff"&gt;UPDATE: Further research shows that the &amp;quot;Spatial Results tab&amp;quot; feature didn't make it into the SQL Express SSMS tool. The Express Tools are a Web download which was not updated when we shipped SQL Server 2008 Express . The download works for both SQL 2005 &amp;amp; SQL 2008. I'm told that we plan to add this feature when we refresh the tools. I don't have a date for that, nor has anyone told me it is definitely in. So for now, the Developer Edition is really the go. Sorry.&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;Don't have a copy of Visual Studio? You can get that a free version of that here &lt;a title="Microsoft free s/w for Developers" href="http://www.microsoft.com/express/"&gt;Visual Studio Express&lt;/a&gt;.&lt;/p&gt;    &lt;p&gt;&lt;u&gt;Why would I buy it?&lt;/u&gt;&lt;/p&gt;    &lt;p&gt;SQL Server 2008 Developer Edition is ~$100. Like Visual Studio Professional or Team Suite editions it includes a lot of other tools &amp;amp; features that will make you more productive, especially if you are producing a product that needs to scale. eg: SQL Profiler, Intellisense &amp;amp; similar features that let you detect &amp;amp; correct coding errors quickly.&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;Tip: Learn SQL Spatial Methods &amp;amp; their Parameters via Visual Studio Code environment&lt;/h2&gt;  &lt;ul&gt;   &lt;li&gt;Even if not writing a program.      &lt;ul&gt;       &lt;li&gt;Use VS2008 Intellisense to suggest syntax. &lt;/li&gt;        &lt;li&gt;Paste the case sensitive Method names into TSQL queries in SSMS. &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Why?      &lt;ul&gt;       &lt;li&gt;SSMS Intellisense is of limited value.          &lt;ul&gt;           &lt;li&gt;Mostly works but:-              &lt;ul&gt;               &lt;li&gt;Doesn&amp;#8217;t suggest possible Method names. &lt;/li&gt;                &lt;li&gt;Sometimes puts Red Underlines on valid syntax &lt;/li&gt;             &lt;/ul&gt;           &lt;/li&gt;         &lt;/ul&gt;       &lt;/li&gt;        &lt;li&gt;Books Online getting better but lacks pictures &amp;amp; has minimal code examples. Which is why I wrote this series of articles. &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;So while I was learning I had a blank Visual Studio project open behind SSMS. I'd tab to it, type in a period &amp;quot;.&amp;quot; at the end of my Geography variable &amp;amp; see how to spell what I wanted &amp;amp; what its parameters are. Then I'd try it in SSMS to see what it looked like.      &lt;br /&gt;Note: Thankfully SSMS is very forgiving. It automatically uses MakeValid to display shapes that would normally cause a .NET exception. It displays the WKT string in the results tab. So sometimes poorly written queries &amp;amp;/or invalid data will cause an exception in your code. Yet SMSS manages to run them OK.&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;How to use SQL Spatial within your VB.NET or C# code.&lt;/h2&gt;  &lt;blockquote&gt;   &lt;p&gt;1. Add a Reference to &lt;strong&gt;Microsoft.SqlServer.Types&lt;/strong&gt; to your project.&lt;/p&gt;    &lt;p&gt;2. Optionally add Imports (vb) or Using (c#) statement in your code.&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; &lt;strong&gt;Imports Microsoft.SqlServer.Types&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;3. Create Variables as needed. eg: &lt;/p&gt;    &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;   Dim &lt;/span&gt;myGeom &lt;span style="color: blue"&gt;As &lt;/span&gt;SqlGeometry
&lt;span style="color: blue"&gt;   Dim &lt;/span&gt;myGeog &lt;span style="color: blue"&gt;As &lt;/span&gt;SqlGeography

&lt;span style="color: blue"&gt;   Dim &lt;/span&gt;myGmBuild &lt;span style="color: blue"&gt;As &lt;/span&gt;SqlGeometryBuilder
&lt;span style="color: blue"&gt;   Dim &lt;/span&gt;myGgBuild &lt;span style="color: blue"&gt;As &lt;/span&gt;SqlGeographyBuilder&lt;/pre&gt;

  &lt;p&gt;4. Retrieve as a DataTable (see below)&lt;/p&gt;

  &lt;p&gt;5. Use Points, or Convert to WPF or GDI+ Lines/Shapes or some 3rd party control to display &lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;h5&gt;Sample to return Geographic data type fields from SQL Server.&lt;span style="color: blue"&gt;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/h5&gt;
&lt;/blockquote&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;      Imports &lt;/span&gt;Microsoft.SqlServer.Types&lt;br /&gt;        :&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;      Dim &lt;/span&gt;dt &lt;span style="color: blue"&gt;As New &lt;/span&gt;DataTable(&lt;font color="#ff00ff"&gt;strTableName&lt;/font&gt;)
&lt;span style="color: blue"&gt;&lt;/span&gt;&lt;span style="color: green"&gt;      ' ===&amp;lt; Retrieve all PK or Unique Columns &amp;gt;=====
&lt;/span&gt;&lt;span style="color: blue"&gt;      Using &lt;/span&gt;connSQL &lt;span style="color: blue"&gt;As New &lt;/span&gt;SqlConnection(&lt;font color="#ff00ff"&gt;myConnectionString1&lt;/font&gt;)
          connSQL.Open()
          &lt;span style="color: green"&gt;'--- Get Data from SQL ---
          &lt;/span&gt;&lt;span style="color: blue"&gt;Dim &lt;/span&gt;cmd &lt;span style="color: blue"&gt;As &lt;/span&gt;SqlCommand = &lt;span style="color: blue"&gt;New &lt;/span&gt;SqlCommand(&lt;font color="#ff00ff"&gt;SQLQueryExGeo&lt;/font&gt;, connSQL)
          cmd.Connection = connSQL
          &lt;span style="color: blue"&gt;Try
              Dim &lt;/span&gt;myReader &lt;span style="color: blue"&gt;As &lt;/span&gt;SqlDataReader = cmd.ExecuteReader
              dt.Load(myReader, LoadOption.OverwriteChanges)
          &lt;span style="color: blue"&gt;Catch &lt;/span&gt;ex &lt;span style="color: blue"&gt;As &lt;/span&gt;SqlException
              &lt;span style="color: green"&gt;'TODO: Improve on this error handling
              &lt;/span&gt;MessageBox.Show(ex.Message)
          &lt;span style="color: blue"&gt;End Try
      End Using&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;blockquote&gt;
  &lt;p&gt;As you can see there is nothing different to any other SQL Query. If your display control supports it, just databind it to the datatable. eg:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;'====&amp;lt; Bind Results to Map Control &amp;gt;======
' Set the data source.
&lt;/span&gt;MapControl1.DataSource = dt&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;blockquote&gt;
  &lt;p&gt;or if you prefer, loop thru the rows in your table (dtGeo) &amp;amp; cast them to the appropriate data type.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;span style="color: green"&gt;' Load Geometry Shapes 
      &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;For &lt;/span&gt;i &lt;span style="color: blue"&gt;As Integer&lt;/span&gt;= 0 &lt;span style="color: blue"&gt;To &lt;/span&gt;dt.Rows.Count - 1 

    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;Dim &lt;/span&gt;geo &lt;span style="color: blue"&gt;As New &lt;/span&gt;SqlGeometry() 

    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; geo = &lt;span style="color: blue"&gt;CType&lt;/span&gt;( dtGeo.Rows(i).Item(GeomIndex), SqlGeometry) 

    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : 

    &lt;br /&gt;&lt;span style="color: blue"&gt;Next&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;h2&gt;Tips: For writing TSQL Queries in SQL Server Management Studio&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Declare Variables as Geometry or Geography &lt;/li&gt;

  &lt;li&gt;Initialise them with WKT values 
    &lt;ul&gt;
      &lt;li&gt;Note: New SQL 2008 syntax lets you Declare &amp;amp; &amp;quot;Set&amp;quot; in one statement. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Display them in one statement with &lt;strong&gt;UNION ALL&lt;/strong&gt;. Why? 

    &lt;ul&gt;
      &lt;li&gt;This lets you see all the shapes in the one Map. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Use UNION ALL &amp;amp; not just UNION 
    &lt;ul&gt;
      &lt;li&gt;UNION implies Duplicates be removed eg: DISTINCT. This results in an error as DISTINCT isn&amp;#8217;t currently supported with Geo Types. To explicitly compare 2 shapes you need to use the .STEquals() method. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;--===&amp;lt; Sample Code, just Paste into SQL Management Studio (SSMS) &amp;gt;===&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;--&amp;lt; Create 2 Variables &amp;amp; see them in the Spatial Results Tab &amp;gt;--
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;geometry &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))'&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@h &lt;span style="color: blue"&gt;geometry &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))'&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL &lt;span style="color: green"&gt;-- Must use the ALL keyword&lt;/span&gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@h&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;h1&gt;More info&lt;/h1&gt;

&lt;blockquote&gt;
  &lt;p&gt;For more info see SQL Server 2008 Books Online &lt;a href="http://msdn.microsoft.com/en-us/library/cc280766.aspx"&gt;Geography Spatial Type&lt;/a&gt; &amp;amp; &lt;a href="http://msdn.microsoft.com/en-us/library/cc280487.aspx"&gt;Geometry Spatial Type&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Please give feedback. Is this series of articles useful? Did it save you time? What was good, What could be better?, Notice any errors? What would you like me to cover? All thoughts, comments, suggestions welcome.&lt;/p&gt;

&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:041a202a-4194-4bba-840c-8f2089822cd5" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/SQL%20Server%20Spatial" rel="tag"&gt;SQL Server Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Spatial" rel="tag"&gt;Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/TSQL" rel="tag"&gt;TSQL&lt;/a&gt;,&lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9027754" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/davidlean/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Spatial/default.aspx">Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/TSQL/default.aspx">TSQL</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server+Spatial/default.aspx">SQL Server Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geometry/default.aspx">Geometry</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Polygon/default.aspx">Polygon</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geography/default.aspx">Geography</category></item><item><title>SQL 2008 Spatial Samples, Part 2 of 9 - Background on Spatial Types &amp; Well Known Text (WKT)</title><link>http://blogs.msdn.com/davidlean/archive/2008/11/01/sql-2008-spatial-samples-part-2-of-n-background-on-spatial-types-well-known-text-wkt.aspx</link><pubDate>Sat, 01 Nov 2008 07:34:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9027749</guid><dc:creator>davele</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/davidlean/comments/9027749.aspx</comments><wfw:commentRss>http://blogs.msdn.com/davidlean/commentrss.aspx?PostID=9027749</wfw:commentRss><wfw:comment>http://blogs.msdn.com/davidlean/rsscomments.aspx?PostID=9027749</wfw:comment><description>&lt;p&gt;This post covers all the Methods to enter data in WKT, WKB &amp;amp; XML(GML) as well as functions to view the in Human Readable form. It also covers MakeValid, STIsValid &amp;amp; STSrid.&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:B3E14793-948F-49af-A347-D19C374A7C4F:ded4b721-efa8-4353-807f-fdbf0304a946" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt; &lt;script type="text/javascript"&gt;&lt;!--
digg_bodytext = 'This post covers all the Methods to enter data in WKT, WKB &amp;amp; XML(GML) as well as functions to write out data.';
//--&gt;&lt;/script&gt; &lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h1&gt;Summary of Methods to convert Geometric Formats&lt;/h1&gt;  &lt;blockquote&gt;   &lt;p&gt;The following table shows all the Methods create or import a spatial object from; Well Known Text (WKT), Well Known Binary (WKB) or Geographic Markup Language (GML) formats. &lt;/p&gt;    &lt;table cellspacing="0" cellpadding="2" width="582" bgcolor="#ffffff" border="3"&gt;&lt;tbody&gt;       &lt;tr bgcolor="#b0c4de"&gt;         &lt;td valign="top" width="144"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="115"&gt;&lt;strong&gt;GML (XML)&lt;/strong&gt;&lt;/td&gt;          &lt;td valign="top" width="164"&gt;&lt;strong&gt;Well Known Text WKT&lt;/strong&gt;&lt;/td&gt;          &lt;td valign="top" width="153"&gt;&lt;strong&gt;Well Known Binary&lt;/strong&gt;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="144" bgcolor="#f0f8ff"&gt;Point&lt;/td&gt;          &lt;td valign="top" width="115"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="164"&gt;.ST&lt;font color="#0000ff"&gt;&lt;strong&gt;Point&lt;/strong&gt;&lt;/font&gt;From&lt;font color="#000000"&gt;Text&lt;/font&gt;()&lt;/td&gt;          &lt;td valign="top" width="153"&gt;.STPointFrom&lt;font color="#000000"&gt;WKB&lt;/font&gt;()&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="144" bgcolor="#f0f8ff"&gt;MultiPoint&lt;/td&gt;          &lt;td valign="top" width="115"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="164"&gt;.ST&lt;strong&gt;&lt;font color="#0000ff"&gt;MPoint&lt;/font&gt;&lt;/strong&gt;FromText()&lt;/td&gt;          &lt;td valign="top" width="153"&gt;.STMPointFromWKB()&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="144" bgcolor="#f0f8ff"&gt;Line&lt;/td&gt;          &lt;td valign="top" width="115"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="164"&gt;.ST&lt;font color="#0000ff"&gt;&lt;strong&gt;Line&lt;/strong&gt;&lt;/font&gt;FromText()&lt;/td&gt;          &lt;td valign="top" width="153"&gt;.STLineFromWKB()&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="144" bgcolor="#f0f8ff"&gt;MultiLine&lt;/td&gt;          &lt;td valign="top" width="115"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="164"&gt;.ST&lt;strong&gt;&lt;font color="#0000ff"&gt;MLine&lt;/font&gt;&lt;/strong&gt;FromText()&lt;/td&gt;          &lt;td valign="top" width="153"&gt;.STMLineFromWKB()&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="144" bgcolor="#f0f8ff"&gt;Polygon&lt;/td&gt;          &lt;td valign="top" width="115"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="164"&gt;.ST&lt;strong&gt;&lt;font color="#0000ff"&gt;Poly&lt;/font&gt;&lt;/strong&gt;FromText()&lt;/td&gt;          &lt;td valign="top" width="153"&gt;.STPolyFromWKB()&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="144" bgcolor="#f0f8ff"&gt;MultiPolygon&lt;/td&gt;          &lt;td valign="top" width="115"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="164"&gt;.ST&lt;strong&gt;&lt;font color="#0000ff"&gt;MPoly&lt;/font&gt;&lt;/strong&gt;FromText()&lt;/td&gt;          &lt;td valign="top" width="153"&gt;.STMPolyFromWKB()&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="144" bgcolor="#f0f8ff"&gt;Geometry&lt;/td&gt;          &lt;td valign="top" width="115"&gt;.GeomFrom&lt;strong&gt;&lt;font color="#ff8040"&gt;Gml&lt;/font&gt;&lt;/strong&gt;()&lt;/td&gt;          &lt;td valign="top" width="164"&gt;.ST&lt;font color="#0000ff"&gt;&lt;strong&gt;Geom&lt;/strong&gt;&lt;/font&gt;From&lt;strong&gt;&lt;font color="#ff8000"&gt;Text&lt;/font&gt;&lt;/strong&gt;()&lt;/td&gt;          &lt;td valign="top" width="153"&gt;.STGeomFrom&lt;font color="#ff8040"&gt;&lt;strong&gt;WKB&lt;/strong&gt;&lt;/font&gt;()&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="144" bgcolor="#f0f8ff"&gt;GeometryCollection&lt;/td&gt;          &lt;td valign="top" width="115"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="164"&gt;.ST&lt;strong&gt;&lt;font color="#0000ff"&gt;GeomColl&lt;/font&gt;&lt;/strong&gt;FromText()&lt;/td&gt;          &lt;td valign="top" width="153"&gt;.STGeomCollFromWKB()&lt;/td&gt;       &lt;/tr&gt;     &lt;/tbody&gt;&lt;/table&gt;    &lt;p&gt;The following table shows all the Methods to export a spatial object into WKT, WKB or Geographic Markup Language (GML) format.&lt;/p&gt;    &lt;table cellspacing="0" cellpadding="2" width="585" bgcolor="#ffffff" border="3"&gt;&lt;tbody&gt;       &lt;tr bgcolor="#b0c4de"&gt;         &lt;td valign="top" width="140"&gt;Display as ...&lt;/td&gt;          &lt;td valign="top" width="115"&gt;&lt;strong&gt;GML (XML)&lt;/strong&gt;&lt;/td&gt;          &lt;td valign="top" width="168"&gt;&lt;strong&gt;Well Known Text WKT&lt;/strong&gt;&lt;/td&gt;          &lt;td valign="top" width="156"&gt;&lt;strong&gt;Well Known Binary&lt;/strong&gt;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="136"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="114"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="166"&gt;.STAsText()&lt;/td&gt;          &lt;td valign="top" width="156"&gt;.STAsBinary()&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="134"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="112"&gt;.AsGml()&lt;/td&gt;          &lt;td valign="top" width="165"&gt;.AsTextZM()&lt;/td&gt;          &lt;td valign="top" width="156"&gt;&amp;#160;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="133"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="111"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="163"&gt;.ToString()&lt;/td&gt;          &lt;td valign="top" width="156"&gt;&amp;#160;&lt;/td&gt;       &lt;/tr&gt;     &lt;/tbody&gt;&lt;/table&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h1&gt;Overview - Background on &amp;#8220;What&amp;#8221; you are loading&lt;/h1&gt;  &lt;p&gt;This post &amp;amp; the next give you a background in what Spatial Data is &amp;amp; the methods available to Import/Export it.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="2"&gt;Basic Geometry Items (Points, Lines etc)&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;Methods to display Well Known Text (WKT)&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;Tips on writing WKT&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;Methods to Load Spatial Data&lt;/font&gt;       &lt;ul&gt;       &lt;li&gt;&lt;font size="2"&gt;WKT, WKB &amp;amp; GML&lt;/font&gt; &lt;/li&gt;        &lt;li&gt;&lt;font size="2"&gt;SQL Builder API&lt;/font&gt;&amp;#160; (my next post)&lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;Basic Geometry Items &lt;/h2&gt;  &lt;blockquote&gt;   &lt;p&gt;Everything is created from 3 core types; Point, Linestring &amp;amp; Polygon.      &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn6ofnBackground_11F89/GeometryTypes_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="230" alt="GeometryTypes" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn6ofnBackground_11F89/GeometryTypes_thumb.png" width="336" border="0" /&gt;&lt;/a&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;     &lt;br /&gt;Spatial objects can comprise of multiple groups of the same object included in one&amp;#160; object. These are the 3 &amp;quot;Multi-&amp;quot; Shapes. They could also be a combination of any &amp;amp; all of the shapetypes, including Multi-shape types. These are called GeometryCollections. &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;They can be converted to a human readable form called Well Known Text (WKT). I've listed all 7 types in the table below.      &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn6ofnBackground_11F89/WKT%20Examples_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="217" alt="WKT Examples" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn6ofnBackground_11F89/WKT%20Examples_thumb.png" width="347" border="0" /&gt;&lt;/a&gt;&amp;#160; &lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;Tip: Valid WKT Syntax (4 Forms)&lt;/h2&gt;  &lt;p&gt;There are 4 different syntax variations which you can use to assign Well-Known-Text to a spatial type. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: There is no performance difference between them. Other then a type check they all use the same base code. The number on the end is a Spatial Reference ID (SRID) discussed further in this post.&lt;/p&gt;  &lt;p&gt;1.&amp;#160; Precise Spatial method&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;This is OGS compliant, lets you set the SRID &amp;amp; does syntax checking to ensure that you only load the spatial type it is designed to handle. ie: STPolyFromText only handles WKT that defines a polygon.&amp;#160; See the table at the start of this post for a list of the methods &amp;amp; the specific geographic objects they handle.&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;Advantage&lt;/strong&gt;: By preventing you from accidentally loading the wrong spatial object into a table. It may reduce errors your users might experience.&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;ST&lt;strong&gt;&lt;font color="#ff00ff"&gt;Poly&lt;/font&gt;&lt;/strong&gt;FromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON( (0 0, 30 0, 30 30, 0 30, 0 0)) '&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;4326&lt;span style="color: gray"&gt;);&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;2.&amp;#160; Generic Spatial method&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;This is OGS compliant, lets you set the SRID &amp;amp; doesn't enforce a specific geometric type. Loads anything that can be in a GeometryCollection.&lt;/p&gt;

  &lt;p&gt;&lt;strong&gt;Advantage&lt;/strong&gt;: Easier to write generic code that can load any spatial object. Also handy if you are just experimenting with code &amp;amp; want to quickly try different inputs.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;ST&lt;font color="#ff00ff"&gt;&lt;strong&gt;Geom&lt;/strong&gt;&lt;/font&gt;FromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON( (0 0, 30 0, 30 30, 0 30, 0 0)) '&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;4326&lt;span style="color: gray"&gt;);&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;3. Short Format&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;This is a side effect of the routines being developed as a SQL CLR Type. They need to have a default method to initialise them &amp;amp; so use the STGeomFromText.&lt;/p&gt;

  &lt;p&gt;&lt;strong&gt;Advantage&lt;/strong&gt;: Less to type. Less clutter to read.&lt;/p&gt;

  &lt;p&gt;&lt;strong&gt;Disadvantage&lt;/strong&gt;: You can't specify the SRID. It uses the default; Geometry =0, Geography=4326. Of course you can change it later eg: SET @g.STSrid = 4120&lt;/p&gt;

  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON( (0 0, 30 0, 30 30, 0 30, 0 0) )'&lt;/span&gt;&lt;span style="color: gray"&gt;;&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;p&gt;4. Short Format - CLR form&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;This is also side effect of the routines being developed as a SQL CLR Type. They need to support a Parse Method. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Advantage&lt;/strong&gt;: Can't think of one. 

    &lt;br /&gt;&lt;strong&gt;Disadvantage&lt;/strong&gt;: You can't specify the SRID. It uses the default; Geometry =0, Geography=4326. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;Parse&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON( (0 0, 30 0, 30 30, 0 30, 0 0) )'&lt;/span&gt;&lt;span style="color: gray"&gt;);&lt;/span&gt;&amp;#160;&lt;br /&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;h2&gt;Sample of complete WKT TSQL Query&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ===&amp;lt; Creating a Single Polygon using WKT &amp;gt;===
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((0 0, 30 0, 30 30, 0 30, 0 0)) '&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;4236&lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Single Polygon'&lt;/span&gt;&lt;span style="color: gray"&gt;;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ===&amp;lt; Using data in GML format &amp;gt;===
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@x &lt;span style="color: blue"&gt;xml&lt;/span&gt;&lt;span style="color: gray"&gt;; 
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@x &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'&amp;lt;LineString xmlns=&amp;quot;http://www.opengis.net/gml&amp;quot;&amp;gt; &amp;lt;posList&amp;gt;100 100 20 180 180 180&amp;lt;/posList&amp;gt; &amp;lt;/LineString&amp;gt;'&lt;/span&gt;&lt;span style="color: gray"&gt;;
 
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;GeomFromGml&lt;span style="color: gray"&gt;(&lt;/span&gt;@x&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Imported GML'&lt;/span&gt;&lt;span style="color: gray"&gt;;&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;See other posts for more examples: Point, MultiPoint, Line, MultiLineString, MultiPolygon, etc &lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;: In SSMS if you give a column an alias you can see its name better in the Spatial Results tab's dropdown list. If you have more then one Spatial column in your result list you will need to use the dropdown list to show the other columns&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Tip: WKT Syntax - watch those parentheses!&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Put Points &amp;amp; Lines in Single Parentheses&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;p&gt;Eg: LINESTRING&lt;strong&gt;&lt;font color="#ff0000"&gt;(&lt;/font&gt;&lt;/strong&gt;10 12, 20 12&lt;strong&gt;&lt;font color="#ff0000"&gt;)&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;p&gt;Polygons have double Parentheses&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;p&gt;Eg: POLYGON&lt;font color="#ff0000"&gt;&lt;strong&gt;((&lt;/strong&gt;&lt;/font&gt; x y, ....., x y&lt;strong&gt;&lt;font color="#ff0000"&gt;))&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;
      &lt;/li&gt;

      &lt;li&gt;
        &lt;p&gt;This is because polygons can have internal &amp;#8220;holes&amp;#8221;&lt;/p&gt;

        &lt;ul&gt;
          &lt;li&gt;
            &lt;p&gt;Eg: &lt;font color="#0000ff"&gt;POLYGON&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt; &lt;strong&gt;&lt;font color="#ff0000"&gt;(&lt;/font&gt;&lt;/strong&gt;&amp;lt;outline&amp;gt;&lt;font color="#ff0000"&gt;&lt;strong&gt;), (&lt;/strong&gt;&lt;/font&gt;&amp;lt;internal hole1&amp;gt;&lt;font color="#ff0000"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;, ..., &lt;strong&gt;&lt;font color="#ff0000"&gt;(&lt;/font&gt;&lt;/strong&gt;&amp;lt;holeN&amp;gt;&lt;font color="#ff0000"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;strong&gt;&lt;font color="#0000ff"&gt;)&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;p&gt;The last Point in a Polygon must be the same as the 1st point&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;p&gt;Eg: POLYGON&lt;font color="#000000"&gt;(( &lt;font color="#ff0000"&gt;&lt;strong&gt;x y,&lt;/strong&gt;&lt;/font&gt; ....., &lt;font color="#ff0000"&gt;&lt;strong&gt;x y&lt;/strong&gt;&lt;/font&gt;))&lt;/font&gt;&lt;/p&gt;
      &lt;/li&gt;

      &lt;li&gt;
        &lt;p&gt;In RTM release only the X &amp;amp; Y values of the points are checked, the Z &amp;amp; M values can be different. This may change in a future release. So best keep Z &amp;amp; M identical too.&lt;/p&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;p&gt;The &amp;quot;Multi&amp;quot; &amp;amp; GeometryCollections require their own &amp;quot;additional&amp;quot; Parentheses&lt;/p&gt;
  &lt;/li&gt;

  &lt;ul&gt;
    &lt;li&gt;
      &lt;p&gt;&lt;font color="#0000ff"&gt;MULTI&lt;/font&gt;POINT&lt;strong&gt;&lt;font color="#ff0000"&gt;(&lt;/font&gt;&lt;/strong&gt; (0 0), (30 0) &lt;strong&gt;&lt;font color="#ff0000"&gt;)&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;
    &lt;/li&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;font color="#000000"&gt;You don't &lt;u&gt;have&lt;/u&gt; to put Parentheses around each individual point in a Multipoint object. But it does improve readability. &lt;/font&gt;&lt;/p&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/ul&gt;
&lt;/ul&gt;

&lt;h2&gt;Tip: WKT Syntax - dots &amp;amp; double dots&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;When you use a method of a Declared variable use &amp;quot;.&amp;quot; (a period or full stop)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- Sample WKT, Use '.' to call a method on a Variable.
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@Ln3 &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(1 5, 5 2)'
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@Ln3&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.2&lt;span style="color: gray"&gt;)
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@Ln3&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.2&lt;span style="color: gray"&gt;).&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;() &lt;span style="color: green"&gt;-- Methods called on other methods&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;ul&gt;
  &lt;li&gt;When you use a method on the Class use &amp;quot;::&amp;quot; (double colon)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- Sample WKT, Use '::' to call a method on the Class.
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@Ln3 &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(1 5, 5 2)'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;);&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Tip: Displaying WKT in 2, 3, &amp;amp; 4 Dimensions&lt;/h2&gt;

&lt;p&gt;All points can be 4 Dimensional (X, Y, Z &amp;amp; M(Measure). The Open Geospatial Consortium spec describes the function names with ST... . This stands for SpatioTemporal, clearly the 4th parameter M was for the Temporal or Time based property. However I've not seen any mention of any Time Dependant aspect of these functions. So you could use it's value represent whatever you want. eg: Perhaps the number of other points in a 5Km radius to this point. Or maybe the Data Mining Cluster this point belongs to.&lt;/p&gt;

&lt;p&gt;There are 3 different functions to display the Spatial Types in human readable form (Well Known Text) &lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;SELECT @g.&lt;font color="#0000ff"&gt;&lt;strong&gt;AsTextZM() &lt;/strong&gt;&lt;/font&gt;

    &lt;ul&gt;
      &lt;li&gt;Displays all 4 Dimensions. If they aren't specified or NULL, they aren't displayed. &lt;/li&gt;

      &lt;li&gt;MS Extension, Not OGC Compliant 
        &lt;br /&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;SELECT @g.&lt;strong&gt;&lt;font color="#0000ff"&gt;ToString()&lt;/font&gt;&lt;/strong&gt; 

    &lt;ul&gt;
      &lt;li&gt;Same output as .AsTextZM() &lt;/li&gt;

      &lt;li&gt;Not OGC compliant &lt;/li&gt;

      &lt;li&gt;Exists as due to .NET inheritance from the Base class 
        &lt;br /&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;&lt;font color="#0000ff"&gt;&lt;font color="#000000"&gt;SELECT @g&lt;/font&gt;.&lt;strong&gt;STAsText()&lt;/strong&gt;&lt;/font&gt; 

    &lt;ul&gt;
      &lt;li&gt;Display only the X &amp;amp; Y values &lt;/li&gt;

      &lt;li&gt;Is OGC Compliant &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;&amp;#160;&lt;span style="color: green"&gt;-- ==&amp;lt; Sample showing the 3 WKT display functions, And how Z &amp;amp; M values aren&amp;#8217;t returned unless you specifically ask&amp;gt;==
      &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;as Geometry &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POINT( 4 4 4 5 )'&lt;/span&gt;&lt;span style="color: gray"&gt;;
      &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: red"&gt;'.STAsText' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Method&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;WKT

    &lt;br /&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: red"&gt;'.AsTextZM'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;AsTextZM&lt;span style="color: gray"&gt;()
      &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: red"&gt;'.ToString'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;();
      &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;go&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

  &lt;pre&gt;&lt;span style="color: gray"&gt;&lt;u&gt;Results:&lt;/u&gt; &lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

  &lt;table cellspacing="0" cellpadding="2" width="205" bgcolor="#ffffff" border="3"&gt;&lt;tbody&gt;
      &lt;tr bgcolor="#b0c4de"&gt;
        &lt;td valign="top" width="83"&gt;&lt;strong&gt;Method&lt;/strong&gt;&lt;/td&gt;

        &lt;td valign="top" width="116"&gt;&lt;strong&gt;WKT&lt;/strong&gt;&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="86"&gt;.STAsText &lt;/td&gt;

        &lt;td valign="top" width="116"&gt;POINT (4 4)&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="89"&gt;.AsTextZM&lt;/td&gt;

        &lt;td valign="top" width="116"&gt;POINT (4 4 4 5)&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="90"&gt;.ToString &lt;/td&gt;

        &lt;td valign="top" width="116"&gt;POINT (4 4 4 5)&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;

  &lt;p&gt;Other samples for you to run. Look at the results WKT column to set how it affects the output.&amp;#160; &lt;/p&gt;

  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Samples showing how to Store &amp;amp; Retrieve 3D (Z axis) &amp;amp; 4D (Measure) values &amp;gt;==
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@p2D &lt;span style="color: blue"&gt;as Geometry &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POINT( 1 1 )'&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@p3D &lt;span style="color: blue"&gt;as Geometry &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POINT( 3 3 4 )'&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@p4D &lt;span style="color: blue"&gt;as Geometry &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POINT( 4 4 4 5 )'&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: green"&gt;-- AsTextZM doesn't display dimensions that aren't used
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@p2D&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.2&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Geo&lt;span style="color: gray"&gt;, &lt;/span&gt;@p2D&lt;span style="color: gray"&gt;.&lt;/span&gt;AsTextZM&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;[WKT 4D]
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@p3D&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.2&lt;span style="color: gray"&gt;), &lt;/span&gt;@p3D&lt;span style="color: gray"&gt;.&lt;/span&gt;AsTextZM&lt;span style="color: gray"&gt;()
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@p4D&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.2&lt;span style="color: gray"&gt;), &lt;/span&gt;@p4D&lt;span style="color: gray"&gt;.&lt;/span&gt;AsTextZM&lt;span style="color: gray"&gt;()
&lt;/span&gt;&lt;span style="color: blue"&gt;go

&lt;/span&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; 3D &amp;amp; 4D also works for Lines &amp;amp; Polygons &amp;gt;===
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@L2D &lt;span style="color: blue"&gt;as Geometry &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING( 2 2, 9 0 )'&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@L3D &lt;span style="color: blue"&gt;as Geometry &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING( 3 3 3, 9 0 3 )'&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@L4D &lt;span style="color: blue"&gt;as Geometry &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING( 4 4 4 4, 9 0 4 4 )'&lt;/span&gt;&lt;span style="color: gray"&gt;;

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@L2D &lt;span style="color: blue"&gt;as &lt;/span&gt;Geo&lt;span style="color: gray"&gt;, &lt;/span&gt;@L2D&lt;span style="color: gray"&gt;.&lt;/span&gt;AsTextZM&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;[WKT 4D]
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@L3D&lt;span style="color: gray"&gt;, &lt;/span&gt;@L3D&lt;span style="color: gray"&gt;.&lt;/span&gt;AsTextZM&lt;span style="color: gray"&gt;() 
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@L4D&lt;span style="color: gray"&gt;, &lt;/span&gt;@L4D&lt;span style="color: gray"&gt;.&lt;/span&gt;AsTextZM&lt;span style="color: gray"&gt;()

&lt;/span&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Polygon Example &amp;gt;===
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;as GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON( (0 0 9, 30 0 9, 30 30 9, 0 30 9, 0 0 9) )'&lt;/span&gt;&lt;span style="color: gray"&gt;;    
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g &lt;span style="color: blue"&gt;as &lt;/span&gt;geo&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;AsTextZM&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;[WKT 3D]
&lt;span style="color: blue"&gt;go

&lt;/span&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Valid MultiPoint String Syntax &amp;gt;==
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;as GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'MULTIPOINT(12 18, 15 15, 10 15)'
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@Br &lt;span style="color: blue"&gt;as GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'MULTIPOINT( (22 18), (25 15), (20 15))'
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@3D &lt;span style="color: blue"&gt;as GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'MULTIPOINT( (12 8 10), (15 5 5), (10 5 7))'
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@4D &lt;span style="color: blue"&gt;as GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'MULTIPOINT( (22 8 10 78), (25 5 5 43), (20 5 7 27))'

&lt;/span&gt;&lt;span style="color: green"&gt;-- Display only 2D --
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.2&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Geo&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;WKT&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'X,Y Points without Bracketing pairs' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Comment
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL 
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@Br&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.2&lt;span style="color: gray"&gt;),&lt;/span&gt;@Br&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'X,Y Points with Bracketing pairs - Preferred
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL 
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@3D&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.2&lt;span style="color: gray"&gt;),&lt;/span&gt;@3D&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'Points that store a Z Dimension'
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL 
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@4D&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.2&lt;span style="color: gray"&gt;),&lt;/span&gt;@4D&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'Points that store Z &amp;amp; M(measure) Dimensions'

&lt;/span&gt;&lt;span style="color: green"&gt;-- Display only all you can --
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.2&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Geo&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;AsTextZM&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;WKT&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'X,Y Points without Bracketing pairs' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Comment
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL 
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@Br&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.2&lt;span style="color: gray"&gt;),&lt;/span&gt;@Br&lt;span style="color: gray"&gt;.&lt;/span&gt;AsTextZM&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'X,Y Points with Bracketing pairs - Preferred
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL 
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@3D&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.2&lt;span style="color: gray"&gt;),&lt;/span&gt;@3D&lt;span style="color: gray"&gt;.&lt;/span&gt;AsTextZM&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'Points that store a Z Dimension'
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL 
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@4D&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.2&lt;span style="color: gray"&gt;),&lt;/span&gt;@4D&lt;span style="color: gray"&gt;.&lt;/span&gt;AsTextZM&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'Points that store Z &amp;amp; M(measure) Dimensions'
&lt;/span&gt;&lt;span style="color: blue"&gt;go&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;h1&gt;What about Well Known Binary &amp;amp; GML?&lt;/h1&gt;

&lt;blockquote&gt;
  &lt;p&gt;From the &amp;quot;How do I use the method?&amp;quot; perspective, WKT &amp;amp; WKB are identical. Substitute WKB for Text in the method name &amp;amp; away you go. WKB is a more efficient way to store &amp;amp; transfer shapes from one system to another. The disadvantage is that both app's need to be compliant with the Open Geospatial Specification (OGS) in order to be successful. 
    &lt;br /&gt;&amp;#160;&amp;#160; Most Web Based applications are designed around an XML based communication. For those, the &lt;a title="Defination of the GML Spec" href="http://www.opengeospatial.org/standards/gml" target="_blank"&gt;OpenGIS Geography Markup Language (GML) Encoding Standard&lt;/a&gt; may serve you better.&lt;/p&gt;

  &lt;p&gt;For more info on WKB, WKT &amp;amp; the OGS Standard for SQL v1.1 look at &lt;a title="OGS for SQL spec" href="http://www.opengeospatial.org/standards/sfs" target="_blank"&gt;OpenGIS Implementation Specification for Geographic information - Simple feature access - Part 2: SQL option&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&amp;#160;&lt;/p&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;h1&gt;What is a Valid Shape?&lt;/h1&gt;

&lt;p&gt;SQL lets you store &amp;amp; retrieve any combination of points to make any geometry you like. But if you do create some convoluted shape which twists &amp;amp; turns over itself, it is often impossible to determine what you've described. So most of the methods will fail. A polygon that crosses over itself is really a multipolygon containing polygons which just touch each other. A Line which draws over itself is really a multilinestring. &lt;/p&gt;

&lt;p&gt;&lt;u&gt;Key Points&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;ul&gt;&lt;/ul&gt;

  &lt;li&gt;
    &lt;p&gt;A Shape is valid where it does NOT cross over itself&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;p&gt;Be diligent, when you create shapes, ensure that you are forming them correctly, especially geography shapes which do not have a MakeValid Method.&lt;/p&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;p&gt;You can only perform calculations on Valid Shapes.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Method &lt;font color="#ff0000"&gt;.STIsValid&lt;/font&gt;()&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Test for a valid shape. (as described above)&lt;/li&gt;

  &lt;li&gt;Works with &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#800040"&gt;Geography&lt;/font&gt; Objects. &lt;/li&gt;

  &lt;li&gt;Companion method of .MakeValid Method. &lt;/li&gt;

  &lt;li&gt;Returns Boolean: 0 = NotValid, 1 = Valid &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Note: Invalid shapes are a common cause of .NET Framework exceptions when working with Geometry types in .NET code. They result in the error message. 
    &lt;br /&gt;&lt;font color="#ff0000"&gt;Msg 6522, Level 16, State 1, Line 2 
      &lt;br /&gt;A .NET Framework error occurred during execution of user-defined routine or aggregate &amp;quot;geometry&amp;quot;: 

      &lt;br /&gt;System.ArgumentException: 24144: This operation cannot be completed because the instance is not valid. 

      &lt;br /&gt;Use MakeValid to convert the instance to a valid instance. 

      &lt;br /&gt;Note that MakeValid may cause the points of a geometry instance to shift slightly.&lt;/font&gt;&lt;/p&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example use: Alter Geometry shapes so they &amp;quot;make sense&amp;quot;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;Method &lt;font color="#ff0000"&gt;.MakeValid&lt;/font&gt;()&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Adjust the definition of a shape to make it valid. 
    &lt;ul&gt;
      &lt;li&gt;Note: MakeValid may cause the points of a geometry instance to shift slightly. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Works with &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#800040"&gt;Geography&lt;/font&gt; Objects. &lt;/li&gt;

  &lt;li&gt;Companion method of .STIsValid Method. &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example use: Alter Geometry shapes so they &amp;quot;make sense&amp;quot;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;--&amp;lt; Sample: Test if a Shape or Line traced back over itself - not Valid &amp;gt;--
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(0 9, 1 8, 2 6, 2 2, 1 1, 3 1, 2 2, 2 6, 3 8, 4 9)'
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Invalid String'
      &lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STIsValid&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'STIsValid'
      &lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;&lt;span style="color: blue"&gt;CASE &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STIsValid&lt;span style="color: gray"&gt;() 
            &lt;/span&gt;&lt;span style="color: blue"&gt;WHEN &lt;/span&gt;0 &lt;span style="color: blue"&gt;THEN &lt;/span&gt;&lt;span style="color: red"&gt;'Not Valid' 
            &lt;/span&gt;&lt;span style="color: blue"&gt;WHEN &lt;/span&gt;1 &lt;span style="color: blue"&gt;THEN &lt;/span&gt;&lt;span style="color: red"&gt;'Valid' 
       &lt;/span&gt;&lt;span style="color: blue"&gt;END As &lt;/span&gt;&lt;span style="color: red"&gt;'Is Valid?'

&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;MakeValid&lt;span style="color: gray"&gt;()    &lt;/span&gt;&lt;span style="color: green"&gt;-- Make it valid
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Valid String'
    &lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STIsValid&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'STIsValid'
    &lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;&lt;span style="color: blue"&gt;CASE &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STIsValid&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;WHEN &lt;/span&gt;0 &lt;span style="color: blue"&gt;THEN &lt;/span&gt;&lt;span style="color: red"&gt;'Not Valid' &lt;/span&gt;&lt;span style="color: blue"&gt;WHEN &lt;/span&gt;1 &lt;span style="color: blue"&gt;THEN &lt;/span&gt;&lt;span style="color: red"&gt;'Valid' &lt;/span&gt;&lt;span style="color: blue"&gt;END As &lt;/span&gt;&lt;span style="color: red"&gt;'Is Valid?'&lt;/span&gt;&lt;/pre&gt;

  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn6ofnBackground_11F89/MakeValid_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="352" alt="MakeValid" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn6ofnBackground_11F89/MakeValid_thumb.png" width="203" border="0" /&gt;&lt;/a&gt;&amp;#160;&amp;#160; &lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn6ofnBackground_11F89/MadeValid_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="355" alt="MadeValid" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn6ofnBackground_11F89/MadeValid_thumb.png" width="199" border="0" /&gt;&lt;/a&gt; 

    &lt;br /&gt;Left: Invalid Line Shape: LINESTRING (0 9, 1 8, &lt;font color="#0000ff"&gt;&lt;strong&gt;2 6, 2 2&lt;/strong&gt;,&lt;/font&gt; 1 1, 3 1, &lt;font color="#0000ff"&gt;&lt;strong&gt;2 2, 2 6,&lt;/strong&gt;&lt;/font&gt; 3 8, 4 9)&lt;/p&gt;

  &lt;p&gt;Right : Valid Line Shape:&amp;#8211;MULTILINESTRING ((&lt;font color="#ff0000"&gt;&lt;strong&gt;0 9, 1 8, 2 6, 3 8, 4 9&lt;/strong&gt;&lt;/font&gt;) ,(&lt;strong&gt;&lt;font color="#008040"&gt;2 6, 2 2&lt;/font&gt;&lt;/strong&gt;), (&lt;font color="#8000ff"&gt;&lt;strong&gt;1 1, 3 1, 2 2, 1 1&lt;/strong&gt;&lt;/font&gt;))&lt;/p&gt;

  &lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;What is a SRID (Spatial Reference ID)?&lt;/h1&gt;

&lt;blockquote&gt;
  &lt;p&gt;The spatial reference identification system is defined by the &lt;a title="OGP Surveying and Positioning Committee" href="http://www.epsg.org/" target="_blank"&gt;European Petroleum Survey Group (EPSG) standard&lt;/a&gt;, which is a set of standards developed for cartography, surveying, and geodetic data storage. This standard is owned by the Oil and Gas Producers (OGP) Surveying and Positioning Committee. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Every spatial object has a Spatial Reference ID (SRID)&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;p&gt;The SRID defines the coordinate system and datum&lt;/p&gt;
      &lt;/li&gt;

      &lt;li&gt;
        &lt;p&gt;Each object can have a different SRID, but usually doesn&amp;#8217;t (difficult to work with)&lt;/p&gt;
      &lt;/li&gt;

      &lt;li&gt;
        &lt;p&gt;Operation between objects require them to have the same SRID&lt;/p&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;p&gt;To see what SRIDs are supported :&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;span style="color: green"&gt;-- &amp;lt; List of Spatial Reference Systems supported in SQL 2008 &amp;gt;-- 
            &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: gray"&gt;* &lt;/span&gt;&lt;span style="color: blue"&gt;FROM &lt;/span&gt;&lt;span style="color: green"&gt;SYS&lt;/span&gt;&lt;span style="color: gray"&gt;.&lt;/span&gt;&lt;span style="color: green"&gt;SPATIAL_REFERENCE_SYSTEMS&lt;/span&gt;&lt;/p&gt;
        &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;p&gt;Most common SRID for Geography is 4326 (also referred to as WGS84)&lt;/p&gt;
  &lt;/li&gt;

  &lt;li&gt;
    &lt;p&gt;The Default SRID for geometry is 0&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;For a more background on SRID's see: &lt;a title="Article written by Ed Katibah &amp;amp; I" href="http://en.wikipedia.org/wiki/SRID" target="_blank"&gt;Wikipedia - SRID&lt;/a&gt; article I posted with the assistance of &lt;a title="Ed Katibah&amp;#39;s blog" href="http://blogs.msdn.com/edkatibah/"&gt;Ed Katibah&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;Property &lt;font color="#ff0000"&gt;.STSrid&lt;/font&gt;&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Read or Change the SRID of a spatial object. 
    &lt;ul&gt;
      &lt;li&gt;Think before changing the SRID of an existing object, understand what it means, as it is typically not a good idea. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h1&gt;More info&lt;/h1&gt;

&lt;blockquote&gt;
  &lt;p&gt;For more info see SQL Server 2008 Books Online &lt;a href="http://msdn.microsoft.com/en-us/library/cc280766.aspx"&gt;Geography Spatial Type&lt;/a&gt; &amp;amp; &lt;a href="http://msdn.microsoft.com/en-us/library/cc280487.aspx"&gt;Geometry Spatial Type&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Please give feedback. Is this series of articles useful? Did it save you time? What was good, What could be better?, Notice any errors? What would you like me to cover? All thoughts, comments, suggestions welcome.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:da61b0d4-9eb2-432a-9666-bc0842dd1a85" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/SQL%20Server%20Spatial" rel="tag"&gt;SQL Server Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Spatial" rel="tag"&gt;Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/TSQL" rel="tag"&gt;TSQL&lt;/a&gt;,&lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SRID" rel="tag"&gt;SRID&lt;/a&gt;,&lt;a href="http://technorati.com/tags/MakeValid" rel="tag"&gt;MakeValid&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9027749" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/davidlean/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Spatial/default.aspx">Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/TSQL/default.aspx">TSQL</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server+Spatial/default.aspx">SQL Server Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geometry/default.aspx">Geometry</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Polygon/default.aspx">Polygon</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geography/default.aspx">Geography</category></item><item><title>SQL 2008 Spatial Samples, Part 3 of 9 - SQL Builder API</title><link>http://blogs.msdn.com/davidlean/archive/2008/10/30/sql-2008-spatial-samples-part-n-5-of-n-sql-builder-api.aspx</link><pubDate>Thu, 30 Oct 2008 09:25:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9023775</guid><dc:creator>davele</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/davidlean/comments/9023775.aspx</comments><wfw:commentRss>http://blogs.msdn.com/davidlean/commentrss.aspx?PostID=9023775</wfw:commentRss><wfw:comment>http://blogs.msdn.com/davidlean/rsscomments.aspx?PostID=9023775</wfw:comment><description>&lt;p&gt;This post covers the &lt;strong&gt;SqlGeographyBuilder&lt;/strong&gt; &amp;amp; &lt;strong&gt;SqlGeometryBuilder&lt;/strong&gt; Classes &amp;amp; their methods. &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:B3E14793-948F-49af-A347-D19C374A7C4F:bef2bbd6-27a0-43f1-b682-c05fdd72ff14" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt; &lt;script type="text/javascript"&gt;&lt;!--
digg_bodytext = 'This post covers the following methods: Reduce, STEnvelope, EnvelopeCenter, EnvelopeAngle, STConvexHull.';
//--&gt;&lt;/script&gt; &lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;h1&gt;Overview of Methods to make your own Spatial Objects using the Builder API.&lt;/h1&gt;  &lt;p&gt;SQL provides a Builder API that permit you to easily write your own Spatial Data Importing utilities. It is also handy manipulating your shapes in a way that is not supported out of the box, perhaps writing your own CLR (Common Language Runtime) extension to SQL. Also handy for generating test data (especially points), reverse geo-coding &amp;amp; similar applications.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;The methods for Geography &amp;amp; Geometry are identical. ( Except that they target a different datatype &amp;amp; have a name change that reflects that, which I've highlighted in bold. )&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;table cellspacing="0" cellpadding="2" width="440" bgcolor="#ffffff" border="3"&gt;&lt;tbody&gt;       &lt;tr bgcolor="#b0c4de"&gt;         &lt;td valign="top" width="110"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="159"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/td&gt;          &lt;td valign="top" width="165"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="110"&gt;&lt;font color="#0080c0"&gt;&lt;strong&gt;&lt;u&gt;Classes&lt;/u&gt;&lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;          &lt;td valign="top" width="164"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="165"&gt;&amp;#160;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="109"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="168"&gt;.Sql&lt;strong&gt;Geometry&lt;/strong&gt;Builder&lt;/td&gt;          &lt;td valign="top" width="165"&gt;.Sql&lt;strong&gt;Geography&lt;/strong&gt;Builder&lt;/td&gt;       &lt;/tr&gt;        &lt;tr bgcolor="#f5f5f5"&gt;         &lt;td valign="top" width="109"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="171"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="165"&gt;&amp;#160;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="108"&gt;&lt;font color="#0080c0"&gt;&lt;strong&gt;&lt;u&gt;Methods&lt;/u&gt;&lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;          &lt;td valign="top" width="174"&gt;&lt;font color="#000000"&gt;&amp;#160;&lt;/font&gt;&lt;/td&gt;          &lt;td valign="top" width="165"&gt;&lt;font color="#000000"&gt;&amp;#160;&lt;/font&gt;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="107"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="176"&gt;.SetSrid() &lt;/td&gt;          &lt;td valign="top" width="165"&gt;.SetSrid() &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="108"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="177"&gt;&lt;font color="#000000"&gt;.Begin&lt;strong&gt;Geometry&lt;/strong&gt;()               &lt;br /&gt; / .End&lt;strong&gt;Geometry&lt;/strong&gt;()&lt;/font&gt;&lt;/td&gt;          &lt;td valign="top" width="165"&gt;.Begin&lt;strong&gt;Geography&lt;/strong&gt;()&amp;#160; &lt;br /&gt; / .End&lt;strong&gt;Geography&lt;/strong&gt;() &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="107"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="178"&gt;.BeginFigure()&amp;#160; &lt;br /&gt; / .EndFigure() &lt;/td&gt;          &lt;td valign="top" width="165"&gt;.BeginFigure()           &lt;br /&gt; / .EndFigure() &lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="107"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="179"&gt;.AddLine() &lt;/td&gt;          &lt;td valign="top" width="165"&gt;.AddLine() &lt;/td&gt;       &lt;/tr&gt;        &lt;tr bgcolor="#f5f5f5"&gt;         &lt;td valign="top" width="106"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="180"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="165"&gt;&amp;#160;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="107"&gt;&lt;font color="#0080c0"&gt;Return&lt;/font&gt;&lt;/td&gt;          &lt;td valign="top" width="180"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="165"&gt;&amp;#160;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="107"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="180"&gt;&lt;font color="#000000"&gt;.Constructed&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;          &lt;td valign="top" width="165"&gt;.Constructed&lt;strong&gt;Geography&lt;/strong&gt;&lt;/td&gt;       &lt;/tr&gt;     &lt;/tbody&gt;&lt;/table&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;Table of methods covered in this post. Fortunately the methods &amp;amp; classes are fairly self explanatory. So I hope you can just cut n paste the samples into your VB.NET code &amp;amp; run them. For C# put a &amp;quot;;&amp;quot; on the end of each line. ;-). NB: Don't forget to add a reference to Microsoft.SqlServer.Types. As mentioned in an earlier post - Learning SQL Spatial&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h4&gt;Main Steps&lt;/h4&gt;  &lt;blockquote&gt;   &lt;p&gt;1.&amp;#160;&amp;#160; Decide what spatial type you wish to create (Geometry / Geography) &amp;amp; declare an instance of the appropriate &amp;quot;Builder&amp;quot; &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Dim&lt;/font&gt; gb &lt;font color="#0000ff"&gt;As New&lt;/font&gt; SqlGeographyBuilder&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;2.&amp;#160;&amp;#160; Set the Spatial Reference ID (SRID). This &lt;strong&gt;must&lt;/strong&gt; be the first method you call. &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; gb.&lt;strong&gt;SetSrid&lt;/strong&gt;(4326)&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;3.&amp;#160;&amp;#160; Create Geographies. Start with BeginGeography &amp;amp; indicate the type of geometry you are creating (point, line, polygon) then close with matching EndGeography. &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;4.&amp;#160;&amp;#160; Create one or more shapes inside it with a BeginFigure ... EndFigure pair. Leaving these out will create an EMPTY Geography. BeginFigure also sets the StartPoint for the shape. &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;5.&amp;#160;&amp;#160; For lines &amp;amp; polygons, use AddLine to additional points with lines connecting them. &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;6.&amp;#160;&amp;#160; When you are done use the ConstructedGeography method to pass the shape to a SQLGeometry or SQLGeography variable. &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;Below are a few code samples showing how to create each of the different shapes. &lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;Sample 1a: Building a Geography Point&lt;/h2&gt;  &lt;blockquote&gt;   &lt;p&gt;This example shows declaring the SqlGeographyBuilder, setting the SRID to WGS 84, Creating a Geography Pair &amp;amp; adding one Figure pair to specify the point. &lt;/p&gt;   &lt;strong&gt;Note&lt;/strong&gt;: For the Geography datatype, the order of Latitude &amp;amp; Longitude coordinates are swapped. Effectively this makes the co-ordinate (Y,X). It is consistent with the way maritime sailors have been readn' maps for centuries, &amp;quot;so get used to it, ya scurvy dog!&amp;quot;. Yes, it is opposite to the WKT format, see output below.&lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;Dim gb As New &lt;strong&gt;&lt;font color="#ff0000"&gt;SqlGeographyBuilder&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; gb.&lt;strong&gt;&lt;font color="#008040"&gt;SetSrid&lt;/font&gt;&lt;/strong&gt;(4326) &lt;font color="#008040"&gt;'Must set First&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; gb.&lt;font color="#8000ff"&gt;&lt;strong&gt;BeginGeography&lt;/strong&gt;&lt;/font&gt;(OpenGisGeographyType.&lt;strong&gt;&lt;font color="#ff0000"&gt;Point&lt;/font&gt;&lt;/strong&gt;)&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; gb.&lt;font color="#0000ff"&gt;BeginFigure&lt;/font&gt;(-33, 151)&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; gb.&lt;font color="#0000ff"&gt;EndFigure&lt;/font&gt;()&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; gb.&lt;font color="#8000ff"&gt;&lt;strong&gt;EndGeography&lt;/strong&gt;&lt;/font&gt;()&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;Dim geo As New &lt;strong&gt;&lt;font color="#ff0000"&gt;SqlGeography&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; geo = gb.&lt;font color="#0000ff"&gt;ConstructedGeography&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; geo.ToString()&lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;&lt;u&gt;The line below shows what is displayed in the Visual Studio debugger window.&lt;/u&gt; &lt;/p&gt;    &lt;p&gt;&amp;gt;? geo.ToString()&lt;/p&gt;    &lt;p&gt;&amp;quot;POINT (151 -33)&amp;quot;&lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;Sample 1b: Building a Geometry Point&lt;/h2&gt;  &lt;blockquote&gt;   &lt;p&gt;For Geometry objects all &amp;quot;geography&amp;quot; methods change to &amp;quot;Geometry&amp;quot; except the &lt;strong&gt;OpenGisGeographyType&lt;/strong&gt; enumeration.&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;Dim gb As New SqlGeometryBuilder      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; gb.SetSrid(4326) 'Must set First       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; gb.&lt;font color="#ff00ff"&gt;&lt;strong&gt;BeginGeometry&lt;/strong&gt;&lt;/font&gt;(&lt;strong&gt;OpenGisGeographyType&lt;/strong&gt;.Point)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; gb.BeginFigure(-33, 151)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; gb.EndFigure()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; gb.&lt;font color="#ff00ff"&gt;&lt;strong&gt;EndGeometry&lt;/strong&gt;&lt;/font&gt;() &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;Dim geo As New &lt;strong&gt;&lt;font color="#ff0080"&gt;SqlGeometry&lt;/font&gt;         &lt;br /&gt;&lt;/strong&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; geo = gb.&lt;strong&gt;&lt;font color="#ff00ff"&gt;ConstructedGeometry&lt;/font&gt;         &lt;br /&gt;&lt;/strong&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; geo.ToString()&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;Sample 2: Building a Polygon&lt;/h2&gt;  &lt;blockquote&gt;   &lt;p&gt;This example extends the one above by adding lines to create a polygon.&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;Dim g As New SqlGeographyBuilder&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g.SetSrid(4326)&amp;#160;&amp;#160; &lt;font color="#008040"&gt;'&amp;lt;= Must set First&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g.BeginGeography(OpenGisGeographyType.&lt;strong&gt;&lt;font color="#ff0000"&gt;Polygon&lt;/font&gt;&lt;/strong&gt;)&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g.&lt;font color="#0000ff"&gt;BeginFigure&lt;/font&gt;(&lt;font color="#ff8040"&gt;&lt;strong&gt;-33, 151&lt;/strong&gt;&lt;/font&gt;)&amp;#160;&amp;#160; &lt;font color="#008000"&gt;&amp;#8216;Note: Lat, Long format&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g.&lt;font color="#ff00ff"&gt;&lt;strong&gt;AddLine&lt;/strong&gt;&lt;/font&gt;(-31, 152)&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g.&lt;font color="#ff00ff"&gt;&lt;strong&gt;AddLine&lt;/strong&gt;&lt;/font&gt;(-30, 152)&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g.&lt;font color="#ff00ff"&gt;&lt;strong&gt;AddLine&lt;/strong&gt;&lt;/font&gt;(&lt;font color="#ff8040"&gt;&lt;strong&gt;-33, 151&lt;/strong&gt;&lt;/font&gt;)&amp;#160;&amp;#160; &lt;font color="#008000"&gt;&amp;#8216;Note: Last Point same as First&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g.&lt;font color="#0000ff"&gt;EndFigure&lt;/font&gt;()&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g.EndGeography()&lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;Dim geo As New &lt;strong&gt;SqlGeography&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; geo = g.ConstructedGeography&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; geo.ToString()&lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;&lt;u&gt;The line below shows what is displayed in the Visual Studio debugger window.&lt;/u&gt;&lt;/p&gt;    &lt;p&gt;&amp;gt;? geo.ToString()      &lt;br /&gt;&amp;quot;POLYGON ((151 -33, 152 -31, 152 -30, 151 -33))&amp;quot;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h2&gt;Sample 3: Building a Complex Polygon&lt;/h2&gt;  &lt;blockquote&gt;   &lt;p&gt;This example extends the one above by adding extra figures to the geometry&lt;/p&gt;    &lt;br /&gt;&lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;Dim g As New SqlGeographyBuilder      &lt;br /&gt;g.SetSrid(4326)&amp;#160; &lt;font color="#008000"&gt;'&amp;lt;= Must set First        &lt;br /&gt;&lt;/font&gt;g.&lt;font color="#8000ff"&gt;BeginGeography&lt;/font&gt;(OpenGisGeographyType.&lt;strong&gt;&lt;font color="#ff0000"&gt;Polygon&lt;/font&gt;&lt;/strong&gt;) &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;&lt;font color="#008000"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ' Exterior shape&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g.&lt;strong&gt;&lt;font color="#0000ff"&gt;BeginFigure&lt;/font&gt;&lt;/strong&gt;(-33, 151)&amp;#160;&amp;#160; 'Note: Lat, Long format     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g.AddLine(-33, 154)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g.AddLine(-30, 154)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g.AddLine(-33, 151)&amp;#160;&amp;#160; 'Note: Last Point same as First     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g.&lt;strong&gt;&lt;font color="#0000ff"&gt;EndFigure&lt;/font&gt;&lt;/strong&gt;() &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;' Interior &amp;quot;Enclosed&amp;quot; shape        &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g.&lt;strong&gt;&lt;font color="#0000ff"&gt;BeginFigure&lt;/font&gt;&lt;/strong&gt;(-32.5, 152)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g.AddLine(-31, 153.5)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g.AddLine(-32.5, 153)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g.AddLine(-32.5, 152)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; g.&lt;strong&gt;&lt;font color="#0000ff"&gt;EndFigure&lt;/font&gt;&lt;/strong&gt;()       &lt;br /&gt;g.&lt;font color="#8000ff"&gt;EndGeography&lt;/font&gt;() &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;Dim geo As New &lt;strong&gt;SqlGeography&lt;/strong&gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; geo = g.ConstructedGeography       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; geo.ToString()&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;u&gt;The line below shows what is displayed in the Visual Studio debugger window.&lt;/u&gt;&lt;/p&gt;    &lt;p&gt;&amp;gt;? geo.ToString()      &lt;br /&gt;&amp;quot;POLYGON ((151 -33, 154 -33, 154 -30, 151 -33), (152 -32.5, 153.5 -31, 153 -32.5, 152 -32.5))&amp;quot;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;Sample 4: Building a MultiPoint shape&lt;/h2&gt;  &lt;blockquote&gt;   &lt;p&gt;Note: BeginGeography for Point nested inside BeginGeography for Multipoint.&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;Dim b As New SqlGeographyBuilder      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.SetSrid(4326) 'Must set 1st       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.&lt;strong&gt;&lt;font color="#8000ff"&gt;BeginGeography&lt;/font&gt;&lt;/strong&gt;(OpenGisGeographyType.&lt;strong&gt;&lt;font color="#ff0000"&gt;MultiPoint&lt;/font&gt;&lt;/strong&gt;) &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.&lt;strong&gt;&lt;font color="#8000ff"&gt;BeginGeography&lt;/font&gt;&lt;/strong&gt;(OpenGisGeographyType.&lt;strong&gt;&lt;font color="#ff0000"&gt;Point&lt;/font&gt;&lt;/strong&gt;)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.BeginFigure(-33, 151)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.EndFigure()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.&lt;strong&gt;&lt;font color="#8000ff"&gt;EndGeography&lt;/font&gt;&lt;/strong&gt;() &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.&lt;strong&gt;&lt;font color="#000000"&gt;BeginGeography&lt;/font&gt;&lt;/strong&gt;(OpenGisGeographyType.&lt;strong&gt;&lt;font color="#ff0000"&gt;Point&lt;/font&gt;&lt;/strong&gt;)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.BeginFigure(-33, 155)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.EndFigure()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.&lt;strong&gt;&lt;font color="#000000"&gt;EndGeography&lt;/font&gt;&lt;/strong&gt;() &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.&lt;strong&gt;BeginGeography&lt;/strong&gt;(OpenGisGeographyType.&lt;strong&gt;&lt;font color="#ff0000"&gt;Point&lt;/font&gt;&lt;/strong&gt;)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.BeginFigure(-32, 153)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.EndFigure()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.&lt;strong&gt;EndGeography&lt;/strong&gt;() &lt;/p&gt;    &lt;p&gt;b.&lt;strong&gt;&lt;font color="#8000ff"&gt;EndGeography&lt;/font&gt;&lt;/strong&gt;() &lt;/p&gt;    &lt;p&gt;Dim geo As New &lt;strong&gt;SqlGeography&lt;/strong&gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; geo = b.ConstructedGeography       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; geo.ToString()&lt;/p&gt;    &lt;p&gt;&lt;u&gt;The line below shows what is displayed in the Visual Studio debugger window.&lt;/u&gt;&lt;/p&gt;    &lt;p&gt;&amp;gt;? geo.ToString()      &lt;br /&gt;&amp;quot;MULTIPOINT ((151 -33), (155 -33), (153 -32))&amp;quot;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;Sample 5: Building a MultiPolygon&lt;/h2&gt;  &lt;blockquote&gt;   &lt;p&gt;Two polygons where one is not contained inside the other, nor do they overlap each other. &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="color: blue"&gt;Dim &lt;/span&gt;b &lt;span style="color: blue"&gt;As New &lt;/span&gt;SqlGeographyBuilder       &lt;br /&gt;b.SetSrid(4326) &lt;span style="color: green"&gt;'Must set 1st        &lt;br /&gt;&lt;/span&gt;b.&lt;strong&gt;BeginGeography&lt;/strong&gt;(OpenGisGeographyType.&lt;font color="#ff0000"&gt;&lt;strong&gt;MultiPolygon&lt;/strong&gt;&lt;/font&gt;)       &lt;br /&gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; b.&lt;strong&gt;BeginGeography&lt;/strong&gt;(OpenGisGeographyType.&lt;strong&gt;&lt;font color="#ff0000"&gt;Polygon&lt;/font&gt;&lt;/strong&gt;)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.BeginFigure(-33, 151)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.AddLine(-31, 152)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.AddLine(-30, 152)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.AddLine(-33, 151)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.EndFigure()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; b.&lt;strong&gt;EndGeography&lt;/strong&gt;()       &lt;br /&gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; b.&lt;strong&gt;BeginGeography&lt;/strong&gt;(OpenGisGeographyType.&lt;strong&gt;&lt;font color="#ff0000"&gt;Polygon&lt;/font&gt;&lt;/strong&gt;)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.BeginFigure(-33, 155)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.AddLine(-31, 156)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.AddLine(-30, 156)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.AddLine(-33, 155)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.EndFigure()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; b.&lt;strong&gt;EndGeography&lt;/strong&gt;()       &lt;br /&gt;b.&lt;strong&gt;EndGeography&lt;/strong&gt;()       &lt;br /&gt;      &lt;br /&gt;&lt;span style="color: blue"&gt;Dim &lt;/span&gt;geo &lt;span style="color: blue"&gt;As New &lt;/span&gt;SqlGeography       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; geo = b.ConstructedGeography       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; geo.ToString()&lt;/p&gt;   &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;u&gt;The line below shows what is displayed in the Visual Studio debugger window.&lt;/u&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;gt;? geo.ToString&lt;/p&gt;    &lt;p&gt;&amp;quot;MULTIPOLYGON (((151 -33, 152 -31, 152 -30, 151 -33)), ((155 -33, 156 -31, 156 -30, 155 -33)))&amp;#8220;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;Sample 6: Building a Building a GeometryCollection&lt;/h2&gt;  &lt;blockquote&gt;   &lt;p&gt;OpenGisGeographyType has the 7 types; Point, LineString, Polygon &amp;amp; their Multi variants, use them in whatever combination you desire. &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;Dim b As New SqlGeographyBuilder &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;b.SetSrid(4326) 'Must set 1st      &lt;br /&gt;b.&lt;strong&gt;BeginGeography&lt;/strong&gt;(OpenGisGeographyType.&lt;strong&gt;&lt;font color="#ff0000"&gt;GeometryCollection&lt;/font&gt;&lt;/strong&gt;) &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.&lt;strong&gt;BeginGeography&lt;/strong&gt;(OpenGisGeographyType.&lt;strong&gt;&lt;font color="#ff0000"&gt;LineString&lt;/font&gt;&lt;/strong&gt;)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.BeginFigure(-33, 151)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.AddLine(-31, 152)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.AddLine(-30, 152)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.AddLine(-33, 151)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.EndFigure()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.EndGeography() &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.&lt;strong&gt;BeginGeography&lt;/strong&gt;(OpenGisGeographyType.&lt;strong&gt;&lt;font color="#ff0000"&gt;Polygon&lt;/font&gt;&lt;/strong&gt;)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.BeginFigure(-33, 155)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.AddLine(-31, 156)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.AddLine(-30, 156)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.AddLine(-33, 155)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.EndFigure()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.EndGeography() &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.&lt;strong&gt;BeginGeography&lt;/strong&gt;(OpenGisGeographyType.&lt;strong&gt;&lt;font color="#ff0000"&gt;Point&lt;/font&gt;&lt;/strong&gt;)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.BeginFigure(-32, 153)       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.EndFigure()       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; b.EndGeography()       &lt;br /&gt;b.EndGeography() &lt;/p&gt;    &lt;p&gt;Dim geo As New &lt;strong&gt;SqlGeography&lt;/strong&gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; geo = b.ConstructedGeography       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; geo.ToString()&lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;&lt;u&gt;The line below shows what is displayed in the Visual Studio debugger window.&lt;/u&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;gt;? geo.ToString&lt;/p&gt;    &lt;p&gt;GEOMETRYCOLLECTION (LINESTRING (151 -33, 152 -31, 152 -30, 151 -33), POLYGON ((155 -33, 156 -31, 156 -30, 155 -33)), POINT (153 -32))&lt;/p&gt; &lt;/blockquote&gt;  &lt;h1&gt;More info&lt;/h1&gt;  &lt;blockquote&gt;   &lt;blockquote&gt;     &lt;p&gt;For more info see SQL Server 2008 Books Online. Might have to wait for the December 08 Update to Books Online as I've not found any mention of these Classes in the Aug 08 release. &lt;/p&gt;   &lt;/blockquote&gt;    &lt;p&gt;Please give feedback. Is this series of articles useful? Did it save you time? What was good, What could be better?, Notice any errors? What would you like me to cover? All thoughts, comments, suggestions welcome.&lt;/p&gt; &lt;/blockquote&gt;  &lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:d77e2ff7-571b-41b2-aa24-b3b540e74b9e" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Spatial" rel="tag"&gt;Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SQL%20Server%20Spatial" rel="tag"&gt;SQL Server Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SQL%20Server" rel="tag"&gt;SQL Server&lt;/a&gt;,&lt;a href="http://technorati.com/tags/TSQL" rel="tag"&gt;TSQL&lt;/a&gt;,&lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SQLGeometryBuilder" rel="tag"&gt;SQLGeometryBuilder&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SQLGeographyBuilder" rel="tag"&gt;SQLGeographyBuilder&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Geometry" rel="tag"&gt;Geometry&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Geography" rel="tag"&gt;Geography&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9023775" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/davidlean/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Spatial/default.aspx">Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server+Spatial/default.aspx">SQL Server Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geometry/default.aspx">Geometry</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Polygon/default.aspx">Polygon</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geography/default.aspx">Geography</category></item><item><title>SQL 2008 Spatial Sample, Part 4 of 9 - Methods for Drawing Spatial Shapes</title><link>http://blogs.msdn.com/davidlean/archive/2008/10/29/sql-2008-spatial-sample-part-n-4-of-n-methods-for-drawing-spatial-shapes.aspx</link><pubDate>Wed, 29 Oct 2008 07:45:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9021635</guid><dc:creator>davele</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/davidlean/comments/9021635.aspx</comments><wfw:commentRss>http://blogs.msdn.com/davidlean/commentrss.aspx?PostID=9021635</wfw:commentRss><wfw:comment>http://blogs.msdn.com/davidlean/rsscomments.aspx?PostID=9021635</wfw:comment><description>&lt;p&gt;This post covers the following methods: STDimension, STGeometryType, STNumGeometries, STGeometryN, STNumPoints, STPointN, STNumInteriorRing / NumRings, STInteriorRingN / RingN, STExteriorRing, STX / Long, STY / Lat, Z &amp;amp; M (measure).&lt;/p&gt;  &lt;p&gt;Note: Unless otherwise stated. All code samples are designed to be Cut n Pasted directly into SQL Server Management Studio (SSMS) &amp;amp; run.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:B3E14793-948F-49af-A347-D19C374A7C4F:1653d677-2a70-4293-8562-22c78e2d6a5c" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt; &lt;script type="text/javascript"&gt;&lt;!--
digg_bodytext = 'This post covers the following methods: Reduce, STEnvelope, EnvelopeCenter, EnvelopeAngle, STConvexHull.';
//--&gt;&lt;/script&gt; &lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;h1&gt;Overview of Rendering methods.&lt;/h1&gt;  &lt;p&gt;Clearly these methods aren't limited to rendering. Anytime you need to really understand the internal detail about a shape, or convert shapes from one format to another you will find these handy. On first glance drawing is pretty straightforward. Especially if you are creating your own test data &amp;amp; have minimal experience with spatial data. You create a series of simple polygons, loop around their points &amp;amp; everything is good. The purpose of this post is to ensure you don't miss half the details. It takes a little more thought to handle&amp;#160; polygons with interior holes, &amp;quot;Multi&amp;quot;-shapes &amp;amp; GeometryCollection objects correctly. &lt;/p&gt;  &lt;p&gt;Below I've described the key methods you will require &amp;amp; provided an outline on how they relate to each other.&lt;/p&gt;  &lt;h1&gt;&amp;#160;&lt;/h1&gt;  &lt;h1&gt;Overview of Methods used for Exploring Shapes&lt;/h1&gt;  &lt;blockquote&gt;   &lt;p&gt;This table shows all the methods that assist in understanding the data that &amp;quot;makes&amp;quot; the shape. While the methods for Geography &amp;amp; Geometry are similar, there are some differences which I've highlighted in bold. &lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;table cellspacing="0" cellpadding="2" width="427" bgcolor="#ffffff" border="3"&gt;&lt;tbody&gt;       &lt;tr bgcolor="#b0c4de"&gt;         &lt;td valign="top" width="117"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="154"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/td&gt;          &lt;td valign="top" width="149"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;&lt;font color="#0080c0"&gt;Shapes / Object&lt;/font&gt;&lt;/td&gt;          &lt;td valign="top" width="154"&gt;.STNumGeometries()&lt;/td&gt;          &lt;td valign="top" width="150"&gt;.STNumGeometries()&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;Shape index&lt;/td&gt;          &lt;td valign="top" width="154"&gt;.STGeometryN()&lt;/td&gt;          &lt;td valign="top" width="150"&gt;.STGeometryN()&lt;/td&gt;       &lt;/tr&gt;        &lt;tr bgcolor="#f5f5f5"&gt;         &lt;td valign="top" width="117"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="154"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="150"&gt;&amp;#160;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;&lt;font color="#0080c0"&gt;Type of Shape&lt;/font&gt;&lt;/td&gt;          &lt;td valign="top" width="154"&gt;&lt;font color="#000000"&gt;.STDimension()&lt;/font&gt;&lt;/td&gt;          &lt;td valign="top" width="150"&gt;&lt;font color="#000000"&gt;.STDimension()&lt;/font&gt;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="154"&gt;.STGeometryType()&lt;/td&gt;          &lt;td valign="top" width="150"&gt;.STGeometryType()&lt;/td&gt;       &lt;/tr&gt;        &lt;tr bgcolor="#f5f5f5"&gt;         &lt;td valign="top" width="117"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="154"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="150"&gt;&amp;#160;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;&lt;font color="#0080c0"&gt;Rings / Polygon&lt;/font&gt;&lt;/td&gt;          &lt;td valign="top" width="154"&gt;.&lt;strong&gt;STNumInteriorRing&lt;/strong&gt;()&lt;/td&gt;          &lt;td valign="top" width="150"&gt;.&lt;strong&gt;NumRings&lt;/strong&gt;()&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="154"&gt;.&lt;strong&gt;STExteriorRing&lt;/strong&gt;()&lt;/td&gt;          &lt;td valign="top" width="150"&gt;&amp;#160;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;Ring index&lt;/td&gt;          &lt;td valign="top" width="154"&gt;.&lt;strong&gt;STInteriorRingN&lt;/strong&gt;()&lt;/td&gt;          &lt;td valign="top" width="150"&gt;.&lt;strong&gt;RingN&lt;/strong&gt;()&lt;/td&gt;       &lt;/tr&gt;        &lt;tr bgcolor="#f5f5f5"&gt;         &lt;td valign="top" width="117"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="154"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="150"&gt;&amp;#160;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;&lt;font color="#0080c0"&gt;Points / Shape&lt;/font&gt;&lt;/td&gt;          &lt;td valign="top" width="154"&gt;.STNumPoints()&lt;/td&gt;          &lt;td valign="top" width="150"&gt;.STNumPoints()&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;Point index&lt;/td&gt;          &lt;td valign="top" width="154"&gt;.STPointN()&lt;/td&gt;          &lt;td valign="top" width="150"&gt;.STPointN()&lt;/td&gt;       &lt;/tr&gt;        &lt;tr bgcolor="#f5f5f5"&gt;         &lt;td valign="top" width="117"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="154"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="150"&gt;&amp;#160;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;&lt;font color="#0080c0"&gt;&lt;strong&gt;4D Point values&lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;          &lt;td valign="top" width="154"&gt;&amp;#160;&lt;/td&gt;          &lt;td valign="top" width="150"&gt;&amp;#160;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;X&lt;/td&gt;          &lt;td valign="top" width="154"&gt;.&lt;strong&gt;STX&lt;/strong&gt;&lt;/td&gt;          &lt;td valign="top" width="150"&gt;.&lt;strong&gt;Long&lt;/strong&gt;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;Y&lt;/td&gt;          &lt;td valign="top" width="154"&gt;.&lt;strong&gt;STY&lt;/strong&gt;&lt;/td&gt;          &lt;td valign="top" width="150"&gt;.&lt;strong&gt;Lat&lt;/strong&gt;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;Z&lt;/td&gt;          &lt;td valign="top" width="154"&gt;.Z&lt;/td&gt;          &lt;td valign="top" width="150"&gt;.Z&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="117"&gt;M (Measure)&lt;/td&gt;          &lt;td valign="top" width="154"&gt;.M&lt;/td&gt;          &lt;td valign="top" width="151"&gt;.M&lt;/td&gt;       &lt;/tr&gt;     &lt;/tbody&gt;&lt;/table&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;u&gt;&lt;font color="#8000ff" size="5"&gt;&lt;/font&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;&lt;u&gt;&lt;font color="#8000ff" size="5"&gt;Drawing Shapes. &lt;/font&gt;&lt;/u&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Often you will know what sort of object you are dealing with as your table only contains either Points, Lines or Polygons. But if you are dealing with GeometryCollection objects or are writing code to handle anything then the following 2 methods are vital.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;u&gt;&lt;font color="#8000ff" size="5"&gt;&lt;/font&gt;&lt;/u&gt;&lt;/p&gt;  &lt;h1&gt;Method &lt;font color="#ff0000"&gt;.STDimension&lt;/font&gt;()&lt;/h1&gt;  &lt;ul&gt;   &lt;li&gt;Returns an integer to indicate the dimensionality of the spatial object.      &lt;ul&gt;       &lt;li&gt;2 = 2D for Polygon &lt;/li&gt;        &lt;li&gt;1 = 1D for Line &lt;/li&gt;        &lt;li&gt;0 = 0D for Point &lt;/li&gt;        &lt;li&gt;-1 = Empty &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;font color="#804000"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/font&gt; objects &lt;/li&gt;    &lt;li&gt;Sister method of .STGeometryType Method.      &lt;p&gt;Syntax: geo1.STDimension()&lt;/p&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;Example use: Handy in Case/Select statements to call different rendering routines. &lt;/p&gt;    &lt;pre class="code"&gt;&lt;span style="color: green"&gt;--&amp;lt; Show STDimension, Returns 2=2D for Polygon, 1=1D for Line &amp;amp; 0 = 0D for Point &amp;gt;--
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@temp &lt;span style="color: blue"&gt;TABLE &lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;Shape &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;), &lt;/span&gt;[geom] &lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: blue"&gt;INSERT INTO &lt;/span&gt;@temp &lt;span style="color: blue"&gt;VALUES 
     &lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'Empty'&lt;/span&gt;&lt;span style="color: gray"&gt;,      &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON EMPTY'&lt;/span&gt;&lt;span style="color: gray"&gt;)
    ,(&lt;/span&gt;&lt;span style="color: red"&gt;'Point'&lt;/span&gt;&lt;span style="color: gray"&gt;,      &lt;/span&gt;&lt;span style="color: red"&gt;'POINT(3 3)'&lt;/span&gt;&lt;span style="color: gray"&gt;)
    ,(&lt;/span&gt;&lt;span style="color: red"&gt;'LineString'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(0 0, 3 3)'&lt;/span&gt;&lt;span style="color: gray"&gt;)     
    ,(&lt;/span&gt;&lt;span style="color: red"&gt;'Polygon'&lt;/span&gt;&lt;span style="color: gray"&gt;,    &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((0 0, 3 0, 0 3, 0 0))'&lt;/span&gt;&lt;span style="color: gray"&gt;);    

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;Shape&lt;span style="color: gray"&gt;, &lt;/span&gt;[geom]&lt;span style="color: gray"&gt;.&lt;/span&gt;STDimension&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;[STDimension]
&lt;span style="color: blue"&gt;FROM &lt;/span&gt;@temp&lt;span style="color: gray"&gt;;&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

  &lt;pre class="code"&gt;&lt;span style="color: gray"&gt;&lt;u&gt;Results:&lt;/u&gt; &lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

  &lt;table cellspacing="0" cellpadding="2" width="178" bgcolor="#ffffff" border="3"&gt;&lt;tbody&gt;
      &lt;tr bgcolor="#b0c4de"&gt;
        &lt;td valign="top" width="85"&gt;&lt;strong&gt;Shape&lt;/strong&gt;&lt;/td&gt;

        &lt;td valign="top" width="87"&gt;&lt;strong&gt;STDimension&lt;/strong&gt;&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="90"&gt;Empty&lt;/td&gt;

        &lt;td valign="top" width="87"&gt;-1&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="90"&gt;Point&lt;/td&gt;

        &lt;td valign="top" width="87"&gt;0&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="94"&gt;LineString&lt;/td&gt;

        &lt;td valign="top" width="87"&gt;1&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="97"&gt;Polygon&lt;/td&gt;

        &lt;td valign="top" width="87"&gt;2&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;

  &lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;Method &lt;font color="#ff0000"&gt;.STGeometryType&lt;/font&gt;()&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;Returns name of Spatial Object Type. 
    &lt;ul&gt;
      &lt;li&gt;eg: Point, Multipoint etc &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Unlikely that you'll use this a lot. But it it is more precise in describing exactly what the geometric object is. &lt;/li&gt;

  &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;font color="#804000"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/font&gt; objects &lt;/li&gt;

  &lt;li&gt;Sister method of .STDimension Method. &lt;/li&gt;

  &lt;li&gt;
    &lt;p&gt;Syntax: geo1.STGeometryType()&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example use: Handy for grouping the spatial objects if you need to know the difference between Single &amp;amp; Multi variations. &lt;/p&gt;

  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;--&amp;lt; Show STGeometryType, Returns Name of object &amp;gt;--
-- Note: It has the same name even with Geography datatypes. ie: Not STGeographyType --
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@temp &lt;span style="color: blue"&gt;TABLE &lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;Shape &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;), &lt;/span&gt;[geom] &lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;);

&lt;/span&gt;&lt;span style="color: blue"&gt;INSERT INTO &lt;/span&gt;@temp &lt;span style="color: blue"&gt;VALUES 
     &lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'Point'&lt;/span&gt;&lt;span style="color: gray"&gt;,   &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POINT(3 3)'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;))
    ,(&lt;/span&gt;&lt;span style="color: red"&gt;'M-Point'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'MULTIPOINT( (4 4), (5 5) )'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;))
    ,(&lt;/span&gt;&lt;span style="color: red"&gt;'Line'&lt;/span&gt;&lt;span style="color: gray"&gt;,    &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(0 5, 3 8)'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;))
    ,(&lt;/span&gt;&lt;span style="color: red"&gt;'M-Line'&lt;/span&gt;&lt;span style="color: gray"&gt;,  &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'MULTILINESTRING( (0 0, 3 3), (0 3, 3 0) )'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;))
    ,(&lt;/span&gt;&lt;span style="color: red"&gt;'Polygon'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((0 0, 3 0, 0 3, 0 0))'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;))
    ,(&lt;/span&gt;&lt;span style="color: red"&gt;'M-Poly'&lt;/span&gt;&lt;span style="color: gray"&gt;,  &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'MULTIPOLYGON( ((0 0, 3 0, 0 3, 0 0)), ((5 0, 8 0, 5 3, 5 0)) )'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;))
    ,(&lt;/span&gt;&lt;span style="color: red"&gt;'GeomCol'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'GEOMETRYCOLLECTION( POLYGON((0 0, 3 0, 0 3, 0 0)), POINT(5 5) )'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;));

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;Shape&lt;span style="color: gray"&gt;, &lt;/span&gt;[geom]&lt;span style="color: gray"&gt;.&lt;/span&gt;STDimension&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;[STDimension]&lt;span style="color: gray"&gt;, &lt;/span&gt;[geom]&lt;span style="color: gray"&gt;.&lt;/span&gt;STGeometryType&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;[STGeometryType]&lt;span style="color: gray"&gt;, &lt;/span&gt;[geom]&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;WKT
&lt;span style="color: blue"&gt;FROM &lt;/span&gt;@temp&lt;span style="color: gray"&gt;;&lt;/span&gt;&lt;span style="color: gray"&gt; &lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="code"&gt;&lt;span style="color: gray"&gt;&lt;u&gt;Results:&lt;/u&gt; &lt;/span&gt;&lt;/pre&gt;

  &lt;table cellspacing="0" cellpadding="2" width="793" bgcolor="#ffffff" border="3"&gt;&lt;tbody&gt;
      &lt;tr bgcolor="#b0c4de"&gt;
        &lt;td valign="top" width="95"&gt;&lt;strong&gt;Shape&lt;/strong&gt;&lt;/td&gt;

        &lt;td valign="top" width="122"&gt;&lt;strong&gt;STDimension&lt;/strong&gt;&lt;/td&gt;

        &lt;td valign="top" width="10"&gt;&lt;strong&gt;STGeometryType&lt;/strong&gt;&lt;/td&gt;

        &lt;td valign="top" width="555"&gt;&lt;strong&gt;WKT&lt;/strong&gt;&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="97"&gt;Point&lt;/td&gt;

        &lt;td valign="top" width="124"&gt;0&lt;/td&gt;

        &lt;td valign="top" width="10"&gt;Point&lt;/td&gt;

        &lt;td valign="top" width="555"&gt;POINT (3 3)&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="98"&gt;M-Point&lt;/td&gt;

        &lt;td valign="top" width="125"&gt;0&lt;/td&gt;

        &lt;td valign="top" width="10"&gt;MultiPoint&lt;/td&gt;

        &lt;td valign="top" width="555"&gt;MULTIPOINT ((4 4), (5 5))&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="98"&gt;Line&lt;/td&gt;

        &lt;td valign="top" width="126"&gt;1&lt;/td&gt;

        &lt;td valign="top" width="10"&gt;LineString&lt;/td&gt;

        &lt;td valign="top" width="555"&gt;LINESTRING (0 5, 3 8)&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="98"&gt;M-Line&lt;/td&gt;

        &lt;td valign="top" width="126"&gt;1&lt;/td&gt;

        &lt;td valign="top" width="10"&gt;MultiLineString&lt;/td&gt;

        &lt;td valign="top" width="555"&gt;MULTILINESTRING ((0 0, 3 3), (0 3, 3 0))&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="98"&gt;Polygon&lt;/td&gt;

        &lt;td valign="top" width="126"&gt;2&lt;/td&gt;

        &lt;td valign="top" width="10"&gt;Polygon&lt;/td&gt;

        &lt;td valign="top" width="555"&gt;POLYGON ((0 0, 3 0, 0 3, 0 0))&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="97"&gt;M-Poly&lt;/td&gt;

        &lt;td valign="top" width="126"&gt;2&lt;/td&gt;

        &lt;td valign="top" width="10"&gt;MultiPolygon&lt;/td&gt;

        &lt;td valign="top" width="555"&gt;MULTIPOLYGON (((0 0, 3 0, 0 3, 0 0)), ((5 0, 8 0, 5 3, 5 0)))&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="97"&gt;GeomCol&lt;/td&gt;

        &lt;td valign="top" width="126"&gt;2&lt;/td&gt;

        &lt;td valign="top" width="10"&gt;GeometryCollection&lt;/td&gt;

        &lt;td valign="top" width="555"&gt;GEOMETRYCOLLECTION (POLYGON ((0 0, 3 0, 0 3, 0 0)), POINT (5 5))&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
  &lt;span style="color: gray"&gt;&lt;/span&gt;&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;font color="#8000ff" size="5"&gt;&lt;strong&gt;Drawing Simple Polygons&lt;/strong&gt;&lt;/font&gt;&lt;/u&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The VB.NET code below shows the relationships between the main methods you will need. It is straightforward, just looping thru all points the shape contains &amp;amp; processing or drawing them (somehow)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;h4&gt;Options for Rendering the shapes&lt;/h4&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Unless you are using the Dundas .NET Map Control you should ignore the references to Shape &amp;amp; ShapeSegment. I left them in the sample code to prompt you that you will probably need to create your own class or similar to render it. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
  &lt;ol&gt;
    &lt;li&gt;Windows Presentation Framework (WPF) is great for this. It is really simple to create a really slick UI. But GDI+ also works if you aren't running .NET v3.5. I plan on writing on this in a future post. &lt;/li&gt;

    &lt;li&gt;DirectX &amp;amp; unmanaged C++ has the potential to give you the best performance, but it requires much more work to match the look of the WPF &amp;amp; if you aren't great at threading &amp;amp; design you may run much slower. &lt;/li&gt;

    &lt;li&gt;I found the &lt;a title="Easiest way to get Maps on the screen for WinForms or WebForm apps." href="http://www.dundas.com/Products/Map/NET/index.aspx"&gt;Dundas Map for .NET&lt;/a&gt; lets you avoid all the map rendering work completely. So unless you are a specialist Spatial S/W developer attempting to create your own distinct look I'd suggest you look at Dundas Map or Virtual Earth. &lt;/li&gt;
  &lt;/ol&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
  &lt;h4&gt;Steps to understand a simple Polygon&lt;/h4&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
  &lt;ol&gt;
    &lt;li&gt;Use &lt;strong&gt;STNumPoints&lt;/strong&gt; to determine how many points comprise the shape. Perhaps use the value to initialise an array or similar collection.&amp;#160; &lt;/li&gt;

    &lt;li&gt;Iterate thru each point 
      &lt;ol&gt;
        &lt;li&gt;For Geometry types use properties: STX, STY, Z &amp;amp; M &lt;/li&gt;

        &lt;li&gt;For Geography types use properties: Long, Lat, Z &amp;amp; M &lt;/li&gt;
      &lt;/ol&gt;
    &lt;/li&gt;

    &lt;li&gt;As mentioned in other posts, the SQL2008 Spatial functions ignore Z &amp;amp; M in their calculations. But if you want to create 3D or even 4D (time) based drawings, this is where you'd start. &lt;/li&gt;
  &lt;/ol&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;:&amp;#160; Points &amp;amp; Geometries collections are 1-Based, not 0-Based as most .NET Collections.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;font size="2"&gt;Private Sub makeGeomSegment(ByRef myShape As Shape, ByVal &lt;font color="#ff8000"&gt;geom&lt;/font&gt; As &lt;font color="#ff8040"&gt;SqlGeography&lt;/font&gt;)&lt;/font&gt;&lt;/p&gt;

  &lt;p&gt;&lt;font color="#008000" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; '--&amp;lt; Segment &amp;gt;--&lt;/font&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;font color="#008000" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font size="2"&gt;Dim newSegment(0) As ShapeSegment&lt;/font&gt;&lt;/p&gt;

  &lt;p&gt;&lt;font color="#008000" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font size="2"&gt;newSegment(0) = New ShapeSegment&lt;/font&gt;&lt;/p&gt;

  &lt;p&gt;&lt;font color="#008000" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font size="2"&gt;newSegment(0).Type = SegmentType.Polygon&lt;/font&gt;&lt;/p&gt;

  &lt;p&gt;&lt;font color="#008000" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font size="2"&gt;newSegment(0).Length = geom.STNumPoints&lt;/font&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/p&gt;

  &lt;p&gt;&lt;font color="#008000"&gt;&lt;font size="2"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font size="2"&gt;&lt;strong&gt;'--&amp;lt; Points &amp;gt;--&lt;/strong&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;

  &lt;p&gt;&lt;font color="#008000" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font size="2"&gt;Dim ptsShape(&lt;font color="#ff8000"&gt;geom&lt;/font&gt;.&lt;font color="#ff00ff"&gt;&lt;strong&gt;STNumPoints&lt;/strong&gt; - 1&lt;/font&gt;) As MapPoint&amp;#160; &lt;font color="#008000"&gt;' Points start at 1 not 0&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;font color="#008000" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font size="2"&gt;For cntPT As Integer = 1 To &lt;font color="#ff8040"&gt;geom&lt;/font&gt;.STNumPoints&lt;/font&gt;&lt;/p&gt;

  &lt;p&gt;&lt;font size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;ptsShape(cntPT - 1).X = &lt;font color="#ff8000"&gt;geom&lt;/font&gt;.&lt;strong&gt;&lt;font color="#ff00ff"&gt;STPointN&lt;/font&gt;&lt;/strong&gt;(cntPT).&lt;strong&gt;&lt;font color="#0000ff"&gt;Long&lt;/font&gt;&lt;/strong&gt;&amp;#160;&amp;#160; 'or&amp;#160; .&lt;strong&gt;&lt;font color="#0000ff"&gt;STX&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;

  &lt;p&gt;&lt;font size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;ptsShape(cntPT - 1).Y = &lt;font color="#ff8000"&gt;geom&lt;/font&gt;.&lt;strong&gt;&lt;font color="#ff00ff"&gt;STPointN&lt;/font&gt;&lt;/strong&gt;(cntPT).&lt;strong&gt;&lt;font color="#0000ff"&gt;Lat&lt;/font&gt;&lt;/strong&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 'or&amp;#160; .&lt;strong&gt;&lt;font color="#0000ff"&gt;STY&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;

  &lt;p&gt;&lt;font color="#008000" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font size="2"&gt;Next&lt;/font&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;font color="#008000" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#008000" size="2"&gt;'- Update Shape&lt;/font&gt;&lt;/p&gt;

  &lt;p&gt;&lt;font color="#008000" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font size="2"&gt;myShape.AddSegments(ptsShape, newSegment)&lt;/font&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;font size="2"&gt;End Sub&lt;/font&gt;&lt;/p&gt;

  &lt;p&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;&lt;font color="#8000ff" size="3"&gt;&lt;u&gt;Short description of the relevant methods&lt;/u&gt;&lt;/font&gt;&lt;/h2&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h1&gt;Method &lt;font color="#ff0000"&gt;.STPointN&lt;/font&gt;()&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;STPointN acts as an index into the &amp;quot;N&amp;quot;th Point in a shape. Where &amp;quot;N&amp;quot; is a positive integer. &lt;/li&gt;

  &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;font color="#804000"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/font&gt; objects. &lt;/li&gt;

  &lt;li&gt;Assists you to enumerate thru all the points in a shape. &lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;Method &lt;font color="#ff0000"&gt;.STNumPoints&lt;/font&gt;()&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;Number of Points in a shape. &lt;/li&gt;

  &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;font color="#804000"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/font&gt; objects. &lt;/li&gt;

  &lt;li&gt;Handy to initialise arrays &amp;amp; to terminate loops. &lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;Method &lt;font color="#ff0000"&gt;.STStartPoint&lt;/font&gt;()&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;Returns the 1st point in a shape. &lt;/li&gt;

  &lt;li&gt;STStartPoint() is the equivalent of STPointN (1) &lt;/li&gt;

  &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;font color="#804000"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/font&gt; objects. &lt;/li&gt;

  &lt;li&gt;Not exciting but useful for drawing a &amp;quot;start&amp;quot; icon on a path. &lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;Method &lt;font color="#ff0000"&gt;.STEndPoint&lt;/font&gt;()&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;Returns the last point in a shape. In a Polygon, Closed Line or Point is it identical to STStartPoint. &lt;/li&gt;

  &lt;li&gt;geo.STEndPoint() is the equivalent of geo.STPointN( geo.STNumPoints() ). &lt;/li&gt;

  &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;font color="#804000"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/font&gt; objects. &lt;/li&gt;

  &lt;li&gt;Not exciting but useful for drawing a &amp;quot;finish&amp;quot; icon on a path. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;font color="#8000ff" size="5"&gt;&lt;strong&gt;Drawing Complex Polygons&lt;/strong&gt;&lt;/font&gt;&lt;/u&gt;&lt;/p&gt;

&lt;h1&gt;
  &lt;blockquote&gt;
    &lt;p&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/p&gt;
  &lt;/blockquote&gt;
&lt;/h1&gt;

&lt;blockquote&gt;
  &lt;p&gt;By complex we mean:-&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
  &lt;li&gt;It has holes in it &lt;/li&gt;

  &lt;li&gt;It is comprised of multiple similar objects, like the 2 polygons that form the North &amp;amp; South shapes for the Islands of New Zealand. &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;I view GeometryCollection as a extension of this. They are often comprised of multiple objects that could be anything, multiple Lines, Polygons &amp;amp; Points. So if you write your code to handle these, everything else is a subset.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h1&gt;&lt;u&gt;&lt;font color="#8000ff" size="4"&gt;Drawing Geometry Polygons&lt;/font&gt;&lt;/u&gt;&lt;/h1&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h1&gt;Method &lt;font color="#ff0000"&gt;.STExteriorRing&lt;/font&gt;()&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;A Geometry Object that defines the outside of a Geometry polygon. &lt;/li&gt;

  &lt;li&gt;Works with &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#804000"&gt;Geography&lt;/font&gt; Objects. 

    &lt;ul&gt;
      &lt;li&gt;Geography equivalent is RingN() &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Syntax: geo1.STExteriorRing()&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example use: &lt;font color="#000000"&gt;Pass the result to a routine that iterate thru the points it contains &amp;amp; draw a shape.&lt;/font&gt;&lt;/p&gt;

  &lt;p&gt;&lt;font color="#000000"&gt;Note: The Sample code adds STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.05&lt;span style="color: gray"&gt;) &lt;font color="#000000"&gt;to the STExteriorRing&lt;/font&gt;&lt;span style="color: gray"&gt;&lt;font color="#000000"&gt;() just to make it easier to see. You wouldn't do this in your rendering code.&lt;/font&gt; &lt;/span&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;

  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Sample: Outer ring of a polygon &amp;gt;==
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@h &lt;span style="color: blue"&gt;AS GEOMETRY  
SET &lt;/span&gt;@h&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(67 36,68 37,69 38,73 38,75 37,76 35,
                                  75 33,74 32,71 32,69 33,58 39,55 40,52 40,50 39,
                                  48 36,48 34,50 31,52 30,56 30,60 31,62 33)'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;0&lt;span style="color: gray"&gt;);

&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@b &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;=  &lt;/span&gt;@h&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;1.5&lt;span style="color: gray"&gt;) &lt;span style="color: green"&gt;-- Make the line a Polygon &lt;/span&gt;

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@b &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Geo'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'Line' &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;&lt;span style="color: red"&gt;'Labels'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;@b&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Text'
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@b&lt;span style="color: gray"&gt;.&lt;/span&gt;STExteriorRing&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.05&lt;span style="color: gray"&gt;), &lt;/span&gt;&lt;span style="color: red"&gt;'STExteriorRing()'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;@b&lt;span style="color: gray"&gt;.&lt;/span&gt;STExteriorRing&lt;span style="color: gray"&gt;().&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;()&lt;/span&gt;&amp;#160;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplePartn4ofnMethodsforD_E1C1/STExteriorRing_2.jpg"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="195" alt="STExteriorRing" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplePartn4ofnMethodsforD_E1C1/STExteriorRing_thumb.jpg" width="386" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

  &lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;Method &lt;font color="#ff0000"&gt;.STInteriorRingN&lt;/font&gt;(n)&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;A Geometry Object that defines one of the &amp;#8220;holes&amp;#8221; in a Geometry polygon. &lt;/li&gt;

  &lt;li&gt;Works with &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#804000"&gt;Geography&lt;/font&gt; Objects. 

    &lt;ul&gt;
      &lt;li&gt;Geography equivalent is RingN() &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Syntax: geo1.STInteriorRingN()&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Sample: Inner ring 1 of a polygon &amp;gt;== 
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@h &lt;span style="color: blue"&gt;AS GEOMETRY  
SET &lt;/span&gt;@h&lt;span style="color: gray"&gt;=&lt;/span&gt;&lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(67 36,68 37,69 38,73 38,75 37,76 35, 
                                  75 33,74 32,71 32,69 33,58 39,55 40,52 40,50 39, 
                                  48 36,48 34,50 31,52 30,56 30,60 31,62 33)'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;0&lt;span style="color: gray"&gt;); 

&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@b &lt;span style="color: blue"&gt;AS GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;@h&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;1.5&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: green"&gt;-- Make the line a Polygon 

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@b &lt;span style="color: blue"&gt;as&lt;/span&gt;&lt;span style="color: red"&gt;'Geo'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'Line' &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;&lt;span style="color: red"&gt;'Labels'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;@b&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Text' 
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL 
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@b&lt;span style="color: gray"&gt;.&lt;/span&gt;STInteriorRingN&lt;span style="color: gray"&gt;(&lt;/span&gt;1&lt;span style="color: gray"&gt;).&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.05&lt;span style="color: gray"&gt;),&lt;/span&gt;&lt;span style="color: red"&gt;'STInteriorRingN(1)'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;@b&lt;span style="color: gray"&gt;.&lt;/span&gt;STInteriorRingN&lt;span style="color: gray"&gt;(&lt;/span&gt;1&lt;span style="color: gray"&gt;).&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;()&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplePartn4ofnMethodsforD_E1C1/STInteriorRingN_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="198" alt="STInteriorRingN" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplePartn4ofnMethodsforD_E1C1/STInteriorRingN_thumb.png" width="406" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

  &lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;Algorithm for Drawing Complex Geometry Polygons&lt;/h1&gt;

&lt;blockquote&gt;
  &lt;p&gt;The VB.NET code below shows the relationships between the main methods you will need.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
  &lt;li&gt;Protect yourself from any invalid data. 
    &lt;ol&gt;
      &lt;li&gt;I do this with &lt;strong&gt;MakeValid&lt;/strong&gt; or you may prefer to raise an error. &lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;

  &lt;li&gt;Use &lt;strong&gt;STNumGeometries&lt;/strong&gt; to check if this geometry object is a MultiPolygon or a GeometryCollection.&amp;#160; While I've not included this in the code sample below, if it is a GeometryCollection you may what to use &lt;strong&gt;STDimension&lt;/strong&gt; &amp;amp; logic to handle Lines &amp;amp; points in addition to the polygons shown below. &lt;/li&gt;

  &lt;li&gt;Use &lt;strong&gt;STGeomertyN&lt;/strong&gt; as an index to loop thru each polygon in the collection. &lt;/li&gt;

  &lt;li&gt;&lt;u&gt;For each polygon&lt;/u&gt; 

    &lt;ol&gt;
      &lt;li&gt;Use &lt;strong&gt;STNumInteriorRing&lt;/strong&gt; to check if there are any internal shapes. &lt;/li&gt;

      &lt;li&gt;If there are, use &lt;strong&gt;STExteriorRing&lt;/strong&gt; to Process the outer Polygon &lt;/li&gt;

      &lt;li&gt;Then use &lt;strong&gt;STInteriorRingN&lt;/strong&gt; to loop thru all the interior polygons. &lt;/li&gt;

      &lt;li&gt;Remember indicate to your drawing routines that you need it to mask out the &amp;quot;holes&amp;quot; formed by the InteriorRing polygons. (below I used shapeComplexPolygon for that task) &lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;

  &lt;li&gt;Note: As STExteriorRing &amp;amp; STinteriorRingN both return a simple polygon, you can then pass their output to your routine that loops thru their points &amp;amp; draws them. &lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplePartn4ofnMethodsforD_E1C1/GeometryPolygons_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="416" alt="GeometryPolygons" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplePartn4ofnMethodsforD_E1C1/GeometryPolygons_thumb.png" width="552" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

  &lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;&lt;u&gt;&lt;font color="#8000ff" size="5"&gt;Drawing Geography Polygons&lt;/font&gt;&lt;/u&gt;&lt;/h1&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h1&gt;Method &lt;font color="#ff0000"&gt;.STRingN&lt;/font&gt;(n)&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;A Geography Object that defines the boundaries of a Geography. 
    &lt;ul&gt;
      &lt;li&gt;Where n = 1 to number of interior shapes. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Geography Objects are cleaner to work with. Just loop thru &amp;amp; no External/Internal stuff to think about. &lt;/li&gt;

  &lt;li&gt;Works with &lt;font color="#804000"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#804000"&gt;&lt;font color="#0000ff"&gt;Geometry&lt;/font&gt; &lt;/font&gt;Objects. 

    &lt;ul&gt;
      &lt;li&gt;&lt;font color="#804000"&gt;&lt;font color="#000000"&gt;Geometry&lt;/font&gt; &lt;/font&gt;equivalent is STInteriorRingN() &amp;amp; STExteriorRing() &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Syntax: geo1.STRingN(ring_number)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Sample: Show all Rings of a Complex Geography Polygon &amp;gt;==
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@h &lt;span style="color: blue"&gt;AS GEOGRAPHY
SET &lt;/span&gt;@h&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(67 36,68 37,69 38,73 38,75 37,76 35,
                                  75 33,74 32,71 32,69 33,58 39,55 40,52 40,50 39,
                                  48 36,48 34,50 31,52 30,56 30,60 31,62 33)'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;4236&lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@b &lt;span style="color: blue"&gt;AS GEOGRAPHY &lt;/span&gt;&lt;span style="color: gray"&gt;=  &lt;/span&gt;@h&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;100000&lt;span style="color: gray"&gt;)  &lt;span style="color: green"&gt;-- Make the line a Polygon &lt;/span&gt;

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@h &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Geo'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'Line' &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;&lt;span style="color: red"&gt;'Labels'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;@h&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Text'
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@b &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Geo'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'Buffer(100000)'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;@b&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: green"&gt;-- NB: Much larger value as in Metres
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@b&lt;span style="color: gray"&gt;.&lt;/span&gt;RingN&lt;span style="color: gray"&gt;(&lt;/span&gt;1&lt;span style="color: gray"&gt;).&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;8000&lt;span style="color: gray"&gt;), &lt;/span&gt;&lt;span style="color: red"&gt;'RingN(1)'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;@b&lt;span style="color: gray"&gt;.&lt;/span&gt;RingN&lt;span style="color: gray"&gt;(&lt;/span&gt;1&lt;span style="color: gray"&gt;).&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: green"&gt;-- Exterior Ring
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@b&lt;span style="color: gray"&gt;.&lt;/span&gt;RingN&lt;span style="color: gray"&gt;(&lt;/span&gt;2&lt;span style="color: gray"&gt;).&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;8000&lt;span style="color: gray"&gt;), &lt;/span&gt;&lt;span style="color: red"&gt;'RingN(2)'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;@b&lt;span style="color: gray"&gt;.&lt;/span&gt;RingN&lt;span style="color: gray"&gt;(&lt;/span&gt;2&lt;span style="color: gray"&gt;).&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: green"&gt;-- Interior Ring
&lt;/span&gt;&lt;span style="color: blue"&gt;go&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplePartn4ofnMethodsforD_E1C1/STRingN_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="187" alt="STRingN" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplePartn4ofnMethodsforD_E1C1/STRingN_thumb.png" width="394" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;&lt;strong&gt;Warning&lt;/strong&gt;: The direction of the points that form the polygon path is important. &lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;Anti-Clockwise fills in the centre. &lt;/li&gt;

    &lt;li&gt;Clockwise fills the entire world except the centre. 
      &lt;ul&gt;
        &lt;li&gt;This good for creating &amp;quot;holes&amp;quot; in polygons. &lt;/li&gt;

        &lt;li&gt;Also great for causing errors stating your polygon is more than half the world. 
          &lt;p&gt;&lt;font color="#ff0000"&gt;Msg 6522, Level 16, State 1, Line 1 
              &lt;br /&gt;A .NET Framework error occurred during execution of user-defined routine or aggregate &amp;quot;geography&amp;quot;: 

              &lt;br /&gt;Microsoft.SqlServer.Types.GLArgumentException: 24205: The specified input does not represent a valid geography instance because it exceeds a single hemisphere. Each geography instance must fit inside a single hemisphere. A common reason for this error is that a polygon has the wrong ring orientation.&lt;/font&gt;&lt;/p&gt;
        &lt;/li&gt;
      &lt;/ul&gt;
    &lt;/li&gt;

    &lt;li&gt;&lt;strong&gt;Tip&lt;/strong&gt;: I use the phrase &amp;#8220;&lt;strong&gt;L&lt;/strong&gt;ook to &lt;strong&gt;L&lt;/strong&gt;eft&amp;#8221;. If you imagine you are walking in the direction the line is being drawn, then left is side of the line that will be filled. &lt;/li&gt;
  &lt;/ul&gt;

  &lt;p&gt;
    &lt;br /&gt;&lt;/p&gt;

  &lt;h1&gt;Algorithm for Drawing Complex Geography Polygons&lt;/h1&gt;

  &lt;p&gt;The VB.NET code below shows the relationships between the main methods you will need. It is very similar to the Geometry processing.&lt;/p&gt;

  &lt;ol&gt;
    &lt;li&gt;Protect yourself from any invalid data. 
      &lt;ol&gt;
        &lt;li&gt;Unfortunately there is no &lt;strong&gt;MakeValid&lt;/strong&gt; for geography objects. It is also harder to create invalid shapes. Still, you need to catch the error &amp;amp; fix it yourself. The most common exception is the points create a wrong ring direction. For more info on how to fix it see &lt;a href="http://blogs.msdn.com/edkatibah/archive/2008/08/19/working-with-invalid-data-and-the-sql-server-2008-geography-data-type-part-1b.aspx"&gt;Working with invalid data &amp;amp; the SQL2008 Geography Datatype&lt;/a&gt; &lt;/li&gt;
      &lt;/ol&gt;
    &lt;/li&gt;

    &lt;li&gt;Use &lt;strong&gt;STNumGeometries&lt;/strong&gt; to check if this geometry object is a MultiPolygon or a GeometryCollection.&amp;#160; While I've not included this in the code sample below, if it is a GeometryCollection you may what to use &lt;strong&gt;STDimension&lt;/strong&gt; &amp;amp; logic to handle Lines &amp;amp; points in addition to the polygons shown below. &lt;/li&gt;

    &lt;li&gt;Use &lt;strong&gt;STGeomertyN&lt;/strong&gt; as an index to loop thru each polygon in the collection. &lt;/li&gt;

    &lt;li&gt;&lt;u&gt;For each polygon&lt;/u&gt; 

      &lt;ol&gt;
        &lt;li&gt;Use &lt;strong&gt;NumRings&lt;/strong&gt; to check if there are any internal shapes. &lt;/li&gt;

        &lt;li&gt;Then &lt;strong&gt;RingN&lt;/strong&gt; to loop thru all the polygons, both Exterior &amp;amp; any Interior &lt;/li&gt;

        &lt;li&gt;Remember indicate to your drawing routines that you need it to mask out the &amp;quot;holes&amp;quot; formed by the InteriorRing polygons. (below I used shapeComplexPolygon for that task) &lt;/li&gt;
      &lt;/ol&gt;
    &lt;/li&gt;

    &lt;li&gt;Note: As RingN returns a simple polygon, you can then pass the output to your routine that loops thru their points &amp;amp; draws them. &lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplePartn4ofnMethodsforD_E1C1/Geography_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="391" alt="Geography" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplePartn4ofnMethodsforD_E1C1/Geography_thumb.png" width="527" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;More info&lt;/h1&gt;

&lt;blockquote&gt;
  &lt;p&gt;For more info see SQL Server 2008 Books Online &lt;a title="Reference Details on methods listed in this post." href="http://msdn.microsoft.com/en-us/library/bb933960.aspx"&gt;OGC Methods on Geometry Instances&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Please give feedback. Is this series of articles useful? Did it save you time? What was good, What could be better?, Notice any errors? What would you like me to cover? All thoughts, comments, suggestions welcome.&lt;/p&gt;

&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:41ff4797-1335-4949-bd3b-ad25225e3952" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Spatial" rel="tag"&gt;Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SQL%20Server%202008" rel="tag"&gt;SQL Server 2008&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SQL%20Server%20Spatial" rel="tag"&gt;SQL Server Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/TSQL" rel="tag"&gt;TSQL&lt;/a&gt;,&lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Geometry" rel="tag"&gt;Geometry&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Geography" rel="tag"&gt;Geography&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Polygon" rel="tag"&gt;Polygon&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9021635" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/davidlean/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Spatial/default.aspx">Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/TSQL/default.aspx">TSQL</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server+Spatial/default.aspx">SQL Server Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geometry/default.aspx">Geometry</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Polygon/default.aspx">Polygon</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geography/default.aspx">Geography</category></item><item><title>SQL 2008 Spatial Samples, Part 5 of 9 - Performance Improvement Methods</title><link>http://blogs.msdn.com/davidlean/archive/2008/10/27/sql-2008-spatial-samples-part-n-3-of-n-performance-improvement-methods.aspx</link><pubDate>Mon, 27 Oct 2008 05:03:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9017332</guid><dc:creator>davele</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/davidlean/comments/9017332.aspx</comments><wfw:commentRss>http://blogs.msdn.com/davidlean/commentrss.aspx?PostID=9017332</wfw:commentRss><wfw:comment>http://blogs.msdn.com/davidlean/rsscomments.aspx?PostID=9017332</wfw:comment><description>&lt;p&gt;This post covers the following methods: Reduce, STEnvelope, EnvelopeCenter, EnvelopeAngle, STConvexHull.&lt;/p&gt; &lt;p&gt;Note: Unless otherwise stated. All code samples are designed to be Cut n Pasted directly into SQL Server Management Studio (SSMS) &amp;amp; run. Sometimes I've added extra detail than what is in the sample picture. &lt;/p&gt; &lt;p&gt;&lt;/p&gt; &lt;div class="wlWriterSmartContent" id="scid:B3E14793-948F-49af-A347-D19C374A7C4F:72dc2bed-a447-4eeb-9d88-7be45315b055" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt; &lt;script type="text/javascript"&gt;&lt;!--
digg_bodytext = 'This post covers the following methods: Reduce, STEnvelope, EnvelopeCenter, EnvelopeAngle, STConvexHull';
//--&gt;&lt;/script&gt; &lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt; &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;h1&gt;Overview of Performance Improvement methods.&lt;/h1&gt; &lt;p&gt;If you collected all the spatial data that describes our world, it could easily fill exabytes. Having this very detailed information is useful when you need to zoom in. It may help to have every house on every street, every bend of every river etc. However when you zoom out it becomes a burden. Plotting thousands of points on one pixel, consumes bandwidth &amp;amp; other resources without providing any benefit.  &lt;p&gt;Clearly there is a need to discard much of the detail. The challenge is to discard as much as you can without impacting the visual experience of the user.  &lt;p&gt;Fortunately smart mathematicians have given this issue some thought. You can take advantage of their expertise by using SQL's .&lt;strong&gt;Reduce&lt;/strong&gt; Method. It uses a variant of the &lt;a title="More details on the algorithm SQL uses in the Reduce method." href="http://geometryalgorithms.com/Archive/algorithm_0205/" target="_blank"&gt;Douglas-Peucker Algorithm&lt;/a&gt;.  &lt;p&gt;The .Reduce method has 1 parameter, Tolerance. It must be a positive floating point number. Any details smaller than this number will probably be removed. The tricky part is knowing what value to set that parameter to. The rest of this post is dedicated to that question.  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Algorithm Summary:&lt;/u&gt;&lt;/strong&gt;&amp;nbsp;&amp;nbsp; &lt;ol&gt; &lt;li&gt;Determine the resolution of your display window (in pixels)  &lt;li&gt;Determine the scale of what you are displaying.  &lt;li&gt;Divide (2) from (1) to find the scale of each pixel.  &lt;li&gt;That gives you a ballpark number for what you will reduce by. &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Unfortunately it is not an exact science, perhaps any detail smaller than 10 - 20 pixels you will remove. Maybe a cutoff of 100 or 1,000 pixels will work better for you. It depends on the resolution (pixels/inch) of your display &amp;amp; a subjective opinion of your User as to what looks "cluttered" to them. Often you will use this to control the extremes &amp;amp; then give the User a way to configure their preference. &lt;/p&gt; &lt;p&gt;It is also worth considering pre-processing your data &amp;amp; storing it as different "Zoom" levels. eg: Zoom Level 1 could represent 10,000kms &amp;amp; show country borders. thru to Zoom Level 'n' which may contain details of land parcels together with easement restrictions &amp;amp; building outlines. Clearly at that level you would be able to filter to a few 100 rows &amp;amp; not attempt to draw 5 million homes at once.&lt;/p&gt; &lt;p&gt;&lt;font color="#ff0000"&gt;&lt;/font&gt;&amp;nbsp; &lt;h1&gt;Method &lt;font color="#ff0000"&gt;.Reduce&lt;/font&gt;()&lt;/h1&gt; &lt;ul&gt; &lt;li&gt;Reduces the number of points in a Spatial Object.  &lt;ul&gt; &lt;li&gt;If it converts a polygon or a line into a single point you can often ignore the row. Resulting in a reduction of rows returned. &lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Works with both &lt;strong&gt;&lt;font color="#0000ff"&gt;Geometry&lt;/font&gt;&lt;/strong&gt; &amp;amp;&amp;nbsp; &lt;strong&gt;&lt;font color="#804000"&gt;Geography&lt;/font&gt;&lt;/strong&gt; objects &lt;/li&gt;&lt;/ul&gt; &lt;blockquote&gt; &lt;p&gt;Syntax: geo1.&lt;strong&gt;Reduce&lt;/strong&gt;(tolerance)&lt;/p&gt;&lt;/blockquote&gt; &lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Sample: Code calling the .Reduce() Method &amp;gt;==
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@h &lt;span style="color: blue"&gt;AS GEOMETRY 
SET &lt;/span&gt;@h &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(67 36,68 37,69 38,73 38, 75 37,76 35,
                                   75 33,74 32,71 32,69 33,58 39,55 40,52 40,50 39,
                                   48 36,48 34,50 31,52 30,56 30,60 31,62 33)'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;0&lt;span style="color: gray"&gt;);

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@h &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Geo'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'Line' &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;&lt;span style="color: red"&gt;'Labels'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;@h&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Text'
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@h&lt;span style="color: gray"&gt;.&lt;/span&gt;Reduce&lt;span style="color: gray"&gt;(&lt;/span&gt;2&lt;span style="color: gray"&gt;), &lt;/span&gt;&lt;span style="color: red"&gt;'Reduce(2)' &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;&lt;span style="color: red"&gt;'Labels'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;@h&lt;span style="color: gray"&gt;.&lt;/span&gt;Reduce&lt;span style="color: gray"&gt;(&lt;/span&gt;2&lt;span style="color: gray"&gt;).&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Text'&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;Sample Output:&lt;br&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn3ofnPerformanc_3A6/Reduce_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="150" alt="Reduce" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn3ofnPerformanc_3A6/Reduce_thumb.png" width="359" border="0"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Purple is the original line consisting of 19 points.&lt;/p&gt;
&lt;p&gt;Green is the line produced with .Reduce(2). It is no more than 2 units away from the original line, now 8 points.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/blockquote&gt;
&lt;h1&gt;Method &lt;font color="#ff0000"&gt;.STEnvelope&lt;/font&gt;()&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;Find the minimum rectangle that fits around this geometry object. 
&lt;li&gt;USE: 
&lt;ul&gt;
&lt;li&gt;Handy to centre the object within a display window. 
&lt;li&gt;Combine with .Reduce to optimise the display &amp;amp; save IO, CPU &amp;amp; Network&lt;/li&gt;&lt;/ul&gt;
&lt;li&gt;Works with &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#804000"&gt;Geography&lt;/font&gt; Objects. 
&lt;li&gt;Geography equivalent is EnvelopeAngle() &amp;amp; EnvelopeCenter()&lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;p&gt;Syntax: myGeometry.STEnvelope()&lt;/p&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Sample: Code displaying using STEnvelope for create a bounding box &amp;gt;==
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@b &lt;span style="color: blue"&gt;AS GEOMETRY 
SET &lt;/span&gt;@b &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(67 36,68 37,69 38,73 38,75 37,76 35,
                                   75 33,74 32,71 32,69 33,58 39,55 40,52 40,50 39,
                                   48 36,48 34,50 31,52 30,56 30,60 31,62 33)'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;0&lt;span style="color: gray"&gt;);

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@b &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Geo'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'Line' &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;&lt;span style="color: red"&gt;'Labels'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;@b&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Text'
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@b&lt;span style="color: gray"&gt;.&lt;/span&gt;STEnvelope&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'STEnvelope()'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;@b&lt;span style="color: gray"&gt;.&lt;/span&gt;STEnvelope&lt;span style="color: gray"&gt;().&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;()
&lt;/span&gt;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: gray"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="149" alt="STEnvelope" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn3ofnPerformanc_3A6/STEnvelope_thumb.png" width="363" border="0"&gt;&lt;br&gt;&lt;/span&gt;&lt;span style="color: gray"&gt;&lt;font color="#000000"&gt;Purple is the original line. 
Blue is the minimum rectangle that fits around this line.&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;Method &lt;font color="#ff0000"&gt;.EnvelopeCenter&lt;font color="#000000"&gt;()&lt;/font&gt; &lt;font color="#000000"&gt;&amp;amp;&lt;/font&gt; .EnvelopeAngle&lt;/font&gt;()&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;Find the bounding &lt;strong&gt;circle&lt;/strong&gt; that fits around this geography object. 
&lt;ul&gt;
&lt;li&gt;Why a circle? Imagine slicing thru a sphere with a knife. You always get a circle. 
&lt;li&gt;Books Online says: “not guaranteed to produce the &lt;strong&gt;minimal&lt;/strong&gt; bounding circle”. I agree, I've never seen it produce the absolute minimum circle. While it is much better than nothing, the lack of precision does limit how useful it really is. Working with a circle is challenging enough, the "not minimal" aspect means that your errors compound. By the time you've &lt;strong&gt;Unioned&lt;/strong&gt; the envelope of a few thousand shapes, your result may be many times bigger than the perfect minimum. 
&lt;li&gt;&lt;strong&gt;Warning&lt;/strong&gt;: 
&lt;ul&gt;
&lt;li&gt;Latitude must be within -90 &amp;amp; +90. Given that Bounding Circle might be bigger than the shape, the unfortunate side-effect is you can attempt to create bounding circles which are larger than 90 degrees. Resulting in a .NET error. 
&lt;li&gt;Similarly if your data only just fits inside a single hemisphere, the bounding circle may exceed it, resulting in the error below. 
&lt;p&gt;&lt;font color="#ff0000"&gt;Msg 6522, Level 16, State 1, Line 22&lt;br&gt;A .NET Framework error occurred during execution of user-defined routine or aggregate "geography": &lt;br&gt;Microsoft.SqlServer.Types.GLArgumentException: 24205: The specified input does not represent a valid geography instance because it exceeds a single hemisphere. Each geography instance must fit inside a single hemisphere. A common reason for this error is that a polygon has the wrong ring orientation.&lt;/font&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;li&gt;You can think of EnvelopeAngle as a distance. Your distance is measuring "Degrees of Longitude" or "Degrees of Latitude" so it is an Angle &amp;amp; not a linear measurement. It is the Radius of the bounding circle.
&lt;li&gt;Use: 
&lt;ul&gt;
&lt;li&gt;EnvelopeCenter is handy to centre the object within a display window. 
&lt;li&gt;Combine with .Reduce to optimise the display &amp;amp; save IO, CPU &amp;amp; Network&lt;/li&gt;&lt;/ul&gt;
&lt;li&gt;Works with &lt;font color="#804000"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; Objects. 
&lt;li&gt;EnvelopeCenter&amp;nbsp; is similar to geometry method STCentroid() &lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;p&gt;Syntax: myGeometry.&lt;strong&gt;EnvelopeCenter&lt;/strong&gt;()&lt;/p&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Sample 1: Show use of Geography Envelope methods. &amp;gt;==
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;AS GEOGRAPHY
SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(67 36,68 37,69 38,73 38,75 37,76 35,
                                    75 33,74 32,71 32,69 33,58 39,55 40,52 40,50 39,
                                    48 36,48 34,50 31,52 30,56 30,60 31,62 33)'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;4236&lt;span style="color: gray"&gt;);

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Geo'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'Line' &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;&lt;span style="color: red"&gt;'Labels'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'WKT'
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: green"&gt;-- Expand the centre point to show its not quite centred
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;1319650&lt;span style="color: gray"&gt;), &lt;/span&gt;&lt;span style="color: red"&gt;'Envelope Bounding Circle'&lt;/span&gt;&lt;span style="color: gray"&gt;,  &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;ToString&lt;span style="color: gray"&gt;() 
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;40000&lt;span style="color: gray"&gt;), &lt;/span&gt;&lt;span style="color: red"&gt;'EnvelopeCenter'&lt;/span&gt;&lt;span style="color: gray"&gt;,  &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;ToString&lt;span style="color: gray"&gt;()

&lt;/span&gt;&lt;span style="color: green"&gt;-- This value can be added or subtracted from EnvelopeCenter().Long to get Min &amp;amp; Max X
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeAngle&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;As &lt;/span&gt;EnvelopeAngle
&lt;span style="color: blue"&gt;go&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;pre class="code"&gt;&lt;span style="color: gray"&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn3ofnPerformanc_3A6/EnvelopeCentre_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="249" alt="EnvelopeCentre" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn3ofnPerformanc_3A6/EnvelopeCentre_thumb.png" width="291" border="0"&gt;&lt;/a&gt; &lt;br&gt;&lt;/span&gt;&lt;span style="color: gray"&gt;&lt;font color="#000000"&gt;Purple is the original line. 
Blue is the bounding circle created for this line.&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;h4&gt;Another example : Showing the bounding circle&lt;/h4&gt;
&lt;p&gt;This sample shows how you can use EnvelopeAngle to create a buffer to show what is covered by the BoundingCircle. You will discover that :-&lt;br&gt;a) EnvelopeCentre is biased to where most of the points are located. &lt;br&gt;b) But the EnvelopeAngle is dramatically affected by outlier points. &lt;/p&gt;
&lt;p&gt;This is good &amp;amp; bad. It means that centring your display window on the EnvelopeCentre will mean that you are likely to show most of your information. But using the EnvelopeAngle to calculate the .Reduce Value can mean you are way off. To get the minimum bounding circle you would want the EnvelopeCentre to be the geometric centre of all the outlier points, which it is not.&lt;br&gt;Try adding new points outlier points to the Polygon in the example below to see how it works.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ===&amp;lt; Sample 2: Show relationship between EnvelopeCentre &amp;amp; EnvelopeAngle &amp;gt;==
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@Buffer &lt;span style="color: blue"&gt;INT 
DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;AS GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON( (1 1, 2 1, 5 3, 7 6, 5 9, 2 11, 1 11, 5 6, 1 1))'

&lt;/span&gt;&lt;span style="color: green"&gt;-- -- Create a line that is the Radius
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@Radius &lt;span style="color: blue"&gt;AS GEOGRAPHY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING (' 
   &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;Long &lt;span style="color: gray"&gt;)  + &lt;/span&gt;&lt;span style="color: red"&gt;' ' 
   &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;Lat &lt;span style="color: gray"&gt;) + &lt;/span&gt;&lt;span style="color: red"&gt;','
   &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;Long &lt;span style="color: gray"&gt;+ &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeAngle&lt;span style="color: gray"&gt;())  + &lt;/span&gt;&lt;span style="color: red"&gt;' ' 
   &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;Lat &lt;span style="color: gray"&gt;) + &lt;/span&gt;&lt;span style="color: red"&gt;')'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;4326&lt;span style="color: gray"&gt;);

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@Radius&lt;span style="color: gray"&gt;.&lt;/span&gt;STLength&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;[STLength (meters)] &lt;span style="color: gray"&gt;, &lt;/span&gt;2 &lt;span style="color: gray"&gt;* &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeAngle&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;[2 * EnvelopeAngle()]
&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@Buffer &lt;span style="color: gray"&gt;= &lt;/span&gt;@Radius&lt;span style="color: gray"&gt;.&lt;/span&gt;STLength&lt;span style="color: gray"&gt;() 

&lt;/span&gt;&lt;span style="color: green"&gt;-- --&amp;lt; Show relationship between objects in SSMS's Spatial results tab &amp;gt;--
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;@Buffer&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;geo&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;@Buffer&lt;span style="color: gray"&gt;).&lt;/span&gt;ToString&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;WKT&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'.EnvelopeCircle' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Method
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'Exterior of Shape' 
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;20000&lt;span style="color: gray"&gt;), &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;ToString&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'.EnvelopeCenter'&lt;/span&gt;&lt;/pre&gt;
&lt;h4&gt;Another example : Creating a bounding rectangle around the bounding circle&lt;/h4&gt;
&lt;p&gt;You may find this handy when drawing your data, especially if you use a matrix transformation to plot them on the screen. But be careful as it is really common to have the envelope "explode" over more than a half hemisphere &amp;amp; result in an error. To avoid that issue instead of a SQLGeography Type try using a GDI+ Rectangle object or similar code object.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ===&amp;lt; Sample 3: Show relationship between EnvelopeCentre &amp;amp; EnvelopeAngle &amp;gt;==

&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;AS GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON( (1 1, 2 1, 5 3, 7 6, 5 9, 2 11, 1 11, 5 6, 1 1))'

&lt;/span&gt;&lt;span style="color: green"&gt;-- --&amp;lt; How to create a Minimal Bounding Box using EnvelopeCentre &amp;amp; EnvelopeAngle &amp;gt;===
-- Check you don't exceed +/-90 deg Latitude
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@Top &lt;span style="color: blue"&gt;FLOAT
DECLARE &lt;/span&gt;@Bot &lt;span style="color: blue"&gt;FLOAT
SET &lt;/span&gt;@Top &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;CASE WHEN &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;Lat &lt;span style="color: gray"&gt;+ &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeAngle&lt;span style="color: gray"&gt;() &amp;gt;  &lt;/span&gt;90 
                &lt;span style="color: blue"&gt;THEN  &lt;/span&gt;90 &lt;span style="color: blue"&gt;ELSE &lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;Lat &lt;span style="color: gray"&gt;+ &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeAngle&lt;span style="color: gray"&gt;()) &lt;/span&gt;&lt;span style="color: blue"&gt;END 
                
SET &lt;/span&gt;@Bot &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;CASE WHEN &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;Lat &lt;span style="color: gray"&gt;- &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeAngle&lt;span style="color: gray"&gt;() &amp;lt; -&lt;/span&gt;90 
                &lt;span style="color: blue"&gt;THEN &lt;/span&gt;&lt;span style="color: gray"&gt;-&lt;/span&gt;90 &lt;span style="color: blue"&gt;ELSE &lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;Lat &lt;span style="color: gray"&gt;- &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeAngle&lt;span style="color: gray"&gt;()) &lt;/span&gt;&lt;span style="color: blue"&gt;END 

&lt;/span&gt;&lt;span style="color: green"&gt;-- Create minimum Rectangle Bounding Box                
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@Env &lt;span style="color: blue"&gt;AS GEOGRAPHY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON ((' 
   &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;Long &lt;span style="color: gray"&gt;- &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeAngle&lt;span style="color: gray"&gt;())  + &lt;/span&gt;&lt;span style="color: red"&gt;' ' &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@Bot&lt;span style="color: gray"&gt;) + &lt;/span&gt;&lt;span style="color: red"&gt;','  &lt;/span&gt;&lt;span style="color: green"&gt;-- Lower Left
   &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;Long &lt;span style="color: gray"&gt;+ &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeAngle&lt;span style="color: gray"&gt;())  + &lt;/span&gt;&lt;span style="color: red"&gt;' ' &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@Bot&lt;span style="color: gray"&gt;) + &lt;/span&gt;&lt;span style="color: red"&gt;','  
   &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;Long &lt;span style="color: gray"&gt;+ &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeAngle&lt;span style="color: gray"&gt;())  + &lt;/span&gt;&lt;span style="color: red"&gt;' ' &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@Top&lt;span style="color: gray"&gt;) + &lt;/span&gt;&lt;span style="color: red"&gt;','  &lt;/span&gt;&lt;span style="color: green"&gt;-- Upper Right
   &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;Long &lt;span style="color: gray"&gt;- &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeAngle&lt;span style="color: gray"&gt;())  + &lt;/span&gt;&lt;span style="color: red"&gt;' ' &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@Top&lt;span style="color: gray"&gt;) + &lt;/span&gt;&lt;span style="color: red"&gt;','
   &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;Long &lt;span style="color: gray"&gt;- &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeAngle&lt;span style="color: gray"&gt;())  + &lt;/span&gt;&lt;span style="color: red"&gt;' ' &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@Bot&lt;span style="color: gray"&gt;) + &lt;/span&gt;&lt;span style="color: red"&gt;'))'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;4326&lt;span style="color: gray"&gt;);

&lt;/span&gt;&lt;span style="color: green"&gt;-- --&amp;lt; Show relationship between objects in SSMS's Spatial results tab &amp;gt;--
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@Env &lt;span style="color: blue"&gt;as &lt;/span&gt;Geog&lt;span style="color: gray"&gt;,  &lt;/span&gt;@Env&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;ToString&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;WKT&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'Envelope on Circle' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Method
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;620000&lt;span style="color: gray"&gt;), &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;620000&lt;span style="color: gray"&gt;).&lt;/span&gt;ToString&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'.EnvelopeCircle'
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g &lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;() , &lt;/span&gt;&lt;span style="color: red"&gt;'Exterior of Shape' 
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;20000&lt;span style="color: gray"&gt;), &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;ToString&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'.EnvelopeCenter'

&lt;/span&gt;&lt;span style="color: green"&gt;-- --&amp;lt; Creating a Line is easier to calculate the actual distance across your Envelope &amp;gt;--
-- -- Create a line from Lower Left to Top Right
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@Line1 &lt;span style="color: blue"&gt;AS GEOGRAPHY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING (' 
   &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;Long &lt;span style="color: gray"&gt;- &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeAngle&lt;span style="color: gray"&gt;())  + &lt;/span&gt;&lt;span style="color: red"&gt;' ' 
   &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@Bot&lt;span style="color: gray"&gt;) + &lt;/span&gt;&lt;span style="color: red"&gt;','
   &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;Long &lt;span style="color: gray"&gt;+ &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeAngle&lt;span style="color: gray"&gt;())  + &lt;/span&gt;&lt;span style="color: red"&gt;' ' 
   &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@Top&lt;span style="color: gray"&gt;) + &lt;/span&gt;&lt;span style="color: red"&gt;')'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;4326&lt;span style="color: gray"&gt;);

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@Line1&lt;span style="color: gray"&gt;.&lt;/span&gt;STLength&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;[STLength (meters)] &lt;span style="color: gray"&gt;, &lt;/span&gt;2 &lt;span style="color: gray"&gt;* &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeAngle&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;[2 * EnvelopeAngle()]&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn3ofnPerformanc_3A6/EnvelopeAngle.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="264" alt="EnvelopeAngle" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn3ofnPerformanc_3A6/EnvelopeAngle_thumb.png" width="250" border="0"&gt;&lt;/a&gt;&lt;br&gt;This diagram shows :-&lt;/p&gt;
&lt;p&gt;a) The &lt;strong&gt;original shape&lt;/strong&gt; (purple).&lt;br&gt;b) The &lt;strong&gt;EnvelopeCentre&lt;/strong&gt; (pink dot). I put a 20km circle around it so you could see it.&lt;br&gt;c) Drawing a 620km circle around the Centre shows the &lt;strong&gt;Bounding Circle&lt;/strong&gt; (brown). In this projection if the centre was a little more to the right, it would be possible to draw a smaller circle around this shape. But remember shape is actually on a sphere &amp;amp; what you are seeing is a flattened representation of that. &lt;br&gt;d) The Bounding box (Blue Square) is formed by adding/Subtracting the EnvelopeAngle to the EnvelopeCentre. Unfortunately it is the Envelope around the bounding circle &amp;amp; not around the Shape itself.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &lt;/p&gt;&lt;/blockquote&gt;
&lt;h1&gt;Method &lt;font color="#ff0000"&gt;.STConvexHull&lt;/font&gt;()&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;Find the smallest bounding shape that completely encloses this object. 
&lt;li&gt;Works with &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#804000"&gt;Geography&lt;/font&gt; Objects. 
&lt;li&gt;Uses 
&lt;ul&gt;
&lt;li&gt;Handy when repainting an invalid area of the screen if you animate or zoom this shape. 
&lt;li&gt;Good for finding points contained within this general path. 
&lt;li&gt;Great for creating areas when paths (linestrings) aren’t closed properly. 
&lt;li&gt;Excellent for converting points into Polygons. Especially groups of 3 point to create a mesh of little triangles you can Union together.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Sample code displaying using STConvexHull() to create a minimal perimeter &amp;gt;==
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@b &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(67 36,68 37,69 38,73 38,75 37,76 35,75 33,74 32,&lt;br&gt;                            71 32,69 33,58 39,55 40,52 40,50 39,48 36,48 34,50 31,52 30,56 30,60 31,62 33)'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;0&lt;span style="color: gray"&gt;);

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@b &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Geo'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'Line' &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;&lt;span style="color: red"&gt;'Labels'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;@b&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Text'
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@b&lt;span style="color: gray"&gt;.&lt;/span&gt;STConvexHull&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'STConvexHull()'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;@b&lt;span style="color: gray"&gt;.&lt;/span&gt;STConvexHull&lt;span style="color: gray"&gt;().&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;()&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn3ofnPerformanc_3A6/STConvexHull_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="147" alt="STConvexHull" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn3ofnPerformanc_3A6/STConvexHull_thumb.png" width="364" border="0"&gt;&lt;/a&gt;&lt;br&gt;&lt;span style="color: gray"&gt;&lt;font color="#000000"&gt;Purple is the original line. Blue is the minimum shape that fits around this line which has no concave areas.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;nbsp; &lt;/p&gt;&lt;/blockquote&gt;
&lt;h1&gt;Part B: Using .Reduce&lt;/h1&gt;
&lt;blockquote&gt;
&lt;p&gt;Below is a sample of putting the .Reduce method in your code. Often it is best to combine it with other filters.&lt;/p&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;font size="4"&gt;SELECT &lt;font color="#808080"&gt;[ID], ....&lt;/font&gt;&lt;/font&gt; 
&lt;p&gt;&lt;font size="4"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,[GEO].&lt;font color="#ff00ff"&gt;Reduce(&lt;font color="#0000ff"&gt;5000&lt;/font&gt;)&lt;/font&gt;&lt;/font&gt; 
&lt;p&gt;&lt;font color="#808080" size="4"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,[GEO].STLength() as 'Length'&lt;/font&gt; 
&lt;p&gt;&lt;font color="#808080" size="4"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,[GEO].Reduce(5000).STLength() as 'STLength'&lt;/font&gt; 
&lt;p&gt;&lt;font color="#808080" size="4"&gt;&lt;font color="#000000"&gt;FROM &lt;/font&gt;[GIS_Australia].[dbo].[coastl]&lt;/font&gt; 
&lt;p&gt;&lt;font size="4"&gt;WHERE [GEO].Reduce(&lt;font color="#0000ff"&gt;5000&lt;/font&gt;)&lt;font color="#ff00ff"&gt;.STLength() &amp;gt; 0&lt;/font&gt;&lt;/font&gt; 
&lt;p&gt;&lt;font color="#808080" size="4"&gt;AND ... &amp;lt; intersects my display area &amp;gt;&lt;/font&gt; 
&lt;p&gt;&lt;font color="#808080" size="4"&gt;AND ... &amp;lt; other filter criteria &amp;gt;&lt;/font&gt;&lt;font color="#808080" size="4"&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;/font&gt;&lt;font color="#808080" size="4"&gt;&lt;/font&gt;
&lt;h2&gt;&amp;nbsp;&lt;/h2&gt;
&lt;h1&gt;How do you calculate the minimum window around all your shapes?&lt;/h1&gt;&lt;pre class="code"&gt;&lt;/pre&gt;
&lt;h4&gt;&lt;u&gt;For Geometry&lt;/u&gt; &lt;/h4&gt;
&lt;p&gt;There are 4 alternatives I've considered so far. If you are in a hurry, just look at Option 3. It is typically twice as fast as Option 2, and 100 to 1,000 times faster than Option 1. I've only included the cursor based approaches as you may see them in other posts &amp;amp; wonder which is best. In the past 2 decades I'm been programming TSQL I've only found 2 queries that ran faster as a cursor than they did as Set Based queries. (and both a long time ago, on versions prior to SQL 2000). So as a rule if you can rewrite a query as a set based operation you get much better performance.&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Notes on different options&lt;/strong&gt;: Option 4 seems to deliver the same performance as Option 3. It is much easier to use. But means that you will need to register the CLR function in every database you use it on. As these routines were developed by members of the SQL Spatial Team it is a fair bet they will make their way into a future release of SQL Server.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Notes on Cursors&lt;/strong&gt;: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt; The cursor option "FAST_FORWARD" is the most efficient for this type of use; Read-Only, Read start to finish.&lt;/li&gt;
&lt;li&gt;Do not use the "With NOLOCK" hint, PageSplits may hurt you. (unless your testing shows it necessary &amp;amp; you understand what you are doing.)&lt;/li&gt;
&lt;li&gt;Instead, For spatial reference data, (roads, suburb boundaries, etc), Try putting the spatial table on a READONLY Filegroup. This too means no locks are taken &amp;amp; also reduces the size of your nightly backups.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h3&gt;Option 1. Cursor Based approach: Envelope around the Union of Shapes.&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;: Really CPU heavy &amp;amp; really slow for Reduce calculation. &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; But handy if you want to create a total union of a set of shapes.&lt;/p&gt;
&lt;p&gt;Step 1: Loop thru each shape, use STUnion to create a total shape outline.&lt;/p&gt;
&lt;p&gt;Step 2: Use Envelope methods to find the minimum bounding box around the total shape.&lt;/p&gt;
&lt;p&gt;Step 3: Find the ratio between the Display Window &amp;amp; the Envelope&lt;/p&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Option 1. Cursor Approach to Calculating a Envelope of Total Shapes &amp;gt;==
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@thisGeom &lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@totalGeom &lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@firstRow &lt;span style="color: blue"&gt;INT &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;1 &lt;span style="color: green"&gt;-- Used as BOOLEAN = TRUE

-- &amp;lt; Calculate a Total Shape &amp;gt;--
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;GeomCursor &lt;span style="color: blue"&gt;CURSOR FAST_FORWARD FOR SELECT &lt;/span&gt;[geom] &lt;span style="color: blue"&gt;FROM &lt;/span&gt;[GIS_NorthShore]&lt;span style="color: gray"&gt;.&lt;/span&gt;[dbo]&lt;span style="color: gray"&gt;.&lt;/span&gt;[NSW_Suburbs_region]&lt;span style="color: gray"&gt;; &lt;/span&gt;&lt;span style="color: green"&gt;-- WITH (NOLOCK);
&lt;/span&gt;&lt;span style="color: blue"&gt;OPEN &lt;/span&gt;GeomCursor&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;FETCH NEXT FROM &lt;/span&gt;GeomCursor &lt;span style="color: blue"&gt;INTO &lt;/span&gt;@thisGeom&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;WHILE &lt;/span&gt;&lt;span style="color: magenta"&gt;@@FETCH_STATUS &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;0
    &lt;span style="color: blue"&gt;BEGIN
        IF &lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@firstRow &lt;span style="color: gray"&gt;= &lt;/span&gt;1&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;BEGIN &lt;/span&gt;&lt;span style="color: green"&gt;-- First Row Returned
            &lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@totalGeom &lt;span style="color: gray"&gt;= &lt;/span&gt;@thisGeom&lt;span style="color: gray"&gt;;
            &lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@firstRow &lt;span style="color: gray"&gt;= &lt;/span&gt;0&lt;span style="color: gray"&gt;; &lt;/span&gt;&lt;span style="color: green"&gt;-- FALSE
        &lt;/span&gt;&lt;span style="color: blue"&gt;END&lt;/span&gt;&lt;span style="color: gray"&gt;;
        &lt;/span&gt;&lt;span style="color: blue"&gt;ELSE BEGIN
            SET &lt;/span&gt;@totalGeom &lt;span style="color: gray"&gt;= ( &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@totalGeom&lt;span style="color: gray"&gt;.&lt;/span&gt;STUnion&lt;span style="color: gray"&gt;(&lt;/span&gt;@thisGeom&lt;span style="color: gray"&gt;));
        &lt;/span&gt;&lt;span style="color: blue"&gt;END&lt;/span&gt;&lt;span style="color: gray"&gt;;
        &lt;/span&gt;&lt;span style="color: blue"&gt;FETCH NEXT FROM &lt;/span&gt;GeomCursor &lt;span style="color: blue"&gt;INTO &lt;/span&gt;@thisGeom&lt;span style="color: gray"&gt;;
    &lt;/span&gt;&lt;span style="color: blue"&gt;END&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;CLOSE &lt;/span&gt;GeomCursor&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;DEALLOCATE &lt;/span&gt;GeomCursor&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: green"&gt;-- SELECT @totalGeom as [Total Shape] -- Use this if you really need the total shape

-- &amp;lt; Get the Envelope of the Total Shape &amp;gt;--
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@totalGeom &lt;span style="color: gray"&gt;= &lt;/span&gt;@totalGeom&lt;span style="color: gray"&gt;.&lt;/span&gt;STEnvelope&lt;span style="color: gray"&gt;()
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@totalGeom&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;1&lt;span style="color: gray"&gt;).&lt;/span&gt;STX &lt;span style="color: blue"&gt;AS &lt;/span&gt;[Left]&lt;span style="color: gray"&gt;, &lt;/span&gt;@totalGeom&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;1&lt;span style="color: gray"&gt;).&lt;/span&gt;STY &lt;span style="color: blue"&gt;AS &lt;/span&gt;[Bottom]&lt;span style="color: gray"&gt;, &lt;/span&gt;@totalGeom&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;3&lt;span style="color: gray"&gt;).&lt;/span&gt;STX &lt;span style="color: blue"&gt;AS &lt;/span&gt;[Right]&lt;span style="color: gray"&gt;,  &lt;/span&gt;@totalGeom&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;3&lt;span style="color: gray"&gt;).&lt;/span&gt;STY &lt;span style="color: blue"&gt;AS &lt;/span&gt;[Top]&lt;span style="color: gray"&gt;; 
&lt;/span&gt;&lt;span style="color: blue"&gt;go&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;Option 2. Cursor Based approach: Union of all the Envelopes of Shapes.&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Summary&lt;/strong&gt;: Much improved, but cursor causes internal overhead.&lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;p&gt;Step 1: Loop thru each shape, Use Envelope methods to find its minimum bounding box &lt;/p&gt;
&lt;p&gt;Step 2: Use STUnion to create a total outline of all the shape envelopes.&lt;/p&gt;
&lt;p&gt;Step 3: Find the ratio between the Display Window &amp;amp; the Total Envelope&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Option 2. Cursor Approach to Calculating Total of Shapes Envelopes &amp;gt;==
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@thisGeom &lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@totalGeom &lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@firstRow &lt;span style="color: blue"&gt;INT &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;1 &lt;span style="color: green"&gt;-- Used as BOOLEAN = TRUE

&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;GeomCursor &lt;span style="color: blue"&gt;CURSOR FAST_FORWARD FOR SELECT &lt;/span&gt;[geom] &lt;span style="color: blue"&gt;FROM &lt;/span&gt;[GIS_NorthShore]&lt;span style="color: gray"&gt;.&lt;/span&gt;[dbo]&lt;span style="color: gray"&gt;.&lt;/span&gt;[NSW_Suburbs_region]&lt;span style="color: gray"&gt;; &lt;/span&gt;&lt;span style="color: green"&gt;-- WITH (NOLOCK);
&lt;/span&gt;&lt;span style="color: blue"&gt;OPEN &lt;/span&gt;GeomCursor&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;FETCH NEXT FROM &lt;/span&gt;GeomCursor &lt;span style="color: blue"&gt;INTO &lt;/span&gt;@thisGeom&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;WHILE &lt;/span&gt;&lt;span style="color: magenta"&gt;@@FETCH_STATUS &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;0
    &lt;span style="color: blue"&gt;BEGIN
        IF &lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@firstRow &lt;span style="color: gray"&gt;= &lt;/span&gt;1&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;BEGIN &lt;/span&gt;&lt;span style="color: green"&gt;-- First Row Returned
            &lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@totalGeom &lt;span style="color: gray"&gt;= &lt;/span&gt;@thisGeom&lt;span style="color: gray"&gt;;
            &lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@firstRow &lt;span style="color: gray"&gt;= &lt;/span&gt;0&lt;span style="color: gray"&gt;; &lt;/span&gt;&lt;span style="color: green"&gt;-- FALSE
        &lt;/span&gt;&lt;span style="color: blue"&gt;END&lt;/span&gt;&lt;span style="color: gray"&gt;;
        &lt;/span&gt;&lt;span style="color: blue"&gt;ELSE BEGIN
            SET &lt;/span&gt;@totalGeom &lt;span style="color: gray"&gt;= ( &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@totalGeom&lt;span style="color: gray"&gt;.&lt;/span&gt;STUnion&lt;span style="color: gray"&gt;(&lt;/span&gt;@thisGeom&lt;span style="color: gray"&gt;).&lt;/span&gt;STEnvelope&lt;span style="color: gray"&gt;()); &lt;/span&gt;&lt;span style="color: green"&gt;-- NB: Envelope of Shape
        &lt;/span&gt;&lt;span style="color: blue"&gt;END&lt;/span&gt;&lt;span style="color: gray"&gt;;
        &lt;/span&gt;&lt;span style="color: blue"&gt;FETCH NEXT FROM &lt;/span&gt;GeomCursor &lt;span style="color: blue"&gt;INTO &lt;/span&gt;@thisGeom&lt;span style="color: gray"&gt;;
    &lt;/span&gt;&lt;span style="color: blue"&gt;END&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;CLOSE &lt;/span&gt;GeomCursor&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;DEALLOCATE &lt;/span&gt;GeomCursor&lt;span style="color: gray"&gt;;

&lt;/span&gt;&lt;span style="color: green"&gt;-- &amp;lt; Return Envelope &amp;gt;--
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@totalGeom&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;1&lt;span style="color: gray"&gt;).&lt;/span&gt;STX &lt;span style="color: blue"&gt;AS &lt;/span&gt;[Left]&lt;span style="color: gray"&gt;,  &lt;/span&gt;@totalGeom&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;1&lt;span style="color: gray"&gt;).&lt;/span&gt;STY &lt;span style="color: blue"&gt;AS &lt;/span&gt;[Bottom]&lt;br&gt;      &lt;span style="color: gray"&gt;,&lt;/span&gt;@totalGeom&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;3&lt;span style="color: gray"&gt;).&lt;/span&gt;STX &lt;span style="color: blue"&gt;AS &lt;/span&gt;[Right]&lt;span style="color: gray"&gt;, &lt;/span&gt;@totalGeom&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;3&lt;span style="color: gray"&gt;).&lt;/span&gt;STY &lt;span style="color: blue"&gt;AS &lt;/span&gt;[Top]&lt;span style="color: gray"&gt;; 
&lt;/span&gt;&lt;span style="color: blue"&gt;go&lt;/span&gt;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;h3&gt;Option 3. Set Based approach: Find the extreme edges of the Shape Envelopes.&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;: Best approach found so far. 
&lt;p&gt;Step 1: Use Envelope methods to find its minimum bounding box of each row. 
&lt;p&gt;Step 2: Return the Left, Lowest, Right &amp;amp; Topmost points of each shape envelope. 
&lt;p&gt;Step 3: Use TSQL Aggregate functions to return the Min of Left &amp;amp; Lowest, and the Max of Right &amp;amp; Top. 
&lt;p&gt;Step 4: Use the coordinates to create an Envelope (or just a line you can get the Distance from) 
&lt;p&gt;Step 5: Find the ratio between the Display Window &amp;amp; the Total Envelope.&lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ===&amp;lt; Option 3: Efficient way to calculate the Minimum Bounding Box of a large set of Geometric Objects &amp;gt;===
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@Left &lt;span style="color: blue"&gt;FLOAT
DECLARE &lt;/span&gt;@Lower &lt;span style="color: blue"&gt;FLOAT
DECLARE &lt;/span&gt;@Right &lt;span style="color: blue"&gt;FLOAT
DECLARE &lt;/span&gt;@Top &lt;span style="color: blue"&gt;FLOAT

SELECT  &lt;/span&gt;@Left &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: magenta"&gt;MIN&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;[geom]&lt;span style="color: gray"&gt;.&lt;/span&gt;STEnvelope&lt;span style="color: gray"&gt;().&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;1&lt;span style="color: gray"&gt;).&lt;/span&gt;STX&lt;span style="color: gray"&gt;) 
       ,&lt;/span&gt;@Lower &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: magenta"&gt;MIN&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;[geom]&lt;span style="color: gray"&gt;.&lt;/span&gt;STEnvelope&lt;span style="color: gray"&gt;().&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;1&lt;span style="color: gray"&gt;).&lt;/span&gt;STY&lt;span style="color: gray"&gt;)   
       ,&lt;/span&gt;@Right &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: magenta"&gt;MAX&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;[geom]&lt;span style="color: gray"&gt;.&lt;/span&gt;STEnvelope&lt;span style="color: gray"&gt;().&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;3&lt;span style="color: gray"&gt;).&lt;/span&gt;STX&lt;span style="color: gray"&gt;) 
       ,&lt;/span&gt;@Top &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: magenta"&gt;MAX&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;[geom]&lt;span style="color: gray"&gt;.&lt;/span&gt;STEnvelope&lt;span style="color: gray"&gt;().&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;3&lt;span style="color: gray"&gt;).&lt;/span&gt;STY&lt;span style="color: gray"&gt;) 
&lt;/span&gt;&lt;span style="color: blue"&gt;FROM &lt;/span&gt;[GIS_NorthShore]&lt;span style="color: gray"&gt;.&lt;/span&gt;[dbo]&lt;span style="color: gray"&gt;.&lt;/span&gt;[NSW_Suburbs_region]

&lt;span style="color: green"&gt;-- --- Either Calculate the diagonal distance across the Bounding box. ---
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@Diagonal &lt;span style="color: blue"&gt;GEOMETRY
SET &lt;/span&gt;@Diagonal &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING (' 
            &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@Left&lt;span style="color: gray"&gt;)  + &lt;/span&gt;&lt;span style="color: red"&gt;' ' &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@Lower&lt;span style="color: gray"&gt;) + &lt;/span&gt;&lt;span style="color: red"&gt;','
            &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@Right&lt;span style="color: gray"&gt;)  + &lt;/span&gt;&lt;span style="color: red"&gt;' ' &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;CONVERT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;varchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;),&lt;/span&gt;@Top&lt;span style="color: gray"&gt;) + &lt;/span&gt;&lt;span style="color: red"&gt;')'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;4326&lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@Diagonal &lt;span style="color: blue"&gt;as &lt;/span&gt;Diagonal&lt;span style="color: gray"&gt;, &lt;/span&gt;@Diagonal&lt;span style="color: gray"&gt;.&lt;/span&gt;STLength&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as Length&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;@Diagonal&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;WKT
       &lt;span style="color: gray"&gt;,&lt;/span&gt;@Left &lt;span style="color: blue"&gt;as &lt;/span&gt;[Left]&lt;span style="color: gray"&gt;, &lt;/span&gt;@Lower &lt;span style="color: blue"&gt;as &lt;/span&gt;[Lower]&lt;span style="color: gray"&gt;, &lt;/span&gt;@Right &lt;span style="color: blue"&gt;as &lt;/span&gt;[Right]&lt;span style="color: gray"&gt;, &lt;/span&gt;@Top &lt;span style="color: blue"&gt;as &lt;/span&gt;[Top] 

&lt;span style="color: green"&gt;-- --- Or Return an Envelope. ---
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@Diagonal&lt;span style="color: gray"&gt;.&lt;/span&gt;STEnvelope&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Envelope&lt;/pre&gt;&lt;pre class="code"&gt;&amp;nbsp;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;h3&gt;Option 4.&amp;nbsp; CLR Based approach: Use Extension provided by SQL team members.&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;: Easiest to use, but requires extra install steps.&lt;/p&gt;
&lt;p&gt;Step 1: Download extra functionality from &lt;a title="Extra transformations, aggregates, data conversion routines provided by SQL Spatial Team members" href="http://www.codeplex.com/sqlspatialtools"&gt;Codeplex: SQL Server Spatial Tools&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Step 2: Install it on any Database you which to use it on.&lt;/p&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ===&amp;lt; Option 4: Use the specific CLR function from Codeplex: SQL Server Spatial Tools &amp;gt;===
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;dbo&lt;span style="color: gray"&gt;.&lt;/span&gt;GeometryEnvelopeAggregate&lt;span style="color: gray"&gt;(&lt;/span&gt;geom&lt;span style="color: gray"&gt;)
&lt;/span&gt;&lt;span style="color: blue"&gt;FROM &lt;/span&gt;dbo&lt;span style="color: gray"&gt;.&lt;/span&gt;NSW_Suburbs_region&lt;/pre&gt;&lt;pre class="code"&gt;&amp;nbsp;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;h4&gt;&lt;u&gt;For Geography&lt;/u&gt;&lt;/h4&gt;
&lt;p&gt;I'd like to tell you I have this one totally thought thru, but I don't. The potential that the bounding circle(s) will exceed the object, &amp;amp; blow out past +/-90 deg Latitude, causes me to be concerned about most approaches derived from EnvelopeCentre &amp;amp; EnvelopeAngle. Yes, this can be solved with an IF statement &amp;amp; only likely to effect those interested in areas near the poles. We still have the issue of compounded errors creating a much larger error, which confuses our .Reduce variable. 
&lt;h3&gt;Option 1: Convert all geography elements to Geometry &amp;amp; use the geometry options above. &lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;I've copied a snippet of the code below from &lt;a title="Small snippet from larger example" href="http://johanneskebeck.spaces.live.com/blog/cns!42E1F70205EC8A96!4983.entry"&gt;Johannes Kebeck's Blog - Virtual Earth &amp;amp; SQL Server 2008: Better Together&lt;/a&gt; just to show you how to convert from Geography to/from Geometry. Once converted to Geometry you could use the approaches above. I'm a little nervous about this approach as swapping back &amp;amp; forth may introduce translation errors. Note: This idea is very different use to the point Johannes is making. He is converting Geometry to Geography to get more accurate real world distance calculations, that idea should work fine.&lt;/p&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;--Determine max and min Latitude
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@BBOX&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;1&lt;span style="color: gray"&gt;).&lt;/span&gt;STY &lt;span style="color: blue"&gt;AS &lt;/span&gt;[Min Latitude]&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@BBOX&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;3&lt;span style="color: gray"&gt;).&lt;/span&gt;STY &lt;span style="color: blue"&gt;AS &lt;/span&gt;[Max Latitude]&lt;span style="color: gray"&gt;;

&lt;/span&gt;&lt;span style="color: green"&gt;--Create GEOGRAPHY from GEOMETRY to calculate distances in meters
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@BBOX_GEOG &lt;span style="color: blue"&gt;geography&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@BBOX_GEOG &lt;span style="color: gray"&gt;= (&lt;/span&gt;&lt;span style="color: blue"&gt;geography&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromWKB&lt;span style="color: gray"&gt;(&lt;/span&gt;@BBOX&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsBinary&lt;span style="color: gray"&gt;(), &lt;/span&gt;4326&lt;span style="color: gray"&gt;));

&lt;/span&gt;&lt;span style="color: green"&gt;--Calculate Distance
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: magenta"&gt;ROUND&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@BBOX_GEOG&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;1&lt;span style="color: gray"&gt;).&lt;/span&gt;STDistance&lt;span style="color: gray"&gt;(&lt;/span&gt;@BBOX_GEOG&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;2&lt;span style="color: gray"&gt;)),&lt;/span&gt;2&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;[X-Dist at Min-Lat]&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: magenta"&gt;ROUND&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@BBOX_GEOG&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;3&lt;span style="color: gray"&gt;).&lt;/span&gt;STDistance&lt;span style="color: gray"&gt;(&lt;/span&gt;@BBOX_GEOG&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;4&lt;span style="color: gray"&gt;)),&lt;/span&gt;2&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;[X-Dist at Max-Lat]&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: magenta"&gt;ROUND&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@BBOX_GEOG&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;2&lt;span style="color: gray"&gt;).&lt;/span&gt;STDistance&lt;span style="color: gray"&gt;(&lt;/span&gt;@BBOX_GEOG&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointN&lt;span style="color: gray"&gt;(&lt;/span&gt;3&lt;span style="color: gray"&gt;)),&lt;/span&gt;2&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;[Y-Dist]&lt;span style="color: gray"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/blockquote&gt;
&lt;h3&gt;Option 2. Use Spatial Tools to Convert all geography elements to Geometry &amp;amp; use the geometry options above.&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Similar to Option 1 except you use the Spatial Tools from from &lt;a title="Extra transformations, aggregates, data conversion routines provided by SQL Spatial Team members" href="http://www.codeplex.com/sqlspatialtools"&gt;Codeplex: SQL Server Spatial Tools&lt;/a&gt; to do the work.&lt;/p&gt;
&lt;p&gt;You can convert from Geometry to Geography using &lt;strong&gt;VacuousGeometryToGeography()&lt;/strong&gt; then use &lt;strong&gt;GeometryEnvelopeAggregate&lt;span style="color: gray"&gt;(&lt;/span&gt;geom&lt;/strong&gt;&lt;span style="color: gray"&gt;&lt;strong&gt;)&lt;/strong&gt;&amp;nbsp;&lt;/span&gt;as we did in geometry Option 4 above.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/blockquote&gt;
&lt;h3&gt;Option 3. Use the Codeplex Spatial Tools methods, to create an aggregate shape.&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Step 1. Create a Total Shape using spatial tools GeographyUnionAggregate method.&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;dbo&lt;span style="color: gray"&gt;.&lt;/span&gt;GeographyUnionAggregate&lt;span style="color: gray"&gt;(&lt;/span&gt;geog&lt;span style="color: gray"&gt;,&lt;/span&gt;1&lt;span style="color: gray"&gt;)
&lt;/span&gt;&lt;span style="color: blue"&gt;FROM &lt;/span&gt;dbo&lt;span style="color: gray"&gt;.&lt;/span&gt;Suburbs_Geog_Region &lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;Step 2. Then use EnvelopeCentre &amp;amp; EnvelopeAngle as described earlier in this post. &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/blockquote&gt;
&lt;h3&gt;Option 4: Run thru all points in all shapes &amp;amp; store Min(Lower Left), Max(Top Right). Then use Geometry Option 3 above.&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;: This has the potential to be much faster than other approaches. But means some kind of preprocessing or CLR function (which has potential to be heavy on CPU) &lt;/p&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;Step 1. Create 4 new columns in your "Shapes" table; Left, Right, Top, Bottom. 
&lt;p&gt;Step 2. Using a Trigger, Batch Job or your CLR function, scan thru all points in the geography shape &amp;amp; extract the leftmost point, the topmost point, etc &amp;amp; store them in this row. 
&lt;p&gt;Step 3.&amp;nbsp; Use query similar to Geometry Option 3 above to find Min &amp;amp; Max values of the ResultSet. Build your Envelope from there.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;span style="color: gray"&gt;&lt;/span&gt;&amp;nbsp; &lt;h1&gt;Summary&lt;/h1&gt;
&lt;blockquote&gt;
&lt;p&gt;All the above assumes you are retrieving rows based on some criteria, eg: High Crime Areas, &amp;amp; attempting to centre that information in a display. If you are coming from the other direction, eg: the user drew a rectangle on a map &amp;amp; wanted to Zoom In on that area. That is a much easier problem to solve. Just use the size of the rectangle to calculate your "Reduce" value. Also use the STIntersects method to retrieve the relevant spatial data.&amp;nbsp; &lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;h1&gt;Final Thoughts - Stating the Obvious.&lt;/h1&gt;
&lt;p&gt;.Reduce is a powerful method for reducing the rendering overhead of your application. But its power comes at a cost, it does require CPU cycles on the server. If you call it twice for each row (once to select &amp;amp; again in the where clause to discard 0 length rows) it could take longer to execute your queries.&amp;nbsp; (still might be faster to render)&lt;/p&gt;
&lt;p&gt;There are other ways that are more effective in reducing the number of rows your query returns. &lt;font color="#ff0080"&gt;DON'T FORGET THE WHERE CLAUSE&lt;/font&gt;. Use a rectangle to only retrieve the rows you want to display. Then consider using Reduce on that result set.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Sample: Using WHERE filter to only return shapes you want to display &amp;gt;==&lt;br&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE&lt;/span&gt;@DisplayWindow &lt;span style="color: blue"&gt;AS GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((151.2 -33.9, 151.3 -33.9, 151.3 -33.7, 151.2 -33.7, 151.2 -33.9))'&lt;br&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;ID&lt;span style="color: gray"&gt;,&lt;/span&gt;GEOM&lt;br&gt;&lt;span style="color: blue"&gt;FROM&amp;nbsp; &lt;/span&gt;dbo&lt;span style="color: gray"&gt;.&lt;/span&gt;NSW_Suburbs_region&lt;br&gt;&lt;span style="color: blue"&gt;WHERE &lt;/span&gt;geom&lt;span style="color: gray"&gt;.&lt;/span&gt;STIntersects&lt;span style="color: gray"&gt;(&lt;/span&gt;@DisplayWindow&lt;span style="color: gray"&gt;) =&lt;/span&gt;1&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- Sorry: this sample can't just be pasted into SSMS as it requires a table you don't have&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;h3&gt;More info&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;For more info see SQL Server 2008 Books Online &lt;a title="Info on Reduce, EnvelopeAngle, EnvelopeCentre etc." href="http://msdn.microsoft.com/en-us/library/bb933968.aspx" target="_blank"&gt;Extended Methods on Geography Instances&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Please give feedback. Is this series of articles useful? Did it save you time? What was good, What could be better?, Notice any errors? What would you like me to cover? All thoughts, comments, suggestions welcome.&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:041a202a-4194-4bba-840c-8f2089822cd5" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/SQL%20Server%20Spatial" rel="tag"&gt;SQL Server Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Spatial" rel="tag"&gt;Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/TSQL" rel="tag"&gt;TSQL&lt;/a&gt;,&lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9017332" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/davidlean/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Spatial/default.aspx">Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/TSQL/default.aspx">TSQL</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server+Spatial/default.aspx">SQL Server Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geometry/default.aspx">Geometry</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Polygon/default.aspx">Polygon</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geography/default.aspx">Geography</category></item><item><title>SQL 2008 Spatial Samples, Part 6 of 9 - Geometric “Set Theory” Methods</title><link>http://blogs.msdn.com/davidlean/archive/2008/10/25/sql-2008-spatial-samples-part-n-2-on-n-geometric-set-theory-methods.aspx</link><pubDate>Sat, 25 Oct 2008 08:40:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9015780</guid><dc:creator>davele</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/davidlean/comments/9015780.aspx</comments><wfw:commentRss>http://blogs.msdn.com/davidlean/commentrss.aspx?PostID=9015780</wfw:commentRss><wfw:comment>http://blogs.msdn.com/davidlean/rsscomments.aspx?PostID=9015780</wfw:comment><description>&lt;p&gt;This post covers the following methods: STUnion, STIntersection, STDifference, STSymDifference, STCentroid / STEnvelopeCentre, STPointOnSurface, STBoundary.&lt;/p&gt;  &lt;h4&gt;Overview of Geometric &amp;quot;Set Theory&amp;quot; Methods&lt;/h4&gt;  &lt;p&gt;These Methods help to understand the relationship between spatial objects &amp;amp; also to create new objects that show that relationship.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: The sample code in the post shows more complex shapes than the diagrams attached. Try cutting the code into SSMS &amp;amp; look at the results.. &lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:B3E14793-948F-49af-A347-D19C374A7C4F:efb7ac78-fad3-457c-b5aa-c71898401a32" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt; &lt;script type="text/javascript"&gt;&lt;!--
digg_bodytext = 'This post covers the following methods: STUnion, STIntersection, STDifference, STSymDifference, STCentroid / STEnvelopeCentre, STPointOnSurface, STBoundary.';
//--&gt;&lt;/script&gt; &lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h1&gt;Method .&lt;font color="#ff0000"&gt;STUnion&lt;/font&gt;()&lt;/h1&gt;  &lt;ul&gt;   &lt;li&gt;Combines to Spatial Objects.      &lt;ul&gt;       &lt;li&gt;Creates a &amp;quot;Binary OR&amp;quot; of the two objects (shapes, lines, points). &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;font color="#804000"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/font&gt; objects &lt;/li&gt;    &lt;li&gt;Sister method of .STIntersection Method.      &lt;p&gt;Syntax: geo1.STUnion(geo2)&lt;/p&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;Example use: Combine all &amp;quot;Mobile Phone Tower&amp;quot; coverage areas to show &amp;quot;complete phone coverage&amp;quot; for an area.      &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn2onnGeometricS_C45F/STUnion_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="189" alt="STUnion" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn2onnGeometricS_C45F/STUnion_thumb.png" width="342" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;    &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Sample: STUnion &amp;gt;===
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@a &lt;span style="color: blue"&gt;GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((1 0, 1 8, 6 4, 1 0))'
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@b &lt;span style="color: blue"&gt;GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((4 1, 3 5, 4 9, 9 5, 4 1),(4 5, 5 7, 6 7, 4 4, 4 5) )'

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@a&lt;span style="color: gray"&gt;.&lt;/span&gt;STUnion&lt;span style="color: gray"&gt;(&lt;/span&gt;@b&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'STUnion'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'STUnion' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Labels

&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@a &lt;span style="color: blue"&gt;as &lt;/span&gt;geo&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'A' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Labels
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@b&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'B'&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="code"&gt;&lt;span style="color: red"&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;h1&gt;Tip: How to &amp;#8220;Union&amp;#8221; many rows&lt;/h1&gt;

&lt;blockquote&gt;
  &lt;p&gt;Quite frequently you may want to aggregate a set of base shapes into a larger region. Unfortunately it seems the Open Geospatial Consortium did not spec out their functions to take an entire set of Spatial Objects as a parameter, just one at a time. While that is fine when using them in your code (VB, C# etc), it is less desirable when working with a relational database which comes with an extremely fast way to process &amp;quot;Sets&amp;quot; of data.&lt;/p&gt;

  &lt;p&gt;This is a sample of using TSQL apply a Spatial Method to a set of rows. While an aggregate UNION of rows is more frequently used than any of the other methods ie: STDifference, this approach will work for them too. Personally I&amp;#8217;m not a big fan of the Cursor Approach to geometric shape manipulation. I did write a Set Based solution to finding the total envelope of a collection of shapes. &amp;lt;see my comments on .Reduce in another Post&amp;gt;. It is at worse 50% faster &amp;amp; so far at best 1,037 TIMES faster than the cursor algorithm below. I&amp;#8217;m am looking for a better algorithm for Unions too, I expect that the solution will be an CLR Aggregate function. &lt;/p&gt;

  &lt;p&gt;Still, aggregating shapes is a mathematically complex problem with many interesting niche scenarios. In the absence of a better solution, if you do find this is not performing well, I&amp;#8217;d suggest you look at a variety of alternatives designs. Eg: &lt;/p&gt;

  &lt;ol&gt;
    &lt;li&gt;Pre-Aggregate your Union shapes, perhaps a batch job off-hours. &lt;/li&gt;

    &lt;li&gt;Off-load the Server CPU, Run the UNION on the Client, &lt;/li&gt;

    &lt;li&gt;Run multiple parallel queries each selecting a different subset of the rows, UNION the rows each subset in parallel &amp;amp; then UNION the subsets together. &lt;/li&gt;

    &lt;li&gt;Use Reduce to strip the detail from your polygons, Union them. Find the boundary, use STRelate to find all the Reduced polygons that live on the aggregate border. Union all the originals of those together &amp;amp; generate a very accurate external border. This is a long process &amp;amp; clearly only worthwhile when you are joining 1,000&amp;#8217;s of highly detailed shapes with no internal holes. &lt;/li&gt;

    &lt;li&gt;Combine some of the alternatives above, ie: 1 &amp;amp; 3, perhaps pre-aggregate some of the shapes. &lt;/li&gt;
  &lt;/ol&gt;

  &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; I've seen similar code sample posts in the Internet using hints like &amp;quot;WITH (NOLOCK)&amp;quot;. Using Hints is an extremely poor practice. DO NOT DO IT unless you have strong empirical evidence, that you needed it to solve an issue on YOUR system. Even then you should document in the code; What you tested, the difference you found, the version you tested it on &amp;amp; maybe the alternatives you considered. NOLOCK in particular can has some really nasty side-effects &amp;amp; should be used with caution.&lt;/p&gt;

  &lt;h4&gt;Sample code: loops thru a resultset generating a larger UNIONed Region.&lt;/h4&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;&lt;span style="color: black"&gt;@thisGeom &lt;/span&gt;&lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: black"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;&lt;span style="color: black"&gt;@totalGeom &lt;/span&gt;&lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: black"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;&lt;span style="color: black"&gt;@firstRow &lt;/span&gt;&lt;span style="color: blue"&gt;INT &lt;/span&gt;&lt;span style="color: black"&gt;= 1 &lt;/span&gt;&lt;span style="color: #009900"&gt;-- Used as BOOLEAN = TRUE

&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;&lt;span style="color: black"&gt;GeomCursor &lt;/span&gt;&lt;span style="color: blue"&gt;CURSOR FAST_FORWARD FOR SELECT &lt;/span&gt;&lt;span style="color: black"&gt;[geom] &lt;/span&gt;&lt;span style="color: blue"&gt;FROM &lt;/span&gt;&lt;span style="color: black"&gt;[dbo].[NSW_Suburbs_region]; &lt;/span&gt;&lt;span style="color: #009900"&gt;-- WITH (NOLOCK);
&lt;/span&gt;&lt;span style="color: blue"&gt;OPEN &lt;/span&gt;&lt;span style="color: black"&gt;GeomCursor;
&lt;/span&gt;&lt;span style="color: blue"&gt;FETCH NEXT FROM &lt;/span&gt;&lt;span style="color: black"&gt;GeomCursor &lt;/span&gt;&lt;span style="color: blue"&gt;INTO &lt;/span&gt;&lt;span style="color: black"&gt;@thisGeom;
&lt;/span&gt;&lt;span style="color: blue"&gt;WHILE &lt;/span&gt;&lt;span style="color: magenta"&gt;@@FETCH_STATUS &lt;/span&gt;&lt;span style="color: black"&gt;= 0
    &lt;/span&gt;&lt;span style="color: blue"&gt;BEGIN
        IF &lt;/span&gt;&lt;span style="color: black"&gt;(@firstRow = 1) &lt;/span&gt;&lt;span style="color: blue"&gt;BEGIN &lt;/span&gt;&lt;span style="color: #009900"&gt;-- First Row Returned
            &lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;&lt;span style="color: black"&gt;@totalGeom = @thisGeom;
            SET @firstRow = 0; &lt;/span&gt;&lt;span style="color: #009900"&gt;-- FALSE
        &lt;/span&gt;&lt;span style="color: blue"&gt;END ELSE BEGIN
            SET &lt;/span&gt;&lt;span style="color: black"&gt;@totalGeom = ( &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: black"&gt;@totalGeom&lt;/span&gt;&lt;span style="color: red"&gt;.STUnion&lt;/span&gt;&lt;span style="color: black"&gt;(@thisGeom)) ;
        &lt;/span&gt;&lt;span style="color: blue"&gt;END&lt;/span&gt;&lt;span style="color: black"&gt;;
        &lt;/span&gt;&lt;span style="color: blue"&gt;FETCH NEXT FROM &lt;/span&gt;&lt;span style="color: black"&gt;GeomCursor &lt;/span&gt;&lt;span style="color: blue"&gt;INTO &lt;/span&gt;&lt;span style="color: black"&gt;@thisGeom;
    &lt;/span&gt;&lt;span style="color: blue"&gt;END&lt;/span&gt;&lt;span style="color: black"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;CLOSE &lt;/span&gt;&lt;span style="color: black"&gt;GeomCursor;
&lt;/span&gt;&lt;span style="color: blue"&gt;DEALLOCATE &lt;/span&gt;&lt;span style="color: black"&gt;GeomCursor;

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: black"&gt;@totalGeom &lt;span style="color: #009900"&gt;-- The total Union of the resultset &lt;/span&gt;
&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;pre class="code"&gt;&lt;span style="color: red"&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;h1&gt;Method &lt;font color="#ff0000"&gt;.STIntersection&lt;/font&gt;()&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;Find the area where the 2 Objects overlap. (show what is in both) 
    &lt;ul&gt;
      &lt;li&gt;Creates a &amp;quot;Binary AND&amp;quot; of the two objects (shapes, lines, points). &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;font color="#804000"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/font&gt; objects &lt;/li&gt;

  &lt;li&gt;Opposite method of .STSymDifference Method. &lt;/li&gt;
&lt;/ul&gt;

&lt;ul&gt;
  &lt;p&gt;Syntax: geo1.STIntersection(geo2)&lt;/p&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example use: Show suburbs with &amp;gt; 5% Jewish population together with our Stores that promote &amp;quot;Ham &amp;amp; Pineapple&amp;quot; Pizza's 
    &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn2onnGeometricS_C45F/STIntersection_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="169" alt="STIntersection" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn2onnGeometricS_C45F/STIntersection_thumb.png" width="331" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Sample: STIntersection &amp;gt;===
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@a &lt;span style="color: blue"&gt;GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((1 0, 1 8, 6 4, 1 0))'
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@b &lt;span style="color: blue"&gt;GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((4 1, 3 5, 4 9, 9 5, 4 1),(4 5, 5 7, 6 7, 4 4, 4 5) )'

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@a&lt;span style="color: gray"&gt;.&lt;/span&gt;STIntersection&lt;span style="color: gray"&gt;(&lt;/span&gt;@b&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'STIntersection'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'STIntersection' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Labels

&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@a &lt;span style="color: blue"&gt;as &lt;/span&gt;geo&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'A' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Labels
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@b&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'B'&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="code"&gt;&lt;span style="color: red"&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;h1&gt;Method &lt;font color="#ff0000"&gt;.STSymDifference&lt;/font&gt;()&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;Find the area where the 2 Objects do not overlap. (what is one or other but not in both) 
    &lt;ul&gt;
      &lt;li&gt;Creates a &amp;quot;Binary XOR&amp;quot; of the two objects (shapes, lines, points). &lt;/li&gt;

      &lt;li&gt;Creates a multishape &amp;quot;GeometryCollection&amp;quot; which only has either 1 region or the other. Any intersection containing both shapes is removed. &lt;/li&gt;

      &lt;li&gt;Short for &lt;a title="Link to Wikipedia giving more details on Symmetric Difference" href="http://en.wikipedia.org/wiki/Symmetric_difference" target="_blank"&gt;Symmetric Difference&lt;/a&gt; &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;font color="#804000"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/font&gt; objects &lt;/li&gt;

  &lt;li&gt;Opposite method of .STIntersection Method. 
    &lt;p&gt;Syntax: geo1.STSymDifference(geo2)&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example use: In a new land development, Show all blocks of land with Electricity connected, combined with block of land with Water &amp;amp; sewerage connected. This would highlight blocks that need the least investment to bring them up to saleable condition. 
    &lt;br /&gt;&amp;#160;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn2onnGeometricS_C45F/SymDifference_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="194" alt="SymDifference" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn2onnGeometricS_C45F/SymDifference_thumb.png" width="356" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Sample: STSymDifference &amp;gt;===
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@a &lt;span style="color: blue"&gt;GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((1 0, 1 8, 6 4, 1 0))'
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@b &lt;span style="color: blue"&gt;GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((4 1, 3 5, 4 9, 9 5, 4 1),(4 5, 5 7, 6 7, 4 4, 4 5) )'

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@a&lt;span style="color: gray"&gt;.&lt;/span&gt;STSymDifference&lt;span style="color: gray"&gt;(&lt;/span&gt;@b&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'STSymDifference'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'STSymDifference' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Labels

&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@a &lt;span style="color: blue"&gt;as &lt;/span&gt;geo&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'A' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Labels
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@b&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'B'&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="code"&gt;&lt;span style="color: red"&gt;&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;h1&gt;Method &lt;font color="#ff0000"&gt;.STDifference&lt;/font&gt;()&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;Remove the parameter shape from this shape. 
    &lt;ul&gt;
      &lt;li&gt;Equivalent of Subtraction (Arithmetic's minus operation). &lt;/li&gt;

      &lt;li&gt;Unlike the other methods in this post, the order is important. A.STDifference(B) yields a different result to B.STDifference(A) &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;font color="#804000"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/font&gt; objects &lt;/li&gt;

  &lt;li&gt;Sister method of .STSymDifference Method. 
    &lt;p&gt;Syntax: geo1.STDifference(geo2)&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example use: Show areas of the city which do not have good mobile phone coverage &amp;quot;Dead Zones&amp;quot;. ie: Use STUnion to get the complete coverage area &amp;amp; then use STDifference to subtract it from a map of the city.&amp;#160; &lt;br /&gt;&amp;#160;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn2onnGeometricS_C45F/STDifference_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="249" alt="STDifference" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn2onnGeometricS_C45F/STDifference_thumb.png" width="446" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Sample: STDifference &amp;gt;===
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@a &lt;span style="color: blue"&gt;GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((1 0, 1 8, 6 4, 1 0))'
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@b &lt;span style="color: blue"&gt;GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((4 1, 3 5, 4 9, 9 5, 4 1),(4 5, 5 7, 6 7, 4 4, 4 5) )'

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@a&lt;span style="color: gray"&gt;.&lt;/span&gt;STDifference&lt;span style="color: gray"&gt;(&lt;/span&gt;@b&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'STDifference'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'A.STDifference(B)' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Labels
&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@b&lt;span style="color: gray"&gt;.&lt;/span&gt;STDifference&lt;span style="color: gray"&gt;(&lt;/span&gt;@a&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'STDifference'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'B.STDifference(A)' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Labels

&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@a &lt;span style="color: blue"&gt;as &lt;/span&gt;geo&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'A' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Labels
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@b&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'B'&lt;/span&gt;&lt;/pre&gt;

  &lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;Method &lt;font color="#ff0000"&gt;.STCentroid&lt;/font&gt;()&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;Geometric centre of a (multi)Polygon. 
    &lt;ul&gt;
      &lt;li&gt;Null for Points &amp;amp; Lines. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Works with &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#804000"&gt;Geography&lt;/font&gt; Objects. &lt;/li&gt;

  &lt;li&gt;Similar to geography method STEnvelopeCentre() &lt;/li&gt;

  &lt;li&gt;Not good for placing labels on shapes, use STPointOnSurface. &lt;/li&gt;
&lt;/ul&gt;

&lt;ul&gt;
  &lt;p&gt;Syntax: geo1.STCentroid()&lt;/p&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example use: Centre the Map display around this object. 
    &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn2onnGeometricS_C45F/STCentriod_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="229" alt="STCentriod" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn2onnGeometricS_C45F/STCentriod_thumb.png" width="439" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ===&amp;lt; Samples for STCentroid &amp;amp; STPointOnSurface &amp;gt;===
-- Sample 1: STCentroid not affected by interior holes. 
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;AS GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON( (1 1, 9 1, 9 9, 1 9, 1 1),(2 2, 4 2, 4 4, 2 4, 2 2))'
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g &lt;span style="color: blue"&gt;as &lt;/span&gt;Geo&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;WKT&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'Exterior' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Method
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STCentroid&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.2&lt;span style="color: gray"&gt;), &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STCentroid&lt;span style="color: gray"&gt;().&lt;/span&gt;ToString&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'STCentroid'
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointOnSurface&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.2&lt;span style="color: gray"&gt;), &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointOnSurface&lt;span style="color: gray"&gt;().&lt;/span&gt;ToString&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'STPointOnSurface'
&lt;/span&gt;&lt;span style="color: blue"&gt;go

&lt;/span&gt;&lt;span style="color: green"&gt;-- Sample 2: STCentroid might put labels in the interior holes. 
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;AS GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON( (1 1, 9 1, 9 9, 1 9, 1 1),(4 4, 6 4, 6 6, 4 6, 4 4))'
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g &lt;span style="color: blue"&gt;as &lt;/span&gt;Geo&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;WKT&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'Exterior' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Method
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STCentroid&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.2&lt;span style="color: gray"&gt;), &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STCentroid&lt;span style="color: gray"&gt;().&lt;/span&gt;ToString&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'STCentroid'
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointOnSurface&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.2&lt;span style="color: gray"&gt;), &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointOnSurface&lt;span style="color: gray"&gt;().&lt;/span&gt;ToString&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'STPointOnSurface'
&lt;/span&gt;&lt;span style="color: blue"&gt;go

&lt;/span&gt;&lt;span style="color: green"&gt;-- Sample 3: STCentroid not so good for labels on Crescent shapes either
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;AS GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON( (1 1, 2 1, 5 3, 7 6, 5 9, 2 11, 1 11, 5 6, 1 1))'
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g &lt;span style="color: blue"&gt;as &lt;/span&gt;Geo&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;WKT&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'Exterior' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Method
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STCentroid&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.2&lt;span style="color: gray"&gt;), &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STCentroid&lt;span style="color: gray"&gt;().&lt;/span&gt;ToString&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'STCentroid'
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointOnSurface&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.2&lt;span style="color: gray"&gt;), &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STPointOnSurface&lt;span style="color: gray"&gt;().&lt;/span&gt;ToString&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'STPointOnSurface'&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;h1&gt;&amp;#160;&lt;/h1&gt;

&lt;h1&gt;Method &lt;font color="#ff0000"&gt;.STPointOnSurface&lt;/font&gt;()&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;Returns a point somewhere on the surface of the object. 
    &lt;ul&gt;
      &lt;li&gt;Usually above &amp;amp; slightly to the left of the centre. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Handy for placing Labels on an object. Especially Complex Polygons with a hole in the centre &amp;amp;/or Crescent shaped objects, where the STCentroid returns a point that is not within the shape. &lt;/li&gt;

  &lt;li&gt;For MultiPoints &amp;amp; Lines it returns the same point as .STEndPoint(). &lt;/li&gt;

  &lt;li&gt;Works with &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#804000"&gt;Geography&lt;/font&gt; Objects. 

    &lt;ul&gt;
      &lt;li&gt;For Geography: 
        &lt;ul&gt;
          &lt;li&gt;Use .STEndPoint for geography Lines &amp;amp; Points. &lt;/li&gt;

          &lt;li&gt;Use a negative value for STBuffer, grab one of the points &amp;amp; test it is ok using STIntersects. &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Companion method to STCentroid 
    &lt;p&gt;Syntax: geo1.STPointOnSurface()&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example use: Drawing icons &amp;amp; labels on a shape. 
    &lt;br /&gt;&lt;font color="#0000ff"&gt;See .STCentroid above for Sample picture &amp;amp; Code.&lt;/font&gt;&lt;/p&gt;

  &lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;Method &lt;font color="#ff0000"&gt;.STEnvelopeCentre&lt;/font&gt;()&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;Approximate centre of a geography object. 
    &lt;ul&gt;
      &lt;li&gt;Unlike STCentroid it works for (multi)Points &amp;amp; Lines as well as Polygons. &lt;/li&gt;

      &lt;li&gt;I stress it is not a perfect Geometric centre, but often good enough. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Works with &lt;font color="#804000"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; Objects. &lt;/li&gt;

  &lt;li&gt;Similar to geometry method STCentroid() &lt;/li&gt;

  &lt;li&gt;Not always reliable for placing labels on shapes, but no geography equivalent to STPointOnSurface. 
    &lt;p&gt;Syntax: geo1.STEnvelopeCentre()&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example use: Centre the Map display around this object. Or perhaps Find the approximate centre of a lot of Points in a MultiPoint object. 
    &lt;br /&gt;&lt;/p&gt;

  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Samples for STEnvelopeCentre being equivalent of STCentroid &amp;amp; STPointOnSurface &amp;gt;==
-- Sample 1: STEnvelopeCentre is not a perfect geometric centre
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;AS GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: green"&gt;-- SET @g = 'POLYGON( (1 1, 9 1, 9 9, 1 9, 1 1),(4 4, 4 6, 6 6, 6 4, 4 4))'  --
-- SET @g = 'POLYGON( (1 1, 90 1, 90 90, 1 90, 1 1),(4 4, 4 6, 6 6, 6 4, 4 4))'    -- Interesting to see how far the centre moves when you remove the inner ring in lower left
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON( (10 1, 90 1, 90 90, 1 90, 10 1))'    &lt;/span&gt;&lt;span style="color: green"&gt;-- Interesting to see Centre is oval shaped at 55 Degrees

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g &lt;span style="color: blue"&gt;as &lt;/span&gt;Geo&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;WKT&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'Exterior' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Method
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;200000&lt;span style="color: gray"&gt;), &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;ToString&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'EnvelopeCenter'
&lt;/span&gt;&lt;span style="color: blue"&gt;Go

&lt;/span&gt;&lt;span style="color: green"&gt;-- Sample 2: EnvelopeCenter is not quite the same as Geometry's STCentroid method
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;AS GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON( (1 1, 2 1, 5 3, 7 6, 5 9, 2 11, 1 11, 5 6, 1 1))'
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g &lt;span style="color: blue"&gt;as &lt;/span&gt;Geo&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;WKT&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'Exterior' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Method
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;20000&lt;span style="color: gray"&gt;), &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;ToString&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'EnvelopeCenter'
&lt;/span&gt;&lt;span style="color: blue"&gt;go

&lt;/span&gt;&lt;span style="color: green"&gt;-- Sample 3: It works with lines
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;AS GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING( 1 1, 2 1, 5 3, 7 6, 5 9, 2 11, 1 11, 5 6, 1 1)'
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g &lt;span style="color: blue"&gt;as &lt;/span&gt;Geo&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;WKT&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'Exterior' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Method
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;20000&lt;span style="color: gray"&gt;), &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;ToString&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'EnvelopeCenter'
&lt;/span&gt;&lt;span style="color: blue"&gt;go

&lt;/span&gt;&lt;span style="color: green"&gt;-- Sample 4: It works with Points - but notice it is in a different position.
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;AS GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'MULTIPOINT( (1 1), (2 1), (5 3), (7 6), (5 9), (2 11), (1 11), (5 6), (1 1))'
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;15000&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Geo&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;WKT&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'Exterior' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Method
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;20000&lt;span style="color: gray"&gt;), &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;EnvelopeCenter&lt;span style="color: gray"&gt;().&lt;/span&gt;ToString&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'EnvelopeCenter'
&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;h1&gt;&amp;#160;&lt;/h1&gt;

&lt;h1&gt;&amp;#160;&lt;/h1&gt;

&lt;h1&gt;&amp;#160;&lt;/h1&gt;

&lt;h1&gt;Method &lt;font color="#ff0000"&gt;.STBoundary&lt;/font&gt;()&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;Returns :- 
    &lt;ul&gt;
      &lt;li&gt;Perimeter of a polygon, as a Linestring &lt;/li&gt;

      &lt;li&gt;Start/End Points of a Line. As a MultiPoint( (End X,Y), (Start X,Y) ) 
        &lt;ul&gt;
          &lt;li&gt;NB: If the line is closed ie: Start &amp;amp; End are the same, then returns Empty &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Works with &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#804000"&gt;Geography&lt;/font&gt; Objects. &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Syntax: geo1.STBoundary(geo2)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example use: Handy to convert a Polygon to Line 
    &lt;br /&gt;&amp;#160;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn2onnGeometricS_C45F/STBoundary_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="227" alt="STBoundary" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn2onnGeometricS_C45F/STBoundary_thumb.png" width="441" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ========&amp;lt; Test 1 - Closed Shapes - Polygon &amp;gt;=========================================
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'Polygon((0 0, 2 0, 4 3, 2 2, 0 2,0 0))'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;0&lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: green"&gt;-- Show Boundary of a Polygon
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STBoundary&lt;span style="color: gray"&gt;(),&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STBoundary&lt;span style="color: gray"&gt;().&lt;/span&gt;ToString&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'STBoundary of Polygons'
&lt;/span&gt;&lt;span style="color: blue"&gt;go

&lt;/span&gt;&lt;span style="color: green"&gt;-- ========&amp;lt; Test 1 - Closed Shapes - Closed Line = Empty &amp;gt;=========================================
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'Polygon((0 0, 2 0, 4 3, 2 2, 0 2,0 0))'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;0&lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: green"&gt;-- Show Boundary of a Closed LineString = Empty
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STBoundary&lt;span style="color: gray"&gt;().&lt;/span&gt;STBoundary&lt;span style="color: gray"&gt;(),&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STBoundary&lt;span style="color: gray"&gt;().&lt;/span&gt;STBoundary&lt;span style="color: gray"&gt;().&lt;/span&gt;ToString&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'STBoundary of Closed LineStrings'&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;go

&lt;/span&gt;&lt;span style="color: green"&gt;-- ========&amp;lt; Test 2 - Open Lines &amp;amp; Points &amp;gt;===================================
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'MULTILINESTRING( (1 0, 2 0, 2 2, 0 2, 0 0, 1 1),&lt;br /&gt;                                  (5 7, 7 7, 7 9, 5 9, 5 7),&lt;br /&gt;                                  (11 1, 13 1, 13 3, 11 3, 11 1, 9 4 ) )'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;0&lt;span style="color: gray"&gt;);

&lt;/span&gt;&lt;span style="color: green"&gt;-- Show Boundary of a MultiLineString is just End &amp;amp; Start Points.
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;,&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;()
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: green"&gt;-- Note: Closed LineStrings aren't there.
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STBoundary&lt;span style="color: gray"&gt;().&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.1&lt;span style="color: gray"&gt;),&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STBoundary&lt;span style="color: gray"&gt;().&lt;/span&gt;ToString&lt;span style="color: gray"&gt;()
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: green"&gt;-- Show Boundary of Points = Empty
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STBoundary&lt;span style="color: gray"&gt;().&lt;/span&gt;STBoundary&lt;span style="color: gray"&gt;(),&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STBoundary&lt;span style="color: gray"&gt;().&lt;/span&gt;STBoundary&lt;span style="color: gray"&gt;().&lt;/span&gt;ToString&lt;span style="color: gray"&gt;();
&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;h1&gt;More info&lt;/h1&gt;

&lt;blockquote&gt;
  &lt;p&gt;For more info see SQL Server 2008 Books Online &lt;a title="SQL BOL Geometry Data Type Method Reference" href="http://msdn.microsoft.com/en-us/library/bb933973.aspx" target="_blank"&gt;Geometry Data Type Method Reference&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Please give feedback. Is this series of articles useful? Did it save you time? What was good, What could be better?, Notice any errors? What would you like me to cover? All thoughts, comments, suggestions welcome.&lt;/p&gt;

&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e6064372-c9ca-49ad-8676-d4ba0ae69d04" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/SQL%20Server%20Spatial" rel="tag"&gt;SQL Server Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SQL%20Server" rel="tag"&gt;SQL Server&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Spatial" rel="tag"&gt;Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/TSQL" rel="tag"&gt;TSQL&lt;/a&gt;,&lt;a href="http://technorati.com/tags/.Net" rel="tag"&gt;.Net&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9015780" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/davidlean/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Spatial/default.aspx">Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/TSQL/default.aspx">TSQL</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server+Spatial/default.aspx">SQL Server Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geometry/default.aspx">Geometry</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Polygon/default.aspx">Polygon</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geography/default.aspx">Geography</category></item><item><title>SQL 2008 Spatial Samples, Part 7 of 9 - STBuffer ... Spatial starts here.</title><link>http://blogs.msdn.com/davidlean/archive/2008/10/24/sql-2008-spatial-samples-part-n-1-of-n-stbuffer.aspx</link><pubDate>Fri, 24 Oct 2008 15:53:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9014641</guid><dc:creator>davele</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/davidlean/comments/9014641.aspx</comments><wfw:commentRss>http://blogs.msdn.com/davidlean/commentrss.aspx?PostID=9014641</wfw:commentRss><wfw:comment>http://blogs.msdn.com/davidlean/rsscomments.aspx?PostID=9014641</wfw:comment><description>&lt;p&gt;This post covers only 2 methods: &lt;strong&gt;STBuffer&lt;/strong&gt; &amp;amp; &lt;strong&gt;BufferWithTolerance&lt;/strong&gt;. They are so handy, they deserve a post all to themselves. &lt;/p&gt;  &lt;p&gt;Note: Unless otherwise stated. All code samples are designed to be Cut n Pasted directly into SQL Server Management Studio (SSMS) &amp;amp; run. Sometimes I've added extra detail than what is in the sample picture. &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:B3E14793-948F-49af-A347-D19C374A7C4F:b1bac225-8ccc-4731-8269-fa8085ba6985" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt; &lt;script type="text/javascript"&gt;&lt;!--
digg_bodytext = 'This post covers only 2 methods: STBuffer &amp;amp; BufferWithTolerance. They are so handy.';
//--&gt;&lt;/script&gt; &lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;h1&gt;Method &lt;font color="#ff0000"&gt;.STBuffer&lt;/font&gt;(distance )&lt;/h1&gt;  &lt;blockquote&gt;   &lt;li&gt;Enables you to &amp;quot;expand&amp;quot; or &amp;quot;contract&amp;quot; a shape. &lt;/li&gt;    &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;font color="#804000"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/font&gt; objects. &lt;/li&gt;    &lt;li&gt;.STBuffer is an extremely powerful &amp;amp; hence very handy method, but don&amp;#8217;t use it &amp;#8220;by Default&amp;#8221;, look for alternatives &amp;amp; use it if there are none.      &lt;p&gt;Why? It is a CPU resource intensive function. &lt;/p&gt;      &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (Don't get me wrong, STBuffer is efficient for what it does, but what it does is not trivial.)&lt;/p&gt;      &lt;h3&gt;Example Use: &lt;/h3&gt;      &lt;p&gt;It is handy to answer questions like &amp;#8220;Give me a list of homes within 500 metres of the railway line.&amp;#8221; You get the path for the Rail Line. Create a 500m buffer on the path &amp;amp; return all points contained within the buffer. &lt;/p&gt;      &lt;p&gt;You could also use it to answer the question &amp;#8220;Give me the homes within 3km radius of this point&amp;#8221;. But, mathematically speaking, that is a much easier problem to solve. So it is much more efficient to use STDistance &amp;amp; find all points within that Distance. This approach will use an index if one exists for a much faster result.&lt;/p&gt;      &lt;p&gt;&lt;strong&gt;Alternative 1:&lt;/strong&gt; Could create a buffer (size=X) from a Point &amp;amp; see what points are contained (STWithin) in that buffer. &lt;/p&gt;      &lt;p&gt;&lt;strong&gt;A Better Alternative:&lt;/strong&gt; Finds all points &amp;lt;= X distance from that point.&lt;/p&gt;   &lt;/li&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;Of course everything is relative, in the examples in this blog, I'm only using a couple of shapes with a few points. Typically you may have a few thousand shapes with many hundreds of points each. That is when you start to see a difference.    &lt;p&gt;&amp;#160;&lt;/p&gt;    &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ===&amp;lt; Sample: Creating a buffer around a Line &amp;gt;===
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@h &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;=  &lt;/span&gt;&lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(67 36,68 37,69 38,73 38,75 37,
                                    76 35,75 33,74 32,71 32,69 33,58 39,55 40,52 40,50 39,48 36,
                                    48 34,50 31,52 30,56 30,60 31,62 33)'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;0&lt;span style="color: gray"&gt;);

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@h &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Geo'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'Line' &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;&lt;span style="color: red"&gt;'Labels'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;@h&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Text'
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@h&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;1.5&lt;span style="color: gray"&gt;), &lt;/span&gt;&lt;span style="color: red"&gt;'STBuffer(1.5)'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;@h&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;1.5&lt;span style="color: gray"&gt;).&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;()
&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn1ofnSTBuffer_12406/STBuffer_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="189" alt="STBuffer" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn1ofnSTBuffer_12406/STBuffer_thumb.png" width="371" border="0" /&gt;&lt;/a&gt; 

    &lt;br /&gt;The purple line was the original LineString, the blue polygon is the Buffer. &lt;/p&gt;

  &lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;h3&gt;Tip: .STBuffer values can be negative when using Polygons&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;ul&gt;
    &lt;li&gt;A negative buffer removes all points within the given distance of the boundary of the geometry instance. &lt;/li&gt;

    &lt;li&gt;Combine with other spatial methods like STDisjoint &amp;amp; STIntersect to find objects within N distance inside a shape perimeter. &lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Sample: Finding a Perimeter with a negative buffer value &amp;gt;==
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@Zone &lt;span style="color: blue"&gt;as GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((50 0, 60 30, 80 20, 70 40, 100 50, 70 60, 80 80, 60 70,
                             50 100, 40 70, 20 80, 30 60, 0 50, 30 40, 20 20, 40 30, 50 0 ))'
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@Perim &lt;span style="color: blue"&gt;as GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;@Zone&lt;span style="color: gray"&gt;.&lt;/span&gt;STSymDifference&lt;span style="color: gray"&gt;(&lt;/span&gt;@Zone&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(-&lt;/span&gt;4&lt;span style="color: gray"&gt;))
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@Perim &lt;span style="color: blue"&gt;as &lt;/span&gt;Perimeter    
&lt;span style="color: green"&gt;-- Then use STIntersects against a set of Points to find which points are in the Perimeter.&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;pre class="code"&gt;&lt;span style="color: green"&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;blockquote&gt;
  &lt;h3&gt;Tip: Using STBuffer with SQL Server Management Studio (SSMS)&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;ul&gt;
    &lt;li&gt;If you are using SSMS to look at POINT data, try putting a Buffer around them. It makes it easier to see them. &lt;/li&gt;

    &lt;li&gt;Do NOT use this approach in your application. Much more efficient to draw a marker (icon) at that point. &lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Sample: Using STBuffer to see Points easier in SSMS Spatial View Tab &amp;gt;===
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;geometry&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'MULTIPOINT( (1 0.5), (5 3),(2 2), (4 1), (4 5), (2 3), (1 2))'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;0&lt;span style="color: gray"&gt;);

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.1&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;[Geo]&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'@g'&lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;[Label]&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;[WKT]&lt;/pre&gt;

  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn1ofnSTBuffer_12406/STBufferPoints_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="286" alt="STBufferPoints" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn1ofnSTBuffer_12406/STBufferPoints_thumb.png" width="260" border="0" /&gt;&lt;/a&gt; 

    &lt;br /&gt;Easier to see points as small circles in SSMS Spatial results Tab&lt;/p&gt;

  &lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;h3&gt;Tip: The buffer values you use for geography are typically much larger (or much smaller) than what we use for geometry.&lt;/h3&gt;

  &lt;ul&gt;
    &lt;li&gt;Why? Typically in geometry, your scale goes from 0 to 100, maybe 1000. Whereas often in geography you map might include an entire continent, often 10,000's of Km across. So the buffer around your points may need to be as big as Scotland, just so you can see them. &lt;/li&gt;

    &lt;li&gt;So while .STBuffer(0.2) may work well for to display points in a Geometry result. Perhaps you may need .STBuffer(2000000) just to see a geography point on a map of the world. &lt;/li&gt;

    &lt;li&gt;&lt;strong&gt;Warning:&lt;/strong&gt; Be aware that Latitude must remain between -90 &amp;amp; +90 degrees. If your point is near the poles &amp;amp; you draw a circle around it, a port of that circle may exceed +/-90 Latitude. If it does you will get a .NET Framework error 

      &lt;p&gt;&lt;font color="#ff0000"&gt;Msg 6522, Level 16, State 1, Line 23 
          &lt;br /&gt;A .NET Framework error occurred during execution of user-defined routine or aggregate &amp;quot;geography&amp;quot;: 

          &lt;br /&gt;System.FormatException: 24201: Latitude values must be between -90 and 90 degrees.&lt;/font&gt;&lt;/p&gt;

      &lt;p&gt;&amp;#160;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h1&gt;Method &lt;font color="#ff0000"&gt;.BufferWithTolerance&lt;/font&gt;(&lt;i&gt;distance&lt;/i&gt;&lt;b&gt;,&lt;/b&gt; &lt;i&gt;tolerance&lt;/i&gt;&lt;b&gt;,&lt;/b&gt; &lt;i&gt;relative&lt;/i&gt;)&lt;/h1&gt;

&lt;ul&gt;
  &lt;blockquote&gt;
    &lt;li&gt;This lets you trade off, accuracy for overhead. Usually less CPU expensive than .STBuffer. Like STBuffer it still enables you to &amp;quot;expand&amp;quot; or &amp;quot;contract&amp;quot; a shape but you can ask it to be less precise. And thus less demanding on your CPU. &lt;/li&gt;

    &lt;li&gt;Works with &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#804000"&gt;Geography&lt;/font&gt; Objects. &lt;/li&gt;

    &lt;li&gt;While distance is the same, tolerance is how much variation between what it returns &amp;amp; &amp;quot;perfect&amp;quot;. &lt;/li&gt;

    &lt;li&gt;&lt;/li&gt;
  &lt;/blockquote&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn1ofnSTBuffer_12406/BufferWithTolerance_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="180" alt="BufferWithTolerance" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn1ofnSTBuffer_12406/BufferWithTolerance_thumb.png" width="363" border="0" /&gt;&lt;/a&gt;&amp;#160; &lt;br /&gt;The Brown is the BufferwithTolerance &amp;amp; the Blue are what you get with STBuffer. Actually I had to set parameters to be really inaccurate in order to show you the difference. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Another example showing both Negative Buffer values &amp;amp; BufferWithTolerance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Sample: Setting a negative Buffer value &amp;gt;===
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@p &lt;span style="color: blue"&gt;as GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((50 0, 60 30, 80 20, 70 40, 100 50, 70 60, 80 80, 60 70,
                         50 100, 40 70, 20 80, 30 60, 0 50, 30 40, 20 20, 40 30, 50 0 ))'
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@p &lt;span style="color: blue"&gt;as &lt;/span&gt;geom
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@p&lt;span style="color: gray"&gt;.&lt;/span&gt;BufferWithTolerance&lt;span style="color: gray"&gt;(-&lt;/span&gt;2&lt;span style="color: gray"&gt;,&lt;/span&gt;2&lt;span style="color: gray"&gt;,&lt;/span&gt;0&lt;span style="color: gray"&gt;)  &lt;/span&gt;&lt;span style="color: green"&gt;/* 0, tolerance (2nd parm) is the absolute maximum variation in the
                                          ideal buffer distance for the returned linear approximation. */
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@p&lt;span style="color: gray"&gt;.&lt;/span&gt;BufferWithTolerance&lt;span style="color: gray"&gt;(-&lt;/span&gt;9&lt;span style="color: gray"&gt;,&lt;/span&gt;3&lt;span style="color: gray"&gt;,&lt;/span&gt;1&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: green"&gt;/* 1, tolerance is relative. Product of the tolerance parameter 
                                            &amp;amp; the diameter of the bounding box */
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@p&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(-&lt;/span&gt;14&lt;span style="color: gray"&gt;)
&lt;/span&gt;&lt;span style="color: blue"&gt;go 
&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn1ofnSTBuffer_12406/BufferStar_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="321" alt="BufferStar" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPartn1ofnSTBuffer_12406/BufferStar_thumb.png" width="325" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;More info&lt;/h1&gt;

&lt;blockquote&gt;
  &lt;p&gt;For more info see SQL Server 2008 Books Online&amp;#160; &lt;a href="http://msdn.microsoft.com/en-us/library/bb933892.aspx"&gt;BufferWithTolerance&lt;/a&gt; &amp;amp; &lt;a href="http://msdn.microsoft.com/en-us/library/bb933963.aspx"&gt;STBuffer&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Please give feedback. Is this series of articles useful? Did it save you time? What was good, What could be better?, Notice any errors? What would you like me to cover? All thoughts, comments, suggestions welcome.&lt;/p&gt;

&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:ecc9e12a-ded6-427a-b8d8-89417138a0c6" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/SQL%20Server%20Spatial" rel="tag"&gt;SQL Server Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SQL%20Server" rel="tag"&gt;SQL Server&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Spatial" rel="tag"&gt;Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/TSQL" rel="tag"&gt;TSQL&lt;/a&gt;,&lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Geometry" rel="tag"&gt;Geometry&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Geography" rel="tag"&gt;Geography&lt;/a&gt;,&lt;a href="http://technorati.com/tags/STBuffer" rel="tag"&gt;STBuffer&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9014641" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/davidlean/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Spatial/default.aspx">Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/TSQL/default.aspx">TSQL</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server+Spatial/default.aspx">SQL Server Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geometry/default.aspx">Geometry</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Polygon/default.aspx">Polygon</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geography/default.aspx">Geography</category></item><item><title>SQL 2008 Spatial Samples, Part 8 of 9 - Conditional Methods</title><link>http://blogs.msdn.com/davidlean/archive/2008/10/24/sql-20008-spatial-samples-part-n-of-n-conditional-methods.aspx</link><pubDate>Fri, 24 Oct 2008 11:36:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9012349</guid><dc:creator>davele</dc:creator><slash:comments>10</slash:comments><comments>http://blogs.msdn.com/davidlean/comments/9012349.aspx</comments><wfw:commentRss>http://blogs.msdn.com/davidlean/commentrss.aspx?PostID=9012349</wfw:commentRss><wfw:comment>http://blogs.msdn.com/davidlean/rsscomments.aspx?PostID=9012349</wfw:comment><description>&lt;p&gt;This post covers the following methods: STDisjoint, &lt;strong&gt;STIntersects&lt;/strong&gt;, Filter,&amp;#160; &lt;strong&gt;STTouches&lt;/strong&gt;, &lt;strong&gt;STWithin&lt;/strong&gt;, &lt;strong&gt;STContains&lt;/strong&gt;,&amp;#160; &lt;strong&gt;STOverlaps&lt;/strong&gt;, STCrosses,&amp;#160; STRelate, &amp;amp; &lt;strong&gt;STEquals&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;One day these examples may find their way to Books Online. For now they can live here.    &lt;br /&gt;&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:B3E14793-948F-49af-A347-D19C374A7C4F:9c74c340-599f-47b0-b1d6-258db75b6fd8" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt; &lt;script type="text/javascript"&gt;&lt;!--
digg_bodytext = 'This post covers the following methods: STDisjoint, STIntersects, Filter,&amp;nbsp;&amp;nbsp;STTouches, STWithin, STContains,&amp;nbsp; STOverlaps, STCrosses,&amp;nbsp; STRelate, &amp;amp; STEquals';
//--&gt;&lt;/script&gt; &lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;h2&gt;Overview of Conditional Methods&lt;/h2&gt;  &lt;p&gt;You will find that these Methods exceptionally handy to find or filter out similar spatial shapes. They are extremely efficient when a suitable spatial index exists. &lt;/p&gt;  &lt;p&gt;I'm not saying these methods can ONLY be used in a WHERE clause. It is often handy to combine them with a CASE clause in the select list. However I wanted to group sets of Methods together &amp;amp; this seemed an appropriate name.&lt;/p&gt;  &lt;p&gt;These methods Return a boolean: 1 (true) or 0 (false). &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Tip: &lt;/strong&gt;They take advantage of Spatial Index to retrieve relevant rows quickly. Especially if you ask for what you &lt;u&gt;want,&lt;/u&gt; so searching for Equality works well, but filtering on not equals, often means the index will not help you. Most methods have a opposite ie: STDisjoint is the opposite of STIntersects. So instead of using &amp;quot;not equals&amp;quot; change to the reciprocal method.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The following code sample&lt;/strong&gt; lets you explore the GIS Methods. Just Cut n Paste into SQL Server Management Studio (SSMS) 2008 (or higher). &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Note 1&lt;/strong&gt;: If your data has POINTs then the SSMS Spatial viewer disables the ability to display labels. While this is nice if you have a few polygons with names, covers in a lot of points, is it inconvenient here. So comment out the last 3 Union Alls if you want show the labels of your shapes &amp;amp; lines. You can still hover over them to see ToolTip labels on points.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Note 2&lt;/strong&gt;: Usually you would not have the @S0 shape in the set of shapes you are querying. So sometimes @S0 is in the results where normally it wouldn&amp;#8217;t be. I've left it in so you can see the relationship with it &amp;amp; the object you are comparing against it.&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ===&amp;lt; Samples:  TSQL Spatial Conditional Methods as a filter &amp;gt;===
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@s0 &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((4 4, 7 4, 7 7, 4 7 ,4 4))'    &lt;/span&gt;&lt;span style="color: green"&gt;-- Main Shape

&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@s1 &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((5 4.2, 6 4.2, 6 5, 5 5, 5 4.2))'  &lt;/span&gt;&lt;span style="color: green"&gt;-- Inside Shape
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@s2 &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((5 3, 6 3, 6 4, 5 4, 5 3))'  &lt;/span&gt;&lt;span style="color: green"&gt;-- Part Edge in Common
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@s3 &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((7.2 4, 8 4, 8 5, 7.2 5, 7.2 4))'  &lt;/span&gt;&lt;span style="color: green"&gt;-- Outside Shape

&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@t1 &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((3 4, 4 4, 4 3, 3 4))'  &lt;/span&gt;&lt;span style="color: green"&gt;-- 2 Corners in Common
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@t2 &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((3 5, 4 5.5, 3 6, 3 5))'  &lt;/span&gt;&lt;span style="color: green"&gt;-- Corner on an Edge
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@t3 &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((6.5 5, 7.5 5.5, 6.5 6, 6.5 5))'  &lt;/span&gt;&lt;span style="color: green"&gt;-- Overlap

&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@L1 &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(5 6.5, 6.5 6.5)'    &lt;/span&gt;&lt;span style="color: green"&gt;-- Line in Shape
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@L2 &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(4.5 3, 4.5 7.5)'    &lt;/span&gt;&lt;span style="color: green"&gt;-- Line Thru Shape
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@L3 &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(3 6.5, 4.2 6.5)'    &lt;/span&gt;&lt;span style="color: green"&gt;-- Line cross Edge of Shape

&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@P1 &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POINT(6.5 4.5)'    &lt;/span&gt;&lt;span style="color: green"&gt;-- Point in Shape
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@P2 &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POINT(4 5)'    &lt;/span&gt;&lt;span style="color: green"&gt;-- Point on Edge
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@P3 &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POINT(6.5 3.5)'    &lt;/span&gt;&lt;span style="color: green"&gt;-- Point off Shape

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: gray"&gt;* &lt;/span&gt;&lt;span style="color: green"&gt;-- Make all variables appear as the rows of one table  
 &lt;/span&gt;&lt;span style="color: blue"&gt;FROM &lt;/span&gt;&lt;span style="color: gray"&gt;(
    &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@s0 &lt;span style="color: blue"&gt;AS &lt;/span&gt;&lt;span style="color: red"&gt;'Geo'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'S0' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Labels'
    &lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL   &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@s1&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'S1'
    &lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL   &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@s2&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'S2'
    &lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL   &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@s3&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'S3'
    &lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL   &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@t1&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'T1'
    &lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL   &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@t2&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'T2'
    &lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL   &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@T3&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'T3'
    &lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL   &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@L1&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'L1'
    &lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL   &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@L2&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'L2'
    &lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL   &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@L3&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'L3'
    &lt;/span&gt;&lt;span style="color: green"&gt;-- As Labels don't display in SSMS when you have Points in output. 
    -- Either Comment out the next 3 lines if you want to see the labels in SSMS.
    -- or put a buffer around them so they display as polygons. &lt;br /&gt;    &lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL   &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@P1&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.1&lt;span style="color: gray"&gt;), &lt;/span&gt;&lt;span style="color: red"&gt;'P1'
    &lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL   &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@P2&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.1&lt;span style="color: gray"&gt;), &lt;/span&gt;&lt;span style="color: red"&gt;'P2'
    &lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL   &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@P3&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.1&lt;span style="color: gray"&gt;), &lt;/span&gt;&lt;span style="color: red"&gt;'P3'
 &lt;/span&gt;&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;A
&lt;span style="color: green"&gt;--Uncomment ONE of the lines below to test the effect of each different where clause
-- WHERE A.Geo.STTouches(@s0) = 1
-- WHERE A.Geo.STWithin(@s0) = 1
-- WHERE A.Geo.STCrosses(@s0) = 1
-- WHERE A.Geo.STContains(@S1) = 1
-- WHERE A.Geo.STOverlaps(@s0) = 1
-- WHERE A.Geo.STIntersects(@s0) = 1
-- WHERE A.Geo.STDisjoint(@s0) = 1
&lt;/span&gt;&lt;/pre&gt;

&lt;pre class="code"&gt;When you run the code all the objects display as shown in this diagram. &lt;br /&gt;Use this as a baseline when we look at the results of the different filters below.&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL20008SpatialSamplesPartnofnConditiona_CFF1/All%20Conditional_4.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="207" alt="All Conditional" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL20008SpatialSamplesPartnofnConditiona_CFF1/All%20Conditional_thumb_1.png" width="244" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:ad871662-eee1-4918-888e-2005d5c2ae2d" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/SQL%20Server%20Spatial" rel="tag"&gt;SQL Server Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SQL%20Server" rel="tag"&gt;SQL Server&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SQL%20Server%202008" rel="tag"&gt;SQL Server 2008&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;h2&gt;Method &lt;font color="#ff0000"&gt;.STDisjoint&lt;/font&gt;()&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Spatial Objects &lt;strong&gt;DO NOT&lt;/strong&gt; touch each other. &lt;/li&gt;

  &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;font color="#800040"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/font&gt; objects &lt;/li&gt;

  &lt;li&gt;Opposite method of .STIntersects &amp;amp; .Filter Methods. &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example use: Show all customers who live outside our standard delivery area 
    &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL20008SpatialSamplesPartnofnConditiona_CFF1/STDisjoint_4.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="215" alt="STDisjoint" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL20008SpatialSamplesPartnofnConditiona_CFF1/STDisjoint_thumb_1.png" width="244" border="0" /&gt;&lt;/a&gt; 

    &lt;br /&gt;WHERE A.Geo.STDisjoint(@s0) = 1 &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Yellow highlights all objects which are Disjoint from Shape S0&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Method &lt;font color="#ff0000"&gt;.STIntersects&lt;/font&gt;()&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Spatial Objects &lt;strong&gt;DO &lt;/strong&gt;touch each other. &lt;/li&gt;

  &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;font color="#800040"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/font&gt; objects &lt;/li&gt;

  &lt;li&gt;Opposite of .STDisjoint Method. &lt;/li&gt;

  &lt;li&gt;Similar to .Filter &lt;/li&gt;

  &lt;li&gt;To use spatial index search on WHERE geo1.STIntersects ( geo2 ) = 1 &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example use: List cancer patients who live within 1km of a high tension power line. 
    &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL20008SpatialSamplesPartnofnConditiona_CFF1/STIntersects_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="215" alt="STIntersects" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL20008SpatialSamplesPartnofnConditiona_CFF1/STIntersects_thumb.png" width="244" border="0" /&gt;&lt;/a&gt; 

    &lt;br /&gt;WHERE A.Geo.STIntersects(@s0) = 1 &lt;/p&gt;

  &lt;p&gt;
    &lt;br /&gt;Yellow highlights all objects which Intersect with Shape S0&lt;/p&gt;

  &lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;Method &lt;font color="#ff0000"&gt;.Filter&lt;/font&gt;()&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Spatial Objects &lt;strong&gt;DO &lt;/strong&gt;touch each other. &lt;/li&gt;

  &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;font color="#800040"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/font&gt; objects &lt;/li&gt;

  &lt;li&gt;Opposite of .STDisjoint Method. &lt;/li&gt;

  &lt;li&gt;Similar to .STIntersects() but :- 
    &lt;ul&gt;
      &lt;li&gt;Less accurate sometimes returns false positive results. Eg: Says they intersect when they do not. &lt;/li&gt;

      &lt;li&gt;often much faster if a relevant spatial index exists. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;So, Great for getting results to display not so good for accurate analysis. Eg: Find all shapes in this rectangle (my window). If you get back a few extra shapes that are outside the rectangle then they will be clipped when you try to draw them outside the window. 
    &lt;br /&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example use: Show cancer patients who live within 1km of a high tension power line.&lt;/p&gt;

  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ===&amp;lt; Sample: Using FILTER to rapidly get relevant results &amp;gt;====
&lt;/span&gt;&lt;span style="color: blue"&gt;USE &lt;/span&gt;someDB &lt;span style="color: green"&gt;-- This will give you an error, reminding you not to create it in Master :-)
&lt;/span&gt;&lt;span style="color: blue"&gt;go

&lt;/span&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Create a Table with a spatial Index &amp;gt;===
&lt;/span&gt;&lt;span style="color: blue"&gt;CREATE TABLE &lt;/span&gt;dbo&lt;span style="color: gray"&gt;.&lt;/span&gt;GeoTable &lt;span style="color: gray"&gt;(&lt;/span&gt;id &lt;span style="color: blue"&gt;INT PRIMARY KEY&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;geom &lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;);
 
&lt;/span&gt;&lt;span style="color: blue"&gt;INSERT INTO &lt;/span&gt;GeoTable &lt;span style="color: blue"&gt;VALUES 
&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;0&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;Point&lt;span style="color: gray"&gt;(&lt;/span&gt;45&lt;span style="color: gray"&gt;, -&lt;/span&gt;120&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;)),
(&lt;/span&gt;1&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;Point&lt;span style="color: gray"&gt;(&lt;/span&gt;45&lt;span style="color: gray"&gt;, -&lt;/span&gt;120.1&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;)),
(&lt;/span&gt;2&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;Point&lt;span style="color: gray"&gt;(&lt;/span&gt;45&lt;span style="color: gray"&gt;, -&lt;/span&gt;120.2&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;)), 
(&lt;/span&gt;3&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;Point&lt;span style="color: gray"&gt;(&lt;/span&gt;45&lt;span style="color: gray"&gt;, -&lt;/span&gt;120.3&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;)), 
(&lt;/span&gt;4&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;Point&lt;span style="color: gray"&gt;(&lt;/span&gt;45&lt;span style="color: gray"&gt;, -&lt;/span&gt;120.4&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;));

&lt;/span&gt;&lt;span style="color: blue"&gt;CREATE SPATIAL INDEX &lt;/span&gt;GeoTable_idx &lt;span style="color: blue"&gt;ON &lt;/span&gt;dbo&lt;span style="color: gray"&gt;.&lt;/span&gt;GeoTable&lt;span style="color: gray"&gt;(&lt;/span&gt;geom&lt;span style="color: gray"&gt;); 

&lt;/span&gt;&lt;span style="color: green"&gt;-- ==&amp;lt; Example: The Filter Query &amp;gt;==
&lt;/span&gt;&lt;span style="color: green"&gt;-- Remember to look in the SSMS Spatial Results tab for see the relationships here&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;id &lt;span style="color: blue"&gt;FROM &lt;/span&gt;dbo&lt;span style="color: gray"&gt;.&lt;/span&gt;GeoTable 
&lt;span style="color: blue"&gt;WHERE &lt;/span&gt;geom&lt;span style="color: gray"&gt;.&lt;/span&gt;Filter&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;Parse&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((-120.2 44.9, -119.9 44.9, -119.9 45.1, -120.1 45.1, -120.2 44.9))'&lt;/span&gt;&lt;span style="color: gray"&gt;)) = &lt;/span&gt;1&lt;span style="color: gray"&gt;;

&lt;/span&gt;&lt;span style="color: green"&gt;-- Show the Geo Data --
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;geom&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;2000&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;Geom&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: magenta"&gt;Cast&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;id &lt;span style="color: blue"&gt;AS VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;4&lt;span style="color: gray"&gt;)) &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;&lt;span style="color: red"&gt;'Labels'
&lt;/span&gt;&lt;span style="color: blue"&gt;FROM &lt;/span&gt;dbo&lt;span style="color: gray"&gt;.&lt;/span&gt;GeoTable
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL 
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;Parse&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((-120.2 44.9, -119.9 44.9, -119.9 45.1, -120.1 45.1, -120.2 44.9))'&lt;/span&gt;&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;SD&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'Poly'&lt;/span&gt;&lt;span style="color: gray"&gt;; 
&lt;/span&gt;&lt;span style="color: blue"&gt;go

DROP TABLE &lt;/span&gt;dbo&lt;span style="color: gray"&gt;.&lt;/span&gt;GeoTable
&lt;span style="color: blue"&gt;go&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;h2&gt;Method &lt;font color="#ff0000"&gt;.STTouches&lt;/font&gt;(Geometry object)&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;The edges of the Spatial Objects &lt;strong&gt;touch but their interiors don&amp;#8217;t overlap&lt;/strong&gt; each other. For points this means they are on the border of an object. &lt;/li&gt;

  &lt;li&gt;Works with &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#800040"&gt;Geography&lt;/font&gt; Objects. &lt;/li&gt;

  &lt;li&gt;To use spatial index search on WHERE geo1.STTouches ( geo2 ) = 1 
    &lt;br /&gt;

    &lt;br /&gt;Example use: List all suburbs next to Suburb X. Part of a larger query to find homes for sale in or near this suburb. 

    &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL20008SpatialSamplesPartnofnConditiona_CFF1/image_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="215" alt="image" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL20008SpatialSamplesPartnofnConditiona_CFF1/image_thumb.png" width="244" border="0" /&gt;&lt;/a&gt; 

    &lt;br /&gt;WHERE A.Geo.STTouches(@s0) = 1 &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;
    &lt;br /&gt;Yellow highlights all objects which touch Shape S0&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Method &lt;font color="#ff0000"&gt;.STWithin&lt;/font&gt;()&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;This Spatial Object is completely contained within another. &lt;/li&gt;

  &lt;li&gt;Works with &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#800040"&gt;Geography&lt;/font&gt; Objects. &lt;/li&gt;

  &lt;li&gt;Sister method of .&lt;strong&gt;STContains&lt;/strong&gt; &lt;/li&gt;

  &lt;li&gt;To use spatial index search on WHERE geo1.STWithin ( geo2 ) = 1 &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example use: Find all homes within 3km of the Bush Fire zone. Would also need &lt;strong&gt;STBuffer&lt;/strong&gt; to expand the Bush Fire Zone shape by 3km. 

    &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL20008SpatialSamplesPartnofnConditiona_CFF1/STWithin_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="215" alt="STWithin" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL20008SpatialSamplesPartnofnConditiona_CFF1/STWithin_thumb.png" width="244" border="0" /&gt;&lt;/a&gt; 

    &lt;br /&gt;WHERE A.Geo.&lt;strong&gt;STWithin&lt;/strong&gt;(@s0) = 1&lt;/p&gt;

  &lt;p&gt;
    &lt;br /&gt;Yellow highlights all objects which are completely inside Shape S0&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Method &lt;font color="#ff0000"&gt;.STContains&lt;/font&gt;()&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;li&gt;This Spatial Object is completely surrounded by another. &lt;/li&gt;

  &lt;li&gt;Works with &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#800040"&gt;Geography&lt;/font&gt; Objects. &lt;/li&gt;

  &lt;li&gt;Sister method of .&lt;strong&gt;STWithin&lt;/strong&gt;, but asks the question from the other perspective. &lt;/li&gt;

  &lt;li&gt;To use spatial index search on WHERE geo1.STContains ( geo2 ) = 1 &lt;/li&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example use: Find the polygon (postcode) that the mouse cursor is currently hovering over. You'd need to convert the mouse value into a point, &amp;amp; see what shape contains that point. 
    &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL20008SpatialSamplesPartnofnConditiona_CFF1/STContains_6.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="215" alt="STContains" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL20008SpatialSamplesPartnofnConditiona_CFF1/STContains_thumb_2.png" width="244" border="0" /&gt;&lt;/a&gt; 

    &lt;br /&gt;I've combined the display of 3 queries into one diagram. ie:- 

    &lt;br /&gt;WHERE&amp;#160; &lt;strong&gt;L1.&lt;/strong&gt;STContains(@S0) = 1 

    &lt;br /&gt;OR&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;strong&gt;S1.&lt;/strong&gt;STContains(@S0) = 1 

    &lt;br /&gt;OR&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;strong&gt;P1.&lt;/strong&gt;STContains(@S0) = 1&lt;/p&gt;

  &lt;p&gt;
    &lt;br /&gt;Yellow highlights all objects which are contained by Shape S0 

    &lt;br /&gt;&lt;strong&gt;Note&lt;/strong&gt;: This query is different, all others use the Geo Column &amp;amp; compare it to the shape S0. These queries use the STContains method on the shape we are interested in &amp;amp; compare it to shape S0&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Method &lt;font color="#ff0000"&gt;.STOverlaps&lt;/font&gt;()&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;li&gt;True when this Shapes partly lie on other shapes OR lines cross. &lt;/li&gt;

  &lt;li&gt;Works with &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#800040"&gt;Geography&lt;/font&gt; Objects. &lt;/li&gt;

  &lt;li&gt;Sister method of .&lt;strong&gt;STCrosses&lt;/strong&gt;, but only works when objects are the same type: ie Shapes with shapes, Lines with lines. &lt;/li&gt;

  &lt;li&gt;To use spatial index search on WHERE geo1.STOverlaps ( geo2 ) = 1 
    &lt;br /&gt;

    &lt;br /&gt;Example use: Show the Sales Territories where it is not clear who &amp;quot;owns&amp;quot; a new customer. 

    &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL20008SpatialSamplesPartnofnConditiona_CFF1/STOverlaps_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="215" alt="STOverlaps" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL20008SpatialSamplesPartnofnConditiona_CFF1/STOverlaps_thumb.png" width="244" border="0" /&gt;&lt;/a&gt; 

    &lt;br /&gt;WHERE A.Geo.STOverlaps(@s0) = 1 &lt;/li&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;Yellow highlights all Polygons which lie over the border of&amp;#160; Shape S0 
  &lt;br /&gt;&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Method &lt;font color="#ff0000"&gt;.STCrosses&lt;/font&gt;()&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;li&gt;True when a Line overlaps a Polygon OR&amp;#160; A Point lies on a line. ie: Something of a lesser Dimension (1D - Line or 0D - Point) &lt;/li&gt;

  &lt;li&gt;Works with &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#800040"&gt;Geography&lt;/font&gt; Objects. &lt;/li&gt;

  &lt;li&gt;Sister method of .&lt;strong&gt;STCrosses&lt;/strong&gt;, but only works when objects are different dimensions. So 

    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;u&gt;Why not T3?&lt;/u&gt; Because the intersection is still a 2D Object, use STOverlaps() for this. 

    &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL20008SpatialSamplesPartnofnConditiona_CFF1/STCrosses_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="215" alt="STCrosses" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL20008SpatialSamplesPartnofnConditiona_CFF1/STCrosses_thumb.png" width="244" border="0" /&gt;&lt;/a&gt; 

    &lt;br /&gt;WHERE A.Geo.STCrosses(@s0) = 1 &lt;/li&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;Yellow highlights all Lines &amp;amp; Points which lie over the border of&amp;#160; Shape S0 
  &lt;br /&gt;

  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- Sample 2: Show a Conditional method being used in a Select list ---
--  Also show STCrosses() working with lines &amp;amp; Points
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@L &lt;span style="color: blue"&gt;as GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(1 0, 5 0)'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;0&lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@Pon &lt;span style="color: blue"&gt;as GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POINT(2 0)'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;0&lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@Pend &lt;span style="color: blue"&gt;as GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POINT(5 0)'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;0&lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@Poff &lt;span style="color: blue"&gt;as GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POINT(3 1)'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;0&lt;span style="color: gray"&gt;);

&lt;/span&gt;&lt;span style="color: green"&gt;-- Test STCrosses 
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;geo&lt;span style="color: gray"&gt;, &lt;/span&gt;WKT&lt;span style="color: gray"&gt;, &lt;/span&gt;Labels&lt;span style="color: gray"&gt;, 
        &lt;/span&gt;&lt;span style="color: blue"&gt;CASE  &lt;/span&gt;@L&lt;span style="color: gray"&gt;.&lt;/span&gt;STCrosses&lt;span style="color: gray"&gt;(&lt;/span&gt;geo&lt;span style="color: gray"&gt;) 
            &lt;/span&gt;&lt;span style="color: blue"&gt;WHEN &lt;/span&gt;0 &lt;span style="color: blue"&gt;THEN &lt;/span&gt;&lt;span style="color: red"&gt;'STCrosses is FALSE' 
            &lt;/span&gt;&lt;span style="color: blue"&gt;WHEN &lt;/span&gt;1 &lt;span style="color: blue"&gt;THEN &lt;/span&gt;&lt;span style="color: red"&gt;'STCrosses is TRUE' 
        &lt;/span&gt;&lt;span style="color: blue"&gt;END AS &lt;/span&gt;STCrosses
&lt;span style="color: blue"&gt;FROM &lt;/span&gt;&lt;span style="color: gray"&gt;(
    &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@L &lt;span style="color: blue"&gt;as &lt;/span&gt;geo&lt;span style="color: gray"&gt;, &lt;/span&gt;@L&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;WKT&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'L1' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Labels'    
    &lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@Pon&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.1&lt;span style="color: gray"&gt;), &lt;/span&gt;@Pon&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'Point on Line'
    &lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@Pend&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.1&lt;span style="color: gray"&gt;), &lt;/span&gt;@Pend&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'Point on End of Line'
    &lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@Poff&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.1&lt;span style="color: gray"&gt;), &lt;/span&gt;@Poff&lt;span style="color: gray"&gt;.&lt;/span&gt;ToString&lt;span style="color: gray"&gt;(), &lt;/span&gt;&lt;span style="color: red"&gt;'Point not on Line'
    &lt;/span&gt;&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;T&lt;/pre&gt;

  &lt;pre class="code"&gt;&lt;strong&gt;Results: &lt;/strong&gt;&lt;/pre&gt;

  &lt;table cellspacing="0" cellpadding="2" width="478" bgcolor="#ffffff" border="3"&gt;&lt;tbody&gt;
      &lt;tr bgcolor="#b0c4de"&gt;
        &lt;td valign="top" width="157"&gt;
          &lt;p align="center"&gt;&lt;strong&gt;&lt;u&gt;WKT&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;
        &lt;/td&gt;

        &lt;td valign="top" width="160"&gt;
          &lt;p align="center"&gt;&lt;strong&gt;&lt;u&gt;Labels&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;
        &lt;/td&gt;

        &lt;td valign="top" width="155"&gt;
          &lt;p align="center"&gt;&lt;strong&gt;&lt;u&gt;STCrosses&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;
        &lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="158"&gt;LINESTRING (1 0, 5 0)&lt;/td&gt;

        &lt;td valign="top" width="160"&gt;Line1&lt;/td&gt;

        &lt;td valign="top" width="155"&gt;STCrosses is FALSE&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="158"&gt;POINT (2 0)&lt;/td&gt;

        &lt;td valign="top" width="160"&gt;Point on Line1 &lt;/td&gt;

        &lt;td valign="top" width="155"&gt;STCrosses is TRUE&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="158"&gt;POINT (5 0)&lt;/td&gt;

        &lt;td valign="top" width="160"&gt;Point on End of Line1&lt;/td&gt;

        &lt;td valign="top" width="155"&gt;STCrosses is TRUE&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="158"&gt;POINT (3 1)&lt;/td&gt;

        &lt;td valign="top" width="160"&gt;Point not on Line1&lt;/td&gt;

        &lt;td valign="top" width="155"&gt;STCrosses is FALSE&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL20008SpatialSamplesPartnofnConditiona_CFF1/STCrossesLine_1.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="132" alt="STCrossesLine" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL20008SpatialSamplesPartnofnConditiona_CFF1/STCrossesLine_thumb_1.png" width="244" border="0" /&gt;&lt;/a&gt;&amp;#160; &lt;br /&gt;Map showing the 3 Points &amp;amp; 1 Linestring&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;h2&gt;Method &lt;font color="#ff0000"&gt;.STRelate&lt;/font&gt;&lt;font color="#000000"&gt;(&amp;#160; ,Intersection Matrix)&lt;/font&gt;&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;li&gt;This Spatial Object has whatever relationship you describe in the Intersection Matrix. &lt;/li&gt;

  &lt;li&gt;Works with &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#800040"&gt;Geography&lt;/font&gt; Objects. &lt;/li&gt;

  &lt;li&gt;It is a superset of all the other methods described in this blog &amp;amp; easily deserves a post dedicated to it. It could easily replace all the &amp;#8220;Conditional methods&amp;#8221; in this blog eg: .STDisjoint(), STTouches() etc &lt;/li&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;As there are already good articles available on the web, I will provide a superficial coverage here, for more detail I suggest you look at the following. &lt;a href="http://edndoc.esri.com/arcsde/9.1/general_topics/understand_spatial_relations.htm "&gt;ESRI's Developer Help: Understanding Spatial Relations&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;h3&gt;What is an Intersection Matrix?&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;The 2nd Parameter that you provide is an Intersection Matrix. 
    &lt;br /&gt;An Intersection Matrix short for a Dimensionally Extended 9 Intersection Model (DE-9IM). This is a 3 * 3 grid that represents the possible intersections between 2 shapes. Each shape is divided into 3 parts; Interior, Boundary &amp;amp; Exterior &amp;amp; results in a grid below. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;
    &lt;br /&gt;1. All possible intersections between 2 shapes; Shape &amp;quot;a&amp;quot; &amp;amp; Shape &amp;quot;b&amp;quot;&lt;/p&gt;

  &lt;table cellspacing="0" cellpadding="2" width="400" border="3"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" width="102"&gt;&amp;#160;&lt;/td&gt;

        &lt;td valign="top" width="95" bgcolor="#b0c4de"&gt;&lt;strong&gt;Interior (b)&lt;/strong&gt;&lt;/td&gt;

        &lt;td valign="top" width="99" bgcolor="#b0c4de"&gt;&lt;strong&gt;Boundary (b)&lt;/strong&gt;&lt;/td&gt;

        &lt;td valign="top" width="98" bgcolor="#b0c4de"&gt;&lt;strong&gt;Exterior (b)&lt;/strong&gt;&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="102" bgcolor="#b0c4de"&gt;&lt;strong&gt;Interior (a)&lt;/strong&gt;&lt;/td&gt;

        &lt;td valign="top" width="95" bgcolor="#ffffff"&gt;I(a) ? I(b)&lt;/td&gt;

        &lt;td valign="top" width="100" bgcolor="#ffffff"&gt;I(a) ? B(b)&lt;/td&gt;

        &lt;td valign="top" width="98" bgcolor="#ffffff"&gt;I(a) ? E(b)&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="102" bgcolor="#b0c4de"&gt;&lt;strong&gt;Boundary (a)&lt;/strong&gt;&lt;/td&gt;

        &lt;td valign="top" width="95" bgcolor="#ffffff"&gt;B(a) ? I(b)&lt;/td&gt;

        &lt;td valign="top" width="99" bgcolor="#ffffff"&gt;B(a) ? B(b)&lt;/td&gt;

        &lt;td valign="top" width="98" bgcolor="#ffffff"&gt;B(a) ? E(b)&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="102" bgcolor="#b0c4de"&gt;&lt;strong&gt;Exterior (a)&lt;/strong&gt;&lt;/td&gt;

        &lt;td valign="top" width="95" bgcolor="#ffffff"&gt;E(a) ? I(b)&lt;/td&gt;

        &lt;td valign="top" width="100" bgcolor="#ffffff"&gt;E(a) ? B(b)&lt;/td&gt;

        &lt;td valign="top" width="98" bgcolor="#ffffff"&gt;E(a) ? E(b)&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;

  &lt;p&gt;&amp;#160;&lt;/p&gt;

  &lt;p&gt;2. Each of the 9 possible intersections can have one of 6 possible values. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;table cellspacing="0" cellpadding="2" width="648" bgcolor="#ffffff" border="3"&gt;&lt;tbody&gt;
      &lt;tr bgcolor="#b0c4de"&gt;
        &lt;td valign="top" width="50"&gt;&lt;strong&gt;Value&lt;/strong&gt;&lt;/td&gt;

        &lt;td valign="top" width="440"&gt;&lt;strong&gt;Comments&lt;/strong&gt;&lt;/td&gt;

        &lt;td valign="top" width="147"&gt;&lt;strong&gt;Maximum Dimensionality&lt;/strong&gt;&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="50" bgcolor="#f0f8ff"&gt;T&lt;/td&gt;

        &lt;td valign="top" width="440"&gt;An intersection must exist&lt;/td&gt;

        &lt;td valign="top" width="149"&gt;dim = 0, 1, or 2&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="50" bgcolor="#f0f8ff"&gt;F&lt;/td&gt;

        &lt;td valign="top" width="440"&gt;An intersection must not exist&lt;/td&gt;

        &lt;td valign="top" width="150"&gt;dim = -1&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="50" bgcolor="#f0f8ff"&gt;*&lt;/td&gt;

        &lt;td valign="top" width="440"&gt;It does not matter if an intersection exists or not&lt;/td&gt;

        &lt;td valign="top" width="151"&gt;dim = -1, 0, 1, or 2&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="50" bgcolor="#f0f8ff"&gt;0&lt;/td&gt;

        &lt;td valign="top" width="440"&gt;An intersection must exist and its maximum dimension must be 0&lt;/td&gt;

        &lt;td valign="top" width="152"&gt;dim = 0&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="50" bgcolor="#f0f8ff"&gt;1&lt;/td&gt;

        &lt;td valign="top" width="440"&gt;An intersection must exist and its maximum dimension must be 1&lt;/td&gt;

        &lt;td valign="top" width="152"&gt;dim = 1&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="50" bgcolor="#f0f8ff"&gt;2&lt;/td&gt;

        &lt;td valign="top" width="440"&gt;An intersection must exist and its maximum dimension must be 2&lt;/td&gt;

        &lt;td valign="top" width="152"&gt;dim = 2&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;

  &lt;p&gt;&lt;strong&gt;For example&lt;/strong&gt;, the intersection matrix for the &lt;strong&gt;STWithin &lt;/strong&gt;method would look like:-&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;table cellspacing="0" cellpadding="2" width="415" border="3"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" width="33"&gt;&amp;#160;&lt;/td&gt;

        &lt;td valign="top" width="100"&gt;&amp;#160;&lt;/td&gt;

        &lt;td valign="top" width="87" bgcolor="#b0c4de"&gt;&amp;#160;&lt;/td&gt;

        &lt;td align="center" width="97" bgcolor="#b0c4de"&gt;b&lt;/td&gt;

        &lt;td valign="top" width="91" bgcolor="#b0c4de"&gt;&amp;#160;&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="34"&gt;&amp;#160;&lt;/td&gt;

        &lt;td valign="top" width="100"&gt;&amp;#160;&lt;/td&gt;

        &lt;td valign="top" width="87" bgcolor="#b0c4de"&gt;&lt;strong&gt;Interior (b)&lt;/strong&gt;&lt;/td&gt;

        &lt;td valign="top" width="97" bgcolor="#b0c4de"&gt;&lt;strong&gt;Boundary (b)&lt;/strong&gt;&lt;/td&gt;

        &lt;td valign="top" width="91" bgcolor="#b0c4de"&gt;&lt;strong&gt;Exterior (b)&lt;/strong&gt;&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td align="center" width="35" bgcolor="#b0c4de"&gt;&amp;#160;&lt;/td&gt;

        &lt;td align="center" width="100" bgcolor="#b0c4de"&gt;&lt;strong&gt;Interior (a)&lt;/strong&gt;&lt;/td&gt;

        &lt;td align="center" width="87" bgcolor="#ffffff"&gt;T&lt;/td&gt;

        &lt;td align="center" width="96" bgcolor="#ffffff"&gt;*&lt;/td&gt;

        &lt;td align="center" width="91" bgcolor="#ffffff"&gt;F&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td align="center" width="36" bgcolor="#b0c4de"&gt;a&lt;/td&gt;

        &lt;td align="center" width="100" bgcolor="#b0c4de"&gt;&lt;strong&gt;Boundary (a)&lt;/strong&gt;&lt;/td&gt;

        &lt;td align="center" width="87" bgcolor="#ffffff"&gt;*&lt;/td&gt;

        &lt;td align="center" width="96" bgcolor="#ffffff"&gt;*&lt;/td&gt;

        &lt;td align="center" width="90" bgcolor="#ffffff"&gt;F&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td align="center" width="37" bgcolor="#b0c4de"&gt;&amp;#160;&lt;/td&gt;

        &lt;td align="center" width="100" bgcolor="#b0c4de"&gt;&lt;strong&gt;Exterior (a)&lt;/strong&gt;&lt;/td&gt;

        &lt;td align="center" width="87" bgcolor="#ffffff"&gt;*&lt;/td&gt;

        &lt;td align="center" width="96" bgcolor="#ffffff"&gt;*&lt;/td&gt;

        &lt;td align="center" width="90" bgcolor="#ffffff"&gt;*&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;

  &lt;p&gt;
    &lt;br /&gt;ie: A must be inside B, But A's Interior &amp;amp; Boundary can not be external to B. 

    &lt;br /&gt;This is then mapped to 9 characters representing &amp;lt;top line&amp;gt;&amp;lt;Middle&amp;gt;&amp;lt;bottom Line&amp;gt;. Which in this case is &amp;quot;T*F**F***&amp;quot;. 

    &lt;br /&gt;&lt;/p&gt;

  &lt;p&gt;Experiment by changing the value in the code below, or combine it into the main code set at the top of this blog. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- Create Shapes
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@Main &lt;span style="color: blue"&gt;as GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((10 10, 20 20, 10 30, 0 20, 10 10))'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;0&lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@S_In &lt;span style="color: blue"&gt;as GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((4 20, 16 20, 10 26, 4 20))'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;0&lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@S_On &lt;span style="color: blue"&gt;as GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((12 12, 12 18, 18 18, 18 12, 12 12))'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;0&lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@S_Out &lt;span style="color: blue"&gt;as GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((0 10, 8 10, 0 18, 0 10))'&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;0&lt;span style="color: gray"&gt;);

&lt;/span&gt;&lt;span style="color: green"&gt;-- Intersection Matrix
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@Matrix &lt;span style="color: blue"&gt;as CHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;9&lt;span style="color: gray"&gt;) = &lt;/span&gt;&lt;span style="color: red"&gt;'T*F**F***' &lt;/span&gt;&lt;span style="color: green"&gt;--'FF*FF****'

-- Use STRelate in a query --
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;geom&lt;span style="color: gray"&gt;, &lt;/span&gt;Labels&lt;span style="color: gray"&gt;,  
        &lt;/span&gt;&lt;span style="color: blue"&gt;CASE  &lt;/span&gt;geom&lt;span style="color: gray"&gt;.&lt;/span&gt;STRelate&lt;span style="color: gray"&gt;(&lt;/span&gt;@Main&lt;span style="color: gray"&gt;,&lt;/span&gt;@Matrix&lt;span style="color: gray"&gt;) 
            &lt;/span&gt;&lt;span style="color: blue"&gt;WHEN &lt;/span&gt;0 &lt;span style="color: blue"&gt;THEN &lt;/span&gt;&lt;span style="color: red"&gt;'FALSE' 
            &lt;/span&gt;&lt;span style="color: blue"&gt;WHEN &lt;/span&gt;1 &lt;span style="color: blue"&gt;THEN &lt;/span&gt;&lt;span style="color: red"&gt;'TRUE' 
        &lt;/span&gt;&lt;span style="color: blue"&gt;END AS [&lt;/span&gt;STRelate]
&lt;span style="color: blue"&gt;FROM &lt;/span&gt;&lt;span style="color: gray"&gt;(
    &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@Main &lt;span style="color: blue"&gt;as &lt;/span&gt;geom&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'Main' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Labels'
    &lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@S_In&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'S_In' 
    &lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@S_On&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'S_On'
    &lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@S_Out&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'S_Out'
&lt;/span&gt;&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;As &lt;/span&gt;Tab1&lt;span style="color: gray"&gt;;&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;h2&gt;Method &lt;font color="#ff0000"&gt;.STEquals&lt;/font&gt;()&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Spatial Objects &lt;strong&gt;represent the same set of points as each other&lt;/strong&gt;. &lt;/li&gt;

  &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;font color="#800040"&gt;&lt;strong&gt;Geography&lt;/strong&gt;&lt;/font&gt; objects &lt;/li&gt;

  &lt;li&gt;To use spatial index search on WHERE geo1.STEquals ( geo2 ) = 1 
    &lt;br /&gt;&lt;/li&gt;

  &lt;li&gt;Be careful with this Method it is not as straightforward as you may first think. The following are all considered equal. 
    &lt;ul&gt;
      &lt;li&gt;At its most trivial, two polygons may be comprised of the same points but have a different starting point. 
        &lt;ul&gt;
          &lt;li&gt;eg: A triangle may have the same 3 points.&amp;#160; But if you start at a different point you will have the same triangle but the points will appear in a different order. Clearly that same logic applies to more complex shapes &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;

      &lt;li&gt;A little less obvious. One shape can have more points than the other. So long as the extra points are still on the boundary of the other. 
        &lt;ul&gt;
          &lt;li&gt;eg: LineString(10 10, 10 20) equals LineString(10 10, 10 15, 10 20) &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;A more subtle issue is in GeometryCollections, this function seem to ignore anything in the interior. So if the external polygons are the same then it doesn't seem to matter if there are extra lines, points, polygons contained within each one. &lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;More info&lt;/h1&gt;

&lt;p&gt;For more info see SQL Server 2008 Books Online &lt;a href="ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.en/s10de_1devconc/html/efff121c-7f1c-4e2d-82fe-05544e2f70f8.htm"&gt;Geometry Methods Supported by Spatial Indexes&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please give feedback. Is this series of articles useful? Did it save you time? What was good, What could be better?, Notice any errors? What would you like me to cover? All thoughts, comments, suggestions welcome.&lt;/p&gt;

&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:4d5ca2a9-fa11-4456-a676-83c1548a6a6c" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/SQL%20Spatial" rel="tag"&gt;SQL Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SQL%20Server%20Spatial" rel="tag"&gt;SQL Server Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Spatial" rel="tag"&gt;Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Geography" rel="tag"&gt;Geography&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Geometry" rel="tag"&gt;Geometry&lt;/a&gt;,&lt;a href="http://technorati.com/tags/TSQL" rel="tag"&gt;TSQL&lt;/a&gt;,&lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9012349" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/davidlean/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Spatial/default.aspx">Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/TSQL/default.aspx">TSQL</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server+Spatial/default.aspx">SQL Server Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geometry/default.aspx">Geometry</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Polygon/default.aspx">Polygon</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geography/default.aspx">Geography</category></item><item><title>SQL 2008 Spatial Samples, Part 9 of 9 - Handy but Obvious Methods</title><link>http://blogs.msdn.com/davidlean/archive/2008/10/17/this-site-under-construction.aspx</link><pubDate>Fri, 17 Oct 2008 02:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9002504</guid><dc:creator>davele</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/davidlean/comments/9002504.aspx</comments><wfw:commentRss>http://blogs.msdn.com/davidlean/commentrss.aspx?PostID=9002504</wfw:commentRss><wfw:comment>http://blogs.msdn.com/davidlean/rsscomments.aspx?PostID=9002504</wfw:comment><description>&lt;p&gt;This post covers the following methods: STArea, STLength, STDistance, STIsEmpty, STIsClosed, STIsSimple, STIsRing.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div class="wlWriterSmartContent" id="scid:B3E14793-948F-49af-A347-D19C374A7C4F:e0addd2c-0eeb-405d-80ed-f5edc1bd8f7f" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt; &lt;script type="text/javascript"&gt;&lt;!--
digg_bodytext = 'This post covers the following methods: ST &amp;lt;DL FIX THIS&amp;gt;';
//--&gt;&lt;/script&gt; &lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;h2&gt;Overview of Handy but obvious Methods&lt;/h2&gt;  &lt;p&gt;The following methods are self evident. I&amp;#8217;ve included here for the sake of completeness. You do need to be aware of them, They are great for calculating &amp;amp; displaying attributes associated with your maps. &lt;/p&gt;  &lt;h2&gt;Method &lt;font color="#ff0000"&gt;.STArea&lt;/font&gt;()&lt;/h2&gt;  &lt;ul&gt;   &lt;li&gt;Area of a &lt;strong&gt;Polygon&lt;/strong&gt;. &lt;/li&gt;    &lt;li&gt;Total area of a MultiPolygon. &lt;/li&gt;    &lt;li&gt;The Area for Lines &amp;amp; Points = 0. &lt;/li&gt;    &lt;li&gt;Exceptionally useful with Geography types as you can quickly calculate land area without the huge distortion usually associated with Flat Earth.      &lt;ul&gt;       &lt;li&gt;Note: even Geography is not perfect, the ellipsoidal approximation does have some error. It is good if you want the area &amp;quot;as the crow flies&amp;quot; or have a fairly flat flood plain but doesn't allow for the steep sides of hills &amp;amp; valleys. If you are spraying fertilizer &amp;amp; need to know the actual surface area of the land in square meters, you'd need to allow for the vertical changes. &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;strong&gt;&lt;font color="#804000"&gt;Geography&lt;/font&gt;&lt;/strong&gt; objects &lt;/li&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;Example use: Show Population Density. ie: People per square mile. &lt;/p&gt;    &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- =====&amp;lt; Sample: Area example &amp;gt;=================
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'MULTIPOLYGON (((5 5, 8 5, 8 8, 5 8, 5 5)),
                    ((0 0, 3 0, 3 3, 0 3, 0 0), (0.25 0.25, 0.25 0.75, 0.75 0.75
                    , 0.75 0.25, 0.25 0.25), (1 1, 1 2, 2 2, 2 1, 1 1)))'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;0&lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STArea&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;[Area]&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;WKT&lt;span style="color: gray"&gt;, &lt;/span&gt;@g &lt;span style="color: blue"&gt;AS &lt;/span&gt;Geo
&lt;span style="color: blue"&gt;go&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPart9of9HandybutObv_102DA/Area.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="244" alt="Area" src="http://blogs.msdn.com/blogfiles/davidlean/WindowsLiveWriter/SQL2008SpatialSamplesPart9of9HandybutObv_102DA/Area_thumb.png" width="243" border="0" /&gt;&lt;/a&gt; 

    &lt;br /&gt;The display of the MultiPolygon, Area = 16.75 

    &lt;br /&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;Method &lt;font color="#ff0000"&gt;.STLength&lt;/font&gt;()&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Length of a &lt;strong&gt;Line&lt;/strong&gt; &amp;amp;/or Perimeter of a &lt;strong&gt;Polygon&lt;/strong&gt;. &lt;/li&gt;

  &lt;li&gt;Total length of all shapes in a GeometryCollection. &lt;/li&gt;

  &lt;li&gt;Length of a Point = 0. &lt;/li&gt;

  &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;strong&gt;&lt;font color="#804000"&gt;Geography&lt;/font&gt;&lt;/strong&gt; objects &lt;/li&gt;

  &lt;li&gt;Example use: 
    &lt;ul&gt;
      &lt;li&gt;What is the driving distance from A to B. ie: Find the distance (length) of a line. 
        &lt;ul&gt;
          &lt;li&gt;SQL 2008 doesn't have functions to help you decide on what is the shortest route. For this release, Use MapPoint or Virtual Earth Web service for that type of spatial calculation. &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;

      &lt;li&gt;Handy to improve performance by filtering out objects whose length is 0. ie: Use with STReduce to filter out Objects whose size has been Reduced to 0. &lt;/li&gt;

      &lt;li&gt;Could use it to find points. (as Lines &amp;amp; Polygons have non-zero length) &lt;/li&gt;

      &lt;li&gt;Good for finding the perimeter of a shape.&amp;#160;&amp;#160; &lt;br /&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Method &lt;font color="#ff0000"&gt;.STDistance&lt;/font&gt;()&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Distance between the closest parts of 2 Spatial Objects. &lt;/li&gt;

  &lt;li&gt;This is FANTASTIC as a filter, but make sure you ask for items &amp;quot;Less Than&amp;quot; or Less Than or Equal To&amp;quot; a distance. &amp;quot;Greater than&amp;quot; will not use the spatial index. &lt;/li&gt;

  &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;strong&gt;&lt;font color="#804000"&gt;Geography&lt;/font&gt;&lt;/strong&gt; objects &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Example use: List all Points (Students) within walking distance 3km of this point (the School)&lt;/p&gt;

  &lt;p&gt;As the August 08 release of Books Online states that Geography object can only calculate the distance if at least one of the objects is a point, I've included copious examples to show this is incorrect. Both Geometry &amp;amp; Geography can calculate the distance between any combination of spatial objects. (To be fair to the documentation team this was a limitation at one point while the product was in development. BOL will be corrected in the Dec 08 release)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ====&amp;lt; Geometry Distance function example &amp;gt;===============
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'MULTIPOLYGON( (( 5 5, 8 5, 8 8, 5 8,5 5))
                    ,((0 0, 3 0, 3 3, 0 3, 0 0),(2 2, 2 1, 1 1, 1 2, 2 2)
                    ,(0.25 0.25, 0.75 0.25, 0.75 0.75, 0.25 0.75, 0.25 0.25)) )'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;0&lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@L1 &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(0 4, 8 3 )'
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@P1 &lt;span style="color: blue"&gt;AS GEOMETRY &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: red"&gt;'POINT(2 6)'

&lt;/span&gt;&lt;span style="color: green"&gt;-- Distance Polygon to Line
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: red"&gt;'MultiPolygon to Line' &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;Comment&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STDistance&lt;span style="color: gray"&gt;(&lt;/span&gt;@L1&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;&lt;span style="color: red"&gt;'Shortest Distance'
      &lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;()&lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;WKT&lt;span style="color: gray"&gt;, &lt;/span&gt;@g &lt;span style="color: blue"&gt;AS GEOMETRY
UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: green"&gt;-- Distance Line to Point
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: red"&gt;'Line to Point'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;@L1&lt;span style="color: gray"&gt;.&lt;/span&gt;STDistance&lt;span style="color: gray"&gt;(&lt;/span&gt;@P1&lt;span style="color: gray"&gt;), &lt;/span&gt;@L1&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;(), &lt;/span&gt;@L1
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: green"&gt;-- Distance Point to Polygon
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: red"&gt;'Point to Polygon'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;@P1&lt;span style="color: gray"&gt;.&lt;/span&gt;STDistance&lt;span style="color: gray"&gt;(&lt;/span&gt;@g&lt;span style="color: gray"&gt;), &lt;/span&gt;@P1&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;(), &lt;/span&gt;@P1&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;0.1&lt;span style="color: gray"&gt;) 
&lt;/span&gt;&lt;span style="color: blue"&gt;go&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- ====&amp;lt; Geography Distance function example &amp;gt;===========================
-- Note: Books Online states that Geography STDistance is limited &amp;amp; 
-- can only show distance if at least one of the objects is a Point. This is false. 
-- As can be seen below it works with any combination of objects. 
-- This Error will be corrected in the next release of Books Online. 
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@g &lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@S1 &lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@L1 &lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@L2 &lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@P1 &lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;;
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@P2 &lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;;

&lt;/span&gt;&lt;span style="color: green"&gt;--SET @g = GEOGRAPHY::STGeomFromText('MULTIPOLYGON( (( 5 5, 8 5, 8 8, 5 8,5 5))
--                    ,((0 0, 3 0, 3 3, 0 3, 0 0), (2 2, 2 1, 1 1, 1 2, 2 2)
--                    ,(0.25 0.25, 0.25 0.75, 0.75 0.75, 0.75 0.25, 0.25 0.25)) )', 4326) -- Exterior
--SET @g = GEOGRAPHY::STGeomFromText('POLYGON( (2 2, 2 1, 1 1, 1 2, 2 2) )', 4326) -- Interior (Clockwise points)
--SET @g = GEOGRAPHY::STGeomFromText('POLYGON( (2 2, 1 2, 1 1, 2 1, 2 2) )', 4326) -- Exterior (AntiClockwise: &amp;quot;Look to the Left&amp;quot; of the direction you are drawing)
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@g &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON( (0 0, 3 0, 3 3, 0 3, 0 0)
                    ,(2 2, 2 1, 1 1, 1 2, 2 2), (0.25 0.25, 0.25 0.75
                    ,0.75 0.75, 0.75 0.25, 0.25 0.25))'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: green"&gt;-- Exterior
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@S1 &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON(( 5 5, 8 5, 8 8, 5 8,5 5))'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: green"&gt;-- Exterior

&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@L1 &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(0 4, 8 3 )'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@L2 &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(0 6, 2 6 )'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@P1 &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POINT(2 6)'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: blue"&gt;SET &lt;/span&gt;@P2 &lt;span style="color: gray"&gt;= &lt;/span&gt;&lt;span style="color: blue"&gt;GEOGRAPHY&lt;/span&gt;&lt;span style="color: gray"&gt;::&lt;/span&gt;STGeomFromText&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'POINT(1 7)'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;4326&lt;span style="color: gray"&gt;);

&lt;/span&gt;&lt;span style="color: green"&gt;-- Distance Polygon to Line
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: red"&gt;'MultiPolygon to Line' &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;Comment&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STDistance&lt;span style="color: gray"&gt;(&lt;/span&gt;@L1&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: red"&gt;'Shortest Distance'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;@g&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;()&lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;WKT&lt;span style="color: gray"&gt;, &lt;/span&gt;@g &lt;span style="color: blue"&gt;as &lt;/span&gt;Geo
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: green"&gt;-- Distance Line to Point
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: red"&gt;'Line to Point'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;@L1&lt;span style="color: gray"&gt;.&lt;/span&gt;STDistance&lt;span style="color: gray"&gt;(&lt;/span&gt;@P1&lt;span style="color: gray"&gt;), &lt;/span&gt;@L1&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;(), &lt;/span&gt;@L1
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: green"&gt;--Distance Point to Polygon
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: red"&gt;'Point to Polygon'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;@P1&lt;span style="color: gray"&gt;.&lt;/span&gt;STDistance&lt;span style="color: gray"&gt;(&lt;/span&gt;@g&lt;span style="color: gray"&gt;), &lt;/span&gt;@P1&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;(), &lt;/span&gt;@P1&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;20000&lt;span style="color: gray"&gt;)
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: green"&gt;--Distance Polygon to Polygon
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: red"&gt;'Polygon to Polygon'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;@S1&lt;span style="color: gray"&gt;.&lt;/span&gt;STDistance&lt;span style="color: gray"&gt;(&lt;/span&gt;@g&lt;span style="color: gray"&gt;), &lt;/span&gt;@S1&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;(), &lt;/span&gt;@S1
&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: green"&gt;--Distance Point to Point
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: red"&gt;'Point to Point'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;@P2&lt;span style="color: gray"&gt;.&lt;/span&gt;STDistance&lt;span style="color: gray"&gt;(&lt;/span&gt;@P1&lt;span style="color: gray"&gt;), &lt;/span&gt;@P2&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;(), &lt;/span&gt;@P2&lt;span style="color: gray"&gt;.&lt;/span&gt;STBuffer&lt;span style="color: gray"&gt;(&lt;/span&gt;20000&lt;span style="color: gray"&gt;)
&lt;/span&gt;&lt;span style="color: blue"&gt;UNION &lt;/span&gt;&lt;span style="color: gray"&gt;ALL
&lt;/span&gt;&lt;span style="color: green"&gt;--Distance Line to Line
&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: red"&gt;'Line to Line'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;@L2&lt;span style="color: gray"&gt;.&lt;/span&gt;STDistance&lt;span style="color: gray"&gt;(&lt;/span&gt;@L1&lt;span style="color: gray"&gt;),  &lt;/span&gt;@L2&lt;span style="color: gray"&gt;.&lt;/span&gt;STAsText&lt;span style="color: gray"&gt;(), &lt;/span&gt;@L2
&lt;span style="color: blue"&gt;go&lt;/span&gt;&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;h2&gt;Method &lt;font color="#ff0000"&gt;.STIsEmpty&lt;/font&gt;()&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Returns: 1 (true) if the Spatial Object contains no value. (obvious huh?). 
    &lt;ul&gt;
      &lt;li&gt;It is 0 (false) if the object is NULL or contains some points. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Note: EMPTY is different to NULL. &lt;/li&gt;

  &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;strong&gt;&lt;font color="#804000"&gt;Geography&lt;/font&gt;&lt;/strong&gt; objects 

    &lt;pre class="code"&gt;&lt;span style="color: green"&gt;-- &amp;lt; Empty means no points, different to NULL &amp;gt;==
&lt;/span&gt;&lt;span style="color: blue"&gt;DECLARE &lt;/span&gt;@temp &lt;span style="color: blue"&gt;TABLE &lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;Shape &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;10&lt;span style="color: gray"&gt;), &lt;/span&gt;[geom] &lt;span style="color: blue"&gt;GEOMETRY&lt;/span&gt;&lt;span style="color: gray"&gt;);
&lt;/span&gt;&lt;span style="color: blue"&gt;INSERT INTO &lt;/span&gt;@temp &lt;span style="color: blue"&gt;VALUES 
     &lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: red"&gt;'Nothing'&lt;/span&gt;&lt;span style="color: gray"&gt;,    NULL)
    ,(&lt;/span&gt;&lt;span style="color: red"&gt;'Im Empty'&lt;/span&gt;&lt;span style="color: gray"&gt;,   &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON EMPTY'&lt;/span&gt;&lt;span style="color: gray"&gt;)
    ,(&lt;/span&gt;&lt;span style="color: red"&gt;'Point'&lt;/span&gt;&lt;span style="color: gray"&gt;,      &lt;/span&gt;&lt;span style="color: red"&gt;'POINT(3 3)'&lt;/span&gt;&lt;span style="color: gray"&gt;)
    ,(&lt;/span&gt;&lt;span style="color: red"&gt;'LineString'&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: red"&gt;'LINESTRING(0 0, 3 3)'&lt;/span&gt;&lt;span style="color: gray"&gt;)     
    ,(&lt;/span&gt;&lt;span style="color: red"&gt;'Polygon'&lt;/span&gt;&lt;span style="color: gray"&gt;,    &lt;/span&gt;&lt;span style="color: red"&gt;'POLYGON((0 0, 3 0, 0 3, 0 0))'&lt;/span&gt;&lt;span style="color: gray"&gt;);    

&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;Shape&lt;span style="color: gray"&gt;, &lt;/span&gt;[geom]&lt;span style="color: gray"&gt;.&lt;/span&gt;STDimension&lt;span style="color: gray"&gt;() &lt;/span&gt;&lt;span style="color: blue"&gt;as &lt;/span&gt;[STDimension]
&lt;span style="color: blue"&gt;FROM &lt;/span&gt;@temp
&lt;span style="color: blue"&gt;WHERE &lt;/span&gt;geom&lt;span style="color: gray"&gt;.&lt;/span&gt;STIsEmpty&lt;span style="color: gray"&gt;() = &lt;/span&gt;1&lt;span style="color: gray"&gt;;&lt;/span&gt;&lt;/pre&gt;
    &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Method &lt;font color="#ff0000"&gt;.STIsClosed&lt;/font&gt;()&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Returns: 1 (true) if the Start &amp;amp; End points of a &lt;strong&gt;Line&lt;/strong&gt; the same &lt;/li&gt;

  &lt;li&gt;Polygons are always closed. &lt;/li&gt;

  &lt;li&gt;Points are always open. &lt;/li&gt;

  &lt;li&gt;GeometryCollections are Closed if all their shapes are closed. eg: No Points &amp;amp; no open lines. &lt;/li&gt;

  &lt;li&gt;Works with both &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; &amp;amp;&amp;#160; &lt;strong&gt;&lt;font color="#804000"&gt;Geography&lt;/font&gt;&lt;/strong&gt; objects&amp;#160; &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Method &lt;font color="#ff0000"&gt;.STIsSimple&lt;/font&gt;()&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Returns: 1 (true) if the Object doesn&amp;#8217;t cross over itself 
    &lt;ul&gt;
      &lt;li&gt;Ie: No figures overlap each other. They can touch. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Works with &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#804000"&gt;Geography&lt;/font&gt; Objects. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Method &lt;font color="#ff0000"&gt;.STIsRing&lt;/font&gt;()&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Returns: 1 (true) if the spatial object is :- 
    &lt;ul&gt;
      &lt;li&gt;(A) a Line, &lt;u&gt;and&lt;/u&gt; &lt;/li&gt;

      &lt;li&gt;(B) Simple &lt;u&gt;and&lt;/u&gt; &lt;/li&gt;

      &lt;li&gt;(C) Closed &lt;/li&gt;

      &lt;li&gt;Ie: A line that forms a closed shape &amp;amp; doesn&amp;#8217;t cross itself. &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Works with &lt;font color="#0000ff"&gt;&lt;strong&gt;Geometry&lt;/strong&gt;&lt;/font&gt; objects only. It is not a method of &lt;font color="#804000"&gt;Geography&lt;/font&gt; Objects. &lt;/li&gt;

  &lt;li&gt;Handy if you want to covert the Line into a Polygon, can quickly determine if it is suitable as the perimeter. &lt;/li&gt;

  &lt;li&gt;Can also be used to filter out both Points &amp;amp; Polygons. &lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;More info&lt;/h1&gt;

&lt;p&gt;For more info see SQL Server 2008 Books Online &lt;a href="ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.en/s10de_1devconc/html/efff121c-7f1c-4e2d-82fe-05544e2f70f8.htm"&gt;Geometry Methods Supported by Spatial Indexes&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please give feedback. Is this series of articles useful? Did it save you time? What was good, What could be better?, Notice any errors? What would you like me to cover? All thoughts, comments, suggestions welcome.&lt;/p&gt;

&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:4d5ca2a9-fa11-4456-a676-83c1548a6a6c" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/SQL%20Spatial" rel="tag"&gt;SQL Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SQL%20Server%20Spatial" rel="tag"&gt;SQL Server Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Spatial" rel="tag"&gt;Spatial&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Geography" rel="tag"&gt;Geography&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Geometry" rel="tag"&gt;Geometry&lt;/a&gt;,&lt;a href="http://technorati.com/tags/TSQL" rel="tag"&gt;TSQL&lt;/a&gt;,&lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9002504" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/davidlean/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Spatial/default.aspx">Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/TSQL/default.aspx">TSQL</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/SQL+Server+Spatial/default.aspx">SQL Server Spatial</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geometry/default.aspx">Geometry</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Polygon/default.aspx">Polygon</category><category domain="http://blogs.msdn.com/davidlean/archive/tags/Geography/default.aspx">Geography</category></item></channel></rss>