View Metadata¶
Install Package
To use the geoai-py
package, ensure it is installed in your environment. Uncomment the command below if needed.
# %pip install geoai-py
Import the package
import geoai
Define URLs for sample datasets: a NAIP imagery raster file and a building footprints vector file
raster_url = (
"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/naip_train.tif"
)
vector_url = "https://huggingface.co/datasets/giswqs/geospatial/resolve/main/naip_train_buildings.geojson"
Download the raster file (NAIP imagery) and save it locally
raster_path = geoai.download_file(raster_url)
File already exists: naip_train.tif
Download the vector file (building footprints) and save it locally
vector_path = geoai.download_file(vector_url)
File already exists: naip_train_buildings.geojson
Display metadata about the raster file, including dimensions, resolution, projection, and bands
geoai.get_raster_info(raster_path)["band_stats"]
[{'band': 1, 'min': 12.0, 'max': 251.0, 'mean': 150.6730747259594, 'std': 48.01908734374099}, {'band': 2, 'min': 49.0, 'max': 251.0, 'mean': 141.92468895229808, 'std': 43.46595463573497}, {'band': 3, 'min': 53.0, 'max': 251.0, 'mean': 120.89909373405554, 'std': 41.78086244480775}, {'band': 4, 'min': 22.0, 'max': 251.0, 'mean': 159.68995855062735, 'std': 54.95588423977727}]
geoai.print_raster_info(raster_path, figsize=(18, 10))
===== RASTER INFORMATION: naip_train.tif ===== Driver: GTiff Dimensions: 2503 x 1126 pixels Number of bands: 4 Data type: uint8 Coordinate Reference System: EPSG:26911 Georeferenced Bounds: BoundingBox(left=454780.8, bottom=5277567.0, right=456282.6, top=5278242.6) Pixel Resolution: 0.5999999999999953, 0.5999999999996691 NoData Value: None ----- Band Statistics ----- Band 1: Min: 12.00 Max: 251.00 Mean: 150.67 Std Dev: 48.02 Band 2: Min: 49.00 Max: 251.00 Mean: 141.92 Std Dev: 43.47 Band 3: Min: 53.00 Max: 251.00 Mean: 120.90 Std Dev: 41.78 Band 4: Min: 22.00 Max: 251.00 Mean: 159.69 Std Dev: 54.96
Display metadata about the vector file, including geometry type, feature count, extent, and attributes
geoai.print_vector_info(vector_path, figsize=(18, 10))
===== VECTOR INFORMATION: naip_train_buildings.geojson ===== Driver: GEOJSON Feature count: 735 Geometry types: {'Polygon': 735} Coordinate Reference System: EPSG:4326 Bounds: [-117.6017984, 47.65016239407519, -117.58246913360121, 47.655846] Number of attributes: 3 Attribute names: id, height, class ----- Attribute Statistics ----- Attribute: id min: 1 max: 735 mean: 368.0000 std: 212.3205 null_count: 0 Attribute: height min: 0 max: 0 mean: 0.0000 std: 0.0000 null_count: 0
Analyze the "height" attribute of buildings to obtain statistical information
geoai.analyze_vector_attributes(vector_path, "height")
{'attribute': 'height', 'type': 'numeric', 'count': np.int64(735), 'null_count': np.int64(0), 'min': np.int32(0), 'max': np.int32(0), 'mean': np.float64(0.0), 'median': np.float64(0.0), 'std': np.float64(0.0), 'unique_values': 1}
Create a visualization of building footprints colored by their height values
geoai.visualize_vector_by_attribute(vector_path, "height")
Clip the raster file to a specified extent
clip_raster_path = "naip_clip.tif"
geoai.clip_raster_by_bbox(
raster_path,
clip_raster_path,
bbox=(0, 0, 500, 500),
bands=[1, 2, 3],
bbox_type="pixel",
)
'naip_clip.tif'
geoai.view_image(clip_raster_path)