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()
AbccityVarchar(82)AbcLong varchar(2411724)1 Abidjan 2 Abu Dhabi 3 Abuja 4 Accra 5 Addis Ababa 6 Algiers 7 Amman 8 Amsterdam 9 Andorra 10 Ankara 11 Antananarivo 12 Apia 13 Ashgabat 14 Asmara 15 Astana 16 Asuncion 17 Athens 18 Baghdad 19 Baku 20 Bamako 21 Bandar Seri Begawan 22 Bangkok 23 Bangui 24 Banjul 25 Basseterre 26 Beijing 27 Beirut 28 Belgrade 29 Belmopan 30 Berlin 31 Bern 32 Bishkek 33 Bissau 34 Bloemfontein 35 Bogota 36 Brasilia 37 Bratislava 38 Brazzaville 39 Bridgetown 40 Brussels 41 Bucharest 42 Budapest 43 Buenos Aires 44 Bujumbura 45 Cairo 46 Canberra 47 Cape Town 48 Caracas 49 Castries 50 Chisinau 51 Colombo 52 Conakry 53 Cotonou 54 Dakar 55 Damascus 56 Dar es Salaam 57 Dhaka 58 Dili 59 Djibouti 60 Doha 61 Dublin 62 Dushanbe 63 Freetown 64 Funafuti 65 Gaborone 66 Georgetown 67 Guatemala 68 Hanoi 69 Harare 70 Hargeysa 71 Havana 72 Helsinki 73 Honiara 74 Islamabad 75 Jakarta 76 Jerusalem 77 Johannesburg 78 Juba 79 Kabul 80 Kampala 81 Kathmandu 82 Khartoum 83 Kiev 84 Kigali 85 Kingston 86 Kingstown 87 Kinshasa 88 Kuala Lumpur 89 Kuwait 90 København 91 La Paz 92 Libreville 93 Lilongwe 94 Lima 95 Lisbon 96 Ljubljana 97 Lome 98 London 99 Luanda 100 Lusaka 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)
AbccityVarchar(82)100%... 🌎Geometry(1048576)100%🌎lonFloat(22)100%1 Abidjan ... 5.32194282609856 2 Abu Dhabi ... 24.4666835723799 3 Abuja ... 9.0852790077542 4 Accra ... 5.55198046444593 5 Addis Ababa ... 9.03525622129575 6 Algiers ... 36.7650106566281 7 Amman ... 31.9519711058275 8 Amsterdam ... 52.3519145466644 9 Andorra ... 42.5000014435459 10 Ankara ... 39.9291844440755 11 Antananarivo ... -18.9146914920322 12 Apia ... -13.8415450424484 13 Ashgabat ... 37.949994933111 14 Asmara ... 15.3333392526819 15 Astana ... 51.1811253042576 16 Asuncion ... -25.2944571170577 17 Athens ... 37.9852720905523 18 Baghdad ... 33.3405943561586 19 Baku ... 40.397217891343 20 Bamako ... 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)
AbccityVarchar(82)100%... 🌎Geometry(1048576)100%🌎lonFloat(22)100%1 Abidjan ... 591.773042154565 2 Abu Dhabi ... 2720.57108506641 3 Abuja ... 1010.23693281258 4 Accra ... 617.352060476089 5 Addis Ababa ... 1004.67465274177 6 Algiers ... 4088.08266305019 7 Amman ... 3552.89708326154 8 Amsterdam ... 5821.26729771856 9 Andorra ... 4725.78454290873 10 Ankara ... 4439.92273523603 11 Antananarivo ... -2103.21773296097 12 Apia ... -1539.10958564241 13 Ashgabat ... 4219.84690274865 14 Asmara ... 1704.9895334181 15 Astana ... 5691.08147379289 16 Asuncion ... -2812.61530364517 17 Athens ... 4223.76954368256 18 Baghdad ... 3707.30494371905 19 Baku ... 4491.96568007214 20 Bamako ... 1406.83382263437 We can effortlessly reverse the operation.
convert_reverse_xy = coordinate_converter(convert_xy, "lon", "lat", reverse = True) display(convert_reverse_xy)
AbccityVarchar(82)100%... 🌎Geometry(1048576)100%🌎lonFloat(22)100%1 Abidjan ... 5.32194282609856 2 Abu Dhabi ... 24.4666835723799 3 Abuja ... 9.0852790077542 4 Accra ... 5.55198046444593 5 Addis Ababa ... 9.03525622129575 6 Algiers ... 36.7650106566281 7 Amman ... 31.9519711058275 8 Amsterdam ... 52.3519145466644 9 Andorra ... 42.5000014435459 10 Ankara ... 39.9291844440755 11 Antananarivo ... -18.9146914920322 12 Apia ... -13.8415450424484 13 Ashgabat ... 37.949994933111 14 Asmara ... 15.3333392526819 15 Astana ... 51.1811253042576 16 Asuncion ... -25.2944571170577 17 Athens ... 37.9852720905523 18 Baghdad ... 33.3405943561586 19 Baku ... 40.397217891343 20 Bamako ... 12.6519605263233 Note
This function can be employed to operate on the Euclidean plane instead of a sphere, significantly improving computation speed.