PostGIS support for fluent-postgres-driver and Vapor 4
.package(url: "https://github.com/rafaellevy/fluent-postgis.git", .branch("master"))
Import module
import FluentPostGIS
Optionally, you can add a Migration
to enable PostGIS:
app.migrations.add(EnablePostGISMigration())
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 |
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 |
Please create an issue with a description of your problem or open a pull request with a fix.
MIT
Ricardo Carvalho - https://rabc.github.io/ Phil Larson - http://dizm.com