1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to calculate distance ratio with momentjs and turfjs

Discussion in 'JavaScript' started by mstdmstd, Mar 1, 2020.

  1. #1
    Hello,
    With JS anf turfjs library I need on several markers calculate
    1) medium speed
    2) time of arrival at any marker point

    I make as:


        const markerIds = Object.keys(markers)
        const points2Data = []
    
        let summarized_distance= 0
        let arrival_date= moment(tripRow.arrival_date)// arrival time
        let departure_date= moment(tripRow.departure_date) // departure time
    
        for (let i = 1; i < markerIds.length; i++) {
          const marker = markers[markerIds[i - 1]]
    
          const nextMarker = markers[markerIds[i]]
          const from = turf.point([marker.lat, marker.lng])
          const to = turf.point([nextMarker.lat, nextMarker.lng])
          const distance = turf.distance(from, to, { units: 'kilometers' })
          summarized_distance= summarized_distance + distance
          // get data
          const points2Item = {
            id: marker.id,
            start: marker.id,
            end: nextMarker.id,
            speed: marker.speed,
            lat: nextMarker.lat,
            lng: nextMarker.lng,
            latlng: [nextMarker.lat, nextMarker.lng],
            distance: distance,
            time: distance / marker.speed,
            eta:null
    
          }
    
          points2Data.push(points2Item)
        } // for (let i = 1; i < markerIds.length; i++) {
    
        // get medium speed
        let calculated_speed = summarized_distance / summarized_trip_time
    
    
        let next_arrival_time= arrival_date
        for (let i = 0; i < points2Data.length; i++) {
    
            // I think I need to get raio of 1 marker distance  from whole distance
          let distance_ratio= summarized_distance / points2Data[i].distance
    
          // I think I need to get calculate time ratio and add it to next_arrival_time, something like
          next_arrival_time= next_arrival_time.add(CALCULATED_RATIO_TIME, 'hour')
          // But how to calculate this CALCULATED_RATIO_TIME ?
          points2Data[i].eta= next_arrival_time.format('YYYY-MM-DD HH:mm')
    Code (JavaScript):
    how to calculate this CALCULATED_RATIO_TIME ?

    I hope my common flow is correct, but I encountered this issue...
    Thanks!
     
    mstdmstd, Mar 1, 2020 IP
  2. mstdmstd

    mstdmstd Well-Known Member

    Messages:
    130
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #2
    I remade it as :
    
        let next_arrival_time= departure_date
        for (let i = 0; i < points2Data.length; i++) {
    
          // That was invalid  I got ratio of distance
          let distance_ratio= points2Data[i].distance / summarized_distance
        
          // I got ratio of time
          next_arrival_time= next_arrival_time.add( summarized_trip_time * distance_ratio   , 'hour')
    
          points2Data[i].eta= next_arrival_time.format('YYYY-MM-DD HH:mm')
    
        } // for (let i = 1; i < points2Data.length; i++) {
    Code (JavaScript):
    Lokking at results I have results very similar as I expect,
    but havinf
    departure_date= ‘2020-03-02 12:00:00’
    and
    arrival_date='2020-03-03 12:00:00'

    with several points I got last point :
    2020-03-03 11:59
    and I lost 1 minutes - why and how to fix it ?
     
    mstdmstd, Mar 2, 2020 IP