NAME

Map::Tube - Lightweight Routing Framework.

VERSION

Version 3.84

DESCRIPTION

The core module defined as Role (Moo) to process the map data. It provides the interface to find the shortest route in terms of stoppage between two nodes.Also you can get all possible routes between two given nodes.

If you are keen to know the internals of Map::Tube then please follow the note documented in Map::Tube::Cookbook.

MAP LEADER BOARD

+----------------------+----------+------------------------------------------+
| Author               | PAUSE ID | Map Count (City)                         |
+----------------------+----------+------------------------------------------+
| Michal Josef Spacek  | SKIM     | 22 (Bucharest, Budapest, Dnipropetrovsk, |
|                      |          | Kazan, Kharkiv, Kiev, KualaLumpur,       |
|                      |          | Malaga, Minsk, Moscow, Nanjing,          |
|                      |          | NizhnyNovgorod, Novosibirsk, Prague,     |
|                      |          | SaintPetersburg, Samara, Singapore,      |
|                      |          | Sofia, Tbilisi, Vienna, Warsaw,          |
|                      |          | Yekaterinburg                            |
|                      |          |                                          |
| Mohammad Sajid Anwar | MANWAR   | 7 (Barcelona, Delhi, Kolkatta, London,   |
|                      |          | Madrid, NYC, Tokyo)                      |
|                      |          |                                          |
| Gisbert W Selke      | GWS      | 5 (Beijing, Glasgow, Hamburg, KoelnBonn, |
|                      |          | Lyon)                                    |
|                      |          |                                          |
| Renee Baecker        | RENEEB   | 1 (Frankfurt)                            |
|                      |          |                                          |
| Stefan Limbacher     | STELIM   | 1 (Nuremberg)                            |
|                      |          |                                          |
| Slaven Rezic         | SREZIC   | 1 (Berlin)                               |
|                      |          |                                          |
| Errietta Kostala     | ERRIETTA | 1 (Athens)                               |
|                      |          |                                          |
| Marco Fontani        | MFONTANI | 1 (Milan)                                |
|                      |          |                                          |
| Soren Lund           | SLU      | 1 (Copenhagen)                           |
|                      |          |                                          |
| FUNG Cheok Yin       | CYFUNG   | 1 (Hong Kong)                            |
|                      |          |                                          |
| Vitali Peil          | VPEIL    | 1 (Bielefeld)                            |
|                      |          |                                          |
| Giuseppe Di Terlizzi | GDT      | 1 (Rome)                                 |
+----------------------+----------+------------------------------------------+

SYNOPSIS

Common Usage

use strict; use warnings;
use Map::Tube::London;

my $map = Map::Tube::London->new;
print $map->get_shortest_route('Baker Street', 'Euston Square'), "\n";

You should expect the result like below:

Baker Street (Circle, Hammersmith & City, Bakerloo, Metropolitan, Jubilee), Great Portland Street (Circle, Hammersmith & City, Metropolitan), Euston Square (Circle, Hammersmith & City, Metropolitan)

Special Usage

use strict; use warnings;
use Map::Tube::London;

my $map = Map::Tube::London->new;
print $map->get_shortest_route('Baker Street', 'Euston Square')->preferred, "\n";

You should now expect the result like below:

Baker Street (Circle, Hammersmith & City, Metropolitan), Great Portland Street (Circle, Hammersmith & City, Metropolitan), Euston Square (Circle, Hammersmith & City, Metropolitan)

METHODS

get_shortest_routes($from, $to)

It expects $from and $to station name, required param. It returns an object of type Map::Tube::Route. On error it throws exception of type Map::Tube::Exception.

get_all_routes($from, $to) *** EXPERIMENTAL ***

It expects $from and $to station name, required param. It returns ref to a list of objects of type Map::Tube::Route. On error it throws exception of type Map::Tube::Exception.

Be carefull when using against a large map. You may encounter warning similar to as shown below when run against London map.

Deep recursion on subroutine "Map::Tube::_get_all_routes"

However for comparatively smaller map, like below,it is happy to give all routes.

  A(1)  ----  B(2)
 /              \
C(3)  --------  F(6) --- G(7) ---- H(8)
 \              /
  D(4)  ----  E(5)

name()

Returns map name.

get_node_by_id($node_id)

Returns an object of type Map::Tube::Node.

get_node_by_name($node_name)

Returns ref to a list of object(s) of type Map::Tube::Node matching node name $node_name in scalar context otherwise returns just a list.

get_line_by_id($line_id)

Returns an object of type Map::Tube::Line.

get_line_by_name($line_name)

Returns an object of type Map::Tube::Line.

get_lines()

Returns ref to a list of objects of type Map::Tube::Line.

get_stations($line_name)

Returns ref to a list of objects of type Map::Tube::Node for the $line_name. If $line_name is missing, it would return all stations in the map.

get_next_stations($station_name)

Returns ref to a list of next stations from the given $station_name as objects of type Map::Tube::Node.

bgcolor($color)

Set the background color for the map. It is optional. Please set it before making call to method "as_image($line_name)" in Map::Tube::Plugin::Graph. If not set, it will try to guess and may not be as good as you would expect.The $color can be a simply color name or hash code.

PLUGINS

Map::Tube::Plugin::Graph

The Map::Tube::Plugin::Graph plugin adds the support to generate the entire map or map for a particular line as base64 encoded string (png image). As of Map::Tube v3.54 or above, you can now set the background color explicitly.

use strict; use warnings;
use MIME::Base64;
use Map::Tube::London;

my $tube = Map::Tube::London->new;

# Optionally, you can override the default background color.
$tube->bgcolor("gray");

# Entire map image
my $name = $tube->name;
open(my $MAP_IMAGE, ">", "$name.png")
    or die "ERROR: Can't open [$name.png]: $!";
binmode($MAP_IMAGE);
print $MAP_IMAGE decode_base64($tube->as_image);
close($MAP_IMAGE);

# Just a particular line map image
my $line = 'Bakerloo';
open(my $LINE_IMAGE, ">", "$line.png")
    or die "ERROR: Can't open [$line.png]: $!";
binmode($LINE_IMAGE);
print $LINE_IMAGE decode_base64($tube->as_image($line));
close($LINE_IMAGE);

Please refer to the documentation for more details.

Map::Tube::Plugin::Formatter

The Map::Tube::Plugin::Formatter plugin adds the support to format the object supported by the plugin.

use strict; use warnings;
use Map::Tube::London;

my $tube = Map::Tube::London->new;

my $node = $tube->get_node_by_name('Baker Street');
print $node->to_xml,    "\n\n";
print $node->to_json,   "\n\n";
print $node->to_yaml,   "\n\n";
print $node->to_string, "\n\n";

my $line = $tube->get_line_by_name('Metropolitan');
print $line->to_xml,    "\n\n";
print $line->to_json,   "\n\n";
print $line->to_yaml,   "\n\n";
print $line->to_string, "\n\n";

my $route = $tube->get_shortest_route('Baker Street', 'Wembley Park');
print $route->to_xml,   "\n\n";
print $route->to_json,  "\n\n";
print $route->to_yaml,  "\n\n";
print $route->to_string,"\n\n";

Please refer to the documentation for more info.

Map::Tube::Plugin::FuzzyFind

Gisbert W. Selke, built the add-on for Map::Tube to find stations and lines by name, possibly partly or inexactly specified. The module is a Moo role which gets plugged into the Map::Tube::* family automatically once it is installed.

use strict; use warnings;
use Map::Tube::London;

my $tube = Map::Tube::London->new();

print 'line matches exactly: ', scalar($tube->fuzzy_find(search => 'erloo', objects => 'lines')), "\n";
print 'line contains       : ', scalar($tube->fuzzy_find(search => 'erloo', objects => 'lines', method  => 'in')), "\n";

Please refer to the documentation for more info.

MAP DATA FORMAT

Map data can be represented in JSON or XML format. The preferred format is JSON. Map::Tube v3.23 or above comes with a handy script map-data-converter, that can be used to change the data format of an existing map data.Below is how we can represent the sample map:

  A(1)  ----  B(2)
 /              \
C(3)  --------  F(6) --- G(7) ---- H(8)
 \              /
  D(4)  ----  E(5)

JSON

{
    "name"  : "sample map",
    "lines" : {
        "line" : [
            { "id" : "A", "name" : "A", "color" : "red"     },
            { "id" : "B", "name" : "B", "color" : "#FFFF00" }
        ]
    },
    "stations" : {
        "station" : [
            { "id" : "A1", "name" : "A1", "line" : "A",   "link" : "B2,C3"    },
            { "id" : "B2", "name" : "B2", "line" : "A",   "link" : "A1,F6"    },
            { "id" : "C3", "name" : "C3", "line" : "A,B", "link" : "A1,D4,F6" },
            { "id" : "D4", "name" : "D4", "line" : "A,B", "link" : "C3,E5"    },
            { "id" : "E5", "name" : "E5", "line" : "B",   "link" : "D4,F6"    },
            { "id" : "F6", "name" : "F6", "line" : "B",   "link" : "B2,C3,E5" },
            { "id" : "G7", "name" : "G7", "line" : "B",   "link" : "F6,H8"    },
            { "id" : "H8", "name" : "H8", "line" : "B",   "link" : "G7"       }
        ]
    }
}

XML

<?xml version="1.0" encoding="UTF-8"?>
<tube name="sample map">
    <lines>
        <line id="A" name="A" color="red"    />
        <line id="B" name="B" color="#FFFF00"/>
    </lines>
    <stations>
        <station id="A1" name="A1" line="A"   link="B2,C3"   />
        <station id="B2" name="B2" line="A"   link="A1,F6"   />
        <station id="C3" name="C3" line="A,B" link="A1,D4,F6"/>
        <station id="D4" name="D4" line="A,B" link="C3,E5"   />
        <station id="E5" name="E5" line="B"   link="D4,F6"   />
        <station id="F6" name="F6" line="B"   link="B2,C3,E5"/>
        <station id="G7" name="G7" line="B"   link="F6,H8"   />
        <station id="H8" name="H8" line="B"   link="G7"      />
    </stations>
</tube>

MAP VALIDATION

DATA VALIDATION

The package Test::Map::Tube can easily be used to validate raw map data. Anyone building a new map using Map::Tube is advised to have a unit test as a part of their distribution.Just like in Map::Tube::London package,there is a unit test something like below:

use strict; use warnings;
use Test::More;
use Map::Tube::London;
use Test::Map::Tube;
ok_map(Map::Tube::London->new);

FUNCTIONAL VALIDATION

The package Test::Map::Tube can easily be used to validate map basic functions provided by Map::Tube.

use strict; use warnings;
use Test::More;
use Test::Map::Tube;
use Map::Tube::London;
ok_map_functions(Map::Tube::London->new);

It can also easily be used to validate map routing functions provided by Map::Tube:

use strict; use warnings;
use Test::More;
use Test::Map::Tube;
use Map::Tube::London;
my $map = Map::Tube::London->new;
my @routes = (
    "Route 1|Tower Gateway|Aldgate|Tower Gateway,Tower Hill,Aldgate",
    "Route 2|Liverpool Street|Monument|Liverpool Street,Bank,Monument",
);
ok_map_routes($map, \@routes);

AUTHOR

Mohammad Sajid Anwar, <mohammad.anwar at yahoo.com>

REPOSITORY

https://github.com/manwar/Map-Tube

SEE ALSO

CONTRIBUTORS

  • Michal Špaček, <skim at cpan.org>

  • Slaven Rezic, <srezic at cpan.org>

  • Gisbert W. Selke, <gws at cpan.org>

  • Toby Inkster, <tobyink at cpan.org>

  • Ed J, <etj at cpan.org>

BUGS

Please report any bugs or feature requests through the web interface at https://github.com/manwar/Map-Tube/issues. I will be notified and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Map::Tube

You can also look for information at:

LICENSE AND COPYRIGHT

Copyright (C) 2010 - 2024 Mohammad Sajid Anwar.

This program is free software; you can redistribute it and / or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:

http://www.perlfoundation.org/artistic_license_2_0

Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License.By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made by someone other than you,you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement,then this Artistic License to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.