Geometric Properties¶
This notebook demonstrates how to calculate geometric properties of objects in a vector dataset and filter out unwanted objects based on these properties.
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 package¶
In [2]:
Copied!
import geoai
import geoai
Load data¶
In [3]:
Copied!
vector_url = "https://huggingface.co/datasets/giswqs/geospatial/resolve/main/naip_buildings_masks.geojson"
raster_url = (
"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/naip_train.tif"
)
vector_url = "https://huggingface.co/datasets/giswqs/geospatial/resolve/main/naip_buildings_masks.geojson"
raster_url = (
"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/naip_train.tif"
)
In [4]:
Copied!
gdf = geoai.read_vector(vector_url)
gdf = geoai.read_vector(vector_url)
In [5]:
Copied!
gdf.head()
gdf.head()
Out[5]:
confidence | class | geometry | |
---|---|---|---|
0 | 0.994213 | 1 | POLYGON ((455181.6 5277618.6, 455177.4 5277614... |
1 | 0.992599 | 1 | POLYGON ((454990.2 5277628.2, 454990.2 5277627... |
2 | 0.989381 | 1 | POLYGON ((454855.8 5277628.2, 454855.8 5277626... |
3 | 0.986561 | 1 | POLYGON ((454981.8 5277798, 454981.2 5277797.4... |
4 | 0.982763 | 1 | POLYGON ((455052 5277904.8, 455050.8 5277903.6... |
Visualize data¶
In [6]:
Copied!
geoai.view_vector_interactive(gdf, column="confidence", tiles=raster_url)
geoai.view_vector_interactive(gdf, column="confidence", tiles=raster_url)
Out[6]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add geometric properties¶
In [7]:
Copied!
gdf_props = geoai.add_geometric_properties(gdf, area_unit="m2", length_unit="m")
gdf_props = geoai.add_geometric_properties(gdf, area_unit="m2", length_unit="m")
In [8]:
Copied!
gdf_props.head()
gdf_props.head()
Out[8]:
confidence | class | geometry | area_m2 | length_m | perimeter_m | area_bbox_m2 | area_convex_m2 | area_filled_m2 | major_length_m | minor_length_m | eccentricity | orientation | elongation | extent | solidity | complexity | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0.994213 | 1 | POLYGON ((455181.6 5277618.6, 455177.4 5277614... | 184.50 | 53.407316 | 53.407316 | 241.92 | 189.18 | 184.50 | 19.2 | 12.6 | 0.754544 | 0.0 | 1.523810 | 0.762649 | 0.975262 | 1.109169 |
1 | 0.992599 | 1 | POLYGON ((454990.2 5277628.2, 454990.2 5277627... | 527.22 | 92.836753 | 92.836753 | 546.84 | 533.52 | 527.22 | 29.4 | 18.6 | 0.774435 | 90.0 | 1.580645 | 0.964121 | 0.988192 | 1.140562 |
2 | 0.989381 | 1 | POLYGON ((454855.8 5277628.2, 454855.8 5277626... | 599.40 | 97.988225 | 97.988225 | 617.40 | 604.44 | 599.40 | 29.4 | 21.0 | 0.699854 | 90.0 | 1.400000 | 0.970845 | 0.991662 | 1.129043 |
3 | 0.986561 | 1 | POLYGON ((454981.8 5277798, 454981.2 5277797.4... | 741.96 | 129.976450 | 129.976450 | 866.52 | 786.24 | 741.96 | 49.8 | 17.4 | 0.936975 | 0.0 | 2.862069 | 0.856253 | 0.943681 | 1.346076 |
4 | 0.982763 | 1 | POLYGON ((455052 5277904.8, 455050.8 5277903.6... | 748.98 | 130.122035 | 130.122035 | 876.96 | 795.78 | 748.98 | 50.4 | 17.4 | 0.938515 | 90.0 | 2.896552 | 0.854064 | 0.941190 | 1.341253 |
Visualize geometric properties¶
In [9]:
Copied!
geoai.view_vector_interactive(gdf_props, column="area_m2", tiles=raster_url)
geoai.view_vector_interactive(gdf_props, column="area_m2", tiles=raster_url)
Out[9]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [10]:
Copied!
geoai.view_vector_interactive(gdf_props, column="elongation", tiles=raster_url)
geoai.view_vector_interactive(gdf_props, column="elongation", tiles=raster_url)
Out[10]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Filter objects based on geometric properties¶
In [11]:
Copied!
gdf_filtered = gdf_props[(gdf_props["area_m2"] < 2000) & (gdf_props["elongation"] < 5)]
gdf_filtered = gdf_props[(gdf_props["area_m2"] < 2000) & (gdf_props["elongation"] < 5)]
Visualize filtered objects¶
In [12]:
Copied!
geoai.view_vector_interactive(gdf_filtered, column="elongation", tiles=raster_url)
geoai.view_vector_interactive(gdf_filtered, column="elongation", tiles=raster_url)
Out[12]:
Make this Notebook Trusted to load map: File -> Trust Notebook