Last active
December 1, 2025 07:44
-
-
Save afarnham/38643a528af3bd5f045bc7f1204422b7 to your computer and use it in GitHub Desktop.
Houston Train Watch data
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
| import Foundation | |
| import MapKit | |
| struct CrossingProps: Decodable { | |
| let crossingStatus: String | |
| let street: String? | |
| let code: String? | |
| let timeUpdated: Double? // epoch ms | |
| let startTime: Double? | |
| let endTime: Double? | |
| let direction: String? | |
| let incidentType: String? | |
| let trainMovement: String? | |
| } | |
| func fetchAllCrossings() async throws -> [MKGeoJSONFeature] { | |
| var comps = URLComponents(string: | |
| "https://services.arcgis.com/NummVBqZSIJKUeVR/arcgis/rest/services/Train_CrossingStatus_Public_Current/FeatureServer/0/query")! | |
| comps.queryItems = [ | |
| .init(name: "where", value: "crossingStatus IN ('blocked','clear')"), | |
| .init(name: "outFields", value: "*"), | |
| .init(name: "outSR", value: "4326"), | |
| .init(name: "f", value: "geojson") | |
| ] | |
| let (data, _) = try await URLSession.shared.data(from: comps.url!) | |
| let features = try MKGeoJSONDecoder() | |
| .decode(data) | |
| .compactMap { $0 as? MKGeoJSONFeature } | |
| return features | |
| } | |
| Task { | |
| let crossings = try! await fetchAllCrossings() | |
| //See all properties on each crossing | |
| let decodedProps = crossings.compactMap { | |
| if let prop = $0.properties { | |
| //tried using CrossingProps to decode here, but got errors. Too lazy to fix at the moment | |
| return String(data: prop, encoding: .utf8)! | |
| } | |
| return nil | |
| } | |
| print(decodedProps) | |
| //Print crossing map geometry | |
| for crossing in allCrossings { | |
| crossing.geometry.forEach { | |
| let anno = $0 as! MKPointAnnotation | |
| print(anno.coordinate) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment