Skip to content

Instantly share code, notes, and snippets.

@nmichaud

nmichaud/2.folk Secret

Created August 21, 2023 23:10
Show Gist options
  • Save nmichaud/7e55347e8689b52c431c457f77c6272e to your computer and use it in GitHub Desktop.
Save nmichaud/7e55347e8689b52c431c457f77c6272e to your computer and use it in GitHub Desktop.
MapKit
# Title wish fulfillment
# for wishes of the form:
# "Wish $tag has title "This is a tag"
When /thing/ has region /region/ {
lassign $region vertices edges angle
lassign $vertices a b c d
set width [::vec2 distance $c $d]
lassign [vec2 scale [vec2 add $c $d] 0.5] x y
set y [expr {$y + 16}]
set radians [region angle $region]
When the collected matches for [list /someone/ wishes $thing has title /text/] are /matches/ {
set text [join [lmap match $matches {dict get $match text}] "\n"]
if {$text eq ""} { return }
set scale 1
Display::text $x $y $scale $text $radians
}
}
Wish $this has title "$this Humanitarian"
When $this points up with length 2 at /map/ {
Wish $map shows tilelayer "https://a.tile.openstreetmap.fr/hot/\$zoom/\$xtile/\$ytile.png" with options [list]
}
Wish $this has title "NYC map"
Wish $this is outlined white
set bbox [list -74.1160 40.7128 -73.9960 40.8128]
Claim $this is geomap of bbox $bbox
Wish $this has title "$this. Geomap Kit"
set zoom 12
set pi 3.14159
proc deg2num {lon lat zoom} {
set pi 3.14159
set n [expr {pow(2, $zoom)}]
set lon_deg $lon
set lat_rad [expr {$lat * ($pi / 180)}]
set xtile [expr {round($n * (($lon_deg + 180) / 360))}]
set ytile [expr {round($n * (1 - (log(tan($lat_rad) + (1 / cos($lat_rad))) / $pi)) / 2)}]
list $xtile $ytile
}
proc sanitizeURL {url} {
set url [string map {http:// "" https:// ""} $url]
set url [string map {/ _ ? "" * "" < "" > "" : "" | "" " " ""} $url]
if {[string length $url] > 255} {
set url [string range $url 0 254]
}
return $url
}
When when tile URL /url/ is downloaded to path /something/ /lambda/ with environment /e/ {
set sanitized [sanitizeURL $url]
set path [string map {".png" ".jpg"} "~/page-data/$sanitized"]
set path [file normalize $path]
if {[file exists $path]} {
set fileId [open $path "r"]
set data [read $fileId]
close $fileId
} else {
puts "curl $url | convert png:- jpg:$path"
[exec -ignorestderr curl $url | convert png:- jpg:$path ]
}
Claim tile URL $url is downloaded to path $path
}
proc get_url {template zoom xtile ytile} {
set url [subst $template]
set url
}
proc range {from to {step 1}} {
set res $from
if {$from > $to } {
set step [expr -1 * (abs($step))]
set mode "decreasing"
} else {
set mode "increasing"
}
while {$step>0?$to>$from:$to<$from} {
set incrCandidate [incr from $step]
if {$mode == "decreasing" && $incrCandidate >= $to} {
lappend res $incrCandidate
} elseif { $mode == "increasing" && $incrCandidate <= $to} {
lappend res $incrCandidate
}
}
return $res
}
When /someone/ wishes /page/ shows tilelayer /url_template/ with options /opts/ & \
/page/ wishes /target/ shows tilelayer /something/ with options /something/ {
if {$page != $target} {
#opts = rt.table(opts) -- Make a mutable copy of opts
#opts.priority = 1 + (opts.priority or 1)
Wish $target shows tilelayer $url_template with options $opts
puts "$page to $target"
}
}
When /someone/ wishes /map/ shows tilelayer /url_template/ with options /opts/ & \
/map/ is geomap of bbox /bbox/ & \
/map/ has region /r/ {
set width [region width $r]
lassign $bbox left bottom right top
#Made-up formula to guess a good zoom from width of bbox.
#set zoom [expr {log(360 / ($right - $left)) / log(2)}]
# experimentally determined width factor
#set zoom [expr {ceil($zoom + $width/16 - 1)}]
set max_zoom 17
if {$zoom > $max_zoom} { set zoom $max_zoom }
lassign [deg2num $left $top $zoom] xtile_left ytile_top
lassign [deg2num $right $bottom $zoom] xtile_right ytile_bottom
set tilesize [expr {$width / ($xtile_right - $xtile_left)}]
set tilesize 256
set halftile [expr {$tilesize / 2}]
set angle [region angle $r]
set c [list $halftile $halftile]
foreach xtile [range $xtile_left $xtile_right] {
foreach ytile [range $ytile_top $ytile_bottom] {
set url [get_url $url_template $zoom $xtile $ytile]
When tile URL $url is downloaded to path /path/ {
set x [expr {$tilesize * ($xtile - $xtile_left) + $halftile}]
set y [expr {$tilesize * ($ytile - $ytile_top) + $halftile}]
set v [list $x $y]
Wish $map displays image $path at location $v
}
}
}
}
When /someone/ wishes /p/ displays image /im/ at location /loc/ {
set im [image load $im]
When $p has region /r/ {
# Compute a scale for im that will fit in the region width/height
# Draw im with scale and rotation
set origin [lindex $r 0 3]
set angle [region angle $r]
set v [::vec2 rotate $loc [expr {$angle - 3.1415}]]
set pos [::vec2 sub $origin $v]
Wish display runs [list Display::image {*}$pos $im $angle 1]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment