Skip to content

Commit a0ee47b

Browse files
committed
Created using Colab
1 parent d711f65 commit a0ee47b

1 file changed

Lines changed: 247 additions & 0 deletions

File tree

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"id": "kTSAeTDH-vc0"
7+
},
8+
"source": [
9+
"# Building Footprint Extraction for Africa\n",
10+
"\n",
11+
"[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/geoai/blob/main/docs/examples/building_footprints_africa.ipynb)\n",
12+
"\n",
13+
"## Install package\n",
14+
"\n",
15+
"To use the `geoai-py` package, ensure it is installed in your environment. Uncomment the command below if needed."
16+
]
17+
},
18+
{
19+
"cell_type": "code",
20+
"source": [
21+
"from google.colab import drive\n",
22+
"drive.mount('/content/drive')"
23+
],
24+
"metadata": {
25+
"id": "kqLaOExU-1yU"
26+
},
27+
"execution_count": null,
28+
"outputs": []
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": null,
33+
"metadata": {
34+
"id": "NVGHK7TR-vc2"
35+
},
36+
"outputs": [],
37+
"source": [
38+
"# %pip install geoai-py"
39+
]
40+
},
41+
{
42+
"cell_type": "markdown",
43+
"metadata": {
44+
"id": "N1ePOZwo-vc4"
45+
},
46+
"source": [
47+
"## Import libraries"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": null,
53+
"metadata": {
54+
"id": "pFiOKWJi-vc5"
55+
},
56+
"outputs": [],
57+
"source": [
58+
"import geoai"
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {
64+
"id": "ehQxMXyb-vc6"
65+
},
66+
"source": [
67+
"## Download sample data"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": null,
73+
"metadata": {
74+
"id": "SRDRFzjl-vc7"
75+
},
76+
"outputs": [],
77+
"source": [
78+
"raster_url = \"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/buildings_africa.tif\""
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": null,
84+
"metadata": {
85+
"id": "EvkEbTqN-vc_"
86+
},
87+
"outputs": [],
88+
"source": [
89+
"raster_path = geoai.download_file(raster_url)"
90+
]
91+
},
92+
{
93+
"cell_type": "markdown",
94+
"metadata": {
95+
"id": "o9xSPTuI-vc_"
96+
},
97+
"source": [
98+
"## Initialize the model"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": null,
104+
"metadata": {
105+
"id": "0PF5gXpN-vdA"
106+
},
107+
"outputs": [],
108+
"source": [
109+
"extractor = geoai.BuildingFootprintExtractor(model_path=\"building_footprints_usa.pth\")"
110+
]
111+
},
112+
{
113+
"cell_type": "markdown",
114+
"metadata": {
115+
"id": "oBJCKHgj-vdA"
116+
},
117+
"source": [
118+
"## Extract building footprints"
119+
]
120+
},
121+
{
122+
"cell_type": "code",
123+
"execution_count": null,
124+
"metadata": {
125+
"id": "vz6u753Y-vdB"
126+
},
127+
"outputs": [],
128+
"source": [
129+
"masks_path = extractor.generate_masks(\n",
130+
" raster_path,\n",
131+
" output_dir=\"building_masks.tif\",\n",
132+
" min_object_area=1000,\n",
133+
" confidence_threshold=0.5,\n",
134+
" threshold=0.5,\n",
135+
")"
136+
]
137+
},
138+
{
139+
"cell_type": "code",
140+
"execution_count": null,
141+
"metadata": {
142+
"id": "c2QcZPgo-vdB"
143+
},
144+
"outputs": [],
145+
"source": [
146+
"geoai.view_raster(masks_path, opacity=0.7, colormap=\"tab20\", basemap=raster_url)"
147+
]
148+
},
149+
{
150+
"cell_type": "markdown",
151+
"metadata": {
152+
"id": "WIuPFC4q-vdB"
153+
},
154+
"source": [
155+
"## Vectorize masks"
156+
]
157+
},
158+
{
159+
"cell_type": "code",
160+
"execution_count": null,
161+
"metadata": {
162+
"id": "LKe5g2ou-vdC"
163+
},
164+
"outputs": [],
165+
"source": [
166+
"gdf = geoai.orthogonalize(\n",
167+
" input_path=masks_path, output_path=\"building_footprints.geojson\", epsilon=1.0\n",
168+
")"
169+
]
170+
},
171+
{
172+
"cell_type": "markdown",
173+
"metadata": {
174+
"id": "5fde0oT2-vdC"
175+
},
176+
"source": [
177+
"## Add geometric attributes"
178+
]
179+
},
180+
{
181+
"cell_type": "code",
182+
"execution_count": null,
183+
"metadata": {
184+
"id": "T3i6tBjO-vdC"
185+
},
186+
"outputs": [],
187+
"source": [
188+
"gdf = geoai.add_geometric_properties(gdf)"
189+
]
190+
},
191+
{
192+
"cell_type": "markdown",
193+
"metadata": {
194+
"id": "qwo5iPwB-vdC"
195+
},
196+
"source": [
197+
"## Visualize results"
198+
]
199+
},
200+
{
201+
"cell_type": "code",
202+
"execution_count": null,
203+
"metadata": {
204+
"id": "GahTxmTn-vdD"
205+
},
206+
"outputs": [],
207+
"source": [
208+
"geoai.view_vector_interactive(\n",
209+
" gdf, style_kwds={\"color\": \"red\", \"fillOpacity\": 0.2}, tiles=raster_url\n",
210+
")"
211+
]
212+
},
213+
{
214+
"cell_type": "markdown",
215+
"metadata": {
216+
"id": "orojwF0k-vdD"
217+
},
218+
"source": [
219+
"![image](https://github.com/user-attachments/assets/8c651e7d-b2c3-4bb4-871e-4317cd1444a1)"
220+
]
221+
}
222+
],
223+
"metadata": {
224+
"kernelspec": {
225+
"display_name": "geo",
226+
"language": "python",
227+
"name": "python3"
228+
},
229+
"language_info": {
230+
"codemirror_mode": {
231+
"name": "ipython",
232+
"version": 3
233+
},
234+
"file_extension": ".py",
235+
"mimetype": "text/x-python",
236+
"name": "python",
237+
"nbconvert_exporter": "python",
238+
"pygments_lexer": "ipython3",
239+
"version": "3.12.9"
240+
},
241+
"colab": {
242+
"provenance": []
243+
}
244+
},
245+
"nbformat": 4,
246+
"nbformat_minor": 0
247+
}

0 commit comments

Comments
 (0)