Inline map data now works too.
This commit is contained in:
parent
20507ba5f3
commit
392a225691
|
@ -5,10 +5,6 @@ body {
|
||||||
padding-bottom: 4em;
|
padding-bottom: 4em;
|
||||||
}
|
}
|
||||||
|
|
||||||
div#app {
|
|
||||||
margin-left: 10%;
|
|
||||||
}
|
|
||||||
|
|
||||||
footer {
|
footer {
|
||||||
clear: both;
|
clear: both;
|
||||||
font-size: smaller;
|
font-size: smaller;
|
||||||
|
@ -33,7 +29,6 @@ footer a {
|
||||||
|
|
||||||
|
|
||||||
h1, h2, h3, h4, h5, h6 {
|
h1, h2, h3, h4, h5, h6 {
|
||||||
width: 100%;
|
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 0.5em 10%;
|
padding: 0.5em 10%;
|
||||||
text-align: left;
|
text-align: left;
|
||||||
|
@ -45,6 +40,14 @@ samp {
|
||||||
background-color: #b0b0ff;
|
background-color: #b0b0ff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#app {
|
||||||
|
margin-left: 10%;
|
||||||
|
}
|
||||||
|
|
||||||
|
#app h2 {
|
||||||
|
width: 60%;
|
||||||
|
}
|
||||||
|
|
||||||
#error
|
#error
|
||||||
{
|
{
|
||||||
background-color: maroon;
|
background-color: maroon;
|
||||||
|
@ -60,3 +63,7 @@ samp {
|
||||||
margin: 0px;
|
margin: 0px;
|
||||||
padding-left: 10%;
|
padding-left: 10%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.leaflet-popup-content h5 {
|
||||||
|
width: 80%;
|
||||||
|
}
|
||||||
|
|
|
@ -21,6 +21,15 @@ crossorigin=""/ -->
|
||||||
<p id="message"></p>
|
<p id="message"></p>
|
||||||
<p id="error"></p>
|
<p id="error"></p>
|
||||||
<div id="app">
|
<div id="app">
|
||||||
|
<p>
|
||||||
|
</p>
|
||||||
|
<h2>
|
||||||
|
Map using inline CSV
|
||||||
|
</h2>
|
||||||
|
<div id="inline-csv-map" style="height: 600px; width: 80%; border: thin solid gray;"></div>
|
||||||
|
<h2>
|
||||||
|
Map using CSV from URL
|
||||||
|
</h2>
|
||||||
<div id="map" style="height: 600px; width: 80%; border: thin solid gray;"></div>
|
<div id="map" style="height: 600px; width: 80%; border: thin solid gray;"></div>
|
||||||
<div id="doc">
|
<div id="doc">
|
||||||
<p>
|
<p>
|
||||||
|
@ -71,7 +80,31 @@ crossorigin=""></script -->
|
||||||
<script src="vendor/node_modules/papaparse/papaparse.min.js"></script>
|
<script src="vendor/node_modules/papaparse/papaparse.min.js"></script>
|
||||||
<script src="js/compiled/geocsv_lite.js" type="text/javascript"></script>
|
<script src="js/compiled/geocsv_lite.js" type="text/javascript"></script>
|
||||||
<script>
|
<script>
|
||||||
geocsv_lite.core.initialise_map_element("map", "/data/data.csv");
|
geocsv_lite.core.initialise_map_element("inline-csv-map", "Name,Latitude,Longitude\n"+
|
||||||
|
"Nether Hazelfield Cottage,54.816274,-3.887435,\n"+
|
||||||
|
"Over Hazelfield Cottage,54.823192,-3.899516,\n"+
|
||||||
|
"Winter Palace,54.822260,-3.920147,\n"+
|
||||||
|
"Craignair,54.842321,-3.872055,\n"+
|
||||||
|
"Carlinscraig,54.843224,-3.8730822,\n"+
|
||||||
|
"Orchardton,54.859129,-3.854212,\n"+
|
||||||
|
"Elderslie,54.845596,-3.866432\n" +
|
||||||
|
"Braendam Lodge,56.191257, -4.173869\n" +
|
||||||
|
"Moncrief Terrace,55.938946, -3.183590\n" +
|
||||||
|
"Craiglockhart Road, 55.916329, -3.243627\n" +
|
||||||
|
"Merchiston Gardens,55.929761, -3.220899\n" +
|
||||||
|
"Stamfordham, 55.042020, -1.877289\n" +
|
||||||
|
"Kaymanton, 54.965216, -2.102066\n" +
|
||||||
|
"Lonsdale College, 54.011382, -2.786183\n" +
|
||||||
|
"Glasson Dock, 53.997440, -2.845856\n" +
|
||||||
|
"Dolphinholme, 53.978104, -2.735363\n" +
|
||||||
|
"Greenfield Street, 54.048707, -2.792666\n" +
|
||||||
|
"Prospect Street,54.044364, -2.792685\n" +
|
||||||
|
"Westminster Palace Gardens, 51.497045, -0.135231\n" +
|
||||||
|
"Girdstingwood Cottage, 54.804639, -3.951338\n" +
|
||||||
|
"Millburn Street, 54.837358, -4.046627\n" +
|
||||||
|
"Palmer's Close, 54.834972, -4.053855"
|
||||||
|
);
|
||||||
|
geocsv_lite.core.initialise_map_element("map", "http://localhost:3449/data/data.csv");
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -37,6 +37,29 @@
|
||||||
(:uri query) (:uri query))))
|
(:uri query) (:uri query))))
|
||||||
|
|
||||||
|
|
||||||
|
(defn prepare-records
|
||||||
|
"`data` is expected to be a vector of vectors, where the first vector
|
||||||
|
contains column headers and the remaining vectors contain records.
|
||||||
|
Return this as a vector of maps, with each map having keys taken from
|
||||||
|
the first vector and values taken from one of the subsequent vectors."
|
||||||
|
[data]
|
||||||
|
(let [cols (map
|
||||||
|
#(let [n (when-not
|
||||||
|
(empty? %)
|
||||||
|
(when (string? %)
|
||||||
|
(cs/lower-case
|
||||||
|
(cs/replace
|
||||||
|
% #"[^\w\d]+" "-"))))]
|
||||||
|
(keyword
|
||||||
|
(if (empty? n)
|
||||||
|
(gensym)
|
||||||
|
n)))
|
||||||
|
(first data))]
|
||||||
|
(map
|
||||||
|
(fn [r] (zipmap cols r))
|
||||||
|
(rest data))))
|
||||||
|
|
||||||
|
|
||||||
(defn default-handler
|
(defn default-handler
|
||||||
"When data is received from a URL, it is received asynchronously. This
|
"When data is received from a URL, it is received asynchronously. This
|
||||||
is the default callback called with the `response` of the HTTP request,
|
is the default callback called with the `response` of the HTTP request,
|
||||||
|
@ -46,46 +69,78 @@
|
||||||
(if
|
(if
|
||||||
(= (:status response) 200)
|
(= (:status response) 200)
|
||||||
(let [content (:body response)
|
(let [content (:body response)
|
||||||
data (map
|
records (prepare-records
|
||||||
#(merge %
|
(:data
|
||||||
{:longitude (js/Number (:longitude %))
|
(js->clj
|
||||||
:latitide (js/Number (:latitude %))})
|
(.parse js/Papa content
|
||||||
(js->clj (.-data (.parse js/Papa content (clj->js {:dynamicTyping true})))))
|
(clj->js {:dynamicTyping true}))
|
||||||
cols (map
|
:keywordize-keys true)))]
|
||||||
#(let [n (when-not
|
|
||||||
(empty? %)
|
|
||||||
(when (string? %)
|
|
||||||
(cs/lower-case
|
|
||||||
(cs/replace
|
|
||||||
% #"[^\w\d]+" "-"))))]
|
|
||||||
(keyword
|
|
||||||
(if (empty? n)
|
|
||||||
(gensym)
|
|
||||||
n)))
|
|
||||||
(first data))
|
|
||||||
records (map
|
|
||||||
(fn [r] (zipmap cols (map str r)))
|
|
||||||
(rest data))
|
|
||||||
]
|
|
||||||
(gis/refresh-map-pins (get-view k) records))
|
(gis/refresh-map-pins (get-view k) records))
|
||||||
(n/error (str "Bad response from server: " (:status response)))))
|
(n/error (str "Bad response from server: " (:status response)))))
|
||||||
|
|
||||||
|
|
||||||
|
(defn get-data-from-uri
|
||||||
|
"Get data for the view identified by this keyword `k` from this `uri`."
|
||||||
|
[k uri]
|
||||||
|
(go (let [response (<! (http/get uri {:with-credentials? "false"}))]
|
||||||
|
(default-handler response k))))
|
||||||
|
|
||||||
|
|
||||||
(defn get-data
|
(defn get-data
|
||||||
"Get data for the view identified by this keyword `k` from this `data-source`.
|
"Get data for the view identified by this keyword `k` from this `data-source`.
|
||||||
In this initial release the data source must be a URL, but in future releases
|
The data source may be a URL, or a CSV or JSON formatted string."
|
||||||
I intend that it may also be a list of maps representing records, or a CSV or
|
|
||||||
JSON formatted string."
|
|
||||||
[k data-source]
|
[k data-source]
|
||||||
(let
|
(let [p (js->clj (.parse js/Papa data-source) :keywordize-keys true)
|
||||||
[uri data-source]
|
data (if
|
||||||
(go (let [response (<! (http/get uri {:with-credentials? "false"
|
(empty? (:errors p))
|
||||||
:access-control-allow-credentials "true"
|
(:data p)
|
||||||
:origin js/window.location.hostname}))]
|
;; otherwise, is it JSON?
|
||||||
(default-handler response k)))))
|
(try
|
||||||
|
(js->clj (.parse js/JSON data-source))
|
||||||
|
(catch :default _ nil)))]
|
||||||
|
;; almost anything can be a valid URL, so it's hard to verify that a given
|
||||||
|
;; string is not one. So we will assume that what we've been passed is a
|
||||||
|
;; URL unless we've been able to parse valid data out of it.
|
||||||
|
(js/console.debug "Found records: " (clj->js data))
|
||||||
|
(if
|
||||||
|
;; it looks like valid data if it's a vector of vectors.
|
||||||
|
(and (vector? data) (every? vector? data))
|
||||||
|
(let [records (prepare-records data)]
|
||||||
|
(n/message (str "Found " (count records) " records of inline data for map `" k "`"))
|
||||||
|
(gis/refresh-map-pins (get-view k) (prepare-records data)))
|
||||||
|
; else
|
||||||
|
(get-data-from-uri k data-source))))
|
||||||
|
|
||||||
|
|
||||||
(defn get-data-with-uri-and-handler
|
(defn get-data-with-uri-and-handler
|
||||||
[uri handler-fn k]
|
[uri handler-fn k]
|
||||||
(go (let [response (<! (http/get uri))]
|
(go (let [response (<! (http/get uri))]
|
||||||
(apply handler-fn (list response k)))))
|
(apply handler-fn (list response k)))))
|
||||||
|
|
||||||
|
;; (def data-source "Name, Latitude, Longitude
|
||||||
|
;; Nether Hazelfield Cottage, 54.816274, -3.887435
|
||||||
|
;; Over Hazelfield Cottage, 54.823192, -3.899516
|
||||||
|
;; Winter Palace, 54.822260, -3.920147
|
||||||
|
;; Craignair, 54.842321, -3.872055
|
||||||
|
;; Carlinscraig, 54.843224,-3.8730822
|
||||||
|
;; Orchardton, 54.859129, -3.854212
|
||||||
|
;; Elderslie, 54.845596, -3.866432")
|
||||||
|
|
||||||
|
;; (prepare-records
|
||||||
|
;; (:data (js->clj (.parse js/Papa data-source)
|
||||||
|
;; :keywordize-keys true)))
|
||||||
|
|
||||||
|
;; (get-data :inline-csv-map "http://localhost:3449/data/data.csv")
|
||||||
|
;; (get-data :inline-csv-map data-source)
|
||||||
|
;; (every? (fn [r] (and (vector? r) (every? vector? r))) (:data (js->clj (.parse js/Papa data-source)
|
||||||
|
;; :keywordize-keys true)))
|
||||||
|
;; (every? vector? (:data (js->clj (.parse js/Papa data-source)
|
||||||
|
;; :keywordize-keys true)))
|
||||||
|
;; (vector? (first (:data (js->clj (.parse js/Papa data-source)
|
||||||
|
;; :keywordize-keys true))))
|
||||||
|
;; (def p (:data (js->clj (.parse js/Papa data-source)
|
||||||
|
;; :keywordize-keys true)))
|
||||||
|
;; (every? vector? p)
|
||||||
|
|
||||||
|
;; (vector? p)
|
||||||
|
;; (every? vector? p)
|
||||||
|
|
Loading…
Reference in a new issue