# Geo without a map

> Canonical URL: https://docs.mountaya.com/en/sdks/swift/geo/
> Last updated: 2026-11-27
> Rules for agents: https://docs.mountaya.com/llms.txt#rules-for-agents

Measure, format and profile routes in Swift with MountayaGeo, Mountaya's rules with no network and no renderer.

`MountayaGeo` holds the rules the map and its elements draw by, as plain functions. It calls no network and draws nothing, so it runs in a test, a widget or a server.

## Measure and format distances

A position is `[longitude, latitude]`, as on the wire:

```swift
import MountayaGeo

// Chamonix to Argentière.
let line: [[Double]] = [[6.8712, 45.9231], [6.9271, 45.9822]]
let meters = routeLengthMeters(line)

let units = UnitPreferences(language: "fr-FR")
print(formatDistance(meters, units: units), formatLngLat(line[0]) ?? "")
```

Every value is written for its reader, in the language and units you pass, never the device's.

- Every rule is a top-level function, named as the other Mountaya SDKs name it, and answers the same numbers and words for the same input: the contract's test cases prove each one.
- Geodesy and route geometry: `haversineMeters(from:to:)`, `routeLengthMeters(_:)`, `cumulativeMeters(_:)`, `bearingDegrees(from:to:)`, `positionAtMeters(_:meters:)`, `distanceToSegment(_:start:end:)`, `metersToRoute(_:lines:)`, `wrapLongitude(_:)`, `boundsOf(_:)`.
- Formatting: `formatDistance`, `formatElevation`, `formatSpeed`, `formatSlope`, `formatTemperature`, `formatDuration`, `formatHumidity`, `formatSnowfall`, each `(_:units:)`; `formatAltitudeRange(min:max:units:)`; `formatLngLat(_:)`; `formatNumber(_:locale:maximumFractionDigits:)`.
- A rule handed a position that names no place answers rather than throws: a distance is `NaN`, a printed position `nil`.
- `formatDateTime(_:locale:timeZone:dateLength:timeLength:)` writes an instant at a `DateLength` and a `TimeLength` (short or medium), or the long date where you name neither, in the zone you name, on the locale's own 12- or 24-hour clock whatever the device is set to. It answers nil for a zone the locale services do not know.

## Build an elevation series

The series every profile and table draws from, and its totals:

```swift
import MountayaClient
import MountayaGeo

func describe(_ itinerary: RestSchema.ItineraryWithDetails) -> String {
  let series = buildElevationSeries(ResolvedItinerary(itinerary).geometry)
  let totals = profileTotals(series)
  let units = UnitPreferences(language: "en")
  return "\(formatElevation(totals.ascent, units: units)) up, "
    + "\(formatElevation(totals.descent, units: units)) down"
}
```

- `ResolvedItinerary(_:)` reads a `RestSchema.ItineraryWithDetails` into the projections every element reads: `geometry`, `legs` (`[ItineraryLeg]`), `waypoints` (`[ItineraryPlace]`), `characteristics`.
- `buildElevationSeries(_:maxPoints:)` answers an `ElevationSeries` of `points`; elevation is each position's third coordinate.
- Profile rules: `profileTotals(_:)`, `rangeTotals(_:from:to:)`, `detectClimbs(_:)` and `climbCategoryFor(lengthMeters:avgGradePercent:)`, `gradeAt(_:distance:)`, `slopeStretches(_:buckets:)`, `classifySteepness(_:)`, `profileCharacteristics(_:waypoints:)`, `itinerarySegmentRows(_:legs:stated:)`, `itineraryWaypointRows(_:geometry:places:)`.
- `ItineraryResourceEngine(cache:itineraryId:)` reads an itinerary by id through an `ItineraryResourceCache` and streams `ItineraryResource` (`.idle`, `.resolving`, `.resolved(ResolvedItinerary)`, `.failed`) from `subscribe()`.

## Read the terrain at a point

The terrain under a point is read off the Tile API's elevation tiles. Geo says which tiles to fetch and what their pixels mean:

```swift
import MountayaGeo

let source = ElevationSource(encoding: .mapbox, tileSize: 512, zoom: 15)
let tiles = elevationTilesAt([6.8712, 45.9231], source: source)

let height = decodeElevation([1, 134, 160], encoding: .mapbox)
let facing = aspectCardinal(236.3)
print(tiles.count, height ?? 0, facing?.rawValue ?? "")
```

Fetch the tiles with your client, then hand their pixels to `terrainFromElevationTiles(_:source:tiles:)` for the height, slope and aspect.

- `elevationTilesAt(_:source:)` answers the `ElevationTile` addresses a point needs at the source's deepest zoom. `terrainFromElevationTiles(_:source:tiles:)` answers a `TerrainReading` (height, slope, aspect) from the pixels you read out of them. Decoding an image into pixels is your platform's codec; the rules start from the bytes.
- `decodeElevation(_:encoding:)` decodes one pixel by the `ElevationEncoding` the source's TileJSON names. `slopeAspectFromNeighbours(_:latitude:spacingDegrees:)` reads slope and aspect from the heights of a point's four neighbours, a `TerrainNeighbours` (`north`, `south`, `east`, `west`).
- `aspectCardinal(_:)` names a bearing by its eighth of the compass (`AspectCardinal`).
- The map's readout reads the terrain this way for you ([read a point](https://docs.mountaya.com/en/sdks/swift/map.md#readout)).

## Units and languages

`UnitPreferences` carries the language and units every figure is written in:

```swift
import MountayaClient
import MountayaGeo

let imperial = UnitPreferences(language: "en-US", length: .imperial)
print(formatDistance(42_195, units: imperial), formatSnowfall(35, units: imperial))

let french = scopeOf(.standard, stating: ElementsScopeStatement(language: .fr, locale: "fr-CH"))
print(french.word("controls.zoomIn"))
```

- `UnitPreferences(language:length:pace:slope:temperature:)`: `length` `.metric` or `.imperial`, `pace` `.perTime` by default, `slope` `.degrees` by default, each a `RestSchema` enum.
- Conversions: `convertLength`, `convertSpeed`, `convertDuration`, `convertTemperature`, `convertAngle`, `convertDataSize`, `convertProportion`, `convertSlope`, each `(_:from:to:)`.
- Words: `resolveBundle(_:language:overrides:handedOver:)` resolves the elements' words for a language with yours folded over them; `scopeOf(_:stating:)` nests a scope that takes every setting it does not state from its parent; `countedText(_:count:language:)` picks the plural form.
- Styling: `ElementTokens.standard` restyled on a copy (`tokens.accent = ThemeToken(light: HexColor(hex: "#e9501e"))`), read with `themeTokenValue(_:in:)`; `MapPaint.standard`; `poiStyleOf(_:)`; `GlyphSet.marker` and `.control`, chained with `GlyphSet(chaining:)`.
- Geo links `MountayaClient` for its wire types, since Swift has no type-only dependency; it never calls the network.

## Next steps

- [Turn-by-turn navigation](https://docs.mountaya.com/en/sdks/swift/navigation.md): Follow a route with Geo's rules on the device.
- [Testing](https://docs.mountaya.com/en/sdks/swift/testing.md): Test the code that calls the API.
