Loading...

verticapy.sql.geo.intersect

verticapy.sql.geo.intersect(vdf: Annotated[str | vDataFrame, ''], index: str, gid: str, g: str | None = None, x: str | None = None, y: str | None = None) → vDataFrame

Spatially intersects a point or points with a set of polygons.

Parameters

vdf: SQLRelation

vDataFrame used to compute the spatial join.

index: str

Name of the index.

gid: str

An integer column or integer that uniquely identifies the spatial object(s) of g or x and y.

g: str, optional

A geometry or geography (WGS84) column that contains points. The g column can contain only point geometries or geographies.

x: str, optional

x-coordinate or longitude.

y: str, optional

y-coordinate or latitude.

Returns

vDataFrame

object containing the result of the intersection.

Examples

For this example, we will use the Cities and World dataset.

import verticapy.datasets as vpd

cities = vpd.load_cities()
world = vpd.load_world()
Abc
city
Varchar(82)
Abc
Long varchar(2411724)
1Abu Dhabi
2Amsterdam
3Apia
4Ashgabat
5Bangui
6Budapest
7Bujumbura
8Castries
9Colombo
10Conakry
11Cotonou
12Dakar
13Dublin
14Georgetown
15Havana
16Kuala Lumpur
17København
18Lilongwe
19Ljubljana
20London
21Monrovia
22Oslo
23Panama City
24Paris
25Phnom Penh
26Podgorica
27Pretoria
28Rangoon
29San Marino
30San Salvador
31Seoul
32Suva
33Tegucigalpa
34Tehran
35Thimphu
36Vatican City
37Warsaw
38Yaounde
39Zagreb
40Andorra
41Athens
42Banjul
43Basseterre
44Belgrade
45Bloemfontein
46Bogota
47Brasilia
48Accra
49Addis Ababa
50Ankara
51Antananarivo
52Asmara
53Astana
54Asuncion
55Bamako
56Bangkok
57Beijing
58Berlin
59Brussels
60Buenos Aires
61Chisinau
62Damascus
63Dar es Salaam
64Dhaka
65Djibouti
66Doha
67Jakarta
68Jerusalem
69Kampala
70Kinshasa
71Kuwait
72La Paz
73Lisbon
74Luxembourg
75Madrid
76Manila
77Maputo
78Mbabane
79Melekeok
80Minsk
81Nairobi
82Naypyidaw
83New Delhi
84Nicosia
85Nouakchott
86Palikir
87Port-au-Prince
88Praia
89Quito
90Rome
91Saint George's
92San Jose
93Sarajevo
94Taipei
95Tbilisi
96Tokyo
97Tripoli
98Valletta
99Vienna
100Vilnius
Rows: 1-100 | Columns: 2
123
pop_est
Integer
Abc
continent
Varchar(32)
Abc
country
Varchar(82)
Abc
Long varchar(2411724)
157713North AmericaGreenland
2279070OceaniaNew Caledonia
3339747EuropeIceland
4360346North AmericaBelize
51291358AsiaTimor-Leste
61467152AfricaeSwatini
71772255AfricaGabon
81792338AfricaGuinea-Bissau
91895250EuropeKosovo
101958042AfricaLesotho
112103721EuropeMacedonia
122214858AfricaBotswana
132484780AfricaNamibia
142875422AsiaKuwait
153045191AsiaArmenia
163351827North AmericaPuerto Rico
173474121EuropeMoldova
183500000AfricaSomaliland
193753142North AmericaPanama
203758571AfricaMauritania
214510327OceaniaNew Zealand
224954674AfricaCongo
235445829EuropeSlovakia
246653210AfricaLibya
258754413EuropeAustria
269850845EuropeHungary
279960487EuropeSweden
2811138234South AmericaBolivia
2911147407North AmericaCuba
3015460732North AmericaGuatemala
3115972000AfricaZambia
3217885245AfricaMali
3318556698AsiaKazakhstan
3423508428AsiaTaiwan
3524184810AfricaCôte d'Ivoire
3625054161AfricaMadagascar
3725248140AsiaNorth Korea
3829310273AfricaAngola
3929384297AsiaNepal
4029748859AsiaUzbekistan
41265100AsiaN. Cyprus
42603253AfricaW. Sahara
43758288AsiaBhutan
44920938OceaniaFiji
451221549AsiaCyprus
463360148South AmericaUruguay
474543126AsiaPalestine
484689021AfricaLiberia
494930258North AmericaCosta Rica
505011102EuropeIreland
515625118AfricaCentral African Rep.
525918919AfricaEritrea
536025951North AmericaNicaragua
546072475AsiaUnited Arab Emirates
558468555AsiaTajikistan
569038741North AmericaHonduras
5710734247North AmericaDominican Rep.
5810768477EuropeGreece
5911901484AfricaRwanda
6013805084AfricaZimbabwe
6117789267South AmericaChile
6219196246AfricaMalawi
6321529967EuropeRomania
6428571770AsiaSaudi Arabia
6531304016South AmericaVenezuela
6639192111AsiaIraq
6739570125AfricaUganda
6840969443AfricaAlgeria
6954841552AfricaSouth Africa
7080594017EuropeGermany
7182021564AsiaIran
7283301151AfricaDem. Rep. Congo
7396160163AsiaVietnam
74105350020AfricaEthiopia
75207353391South AmericaBrazil
761281935911AsiaIndia
77282814OceaniaVanuatu
78329988North AmericaBahamas
79443593AsiaBrunei
80594130EuropeLuxembourg
81737718South AmericaGuyana
82865267AfricaDjibouti
831218208North AmericaTrinidad and Tobago
841251581EuropeEstonia
851972126EuropeSlovenia
862051363AfricaGambia
872823859EuropeLithuania
882990561North AmericaJamaica
893068243AsiaMongolia
903856181EuropeBosnia and Herz.
914292095EuropeCroatia
925789122AsiaKyrgyzstan
936163195AfricaSierra Leone
946172011North AmericaEl Salvador
957111024EuropeSerbia
967126706AsiaLaos
977531386AfricaSomalia
987965055AfricaTogo
998236303EuropeSwitzerland
1008299706AsiaIsrael
Rows: 1-100 | Columns: 4

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 preprocess the datasets by extracting latitude and longitude values and creating an index.

world["id"] = "ROW_NUMBER() OVER(ORDER BY country, pop_est)"
display(world)

cities["id"] = "ROW_NUMBER() OVER (ORDER BY city)"
cities["lat"] = "ST_X(geometry)"
cities["lon"] = "ST_Y(geometry)"
display(cities)
123
pop_est
Int
100%
...
🌎
Geometry(1048576)
100%
123
id
Integer
100%
134124811...1
23047987...2
340969443...3
429310273...4
54050...5
644293293...6
73045191...7
823232413...8
98754413...9
109961396...10
11329988...11
12157826578...12
139549747...13
1411491346...14
15360346...15
1611038805...16
17758288...17
1811138234...18
193856181...19
202214858...20
Abc
city
Varchar(82)
100%
...
🌎
lat
Float(22)
100%
🌎
lon
Float(22)
100%
1Abu Dhabi...54.36659338259224.4666835723799
2Amsterdam...4.9146943174009752.3519145466644
3Apia...-171.738641608603-13.8415450424484
4Ashgabat...58.383299111774637.949994933111
5Bangui...18.55828812528734.36664430634909
6Budapest...19.081374818759747.5019521849914
7Bujumbura...29.3600060615284-3.37608722037464
8Castries...-61.000008180369514.0019734893303
9Colombo...79.85775060925646.93196575818212
10Conakry...-13.68218088612399.53346870502179
11Cotonou...2.51804474056866.40195442278247
12Dakar...-17.475075987050614.7177775836233
13Dublin...-6.2508515403910753.3350069945849
14Georgetown...-58.16702864748066.80197369275203
15Havana...-82.366128029953323.1339046995422
16Kuala Lumpur...101.6980374167463.16861173071237
17København...12.561539888703355.6805100490259
18Lilongwe...33.7833019599835-13.9832950654692
19Ljubljana...14.514969033474146.0552883087945
20London...-0.11866770247593251.5019405883275

Let’s create the geo-index.

from verticapy.sql.geo import create_index

create_index(world, "id", "geometry", "world_polygons", True)
Abc
type
Varchar(20)
...
123
max_y
Float(22)
Abc
info
Varchar(500)
1GEOMETRY...83.64513

Let’s calculate the intersection between the cities and the various countries by using the GEOMETRY data type.

from verticapy.sql.geo import intersect

intersect(cities, "world_polygons", "id", "geometry")
123
point_id
Integer
100%
123
polygon_gid
Integer
100%
1197113
2956
310161
41196
513162
61449
71582
816124
91762
101875
111910
122099
132122
1422156
152329
162458
17140
182165
193116
20461

The same can be done using directly the longitude and latitude.

intersect(cities, "world_polygons", "id", x="lat", y="lon")
123
point_id
Integer
100%
123
polygon_gid
Integer
100%
1140
22165
33116
4461
5551
663
7781
88111
9956
1010161
111196
1213162
131449
141582
1516124
161762
171875
181910
192099
202122

Note

For geospatial functions, Vertica utilizes indexing to expedite computations, especially considering the potentially extensive size of polygons. This is a unique optimization approach employed by Vertica in these scenarios.