Skip to content

Instantly share code, notes, and snippets.

@mbritton
Created November 15, 2016 05:13
Show Gist options
  • Save mbritton/9f7468e4599f35c50bc1e90ee300cf64 to your computer and use it in GitHub Desktop.
Save mbritton/9f7468e4599f35c50bc1e90ee300cf64 to your computer and use it in GitHub Desktop.
import axios from 'axios'
const xml2js = require('xml2js')
const parseTrack = require('../../node_modules/parse-gpx/src/parseTrack.js')
const trackPoint = require('../../node_modules/parse-gpx/src/TrackPoint.js')
const routes = [
{ id: -1, name: 'Racing Rain, Embracing Pain', gpx: 'Racing_Rain_Embracing_Pain.gpx', county: 'Paulding', description: 'Description' },
{ id: 0, name: 'Sky Loop - Challenge', gpx: 'sky_loop_back_30.gpx', county: 'Paulding', description: 'Description' },
{ id: 1, name: 'Round Rockmart', gpx: 'round_rockmart.gpx', county: 'Polk', description: 'Description' },
{ id: 2, name: 'South of Rockmart', gpx: 'south_of_rockmart.gpx', county: 'Polk', description: 'Description' },
{ id: 3, name: 'Coots Alternative', gpx: 'coots_alternate.gpx', county: 'Polk', description: 'Description' },
{ id: 4, name: 'Miller Willow', gpx: 'miller_willow.gpx', county: 'Paulding', description: 'Description' },
{ id: 5, name: 'Rockabama', gpx: 'rockabama.gpx', county: 'Polk', description: 'Description' },
{ id: 6, name: 'Coots (Short)', gpx: 'coots_short.gpx', county: 'Paulding', description: 'Description' },
{ id: 7, name: 'Hillseytown', gpx: 'hillseytown.gpx', county: 'Paulding', description: 'Description' },
{ id: 8, name: 'Rock-Cedar-Bama', gpx: 'rock_cedar_bama_68.gpx', county: 'Polk', description: 'Description' },
{ id: 9, name: 'Brookstone to Buckhead', gpx: 'brookstone_to_buckhead.gpx', county: 'Cobb', description: 'Description' },
{ id: 10, name: 'Winndale Route', gpx: 'thur-winndale.gpx', county: 'Paulding', description: 'Description' },
{ id: 11, name: 'North of Rockmart', gpx: 'north_of_rockmart.gpx', county: 'Polk', description: 'Description' },
{ id: 12, name: 'Dallas Loop 32', gpx: 'Dallas_Loop_32.gpx', county: 'Paulding', description: 'Description' },
{ id: 13, name: 'Coppermine Loop', gpx: 'coppermine_loop.gpx', county: 'Polk', description: 'Description' },
{ id: 14, name: 'Sky Loop', gpx: 'sky_loop.gpx', county: 'Paulding', description: 'Description' },
{ id: 15, name: 'Hillseytown II', gpx: 'hell-sey-town.gpx', county: 'Paulding', description: 'Description' }
]
export function fetchRoutes() {
return function(dispatch) {
dispatch({
type: 'FETCH_ROUTES_FULFILLED',
payload: {
routes: routes
}
})
}
}
export function selectRoute(routeId) {
return function(dispatch) {
let route = null
for (var i = 0; i < routes.length; i++) {
if (routes[i]['id'] == routeId) {
route = routes[i]
}
}
dispatch({
type: 'SELECT_ROUTE',
selectedRoute: route
})
}
}
export function setChartConfig(selectedRoute) {
let parser = new xml2js.Parser()
let chartConfig = {}
let cdata = []
let lastPoint
let totalDistance = 0
let distance = 0
return function(dispatch) {
axios.get('../../'+selectedRoute)
.then(function (response) {
parser.parseString(response.data, (err, xml) => {
if (err) {
console.log('PARSE ERROR', err)
} else {
console.log('xml.gpx', xml.gpx)
for (var i = 0; i < xml.gpx.trk[0].trkseg[0].trkpt.length; i++) {
const trkpt = xml.gpx.trk[0].trkseg[0].trkpt[i]
const ele = trkpt.ele[0]
const lat = parseFloat(trkpt['$'].lat)
const lon = parseFloat(trkpt['$'].lon)
const point = {
lat: lat,
lon: lon
}
if (lastPoint === undefined) {
lastPoint = { lat: 0, lon: 0 }
}
const toRad = Math.PI / 180
const R = 3959 // Radius of the earth in miles
let dLat = (point.lat - lastPoint.lat) * toRad
let dLon = (point.lon - lastPoint.lon) * toRad
let a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lastPoint.lat * toRad) * Math.cos(point.lat * toRad) *
Math.sin(dLon / 2) * Math.sin(dLon / 2)
let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
console.log('c', c) // 00.0000289574 blah
distance = R * c // mi
console.log('distance', distance)
// console.log('distance', distance)
// console.log('totalDistance', totalDistance)
totalDistance += distance
console.log('totalDistance', totalDistance)
// console.log(
// 'elevation', ele,
// 'lat', lat,
// 'lon', lon,
// 'distance', distance,
// 'totalDistance', totalDistance
// )
//console.log('Math.round(totalDistance * 100) / 100', Math.round(totalDistance * 100) / 100)
//x: Math.round(totalDistance * 100),
cdata.push({
x: Math.round(totalDistance)/100,
y: ele - 10,
foo:point
})
lastPoint = point
}
}
})
dispatch({
type: 'SET_CHART_CONFIG',
data: cdata
})
})
.catch(function (error) {
console.log('error',error);
})
}
}
export function toggleSelectedRoute(routeId) {
return function(dispatch) {
dispatch({
type: 'TOGGLE_SELECTED_ROUTE'
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment