Category Archives: Moose

New York City Household Furniture Moving Travel Time Calculator

My next project is to create a travel time calculator application using Dancer2. Hopefully, this would be useful to any household furniture moving company operating in New York City.
When charging for the travel time between two locations, many New York City Moving companies use the following DOT guidelines.

  1. 1/2 an hour within any borough.
  2. 3/4 an hour from one borough to another.
  3. 1 hour for the first 20 miles between locations that either start or end within New York City
  4. 1 hour for each 40 miles after the first 20 miles, which is equivalent to 15 minutes for each 10 miles.

Most of the information that I need to get the distances between locations is freely available from Google’s Distance Matrix API. I checked out CPAN to see if there was a suitable module available to access this API. There were some that partially did what I required, but not all of what I required.
I decided to write my own, which you can read about here.


Form To Input Addresses

There are two methods of User Input that I wanted to experiment with. The first is by means of a form where you would enter the exact origin and destination addresses. I describe this in detail here.
The second method, using jQuery autocomplete, I discuss here.

Dancer2 Application TravelTime

The Perl App that implements the Travel Time Calculator

I used the Perl Dancer2 framework for building this application. Dancer2 bills itself as a “Lightweight yet powerful web application framework”, which I guess is true. It works well for me and is not too complex. I did run into a few bugs which seem to have been ironed out in the very latest version. So, it does have an active group of developers who are maintaining and upgrading this software. Thank you for that.
Here is an excerpt from this module, showing one particular route handler. This particular one handles an Ajax call to provide a list of cities that match a specific search criteria. The Dancer2::Plugin::Ajax helps out with this method. The caller in this case is a JavaScript program that I use to implement a jQuery autocomplete input field. You can read about this here

#    Get a list of city, states and counties.
ajax '/city_states' => sub {
    #------ Trim  whitespace from lhs and append the '%' SQL match char.
    #       This will handle searches like 'Sunn,      New York' (Sunnyside, New York)
    #       or                             'Kew Gar,      New York' (Kew Gardens,  New York)
    #       or                             'Kew,New York'
    my @find_params =
      map { $_ =~ s/^\s+//; s/\s+\z//; $_ . '%' }
      split( ',', params->{find} );
    debug 'The find params array now contains : ' . dump @find_params;

    my $csc_arr;

    #------ Allow some short cuts for the Big Apple
    if ( $find_params[0] =~ /^(nyc|manhattan|base)%\z/i ) {
        $csc_arr->[0] = [ 'New York City', 'New York', 'New York County' ];
    else {

        #----- Search DB for City and Counties matching the search params.
        connect_to_cities() unless $DBH;
        process_error( { Error => 'Unable to connect to the City, State,
                county database! Please try later!', } ) unless $DBH;
        $csc_arr = select_city_state_cty( $DBH, \@find_params );

    #------ Returns sorted list of Cities, States and Counties to
    #       city-states.js
    { city_states => $csc_arr };

Have a look at the entire Application code on GitHub.
For a live demo, here is the Moving Truck Travel Time Calculator application running on the dotCloud hosting service.
This is how I deployed my Dancer2 Application to dotCloud.

Perl Google Distance Matrix API Interface

This is my Perl Module Google::Travel::Matrix, which will be an interface to the Google Distance Matrix API. I will use this for calculating distance and travel times between multiple locations.

Google Distance Matrix API Interface


This module provides an interface with the Google Distance Matrix API. It will return the distance between given locations in JSON format.
It will accept one or many “Origin” addresses in String, Array or Hash format.It will also accept many Destination addresses. The Google Matrix configuration attributes can be set here, but it may be easier just to accept the defaults that I built in unless you have other specific requirements.
I used the Perl Moose Object framework as it does a really great job of helping set up the configuration attributes using Moose Types and coercion. It will also make it easier for me to build my sub classes.
The module is available here on GitHub, Google::Travel::Matrix

=head2 get_all_elements
 Given the Google Distance Matrix output as a scalar reference to a Perl data
 structure, returns an ArrayRef of Matrix elements or undef.
 If no Google Output data passed, then it will create one using the Origin and
 destination addresses;
 It would be a good idea to check that the Google Matrix Return is 'OK' before calling
 this method.

sub get_all_elements {
    my $self = shift;
    my $matrix = shift || $self->get_google_matrix_data_as_scalar_ref();

    my $google_status = $self->get_matrix_status_message($matrix);

    if ( ( not $google_status ) || ( $google_status ne $VALID_REQ ) ) {

        $log->debug( 'Google return status message is: '
              . ( $google_status // 'No Google Status' ) );
        return $FAIL;

    #------ Preserve the Original address sent to Google
    #       as it may be needed later.
    #       Yet again,  this is something I would prefer 
    #       to put into a Child Class
    my $original_origins      = $self->_get_array_of_origins();
    my $original_destinations = $self->_get_array_of_destinations();

    #------ Get each combination for the origin destination addresses
    my $origin_ct = 0;
    my @elements_array;
    foreach my $origin_addr ( @{ $self->get_matrix_origin_addresses($matrix) } )
        my $original_origin_addr = $original_origins->[ $origin_ct++ ];
        $log->debug( 'Original origin address is : ' . $original_origin_addr );

        #---Get results for current origination address
        my $row = shift @{ $matrix->{rows} };

        #------ Match origination address with all destination addresses
        my $dest_ct = 0;
        foreach my $destination_addr (
            @{ $self->get_matrix_destination_addresses($matrix) } )

            my $original_destination_addr =
              $original_destinations->[ $dest_ct++ ];

            $log->debug( 'Original destination address is : '
                  . $original_destination_addr );

           #----- get the result for the current Origination -> Destination pair
            my $element = shift @{ $row->{elements} };

            push @elements_array,
                origin_address               => $origin_addr,
                destination_address          => $destination_addr,
                original_origin_address      => $original_origin_addr,
                original_destination_address => $original_destination_addr,
                element_status               => $element->{status},
                element_duration_text        => $element->{duration}{text},
                element_duration_value       => $element->{duration}{value},
                element_distance_text        => $element->{distance}{text},
                element_distance_value       => $element->{distance}{value},
        'Array of all elements returned by Google:  ' . dump @elements_array );
    return \@elements_array;

This is far from being a completed masterpiece, but for now it will get me what I need. I intentionally didn’t allow for receiving the data in XML format, as Google recommends JSON format.I may add XML processing at a later date.I may even consider refactoring it from Moose to Moo, to make it lighter and nimbler.
I also plan to create a subclass class to Google::Travel::Time that will do the actual conversion from distance to a travel time acceptable to the NYC moving and storage industry. Until then, I don’t consider this module to be CPAN worthy.