Spatial Memory

Overview

Spatial memory in the memories-dev framework enables AI systems to develop sophisticated understanding of geographic and topological information, supporting reasoning about space, location, and physical relationships.

Key Concepts

Spatial Data Models

The framework supports multiple spatial data models:

  • Vector: Points, lines, polygons, and multipart features

  • Raster: Gridded data with regular cells

  • TIN: Triangulated irregular networks

  • Voxel: Three-dimensional volumetric data

  • Point Cloud: Collections of 3D points

Coordinate Reference Systems

Proper handling of spatial reference systems:

from memories.earth.spatial import SpatialReference

# Create a spatial reference system
wgs84 = SpatialReference(epsg=4326)  # WGS 84
utm_zone10 = SpatialReference(epsg=32610)  # UTM Zone 10N

# Transform between reference systems
point_wgs84 = (37.7749, -122.4194)  # San Francisco in WGS 84
point_utm = wgs84.transform_to(utm_zone10, point_wgs84)

Spatial Relationships

Understanding topological relationships between spatial entities:

from memories.earth.spatial import SpatialRelations

# Create spatial relation analyzer
spatial_relations = SpatialRelations()

# Check if one geometry contains another
contains = spatial_relations.contains(polygon1, polygon2)

# Calculate the shortest distance between geometries
distance = spatial_relations.distance(point, line)

# Find all intersections
intersections = spatial_relations.intersections(roads, rivers)

Implementation

Spatial Memory Architecture

Core components of spatial memory:

from memories.architecture import SpatialMemory

# Create a spatial memory system
spatial_memory = SpatialMemory(
    name="geographic_memory",
    default_crs="EPSG:4326",
    storage_path="/data/spatial",
    indexing_method="rtree"
)

# Add a feature to memory
lake_feature = {
    "type": "Feature",
    "geometry": {
        "type": "Polygon",
        "coordinates": [[[x1, y1], [x2, y2], ...]]
    },
    "properties": {
        "name": "Lake Superior",
        "area_km2": 81700,
        "max_depth_m": 406
    }
}

spatial_memory.add_feature(lake_feature, collection="water_bodies")

Spatial Queries

Querying data using spatial filters:

# Point-based query (find features near a location)
nearby_features = spatial_memory.query(
    point=(45.7, -86.9),
    distance=10000,  # meters
    collections=["water_bodies", "land_features"]
)

# Bounding box query
region_features = spatial_memory.query_bbox(
    min_x=45.0, min_y=-87.5,
    max_x=46.5, max_y=-86.0,
    collections=["water_bodies"]
)

# Polygon-based query
watershed_features = spatial_memory.query_intersects(
    geometry=watershed_polygon,
    collections=["rivers", "lakes"]
)

Spatial Analysis

Vector Analysis

Analytical operations on vector data:

from memories.earth.analysis import VectorAnalysis

# Create vector analyzer
vector_analyzer = VectorAnalysis()

# Buffer a geometry
buffer = vector_analyzer.buffer(point, distance=1000)

# Calculate area
area = vector_analyzer.area(polygon)

# Perform overlay analysis
intersection = vector_analyzer.overlay(
    layer1=urban_areas,
    layer2=flood_zones,
    operation="intersection"
)

Raster Analysis

Operations on raster datasets:

from memories.earth.analysis import RasterAnalysis

# Create raster analyzer
raster_analyzer = RasterAnalysis()

# Calculate zonal statistics
statistics = raster_analyzer.zonal_stats(
    raster=elevation_data,
    zones=watershed_polygons,
    stats=["mean", "min", "max", "std"]
)

# Perform map algebra
ndvi = raster_analyzer.calculate(
    "(nir - red) / (nir + red)",
    variables={
        "nir": nir_band,
        "red": red_band
    }
)

Spatial Modeling

Building models based on spatial data:

from memories.earth.modeling import SpatialModeler

# Create spatial modeler
modeler = SpatialModeler()

# Create a suitability model
suitability = modeler.create_suitability_model(
    factors=[
        {"data": slope, "weight": 0.3, "function": "linear_decrease"},
        {"data": distance_to_water, "weight": 0.4, "function": "exponential_decrease"},
        {"data": soil_quality, "weight": 0.3, "function": "categorical_map"}
    ],
    constraints=[protected_areas]
)

# Export results
modeler.export_results(suitability, "/results/habitat_suitability.tif")

Best Practices

  1. CRS Standardization: Define and consistently use appropriate coordinate reference systems

  2. Scale Awareness: Consider the scale and resolution appropriate for your application

  3. Topology Validation: Ensure vector data maintains topological integrity

  4. Metadata Management: Maintain comprehensive metadata for all spatial datasets

  5. Efficient Indexing: Use spatial indices for large datasets to improve query performance

  6. Error Propagation: Track spatial uncertainty through analysis operations

  7. Edge Effects: Account for edge effects in spatial analysis operations

Advanced Topics

  • 3D Analysis: Techniques for volumetric and 3D surface analysis

  • Temporal-Spatial Integration: Methods for analyzing spatio-temporal patterns

  • Network Analysis: Analyzing connectivity and flow across spatial networks

  • Spatial Statistics: Advanced statistical methods for spatial data

  • Spatial Machine Learning: AI approaches for spatial prediction and classification