verticapy.sql.geo.coordinate_converter¶
- verticapy.sql.geo.coordinate_converter(vdf: Annotated[str | vDataFrame, ''], x: str, y: str, x0: float = 0.0, earth_radius: Annotated[int | float | Decimal, 'Python Numbers'] = 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()
AbccityAbc1 Abu Dhabi 2 Amsterdam 3 Apia 4 Ashgabat 5 Bangui 6 Budapest 7 Bujumbura 8 Castries 9 Colombo 10 Conakry 11 Cotonou 12 Dakar 13 Dublin 14 Georgetown 15 Havana 16 Kuala Lumpur 17 København 18 Lilongwe 19 Ljubljana 20 London 21 Monrovia 22 Oslo 23 Panama City 24 Paris 25 Phnom Penh 26 Podgorica 27 Pretoria 28 Rangoon 29 San Marino 30 San Salvador 31 Seoul 32 Suva 33 Tegucigalpa 34 Tehran 35 Thimphu 36 Vatican City 37 Warsaw 38 Yaounde 39 Zagreb 40 Andorra 41 Athens 42 Banjul 43 Basseterre 44 Belgrade 45 Bloemfontein 46 Bogota 47 Brasilia 48 Accra 49 Addis Ababa 50 Ankara 51 Antananarivo 52 Asmara 53 Astana 54 Asuncion 55 Bamako 56 Bangkok 57 Beijing 58 Berlin 59 Brussels 60 Buenos Aires 61 Chisinau 62 Damascus 63 Dar es Salaam 64 Dhaka 65 Djibouti 66 Doha 67 Jakarta 68 Jerusalem 69 Kampala 70 Kinshasa 71 Kuwait 72 La Paz 73 Lisbon 74 Luxembourg 75 Madrid 76 Manila 77 Maputo 78 Mbabane 79 Melekeok 80 Minsk 81 Nairobi 82 Naypyidaw 83 New Delhi 84 Nicosia 85 Nouakchott 86 Palikir 87 Port-au-Prince 88 Praia 89 Quito 90 Rome 91 Saint George's 92 San Jose 93 Sarajevo 94 Taipei 95 Tbilisi 96 Tokyo 97 Tripoli 98 Valletta 99 Vienna 100 Vilnius Rows: 1-100 | Columns: 2Note
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)
Abccity100%... 🌎100%🌎lon100%1 Andorra ... 42.5000014435459 2 Athens ... 37.9852720905523 3 Banjul ... 13.4538764603159 4 Basseterre ... 17.3020304554894 5 Belgrade ... 44.8205913044467 6 Bloemfontein ... -29.1199938773787 7 Bogota ... 4.59836942114782 8 Brasilia ... -15.781394372879 9 Bridgetown ... 13.1020025827511 10 Cairo ... 30.0519062051037 11 Cape Town ... -33.9180651086288 12 Dili ... -8.55938840854645 13 Funafuti ... -8.51665199904107 14 Gaborone ... -24.6463134574389 15 Guatemala ... 14.6230805214482 16 Harare ... -17.8158438357778 17 Helsinki ... 60.1775092325681 18 Johannesburg ... -26.1680988813841 19 Juba ... 4.82997519827796 20 Khartoum ... 15.5900240842777 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)
Abccity100%... 🌎100%🌎lon100%1 Andorra ... 4725.78454290873 2 Athens ... 4223.76954368256 3 Banjul ... 1496.00280608979 4 Basseterre ... 1923.89800730006 5 Belgrade ... 4983.8223622637 6 Bloemfontein ... -3237.99558308512 7 Bogota ... 511.315350469114 8 Brasilia ... -1754.81098964113 9 Bridgetown ... 1456.87621608583 10 Cairo ... 3341.61950600567 11 Cape Town ... -3771.51676167934 12 Dili ... -951.760566210609 13 Funafuti ... -947.008494290607 14 Gaborone ... -2740.54501695872 15 Guatemala ... 1626.0123658999 16 Harare ... -1981.03144843023 17 Abu Dhabi ... 2720.57108506641 18 Amsterdam ... 5821.26729771856 19 Apia ... -1539.10958564241 20 Ashgabat ... 4219.84690274865 We can effortlessly reverse the operation.
convert_reverse_xy = coordinate_converter(convert_xy, "lon", "lat", reverse = True) display(convert_reverse_xy)
Abccity100%... 🌎100%🌎lon100%1 Andorra ... 42.5000014435459 2 Athens ... 37.9852720905523 3 Banjul ... 13.4538764603159 4 Basseterre ... 17.3020304554894 5 Belgrade ... 44.8205913044467 6 Bloemfontein ... -29.1199938773787 7 Bogota ... 4.59836942114782 8 Brasilia ... -15.781394372879 9 Abu Dhabi ... 24.4666835723799 10 Amsterdam ... 52.3519145466644 11 Apia ... -13.8415450424484 12 Ashgabat ... 37.949994933111 13 Bangui ... 4.36664430634909 14 Budapest ... 47.5019521849913 15 Bujumbura ... -3.37608722037464 16 Castries ... 14.0019734893303 17 Colombo ... 6.93196575818212 18 Conakry ... 9.53346870502179 19 Cotonou ... 6.40195442278247 20 Dakar ... 14.7177775836233 Note
This function can be employed to operate on the Euclidean plane instead of a sphere, significantly improving computation speed.