Skip to content

Swift PostGIS support for FluentPostgreSQL and Vapor

License

Notifications You must be signed in to change notification settings

rabc/fluent-postgis

This branch is 18 commits ahead of plarson/fluent-postgis:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

93cff8c · Oct 9, 2020
Oct 9, 2020
Jun 19, 2020
Dec 9, 2018
Dec 14, 2018
Jun 19, 2020
May 6, 2020

Repository files navigation

FluentPostGIS

Platforms Package Managers Twitter rabc

PostGIS support for fluent-postgres-driver and Vapor 4

Installation

Swift Package Manager

.package(url: "https://github.com/rabc/fluent-postgis.git", .branch("vapor_4"))

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 UserLocation: Model {
    static let schema = "user_location"
    
    @ID(custom: "id", generatedBy: .database)
    var id: Int?
    @Field(key: "location")
    var location: GeometricPoint2D
}

Then use its data type in the Migration:

struct UserLocationMigration: Migration {
    func prepare(on database: Database) -> EventLoopFuture<Void> {
        return database.schema(UserLocation.schema)
            .field("id", .int, .identifier(auto: true))
            .field("location", GeometricPoint2D.dataType)
            .create()
    }
    func revert(on database: Database) -> EventLoopFuture<Void> {
        return database.schema(UserLocation.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 searchLocation = GeometricPoint2D(x: 1, y: 2)
try UserLocation.query(on: conn).filterGeometryDistanceWithin(\.$location, user.location, 1000).all().wait()
Queries
filterGeometryContains
filterGeometryCrosses
filterGeometryDisjoint
filterGeometryDistance
filterGeometryDistanceWithin
filterGeometryEquals
filterGeometryIntersects
filterGeometryOverlaps
filterGeometryTouches
filterGeometryWithin

💝 Contributing

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

✌️ License

MIT

👽 Author

Ricardo Carvalho - https://rabc.github.io/ Phil Larson - http://dizm.com

About

Swift PostGIS support for FluentPostgreSQL and Vapor

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 100.0%