Loading...

verticapy.sql.geo.create_index

verticapy.sql.geo.create_index(vdf: Annotated[str | vDataFrame, ''], gid: str, g: str, index: str, overwrite: bool = False, max_mem_mb: int = 256, skip_nonindexable_polygons: bool = False) → TableSample

Creates a spatial index on a set of polygons to speed up spatial intersection with a set of points.

Parameters

vdf: SQLRelation

vDataFrame used to compute the spatial join.

gid: str

Name of an integer column that uniquely identifies the polygon. The gid cannot be NULL.

g: str

Name of a geometry or geography (WGS84) column or expression that contains polygons and multipolygons. Only polygon and multipolygon can be indexed. Other shape types are excluded from the index.

index: str

Name of the index.

overwrite: bool, optional

BOOLEAN value that specifies whether to overwrite the index, if an index exists.

max_mem_mb: int, optional

A positive integer that assigns a limit to the amount of memory in megabytes that create_index can allocate during index construction.

skip_nonindexable_polygons: bool, optional

In rare cases, intricate polygons (for instance, those with too high resolution or anomalous spikes) cannot be indexed. These polygons are considered non-indexable. When set to False, non-indexable polygons cause the index creation to fail. When set to True, index creation can succeed by excluding non-indexable polygons from the index.

Returns

TableSample

geospatial indexes.

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%
1Accra...-0.2186615989606945.55198046444593
2Addis Ababa...38.69805857534879.03525622129575
3Ankara...32.862445782356639.9291844440755
4Antananarivo...47.5146780415299-18.9146914920322
5Asmara...38.933323525759315.3333392526819
6Astana...71.42777420948351.1811253042576
7Asuncion...-57.6434510279013-25.2944571170577
8Bamako...-8.001984963249712.6519605263233
9Bangkok...100.51469879369513.751945064088
10Beijing...116.38633982565939.9308380899091
11Berlin...13.399602764700552.5237645222512
12Brussels...4.3313707496904550.8352629353303
13Buenos Aires...-58.3994772323314-34.6005557499074
14Chisinau...28.857711139651447.0050236196706
15Damascus...36.298050030417133.5019798542061
16Dar es Salaam...39.2663959776946-6.79806673612438
17Dhaka...90.406633608107523.7250055703128
18Djibouti...43.148001667052311.5950144642555
19Doha...51.532967894299325.2865560089066
20Jakarta...106.82749176247-6.17247184679889

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%
120236
2140
32165
43116
5461
6551
763
8781
98111
10956
1110161
121196
1313162
141449
151582
1616124
172632
182789
1928137
202915

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.

See also

describe_index() : Describes the geo index.
intersect() : Spatially intersects a point or points with a set of polygons.
rename_index() : Renames the geo index.