Loading...

verticapy.sql.geo.coordinate_converter#

verticapy.sql.geo.coordinate_converter(vdf: str | vDataFrame, x: str, y: str, x0: float = 0.0, earth_radius: int | float | Decimal = 6371, reverse: bool = False) vDataFrame#

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

Parameters#

vdf: SQLRelation

Input vDataFrame.

x: str

vDataColumn used as the abscissa (longitude).

y: str

vDataColumn 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#

vDataFrame

result of the transformation.

Examples#

For this example, we will use the Cities dataset.

import verticapy.datasets as vpd

cities = vpd.load_cities()
Abc
city
Varchar(82)
Abc
Long varchar(2411724)
1Abidjan
2Abu Dhabi
3Abuja
4Accra
5Addis Ababa
6Algiers
7Amman
8Amsterdam
9Andorra
10Ankara
11Antananarivo
12Apia
13Ashgabat
14Asmara
15Astana
16Asuncion
17Athens
18Baghdad
19Baku
20Bamako
21Bandar Seri Begawan
22Bangkok
23Bangui
24Banjul
25Basseterre
26Beijing
27Beirut
28Belgrade
29Belmopan
30Berlin
31Bern
32Bishkek
33Bissau
34Bloemfontein
35Bogota
36Brasilia
37Bratislava
38Brazzaville
39Bridgetown
40Brussels
41Bucharest
42Budapest
43Buenos Aires
44Bujumbura
45Cairo
46Canberra
47Cape Town
48Caracas
49Castries
50Chisinau
51Colombo
52Conakry
53Cotonou
54Dakar
55Damascus
56Dar es Salaam
57Dhaka
58Dili
59Djibouti
60Doha
61Dublin
62Dushanbe
63Freetown
64Funafuti
65Gaborone
66Georgetown
67Guatemala
68Hanoi
69Harare
70Hargeysa
71Havana
72Helsinki
73Honiara
74Islamabad
75Jakarta
76Jerusalem
77Johannesburg
78Juba
79Kabul
80Kampala
81Kathmandu
82Khartoum
83Kiev
84Kigali
85Kingston
86Kingstown
87Kinshasa
88Kuala Lumpur
89Kuwait
90København
91La Paz
92Libreville
93Lilongwe
94Lima
95Lisbon
96Ljubljana
97Lome
98London
99Luanda
100Lusaka
Rows: 1-100 | Columns: 2

Note

VerticaPy 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 VerticaPy environment.

Let’s extract the latitude and longitude.

cities["lat"] = "ST_X(geometry)"
cities["lon"] = "ST_Y(geometry)"
display(cities)
Abc
city
Varchar(82)
100%
...
🌎
Geometry(1048576)
100%
🌎
lon
Float(22)
100%
1Abidjan...5.32194282609856
2Abu Dhabi...24.4666835723799
3Abuja...9.0852790077542
4Accra...5.55198046444593
5Addis Ababa...9.03525622129575
6Algiers...36.7650106566281
7Amman...31.9519711058275
8Amsterdam...52.3519145466644
9Andorra...42.5000014435459
10Ankara...39.9291844440755
11Antananarivo...-18.9146914920322
12Apia...-13.8415450424484
13Ashgabat...37.949994933111
14Asmara...15.3333392526819
15Astana...51.1811253042576
16Asuncion...-25.2944571170577
17Athens...37.9852720905523
18Baghdad...33.3405943561586
19Baku...40.397217891343
20Bamako...12.6519605263233

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

from verticapy.sql.geo import coordinate_converter

convert_xy = coordinate_converter(cities, "lon", "lat")
display(convert_xy)
Abc
city
Varchar(82)
100%
...
🌎
Geometry(1048576)
100%
🌎
lon
Float(22)
100%
1Abidjan...591.773042154565
2Abu Dhabi...2720.57108506641
3Abuja...1010.23693281258
4Accra...617.352060476089
5Addis Ababa...1004.67465274177
6Algiers...4088.08266305019
7Amman...3552.89708326154
8Amsterdam...5821.26729771856
9Andorra...4725.78454290873
10Ankara...4439.92273523603
11Antananarivo...-2103.21773296097
12Apia...-1539.10958564241
13Ashgabat...4219.84690274865
14Asmara...1704.9895334181
15Astana...5691.08147379289
16Asuncion...-2812.61530364517
17Athens...4223.76954368256
18Baghdad...3707.30494371905
19Baku...4491.96568007214
20Bamako...1406.83382263437

We can effortlessly reverse the operation.

convert_reverse_xy = coordinate_converter(convert_xy, "lon", "lat", reverse = True)
display(convert_reverse_xy)
Abc
city
Varchar(82)
100%
...
🌎
Geometry(1048576)
100%
🌎
lon
Float(22)
100%
1Abidjan...5.32194282609856
2Abu Dhabi...24.4666835723799
3Abuja...9.0852790077542
4Accra...5.55198046444593
5Addis Ababa...9.03525622129575
6Algiers...36.7650106566281
7Amman...31.9519711058275
8Amsterdam...52.3519145466644
9Andorra...42.5000014435459
10Ankara...39.9291844440755
11Antananarivo...-18.9146914920322
12Apia...-13.8415450424484
13Ashgabat...37.949994933111
14Asmara...15.3333392526819
15Astana...51.1811253042576
16Asuncion...-25.2944571170577
17Athens...37.9852720905523
18Baghdad...33.3405943561586
19Baku...40.397217891343
20Bamako...12.6519605263233

Note

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