Skip to content
Ahmad Ammar

4. Vector data

4.1 Places, buffers and statistics

Vector data describe features as points, lines or polygons with attributes. Here we create a few points of interest with geopandas, project them to the image's CRS, draw a 300 m circle (a buffer) around each, and average the indices inside each circle: a zonal statistic.

In [6]
places = gpd.GeoDataFrame({"place": ["City Hall", "Belfast Lough", "Cave Hill", "Botanic Gardens", "Harbour estate"]},
                          geometry=[Point(-5.9301, 54.5965), Point(-5.885, 54.630), Point(-5.948, 54.640), Point(-5.932, 54.582), Point(-5.895, 54.615)],
                          crs="EPSG:4326").to_crs(crs)
places["geometry"] = places.buffer(300)
rows, cols = np.indices(shape)
xs, ys = rasterio.transform.xy(transform, rows.ravel(), cols.ravel())
pts = np.c_[xs, ys]
out = []
for _, p in places.iterrows():
    minx, miny, maxx, maxy = p.geometry.bounds
    inside = (pts[:, 0] >= minx) & (pts[:, 0] <= maxx) & (pts[:, 1] >= miny) & (pts[:, 1] <= maxy)
    inside &= np.hypot(pts[:, 0] - p.geometry.centroid.x, pts[:, 1] - p.geometry.centroid.y) <= 300
    out.append({"place": p.place, "pixels": int(inside.sum()), "NDVI": np.nanmean(ndvi.ravel()[inside]),
                "NDWI": np.nanmean(ndwi.ravel()[inside]), "NDBI": np.nanmean(ndbi.ravel()[inside])})
zs = pd.DataFrame(out).round(2)
print(zs.to_string(index=False))
print("\nmost vegetated:", zs.loc[zs.NDVI.idxmax(), "place"], "| wettest:", zs.loc[zs.NDWI.idxmax(), "place"], "| most built-up:", zs.loc[zs.NDBI.idxmax(), "place"])
Out [6]
place  pixels  NDVI  NDWI  NDBI
      City Hall    2827  0.15 -0.18  0.08
  Belfast Lough    2828 -0.26  0.42 -0.03
      Cave Hill    2823  0.92 -0.84 -0.45
Botanic Gardens    2825  0.50 -0.48 -0.11
 Harbour estate    2833  0.08  0.01  0.05

most vegetated: Cave Hill | wettest: Belfast Lough | most built-up: City Hall

How to read the table. One row per place; "pixels" counts the 10 m pixels inside its 300 m circle (about 2,800); the other columns are the mean index values there. Read down each column to rank the places. The line under the table names the extremes, which should match what you know of each place: open water in the Lough, woodland on Cave Hill, streets and roofs around City Hall and the harbour.