#!/usr/bin/perl # convert Fig file to OZI files (as used by Mapdekode) # Andrew Daviel TRIUMF Aug 2007 advax@triumf.ca # usage: fig2ozi # creates multiple .plt files; best run in a subdirectory # these parameters should be moved to a data file ..... # map depth to OZI data types or area types $type{41} = 0 ; # Lat/Long ticks (black) $type{42} = 0 ; # Lat/Long grid (black) $type{43} = 0 ; # Lat/Long points (yellow) $type{49} = 0 ; # Park Boundary (green) $atype{49} = '20' ; # Park Boundary (green) -> State Park - green area $type{50} = '15' ; # Lakes (blue) -> Shoreline - $type{45} = 0 ; $atype{45} = '41' ; # Lakes -> small Lake $atype{46} = '40' ; # Lakes -> small Lake $type{51} = '18' ; # Streams (blue) -> Stream $type{52} = '03' ; # Highway (red) -> Principal HWY $type{53} = '04' ; # Major road (red) -> Road $type{54} = '05' ; # Minor road (red) -> Unpaved Road $type{55} = 0 ; # image ID (purple) $type{56} = 0 ; # clearcut (green) $atype{56} = '54' ; # clearcut -> Area $type{57} = '16' ; # track (brown/red) -> Trail $type{60} = 0 ; # Satellite image # mapping of Xfig coordinates (X,Y) to WGS84 Lat/Long (decimal degrees) # e.g. 2 diagonal corners $lat1 = 49.31 ; $Y1 = -68325 ; $lat2 = 49.23 ; $Y2 = 17400 ; $long1 = -122.32 ; $X1 = 32100 ; $long2 = -122.42 ; $X2 = -37800 ; $yscale = ($lat1 - $lat2) / ($Y1 - $Y2) ; $xscale = ($long1 - $long2) / ($X1 - $X2) ; # max,min used for overview map $minlat = 49.22 ; $maxlat = 49.40 ; $minlong = -122.45 ; $maxlong = -122.30 ; unless ($ARGV[0]) { &usage ; exit ; } @mon= ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec') ; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime() ; $date = sprintf("%2.2d-%s-%2.2d",$mday,$mon[$mon],$year-100) ; $time = sprintf("%2.2d:%2.2d:%2.2d",$hour,$min,$sec) ; print "$date,$time\n" ; # text see /home/andrew/vrweb/fonts $file = shift ; open (IN,"$file.fig") or die ; $line = 0 ; $fname = '' ; while () { chomp ; $line++ ; tr/ / /s ; if (/^# (.*)/) { $fname = $1 ; } if (/^2 1 / or /^2 3 / or /^3 0 / or /^3 2 / or /^3 1 / or /^3 3 /) { # open line, closed line (polygon), approx spline, inter spline, approx closed spline, inter closed spline if (/^2 /) { # line ($t,$t2,$style,$width,$fg,$bg,$depth,$u2,$fill,$angle,$u3,$u4,$u5,$u6,$u7,$npts) = split(/ /) ; print "line $t2 width $width fg $fg bg $bg depth $depth $npts points $fname\n" ; $width{"$depth;$fname"} = $width ; } elsif (/^3 /) { # spline ($t,$t2,$style,$width,$fg,$bg,$depth,$u2,$fill,$angle,$u3,$u4,$u5,$npts) = split(/ /) ; print "spline $t2 width $width fg $fg bg $bg depth $depth $npts points $fname\n" ; $width{"$depth;$fname"} = $width ; } $fname = '' ; } elsif (/^[\d] /) { print "Line $line Unsupported $_\n" ; # ^1 3 circle radius # ^4 text # ^2 2 box # ^2 4 rounded box # ^5 1 arc # ^6 compound # ^1 1 ellipse radius # ^1 4 circle diameter # ^1 2 ellipse diameter $fname = '' ; } } foreach $key ( keys %width ) { $maxzone = $width{$key} - 1 ; if ($maxzone <= 0) { $maxzone = 0 ; } ($indepth,$name) = split(/;/,$key) ; $fname = "$file-$name-$indepth" ; $fname =~ tr/ //d ; open (IN,"$file.fig") or die ; $line = 0 ; $type = $type{$indepth} ; if ($type{$indepth}) { $type = $type{$indepth} ; open (OUT,">$fname-$type-L.plt") or die ; print OUT<$fname-$type-F.plt") or die ; print OUT<) { chomp ; $line++ ; tr/ / /s ; if (/^# (.*)/) { $name1 = $1 ; } if (/^2 1 / or /^2 3 / or /^3 0 / or /^3 2 / or /^3 1 / or /^3 3 /) { # open line, closed line (polygon), approx spline, inter spline, approx closed spline, inter closed spline if (/^2 /) { # line ($t,$t2,$style,$width,$fg,$bg,$depth,$u2,$fill,$angle,$u3,$u4,$u5,$u6,$u7,$npts) = split(/ /) ; } elsif (/^3 /) { # spline ($t,$t2,$style,$width,$fg,$bg,$depth,$u2,$fill,$angle,$u3,$u4,$u5,$npts) = split(/ /) ; } if ($depth == $indepth and $name1 eq $name ) { if ($t == 2) { print "line $t2 $npts points " ; } elsif ($t == 3) { print "spline $t2 $npts points " ; } $start = 1 ; while (1) { unless ($npts) { last ; } $_ = () ; chomp ; $line++ ; unless (/^\t[\s]*[\d\-]+/) { print "Line $line expected TAB + digits: \"$_\"\n" ; } tr/ \t/ /s ; s/^ // ; @pts = split(/ /) ; $npts2 = @pts ; unless ($npts2/2 == $npts || $npts2 == 12) { print "Line $line expected $npts points got $npts2 pairs\n" ; } $npts -= $npts2/2 ; if ($start) { $x = shift(@pts) ; $y = shift(@pts) ; $lat = sprintf("%.7f",($y - $Y1) * $yscale + $lat1) ; $long = sprintf("%.7f",($x - $X1) * $xscale + $long1) ; print "x $x y $y $lat $long\n" ; $lat0 = $lat ; $long0 = $long ; print OUT "$lat,$long,1,-777,36892.6060778,$date,$time\r\n" ; } for ($i=$start;$i<$npts2/2;$i++) { $x = shift(@pts) ; $y = shift(@pts) ; $lat = sprintf("%.7f",($y - $Y1) * $yscale + $lat1) ; $long = sprintf("%.7f",($x - $X1) * $xscale + $long1) ; print "$name x $x y $y $lat $long\n" ; print OUT "$lat,$long,0,-777,36892.6060778,$date,$time\r\n" ; } $start = 0 ; } if ($t == 3 and ($t2 == 1 or $t2 == 3) ) { print "close x $x y $y $lat $long\n" ; print OUT "$lat0,$long0,0,-777,36892.6060778,$date,$time\r\n" ; } } $name1 = '' ; } elsif(/^[\d] [\d] /) { $name1 = '' ; } } close(OUT) ; } open (OUT,">$file-4B-F.plt") or die ; $name = "$file-bg" ; $name = 'Stave Lake Trails' ; print OUT<$file-4A-F.plt") or die ; print OUT< (or all) elements from Fig EOT }