From 41d00d5707ccc5b689958af56f4358738f8e24d6 Mon Sep 17 00:00:00 2001 From: John Wiseman Date: Thu, 23 Jul 2020 16:54:42 -0700 Subject: [PATCH] Map attribution WIP. --- package-lock.json | 38 +++++++++++++++++++ package.json | 1 + src/main/lemondronor/advisorycircular.cljs | 43 ++++++++++++++++++++-- 3 files changed, 78 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index ee656a2..2cd2075 100644 --- a/package-lock.json +++ b/package-lock.json @@ -278,6 +278,16 @@ "integrity": "sha1-hZgoeOIbmOHGZCXgPQF0eI9Wnug=", "dev": true }, + "canvas": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/canvas/-/canvas-2.6.1.tgz", + "integrity": "sha512-S98rKsPcuhfTcYbtF53UIJhcbgIAK533d1kJKMwsMwAIFgfd58MOyxRud3kktlzWiEkFliaJtvyZCBtud/XVEA==", + "requires": { + "nan": "^2.14.0", + "node-pre-gyp": "^0.11.0", + "simple-get": "^3.0.3" + } + }, "caseless": { "version": "0.12.0", "resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz", @@ -476,6 +486,14 @@ "ms": "^2.1.1" } }, + "decompress-response": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/decompress-response/-/decompress-response-4.2.1.tgz", + "integrity": "sha512-jOSne2qbyE+/r8G1VU+G/82LBs2Fs4LAsTiLSHOCOMZQl2OKZ6i8i4IyHemTe+/yIXOtTcRQMzPcgyhoFlqPkw==", + "requires": { + "mimic-response": "^2.0.0" + } + }, "deep-extend": { "version": "0.6.0", "resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz", @@ -1026,6 +1044,11 @@ "mime-db": "1.42.0" } }, + "mimic-response": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-2.1.0.tgz", + "integrity": "sha512-wXqjST+SLt7R009ySCglWBCFpjUygmCIfD790/kVbiGmUgfYGuB14PiTd5DwVxSV4NcYHjzMkoj5LjQZwTQLEA==" + }, "minimalistic-assert": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/minimalistic-assert/-/minimalistic-assert-1.0.1.tgz", @@ -1588,6 +1611,21 @@ "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz", "integrity": "sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0=" }, + "simple-concat": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/simple-concat/-/simple-concat-1.0.1.tgz", + "integrity": "sha512-cSFtAPtRhljv69IK0hTVZQ+OfE9nePi/rtJmw5UjHeVyVroEqJXP1sFztKUy1qU+xvz3u/sfYJLa947b7nAN2Q==" + }, + "simple-get": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/simple-get/-/simple-get-3.1.0.tgz", + "integrity": "sha512-bCR6cP+aTdScaQCnQKbPKtJOKDp/hj9EDLJo3Nw4y1QksqaovlW/bnptB6/c1e+qmNIDHRK+oXFDdEqBT8WzUA==", + "requires": { + "decompress-response": "^4.2.0", + "once": "^1.3.1", + "simple-concat": "^1.0.0" + } + }, "simple-swizzle": { "version": "0.2.2", "resolved": "https://registry.npmjs.org/simple-swizzle/-/simple-swizzle-0.2.2.tgz", diff --git a/package.json b/package.json index 711db4d..cad71b7 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "shadow-cljs": "^2.8.109" }, "dependencies": { + "canvas": "^2.6.1", "commander": "^4.0.1", "geolib": "^3.2.0", "js-yaml": "^3.13.1", diff --git a/src/main/lemondronor/advisorycircular.cljs b/src/main/lemondronor/advisorycircular.cljs index 3d0ba24..6ad1093 100644 --- a/src/main/lemondronor/advisorycircular.cljs +++ b/src/main/lemondronor/advisorycircular.cljs @@ -1,5 +1,6 @@ (ns lemondronor.advisorycircular (:require + ["canvas" :as canvas] ["commander" :as commander] ["fs" :as fs] [cljs.pprint :as pprint] @@ -636,7 +637,7 @@ (p/let [nearby (landmark config lat lon) ;;_ (log-info "WOOO %s" nearby) ac-db-rec (if-let [ac-info-db-path (:aircraft-info-db-path config)] - (get-aircraft-info-record icao ac-info-db-path))] + (get-aircraft-info-record icao ac-info-db-path))] (let [description (generate-description ac ac-db-rec coarse nearby)] (log-info "%s Description: %s" (:icao ac) description) description))]) @@ -743,8 +744,18 @@ (def default-config-path "config.yaml") + +(defn write-stream-to-file [stream path] + (p/promise + [resolve] + (let [out (fs/createWriteStream path)] + (.pipe stream out) + (.on out "finish" #(do (println "resolving") (resolve nil))) + (.on out "error" #(throw %))))) + + (defn main [& args] - (-> commander + (-> commander (.option "--lat " "Latitude of the circle of region of interest" parse-number) (.option "--lon " "Longitude of the circle of the region of interest" parse-number) (.option "--adsbx-url " "ADSBX API url") @@ -767,6 +778,30 @@ (logging/set-log-prefix! (or (.-logPrefix commander) "")) (reset! log-prefix (or (.-logPrefix commander) "")) (p/try + (p/let [path (nth (.-argv js/process) 2) + _ (println path) + image (canvas/loadImage path) + canvas (canvas/createCanvas (.-width image) (.-height image)) + ctx (.getContext canvas "2d")] + (.drawImage ctx image 0 0) + (set! (.-font ctx) "12px Impact") + (let [attribution "Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community" ;; "© OpenStreetMap contributors" + text-metrics (.measureText ctx attribution) + w (.-width text-metrics) + h 12 + [mw mh] [1 1] + [pw ph] [2 2] + text-x (- (.-width image) w pw mw) + text-y (- (.-height image) h ph mh )] + (.beginPath ctx) + (set! (.-fillStyle ctx) "rgba(128,128,128,0.7)") + (.rect ctx (- text-x pw) (- text-y ph) (+ w pw pw) (+ h ph ph)) + (.fill ctx) + (set! (.-fillStyle ctx) "black") + (.fillText ctx attribution text-x (+ text-y h))) + (write-stream-to-file (.createPNGStream canvas) "out.png") + (println "DONE!") + (.exit js/process 0)) (cond (.-createAircraftInfoDbFromJson commander) (create-aircraft-info-db (.-createAircraftInfoDbFromJson commander) (.-aircraftInfoDb commander)) @@ -830,8 +865,8 @@ (map #(str (:icao %) ":" (.toFixed (:curviness %) 0))) (string/join " ")))))))) (p/catch :default e - (log-error "%s" e) - (log-error "%s" (.-stack e)) + (log-error "wee1 %s" e) + (log-error "wee2 %s" (.-stack e)) (.exit js/process 1))))