Skip to main content

A CLI tool and Python library for extracting vector features from geospatial raster (TIF) files using the Segment Anything Model (SAM), and exporting them as GeoJSON.

Project description

Raster Feature Extractor

A CLI tool and Python library for extracting vector features from geospatial raster (TIF) files using the Segment Anything Model (SAM), and exporting them as GeoJSON.

Installation

pip install orthomasker

Usage

# Using CLI
orthomasker your_input_filename.tif your_output_filename.geojson

# Using Python
from orthomasker.feature_extractor import RasterFeatureExtractor

# Provide your own test TIF file (upload or use a sample)
input_tif = "your_input_filename.tif"
output_geojson = "your_output_filename.geojson"

# Set up the extractor (use the path to your .pth file)
extractor = RasterFeatureExtractor()

extractor.convert(input_tif, output_geojson)

Options

  • --sam-checkpoint: Path to SAM model weights (default: sam_vit_h_4b8939.pth)

  • --model-type: SAM model type (vit_h, vit_l, vit_b; default: vit_h)

  • --confidence-threshold: Minimum stability score to keep a mask (0–100; default: 0, no filter)

  • --tile-size: Tile size for processing (default: 1024)

  • --overlap: Tile overlap in pixels (default: 128)

  • --class-name: Class label for output features (default: sam_object)

  • --min-area: Minimum area (in square units of TIF CRS) for output features (optional)

  • --max-area: Maximum area (in square units of TIF CRS) for output features (optional)

  • --compactness: Minimum compactness threshold (0.0–1.0) using Polsby-Popper metric for filtering irregular shapes (optional)

  • --fixed-bounds: Bounding box (minx, miny, maxx, maxy) in image CRS

  • --merge: Merge overlapping polygons in output (optional)

  • --verbose: Enable verbose output

Compactness Filtering

The --compactness option allows you to filter out irregular or elongated shapes by setting a minimum compactness threshold. This uses the Polsby-Popper compactness metric:
Compactness = (4π × Area) / (Perimeter²)

  • Perfect circle: compactness = 1.0
  • Square: compactness ≈ 0.785
  • Elongated shapes: compactness approaches 0.0

Common threshold values:

  • 0.1: Very permissive (removes only extremely irregular shapes)
  • 0.3: Moderate filtering (removes highly irregular shapes)
  • 0.6: Strict filtering (keeps only relatively compact shapes)
  • 0.8: Very strict (keeps only very round/square shapes)

Development

Setup

git clone https://github.com/nickmccarty/orthomasker.git
cd orthomasker
pip install -r requirements.txt
pip install -e ".[ml,dev]"

Acknowledgments

This project leverages Meta AI’s Segment Anything Model (SAM) for automatic mask generation, which is faciliated by utilizing segment-anything-py as a dependency; many thanks to Qiusheng Wu, et al. for their work!

Citations

@article{kirillov2023segany,
title={Segment Anything},
author={Kirillov, Alexander and Mintun, Eric and Ravi, Nikhila and Mao, Hanzi and Rolland, Chloe and Gustafson, Laura and Xiao, Tete and Whitehead, Spencer and Berg, Alexander C. and Lo, Wan-Yen and Doll{'a}r, Piotr and Girshick, Ross},
journal={arXiv:2304.02643},
year={2023}
}

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

orthomasker-0.9.2.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

orthomasker-0.9.2-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file orthomasker-0.9.2.tar.gz.

File metadata

  • Download URL: orthomasker-0.9.2.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for orthomasker-0.9.2.tar.gz
Algorithm Hash digest
SHA256 b63a4cf552bd69c36fea0b31b010e5651a29ab929d2b80736bd3acbf12f6d038
MD5 951c31d0f48a9b9f27facd80f43d6470
BLAKE2b-256 b82d0725ea917d3fd52d29559883efa3d3209b12cc94255238c9b4a86337ed18

See more details on using hashes here.

File details

Details for the file orthomasker-0.9.2-py3-none-any.whl.

File metadata

  • Download URL: orthomasker-0.9.2-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for orthomasker-0.9.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2f4384429a74740c954b8c8d5074f122b8de2c5e14b60d3a01b4b42627dfff6c
MD5 ab4f7749655d566633a8b7e60c59445c
BLAKE2b-256 6e3985fca278d4922e85f891ef301acde0263434357910683db65540be51000a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page