-
Notifications
You must be signed in to change notification settings - Fork 0
Add near and withinBounds filters with local filtering support #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
Sources/StreamCore/OpenAPI/Query/FilterValue+Location.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| // | ||
| // Copyright © 2025 Stream.io Inc. All rights reserved. | ||
| // | ||
|
|
||
| import CoreLocation | ||
| import Foundation | ||
|
|
||
| /// A circular geographic region defined by a center point and a radius. | ||
| /// | ||
| /// Use `CircularRegion` to represent a circular area on Earth's surface, typically used for location-based | ||
| /// queries such as finding all points within a certain distance from a center coordinate. | ||
| /// | ||
| /// ## Example | ||
| /// ```swift | ||
| /// let center = CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194) | ||
| /// let region = CircularRegion(center: center, radiusInMeters: 5000) | ||
| /// ``` | ||
| public struct CircularRegion { | ||
| /// The center coordinate of the circular region. | ||
| public let center: CLLocationCoordinate2D | ||
|
|
||
| /// The radius of the circular region in meters. | ||
| public let radius: Double | ||
|
|
||
| /// Creates a circular region with the specified center and radius. | ||
| /// | ||
| /// - Parameters: | ||
| /// - center: The center coordinate of the circular region. | ||
| /// - radiusInMeters: The radius of the region in meters. | ||
| public init(center: CLLocationCoordinate2D, radiusInMeters: Double) { | ||
| self.center = center | ||
| self.radius = radiusInMeters | ||
| } | ||
|
|
||
| /// Creates a circular region with the specified center and radius. | ||
| /// | ||
| /// - Parameters: | ||
| /// - center: The center coordinate of the circular region. | ||
| /// - radiusInKM: The radius of the region in kilometers. This value is automatically converted to meters. | ||
| public init(center: CLLocationCoordinate2D, radiusInKM: Double) { | ||
| self.center = center | ||
| self.radius = radiusInKM * 1000.0 | ||
| } | ||
| } | ||
|
|
||
| extension CircularRegion: FilterValue { | ||
| static let rawJSONDistanceInKmKey = "distance" | ||
|
|
||
| init?(from rawJSON: [String: RawJSON]) { | ||
| guard let center = CLLocationCoordinate2D(from: rawJSON) else { return nil } | ||
| guard let distanceInKm = rawJSON[Self.rawJSONDistanceInKmKey]?.numberValue else { return nil } | ||
| self.center = center | ||
| self.radius = distanceInKm * 1000.0 | ||
| } | ||
|
|
||
| public var rawJSON: RawJSON { | ||
| [ | ||
| CLLocationCoordinate2D.rawJSONLatitudeKey: .number(center.latitude), | ||
| CLLocationCoordinate2D.rawJSONLongitudeKey: .number(center.longitude), | ||
| Self.rawJSONDistanceInKmKey: .number(radius / 1000.0) | ||
| ] | ||
| } | ||
|
|
||
| func contains(_ coordinate: CLLocationCoordinate2D) -> Bool { | ||
| let centerLocation = CLLocation(latitude: center.latitude, longitude: center.longitude) | ||
| let coordinateLocation = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude) | ||
| let distance = centerLocation.distance(from: coordinateLocation) | ||
| return distance <= radius | ||
| } | ||
| } | ||
|
|
||
| /// A rectangular geographic region defined by northeast and southwest corner coordinates. | ||
| /// | ||
| /// Use `BoundingBox` to represent a rectangular area on Earth's surface, typically used for location-based | ||
| /// queries such as finding all points within a specific geographic boundary. | ||
| /// | ||
| /// The bounding box is defined by two corner coordinates: | ||
| /// - `northeast`: The northeast (top-right) corner of the rectangle | ||
| /// - `southwest`: The southwest (bottom-left) corner of the rectangle | ||
| /// | ||
| /// ## Example | ||
| /// ```swift | ||
| /// let ne = CLLocationCoordinate2D(latitude: 40.7128, longitude: -74.0060) | ||
| /// let sw = CLLocationCoordinate2D(latitude: 40.7580, longitude: -73.9855) | ||
| /// let boundingBox = BoundingBox(northeast: ne, southwest: sw) | ||
| /// ``` | ||
| public struct BoundingBox { | ||
| /// The northeast (top-right) corner coordinate of the bounding box. | ||
| public let northeast: CLLocationCoordinate2D | ||
|
|
||
| /// The southwest (bottom-left) corner coordinate of the bounding box. | ||
| public let southwest: CLLocationCoordinate2D | ||
|
|
||
| /// Creates a bounding box with the specified corner coordinates. | ||
| /// | ||
| /// - Parameters: | ||
| /// - northeast: The northeast (top-right) corner coordinate. | ||
| /// - southwest: The southwest (bottom-left) corner coordinate. | ||
| public init(northeast: CLLocationCoordinate2D, southwest: CLLocationCoordinate2D) { | ||
| self.northeast = northeast | ||
| self.southwest = southwest | ||
| } | ||
| } | ||
|
|
||
| extension BoundingBox: FilterValue { | ||
| static let rawJSONNortheastLatitudeKey = "ne_lat" | ||
| static let rawJSONNortheastLongitudeKey = "ne_lng" | ||
| static let rawJSONSouthwestLatitudeKey = "sw_lat" | ||
| static let rawJSONSouthwestLongitudeKey = "sw_lng" | ||
|
|
||
| init?(from rawJSON: [String: RawJSON]) { | ||
| guard let neLatitude = rawJSON[Self.rawJSONNortheastLatitudeKey]?.numberValue else { return nil } | ||
| guard let neLongitude = rawJSON[Self.rawJSONNortheastLongitudeKey]?.numberValue else { return nil } | ||
| guard let swLatitude = rawJSON[Self.rawJSONSouthwestLatitudeKey]?.numberValue else { return nil } | ||
| guard let swLongitude = rawJSON[Self.rawJSONSouthwestLongitudeKey]?.numberValue else { return nil } | ||
| northeast = CLLocationCoordinate2D(latitude: neLatitude, longitude: neLongitude) | ||
| southwest = CLLocationCoordinate2D(latitude: swLatitude, longitude: swLongitude) | ||
| } | ||
|
|
||
| public var rawJSON: RawJSON { | ||
| [ | ||
| Self.rawJSONNortheastLatitudeKey: .number(northeast.latitude), | ||
| Self.rawJSONNortheastLongitudeKey: .number(northeast.longitude), | ||
| Self.rawJSONSouthwestLatitudeKey: .number(southwest.latitude), | ||
| Self.rawJSONSouthwestLongitudeKey: .number(southwest.longitude) | ||
| ] | ||
| } | ||
|
|
||
| func contains(_ coordinate: CLLocationCoordinate2D) -> Bool { | ||
| guard coordinate.latitude >= southwest.latitude && coordinate.latitude <= northeast.latitude else { return false } | ||
| guard coordinate.longitude >= southwest.longitude && coordinate.longitude <= northeast.longitude else { return false } | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| extension CLLocationCoordinate2D: FilterValue { | ||
| static let rawJSONLatitudeKey = "lat" | ||
| static let rawJSONLongitudeKey = "lng" | ||
|
|
||
| init?(from rawJSON: [String: RawJSON]) { | ||
| guard let latitude = rawJSON[CLLocationCoordinate2D.rawJSONLatitudeKey]?.numberValue else { return nil } | ||
| guard let longitude = rawJSON[CLLocationCoordinate2D.rawJSONLongitudeKey]?.numberValue else { return nil } | ||
| self.init(latitude: latitude, longitude: longitude) | ||
| } | ||
|
|
||
| public var rawJSON: RawJSON { | ||
| .dictionary([ | ||
| Self.rawJSONLatitudeKey: .number(latitude), | ||
| Self.rawJSONLongitudeKey: .number(longitude) | ||
| ]) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is used for API requests in the Feeds SDK.
rawJSONabove comes from theFilterValueprotocol and is used bytoRawJSONDictionary. All in all,toRawJSONDictionaryis just a convenience for API requests.rawJSONwas moved to the protocol since then it is better to manage conformance of all the types. Discovered some mapping issues likeIntwas not correctly encoded toRawJSONthanks to this change (there was a big switch statement before which).