Created
August 5, 2018 05:42
-
-
Save alex-hhh/7304c2a09bf1b7ec8514c2523a827b05 to your computer and use it in GitHub Desktop.
df map demo
This file contains 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
#lang racket/gui | |
(require data-frame map-widget mrlib/snip-canvas plot) | |
(define toplevel (new frame% [label "Map Demo"] [width 600] [height 400])) | |
(define map (new map-widget% [parent toplevel])) | |
(send toplevel show #t) | |
;; Load the GPX file in a data frame | |
(define df (df-read/gpx "./tarn-shelf.gpx")) | |
(df-describe df) | |
;; Add the GPS track to the map | |
(send map add-track (df-select* df "lat" "lon") 'track) | |
(send map center-map) | |
;; Find the locations of the mile markers... | |
(define total-distance (df-ref df (sub1 (df-row-count df)) "dst")) | |
(define marker-locations | |
(for/list ([mile (in-range 0 total-distance 1609)]) | |
(df-lookup df "dst" '("lat" "lon") mile))) | |
;; And add them to the map too | |
(define marker-color (make-color 0 135 36)) | |
(for ([(position index) (in-indexed marker-locations)]) | |
(send map add-marker position (format "Mile ~a" index) 1 marker-color)) | |
(define (make-elevation-profile-plot df map) | |
(send map set-track-current-location #t) | |
(define (plot-callback snip event x y) | |
(define-values (overlays current-location) | |
(if (and x y (eq? (send event get-event-type) 'motion)) | |
(values | |
(let ((elevation (df-lookup/interpolated df "dst" "alt" x))) | |
(list (vrule x #:style 'long-dash) | |
(point-label (vector x y) | |
(format "~a miles, height: ~a meters" | |
(~r (/ x 1609) #:precision 2) | |
(~r elevation #:precision 1)) | |
#:anchor 'auto))) | |
(df-lookup/interpolated df "dst" '("lat" "lon") x)) | |
(values '() #f))) | |
(send snip set-overlay-renderers overlays) | |
(send map set-current-location current-location)) | |
(define elevation (df-select* df "dst" "alt")) | |
(define (make-snip width height) | |
(parameterize ([plot-x-label "Distance (miles)"] | |
[plot-y-label "Elevation (meters)"] | |
[plot-x-ticks | |
(ticks (linear-ticks-layout) | |
(lambda (min max pre-ticks) | |
(for/list ([pt pre-ticks]) | |
(~a (exact-truncate (/ (pre-tick-value pt) 1609))))))]) | |
(define snip (plot-snip (lines elevation) #:width width #:height height)) | |
(send snip set-mouse-event-callback plot-callback) | |
snip)) | |
(define frame (new frame% [label "Elevation Plot"] [width 600] [height 300])) | |
(define canvas (new snip-canvas% [parent frame] [make-snip make-snip])) | |
(send frame show #t) | |
frame) | |
(make-elevation-profile-plot df map) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment