Regularization¶
This example demonstrates how to regularize building footprints using the regularize
function from the geoai
package. The function is a wrapper around the regularize_geodataframe
function from the buildingregulariser package. Credits to the original author, Nick Wright.
It can be used to regularize features such as building footprints, solar panels, cars, and other objects that have a regular shape.
Install package¶
To use the geoai-py
package, ensure it is installed in your environment. Uncomment the command below if needed.
In [1]:
Copied!
# %pip install geoai-py
# %pip install geoai-py
Import libraries¶
In [2]:
Copied!
import geoai
import geoai
Use sample data¶
In [3]:
Copied!
raster_url = (
"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/naip_train.tif"
)
vector_url = "https://huggingface.co/datasets/giswqs/geospatial/resolve/main/buildings_original.geojson"
raster_url = (
"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/naip_train.tif"
)
vector_url = "https://huggingface.co/datasets/giswqs/geospatial/resolve/main/buildings_original.geojson"
Visualize data¶
In [4]:
Copied!
geoai.view_vector_interactive(vector_url, tiles=raster_url)
geoai.view_vector_interactive(vector_url, tiles=raster_url)
Out[4]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Regularize buildings¶
In [5]:
Copied!
gdf = geoai.regularize(
data=vector_url,
simplify_tolerance=2.0,
allow_45_degree=True,
diagonal_threshold_reduction=30,
allow_circles=True,
circle_threshold=0.9,
)
gdf = geoai.regularize(
data=vector_url,
simplify_tolerance=2.0,
allow_45_degree=True,
diagonal_threshold_reduction=30,
allow_circles=True,
circle_threshold=0.9,
)
In [6]:
Copied!
geoai.view_vector_interactive(gdf, tiles=raster_url)
geoai.view_vector_interactive(gdf, tiles=raster_url)
Out[6]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [7]:
Copied!
geoai.create_split_map(
left_layer=gdf,
right_layer=raster_url,
left_label="Regularized Buildings",
right_label="NAIP Imagery",
left_args={"style": {"color": "red", "fillOpacity": 0.3}},
basemap=raster_url,
)
geoai.create_split_map(
left_layer=gdf,
right_layer=raster_url,
left_label="Regularized Buildings",
right_label="NAIP Imagery",
left_args={"style": {"color": "red", "fillOpacity": 0.3}},
basemap=raster_url,
)
Out[7]:
Compare results¶
In [8]:
Copied!
import leafmap.foliumap as leafmap
import leafmap.foliumap as leafmap
In [9]:
Copied!
m = leafmap.Map()
m.add_cog_layer(raster_url, name="NAIP")
m.add_vector(
vector_url, style={"color": "yellow", "fillOpacity": 0}, layer_name="Original"
)
m.add_gdf(gdf, style={"color": "red", "fillOpacity": 0}, layer_name="Regularized")
legend = {
"Original": "yellow",
"Regularized": "red",
}
m.add_legend(title="Building Footprints", legend_dict=legend)
m
m = leafmap.Map()
m.add_cog_layer(raster_url, name="NAIP")
m.add_vector(
vector_url, style={"color": "yellow", "fillOpacity": 0}, layer_name="Original"
)
m.add_gdf(gdf, style={"color": "red", "fillOpacity": 0}, layer_name="Regularized")
legend = {
"Original": "yellow",
"Regularized": "red",
}
m.add_legend(title="Building Footprints", legend_dict=legend)
m
Out[9]: