From 9b784526af12f91ee4fa65a87d3044a634b5412e Mon Sep 17 00:00:00 2001 From: John Wiseman Date: Wed, 28 Oct 2020 20:46:31 -0700 Subject: [PATCH] Polygonize airport circles in geojson. Support add'l airports in config. --- package-lock.json | 31 ++++++++++++ package.json | 1 + src/main/lemondronor/advisorycircular.cljs | 57 ++++++++++++++-------- 3 files changed, 70 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index bc1ee69..096f9fb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,37 @@ "kuler": "^2.0.0" } }, + "@turf/circle": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/@turf/circle/-/circle-6.0.1.tgz", + "integrity": "sha512-pF9XsYtCvY9ZyNqJ3hFYem9VaiGdVNQb0SFq/zzDMwH3iWZPPJQHnnDB/3e8RD1VDtBBov9p5uO2k7otsfezjw==", + "requires": { + "@turf/destination": "6.x", + "@turf/helpers": "6.x" + } + }, + "@turf/destination": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/@turf/destination/-/destination-6.0.1.tgz", + "integrity": "sha512-MroK4nRdp7as174miCAugp8Uvorhe6rZ7MJiC9Hb4+hZR7gNFJyVKmkdDDXIoCYs6MJQsx0buI+gsCpKwgww0Q==", + "requires": { + "@turf/helpers": "6.x", + "@turf/invariant": "6.x" + } + }, + "@turf/helpers": { + "version": "6.1.4", + "resolved": "https://registry.npmjs.org/@turf/helpers/-/helpers-6.1.4.tgz", + "integrity": "sha512-vJvrdOZy1ngC7r3MDA7zIGSoIgyrkWcGnNIEaqn/APmw+bVLF2gAW7HIsdTxd12s5wQMqEpqIQrmrbRRZ0xC7g==" + }, + "@turf/invariant": { + "version": "6.1.2", + "resolved": "https://registry.npmjs.org/@turf/invariant/-/invariant-6.1.2.tgz", + "integrity": "sha512-WU08Ph8j0J2jVGlQCKChXoCtI50BB3yEH21V++V0T4cR1T27HKCxkehV2sYMwTierfMBgjwSwDIsxnR4/2mWXg==", + "requires": { + "@turf/helpers": "6.x" + } + }, "@types/mime-types": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/@types/mime-types/-/mime-types-2.1.0.tgz", diff --git a/package.json b/package.json index caaab5c..80c6dcb 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "shadow-cljs": "^2.11.5" }, "dependencies": { + "@turf/circle": "^6.0.1", "canvas": "^2.6.1", "commander": "^4.0.1", "geolib": "^3.2.0", diff --git a/src/main/lemondronor/advisorycircular.cljs b/src/main/lemondronor/advisorycircular.cljs index 62626d4..7937075 100644 --- a/src/main/lemondronor/advisorycircular.cljs +++ b/src/main/lemondronor/advisorycircular.cljs @@ -1,5 +1,6 @@ (ns lemondronor.advisorycircular (:require + ["@turf/circle" :default turf-circle] ["canvas" :as canvas] [cemerick.url :as c-url] ["commander" :as commander] @@ -418,20 +419,37 @@ v))) +;; Returns airports defined in the config. +(defn additional-airports [config lat lon] + ;; Get airports from config and annotate with :distance as though it + ;; was returned by a pelias query. + (map (fn [airport] + (let [a-lon (get-in airport [:geometry :coordinates 0]) + a-lat (get-in airport [:geometry :coordinates 1]) + distance (geo/distance {:lat lat :lon lon} + {:lat a-lat :lon a-lon})] + (assoc-in airport [:properties :distance] distance))) + (-> config :airports :additional))) + + (defn nearby-airports ([config lat lon] (nearby-airports config lat lon {})) ([config lat lon options] (p/let [radius (or (:radius options) 7) - results (pelias/nearby (:pelias config) - lat lon - {:categories "transport:air:aerodrome" - :boundary.circle.radius radius}) + pelias-results (pelias/nearby (:pelias config) + lat lon + {:categories "transport:air:aerodrome" + :boundary.circle.radius radius}) + additional-results (additional-airports config lat lon) + _ (log-info "Pelias airport results: %s" pelias-results) + _ (log-info "Additional airports from config: %s" additional-results) + results (concat (:features pelias-results) additional-results) blocklist (get-in config [:airport :blocklist] []) blocklist-patterns (map #(re-pattern (str "(?i)" %)) blocklist)] - (->> (:features results) - (remove (fn [airport] (some #(re-find % (get-in airport [:properties :label])) - blocklist-patterns))))))) + (remove (fn [airport] (some #(re-find % (get-in airport [:properties :label])) + blocklist-patterns)) + results)))) (defn closest-airport [config lat lon] @@ -444,12 +462,14 @@ (defn airport-geojson [config] (p/let [airport->feature (fn [a] (let [props (:properties a)] - {:type "Feature" - :properties {:shape "Circle" - :radius (* 1000 (:minimum-airport-distance-km config)) - :name (:label props)} - :geometry {:type "Point" - :coordinates (-> a :geometry :coordinates)}})) + (js->clj + (turf-circle (clj->js (-> a :geometry :coordinates)) + (:minimum-airport-distance-km config) + (clj->js + {:units "kilometers" + :properties {:type "airport" + :name (:label props)}})) + :keywordize-keys true))) lat (:lat config) lon (:lon config) airports (nearby-airports config lat lon {:radius (+ (:radius-km config) @@ -457,12 +477,11 @@ {:type "FeatureCollection" :features (conj (map airport->feature airports) - {:type "Feature" - :properties {:shape "Circle" - :radius (* 1000 (:radius-km config)) - :name "Center"} - :geometry {:type "Point" - :coordinates [(:lon config) (:lat config)]}})})) + (turf-circle (clj->js [(:lon config) (:lat config)]) + (:radius-km config) + (clj->js + {:units "kilometers" + :properties {:type "coverage"}})))})) (defn log-table [table keys]