Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,20 @@ The deprecated aliases from 2.x have been removed. Use the current names:
are covered by a new differential test suite; the generator script is
development-only and the package keeps no third-party runtime dependency.

- Extended WKB/WKT/TWKB geometry coverage (#404):
- WKB and WKT decode `CircularString`, `CompoundCurve`, `CurvePolygon`,
`MultiCurve` and `MultiSurface`, linearizing arcs into `LineString`,
`Polygon`, `MultiLineString` and `MultiPolygon`.
- `TIN` and `PolyhedralSurface` decode to `MultiPolygon`, with each triangle
or patch represented as a `Polygon`.
- `POINT EMPTY` decodes to an empty `MultiPoint`; WKT `EMPTY` is accepted for
every geometry type and empty geometries encode as `TYPE EMPTY`.
- WKB accepts ISO SQL/MM dimension codes (`+1000` Z, `+2000` M, `+3000` ZM)
in addition to EWKB dimension flags.
- TWKB empty metadata decodes to the matching empty geometry. PostGIS
encodes `TIN` as a `GeometryCollection` in TWKB; curves and
`PolyhedralSurface` are not representable in TWKB.

- API compatibility CI (#380): pull requests now run
`swift package diagnose-api-breaking-changes` against their base branch.
Accidental public API removals or signature changes fail CI; intentional
Expand Down
26 changes: 26 additions & 0 deletions Sources/GISTools/GISTools.docc/GeoJSONAndCoders.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,27 @@ exercise insertion and fetching for every supported geometry type.
ring, polygon, multi-point, multi-line string, multi-polygon,
geometry collection and triangle geometry types.

Curved geometries are linearized into the existing model types while decoding:

| Input | Decoded as |
| --- | --- |
| `CircularString` | `LineString` |
| `CompoundCurve` | `LineString` |
| `CurvePolygon` | `Polygon` |
| `MultiCurve` | `MultiLineString` |
| `MultiSurface` | `MultiPolygon` |

`TIN` and `PolyhedralSurface` decode to a `MultiPolygon`, with every triangle
or patch represented as a `Polygon`. The mapping is lossy: re-encoding writes
the linear type, not the original curve or surface type.

`POINT EMPTY` decodes to an empty `MultiPoint` because the model has no empty
`Point`. The other empty geometries decode as their empty counterparts, and WKT
writes `TYPE EMPTY` when encoding an empty geometry.

WKB accepts both EWKB dimension flags and the ISO SQL/MM dimension codes used
by PostGIS (`+1000` for Z, `+2000` for M, `+3000` for ZM).

Every geometry also has convenience initializers for decoding and `asWKB` and
`asWKT` accessors. Decoding can reproject on the fly:

Expand Down Expand Up @@ -159,6 +180,11 @@ let point = try TWKBCoder.decode(
sourceProjection: .epsg4326)
```

TWKB empty metadata decodes to the corresponding empty geometry. PostGIS
encodes `TIN` as a `GeometryCollection` of polygons in TWKB, so it decodes as a
collection rather than a `MultiPolygon`. PostGIS rejects curves and
`PolyhedralSurface` when writing TWKB.

A `Feature` or `FeatureCollection` containing an unlocated feature cannot be
encoded to WKB or WKT; encoding returns `nil`.

Expand Down
151 changes: 151 additions & 0 deletions Sources/GISTools/GeoJson/CurveLinearization.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import Foundation

/// Linearizes curved WKB/WKT geometries into the straight geometry types that
/// the GeoJSON model supports.
///
/// The GeoJSON model has no native curve types, so `CircularString`,
/// `CompoundCurve`, `CurvePolygon`, `MultiCurve` and `MultiSurface` are
/// converted while decoding. Circular arcs are sampled with
/// ``segmentsPerQuadrant`` segments per quarter circle.
enum CurveLinearization {

/// The number of line segments used for a quarter circle.
static let segmentsPerQuadrant = 32

/// Converts the points of a `CircularString` into a linear coordinate list.
///
/// A `CircularString` stores arcs as consecutive triples: start, a point on
/// the arc, and end. Consecutive arcs share their first and last point.
///
/// - Parameter coordinates: The control points of the circular string.
/// - Returns: The linearized coordinates.
static func circularString(_ coordinates: [Coordinate3D]) -> [Coordinate3D] {
guard coordinates.count >= 3 else { return coordinates }

var result: [Coordinate3D] = [coordinates[0]]
var index = 0

while index + 2 < coordinates.count {
let arc = arc(
from: coordinates[index],
through: coordinates[index + 1],
to: coordinates[index + 2])

result.append(contentsOf: arc.dropFirst())
index += 2
}

// Invalid input with an even number of points: keep the trailing point
// instead of silently dropping it.
if index + 1 < coordinates.count {
result.append(coordinates[index + 1])
}

return result
}

/// Samples the circular arc through three points.
///
/// - Parameters:
/// - start: The start point.
/// - mid: A point on the arc between `start` and `end`.
/// - end: The end point.
/// - Returns: The sampled arc, including both endpoints.
private static func arc(
from start: Coordinate3D,
through mid: Coordinate3D,
to end: Coordinate3D,
) -> [Coordinate3D] {
let ax = start.x
let ay = start.y
let bx = mid.x
let by = mid.y
let cx = end.x
let cy = end.y

let scale = max(1.0, abs(ax), abs(ay), abs(bx), abs(by), abs(cx), abs(cy))
let tolerance = 1e-9 * scale

let determinant = 2.0 * ((ax * (by - cy)) + (bx * (cy - ay)) + (cx * (ay - by)))

guard abs(determinant) > 1e-12 * scale * scale else {
// Collinear or coincident points: no meaningful circle.
return [start, mid, end]
}

let aSquared = (ax * ax) + (ay * ay)
let bSquared = (bx * bx) + (by * by)
let cSquared = (cx * cx) + (cy * cy)

let centerX = ((aSquared * (by - cy)) + (bSquared * (cy - ay)) + (cSquared * (ay - by))) / determinant
let centerY = ((aSquared * (cx - bx)) + (bSquared * (ax - cx)) + (cSquared * (bx - ax))) / determinant

let radius = hypot(ax - centerX, ay - centerY)
guard radius > tolerance else { return [start, mid, end] }

let startAngle = atan2(ay - centerY, ax - centerX)
let midAngle = atan2(by - centerY, bx - centerX)
let endAngle = atan2(cy - centerY, cx - centerX)

let isFullCircle = hypot(ax - cx, ay - cy) <= tolerance
let sweep: Double

if isFullCircle {
// The start and end point are identical, so use the side of the
// centre that the middle point lies on to choose the direction.
let cross = ((bx - centerX) * (ay - centerY)) - ((by - centerY) * (ax - centerX))
sweep = cross >= 0 ? 2.0 * .pi : -2.0 * .pi
}
else {
let counterClockwiseSweep = normalizedAngle(endAngle - startAngle)
let counterClockwiseMid = normalizedAngle(midAngle - startAngle)
sweep = counterClockwiseMid <= counterClockwiseSweep
? counterClockwiseSweep
: counterClockwiseSweep - (2.0 * .pi)
}

let segmentCount = max(
2,
Int(ceil(abs(sweep) / (.pi / 2.0) * Double(segmentsPerQuadrant))))

var result: [Coordinate3D] = []
result.reserveCapacity(segmentCount + 1)

for index in 0 ... segmentCount {
let fraction = Double(index) / Double(segmentCount)
let angle = startAngle + (sweep * fraction)

result.append(
Coordinate3D(
x: centerX + (radius * cos(angle)),
y: centerY + (radius * sin(angle)),
z: interpolate(start.z, end.z, fraction),
m: interpolate(start.m, end.m, fraction),
projection: start.projection))
}

// Avoid trigonometric drift at the endpoints.
result[0] = start
result[result.count - 1] = end

return result
}

/// Normalizes an angle to `0 ..< 2π`.
private static func normalizedAngle(_ angle: Double) -> Double {
var result = angle.truncatingRemainder(dividingBy: 2.0 * .pi)
if result < 0 {
result += 2.0 * .pi
}
return result
}

/// Linearly interpolates an optional value.
private static func interpolate(_ start: Double?, _ end: Double?, _ fraction: Double) -> Double? {
if let start, let end {
return start + ((end - start) * fraction)
}
return start ?? end
}

}
20 changes: 19 additions & 1 deletion Sources/GISTools/GeoJson/TWKBCoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,25 @@ public enum TWKBCoder {

let (geomType, scale) = try decodeHeader(bytes: bytes, offset: &offset)
let metadata = try decodeMetadata(bytes: bytes, offset: &offset)
guard !metadata.isEmpty else { throw TWKBError.emptyGeometry }

if metadata.isEmpty {
switch geomType {
case 1, 4:
return MultiPoint()
case 2:
return LineString()
case 3:
return Polygon()
case 5:
return MultiLineString()
case 6:
return MultiPolygon()
case 7:
return GeometryCollection([])
default:
throw TWKBError.unexpectedType
}
}

let sourceProjection = sourceProjection ?? .epsg4326

Expand Down
Loading
Loading