Added --airport-geojson.

This commit is contained in:
John Wiseman 2020-06-03 15:57:06 -07:00
parent 9b65ea88dd
commit 421c43c044

View File

@ -267,21 +267,46 @@
v))) v)))
(defn closest-airport [config lat lon] (defn nearby-airports
(p/let [results (pelias/nearby (:pelias config) ([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 lat lon
{:categories "transport:air:aerodrome" {:categories "transport:air:aerodrome"
:boundary.circle.radius 7}) :boundary.circle.radius radius})
blocklist (get-in config [:airport :blocklist] []) blocklist (get-in config [:airport :blocklist] [])
blocklist-patterns (map #(re-pattern (str "(?i)" %)) blocklist) blocklist-patterns (map #(re-pattern (str "(?i)" %)) blocklist)]
_ (println blocklist-patterns)]
(->> (:features results) (->> (:features results)
(remove (fn [airport] (some #(re-find % (get-in airport [:properties :label])) (remove (fn [airport] (some #(re-find % (get-in airport [:properties :label]))
blocklist-patterns))) blocklist-patterns)))))))
(defn closest-airport [config lat lon]
(p/let [airports (nearby-airports config lat lon)]
(->> airports
(sort-by #(get-in % [:properties :distance])) (sort-by #(get-in % [:properties :distance]))
first))) first)))
(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)}}))
lat (:lat config)
lon (:lon config)
airports (nearby-airports config lat lon {:radius (+ (:radius-km config)
(:minimum-airport-distance-km config))})]
{:type "FeatureCollection"
:features (map airport->feature airports)}))
(defn log-table [table keys] (defn log-table [table keys]
(let [s (with-out-str (pprint/print-table keys table)) (let [s (with-out-str (pprint/print-table keys table))
lines (string/split-lines s)] lines (string/split-lines s)]
@ -638,13 +663,30 @@
(.option "--secrets <path>" "Path to the secrets yaml file" "secrets.yaml") (.option "--secrets <path>" "Path to the secrets yaml file" "secrets.yaml")
(.option "--history <path>" "Path to history/state file" "advisorycircular.json") (.option "--history <path>" "Path to history/state file" "advisorycircular.json")
(.option "--log-prefix <prefix>" "Log prefix to use") (.option "--log-prefix <prefix>" "Log prefix to use")
(.option "--airport-geojson" "Generate airport GEOJSON and exit")
(.parse (.-argv js/process))) (.parse (.-argv js/process)))
(logging/set-log-prefix! (or (.-logPrefix commander) "")) (logging/set-log-prefix! (or (.-logPrefix commander) ""))
(reset! log-prefix (or (.-logPrefix commander) "")) (reset! log-prefix (or (.-logPrefix commander) ""))
(if (.-airportGeojson commander)
(p/try
(p/let [base-config (if-let [config-path (.-config commander)]
(util/read-config config-path)
(if (fs/existsSync default-config-path)
(util/read-config default-config-path)
{}))
cli-config (build-config-from-commander commander)
config (build-config base-config cli-config {})
geojson (airport-geojson config)]
(println (.stringify js/JSON (clj->js geojson) nil " ")))
(p/catch :default e
(log-error "%s" e)
(log-error "%s" (.-stack e))
(.exit js/process 1)))
(let [start-time (current-time)] (let [start-time (current-time)]
;; If --config-path is specified, definitely try to read that ;; If --config-path is specified, definitely try to read that
;; file. Otherwise, only read config.yaml if it exists. ;; file. Otherwise, only read config.yaml if it exists.
(p/try (p/try
(println "OMG DOING THIS")
(p/let [base-config (if-let [config-path (.-config commander)] (p/let [base-config (if-let [config-path (.-config commander)]
(util/read-config config-path) (util/read-config config-path)
(if (fs/existsSync default-config-path) (if (fs/existsSync default-config-path)
@ -689,7 +731,7 @@
(p/catch :default e (p/catch :default e
(log-error "%s" e) (log-error "%s" e)
(log-error "%s" (.-stack e)) (log-error "%s" (.-stack e))
(.exit js/process 1))))) (.exit js/process 1))))))
;; (.on js/process "unhandledRejection" ;; (.on js/process "unhandledRejection"