GeoTrellis Documentation

repository·master·Indexed 23 days ago

https://github.com/locationtech/geotrellis

A Scala-based framework for high-performance geospatial processing. GeoTrellis provides APIs for reading, writing, and performing MapAlgebra operations on raster and vector data, with extensive support for distributed processing via Apache Spark. It includes modular packages for integrations with S3, HBase, Cassandra, Accumulo, and GDAL, as well as support for the Mapbox VectorTile specification.

Tokens
72.2K
Snippets
139
Records
282
Agent score
80%

What's inside GeoTrellis

  1. What is GeoTrellis?

    master

    GeoTrellis is a Scala library and framework designed for high-performance raster data processing at scale. It leverages Apache Spark to handle arbitrarily large datasets (terabyte-level and beyond).

    Key Capabilities:

    • Raster Operations: Implements Map Algebra operations and vector-to-raster/raster-to-vector conversions.
    • I/O: Fast reading and writing of raster data.
    • Rendering: Tools to render rasters into PNGs.
    • Metadata: Ability to store raster metadata as JSON.
    • Scalability: Designed for both sub-second RESTful endpoint responses and large-scale batch processing.
  2. Process vector data with geotrellis-vector

    master

    The geotrellis-vector module provides types and algorithms for processing vector data via the geotrellis.vector.* package. Key capabilities include:

    • JTS Helpers: Idiomatic helpers for JTS types like Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection.
    • Geometric Operations: Type-safe geometric operations, Convex Hull, Densification, Simplification, and affine transformations.
    • Feature Type: A Feature type that composes an id, a geometry, and a generic data type.
    • I/O Formats: Read and write geometries and features using GeoJSON, WKT, and WKB.
    • Reprojection: Reproject geometries between two Coordinate Reference Systems (CRS).
    • Interpolation: Perform Kriging interpolation on point values.
  3. Use geotrellis.raster.io for raster serialization and deserialization

    master

    The geotrellis.raster.io package provides various methods for serializing and deserializing raster data for persistence and networking. Depending on your requirements, you can use different sub-packages:

    • GeoTIFF: Use geotrellis.raster.io.geotiff (specifically the reader and writer packages) to read and write .tif files. This is the most common use case for raster I/O.
    • JSON: Use geotrellis.raster.io.json to encode or decode raster data (typically metadata or small subsets rather than entire rasters) as JSON.
    • ARG: Use geotrellis.raster.io.arg to move data in and out of the Azavea Raster Grid format.
    • ASCII: Use geotrellis.raster.io.ascii to interact with ASCII-art representations of rasters.
  4. Use geotrellis-util for core plumbing and data structures

    master

    The geotrellis-util module provides essential low-level utilities and data structures used across the GeoTrellis ecosystem. It provides the geotrellis.util.* package, which includes:

    • Constants and common data structures (e.g., BTree).
    • Haversine implementation for distance calculations.
    • Lenses for data manipulation.
    • RangeReaderProvider for reading contiguous subsets of data.
    • Implementations for FileRangeReader and HttpRangeReader.
  5. Understand the GeoTrellis module hierarchy and dependencies

    master

    GeoTrellis is composed of several interdependent modules. You can include only the modules you need in your build.sbt to manage dependencies effectively. The modules are categorized by their primary function: core raster processing, layer management, Spark integration, storage backends, and specialized utilities (like GDAL or Proj4).

    Key functional groups include:

    • Core Processing: geotrellis-raster (raster algorithms), geotrellis-layer (layer data types), geotrellis-proj4 (CRS transformations).
    • Distributed Computing: geotrellis-spark (RDD-based layer operations), geotrellis-spark-pipeline (JSON/Scala DSL for ETL).
    • Storage Backends: geotrellis-store (abstract interfaces), geotrellis-s3 (AWS S3), geotrellis-accumulo (Apache Accumulo), geotrellis-cassandra (Apache Cassandra), geotrellis-hbase (Apache HBase).
    • Specialized I/O: geotrellis-gdal (GDAL support), geotrellis-shapefile (Shapefile reading), geotrellis-geotools (GeoTools integration).
  6. What is the GeoTrellis Pipeline Tool?

    master

    The Pipeline Tool is an ETL (Extract, Transform, Load) abstraction inspired by PDAL. It allows you to define a sequence of instructions for reading, transforming, and writing geospatial data using JSON objects. Each instruction is represented as a Stage Object.

    Stages are categorized into three types:

    1. Readers: Load data into Spark memory.
    2. Transformations: Process the data (e.g., reprojecting, resampling, building pyramids).
    3. Writers: Save the processed data to storage.

    Pipelines can be defined using JSON or via the internal Scala DSL.

  7. What is an ingest in GeoTrellis?

    master

    In GeoTrellis, an 'ingest' is the process of collecting raw data, transforming it into rasters of a desirable format, and storing it for efficient querying.

    Modern GeoTrellis workflows have moved away from monolithic ETL (Extract/Transform/Load) packages and generic pipeline approaches. Instead, the recommended pattern is to write programs using lower-level constructs, specifically the RasterSource abstraction. This allows for better handling of edge cases and more expressive interaction with imagery sources.

  8. Configure LayoutSchemes for zoom levels

    master

    A LayoutScheme defines how zoom levels are structured within a pyramid. It provides the levelFor() method to determine an integer zoom level and layout definition based on an extent and cell size, and allows navigation via zoomIn() and zoomOut().

    There are two primary modes:

    1. Local Layout Schemes (LocalLayoutScheme): Starts with a specific LayoutDefinition and assigns it an arbitrary zoom number. Subsequent lower-resolution levels are generated via power-of-two reductions. The user must specify the initial numerical zoom level.
    2. Global Layout Schemes (ZoomedLayoutScheme): Has a predefined structure starting from a global extent defined by a CRS. It defines the cell size at zoom level 0 (where one tile covers the entire world extent), and resolution doubles at each successive zoom level. This is compatible with web map standards like TMS.
  9. Perform geometric operations with symbolic operators

    master

    GeoTrellis provides idiomatic Scala extensions for geometric operations that allow for exhaustive pattern matching. Instead of using standard JTS methods that return a generic Geometry (requiring a wildcard _ case), GeoTrellis uses symbolic operators that return a GeometryResult. This allows you to handle all possible outcomes (like NoResult or GeometryCollectionResult) at compile time.

    Operators:

    • & : Intersection
    • | : Union
    • - : Difference

    Example:

    import geotrellis.vector._
    
    val line: LineString = ...
    val poly: Polygon = ...
    
    line & poly match {
      case NoResult => ...
      case PointResult(p) => ...
      case LineStringResult(l) => ...
      case GeometryCollectionResult(gc) => ...
    }
    import geotrellis.vector._
    
    val line: LineString = ...
    val poly: Polygon = ...
    
    // Using Geotrellis wrapper
    line & poly match {
      case NoResult => ...
      case PointResult(p) => ...
      case LineStringResult(l) => ...
      case GeometryCollectionResult(gc) => ...
    }
  10. Manage and preserve Metadata in Spark RDDs

    master

    GeoTrellis uses the Metadata[M] trait to attach layer information (like TileLayerMetadata) to RDDs. This metadata includes the TileLayout, extent, CRS, and CellType.

    ContextRDD

    The concrete implementation of an RDD with metadata is ContextRDD[K, V, M].

    Preserving Metadata

    When performing transformations, metadata can be lost. You must explicitly manage it to ensure consistency for subsequent operations or when writing layers.

    • withContext: Wraps a transformation to preserve the existing metadata. Use this when the operation changes the RDD content but the layout/metadata remains the same.
    • mapContext: Allows you to transform the metadata itself after an operation (e.g., updating the CellType or Bounds).
    • Spatial Joins: Because spatial joins produce new Bounds, you must use .withContext wrappers at every transformation step to allow the updated Bounds to flow through the pipeline.
  11. Subdivide Rasters into Tiles using TileLayout

    master

    For large-scale datasets, GeoTrellis uses a tiling strategy to divide a large spatial Extent into a grid of smaller Tile objects. This allows for distributed processing (e.g., via Apache Spark).

    Key components for tiling:

    • TileLayout: Defines the grid structure. It specifies the number of columns/rows and the dimensions (width/height in cells) of each tile.
    • LayoutDefinition: Combines an Extent with a TileLayout to define the complete spatial grid.
    • mapTransform: A property of LayoutDefinition used to translate spatial extents into grid indices (e.g., finding which tiles overlap a specific area).
    import geotrellis.spark.tiling._
    
    // Create a 7x4 grid where each tile is 100x100 cells
    val tl = TileLayout(7, 4, 100, 100)
    
    // Combine with spatial extent
    val ld = LayoutDefinition(extent, tl)