Skip to content

Instantly share code, notes, and snippets.

@soh335
Created November 11, 2010 04:39
Show Gist options
  • Save soh335/672017 to your computer and use it in GitHub Desktop.
Save soh335/672017 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
use Imager;
my $xml = XML::Simple->new;
my $data = $xml->XMLin('map.osm');
my $output = "sample.png";
my $bounds = $data->{bounds};
# base
my $rgb = Imager->new(xsize => 400, ysize => 400);
$rgb->box(filled => 1, color => 'white');
# draw
for my $way_id (keys(%{$data->{way}})) {
my @nodes = @{$data->{way}->{$way_id}->{nd}};
my $count = @nodes;
my $i = 0;
while ( ++$i < $count - 1) {
#warn $i." x1 ".$data->{node}->{$nodes[$i-1]->{ref}}->{lon};
#warn $i." x2 ".$data->{node}->{$nodes[$i]->{ref}}->{lon};
my $lon2 = $data->{node}->{$nodes[$i-1]->{ref}}->{lon};
my $lon1 = $data->{node}->{$nodes[$i]->{ref}}->{lon};
my $lat2 = $data->{node}->{$nodes[$i-1]->{ref}}->{lat};
my $lat1 = $data->{node}->{$nodes[$i]->{ref}}->{lat};
my $x1 = (($lon1 - $bounds->{minlon}) / ($bounds->{maxlon} - $bounds->{minlon})) * 400;
my $x2 = (($lon2 - $bounds->{minlon}) / ($bounds->{maxlon} - $bounds->{minlon})) * 400;
my $y1 = (($lat1 - $bounds->{minlat}) / ($bounds->{maxlat} - $bounds->{minlat})) * 400;
my $y2 = (($lat2 - $bounds->{minlat}) / ($bounds->{maxlat} - $bounds->{minlat})) * 400;
$rgb->line( color => 'black', x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y2, aa => 1 );
}
}
$rgb->flip( dir => 'v' );
# output
$rgb->write( file => $output ) or die $rgb->errstr;
`open $output`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment