Saturday, March 18, 2017

SQL Spatial Data Types Introduction


   Spatial Data represent information about the physical location and shape of geometric objects.
Spatial Data type is one of the hidden gems in Microsoft Sql Server. If you have 2008R2 or later version, you can start to use it.
There are two ways to create spatial data type.
  • GEOMETRY
  • GEOGRAPHY
Main difference is Geometry is in 2D, Geography is in 3D. It is very common question to ask Which one should I choose.
To explain it better, let's look at the following pictures.

If you use Geometry type in your table, and you write a query to calculate the distance between this two points. Geometry function will draw a line between the two points and It will calculate the distance.
If your application does not need the perfect spatial data calculations, you can use this method. As I mentioned before Geometry calculates data in 2D.
For example if you are trying to find X stores in y miles, I would use Geometry. But if you are trying to calculate how much fuel you might need for a plane to fly from point A to point B then I would use the Geometry type.
Since the Earth is not a 2D object, It is not easy to represent locations using 2D mathematics.

Let's look at the Geography type,
The geography type represents data in a round-earth coordinate system. As you can see on the next picture. Geography calculates the distance in 3D. (Black line is the Geometry way to calculate the distance)



As you can guess, Geometry functions are faster than Geography functions. I would suggest to use Geometry unless you are planning calculate distances across the globe.

No comments:

Post a Comment