Skip to content

Commit

Permalink
Add basic geographic support
Browse files Browse the repository at this point in the history
  • Loading branch information
0xTim committed Sep 14, 2019
1 parent 0da859f commit ffcca3a
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Sources/FluentPostGIS/Queries/QueryBuilder+DistanceWithin.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import FluentPostgreSQL
import WKCodable

extension QueryBuilder where
Database: QuerySupporting,
Expand Down Expand Up @@ -39,6 +40,18 @@ extension QueryBuilder where
return self.filter(custom: Database.queryFilterGeometryDistanceWithin(field, filter, value))
}

@discardableResult
public func filterGeographyDistanceWithin<T,V>(_ key: KeyPath<Result, T>, _ filter: V, _ value: Double) -> Self
where T: GeometryConvertible, V: GeometryConvertible
{
return filterGeographyDistanceWithin(Database.queryField(.keyPath(key)), Database.queryFilterValueGeographic(filter), Database.queryFilterValue([value]))
}

@discardableResult
private func filterGeographyDistanceWithin(_ field: Database.QueryField, _ filter: Database.QueryFilterValue, _ value: Database.QueryFilterValue) -> Self {
return self.filter(custom: Database.filterGeographyDistanceWithin(field, filter, value))
}

}

extension QuerySupporting where
Expand All @@ -56,3 +69,26 @@ extension QuerySupporting where
return .function("ST_DWithin", args)
}
}

extension QuerySupporting where QueryFilterValue: SQLExpression {
public static func queryFilterValueGeographic<T: GeometryConvertible>(_ geometry: T) -> QueryFilterValue {
let geometryText = WKTEncoder().encode(geometry.geometry)
return .function("ST_GeogFromText", [.expression(.literal(.string(geometryText)))])
}
}

extension QuerySupporting where
QueryFilter: SQLExpression,
QueryField == QueryFilter.ColumnIdentifier,
QueryFilterMethod == QueryFilter.BinaryOperator,
QueryFilterValue == QueryFilter
{
public static func filterGeographyDistanceWithin(_ field: QueryField, _ filter: QueryFilterValue, _ value: QueryFilterValue) -> QueryFilter {
let args: [QueryFilter.Function.Argument] = [
GenericSQLFunctionArgument<PostgreSQLExpression>.expression(PostgreSQLExpression.column(field as! PostgreSQLColumnIdentifier)),
GenericSQLFunctionArgument<PostgreSQLExpression>.expression(filter as! PostgreSQLExpression),
GenericSQLFunctionArgument<PostgreSQLExpression>.expression(value as! PostgreSQLExpression),
] as! [QueryFilter.Function.Argument]
return .function("ST_DWithin", args)
}
}

0 comments on commit ffcca3a

Please sign in to comment.