SAMPLE SELECT SQL- USING Spatial Data (SQL Server)
Spatial Data
Spatial data represents information about the physical location and shape of geometric objects. These objects can be point locations or more complex objects such as countries, roads, or lakes.
SQL Server supports two spatial data types: the geometry data type and the geography data type.
The geometry type represents data in a Euclidean (flat) coordinate system.
The geography type represents data in a round-earth coordinate system.
Both data types are implemented as .NET common language runtime (CLR) data types in SQL Server.
SAMPLE Spatial Data:
create table #TabTempData
(
Id int,
Country varchar(10),
CityDestination varchar (50),
UF varchar(2),
latitude varchar(50),
longitude varchar(50),
)
insert into #TabTempData
select 1, 'Brasil', 'Curitiba', 'PR', '-25.429254184060234', '-49.26584648282211' union all
select 2, 'Brasil', 'São Paulo', 'SP', '-23.55235554266759', '-46.59968586115112' union all
select 3, 'Brasil', 'Rio de Janeiro', 'RJ', '-22.952250809418217', '-43.22048362692426' union all
select 4, 'Brasil', 'Belo Horizonte', 'MG', '-19.92906240989488', '-43.942731675446765' union all
select 5, 'Brasil', 'Cuiabá', 'MS', '-15.56707737175929', '-56.06402016869596' union all
select 6, 'Brasil', 'Manaus', 'PA', '-3.0672756192623662', '-59.99457316080589' union all
select 7, 'Brasil', 'Goiania', 'GO', '-16.703658890071704', '-49.282282683335005' union all
select 8, 'Brasil', 'Salvador', 'BA', '-12.923733407245209', '-38.34796190742438' union all
select 9, 'Brasil', 'Porto Alegre', 'RS', '-30.00355235670754', '-51.16218955930785' union all
select 10, 'Brasil', 'Florianópolis', 'SC', '-27.588200267761327', '-48.54030952351798'
/* CALCULATE DISTANCE CITY ORIGIN AND CITY DESTINATION */
SELECT distinct
T.*
,CityOrigin = @CityOrigin
,UFOrigin = @UForigin
,latitudeOrigin=@latitude
,longitudeOrigin=@longitude
,cast(cast('POINT(' + replace(T.[Longitude],',','.') + ' ' + replace(T.[Latitude],',','.') + ')' as geography).STDistance(cast('POINT(' + replace(@longitude,',','.') + ' ' + replace(@latitude,',','.') + ')' as geography)) / 1000 as int) as DistKMLineStraight
FROM #TabTempData T with (nolock)
WHERE (isnull(T.[Latitude],'') <> '' and isnull(T.[Longitude],'') <> '')
AND (cast('POINT(' + replace(T.[Longitude],',','.') + ' ' + replace(T.[Latitude],',','.') + ')' as geography).STDistance(cast('POINT(' + replace(@longitude,',','.') + ' ' + replace(@latitude,',','.') + ')' as geography)) IS NOT NULL)
Order by DistKMLineStraight
Sample 01 - Spatial Data - Calculate Distance
City Origin: Brasília-DF
Latitude Origin: -15.772943465543149
Longitude Origin: -47.894965768880766
7 | Brasil | Goiania-GO | -16.703658890071704 | -49.282282683335005 | 180 |
4 | Brasil | Belo Horizonte-MG | -19.92906240989488 | -43.942731675446765 | 622 |
2 | Brasil | São Paulo-SP | -23.55235554266759 | -46.59968586115112 | 871 |
5 | Brasil | Cuiabá-MS | -15.56707737175929 | -56.06402016869596 | 876 |
3 | Brasil | Rio de Janeiro-RJ | -22.952250809418217 | -43.22048362692426 | 933 |
8 | Brasil | Salvador-BA | -12.923733407245209 | -38.34796190742438 | 1076 |
1 | Brasil | Curitiba-PR | -25.429254184060234 | -49.26584648282211 | 1078 |
10 | Brasil | Florianópolis-SC | -27.588200267761327 | -48.54030952351798 | 1309 |
9 | Brasil | Porto Alegre-RS | -30.00355235670754 | -51.16218955930785 | 1610 |
6 | Brasil | Manaus-PA | -3.0672756192623662 | -59.99457316080589 | 1932 |