/fluent-postgis

Swift PostGIS support for FluentPostgreSQL and Vapor

Primary LanguageSwiftMIT LicenseMIT

FluentPostGIS

Platforms Package Managers

A fork of the FluentPostGIS package which adds support for geographic queries. FluentPostGIS provides PostGIS support for fluent-postgres-driver and Vapor 4.

Installation

Swift Package Manager

Add this line to your dependencies in Package.swift:

.package(url: "https://github.com/brokenhandsio/fluent-postgis.git", from: "0.3.0")

Then add this line to a target's dependencies:

.product(name: "FluentPostGIS", package: "fluent-postgis"),

Setup

Import module

import FluentPostGIS

Optionally, you can add a Migration to enable PostGIS:

app.migrations.add(EnablePostGISMigration())

Models

Add a type to your model

final class User: Model {
    static let schema = "user"
    
    @ID(key: .id)
    var id: UUID?

    @Field(key: "location")
    var location: GeometricPoint2D
}

Then use its data type in the Migration:

struct UserMigration: AsyncMigration {
    func prepare(on database: Database) async throws -> {
        try await database.schema(User.schema)
            .id()
            .field("location", .geometricPoint2D)
            .create()
    }
    func revert(on database: Database) async throws -> {
        try await database.schema(User.schema).delete()
    }
}
Geometric Types Geographic Types
GeometricPoint2D GeographicPoint2D
GeometricLineString2D GeographicLineString2D
GeometricPolygon2D GeographicPolygon2D
GeometricMultiPoint2D GeographicMultiPoint2D
GeometricMultiLineString2D GeographicMultiLineString2D
GeometricMultiPolygon2D GeographicMultiPolygon2D
GeometricGeometryCollection2D GeographicGeometryCollection2D

Queries

Query using any of the filter functions:

let eiffelTower = GeographicPoint2D(longitude: 2.2945, latitude: 48.858222)
try await User.query(on: database)
    .filterGeographyDistanceWithin(\.$location, eiffelTower, 1000)
    .all()
Queries
filterGeometryContains
filterGeometryCrosses
filterGeometryDisjoint
filterGeometryDistance
filterGeometryDistanceWithin
filterGeographyDistanceWithin
filterGeometryEquals
filterGeometryIntersects
filterGeometryOverlaps
filterGeometryTouches
filterGeometryWithin
sortByDistance

💝 Contributing

Please create an issue with a description of your problem or open a pull request with a fix.

✌️ License

MIT

👽 Author

BrokenHands, Tim Condon, Nikolai Guyot - https://www.brokenhands.io/ Ricardo Carvalho - https://rabc.github.io/ Phil Larson - http://dizm.com