Page 1 of 1

[JS - Leaflet] Convert GDF Time Domain to a readable value

Posted: Thu Oct 31, 2019 7:43 am
by stunty
Hello,

I m using Leaflet to render the PTV map with Traffic Incidents & co.

On Traffic Incidents popups, there is a time/duration value written like that :

Code: Select all

time Domain: [(y2019M10d31h6m52s30){h1m2s30}]
relevant By Time: true
Is there a function that can convert that to a more readable string ?

I have found these docs too but I m lazy to develop my own function if something else already exists...

https://xserver2-dashboard.cloud.ptvgro ... omains.htm
https://china.cloud.ptvgroup.com/manual ... omains.htm


Thank you very much in advance

Re: [JS - Leaflet] Convert GDF Time Domain to a readable value

Posted: Mon Dec 02, 2019 4:30 pm
by Bernd Welter
Hm... that's a challenge. The documentation of the xServer2 seems to be buggy: the link to the external reference of the party that specified the style is not valid. I asked my colleagues to provide a new link...

Re: [JS - Leaflet] Convert GDF Time Domain to a readable value

Posted: Tue Dec 03, 2019 7:50 am
by YellowFoxNP
Hi here is my implementation. It's not complete and not bulletproof at the moment but i hope it's a good start for you:

Code: Select all

function parseTimeDomain( input )
{
   var tmp = input.split( /[()]+/ );
   var result = {};
   if( tmp.length >= 3 )
   {
      result.startDate = parseDateByGDF( tmp[1] );
      if( tmp[2].indexOf( '{' ) > -1 )
      {
         result.endDate = parseEndDateByGDFDuration( tmp[2].split( /[{}]+/ )[1], new Date(result.startDate.getTime() ) );
      }
   }

   if( tmp.length == 4 )
   {
      result.endDate = parseDateByGDF( tmp[2] );
   }

   return result;
}

function parseDateByGDF( parse )
{
   parse = parse.split( /[A-Za-z]/ );

   if( parse[5] < 10 )
   {
      parse[5] = '0' + parse[5];
   }
   if( parse[6] < 10 )
   {
      parse[6] = '0' + parse[6];
   }

   return new Date( parse[1] + '-' + parse[2] + '-' + parse[3] + ' ' + parse[4] + ':' + parse[5] + ':' + parse[6] ) ;
}

function getDateInGermanFormat( date )
{
   var day = date.getDate();
   var month = date.getMonth();
   if( day.toString().length == 1 )
   {
      day = '0' + day;
   }

   if( month.toString().length == 1 )
   {
      month = '0' + month;
   }
   return  day + '.' + month + '.' + date.getFullYear() + ' ' + date.toLocaleTimeString( 'de-DE' );
}

function parseEndDateByGDFDuration( durationString, date )
{
   var tmp = durationString.split('');
   var type = '';
   var value = '';
   for( var i = 0; i < tmp.length; i++ )
   {
      if( tmp[i].match( /[a-zA-Z]+/ ) != undefined )
      {
         if( type != '' )
         {
            setIntervalByType( type, parseInt( value ), date );
         }
         type = tmp[i];
         value = '';
      }
      else
      {
         value += tmp[i].toString();
      }
   }

   if( type != '' )
   {
      setIntervalByType( type, parseInt( value ), date );
   }
   return date;
}

function setIntervalByType( type, value, date )
{
   if( type == 's' ) date.setTime( date.getTime() + ( value * 1000 ) );
   if( type == 'm' ) date.setTime( date.getTime() + ( value * 1000 * 60 ) );
   if( type == 'h' ) date.setTime( date.getTime() + ( value * 1000 * 60 * 60 ) );
   if( type == 'd' ) date.setTime( date.getTime() + ( value * 1000 * 60 * 60 * 24 ) );
   if( type == 'w' ) date.setTime( date.getTime() + ( value * 1000 * 60 * 60 * 24 * 7 ) );
   if( type == 'M' )
   {
      date.setMonth( date.getMonth() + value - 1 );
   }

   if( type == 'y' )
   {
      date.setFullYear( date.getFullYear() + value );
   }
}

Re: [JS - Leaflet] Convert GDF Time Domain to a readable value

Posted: Mon Dec 23, 2019 12:05 pm
by Bernd Welter
Thanks for your implementation, Nico,

In the meantime I raised a new JIRA (XS-3574) that requests a proper documentation of the format despite the broken link to the external documents.

Have a nice christmas time!

Bernd

Re: [JS - Leaflet] Convert GDF Time Domain to a readable val

Posted: Sun Dec 13, 2020 6:18 pm
by s.warnat
Hy,

I know it is an old thread, but the one found by Google.

We extended the leaflet-xserver repo by an almost complete, german only, TimeDomain parser here, which we will provide for general usage:

It can also be used standalone, without the leaflet library, but is included into the new customized.html example.

There are some easy examples how to use the library

We never worked with the browserify library, that's why created the easy way.

We will prepare the merge request, to make it available for all developers to be extended and maybe to implement some more "special cases" to improve understandability.

Regards!

Re: [JS - Leaflet] Convert GDF Time Domain to a readable value

Posted: Mon Mar 11, 2024 9:32 am
by Bernd Welter
Attention:
In the context of the GDF Time domain more and more often we get asked for the readable format.
In PTV Developer we offer some solution for this requirement based on the VectorMaps.
Check this Showcase Map Rendering and Geocoding (as of march 2024: english only)

Here are some examples:
Städtisches Klinikum Karlsruhe (no trucks at night time)
Städtisches Klinikum Karlsruhe (no trucks at night time)
Some fancy restrictions at teh french-spanish border near Canejan
Some fancy restrictions at teh french-spanish border near Canejan