What is spatial sql


Published on

My Power-point Slides from my presentation on Spatial-SQL at #dddnorth in Bradford UK 13th Oct 2012

Published in: Education
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Just want to say a quick thanks to Syncfusion and their awesome collection of developer Ebooks called the succintly series.They are sponsoring this Event today, and in the very near future also publishing my Ebook on GIS in .NETYou can download that book and more from the address on the screen.
  • As per the slide..
  • As per the slide.
  • Well truth is, it looks NO different to regular SQL, well unless your using MS-SQL server but we’ll come back to that in just a moment.As far as SQL goes however, ALL of your existing skills are easily transferable, there are some new concepts to learn, and I’ll give you a basic grounding in those concepts in this session this morning.Just on a quick not before we go any further. This session will NOT be MS-SQL server specific, what I’m going to show you is general Spatial SQL as defined in the OGC specification, you’ll see later that this still applies to MS-SQL but afterwards you’ll also understand why I’ve choose to do things this way.. “I Hope anyway ”Moving on.. So if it doesn’t look any different then why do we have such a separate branch of SQL devoted just to this subject.
  • Is it more complicated?Well yes and no, it can be there’s no two ways about it. The SQL statement you see on the screen at the moment actually comes from the core of a mobile application I’m currently working on, and esentially while it looks very scary it’s deceptively simple.What it does, is given a specific point at lat/lng 54.78655,-1.59244 it returns some attributes about a specific road (Defined in a spatial database) and uses some spatial operations to tell me how far away from that road I am, and what my distance in meters is from the start and ends of the road.All whilst restricting the search for that road only to within 12 meters of the location provided.This is done ALL in one statement, and each of the data items requested comes back as an ordinary everyday data row, just like any other data row you’d get from any back end database you work with.That said, I’ve certainly seen regular SQL that’s way more complicated than what I’ve got here, and for the record this statement searches a spatial table on a mobile device with about 10,000 rows and comes back in under a second.
  • As the first bullet point says, YES... And that’s the whole reason spatial SQL exists. It’s not something magical that suddenly enables your DBMS to make maps, it’s simply additions that allow the system to handle something it originally didn’t. We’ll see in a little while a few differences.The core thing to remember here though, is spatial data by it’s very nature needs is a highly specialised form of data, because of this (and a huge amount of maths) we need special functions and data types to handle and operate on that data.So one more question to complete this basic question...
  • If you've worked with Images / files and other discrete objects in any DBMS system, then you’ve almost definitely worked with BLOBS or Binary Large Objects to give them the full name.Spatial data is stored using the same mechanism in most cases, which means at a base level it’s actually possible to store spatial data in a DBMS that’s not actually spatially aware. We’ll come to that a little more later when I briefly touch on some of the software choices you have available.Along with your data blob, you’ll also have a series of attributes, attributes in spatial terms is the term used to describe data annotations that are attached to a specific instance of geometry.For instance, a polygon showing the outline of a country might have attributes that show the population, the %of voters for different leaders and many other things.Next slide
  • It’s the combination of these attributes and data blobs that makes things so useful.On the screen is a small section of one of the ordnance survey products called “Code point with Polygons” this data set has a polygon outline for EVERY postcode in the UK (1.7 million at the time I wrote this presentation)You can clearly see the geometry blob that defines that polygon, and you can see we have 4 columns of attributes.We have GID – which is the geometric equivalent of a primary key IDWe have the actual postcode, and 2 fields upp and pc area.The post code area chows the first to letters of the post codes ‘Out code’ and the upp is the royal mail universal post point identifier. (That small light coloured mail bar code thing that's normally stamped on the front of most letters you receive)
  • As per the slide....YESA blob in spatial terms holds a huge amount of information, looking back at the previous slide, you’ll see the headers of the blob fields are actually quite uniform.Crammed into these blobs is everything a spatial engine needs to reconstruct a vector version of the defined geometry, and more importantly all the details it needs to perform tasks such as re-projection of co-ordinates and exactly what type of geometry it is.
  • If you want to store your data in regular columns, then there’s absolutely no reason why you cant, but think about a few previous points.A polygon for example may have in excess of several hundred individual co-ordiantes defining it’s outer shell, just how many columns do you need in your table in order to accurately represent every possible polygon you want to store in there?A simple point column on the other hand where you have one dot per user from a mobile device is a different story, that’s 2 columns you can work that out ahead of time, but then you deny yourself the ability to use those points as source points to do lookups for example on the post codes you saw in the previous slides, or against a list of your companies depots.Realistically, you wouldn’t want to use regular columns, unless you had a specific reason.
  • Why, well here’s the final call....As per the slides...
  • Ok so now you understand what spatial data is all about... WHY do we need it.
  • Because of the mobile phone and our increasingly mobile yet socially active life.EVERY emergency service out there is now fitted with some kind of GPS system, almost ALL consumer cars now either have a sat nav fitted as standard, or one installed by the owner.In any given instance, you are most likely to be no further away from someone broadcasting their location on some kind of personal device than 20 meters
  • All of these “Outputs” produce masses and masses of data based on everything from where we are, to where we’ve physically been.Just as there is a move in normal DBMS circles to the “BIG Data” paradigm (With companies using systems such as Hadoop and other document db’s) then so there is also a move to manage all this trillions of terabytes of spatial data that we are now producing.Imagine you’ve published a tourist app to guide people around the city of London, and in that app’s data store you’ve listed every theatre, every pub, every restraunt, cinema, sports venue and so on. Now imagine your tourist, walking around with a Google map using your app to find these venues in a given radius let’s say 1 mile from their location. That’s one user, consuming a lot of data, scale that up to 100 or even 1000 users, and then imagine that , those 1000 users ALL share that location info with their friends via twitter or facebook.
  • So what is this all designed to solve....
  • As per the slide...
  • As per the slide..
  • Location on earth is a particularly tricky thing to calculate for a number of different reasons, you’ll see in just a moment, why when we get to covering co-ordinate systems, but just take a quick moment to look at the formulae below.The meridional arc is the curvature of the prime meridian, curved because of the curvature of the earth, and that formulae is used to take that curve into account when producing a linear set of co-ordinates.
  • As per slides...
  • As per slides...
  • As per slides...
  • As per slides...
  • As per slides...
  • As per slides...
  • As per slides...
  • As per slides...
  • As per slides...
  • As per slides...
  • As per slides...
  • Numbers numbers and more numbers. The world of GIS and spatial is full of them. However there is one number that shines above them all, the SRID or “Spatial Reference System Identifier”, this is a unique number that identifies the local co-ordinate system that your points making up your geometry are defined in.WGS84 is the one most of you will be familiar with, WGS84 is the system used above all by Google maps, and is generally the one that your mobile phone will give out from it’s GPS sensor.WGS is what’s known as a global geodetic co-ordinate system, that is it covers the entire world, with the point of origin at 0,0 (Just off the coast of Africa.Here in the UK we also have to deal with the prime meridian, the line of 0 degrees longitude that passes through the UK in Greenwich London.West of this line, co-ordinates are expressed as a negative decimal, east they are represented as a positive decimal.OSGB36 on the other hand is strictly only to cover the UK, is typically expressed in meter’s east and north from it’s origin just off the coast of the Scilly Isles.
  • Yes I said it... All you nay sayers out there who still believe the world is flat, well I'm sorry to shatter your illusion but it’s not However in order to do great (and accurate) mapping, we need a way to take the horrid maths that comes from calculating position round a sphere and making so it works with all those maps that ... Well need to be flat.
  • There are many different co-ordinate systems in use throughout the world.In the US for example, there are more than 100 just to divide up and map the entire continent. The reason for this is because of the size of the country. As it is so large, the curvature of the earth actually effects the local geography.If your working in a specific territory, especially if your working with systems such as satellite navigation, then in order to get the best accuracy you MUST make sure you use the correct co-ordinate system for the area where your app is to work.If your working with data obtained from the ordnance survey in the UK, then you’ll almost be guaranteed to be dealing with data that’s defined using SRID 27700.
  • All spatial databases hold a meta table which defines the different SRID’s they know about, this table is populated when you create and set up a spatial DB automatically by the SP’s or init scripts used to create that database.MS-SQL does not set this table up, nor does it populate it when adding geometry, and it doesn’t usually reference it when working with SRID’s, this prevents you from doing on the fly transformations in your SQL statements.Another kick in the teeth when it comes to MS-SQL is that it does not populate the objects SRID property by default, instead every geometric object is stored with an SRID of 0 and you are expected to populate it yourself, this cause a LOT of problems when trying to use MS-SQL with web mapping systems and GIS management software that expects the SRID to be set.It’s not an issue if you are using EF to make use of the objects however as all of the functionality inside MS-SQL is designed to work with an SRID of 0.
  • As you previously saw, most of the time geometric objects also have a number of attributes defined along side the geometry blob.Without attributes, most forms of geometry would be next to useless. Attributes allow us to give meaning and context to our geometry.Imagine a list of place names with out a column holding the places name as a regular VARCHAR column, you’d have a point showing a location, but nothing to allow you to give an identity to that location.Attributes however don’t have to be physical. You can ask your spatial database to compute distances, areas, sizes, and even proximity to other objects such as “How many customers live near our main office”It’s questions such as this where spatial SQL really shines through, it allows you to ask near English questions that relate to your geometries position, it’s attributes and proximity to other objects in your database.
  • The geometry type is stored as part of the blob that defines the geometry instance along with it’s SRID. MS-SQL actually stores this correctly, due to it storing the blob in an OGC compliant binary format.The MULTI geometry objects are similar to .NET collections, in so much that you can think of them as a LIST of the given Geometry type.It’s very rare to see MULTIPOINTS but MULTIPOLYGONS and MULTILINES are very common place
  • As with the SRID meta data table, there is also a Meta Data table that is used to define what data is held in what columns for the geometry in the table.Almost ALL GIS software used to work with a spatial data table will expect this table to be present, in fact a lot of GIS management software won’t recognise you have geometric spatial data in your DB unless this table is present and the table, column type and SRID of the geometry are listed in it.MS-SQL does NOT create this table, nor does it populate it when you add geometry to the table using the ST_AddGeometryColumn spatial function. You will have to create and populate this by hand if you want other user space apps to be able to use your spatial database.
  • Geometries as you have seen are stored in a specialist binary format. This format is known as WKB or “Well known Binary”, there is also an extended version that stores extra information such as the SRID, this is called EWKB or “Extended Well Known Binary”The text version is known as WKT “Well known Text” and EWKT “Extended Well Known Text” , both text and binary formats are actually stored in your database as binary objects.
  • Well known text is very easy to read, once you know a few little things.Notice that there is a SPACE between the co-ordinate pairs and not a comma as you might expect. A comma is used to seperate co-ordinate pairs as you can see in the line example on the slide.Notice also that the polygon definition has 2 sets of parentheses surrounding it. This is because a single polygon can be a multi object in it’s own right, for example, think about a donught shape with the middle cut out.
  • Adding geometry is easy once you know how to define the geometry in it’s well known text format, the ST_GeomFromText function can be used in an insert statemant, and it can also be used to create geometrys on the fly.For example, if you wanted to take a GPS position and search against some data you already had, you would use GeomFrom text and a text format of the point to inject that point into your query.
  • As per the slide....
  • Reading your geometry back is just as easy, and most spatial databases will allow you to output the data not only in WKT format, but in a number of different formats.GeoJSON – a specialist form of JSON designed to represent spatial objects in Javascript and very similar to regular JSON, used by systems such as Openlayers a Javascript mapping toolkit that’s part of the OpenGeo stackGML – A specialist form of XML called Geography Mark Up Language and used extensively here in the UK by the ordnance survey in there paid for OS-Vectormap local products and many others.KML – A specialist form of XML used by Google earth for producing GE overlays when doing custom layers using the Google earth API.
  • As per slide.
  • As per slide.
  • As per slide.
  • As per slide.
  • Finding points in a polygon is probably one of the most common spatial operations performed, here we build a polygon on the fly using GeomFromText then ask our database to return all the points that fall within that polygon.Both the table of the points and the polygon are expected to be defined using SRID 27700 which will represent all co-ordinates in meters east and north of the uk origin.
  • As per the slide.
  • In this example, we cast a radius of 10 meters using and SRID of 27700 around a point located at 1,2, we then ask the database to return all lines that fall within that radius.In this case, those lines could represent roads, and the point could be a position from an in vehicle GPS device.Attributes against that road could list the road name, the town where it’s situated and other stuff.The Ordanance Survey opendata site provide a free map product to download called ‘Vectormap District’ which contains this exact data for every road in the UK, allowing you to produce such a system fro tracking deliverys or service vehicles.
  • As per slide
  • Another common task is listing polygons in order of size.This would prove useful for a housing developer to know the size and cost of a number of plots, or a farmer wanting to decide which field to use for a given crop.Here we use the ST_Area virtual attribute function to list the top 10 largest in our database.
  • As per slide.
  • In our final example we show the distance from a given point to the first 10 points in our database.This comes in useful for pilots looking to locate landmarks, or geocachers trying to find hidden loot in a GeoCache competition.
  • As per slide
  • What is spatial sql

    1. 1. WHAT IS SPATIAL SQL?Peter “Shawty” Shaw
    2. 2. WHO AM I• I‟m the UK & Europe group manager for the Linked.NET Users group (Lidnug) a global .NET user group based on the Linked-In platform.• I‟ve been doing I.T & Computing in one form or another since the early 1980‟s and I pride myself on being one of the UK‟s original back bedroom developers.• I now own & run Digital Solutions UK, offering consultancy & other I.T related services in the North East of England.• I‟m also Chief Technical Person for a GPS/GIS fleet management solutions provider based in the North East of England called Enablecom.
    3. 3. HOW TO CONTACT ME....• Twitter : @shawty_ds• Skype : shawty_ds• Emails peter.shaw@lidnug.org peter.shaw@digital-solutions.me.uk• Lidnug http://www.linkedin.com/groups/LinkedNET- users-group-LIDNUG-43315
    4. 4. A QUICK SPONSORS PLUG Syncfusion are generously sponsoring this session today with the „Succinctly Series‟ of free .NET developer E-books. In the next few weeks my book „GIS Succinctly‟ (GIS for the .NET developer will be available along with all the other great titles.You can download the E-Books free from :
    5. 5. INTRODUCTIONAka What will we be covering in this session.
    6. 6. WHAT WILL WE BE COVERING• The Definition of Spatial SQL. (What makes it different from normal SQL)• Why we have a need for Spatial SQL.• What problem is Spatial SQL designed to solve?• OGC Standards. (The rulebook of everything GIS)• Spatial Specifics Part 1 – SRID‟s & Co-Ordinate systems.
    7. 7. WHAT WILL WE BE COVERING• Spatial Specifics Part 2 – Geometries & Attributes• Software choices.• Spatial SQL Examples.
    9. 9. WHAT EXACTLY DOESSPATIAL SQL LOOK LIKE?• Just the same as normal SQL:
    10. 10. SO IT MUST BE MORECOMPLICATED THEN?• If it‟s not so different to regular SQL that is....It CAN be.... But no more so than some regular SQL I‟ve seen:SELECT roadType,approachSpeed,recordID,Distance(Transform(GeomFromText(POINT(-1.5924443 54.7865551),4326),27700),PointN(Transform(geometry,27700),NumPoints(geometry)) ) AS DistanceToEnd,Distance(Transform(GeomFromText(POINT(-1.5924443 54.7865551),4326),27700),PointN(Transform(geometry,27700),1) ) AS DistanceFromStart,Distance(Transform(GeomFromText(POINT(-1.5924443 54.7865551),4326),27700),Transform(geometry,27700)) AS Distance FROM roads WHERE Distance(GeomFromText(POINT(-1.5924443 54.7865551),4326),geometry) < 0.0002 ORDER BY Distance(GeomFromText(POINT(-1.5924443 54.7865551),4326),geometry) LIMIT 1
    11. 11. SO IT MUST HANDLE SOMETHINGDATABASES NORMALY DONT?• YES, that‟s to $1000 dollar answer.• We have databases with specific spatial functionality, because the extra data that needs to be processed, typically needs to be processed in a highly specialised way.• In order to understand that, we need to answer one more question.......
    12. 12. WHAT DOES SPATIAL DATALOOK LIKE?• In it‟s raw form spatial data is generally stored in a BLOB• For spatial data to be useful it also needs attributes.• Thus in a standard non spatial database a spatial data record is no more than a Binary Object with accompanying normal database columns containing supporting data.
    14. 14. BUT THERE‟S NOTHING SPECIALABOUT BLOBS IS THERE?• In the case of spatial yes, very much so.• If you where able to see on the previous slide, the blobs shown where all actually quite uniform.• The data is used to hold things like the geometry type, the spatial reference ID and the actual definition of the points that make the geometry up.
    15. 15. SO CAN‟T I JUST STORE ALL THATINFO IN REGULAR COLUMNS?• Yes, if you want to there‟s nothing stopping you.• For example, there‟s nothing stopping you keeping 2 decimal columns labelled X & Y and filling them with the Longitude & Latitude values you obtain from your users logging onto your app with a mobile phone.• In reality though, you wouldn‟t want to.......
    16. 16. THERE‟S ONE MORE PART TOTHE GIS STORY.....• GIS Databases have highly optimized functions added that understand the different specialist types of BLOB.• These functions understand how to use GIS specific indexes (such as GIST indexes) to speed up location searches.• They also understand how to store multiple arrays of many co-ordinates in ONE binary field and still perform well.• And they know how to output that binary data into many different formats we can read.
    17. 17. THERE‟S ONE MORE PART TOTHE GIS STORY.....• Putting your data into normal non GIS columns means that you cannot use ANY of these specialist functions to perform searches, transformations, composition and many other tasks that GIS systems do routinely.• It means that in the long run, you‟ll most likely end up writing your own code, inside your own app to do many of the tasks that you can do in simple SQL statements.
    19. 19. TODAY‟S MODERN WORLDRELIES ON MOBILE• More and more we are using devices that we carry with us.• Most devices these days are fitted with some kind of location awareness.• Most vehicles have built in sat-navs which can provide location awareness and web browsing capabilities.• Social media sites such as Twitter & Facebook can do clever things if they know a persons location.
    20. 20. ALL OF THESE SOURCESPRODUCE TERABYTES OFSPATIAL DATA.• That‟s FAR too much specialist data to be handled in an ordinary way using standard SQL database techniques.• Imagine for example doing a lookup for point‟s of interest in a given area from a mobile app, in the centre of London?• Or how about a company such as the AA wanting to keep track of where every one of their vehicles deployed in the UK are?
    22. 22. PUT SIMPLY....• Spatial SQL is designed to make easier the handling of masses of specialist formatted data, that‟s stored using it‟s own binary format.• It‟s designed to allow us as humans to NOT need know anything about the maths behind it all (Who remembers trigonometry classes from school?)• It‟s designed to output this specialist data, no matter what it‟s input format as something we can read and understand easily.
    23. 23. PUT SIMPLY....• Most importantly however......• It‟s designed to do ALL OF THE ABOVE, very, very fast.• After all. What‟s the use of having this data if your web app for tourists in London takes over a minute to find points of interest?
    24. 24. THERE‟S ALSO THE TRICKYSUBJECT OF ACTUAL LOCATION• As we‟ll see in just a moment, your location on the earth isn‟t always what you might think it is.• The maths to make sure this location is as precise as it can be would make the average developers hair go white instantly.
    25. 25. OGC STANDARDSThe rulebook of everything GIS
    26. 26. GROAN!! NOT MORESTANDARDS COMPLIANCE...• Unfortunately YES, but... NOT for you as a GIS developer.• The Open Geospatial Consortium (OGC) are responsible for making sure that anyone who makes any kind of Geospatial software, make it so that pretty much everything can make use of it.• This means that any software designed to work with a GIS database for example can do so without a problem.• Which leads me onto...........
    27. 27. A SPOT OF SQL-SERVERBASHING.....• My favourite phrase for this part of my talk is:“The Most Compatible, Incompatible Spatial SQL implementation, I’ve ever seen”
    28. 28. WHY IS SQL SERVER SO BADFOR GIS?The Good points:• It supports 75% of the over 300 functions the OGC recommend.• It supports both geometry and geography data types.• It works seamlessly with the tools we as .NET developers are used too.
    29. 29. WHY IS SQL SERVER SO BADFOR GIS?The Bad points:• It‟s implementation is CLR based and therefore an object model that doesn‟t scale well and cannot do normal SQL set based operations.• It understands SRIDs in it‟s own way but doesn‟t create standard meta data about spatial co-ordinate systems preventing use with standard GIS software.
    30. 30. WHY IS SQL SERVER SO BADFOR GIS?The Bad points:• It generally needs specialist adapter technologies (such as ODBC) to translate calls for standard Spatial SQL statements (Due to it‟s CLR nature)• It does not understand a lot of the intricies involved in Spatial SQL, it‟s designed for high speed searching of flat geometry via the Entity Framework classes supplied in EF4.5
    31. 31. WHY IS SQL SERVER SO BADFOR GIS?The Bad points:• Specialist tools are needed to load data into the system.• I‟ll stop here  because there are many more.
    32. 32. BUT AS .NET DEV‟S WERESTUCK WITH SQL-SERVER?• If you absolutley have to use SQL-Server, there are a number of things you can do to become more standards based.• You can use toolkits such as “Dot Spatial”, “Sharp Map” and the “.NET Topology Suite”• All of these are available via NuGet and / or Codeplex.
    33. 33. BUT AS .NET DEV‟S WERESTUCK WITH SQL-SERVER?• However......• I strongly recommend that if possible you use a database made for the job such as “PostgreSQL” and it‟s companion “PostGIS”• Both are free and you can get an all in one installer from “Enterprise DB” at: http://www.enterprisedb.com/products- services-training/pgdownload
    34. 34. BUT AS .NET DEV‟S WERESTUCK WITH SQL-SERVER?• If you can‟t install and / or use a completely different server.• You can install “System.Data.SQLite” from NuGet, to install the SQLite single file database.• Then you can download the “Spatialite” extensions for SQLite from http://www.gaia-gis.it/gaia-sins/And with a simple SQL command load it in to the DB to give you local spatial facilities.
    35. 35. ONCE YOUR OGCCOMPLIANT LIFE IS GOOD• You can data load using standard tools, such as the excellent free GIS package “Quantum GIS”• Everything else I show you here, will just work.• Index, Geometry, Functions and Scripts are 99.5% compatible across all platforms.• There is a heap of documentation, and websites out there (Such as BostonGIS.com and gis.stackexchange.com) that will help you go further.
    36. 36. SPATIAL SPECIFICS PART 1SRID‟s & Co-Ordinate systems
    37. 37. WHAT‟S IN A NUMBER?• When it comes to spatial SQL numbers are everything• There is one overall number you MUST know if you want to do anything useful, that number is the SRID• The 2 most common SRID‟s you‟ll use in the UK are 4326 (Known as WGS84) and 27700 (Known as OSGB36)
    38. 38. WHY DO WE HAVE DIFFERENTCO-ORDINATE SYSTEMS?• In a word....• Because the Earth is round!!!• Translating from a spherical co-ordinate system to a flat planar model needed for a map is actually a very tricky thing to do.• Local co-ordinate systems where invented to allow more accuracy over a smaller area when used on a flat map than if using a global system.
    39. 39. HOW MANY CO-ORDINATESYSTEMS ARE THERE?• At last count, well over 900• Most of the co-ordinate systems are defined by the EPSG (European Petroleum Survey Group), a full list can be found on their site at http://www.epsg.org/• If your dealing with world wide positioning, most of the time you can just use WGS84 and take a hit on the accuracy.• If your working in a specific territory however, then you really do need to use an SRID for that locale.
    40. 40. MS-SQL AND SRID‟S• An OGC compliant database will have a meta table called „spatial_ref_sys‟• This table will hold a list of ALL the SRID‟s known by that DB, allowing you to transform co-ordinates on the fly in standard SQL.• MS-SQL DOES NOT by default create or use this table. Instead, each geometry instance holds a „property‟ in it‟s CLR object letting anything referencing the object know what SRID it was defined in, which is 0 by default.
    41. 41. SPATIAL SPECIFICS PART 2Geometries & Attributes
    42. 42. ATTRIBUTES• Attributes as we‟ve already seen are discrete items of data that mean something only when used with a specific bit of geometry• Attributes are just regular data columns like any other data type in a normal DBMS.• Attributes can also be virtual, EG: ST_Area() used on a polygon object will compute and return the overall area of an object defined in it‟s local co-ordinate system.
    43. 43. GEOMETRIES• There are 3 base geometry types: POINT LINE POLYGON• As well as these three base types there are also „MULTI‟ versions (Think of them as being similar to .NET collections) MULTIPOINT, MULTILINE, MULTIPOLYGON
    44. 44. GEOMETRIES• One other meta data table that is managed by the DB is a table called „geometry_columns‟ which holds details of the geometry type and SRID assigned to it.• This is the most important of the 2 tables as most GIS software will use this to locate spatial data in your database.• MS-SQL does NOT create and / or populate this table automatically, other OGC compliant DB‟s do
    45. 45. GEOMETRIES• An OGC compliant DB will provide access functions to transform these geometry blobs between different formats that we as humans can understand.• OGC Compliant formats are known as WKB, EWKB, WKT and EWKT• Input / output functions will Take these geometry objects and transfer them between GeoJSON, KML, GML and many other regular text formats.
    46. 46. WHAT DOES WKT LOOK LIKE?• Very simple, it‟s the human readable version of the binary blob. POINT(-1.23456 54.123456)• Or as an EWKT SRID=4326;POINT(-1.23456 54.123456)• Lines and polygons are similar but with multiple points LINE(1 2,3 4,5 6) or POLYGON((1 2,3 4,5 6))
    47. 47. ADDING WKT TO YOURDATABASE• You can use the WKT format to add data to the binary object in your DB by using the ST_GeomFromText() function. INSERT INTO table VALUES(ST_GeomFromText(„POINT(1 2)‟,27700)• The first parameter is the text the second is the SRID to use.
    48. 48. ADDING WKT TO MS-SQL• Yes, you guessed it... MS does it differently INSERT INTO mydb VALUES(geometry::STGeomFromText( „POINT(1 2)‟).SRID = 27700;• No surprise there... 
    49. 49. WHAT ABOUT READINGGEOMETRY BACK?• Again very simple. SELECT ST_AsText(myGeometry)• Will give you the WKT format SELECT ST_AsGeoJSON(myGeometry) SELECT ST_AsKml(myGeometry)• Will give you GeoJSON & KML as expected
    50. 50. READING IT BACK IN MS-SQL• And once again.... SELECT geometry::myGeometryColumn.AsText()• To the best of my knowledge only GeoJSON and GML outputs are supported on top of WKT in MS-SQL
    52. 52. DEVELOPMENT KITS• Dot Spatial• Sharp Map• .NET Topology Suite• Telerik RAD Controls• DEV Express controls
    53. 53. MANAGMENT TOOLS• Jump GIS• Quantum GIS• Arc GIS• Map Info• Open GEO
    54. 54. DATA UP-LOADERS• Geo Kettle• Safe FME• SharpMap MS-SQL Tools• Quantum GIS Plugin (Spit)• GDAL
    56. 56. FINDING POINTS IN APOLYGON• Given a source polygon, return a list of geometric points that are contained within it SELECT * FROM mytable WHERE ST_Within(points,ST_GeomFromText( „POLYGON( ..... )‟, 27700) Where 27700 MUST be the same SRID as the „points‟ column in your database
    57. 57. FINDING POINTS IN A POLYGON(THE MS-SQL WAY)• Given a source polygon, return a list of geometric points that are contained within it SELECT * FROM mytable WHERE geometry::STGeomFromText(„POLYGON(...)‟).S RID = 27700.STWithin(geometry::points) Where 27700 MUST be the same SRID as the „points‟ column in your database
    58. 58. FINDING LINES IN A GIVENRADIUS• Given a table of „LINES‟ find which lines are within a 10 meter radius of the supplied point. SELECT * FROM mylinetable WHERE ST_Dwithin(geometrycolumn,ST_GeomFromTex t(„POINT(1 2)‟,27700),10) Where 27700 is the SRID of the point your querying and of the lines in your database so that the „10‟ represents meters.
    59. 59. FINDING LINES IN A GIVENRADIUS (THE MS-SQL WAY)• Given a table of „LINES‟ find which lines are within a 10 meter radius of the supplied point. SELECT * FROM mylinetable WHERE geometry::geometrycolumn.STDwithin(geome try::STGeomFromText(„POINT(1 2)‟).SRID=27700,10) Where 27700 is the SRID of the point your querying and of the lines in your database so that the „10‟ represents meters.
    60. 60. LISTING POLYGONS INORDER OF SIZE• Given a table of „POLYGONS‟ list the top 10 largest , ordering by the biggest at the top. SELECT * FROM mypolytable ORDER BY ST_Area(geometrycolumn) LIMIT 10
    61. 61. LISTING POLYGONS INORDER OF SIZE (MS-SQL)• Given a table of „POLYGONS‟ list the top 10 largest , ordering by the biggest at the top. SELECT TOP 10 * FROM mypolytable ORDER BY geometrycolumn.STArea()
    62. 62. FINDING THE DISTANCEBETWEEN TWO POINTS• Given a table of „POINTS‟ show the distance in fractions of a degree between that point and a provided point, listing the top 10 closest. SELECT ST_Distance(geometrycolumn,ST_GeomFromT ext(„POINT(1 2)‟,4326) FROM mypointtable ORDER BY ST_Distance(geometrycolumn,ST_GeomFromT ext(„POINT(1 2)‟,4326) LIMIT 10
    63. 63. FINDING THE DISTANCE BETWEENTWO POINTS (MS-SQL)• Given a table of „POINTS‟ show the distance in fractions of a degree between that point and a provided point, listing the top 10 closest. SELECT geometry::geometrycolumn.STDistance(ST_Ge omFromText(„POINT(1 2)‟).SRID=4326) FROM mypointtable ORDER BY geometry::geometrycolumn.STDistance(ST_Ge omFromText(„POINT(1 2)‟).SRID=4326)
    64. 64. SUMMARY
    65. 65. WHAT DID WE LEARN• Spatial SQL is noting special.• Spatial SQL is pretty much like normal SQL with a lot of specialist functions and datatypes for handling specialist data.• Spatial SQL is governed by a worldwide standard to ensure that most software that is used to manage it all works in harmony.• Spatial Data is everywhere around us weather we realise it or not.
    66. 66. WHAT DID WE LEARN• Spatial SQL cam produce huge volumes of specialist data that is difficult to manage.• SRID‟s are extremely important if you want to represent real world locations.• If you mix your SRID‟s your heading for trouble.• MS-SQL spatial may be useful and interesting, but of your serious about handling spatial data MS-SQL should not be your first choice.
    67. 67. THANK YOUFor watching....