Note: Before running this notebook, ensure that you have installed SedonaDB:
pip install "apache-sedona[db]"
This notebook shows how to leverage GeoPandas with SedonaDB for large-scale geospatial analysis.
You'll learn how to:
Any file type that can be read by GeoPandas can also be read into a SedonaDB DataFrame!
import sedona.db import geopandas as gpd sd = sedona.db.connect()
gdf = gpd.read_file("sample_geometries.json")
gdf
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
gdf.info()
<class 'geopandas.geodataframe.GeoDataFrame'> RangeIndex: 3 entries, 0 to 2 Data columns (total 3 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 prop0 3 non-null object 1 prop1 2 non-null object 2 geometry 3 non-null geometry dtypes: geometry(1), object(2) memory usage: 204.0+ bytes
df = sd.create_data_frame(gdf)
df.show()
┌────────┬────────────────────┬──────────────────────────────────────────┐
│ prop0 ┆ prop1 ┆ geometry │
│ utf8 ┆ utf8 ┆ geometry │
╞════════╪════════════════════╪══════════════════════════════════════════╡
│ value0 ┆ ┆ POINT(102 0.5) │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ value1 ┆ 0.0 ┆ LINESTRING(102 0,103 1,104 0,105 1) │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ value2 ┆ { "this": "that" } ┆ POLYGON((100 0,101 0,101 1,100 1,100 0)) │
└────────┴────────────────────┴──────────────────────────────────────────┘
This code demonstrates how to read a FlatGeobuf file with GeoPandas and then convert it to a SedonaDB DataFrame.
# Read a FlatGeobuf file with GeoPandas path = "https://raw.githubusercontent.com/geoarrow/geoarrow-data/v0.2.0/natural-earth/files/natural-earth_cities.fgb" gdf = gpd.read_file(path)
# Convert the GeoPandas DataFrame to a SedonaDB DataFrame df = sd.create_data_frame(gdf)
df.show(3)
┌──────────────┬──────────────────────────────┐ │ name ┆ geometry │ │ utf8 ┆ geometry │ ╞══════════════╪══════════════════════════════╡ │ Vatican City ┆ POINT(12.4533865 41.9032822) │ ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ │ San Marino ┆ POINT(12.4417702 43.9360958) │ ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ │ Vaduz ┆ POINT(9.5166695 47.1337238) │ └──────────────┴──────────────────────────────┘