source: subversion/applications/rendering/imgAtlas/imgAtlas.pl @ 2667

Last change on this file since 2667 was 2530, checked in by jochen, 13 years ago
  • moving things around
  • Property svn:executable set to *
File size: 7.4 KB
Line 
1#!/usr/bin/perl
2#-----------------------------------------------------------------------------
3# Document-creation system, for PDF output
4# Supports the layout of:
5#  * JPEG photos
6#  * OpenStreetMap maps
7#  * Simple text
8#
9#-----------------------------------------------------------------------------
10# Copyright 2007
11#  * Oliver White
12#
13#    (Add your name here if you edit the program)
14#-----------------------------------------------------------------------------
15# This program is free software; you can redistribute it and/or
16# modify it under the terms of the GNU General Public License
17# as published by the Free Software Foundation; either version 2
18# of the License, or (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program; if not, write to the Free Software
27# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
28#-----------------------------------------------------------------------------
29use strict;
30use osm::getTileArea;
31use PDF::API2;
32use constant mm => 25.4 / 72;
33my $Usage = "$0 [config file] [output pdf filename]\n";
34my $Config = shift() || "config.txt";
35my $Filename = shift() || "atlas.pdf";
36
37open(COMMANDS, '<', $Config) || die($Usage);
38my $PDF = PDF::API2->new();
39my ($Page, @Attributions);
40my $PageNum = 1;
41# Default options
42my %Option = (dpi=>150); 
43
44newPage();
45my $Font = $PDF->corefont('Helvetica');
46
47 
48while(my $Line = <COMMANDS>){
49  $Line =~ s{     # replace:
50    ^             # at start of line
51    \s*           #
52    \#            # Comment characters
53    .*            # ...
54    $             # to end of line
55    }{}x;         # with nothing
56
57  #---------------------------------------------------------------------------
58  #  New page
59  #---------------------------------------------------------------------------
60  if($Line =~ m{==page==}i){
61    $Page = $PDF->page;
62    $PageNum++;
63  }
64  #---------------------------------------------------------------------------
65  #  Change an option
66  #---------------------------------------------------------------------------
67  elsif($Line =~ m{
68    option:       # command
69    \s*           #
70    (\w+)         # option name
71    \s* = \s*     #  =
72    (.*)          # value
73    }x){
74    $Option{$1} = $2;
75    print "Setting option \"$1\" to \"$2\"\n";
76  }
77  #---------------------------------------------------------------------------
78  #  Add a line of text
79  #---------------------------------------------------------------------------
80  elsif($Line =~ m{
81    text:               # command
82    \s*                 #
83    (.*?)               # text
84    \s*                 #
85    \(                  # (
86    (.*?), \s*          #  x
87    (.*?), \s*          #  y
88    (.*?)               #  size
89    \)                  # )
90    }x){
91    print "Adding \"$1\" at $2, $3, size $4\n";
92    textAt($1,$2,$3,$4);
93  }
94  #---------------------------------------------------------------------------
95  #  Add a list of images used within the document
96  #---------------------------------------------------------------------------
97  elsif($Line =~ m{
98    attribution:        # command
99    \s*                 #
100    \(                  # (
101    (.*?), \s*          #  x
102    (.*?), \s*          #  y
103    (.*?)               #  size
104    \)                  # )
105    }x){
106    printf "Adding attribution block\n";
107    my $X = $1;
108    my $Y = $2;
109    my $Size = $3;
110   
111    # Add a title, just as an extra line of text at the beginning of the textblock
112    unshift(@Attributions, "Image credits:");
113   
114    # Draw each line of text (TODO: make a generic "write array of text lines")
115    foreach my $Line (@Attributions){
116      textAt($Line, $X, $Y, $Size);
117      $Y -= $Size * 1.25;
118    }
119  }
120  #---------------------------------------------------------------------------
121  #  Add a JPEG image to the page
122  #---------------------------------------------------------------------------
123  elsif($Line =~ m{
124    image:             # Command
125    \s*                #
126    (.*?)              # Filename
127    \s*                #
128    \(                 # (
129    (\d+), \s*         #  left
130    (\d+), \s*         #  bottom
131    (\d+), \s*         #  width
132    (\d+), \s*         #  height
133    \"(.*?)\",\s*      #  author
134    \"(.*?)\"          #  description
135    \)                 # )
136    }ix)
137    {
138    print "Adding $1 ($7) by $6 at $2, $3, $4, $5\n";
139    my $PageGfx = $Page->gfx;
140    my $Image = $PDF->image_jpeg($1) || print("Failed to load");
141    $PageGfx->image($Image, $2/mm, $3/mm, $4/mm, $5/mm ); # from left, from bottom, width, height
142   
143    # Add to the list of images
144    # (this text needs to identify which image we're talking about, hence the description)
145    push(@Attributions, "Page $PageNum: Image \"$7\", by $6");
146    }
147  #---------------------------------------------------------------------------
148  #  Add a map to the page
149  #---------------------------------------------------------------------------
150  elsif($Line =~ m{
151    map:               # Command
152    \s*                #
153    \((.*?)\)          # Map options
154    \s*                #
155    at                 # at
156    \s*                #
157    \((.*?)\)          # Position options
158    }ix)
159    {
160    my ($MapOptions, $PositionOptions) = ($1, $2);
161    my ($Lat, $Long, $SizeKm, $Zoom) = split(/\s*,\s*/, $MapOptions);
162    my ($X,$Y,$W,$H) = split(/\s*,\s*/, $PositionOptions);
163   
164    # Process those map options
165    my $Filename = datafile('png');
166    my $AspectRatio = $W / $H;
167    my $ImgW = ($W / 25) * $Option{dpi}; # pixels = width in inches * DPI
168    my $ImgH = $ImgW / $AspectRatio;
169   
170    my $SizeOfTileKm = 10 * (2 ** (12 - $Zoom));  # Z-12 is about 10km, and each additional zoom halves that
171    my $WidthInTiles = $SizeKm / $SizeOfTileKm;   # How many tiles should fit across the map
172    my $SizeOfTilePx = $ImgW / $WidthInTiles;     # How wide each tile should be made, in pixels
173    print "$SizeKm km across, $SizeOfTileKm km per tile =  $WidthInTiles tiles, so $SizeOfTilePx px per tile\n";
174    my %Area = (lat=>$Lat, long=>$Long, zoom=>$Zoom, width=>$ImgW, height=>$ImgH, size=>$SizeOfTilePx);
175   
176    # Create the map
177    print "Creating map around $Lat, $Long, from zoom $Zoom tiles\nUsing image $ImgW x $ImgH\n";
178    getTileArea::createArea(\%Area, $Filename);
179
180    # Add map to page
181    my $PageGfx = $Page->gfx;
182    my $Image = $PDF->image_png($Filename) || print("Failed to load");
183    $PageGfx->image($Image, $X/mm, $Y/mm, $W/mm, $H/mm ); # from left, from bottom, width, height
184
185    }
186  #---------------------------------------------------------------------------
187  #  Unrecognised lines of input
188  #---------------------------------------------------------------------------
189  else{
190    #print "Misunderstood $Line\n";
191  }
192 
193 
194}
195
196print "Saving PDF to $Filename\n";
197$PDF->saveas($Filename);
198
199BEGIN {
200  my $NextFileNum = 1;
201  sub datafile{
202    my $Suffix = shift();
203    return
204      sprintf("Data/%05d.$Suffix", $NextFileNum++);
205  }
206}
207sub textAt{
208  my ($Text, $X, $Y, $Size) = @_;
209 
210  my $TextHandler = $Page->text;
211  $TextHandler->font($Font, $Size/mm );
212  $TextHandler->fillcolor('black');
213  $TextHandler->translate($X/mm, $Y/mm);
214  $TextHandler->text($Text);
215 
216}
217sub newPage{
218  # A4 page
219  $Page = $PDF->page;
220  $Page->mediabox(210/mm, 297/mm);
221  $Page->cropbox (10/mm, 10/mm, 200/mm, 287/mm);
222}
Note: See TracBrowser for help on using the repository browser.