Home
Softono
o

opengeos

Professional software vendor delivering innovative solutions on the Softono platform. Specialized in both open-source and proprietary software development.

Total Products
2

Software by opengeos

GeoAgent
Open Source

GeoAgent

# GeoAgent [![QGIS Plugin](https://img.shields.io/badge/QGIS-Plugin-green.svg)](https://plugins.qgis.org/plugins/open_geoagent) [![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/GeoAgent/blob/main) [![notebook-link](https://img.shields.io/badge/notebook-link-e2d610?logo=jupyter&logoColor=white)](https://notebook.link/github/opengeos/GeoAgent/tree/main/lab/?path=docs%2Fnotebooks%2F00_key_features.ipynb) [![image](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/opengeos/leafmap/HEAD) [![image](https://img.shields.io/pypi/v/GeoAgent.svg)](https://pypi.python.org/pypi/GeoAgent) [![image](https://static.pepy.tech/badge/GeoAgent)](https://pepy.tech/project/GeoAgent) [![Conda Recipe](https://img.shields.io/badge/recipe-GeoAgent-green.svg)](https://github.com/conda-forge/GeoAgent-feedstock) [![image](https://img.shields.io/conda/vn/conda-forge/GeoAgent.svg)](https://anaconda.org/conda-forge/GeoAgent) [![Conda Downloads](https://img.shields.io/conda/dn/conda-forge/GeoAgent.svg)](https://anaconda.org/conda-forge/GeoAgent) [![image](https://github.com/opengeos/leafmap/workflows/docs/badge.svg)](https://leafmap.org) [![image](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![image](https://img.shields.io/badge/YouTube-Channel-red)](https://youtube.com/@giswqs) **GeoAgent** is a shared AI agent layer for geospatial Python packages, live map widgets, and QGIS plugins. With one consistent interface, projects such as **leafmap**, **anymap**, **geoai**, **geemap**, STAC, and NASA Earthdata can expose their tools to large language models, so each library no longer needs to build and maintain its own agent stack. Built on **[Strands Agents](https://strandsagents.com/)**, GeoAgent adds geospatial context, structured tool metadata, optional package adapters, provider configuration, and confirmation hooks that pause the agent before destructive, expensive, or otherwise irreversible operations. See it in action: - Short demo: [QGIS OpenGeoAgent Plugin Demo](https://youtu.be/1me0ecJ5kfk) - Full tutorial: [OpenGeoAgent: A Multimodal AI Agent for Automated Geospatial Analysis and Visualization](https://youtu.be/5zkXQlHUsu8) [![OpenGeoAgent video tutorial](https://img.youtube.com/vi/5zkXQlHUsu8/maxresdefault.jpg)](https://youtu.be/5zkXQlHUsu8) ## Why GeoAgent? Many geospatial libraries need the same agent features: - bind an agent to a live map, QGIS session, dataset, or workflow object; - expose package functions as structured tools with docstrings and metadata; - support OpenAI, ChatGPT/Codex OAuth, Anthropic, Google Gemini, Bedrock, OpenRouter, LiteLLM, vLLM, and local Ollama models; - keep optional geospatial stacks optional; - ask for confirmation before deleting layers, saving files, or running expensive processing jobs; - support multimodal plugin workflows such as pasted images and screenshots when the selected model provider supports vision; - provide a stable integration point for downstream packages and plugins. GeoAgent centralizes that layer so each package does not need to maintain its own agent framework. ## Core Concepts | Concept | Purpose | | ----------------- | ---------------------------------------------------------------------------------- | | `GeoAgent` | High-level facade around a Strands `Agent`. | | `GeoAgentConfig` | Provider, model, temperature, token, and client settings. | | `GeoAgentContext` | Runtime objects bound to the agent, such as a map or QGIS iface. | | `@geo_tool` | Decorator that turns a Python function into a Strands tool with GeoAgent metadata. | | `GeoToolRegistry` | Registry for tool metadata, safety flags, categories, and fast-mode filtering. | | `for_leafmap` | Factory that binds tools to a `leafmap.Map`-compatible object. | | `for_anymap` | Factory that binds tools to an `anymap.Map`-compatible object. | | `for_qgis` | Factory that binds tools to `qgis.utils.iface` and an optional `QgsProject`. | | `for_nasa_opera` | Factory that binds NASA OPERA search/display tools plus QGIS tools. | | `for_stac` | Factory that binds STAC catalog search/asset tools plus optional QGIS loading. | | `create_agent` | Factory for custom tools or package-specific integrations. | ## Installation Install the core package: ```bash pip install GeoAgent ``` Core installs only the agent framework dependencies, mainly `strands-agents` and `pydantic`. Geospatial packages and provider clients are optional extras: | Extra | Purpose | | ----------------------- | ----------------------------------------------------------------- | | `GeoAgent[openai]` | OpenAI model support through Strands. | | `GeoAgent[anthropic]` | Anthropic Claude model support through Strands. | | `GeoAgent[gemini]` | Google Gemini model support through Strands. | | `GeoAgent[ollama]` | Local Ollama model support. | | `GeoAgent[litellm]` | LiteLLM model support for many hosted and proxy providers. | | `GeoAgent[openrouter]` | OpenRouter support for DeepSeek, Qwen, and other hosted models. | | `GeoAgent[vllm]` | vLLM server support through the Strands community provider. | | `GeoAgent[leafmap]` | leafmap live map integration. | | `GeoAgent[anymap]` | anymap live map integration. | | `GeoAgent[stac]` | STAC client dependencies. | | `GeoAgent[earthdata]` | NASA Earthdata dependencies. | | `GeoAgent[nasa-opera]` | NASA OPERA search dependencies. | | `GeoAgent[geoai]` | geoai integration dependencies. | | `GeoAgent[earthengine]` | Google Earth Engine dependencies. | | `GeoAgent[ui]` | Solara UI dependencies. | | `GeoAgent[browser]` | FastAPI WebSocket backend for browser MapLibre apps. | | `GeoAgent[providers]` | OpenAI, Anthropic, Gemini, Ollama, LiteLLM, OpenRouter, and vLLM clients. | | `GeoAgent[all]` | Most optional integrations. QGIS itself remains system-installed. | Examples: ```bash pip install "GeoAgent[leafmap,openai]" pip install "GeoAgent[anymap,anthropic]" pip install "GeoAgent[stac,earthdata,openai]" ``` For QGIS, install GeoAgent in the Python environment used by QGIS. The `GeoAgent[qgis]` extra is a marker extra; QGIS is provided by the desktop application or system package manager. ## Provider Configuration GeoAgent selects a provider from environment variables when no provider is specified: | Provider | Environment | | ------------------- | ------------------------------------------------------------------ | | OpenAI | `OPENAI_API_KEY`, optional `OPENAI_MODEL` | | ChatGPT/Codex OAuth | `OPENAI_CODEX_ACCESS_TOKEN`, optional `OPENAI_CODEX_MODEL` | | Anthropic | `ANTHROPIC_API_KEY`, optional `ANTHROPIC_MODEL` | | Google Gemini | `GEMINI_API_KEY` or `GOOGLE_API_KEY`, optional `GEMINI_MODEL` | | LiteLLM | `LITELLM_API_KEY`, optional `LITELLM_MODEL` and `LITELLM_BASE_URL` | | OpenRouter | `OPENROUTER_API_KEY`, optional `OPENROUTER_MODEL` and `OPENROUTER_BASE_URL` | | vLLM | `VLLM_BASE_URL`, `VLLM_MODEL_ID`, optional `VLLM_API_KEY` | | Ollama | `OLLAMA_HOST` or `USE_OLLAMA=1`, optional `OLLAMA_MODEL` | Bedrock is not auto-detected. Pass `provider="bedrock"` explicitly; it then uses the AWS credential chain, configured region, and Bedrock model access, with optional `BEDROCK_MODEL`. ChatGPT/Codex OAuth uses the Codex browser login flow and the Codex Responses backend. vLLM support expects a running vLLM server. When using GeoAgent tools, start the server with vLLM tool-calling support enabled for the selected model and chat template. For notebooks and Python scripts, log in once with the CLI: ```bash geoagent codex login ``` or from Python/Jupyter: ```python from geoagent import login_openai_codex login_openai_codex() ``` GeoAgent stores the refresh token in your user config directory and exports `OPENAI_CODEX_ACCESS_TOKEN` for the current Python process. Later sessions can reuse the stored login automatically, or explicitly call: ```python from geoagent import ensure_openai_codex_environment ensure_openai_codex_environment() ``` You can also configure providers explicitly: ```python from geoagent import GeoAgentConfig, for_leafmap agent = for_leafmap( m, config=GeoAgentConfig( provider="openai", model="gpt-5.5", temperature=0, max_tokens=4096, ), ) ``` Factories also accept `provider=` and `model_id=` shortcuts: ```python agent = for_leafmap(m, provider="gemini", model_id="gemini-3.1-pro-preview") ``` OpenRouter can be used for DeepSeek and Qwen models: ```python agent = GeoAgent( config=GeoAgentConfig( provider="openrouter", model="deepseek/deepseek-chat", ) ) qwen_agent = GeoAgent( config=GeoAgentConfig( provider="openrouter", model="qwen/qwen3-32b", ) ) ``` ## Quickstart Use GeoAgent directly when you do not need a map or package-specific toolset: ```python from geoagent import GeoAgent, GeoAgentConfig agent = GeoAgent(config=GeoAgentConfig(provider="openai", model="gpt-5.5")) resp = agent.chat("Explain STAC in two sentences.") print(resp.answer_text) ``` Stream model output as it is generated: ```python import asyncio from geoagent import GeoAgent agent = GeoAgent() async def main(): async for event in agent.stream_chat("Explain STAC in two sentences."): if "data" in event: print(event["data"], end="", flush=True) asyncio.run(main()) ``` Bind an agent to a live `leafmap` map: ```python import leafmap from geoagent import for_leafmap m = leafmap.Map() agent = for_leafmap(m) resp = agent.chat("Add a marker for Knoxville and zoom to it.") print(resp.answer_text) m ``` Bind an agent to an `anymap` map: ```python import anymap from geoagent import for_anymap m = anymap.Map() agent = for_anymap(m) agent.chat("Change the basemap and list the current layers.") ``` Launch the browser workspace: ```bash pip install "GeoAgent[ui,anymap,openai]" geoagent ui ``` The Solara UI opens directly to a map chat workspace with provider/model controls, fast mode, session chat history, compact tool-call results, and a conservative confirmation policy. Confirmation-required tools are denied by default unless you enable auto-approve in the UI. Use GeoAgent from a custom browser MapLibre app: ```bash pip install "GeoAgent[browser]" geoagent codex login geoagent browser --host 127.0.0.1 --port 8765 --model gpt-5.5 ``` Then open the end-to-end example in `examples/browser_maplibre/`. The page connects to `/geoagent/ws`, sends chat prompts, executes `map_command` messages against a live MapLibre map, and returns `map_command_result` messages to the backend. The browser backend uses the `openai-codex` provider by default; pass `--provider` to use a different provider. A TypeScript/Vite version is available in `examples/browser_maplibre_typescript/`. For a browser-only TypeScript agent that calls the live MapLibre map directly with the Strands TypeScript SDK, see `examples/browser_maplibre_strands_typescript/`. This example does not require the Python WebSocket backend and supports OpenAI Responses, OpenAI Chat, Anthropic, and Google Gemini providers from the browser UI. For a Node.js TypeScript MapLibre app that keeps model authentication on the server, see `examples/node_maplibre_strands_typescript/`. It supports the same provider ids as the GeoAgent/QGIS UI, including ChatGPT/Codex OAuth, OpenAI, Anthropic, Gemini, Amazon Bedrock, OpenRouter, LiteLLM, vLLM, and Ollama. For local sessions where you want PyQGIS-style fallback behavior, add `--allow-browser-code`. This exposes `run_maplibre_script`, allowing the agent to execute generated MapLibre JavaScript in the browser when no dedicated tool fits the request. To let the agent remove or clear browser map layers in trusted local sessions, add `--auto-approve-browser-tools`. Use GeoAgent inside QGIS: ```python from qgis.utils import iface from geoagent import for_qgis agent = for_qgis(iface) resp = agent.chat("Summarize the project layers and zoom to the active layer.") print(resp.answer_text) ``` `geoagent.tools.qgis` is import-safe outside QGIS. It imports QGIS classes only inside tool bodies, so tests and non-QGIS environments can import the module. The OpenGeoAgent QGIS plugin adds a dockable chat UI on top of this factory. It supports provider and model controls, streaming responses, pasted image attachments, screenshot capture from the map canvas, QGIS window, or selected screen regions, image preview and save actions, Markdown transcript copying, and copying the PyQGIS script used for an executed result. ## QGIS Plugin OpenGeoAgent is the QGIS plugin interface for GeoAgent. It adds a dockable AI assistant to QGIS so you can inspect projects, navigate the map canvas, load data, run processing workflows, style layers, and execute confirmation-gated PyQGIS scripts from natural language. ![OpenGeoAgent QGIS plugin](https://github.com/user-attachments/assets/ba33831f-3259-461a-89f3-1e9a13cac3e0) ![OpenGeoAgent QGIS plugin GUI](https://github.com/user-attachments/assets/393065b9-7c6a-4219-90e9-c8eb59b9bae7) Key plugin features: - provider and model controls for Bedrock, OpenAI, ChatGPT/Codex OAuth, Anthropic, Google Gemini, Ollama, OpenRouter, LiteLLM, and vLLM; - project-aware QGIS tools for layers, selections, map navigation, processing, project saving, and attribute table actions; - image-aware chat with clipboard paste and screenshot attachments for models that support vision inputs; - screenshot capture from the map canvas, selected map regions, the QGIS window, and selected screen regions; - image preview and save/export actions, plus inline rendering of image outputs returned by multimodal models; - direct image generation with the `generate_image` tool when `OPENAI_API_KEY` is configured; - copy Markdown transcript and copy executed PyQGIS script actions; - confirmation-gated `run_pyqgis_script` fallback when a task needs QGIS API operations that are not covered by a dedicated tool; - lazy dependency checks so opening the chat dock stays responsive. See the [QGIS plugin documentation](docs/qgis-plugin.md) for setup and usage. ## Built-In Tool Surfaces ### leafmap and anymap `for_leafmap(m)` and `for_anymap(m)` expose a shared map-control surface: - inspect state and layers: `get_map_state`, `list_layers`; - navigate: `set_center`, `fly_to`, `set_zoom`, `zoom_in`, `zoom_out`, `zoom_to_bounds`, `zoom_to_layer`; - manage layers: `add_layer`, `remove_layer`, `clear_layers`, `set_layer_visibility`, `set_layer_opacity`; - add data: `add_vector_data`, `add_geojson_data`, `add_raster_data`, `add_cog_layer`, `add_stac_layer`, `add_xyz_tile_layer`, `add_pmtiles_layer`, `add_marker`; - change basemaps and export maps: `change_basemap`, `save_map`. Layer lookup accepts exact names or a unique case-insensitive substring for operations such as `remove_layer`, `zoom_to_layer`, `set_layer_visibility`, and `set_layer_opacity`. ### QGIS `for_qgis(iface, project=None)` exposes tools that run through a Qt GUI-thread marshaller: - inspect project and layer state: `list_project_layers`, `get_active_layer`, `get_project_state`, `get_layer_summary`, `inspect_layer_fields`, `get_selected_features`; - navigate: `zoom_in`, `zoom_out`, `zoom_to_layer`, `zoom_to_extent`, `zoom_to_selected`, `set_center`, `set_scale`, `refresh_canvas`; - manage layers and data: `add_vector_layer`, `add_raster_layer`, `add_xyz_tile_layer`, `remove_layer`, `set_layer_visibility`, `set_layer_opacity`; - select and process: `select_features_by_expression`, `clear_selection`, `run_processing_algorithm`; - open QGIS UI and save: `open_attribute_table`, `save_project`; - run confirmation-gated PyQGIS fallback scripts with `run_pyqgis_script` when a task requires QGIS API operations that are not covered by a dedicated tool. QGIS chat uses a sequential tool executor and GUI-thread marshalling so map canvas and layer-tree calls are routed safely. The PyQGIS fallback receives the current `iface`, `project`, `canvas`, and `active_layer`, making it useful for actions such as raster band renderer changes, labeling tweaks, layer tree updates, and other QGIS API operations. ### NASA OPERA `for_nasa_opera(iface, project=None)` exposes NASA OPERA product tools and the standard QGIS tool surface: - inspect OPERA products: `get_available_datasets`, `get_dataset_info`; - search Earthdata granules: `search_opera_data`; - display results: `display_footprints`, `display_raster`, `create_mosaic`. The OPERA integration is implemented as native GeoAgent tools and does not wrap the NASA OPERA plugin's legacy `nasa_opera.ai.tools` registry. When adding OPERA capabilities, implement the reusable tool logic in `geoagent/tools/nasa_opera.py`. Keep the NASA OPERA QGIS plugin focused on UI, settings, provider selection, progress display, and compatibility aliases. The plugin consumes GeoAgent tool metadata automatically, so new GeoAgent OPERA tools are available to the plugin AI Assistant without duplicating tool logic. Use it from the QGIS Python console or from plugin code. For direct tool testing, use `submit_nasa_opera_search_task(...)`; this avoids LLM/provider initialization and reports progress in QGIS's message bar and Log Messages panel: ```python from geoagent.tools.nasa_opera import submit_nasa_opera_search_task task = submit_nasa_opera_search_task( iface, dataset="OPERA_L3_DSWX-HLS_V1", bbox="-95.5,29.5,-95.0,30.0", start_date="2024-01-01", end_date="2024-01-31", max_results=5, display_footprints=True, ) ``` For a longer QGIS-console script, see `examples/nasa_opera_qgis.py`. Natural-language OPERA chat is intentionally disabled inside QGIS for now. Use direct tools or `submit_nasa_opera_search_task(...)` so QGIS task/thread ownership remains explicit. ### Vantor Open Data `for_vantor(iface, project=None, plugin=None)` exposes native tools for the QGIS Vantor plugin's public Open Data STAC catalog: - browse event collections: `list_vantor_events`, `get_vantor_event_info`; - search imagery: `get_current_vantor_search_extent`, `search_vantor_items`; - display results in QGIS: `display_vantor_footprints`, `load_vantor_cog`; - open the plugin UI when a plugin instance is supplied: `open_vantor_panel`. Footprint display and COG loading are confirmation-gated because they add layers to the current QGIS project. ## Direct Tool Calls Every GeoAgent exposes the underlying Strands tool caller. This is useful for tests, notebooks, and plugin UI actions: ```python agent = for_leafmap(m) agent.tool.add_marker(lat=35.9606, lon=-83.9207, name="Knoxville") agent.tool.list_layers() ``` You can inspect registered tool names and metadata: ```python agent.tool_names agent.tool_registry.get_all_tools_config() ``` ## Safety and Confirmation Tools carry metadata such as `requires_confirmation`, `destructive`, and `long_running`. Confirmation is enforced by a Strands hook before gated tools run. By default, confirmation-required tools are denied unless you pass a confirmation callback: ```python from geoagent import auto_approve_all, for_leafmap agent = for_leafmap(m, confirm=auto_approve_all) ``` For real applications, pass a callback that opens a Qt dialog, notebook modal, web UI prompt, or CLI prompt. Use confirmation for operations that delete data, overwrite files, save projects, launch expensive jobs, or call external services with cost implications. ## Custom Tools Package integrations can expose their own functions with `@geo_tool`: ```python from geoagent import GeoAgentContext, create_agent, geo_tool @geo_tool(category="demo") def buffer_distance(layer_name: str, distance: float) -> str: """Buffer a named layer by a distance in map units.""" return f"Buffered {layer_name} by {distance}." agent = create_agent( context=GeoAgentContext(), tools=[buffer_distance], ) ``` For package-specific adapters, prefer a factory that binds live objects through closures, just like `for_leafmap`, `for_anymap`, and `for_qgis`. This keeps widgets, clients, credentials, and session objects out of the LLM-visible arguments. ## Fast Mode Pass `fast=True` to reduce the exposed tool surface and cap response tokens for lower-latency map control: ```python agent = for_leafmap(m, fast=True) ``` Fast mode keeps common inspection, navigation, and basemap tools, filters out heavier or more specialized tools, and limits model responses to short post-tool replies. The cap is conservative enough for tool calls while avoiding very long responses. The model call still dominates latency for local models, so small prompts may not show a large timing difference on every provider. ## Examples Runnable notebooks live under `docs/examples/`: - `docs/examples/intro.ipynb` — basic GeoAgent usage. - `docs/examples/openai_codex.ipynb` — ChatGPT/Codex OAuth in Python/Jupyter. - `docs/examples/stream_chat_openai_codex.ipynb` — streamed ChatGPT/Codex output. - `docs/examples/live_mapping.ipynb` — live map workflow. - `docs/examples/qgis_agent.ipynb` — QGIS-oriented workflow using mocks. - `docs/examples/stac_workflow.ipynb` — STAC catalog search and mock QGIS loading. - `examples/nasa_opera_qgis.py` — NASA OPERA workflow for QGIS. Prompt ideas: - "List layers on the current map." - "Add a marker for Seattle and zoom to it." - "Show the QGIS project state and summarize each layer." - "Select parcels where population is greater than 10000." - "Add a STAC layer and set its opacity to 0.6." - "Search for January 2024 OPERA surface water near Houston and display the footprints." ## Development ```bash git clone https://github.com/opengeos/GeoAgent.git cd GeoAgent python -m venv .venv source .venv/bin/activate pip install -e ".[dev]" pre-commit install ``` Run checks: ```bash ruff check geoagent tests pytest -q ``` See [docs/contributing.md](docs/contributing.md) for guidance on adding new package and tool integrations. ## License GeoAgent is released under the MIT License. See `LICENSE`. ## Links - Documentation: <https://geoagent.gishub.org> - Repository: <https://github.com/opengeos/GeoAgent> - Issues: <https://github.com/opengeos/GeoAgent/issues> - Strands Agents: <https://strandsagents.com>

AI Agents Geospatial & GIS
371 Github Stars
segment-geospatial
Open Source

segment-geospatial

# SamGeo [![image](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/opengeos/segment-geospatial/blob/main/docs/examples/satellite.ipynb) [![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/segment-geospatial/blob/main/docs/examples/satellite.ipynb) [![image](https://img.shields.io/pypi/v/segment-geospatial.svg)](https://pypi.python.org/pypi/segment-geospatial) [![image](https://img.shields.io/conda/vn/conda-forge/segment-geospatial.svg)](https://anaconda.org/conda-forge/segment-geospatial) [![Docker Pulls](https://badgen.net/docker/pulls/giswqs/segment-geospatial?icon=docker&label=pulls)](https://hub.docker.com/r/giswqs/segment-geospatial) [![PyPI Downloads](https://static.pepy.tech/badge/segment-geospatial)](https://pepy.tech/project/segment-geospatial) [![Conda Recipe](https://img.shields.io/badge/recipe-segment--geospatial-green.svg)](https://github.com/conda-forge/segment-geospatial-feedstock) [![Conda Downloads](https://anaconda.org/conda-forge/segment-geospatial/badges/downloads.svg)](https://anaconda.org/conda-forge/segment-geospatial) [![DOI](https://joss.theoj.org/papers/10.21105/joss.05663/status.svg)](https://doi.org/10.21105/joss.05663) [![QGIS](https://img.shields.io/badge/QGIS-plugin-orange.svg)](https://github.com/opengeos/qgis-samgeo-plugin) [![logo](https://raw.githubusercontent.com/opengeos/segment-geospatial/main/docs/assets/logo_rect.png)](https://github.com/opengeos/segment-geospatial/blob/main/docs/assets/logo.png) **A Python package for segmenting geospatial data with the Segment Anything Model (SAM)** ## Introduction The **SamGeo** package draws its inspiration from [segment-anything-eo](https://github.com/aliaksandr960/segment-anything-eo) repository authored by [Aliaksandr Hancharenka](https://github.com/aliaksandr960). The primary objective of SamGeo is to simplify the process of leveraging SAM for geospatial data analysis by enabling users to achieve this with minimal coding effort. The source code of SamGeo was adapted from the [segment-anything-eo](https://github.com/aliaksandr960/segment-anything-eo) repository, and credit for its original version goes to Aliaksandr Hancharenka. - Free software: MIT license - Documentation: <https://samgeo.gishub.org> ## Citations - Wu, Q., & Osco, L. (2023). samgeo: A Python package for segmenting geospatial data with the Segment Anything Model (SAM). _Journal of Open Source Software_, 8(89), 5663. <https://doi.org/10.21105/joss.05663> - Osco, L. P., Wu, Q., de Lemos, E. L., Gonçalves, W. N., Ramos, A. P. M., Li, J., & Junior, J. M. (2023). The Segment Anything Model (SAM) for remote sensing applications: From zero to one shot. _International Journal of Applied Earth Observation and Geoinformation_, 124, 103540. <https://doi.org/10.1016/j.jag.2023.103540> ## Features - Download map tiles from Tile Map Service (TMS) servers and create GeoTIFF files - Segment GeoTIFF files using the Segment Anything Model ([SAM](https://github.com/facebookresearch/segment-anything)) and [HQ-SAM](https://github.com/SysCV/sam-hq) - Segment remote sensing imagery with text prompts - Create foreground and background markers interactively - Load existing markers from vector datasets - Save segmentation results as common vector formats (GeoPackage, Shapefile, GeoJSON) - Save input prompts as GeoJSON files - Visualize segmentation results on interactive maps - Segment objects from timeseries remote sensing imagery - REST API for serving segmentation over HTTP (see [API docs](https://samgeo.gishub.org/api)) ## QGIS Plugin SamGeo is also available as a [QGIS plugin](https://github.com/opengeos/qgis-samgeo-plugin). Check out this [short video demo](https://youtu.be/DKKrQKeU3Ik) and [full video tutorial](https://youtu.be/oPZc7BvDsHE) on how to use the plugin. [![](https://github.com/user-attachments/assets/0a9dbc4a-98fa-4a14-8238-be5b871926fa)](https://youtu.be/oPZc7BvDsHE) ## Installation ### Install with pixi (Recommended) For the most reliable installation experience, especially on Windows or when dealing with complex dependencies like PyTorch/CUDA and SAM 3, we recommend using [pixi](https://pixi.prefix.dev/latest). Pixi provides faster and more reliable dependency resolution than conda/mamba and avoids common numpy version conflicts. See the [full pixi installation guide](https://samgeo.gishub.org/installation/#install-with-pixi-recommended) for detailed instructions. Quick start with pixi: ```bash # Install pixi (Linux/macOS) curl -fsSL https://pixi.sh/install.sh | sh # Or on Windows (PowerShell) powershell -ExecutionPolicy Bypass -c "irm -useb https://pixi.sh/install.ps1 | iex" # Create a new pixi project pixi init geo cd geo # Edit pixi.toml with your configuration (see docs for GPU/CPU examples) # Then install pixi install # Start Jupyter Lab pixi run jupyter lab ``` ### Install from PyPI **segment-geospatial** is available on [PyPI](https://pypi.org/project/segment-geospatial/) and can be installed in several ways so that its dependencies can be controlled more granularly. This reduces package size for CI environments, since not every time all of the models will be used. Depending on what tools you need to use, you might want to do: - `segment-geospatial` or `segment-geospatial[samgeo]`: Installs only the minimum required dependencies to run SAMGeo - `segment-geospatial[samgeo2]`: Installs the dependencies to run SAMGeo 2 - `segment-geospatial[samgeo3]`: Installs the dependencies to run SAMGeo 3 - `segment-geospatial[fast]`: Installs the dependencies to run Fast SAM - `segment-geospatial[hq]`: Installs the dependencies to run HQ-SAM - `segment-geospatial[text]`: Installs Grounding DINO to use SAMGeo 1 and 2 with text prompts - `segment-geospatial[fer]`: Installs the dependencies to run the feature edge reconstruction algorithm - `segment-geospatial[api]`: Installs FastAPI and Uvicorn for serving segmentation as a REST API Additionally, these other two optional imports are defined: - `segment-geospatial[all]`: Installs the dependencies to run all of the SAMGeo models - `segment-geospatial[extra]`: Installs the dependencies to run all of the SAMGeo models and other utilities to run the examples like Jupyter notebook support, `leafmap`, etc. Simply running the following should install the dependencies for each use case: ```bash pip install "segment-geospatial[samgeo3]" # Or any other choice of the above ``` To see more in detail what packages come with each choice, please refer to `pyproject.toml`. ### Install from conda-forge **segment-geospatial** is also available on [conda-forge](https://anaconda.org/conda-forge/segment-geospatial). If you have [Anaconda](https://www.anaconda.com/distribution/#download-section) or [Miniconda](https://docs.conda.io/en/latest/miniconda.html) installed on your computer, you can install segment-geospatial using the following commands. It is recommended to create a fresh conda environment for **segment-geospatial**. The following commands will create a new conda environment named `geo` and install **segment-geospatial** and its dependencies: ```bash conda create -n geo python conda activate geo conda install -c conda-forge segment-geospatial ``` If your system has a GPU, but the above commands do not install the GPU version of pytorch, you can force the installation of the GPU version of pytorch using the following command: ```bash conda install -c conda-forge segment-geospatial "pytorch=*=cuda*" ``` segment-geospatial has some optional dependencies that are not included in the default conda environment. To install these dependencies, run the following command: ```bash conda install -c conda-forge groundingdino-py segment-anything-fast ``` ### Install SAM 3 on Windows It is a bit tricky to install SAM 3 on Windows. Run the following commands on Windows to install SamGeo: ```bash conda create -n geo python=3.12 conda activate geo conda install pytorch torchvision pytorch-cuda=12.1 -c pytorch -c nvidia pip install "segment-geospatial[samgeo3]" pip install triton-windows ipykernel jupyterlab ``` ## Examples - [Segmenting remote sensing imagery](https://samgeo.gishub.org/examples/satellite) - [Automatically generating object masks](https://samgeo.gishub.org/examples/automatic_mask_generator) - [Segmenting remote sensing imagery with input prompts](https://samgeo.gishub.org/examples/input_prompts) - [Segmenting remote sensing imagery with box prompts](https://samgeo.gishub.org/examples/box_prompts) - [Segmenting remote sensing imagery with text prompts](https://samgeo.gishub.org/examples/text_prompts) - [Batch segmentation with text prompts](https://samgeo.gishub.org/examples/text_prompts_batch) - [Using segment-geospatial with ArcGIS Pro](https://samgeo.gishub.org/examples/arcgis) - [Segmenting swimming pools with text prompts](https://samgeo.gishub.org/examples/swimming_pools) - [Segmenting satellite imagery from the Maxar Open Data Program](https://samgeo.gishub.org/examples/max_open_data) ## Demos - Automatic mask generator ![](https://i.imgur.com/I1IhDgz.gif) - Interactive segmentation with input prompts ![](https://i.imgur.com/2Nyg9uW.gif) - Input prompts from existing files ![](https://i.imgur.com/Cb4ZaKY.gif) - Interactive segmentation with text prompts ![](https://i.imgur.com/wydt5Xt.gif) ## Tutorials Video tutorials are available on my [YouTube Channel](https://youtube.com/@giswqs). - Automatic mask generation [![Alt text](https://img.youtube.com/vi/YHA_-QMB8_U/0.jpg)](https://www.youtube.com/playlist?list=PLAxJ4-o7ZoPcrg5RnZjkB_KY6tv96WO2h) - Using SAM with ArcGIS Pro [![Alt text](https://img.youtube.com/vi/VvyInoQ6N8Q/0.jpg)](https://www.youtube.com/playlist?list=PLAxJ4-o7ZoPcrg5RnZjkB_KY6tv96WO2h) - Interactive segmentation with text prompts [![Alt text](https://img.youtube.com/vi/cSDvuv1zRos/0.jpg)](https://www.youtube.com/playlist?list=PLAxJ4-o7ZoPcrg5RnZjkB_KY6tv96WO2h) ## Using SAM with Desktop GIS - **QGIS**: Check out the [SamGeo QGIS Plugin](https://github.com/opengeos/qgis-samgeo-plugin). - **ArcGIS**: Check out the [Segment Anything Model (SAM) Toolbox for ArcGIS](https://www.arcgis.com/home/item.html?id=9b67b441f29f4ce6810979f5f0667ebe) and the [Resources for Unlocking the Power of Deep Learning Applications Using ArcGIS](https://community.esri.com/t5/education-blog/resources-for-unlocking-the-power-of-deep-learning/ba-p/1293098). ## Computing Resources The Segment Anything Model is computationally intensive, and a powerful GPU is recommended to process large datasets. It is recommended to have a GPU with at least 8 GB of GPU memory. You can utilize the free GPU resources provided by Google Colab. Alternatively, you can apply for [AWS Cloud Credit for Research](https://aws.amazon.com/government-education/research-and-technical-computing/cloud-credit-for-research), which offers cloud credits to support academic research. If you are in the Greater China region, apply for the AWS Cloud Credit [here](https://aws.amazon.com/cn/events/educate_cloud/research-credits). ## Legal Notice This repository and its content are provided for educational purposes only. By using the information and code provided, users acknowledge that they are using the APIs and models at their own risk and agree to comply with any applicable laws and regulations. Users who intend to download a large number of image tiles from any basemap are advised to contact the basemap provider to obtain permission before doing so. Unauthorized use of the basemap or any of its components may be a violation of copyright laws or other applicable laws and regulations. ## Contributing Please refer to the [contributing guidelines](https://samgeo.gishub.org/contributing) for more information. ## Acknowledgements This project is based upon work partially supported by the National Aeronautics and Space Administration (NASA) under Grant No. 80NSSC22K1742 issued through the [Open Source Tools, Frameworks, and Libraries 2020 Program](https://bit.ly/3RVBRcQ). This project is also supported by Amazon Web Services ([AWS](https://aws.amazon.com/)). In addition, this package was made possible by the following open source projects. Credit goes to the developers of these projects. - [segment-anything](https://github.com/facebookresearch/segment-anything) - [SAM 3](https://github.com/facebookresearch/sam3) - [segment-anything-eo](https://github.com/aliaksandr960/segment-anything-eo) - [tms2geotiff](https://github.com/gumblex/tms2geotiff) - [GroundingDINO](https://github.com/IDEA-Research/GroundingDINO) - [lang-segment-anything](https://github.com/luca-medeiros/lang-segment-anything)

ML Frameworks
4K Github Stars