这是 FluentPostGIS 包的一个分支,增加了对地理查询的支持。FluentPostGIS 为 fluent-postgres-driver 和 Vapor 4 提供了 PostGIS 支持。
在你的 Package.swift
文件中,将这行添加到依赖项:
.package(url: "https://github.com/brokenhandsio/fluent-postgis.git", from: "0.3.0")
然后将这行添加到目标(target)的依赖项:
.product(name: "FluentPostGIS", package: "fluent-postgis"),
导入模块
import FluentPostGIS
可选地,你可以添加一个 Migration
来启用 PostGIS
app.migrations.add(EnablePostGISMigration())
添加一个类型到你的模型
final class User: Model {
static let schema = "user"
@ID(key: .id)
var id: UUID?
@Field(key: "location")
var location: GeometricPoint2D
}
然后在 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()
}
}
几何类型 | 地理类型 |
---|---|
GeometricPoint2D | GeographicPoint2D |
GeometricLineString2D | GeographicLineString2D |
GeometricPolygon2D | GeographicPolygon2D |
GeometricMultiPoint2D | GeographicMultiPoint2D |
GeometricMultiLineString2D | GeographicMultiLineString2D |
GeometricMultiPolygon2D | GeographicMultiPolygon2D |
GeometricGeometryCollection2D | GeographicGeometryCollection2D |
使用任何过滤器函数进行查询
let eiffelTower = GeographicPoint2D(longitude: 2.2945, latitude: 48.858222)
try await User.query(on: database)
.filterGeographyDistanceWithin(\.$location, eiffelTower, 1000)
.all()
查询 |
---|
filterGeometryContains |
filterGeometryCrosses |
filterGeometryDisjoint |
filterGeometryDistance |
filterGeometryDistanceWithin |
filterGeographyDistanceWithin |
filterGeometryEquals |
filterGeometryIntersects |
filterGeometryOverlaps |
filterGeometryTouches |
filterGeometryWithin |
sortByDistance |
请创建一个 issue 描述你的问题,或者提交一个 pull request 来修复它。
MIT
BrokenHands, Tim Condon, Nikolai Guyot - https://www.brokenhands.io/ Ricardo Carvalho - https://rabc.github.io/ Phil Larson - http://dizm.com