Loading...

vastorbit.sql.geo.coordinate_converter

vastorbit.sql.geo.coordinate_converter(vdf: Annotated[str | VastFrame, ''], x: str, y: str, x0: float = 0.0, earth_radius: Annotated[int | float | Decimal, 'Python Numbers'] = 6371, reverse: bool = False) VastFrame

Converts between geographic coordinates (latitude and longitude) and Euclidean coordinates (x,y).

Parameters:
  • vdf (SQLRelation) – Input VastFrame.

  • x (str) – VastColumn used as the abscissa (longitude).

  • y (str) – VastColumn used as the ordinate (latitude).

  • x0 (float, optional) – The initial abscissa.

  • earth_radius (PythonNumber, optional) – Earth radius in km.

  • reverse (bool, optional) – If set to True, the Euclidean coordinates are converted to latitude and longitude.

Returns:

result of the transformation.

Return type:

VastFrame

Examples

For this example, we will use the Cities dataset.

import vastorbit.datasets as vod

cities = vod.load_cities()
Abc
name
Varchar(82)
Abc
geometry
Varchar
1Vatican CityPOINT (12.45338654497177 41.90328217996012)
2San MarinoPOINT (12.44177015780014 43.936095834768)
3VaduzPOINT (9.516669472907267 47.13372377429357)
4LuxembourgPOINT (6.130002806227083 49.61166037912108)
5PalikirPOINT (158.1499743237623 6.916643696007725)
6MajuroPOINT (171.3800001757465 7.103004311216239)
7FunafutiPOINT (179.2166470940289 -8.516651999041073)
8MelekeokPOINT (134.6265484669922 7.487396172977981)
9MonacoPOINT (7.406913173465057 43.73964568785249)
10TarawaPOINT (173.0175708285494 1.338187505624603)
11MoroniPOINT (43.24024409869332 -11.70415769566847)
12AndorraPOINT (1.51648596050552 42.5000014435459)
13Port-of-SpainPOINT (-61.51703088544974 10.65199708957726)
14KigaliPOINT (30.05858591906411 -1.95164421006325)
15MbabanePOINT (31.13333451205636 -26.31665077840921)
16JubaPOINT (31.58002559278731 4.829975198277964)
17LjubljanaPOINT (14.51496903347413 46.0552883087945)
18BratislavaPOINT (17.11698075223461 48.15001832996171)
19DohaPOINT (51.5329678942993 25.28655600890659)
20PodgoricaPOINT (19.26630692411823 42.46597251288171)
21BernPOINT (7.466975462482424 46.91668275866772)
22PristinaPOINT (21.16598425159987 42.66670961411938)
23RoseauPOINT (-61.38701298180337 15.30101564428332)
24DjiboutiPOINT (43.14800166705226 11.59501446425548)
25BanjulPOINT (-16.5917014892126 13.45387646031594)
26SkopjePOINT (21.4334614651425 42.00000612290586)
27BridgetownPOINT (-59.61652673505159 13.10200258275114)
28BujumburaPOINT (29.3600060615284 -3.376087220374643)
29KingstownPOINT (-61.21206242027932 13.14827882786841)
30CastriesPOINT (-61.00000818036955 14.00197348933034)
31BasseterrePOINT (-62.71700931969934 17.30203045548939)
32Port LouisPOINT (57.49999385460973 -20.16663857135325)
33Saint George'sPOINT (-61.74164322607476 12.05263340172041)
34ManamaPOINT (50.58305171591019 26.23613629048594)
35Saint John'sPOINT (-61.85003381513866 17.11803651831411)
36MontevideoPOINT (-56.17299814703597 -34.85609570759073)
37LomePOINT (1.22081126074562 6.133882930268385)
38TunisPOINT (10.17967809921203 36.80277813623144)
39Abu DhabiPOINT (54.36659338259199 24.46668357237991)
40AshgabatPOINT (58.38329911177465 37.94999493311099)
41LusakaPOINT (28.28138173611427 -15.41469840933593)
42HararePOINT (31.04276357206282 -17.8158438357778)
43DiliPOINT (125.5794559317051 -8.559388408546454)
44Port VilaPOINT (168.3166405835686 -17.73335040402583)
45TegucigalpaPOINT (-87.2194751979415 14.1039907590764)
46GeorgetownPOINT (-58.16702864748061 6.801973692752028)
47ReykjavíkPOINT (-21.95001448717954 64.15002361973922)
48Port-au-PrincePOINT (-72.33798044690553 18.54297045473237)
49KampalaPOINT (32.58137766712105 0.318604813383331)
50ParamariboPOINT (-55.16703088542437 5.835030129922586)
51NiameyPOINT (2.114710186530374 13.51865181050647)
52DushanbePOINT (68.77387935270173 38.56003521631658)
53AsuncionPOINT (-57.64345102790134 -25.29445711705768)
54ManaguaPOINT (-86.27043751890119 12.15496243875612)
55FreetownPOINT (-13.23616159901275 8.471957271098177)
56IslamabadPOINT (73.16468862105955 33.70194180895959)
57KathmanduPOINT (85.31469635222788 27.71863777247722)
58BloemfonteinPOINT (26.22991288117737 -29.1199938773787)
59PretoriaPOINT (28.22748321723384 -25.70497469518443)
60Port MoresbyPOINT (147.1925036205936 -9.464707825867777)
61HoniaraPOINT (159.9497657336057 -9.437994295089595)
62Panama CityPOINT (-79.53498301041077 8.969963049094872)
63RabatPOINT (-6.83640815612614 34.02530731107282)
64ChisinauPOINT (28.85771113965143 47.00502361967062)
65MaputoPOINT (32.58721710397009 -25.95333162877898)
66MogadishuPOINT (45.36473175245874 2.068627192947531)
67MuscatPOINT (58.59331213260884 23.61332480772813)
68ColomboPOINT (79.85775060925641 6.931965758182116)
69UlaanbaatarPOINT (106.9146699037465 47.91861925856074)
70WellingtonPOINT (174.7832742912769 -41.29997393927641)
71WindhoekPOINT (17.08354610054181 -22.57000608438381)
72AbujaPOINT (7.53138214293233 9.085279007754195)
73BissauPOINT (-15.59836084132075 11.86502382298056)
74AmmanPOINT (35.93135406687412 31.95197110582745)
75VilniusPOINT (25.3166352932829 54.68336631175862)
76RigaPOINT (24.09996537140319 56.95002382316096)
77BishkekPOINT (74.58325836390367 42.87502530509011)
78MaseruPOINT (27.48327306998448 -29.31667437868163)
79AntananarivoPOINT (47.51467804152986 -18.91469149203215)
80QuitoPOINT (-78.5019969671124 -0.2130423220355624)
81San JosePOINT (-84.08599721127536 9.936958288356607)
82San SalvadorPOINT (-89.2049870794599 13.71194750549404)
83KingstonPOINT (-76.7674337136691 17.9770766238309)
84NdjamenaPOINT (15.0472024554623 12.11504239481064)
85MalaboPOINT (8.783277545821136 3.750015278026183)
86AsmaraPOINT (38.93332352575931 15.33333925268192)
87ZagrebPOINT (15.99999466824568 45.80000673327254)
88TallinnPOINT (24.72804072947855 59.43387737948592)
89LilongwePOINT (33.78330195998353 -13.98329506546918)
90GuatemalaPOINT (-90.52891143656154 14.62308052144817)
91LibrevillePOINT (9.457965045823698 0.3853886097185182)
92SuvaPOINT (178.4417073153799 -18.13301593137123)
93NouakchottPOINT (-15.97534041489001 18.08642702124752)
94BamakoPOINT (-8.001984963249697 12.65196052632325)
95BeirutPOINT (35.50776235137766 33.8739209756269)
96TbilisiPOINT (44.78884959099798 41.72695584707759)
97AstanaPOINT (71.427774209483 51.18112530425759)
98VientianePOINT (102.5999800201548 17.96669272762739)
99BrazzavillePOINT (15.28274363384867 -4.257239913197509)
100ConakryPOINT (-13.68218088612394 9.53346870502179)
Rows: 1-100 | Columns: 2

Note

vastorbit offers a wide range of sample datasets that are ideal for training and testing purposes. You can explore the full list of available datasets in the Datasets, which provides detailed information on each dataset and how to use them effectively. These datasets are invaluable resources for honing your data analysis and machine learning skills within the vastorbit environment.

Let’s extract the latitude and longitude.

cities["lat"] = "ST_X(geometry)"
cities["lon"] = "ST_Y(geometry)"
display(cities)
Abc
name
Varchar(82)
100%
🌎
geometry
Geometry
100%
🌎
lat
Double
100%
🌎
lon
Double
100%
1Vatican CityPOINT (12.45338654497177 41.90328217996012)12.4533865449717741.90328217996012
2San MarinoPOINT (12.44177015780014 43.936095834768)12.4417701578001443.936095834768
3VaduzPOINT (9.516669472907267 47.13372377429357)9.51666947290726747.13372377429357
4LuxembourgPOINT (6.130002806227083 49.61166037912108)6.13000280622708349.61166037912108
5PalikirPOINT (158.1499743237623 6.916643696007725)158.14997432376236.916643696007725
6MajuroPOINT (171.3800001757465 7.103004311216239)171.38000017574657.103004311216239
7FunafutiPOINT (179.2166470940289 -8.516651999041073)179.2166470940289-8.516651999041073
8MelekeokPOINT (134.6265484669922 7.487396172977981)134.62654846699227.487396172977981
9MonacoPOINT (7.406913173465057 43.73964568785249)7.40691317346505743.73964568785249
10TarawaPOINT (173.0175708285494 1.338187505624603)173.01757082854941.338187505624603
11MoroniPOINT (43.24024409869332 -11.70415769566847)43.24024409869332-11.70415769566847
12AndorraPOINT (1.51648596050552 42.5000014435459)1.5164859605055242.5000014435459
13Port-of-SpainPOINT (-61.51703088544974 10.65199708957726)-61.5170308854497410.65199708957726
14KigaliPOINT (30.05858591906411 -1.95164421006325)30.05858591906411-1.95164421006325
15MbabanePOINT (31.13333451205636 -26.31665077840921)31.13333451205636-26.31665077840921
16JubaPOINT (31.58002559278731 4.829975198277964)31.580025592787314.829975198277964
17LjubljanaPOINT (14.51496903347413 46.0552883087945)14.5149690334741346.0552883087945
18BratislavaPOINT (17.11698075223461 48.15001832996171)17.1169807522346148.15001832996171
19DohaPOINT (51.5329678942993 25.28655600890659)51.532967894299325.28655600890659
20PodgoricaPOINT (19.26630692411823 42.46597251288171)19.2663069241182342.46597251288171

Let’s leverage the coordinate_converter function to calculate Euclidean distances. We’ll project the latitude and longitude into x, y coordinates.

from vastorbit.sql.geo import coordinate_converter

convert_xy = coordinate_converter(cities, "lon", "lat")
display(convert_xy)
Abc
name
Varchar(82)
100%
🌎
geometry
Geometry
100%
🌎
lat
Double
100%
🌎
lon
Double
100%
1Vatican CityPOINT (12.45338654497177 41.90328217996012)1395.78709070616154659.432388166911
2San MarinoPOINT (12.44177015780014 43.936095834768)1394.4643136926374885.470953395329
3VaduzPOINT (9.516669472907267 47.13372377429357)1063.10487273054975241.030957567467
4LuxembourgPOINT (6.130002806227083 49.61166037912108)682.92932660475535516.56493657113
5PalikirPOINT (158.1499743237623 6.916643696007725)nan769.0956884041286
6MajuroPOINT (171.3800001757465 7.103004311216239)nan789.8180433416741
7FunafutiPOINT (179.2166470940289 -8.516651999041073)nan-947.0084942906066
8MelekeokPOINT (134.6265484669922 7.487396172977981)nan832.5604682130363
9MonacoPOINT (7.406913173465057 43.73964568785249)825.91483420583964863.626693719747
10TarawaPOINT (173.0175708285494 1.338187505624603)nan148.79966152459278
11MoroniPOINT (43.24024409869332 -11.70415769566847)5342.626251721591-1301.4429564062034
12AndorraPOINT (1.51648596050552 42.5000014435459)168.645236677104584725.784542908726
13Port-of-SpainPOINT (-61.51703088544974 10.65199708957726)-8735.7337195383951184.4480349935966
14KigaliPOINT (30.05858591906411 -1.95164421006325)3507.153915093358-217.01293477426088
15MbabanePOINT (31.13333451205636 -26.31665077840921)3645.992942090856-2926.278052835681
16JubaPOINT (31.58002559278731 4.829975198277964)3704.1583176657364537.0687378675563
17LjubljanaPOINT (14.51496903347413 46.0552883087945)1631.53699319401815121.114405090408
18BratislavaPOINT (17.11698075223461 48.15001832996171)1932.28194366398775354.037756134252
19DohaPOINT (51.5329678942993 25.28655600890659)6708.6014190944142811.7367405038945
20PodgoricaPOINT (19.26630692411823 42.46597251288171)2183.8680295958814722.0006984597285

We can effortlessly reverse the operation.

convert_reverse_xy = coordinate_converter(convert_xy, "lon", "lat", reverse = True)
display(convert_reverse_xy)
Abc
name
Varchar(82)
100%
🌎
geometry
Geometry
100%
🌎
lat
Double
100%
🌎
lon
Double
100%
1Vatican CityPOINT (12.45338654497177 41.90328217996012)12.45338654497176641.90328217996012
2San MarinoPOINT (12.44177015780014 43.936095834768)12.44177015780014543.93609583476798
3VaduzPOINT (9.516669472907267 47.13372377429357)9.51666947290726447.13372377429357
4LuxembourgPOINT (6.130002806227083 49.61166037912108)6.13000280622708449.61166037912108
5PalikirPOINT (158.1499743237623 6.916643696007725)nan6.916643696007725
6MajuroPOINT (171.3800001757465 7.103004311216239)nan7.103004311216238
7FunafutiPOINT (179.2166470940289 -8.516651999041073)nan-8.516651999041073
8MelekeokPOINT (134.6265484669922 7.487396172977981)nan7.48739617297798
9MonacoPOINT (7.406913173465057 43.73964568785249)7.406913173465051543.73964568785248
10TarawaPOINT (173.0175708285494 1.338187505624603)nan1.338187505624603
11MoroniPOINT (43.24024409869332 -11.70415769566847)43.24024409869333-11.704157695668473
12AndorraPOINT (1.51648596050552 42.5000014435459)1.516485960505518842.50000144354589
13Port-of-SpainPOINT (-61.51703088544974 10.65199708957726)-61.5170308854497310.65199708957726
14KigaliPOINT (30.05858591906411 -1.95164421006325)30.058585919064114-1.9516442100632503
15MbabanePOINT (31.13333451205636 -26.31665077840921)31.13333451205635-26.316650778409205
16JubaPOINT (31.58002559278731 4.829975198277964)31.5800255927873224.829975198277965
17LjubljanaPOINT (14.51496903347413 46.0552883087945)14.51496903347413646.0552883087945
18BratislavaPOINT (17.11698075223461 48.15001832996171)17.11698075223461448.150018329961725
19DohaPOINT (51.5329678942993 25.28655600890659)51.5329678942992825.28655600890659
20PodgoricaPOINT (19.26630692411823 42.46597251288171)19.26630692411822642.465972512881706

Note

This function can be employed to operate on the Euclidean plane instead of a sphere, significantly improving computation speed.