In [1]:
Copied!
# %pip install geoai-py
# %pip install geoai-py
Import package¶
In [2]:
Copied!
import geoai
import geoai
Download sample data¶
In [3]:
Copied!
naip_url = (
"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/naip_train.tif"
)
masks_url = "https://huggingface.co/datasets/giswqs/geospatial/resolve/main/naip_building_masks.tif"
naip_url = (
"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/naip_train.tif"
)
masks_url = "https://huggingface.co/datasets/giswqs/geospatial/resolve/main/naip_building_masks.tif"
In [4]:
Copied!
masks_path = geoai.download_file(masks_url)
naip_path = geoai.download_file(naip_url)
masks_path = geoai.download_file(masks_url)
naip_path = geoai.download_file(naip_url)
naip_building_masks.tif: 0%| | 0.00/61.6k [00:00<?, ?B/s]
naip_building_masks.tif: 100%|██████████| 61.6k/61.6k [00:00<00:00, 4.38MB/s]
naip_train.tif: 0%| | 0.00/12.1M [00:00<?, ?B/s]
naip_train.tif: 52%|█████▏ | 6.25M/12.1M [00:00<00:00, 65.5MB/s]
naip_train.tif: 100%|██████████| 12.1M/12.1M [00:00<00:00, 81.2MB/s]
In [5]:
Copied!
geoai.view_image(masks_path, figsize=(18, 10))
geoai.view_image(masks_path, figsize=(18, 10))
In [6]:
Copied!
geoai.create_split_map(
left_layer=masks_url,
right_layer=naip_url,
left_label="Building Masks",
right_label="NAIP Imagery",
left_args={
"colormap": {"255": "#0000ff"},
"opacity": 0.5,
},
basemap=naip_url,
)
geoai.create_split_map(
left_layer=masks_url,
right_layer=naip_url,
left_label="Building Masks",
right_label="NAIP Imagery",
left_args={
"colormap": {"255": "#0000ff"},
"opacity": 0.5,
},
basemap=naip_url,
)
Out[6]:
Convert raster to vector¶
In [7]:
Copied!
gdf = geoai.raster_to_vector(masks_path, output_path="naip_building_masks.geojson")
gdf = geoai.raster_to_vector(masks_path, output_path="naip_building_masks.geojson")
Vectorized data saved to naip_building_masks.geojson
In [8]:
Copied!
geoai.view_vector_interactive(
gdf, style_kwds={"color": "blue", "fillOpacity": 0.4}, tiles=naip_url
)
geoai.view_vector_interactive(
gdf, style_kwds={"color": "blue", "fillOpacity": 0.4}, tiles=naip_url
)
Out[8]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [9]:
Copied!
geoai.create_split_map(
left_layer=gdf,
right_layer=naip_url,
left_label="Buildings",
right_label="NAIP Imagery",
left_args={"style": {"color": "blue"}, "fillOpacity": 0.4},
basemap=naip_url,
)
geoai.create_split_map(
left_layer=gdf,
right_layer=naip_url,
left_label="Buildings",
right_label="NAIP Imagery",
left_args={"style": {"color": "blue"}, "fillOpacity": 0.4},
basemap=naip_url,
)
Out[9]:
Building regularization¶
In [10]:
Copied!
gdf_regularized = geoai.regularization(
building_polygons=gdf,
angle_tolerance=10,
simplify_tolerance=0.5,
orthogonalize=True,
preserve_topology=True,
)
gdf_regularized = geoai.regularization(
building_polygons=gdf,
angle_tolerance=10,
simplify_tolerance=0.5,
orthogonalize=True,
preserve_topology=True,
)
In [11]:
Copied!
geoai.view_vector_interactive(
gdf_regularized, style_kwds={"color": "red", "fillOpacity": 0.4}, tiles=naip_url
)
geoai.view_vector_interactive(
gdf_regularized, style_kwds={"color": "red", "fillOpacity": 0.4}, tiles=naip_url
)
Out[11]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [12]:
Copied!
geoai.create_split_map(
left_layer=gdf_regularized,
right_layer=naip_url,
left_label="Regularized Buildings",
right_label="NAIP Imagery",
left_args={"style": {"color": "red"}, "fillOpacity": 0.4},
basemap=naip_url,
)
geoai.create_split_map(
left_layer=gdf_regularized,
right_layer=naip_url,
left_label="Regularized Buildings",
right_label="NAIP Imagery",
left_args={"style": {"color": "red"}, "fillOpacity": 0.4},
basemap=naip_url,
)
Out[12]:
Hybrid regularization¶
In [13]:
Copied!
gdf_hybrid = geoai.hybrid_regularization(gdf)
gdf_hybrid = geoai.hybrid_regularization(gdf)
In [14]:
Copied!
geoai.view_vector_interactive(
gdf_hybrid, style_kwds={"color": "green", "fillOpacity": 0.4}, tiles=naip_url
)
geoai.view_vector_interactive(
gdf_hybrid, style_kwds={"color": "green", "fillOpacity": 0.4}, tiles=naip_url
)
Out[14]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [15]:
Copied!
geoai.create_split_map(
left_layer=gdf_regularized,
right_layer=naip_url,
left_label="Regularized Buildings",
right_label="NAIP Imagery",
left_args={"style": {"color": "green", "fillOpacity": 0.4}},
basemap=naip_url,
)
geoai.create_split_map(
left_layer=gdf_regularized,
right_layer=naip_url,
left_label="Regularized Buildings",
right_label="NAIP Imagery",
left_args={"style": {"color": "green", "fillOpacity": 0.4}},
basemap=naip_url,
)
Out[15]:
Adaptive regularization¶
In [16]:
Copied!
gdf_adaptive = geoai.adaptive_regularization(
building_polygons=gdf,
simplify_tolerance=0.5,
area_threshold=0.9,
preserve_shape=True,
)
gdf_adaptive = geoai.adaptive_regularization(
building_polygons=gdf,
simplify_tolerance=0.5,
area_threshold=0.9,
preserve_shape=True,
)
In [17]:
Copied!
geoai.view_vector_interactive(
gdf_adaptive, style_kwds={"color": "yellow", "fillOpacity": 0.4}, tiles=naip_url
)
geoai.view_vector_interactive(
gdf_adaptive, style_kwds={"color": "yellow", "fillOpacity": 0.4}, tiles=naip_url
)
Out[17]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [18]:
Copied!
geoai.create_split_map(
left_layer=gdf_adaptive,
right_layer=naip_url,
left_label="Adaptive Regularization Buildings",
right_label="NAIP Imagery",
left_args={"style": {"color": "yellow", "fillOpacity": 0.4}},
basemap=naip_url,
)
geoai.create_split_map(
left_layer=gdf_adaptive,
right_layer=naip_url,
left_label="Adaptive Regularization Buildings",
right_label="NAIP Imagery",
left_args={"style": {"color": "yellow", "fillOpacity": 0.4}},
basemap=naip_url,
)
Out[18]:
Compare regularization methods¶
In [19]:
Copied!
import leafmap.foliumap as leafmap
import leafmap.foliumap as leafmap
In [20]:
Copied!
m = leafmap.Map()
m.add_basemap("SATELLITE")
m.add_gdf(gdf, layer_name="Original")
m.add_gdf(
gdf_regularized, style={"color": "red", "fillOpacity": 0}, layer_name="Regularized"
)
m.add_gdf(gdf_hybrid, style={"color": "green", "fillOpacity": 0}, layer_name="Hybrid")
m.add_gdf(
gdf_adaptive, style={"color": "yellow", "fillOpacity": 0}, layer_name="Adaptive"
)
legend = {
"Original": "blue",
"Regularized": "red",
"Hybrid": "green",
"Adaptive": "yellow",
}
m.add_legend(title="Building Footprints", legend_dict=legend)
m
m = leafmap.Map()
m.add_basemap("SATELLITE")
m.add_gdf(gdf, layer_name="Original")
m.add_gdf(
gdf_regularized, style={"color": "red", "fillOpacity": 0}, layer_name="Regularized"
)
m.add_gdf(gdf_hybrid, style={"color": "green", "fillOpacity": 0}, layer_name="Hybrid")
m.add_gdf(
gdf_adaptive, style={"color": "yellow", "fillOpacity": 0}, layer_name="Adaptive"
)
legend = {
"Original": "blue",
"Regularized": "red",
"Hybrid": "green",
"Adaptive": "yellow",
}
m.add_legend(title="Building Footprints", legend_dict=legend)
m
Out[20]: