Loading...

Integrating with GeoPandas

As of version 0.4.0, VerticaPy features GeoPandas integration. This allows you to easily export a vDataFrame as a GeoPandas DataFrame, giving you more control over geospatial data.

This example demonstrates the advantages of GeoPandas integration with the world dataset.

import verticapy as vp
from verticapy.datasets import load_world

# Setting the plotting lib
vp.set_option("plotting_lib", "matplotlib")

world = load_world()
world.head(100)
123
pop_est
Int
100%
...
Abc
country
Varchar(82)
100%
🌎
Geometry(1048576)
100%
157713...Greenland
2279070...New Caledonia
3339747...Iceland
4360346...Belize
51291358...Timor-Leste
61467152...eSwatini
71772255...Gabon
81792338...Guinea-Bissau
91895250...Kosovo
101958042...Lesotho
112103721...Macedonia
122214858...Botswana
132484780...Namibia
142875422...Kuwait
153045191...Armenia
163351827...Puerto Rico
173474121...Moldova
183500000...Somaliland
193753142...Panama
203758571...Mauritania
214510327...New Zealand
224954674...Congo
235445829...Slovakia
246653210...Libya
258754413...Austria
269850845...Hungary
279960487...Sweden
2811138234...Bolivia
2911147407...Cuba
3015460732...Guatemala
3115972000...Zambia
3217885245...Mali
3318556698...Kazakhstan
3423508428...Taiwan
3524184810...Côte d'Ivoire
3625054161...Madagascar
3725248140...North Korea
3829310273...Angola
3929384297...Nepal
4029748859...Uzbekistan
41265100...N. Cyprus
42603253...W. Sahara
43758288...Bhutan
44920938...Fiji
451221549...Cyprus
463360148...Uruguay
474543126...Palestine
484689021...Liberia
494930258...Costa Rica
505011102...Ireland
515625118...Central African Rep.
525918919...Eritrea
536025951...Nicaragua
546072475...United Arab Emirates
558468555...Tajikistan
569038741...Honduras
5710734247...Dominican Rep.
5810768477...Greece
5911901484...Rwanda
6013805084...Zimbabwe
6117789267...Chile
6219196246...Malawi
6321529967...Romania
6428571770...Saudi Arabia
6531304016...Venezuela
6639192111...Iraq
6739570125...Uganda
6840969443...Algeria
6954841552...South Africa
7080594017...Germany
7182021564...Iran
7283301151...Dem. Rep. Congo
7396160163...Vietnam
74105350020...Ethiopia
75207353391...Brazil
761281935911...India
77282814...Vanuatu
78329988...Bahamas
79443593...Brunei
80594130...Luxembourg
81737718...Guyana
82865267...Djibouti
831218208...Trinidad and Tobago
841251581...Estonia
851972126...Slovenia
862051363...Gambia
872823859...Lithuania
882990561...Jamaica
893068243...Mongolia
903856181...Bosnia and Herz.
914292095...Croatia
925789122...Kyrgyzstan
936163195...Sierra Leone
946172011...El Salvador
957111024...Serbia
967126706...Laos
977531386...Somalia
987965055...Togo
998236303...Switzerland
1008299706...Israel

The apply() function of the VerticaPy stats module allows you to apply any Vertica function to the data. Let’s compute the area of each country.

import verticapy.sql.functions as fun

world["geography"] = fun.apply("stv_geography", world["geometry"])
world["geography"].astype("geography")
world["area"] = fun.apply("st_area", world["geography"])
world.head(100)
123
pop_est
Int
100%
...
🌎
Geography
100%
123
area
Float(22)
100%
157713...2189751414857.83
2279070...23282206866.0818
3339747...107031853684.148
4360346...22118506248.1485
51291358...14776389256.2834
61467152...18151531542.9276
71772255...270683290581.716
81792338...36333129902.2658
91895250...11211660926.8689
101958042...27538833464.2252
112103721...25026416110.0201
122214858...593439013734.129
132484780...827008137847.344
142875422...16673251066.4255
153045191...28624801795.8552
163351827...9253937792.64593
173474121...32231982684.4869
183500000...168036707875.77
193753142...75581062586.3847
203758571...1057124493617.98
214510327...277235027306.03
224954674...341198516478.001
235445829...46922595108.0112
246653210...1636524223070.76
258754413...84824085671.1678
269850845...92223563486.7973
279960487...447872323030.887
2811138234...1088907530803.22
2911147407...115172058712.546
3015460732...109842365175.189
3115972000...754741572356.983
3217885245...1239601857911.99
3318556698...2721832544138.42
3423508428...34425102017.393
3524184810...331227597302.727
3625054161...591144813591.594
3725248140...125542308783.322
3829310273...1250257451714.37
3929384297...150930213526.882
4029748859...460739190418.327
41265100...3786381407.62268
42603253...96485945529.7125
43758288...39441951914.4718
44920938...19353579665.1023
451221549...6207615256.1115
463360148...176951396583.201
474543126...5040777637.52613
484689021...98630883299.1132
494930258...54052091511.1307
505011102...58217540791.3509
515625118...624537721851.14
525918919...119741273687.089
536025951...130035380109.286
546072475...80064396581.0864
558468555...138010387019.761
569038741...114200451680.527
5710734247...48306262335.7236
5810768477...131854192993.303
5911901484...23469928090.8351
6013805084...377484760106.479
6117789267...814371484921.802
6219196246...111617431346.822
6321529967...237780079437.119
6428571770...1924600282272.59
6531304016...912366830926.293
6639192111...437184378903.089
6739570125...246868436060.57
6840969443...2319361725724.86
6954841552...1218030001843.07
7080594017...356134219989.012
7182021564...1618552483050.3
7283301151...2333671350442.98
7396160163...337108613977.317
74105350020...1137102568658.21
75207353391...8540950345692.85
761281935911...3150427056043.35
77282814...7516451865.1763
78329988...15615897741.1302
79443593...10747411137.2812
80594130...2408816376.51917
81737718...210721444770.898
82865267...21966242368.5798
831218208...7769152795.90834
841251581...44441790595.9614
851972126...19070599769.6852
862051363...14084033590.0507
872823859...63539126862.9918
882990561...12500311840.4183
893068243...1540264832849.55
903856181...50502366239.6244
914292095...57402973305.489
925789122...195599523413.606
936163195...76298870280.0899
946172011...20971345487.6227
957111024...76232737912.4884
967126706...229798138060.241
977531386...486443125709.23
987965055...61222637781.2825
998236303...46063257324.0393
1008299706...23010251034.8654

We can now export our vDataFrame as a GeoPandas DataFrame.

df = world.to_geopandas(geometry = "geometry")

df.head(200)
Out[2]: 
        pop_est      continent                   country                                          geography          area                                           geometry
0         57713  North America                 Greenland  b'\x96\xaa\xa0\x00\x00\x00\x00\x00\x00\x00\x00...  2.189751e+12  POLYGON ((-46.76379 82.62796, -43.40644 83.225...
1        279070        Oceania             New Caledonia  b'\xa9U^\x18\x00\x00\x00\x00\x00\x00\x00\x00\x...  2.328221e+10  POLYGON ((165.77999 -21.08, 166.59999 -21.7000...
2        339747         Europe                   Iceland  b'\x96\xaa\xac\x00\x00\x00\x00\x00\x00\x00\x00...  1.070319e+11  POLYGON ((-14.5087 66.45589, -14.73964 65.8087...
3        360346  North America                    Belize  b'\x96\xaa\xa3\x00\x00\x00\x00\x00\x00\x00\x00...  2.211851e+10  POLYGON ((-89.14308 17.80832, -89.15091 17.955...
4       1291358           Asia               Timor-Leste  b'\xa9U\\\xfc\x00\x00\x00\x00\x00\x00\x00\x00\...  1.477639e+10  POLYGON ((124.96868 -8.89279, 125.08625 -8.656...
..          ...            ...                       ...                                                ...           ...                                                ...
172    68414135           Asia                  Thailand  b'\xbc\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00...  5.119377e+11  POLYGON ((105.21878 14.27321, 104.28142 14.416...
173   104256076           Asia               Philippines  b'\xbc\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00...  2.933923e+11  MULTIPOLYGON (((120.8339 12.7045, 120.32344 13...
174   124574795  North America                    Mexico  b'\x96\xaa\xa0\x00\x00\x00\x00\x00\x00\x00\x00...  1.973907e+12  POLYGON ((-117.12776 32.53534, -115.99135 32.6...
175   326625791  North America  United States of America  b'\x96\xaa\xa0\x00\x00\x00\x00\x00\x00\x00\x00...  9.494301e+12  MULTIPOLYGON (((-122.84 49, -120 49, -117.0312...
176  1379302771           Asia                     China  b'\xbc\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00...  9.408026e+12  MULTIPOLYGON (((109.47521 18.1977, 108.65521 1...

[177 rows x 6 columns]

From there, we can draw any geospatial object.

ax = df.plot(
    edgecolor = "black",
    color = "white",
    figsize = (10, 9),
)


ax.set_title("World Map")
Out[4]: Text(0.5, 1.0, 'World Map')
_images/ug_fs_plot_gpd_3.png
from verticapy.datasets import load_cities

# Loading the cities dataset
cities = load_cities()

import matplotlib.pyplot as plt

# Creating a Matplotlib figure
fig, ax = plt.subplots()

fig.set_size_inches(11, 8)

# Extracting longitude and latitude
cities["lon"] = fun.apply("st_x", cities["geometry"])

cities["lat"] = fun.apply("st_y", cities["geometry"])

# Drawing the data on a Map
ax = cities.scatter(["lon", "lat"], ax = ax)

df.plot(
    edgecolor = "black",
    color = "white",
    ax = ax,
)

Out[13]: <Axes: xlabel='lon', ylabel='lat'>
_images/ug_fs_plot_gpd_4.png

You can also draw maps using the geo_plot() method.

from verticapy.datasets import load_africa_education

# Africa Dataset
africa = load_africa_education()

africa_world = load_world()

africa_world = africa_world[africa_world["continent"] == "Africa"]

ax = africa_world["geometry"].geo_plot(
    color = "white",
    edgecolor = 'black',
)


# displaying schools in Africa
africa.scatter(
    ["lon", "lat"],
    by = "country_long",
    ax = ax,
    max_cardinality = 20,
)

Out[19]: <Axes: xlabel='lon', ylabel='lat'>
_images/ug_fs_plot_gpd_5.png