#!/usr/bin/perl # script to convert a polygon file to an OSM file for # editing in JOSM etc. # the polygon file is expected to have the structure # # line 1: symbolic name # line 2: id of first polygon # lines 3-n: coordinates of first polygon, each line beginning with # whitespace, then having lon and lat of the point in scientific notation # line n+1: END # the lines 2 to n+1 may then be repeated for further polygons, and the last line must be # END (so that the file ends with 2 lines having "END"). # # written by Frederik Ramm , public domain. my %nodehash; # first line # (employ workaround for polygon files without initial text line) my $poly_file = <>; chomp($poly_file); my $workaround = 0; if ($poly_file =~ /^\d+$/) { $workaround=$poly_file; $poly_file="none"; } my $nodecnt = -1; my $waycnt = -1; my $nodes; my $ways; my $note = " \n"; while(process_poly()) { undef $workaround; }; print "\n"; print $nodes; print $ways; print "\n"; sub process_poly() { my $poly_id; if ($workaround==0) { $poly_id=<>; } else { $poly_id=$workaround; } chomp($poly_id); my $startnode = $nodecnt; return 0 if ($poly_id =~ /^END/); # end of file $ways .= sprintf(" \n \n \n", $waycnt--, $poly_id, $poly_file); $ways .= $note; while($line = <>) { last if ($line =~ /^END/); # end of poly my ($dummy, $x, $y) = split(/\s+/, $line); my $existingnode = $nodehash{"$x|$y"}; if (defined($existingnode)) { $ways .= sprintf(" \n", $existingnode); } else { $nodehash{"$x|$y"} = $nodecnt; $ways .= sprintf(" \n", $nodecnt); $nodes .= sprintf(" \n", $nodecnt--, $y, $x); } } $ways .= " \n"; return 1; }