Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Extracting building footprints and vectorising the results

You know all three prompt types conceptually. This page runs the complete building-extraction pipeline on Washington State imagery: prompting with real geographic coordinates, exporting masks, and cleaning the result into regularized, GIS-ready polygons.


1. Motivation

This is where every earlier page in this lesson comes together into a deliverable you could actually hand off: not just a segmentation, but a clean vector file ready to load into a GIS. Building footprints are a natural anchor for this, since box and point prompts both work well on their well-defined rectangular shapes.


2. Core idea

The full pipeline has four stages: prompt with geographic coordinates (points or boxes), generate and save georeferenced masks, convert those masks to vector polygons, and regularize the polygon edges into clean, usable shapes.


3. Workflow

A. Point prompts from coordinates

generate_masks_by_points_patch() segments the building at each coordinate and writes the results as one georeferenced GeoTIFF. dtype="uint8" is enough for up to 255 distinct objects; more objects need uint16 (up to 65,535), a limit worth checking against your actual building count before running a large batch.

B. Point prompts from a GeoJSON file

Rather than typing coordinates manually, passing a GeoJSON path directly lets samgeo read every point geometry from the file and process them all in one call, the practical version of step A for a real building-centroid dataset rather than a handful of manually chosen points.

C. Box prompts from a vector file

Just as with point prompts, a vector file of bounding boxes can be passed directly rather than typed out, with samgeo extracting each feature’s geometry as a separate box prompt. multimask_output=False returns one best mask per box rather than several candidates, a reasonable default once you trust your box prompts are already well-targeted.

D. Convert to vector and regularize

raster_to_vector() performs the same vectorization step you have used in earlier lessons, converting the raster mask into one polygon per detected object. regularize() is samgeo’s version of orthogonalization from L07: it adjusts jagged, pixel-aligned raster-derived boundaries into cleaner, more regular building footprints, better representing how buildings actually look than an unregularized raster-to-vector conversion would.


4. Python reactivation

point_coords_batch is a list of two-element lists, the same nested-list pattern from the previous page, here holding longitude-latitude pairs instead of pixel coordinates. Passing a file path (geojson_path) in place of that list, as in step B, works because samgeo checks the input type and reads coordinates from the file automatically, a convenience pattern you have seen elsewhere in this course where a function accepts either an in-memory object or a path to load one from.


5. Common pitfalls


6. Mini task

You have a GeoJSON file with 800 building centroids for a large study area. Which dtype would you choose for the output mask, and why does this matter more here than it did for the four-point example in step A?

Sample solution

dtype="uint16" is the right choice, since it supports up to 65,535 distinct object IDs, comfortably covering 800 buildings, while uint8’s 255-object limit would be exceeded partway through the batch. This mattered less for the four-point example in step A, where uint8 was more than sufficient, but choosing a dtype without checking your actual object count against its limit is a mistake that only shows up once you run a genuinely large batch.


7. Key takeaways