Skip to main content

Print countries in ASCII Art based on Natural Earth shape files.

Project description

Natural Earth

asciimap

Print countries in ASCII Art based on shape files from Natural Earth project

Code Style: Black License Python Version GitHub tag (latest SemVer) Travis (.com) branch

          *-------*                                       $-------$                     
         /......./                                       /       /                      
        *......./                                       $       /                       
        *......*                                        $      $                        
       *........*------*                               $        $------$                
       *...............*                               $               $                
      *...............*                               $               $                 
      *...............*                               $               $                 
       \............./                                 \             /                  
        *.........../                                   $           /                   
        |..........*                                    |          $                    
        *.**........*--*                                $ $$        $--$                
         *  *...........*                                $  $           $               
            *...........*                                   $           $               
   *       *.............\                         $       $             \              
*-/.\      *-*............\                     $-/ \      $-$            \             
 |...**       *--*.........\                     |   $$       $--$         \            
 |.....*          \.........*-*                  |     $          \         $-$         
*--*../            *......*...*                 $--$  /            $          $         
    **              United Kingdom                  $$              United Kingdom      
                    *.....*....*                                    $          $        
                    *...........\                                   $           \       
             *-----*.............*                           $-----$             $      
            *....................*                          $                    $      
            *.....................*-*                       $                     $-$   
             \.......................*-*                     \                       $-$
              *........................|                      $                        |
            **......................../*                    $$                        /$
          **.........................*                    $$                         $  
           *---*.....................|                     $---$                     |  
                *....................|                          $                    |  
               /.....................**                        /                     $$ 
             **..............*--*..**                        $$              $--$  $$   
           **......*-*.*----*    **                        $$      $-$ $----$    $$     
          /..\..../   *                                   /  \    /   $                 
         *.** *--*                                       $ $$ $--$                      
         **                                              $$                             

You can print any country with asciimap for which a Geometry exists (currently 177 countries). In general printing is pretty fast though big countries like USA, Russia, Canada etc. need some extra computation time. To circumvent loading times store the results in a database or similar. Disk space isn't much for normal 40x80 sized ascii maps.

Dependencies

You need the python bindings for GDAL and GDAL itself installed. I experienced problems installing the python bindings into a virtualenv, so I recommend installing the system packages.

Arch

pacman -Sy gdal python-gdal

Debian/Ubuntu

apt-get update
apt-get install gdal-bin libgdal-dev python3-gdal

Installation

PyPi

Installation with pip is straightforward:

sudo pip install asciimap

Git

Installation with git from master branch:

git clone git@github.com:MaelStor/asciimap.git
cd asciimap
git checkout master
In userspace

Install in user space with:

pip install --user .
System

Install system wide with:

sudo pip install .

Usage

usage: asciimap [-h] [--fill FILL] [--empty EMPTY] [--outside OUTSIDE]
                [--height HEIGHT] [--width WIDTH] [--blur BLUR]
                [--method {full,f,dynamic,d,height,h,width,w}]
                [--surface SURFACE] [--negative] [--benchmark]
                country

Print countries in ASCII Art

positional arguments:
  country               Select country by ISO 3166-1 alpha-2 codes. For a
                        complete list of ISA A2 codes use 'list' as argument

optional arguments:
  -h, --help            show this help message and exit
  --fill FILL, -f FILL  Single character marking the edges of the land surface
  --empty EMPTY, -e EMPTY
                        The character to use for the land surface
  --outside OUTSIDE, -o OUTSIDE
                        Single character marking the outside surface
  --height HEIGHT, -i HEIGHT
                        Height of the map as integer
  --width WIDTH, -w WIDTH
                        Width of the map as integer
  --blur BLUR, -b BLUR  Add blur to radius and inflate the surface by double
                        value
  --method {full,f,dynamic,d,height,h,width,w}, -m {full,f,dynamic,d,height,h,width,w}
                        Change rendering method
  --surface SURFACE, -s SURFACE
                        Choose a surface by number or 'all'
  --negative, -n        Print the negative
  --benchmark, -t       Print execution times of methods along with the map

List all countries and ISO 3166-1 alpha-2 codes with 'list'

Examples

$ asciimap it
                  *-*                                                           
              *--*   \                                                          
              /       *---*                                                     
         *---*            |                                                     
*    *--*               *-|                                                     
*---*                 **  *                                                     
 |                   /                                                          
 *                  *                                                           
*                   |                                                           
 \                  *                                                           
 *\   *---*          *                                                          
   \ /     **        *                                                          
   **        \        **                                                        
              *         \*                                                      
              |          \                                                      
              *           \                                                     
               \    Italy  \                                                    
                \           \                                                   
                 **          \                                                  
                   \          **                                                
                    **          *--*                                            
                      *-*         \                                             
                         **        *-*                                          
        **                 **         **                                        
      */  \                  **         \*                                      
      |    *                   \      *----*                                    
      |    *                    \    /    *-*                                   
      |   *                      \  *                                           
      |   |                       \  \                                          
      *  /*                        *  **                                        
       **                          |  /                                         
                                   | /                                          
                                   |/                                           
                                  *|                                            
                      *-*   *---* **                                            
                      |  *-*    *                                               
                      *-*      *                                                
                         *--*  |                                                
                             *-|                                                
                               *                                                
$ asciimap no
                       *------------*                                           
            *-*   *---*   |-----------*                                         
          **   *-*     \--/                                                     
            **          /*                                                      
              **      **    *--*                                                
                **   /        **                                                
                  *--*                                                          










                                  *--------*                                    
                             ** **   /---\  \                                   
                        *----* /    /     *--*                                  
                  Norway  /   *-----*                                           
                    *-* **                                                      
                  *-/                                                           
                ** /                                                            
               /  /                                                             
              /  /                                                              
             /  /                                                               
           **  /                                                                
          /  *-*                                                                
        **  *                                                                   
      **    *                                                                   
   *-*     *                                                                    
 **        *                                                                    
*           *                                                                   
|           |                                                                   
|           |                                                                   
*           *                                                                   
 *      *--*                                                                    
 *    **                                                                        
  *--*                                                                          

You can print a country looking like a negative with

$ asciimap --surface 1 fr --negative
..............................**................................................
............................     ...............................................
............................        ............................................
...........................          ...........................................
...........................            .........................................
........................                .  .....................................
............ ........                        ...................................
............   ...                             .................................
.............                                     ..............................
.............                                           ........................
....    .....                                            .......................
*                                                       ........................
|                                                       ........................
|                                                      .........................
*                                                      .........................
....                                                   .........................
........                                           .  ..........................
.........                                           ............................
...........                                       ..............................
............                                     ...............................
.............                                   ................................
..............                                  ................................
...............                                    .............................
...............                                     ............................
...............                                     ............................
...............                                     ............................
...............                                      ...........................
...............                                     ............................
...............                                     ............................
...............                                     ............................
...............                                     ............................
...............                                      ...........................
..............                                         .........................
..............                                         .........................
.............                                         ..........................
.............                           ....        ............................
..............                      ............................................
.................                  ............................ ................
.....................  ..          ..........................   ................
.......France...............*----*..........................*--*................

or if you want the borders showed:

$ asciimap --surface 1 --outside '.' fr
..............................**................................................
............................*/  \...............................................
............................/    *-*............................................
...........................*        \...........................................
...........................*         **.........................................
........................*-*            \.**.....................................
............*........*-*                *  **...................................
............*-*...*-*                        **.................................
.............| *-*                             *-*..............................
.............|                                    *----*........................
....*--*.....*                                          *.......................
*--*    *---*                                          *........................
|                                                      *........................
|                                                     *.........................
*--*                                                  |.........................
....*--*                                           *  *.........................
........\                                         *.**..........................
.........**                                       **............................
...........\                                     /..............................
............\                                   /...............................
.............\                                 *................................
..............\                                *................................
...............*                                *-*.............................
...............|                                   *............................
...............|                                   |............................
...............|                                   *............................
...............|                                    *...........................
...............|                                   *............................
...............|                                   |............................
...............|                                   |............................
...............|                                   *............................
...............*                                    \...........................
..............*                                      \*.........................
..............*                                       *.........................
.............*                          *--*        **..........................
.............*                      *--*....*------*............................
..............*-*                  /............................................
.................*--*  **         *............................*................
.....................**..*-*      *..........................*-|................
.......France...............*----*..........................*--*................

Fillings can be combined. If you want to fill the land surface with ., everything outside the land surface with ~ and corners with ^ use:

$ asciimap nz --outside '~' --empty '.' --fill '^'
~~~~~~~~~~~~~~~~~~~~^^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~|\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~^.^^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~\..^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~\.^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~\.^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~\|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~|\~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~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~
~^............^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~^.........../~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
^.........../^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
^---^.....^^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~^---^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Advanced usage

Some advanced usage with command-line tools. The picture in the title was produced with

$ asciimap gb --height 30 --width 31 | paste - <(asciimap gb --height 30 --width 31 --fill "$") | paste - <(asciimap gb --height 30 --width 31 --fill "'")

another example with 'paste' and usage of 'blur':

$ asciimap -b 0 jp -i 30 -w 30 | paste - <(asciimap -b 2.0 jp -i 30 -w 30)
                                                     *        
                       *                           */ **      
                       |\                          /    *--*  
                       / **  *                    /         \ 
                      /    \-*                   /           *
                    **     /*                   *            |
                   *  *-* /                     |            |
                   *-*   *                      |            *
                                                |           / 
                    *-*                         |         **  
                    | |                         |        /    
                    | |                         *       *     
                    * |                        /        *     
                   *  *                     *-*        *      
                   * *                     /   Japan   *      
                Japan                  *--*           *       
                *    *               **               |       
             *-*    *               /                 |       
            *       |              /                  *       
        *-* *       *            **                  *        
     *-*  \*  *----*            *                    *        
    // \ / | /                  |                 *-*         
  *-/ \ /* **                   *              *-*            
 / \  |-*                        *         *--*               
**  * *                          |       **                   
  \/                             *      /                     
  |*                              \    /                      
  *                                *--*                       
$ asciimap au --blur 0.1 --empty '.' --method full
                                                         **                     
                                      *-*                /\                     
                                  *--*...*----*         *..*                    
                                 /..........|           |..*                    
                        *---*   /...........|           |...**                  
                       /.....*-*............*           *.....\*                
                      /......................*-*       *.......\                
                  *--*..........................*-*    *........*               
                  /................................*--*.........|               
                 /..............................................*               
                /................................................**             
           *---*...................................................*-*          
     *----*...........................................................\         
   **..................................................................\        
 */.....................................................................**      
 /........................................................................\     
*..........................................................................**   
 *...........................................................................\  
*.........................................Australia...........................* 
 *............................................................................| 
 *............................................................................* 
  \............................................................................*
   *...........................................................................*
   *..........................................................................* 
    \...........................*--*..........................................| 
     *...................*-----*    *---*.....................................* 
     *................*-*                \......*...........................**  
    /........*-------*                    \...** *........................./    
   *-\....*-*                              *-*  /*......................../     
      *--*                                     ** \....................../      
                                                   \....................*       
                                                    \...................|       
                                                     *-*............*---*       
                                                        *----------*            


                                                              **   *-*          
                                                               |--*..*          
                                                               *...**           
                                                                *-*             

Sometimes a country has more than one land surface (or more precise: Polygons) defined, what scrambles the surface of interest when printing all surfaces:

$ asciimap --surface all --method full ru --fill "'"
                                                  '          '                  
                                                             '                  
                                                              '                 

                                                              ''                
                                                     '      ''  '      '        
                                                    '      /    |               
                                                    '     '     '      '        
                                                       '  '      '--'  |        
                                                   '   |-'          \  '\       
                                                    \  | '           ''  \'     
                                                     \ |/                 \   ' 
                                              ''     '-| '                 '-' '
'                                              \'  ''  \ '                     |
|'                                              \ /     '                      |
'|                                             ' /                             |
 '                                             |/                              |
                                               |                               |
                                               |                               '
                                               '             Russia        '  / 
                                              '                           / ''  
                                              |                        '-'      
                                              |                       '    '    
                                              |                       |    |    
                                              '                       '    |    
                                               '       '             '     |    
                                            '  |      / '            '     |    
                                               |     '  '          '  \    '    
                                               '   ' '   \        ' '  '        
                                                ' ' '     \ ' '   | |  '        
                                                ' '        ' ' \ /' | '         
                                                 '         '    '   ' |         
                                                 |                   \|         
                                                 ''                   |         
                                                / |                   |         
                                               '  '                   '         
                                               '-'                   '          
                                                 \'                  '          
                                                  |                             
                                                  '                             

To overcome the bias you can just render the big land surface for example with:

$ asciimap --surface 1 --method height ru --fill "'" 
                                       '----'                                 ''
                                 '----'                             '--------' |
                               '--'                             '--'           |
                              /-'                         '----'               |
                            '-|               '--'  '     /                    |
                           '' |              /  \  ' '---'                     |
                             '--'          ''    ' |                           |
                                            \    | '                           |
 '-------'                           '---'   '   |  '                          |
  |       '--'    '-'     '---------'     '-'    '\ '                          |
  '    '-'    '   /\   '-'                      '  /                           |
   '  '   '--'  ''  '-'                         ' /                            |
   |   '      ''                                 '                             |
   |   '   '-'                                                                 |
   '    '-'                                                                    |
    '                                                                          |
   /                                                                     Russia|
 ''                                                                            |
  '                                                                            |
'/                                                                             |
'                                                                              |
 \                                                                             |
 '-'                                                                           |
    \                                     '----'                               |
    '\                               '---'      '-----'                        |
      \                             '                  \                       |
     '-\                  ''        |                   \                     /|
        \               ''  '-----' '                    '---'               / '
         '--'         ''           '                          ''    '-------'   
             \       /                                          '--'        '   
              '     '                                                           
              '      \                                                          
             /        \                                                         
            /         ''                                                        
      '----'         /                                                          
            '-'     /                                                           
               '---' \                                                          
                    '-'                                                         
                      '                                                         

what cuts off the eastern part of Russia. To print the whole surface:

$ asciimap --surface 1 --method width --height 20 ru --fill "'"
                                      '-'                                       
                    ''                 / '-'                                    
               ''              '------'    \                                    
             ''       ''    '-'             '-------'      ''                   
              '\     ' | '-'                         '----'  '------'           
 '--'           \ ''  /' /                                           '---' '---'
 |  '-'  ' '---' '  '-' /                                                 '    |
 ' /   '' '            '                                                       |
  / '-'                                                             ' '     '--'
 /                                  Russia                      '  / \ '---'    
'                                                          '---' ''   /         
'\                                                       ''         '-'         
  \               '----' '                              '          ' |          
   \        '    /      ' \          '           '-'     '' '      '-'          
    ''    '' '---'         '-'   '--' '--'   '' /   \      /|                   
      '  '                    '-'         '-'  '     \ '  ' '                   
      |  '                                            ' \ '                     
   '  |   '                                             |/                      
      '--'                                            '-'                       
          '                                                                     

To find the right surface you have to try around a bit. I'm working on a better solution.

Performance

Rendering time heavily depends on the count of Polygons and the size of the resulting map. Printing a common sized country with 40x80 (h x w) usually takes about 0.3 - 0.7 seconds on a 2-core system. Rendering is implemented to use parallel processes and drastically reduces computation time on multicore systems.

$ asciimap de --benchmark
parse_undefined_countries    0.0001 s
_get_boundaries              0.0000 s
__init__                     0.0007 s
render_parallel              0.2032 s
is_border                    0.0440 s
is_vertical                  0.0034 s
is_horizontal                0.0033 s
is_negativ_diagonal          0.0032 s
is_positiv_diagonal          0.0031 s
print_map                    0.0919 s
-------------------------------------
Sum                          0.3529 s

Bigger countries like USA, Russia, Canada etc. take a little more time. Something between 0.7 - 1.5 seconds.

$ asciimap us -t
_get_boundaries              0.0001 s
__init__                     0.0036 s
render_parallel              0.6032 s
is_border                    0.0324 s
is_vertical                  0.0029 s
is_horizontal                0.0028 s
is_negativ_diagonal          0.0027 s
is_positiv_diagonal          0.0027 s
print_map                    0.0697 s
-------------------------------------
Sum                          0.7200 s

You can run asciimap with --benchmark if you want to know execution times of all used methods. Times for each method add up in case a method/function is called multiple times.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asciimap-0.1.1.tar.gz (231.9 kB view hashes)

Uploaded Source

Built Distribution

asciimap-0.1.1-py3-none-any.whl (225.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page