Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
aire-cdmx
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Rodrigo Tapia-McClung
aire-cdmx
Commits
ae024f9d
Commit
ae024f9d
authored
Aug 18, 2023
by
Rodrigo Tapia-McClung
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix read data asynchronously
parent
d73ac168
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
3 deletions
+40
-3
functions.js
public/js/functions.js
+40
-3
No files found.
public/js/functions.js
View file @
ae024f9d
...
...
@@ -2,7 +2,7 @@
* Copyright 2021 - All rights reserved.
* Rodrigo Tapia-McClung
*
* February-July 2021
* February-July 2021
- August 2023
*/
/* global mapboxgl, turf, Papa */
...
...
@@ -20,8 +20,41 @@ let cdmx, estaciones, ozono, fechas,
playing
=
false
,
animate
;
const
readData
=
async
()
=>
{
// Read CDMX boundaries
let
cdmx
=
await
fetch
(
`
${
baseUrl
}
/data/cdmx.geojson`
)
.
then
(
response
=>
response
.
json
())
// Read and parse csv station data
let
estaciones
=
await
fetch
(
`
${
baseUrl
}
/data/cat_estacion.csv`
)
.
then
(
response
=>
response
.
arrayBuffer
())
.
then
(
d
=>
new
TextDecoder
(
"iso-8859-1"
).
decode
(
d
).
split
(
/
\r\n
|
\n
|
\r
/
))
.
then
(
d
=>
{
// arrange elements as a decent array
let
array
=
[];
d
.
forEach
(
d
=>
array
.
push
(
d
.
split
(
","
)));
let
array2
=
array
.
slice
(
1
,
-
1
);
// remove spurious first and last line
const
[
keys
,
...
values
]
=
array2
;
// deconstruct
let
estacionesCSV
=
values
.
map
(
array2
=>
// create useful object of key:value pairs for each station
array2
.
reduce
((
a
,
v
,
i
)
=>
({
...
a
,
[
keys
[
i
]]:
v
}),
{})
);
let
estacionesArray
=
[];
estacionesCSV
.
forEach
(
e
=>
{
// create features for feature collection
let
location
=
turf
.
point
([
+
e
.
longitud
,
+
e
.
latitud
],
{
cve_estac
:
e
.
cve_estac
,
nom_estac
:
e
.
nom_estac
,
longitud
:
+
e
.
longitud
,
latitud
:
+
e
.
latitud
,
alt
:
+
e
.
alt
});
estacionesArray
.
push
(
location
);
});
return
turf
.
featureCollection
(
estacionesArray
);
});
return
{
cdmx
,
estaciones
}
}
// Read CDMX boundaries
fetch
(
`
${
baseUrl
}
/data/cdmx.geojson`
)
/*let cdmx =
fetch(`${baseUrl}/data/cdmx.geojson`)
.then(response => response.json())
.then(d => cdmx = d);
...
...
@@ -50,7 +83,7 @@ fetch(`${baseUrl}/data/cat_estacion.csv`)
});
estaciones = turf.featureCollection(estacionesArray);
});
*/
const
papaPromise
=
url
=>
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
Papa
.
parse
(
url
,
{
...
...
@@ -195,6 +228,10 @@ const run = () => {
map
.
on
(
"style.load"
,
async
()
=>
{
let
data
=
await
readData
();
cdmx
=
data
.
cdmx
;
estaciones
=
data
.
estaciones
;
// add vector tile source
/*map.addSource("estaciones", {
"type": "vector",
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment