2023-03-26 09:08:08 -07:00
|
|
|
//
|
|
|
|
|
// PersistenceEntityExtenstion.swift
|
|
|
|
|
// Meshtastic
|
|
|
|
|
//
|
|
|
|
|
// Copyright(c) Garth Vander Houwen 11/28/21.
|
|
|
|
|
//
|
|
|
|
|
|
2021-12-16 14:13:54 -08:00
|
|
|
import CoreData
|
|
|
|
|
import CoreLocation
|
|
|
|
|
import MapKit
|
2024-06-07 22:09:20 -05:00
|
|
|
import MeshtasticProtobufs
|
2021-12-16 14:13:54 -08:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
|
|
extension PositionEntity {
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2025-10-05 17:51:18 -07:00
|
|
|
@MainActor
|
|
|
|
|
static func allPositionsFetchRequest() -> NSFetchRequest<PositionEntity> {
|
|
|
|
|
|
2024-03-23 09:01:44 -07:00
|
|
|
let request: NSFetchRequest<PositionEntity> = PositionEntity.fetchRequest()
|
2024-03-23 18:01:20 -07:00
|
|
|
request.sortDescriptors = [NSSortDescriptor(key: "time", ascending: false)]
|
2025-10-05 17:51:18 -07:00
|
|
|
let positionPredicate = NSPredicate(format: "nodePosition != nil AND nodePosition.user != nil AND latest == true AND nodePosition.user.shortName != ''")
|
|
|
|
|
request.predicate = positionPredicate
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2025-10-05 17:51:18 -07:00
|
|
|
// Distance Predicate
|
|
|
|
|
if let cl = LocationsHandler.currentLocation {
|
|
|
|
|
|
2024-05-29 16:40:07 -05:00
|
|
|
let d: Double = UserDefaults.meshMapDistance * 1.1
|
2025-10-05 17:51:18 -07:00
|
|
|
let r: Double = 6371009 // Earth's mean radius in meters
|
|
|
|
|
|
|
|
|
|
// Calculate Bounding Box
|
|
|
|
|
let meanLatitidue = cl.latitude * .pi / 180
|
2024-05-29 16:40:07 -05:00
|
|
|
let deltaLatitude = d / r * 180 / .pi
|
|
|
|
|
let deltaLongitude = d / (r * cos(meanLatitidue)) * 180 / .pi
|
2025-10-05 17:51:18 -07:00
|
|
|
|
|
|
|
|
let minLatitude: Double = cl.latitude - deltaLatitude
|
|
|
|
|
let maxLatitude: Double = cl.latitude + deltaLatitude
|
|
|
|
|
let minLongitude: Double = cl.longitude - deltaLongitude
|
|
|
|
|
let maxLongitude: Double = cl.longitude + deltaLongitude
|
|
|
|
|
|
|
|
|
|
// Scale bounding box values by 1e7 and use integer attributes (longitudeI, latitudeI)
|
|
|
|
|
let scale: Double = 1e7
|
|
|
|
|
let minLongitudeI = Int(minLongitude * scale)
|
|
|
|
|
let maxLongitudeI = Int(maxLongitude * scale)
|
|
|
|
|
let minLatitudeI = Int(minLatitude * scale)
|
|
|
|
|
let maxLatitudeI = Int(maxLatitude * scale)
|
|
|
|
|
|
|
|
|
|
// Use integer comparison in the predicate
|
|
|
|
|
let distancePredicate = NSPredicate(format: "(%ld <= longitudeI) AND (longitudeI <= %ld) AND (%ld <= latitudeI) AND (latitudeI <= %ld)",
|
|
|
|
|
minLongitudeI, maxLongitudeI, minLatitudeI, maxLatitudeI)
|
|
|
|
|
|
2024-03-24 13:13:05 -07:00
|
|
|
request.predicate = NSCompoundPredicate(type: .and, subpredicates: [positionPredicate, distancePredicate])
|
2024-03-24 11:45:49 -07:00
|
|
|
}
|
2025-10-05 17:51:18 -07:00
|
|
|
|
2024-03-23 09:01:44 -07:00
|
|
|
return request
|
|
|
|
|
}
|
2021-12-16 14:13:54 -08:00
|
|
|
var latitude: Double? {
|
|
|
|
|
|
|
|
|
|
let d = Double(latitudeI)
|
|
|
|
|
if d == 0 {
|
2021-12-18 20:49:50 -08:00
|
|
|
return 0
|
2021-12-16 14:13:54 -08:00
|
|
|
}
|
|
|
|
|
return d / 1e7
|
|
|
|
|
}
|
2021-12-25 23:48:12 -08:00
|
|
|
|
2021-12-16 14:13:54 -08:00
|
|
|
var longitude: Double? {
|
|
|
|
|
|
|
|
|
|
let d = Double(longitudeI)
|
|
|
|
|
if d == 0 {
|
2021-12-18 20:49:50 -08:00
|
|
|
return 0
|
2021-12-16 14:13:54 -08:00
|
|
|
}
|
|
|
|
|
return d / 1e7
|
|
|
|
|
}
|
2021-12-25 23:48:12 -08:00
|
|
|
|
2023-01-10 06:49:19 -08:00
|
|
|
var nodeCoordinate: CLLocationCoordinate2D? {
|
2021-12-20 22:29:28 -08:00
|
|
|
if latitudeI != 0 && longitudeI != 0 {
|
2021-12-16 14:13:54 -08:00
|
|
|
let coord = CLLocationCoordinate2D(latitude: latitude!, longitude: longitude!)
|
|
|
|
|
return coord
|
|
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2023-01-27 20:22:17 -08:00
|
|
|
var nodeLocation: CLLocation? {
|
|
|
|
|
if latitudeI != 0 && longitudeI != 0 {
|
|
|
|
|
let location = CLLocation(latitude: latitude!, longitude: longitude!)
|
|
|
|
|
return location
|
|
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2021-12-16 14:13:54 -08:00
|
|
|
var annotaton: MKPointAnnotation {
|
|
|
|
|
let pointAnn = MKPointAnnotation()
|
2023-01-10 06:49:19 -08:00
|
|
|
if nodeCoordinate != nil {
|
|
|
|
|
pointAnn.coordinate = nodeCoordinate!
|
2021-12-16 14:13:54 -08:00
|
|
|
}
|
|
|
|
|
return pointAnn
|
|
|
|
|
}
|
2025-10-21 09:23:22 -04:00
|
|
|
|
|
|
|
|
var isPreciseLocation: Bool {
|
|
|
|
|
precisionBits == 32 || precisionBits == 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var fuzzedNodeCoordinate: CLLocationCoordinate2D? {
|
|
|
|
|
// With reduced precisionBits, many nodes can overlap on the map, making them unclickable.
|
|
|
|
|
// Use a hash of the position ID to fuzz coordinate slightly so that these nodes can be distinguished at the higest zoom levels. This allows them to be clicked individually.
|
|
|
|
|
if latitudeI != 0 && longitudeI != 0 {
|
|
|
|
|
// Derive two uniform pseudorandom numbers [0,1) from id.hashValue
|
|
|
|
|
let u1 = Double(id.hashValue & 0xFFFF) / 65536.0
|
|
|
|
|
let u2 = Double((id.hashValue >> 16) & 0xFFFF) / 65536.0
|
|
|
|
|
|
|
|
|
|
// Angle and radius
|
|
|
|
|
let offsetAngle = 2.0 * .pi * u1
|
|
|
|
|
let offsetRadius = 0.00001 * sqrt(u2) // 1.0e-5 degrees at equator is about 1.11 m or 4 ft
|
|
|
|
|
|
|
|
|
|
let dLat = sin(offsetAngle) * offsetRadius
|
|
|
|
|
let dLon = cos(offsetAngle) * offsetRadius
|
|
|
|
|
|
|
|
|
|
let coord = CLLocationCoordinate2D(
|
|
|
|
|
latitude: latitude! + dLat,
|
|
|
|
|
longitude: longitude! + dLon
|
|
|
|
|
)
|
|
|
|
|
return coord
|
|
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-16 14:13:54 -08:00
|
|
|
}
|
2023-01-10 06:49:19 -08:00
|
|
|
|
|
|
|
|
extension PositionEntity: MKAnnotation {
|
2025-01-21 09:19:14 -08:00
|
|
|
public var coordinate: CLLocationCoordinate2D { nodeCoordinate ?? LocationsHandler.DefaultLocation }
|
2025-10-21 09:23:22 -04:00
|
|
|
public var fuzzedCoordinate: CLLocationCoordinate2D { fuzzedNodeCoordinate ?? LocationsHandler.DefaultLocation }
|
2025-04-27 16:19:10 -07:00
|
|
|
public var title: String? { nodePosition?.user?.shortName ?? "Unknown".localized }
|
2023-01-11 13:53:50 -08:00
|
|
|
public var subtitle: String? { time?.formatted() }
|
2023-01-10 06:49:19 -08:00
|
|
|
}
|