--- cup.perl 2013/11/17 20:18:17 1.2 +++ cup.perl 2025/05/18 05:59:20 1.6 @@ -1,33 +1,115 @@ #! /usr/bin/perl -w -# $Id: cup.perl,v 1.2 2013/11/17 20:18:17 philip Exp $ -use XML::Simple; +# $Id: cup.perl,v 1.6 2025/05/18 05:59:20 philip Exp $ +use JSON; use strict; -my $strips; +my $json = JSON->new; + +open my $fh, '<', 'OmLand.geojson' or die "error opening OmLand.geojson $!\n"; +my $text = do { local $/; <$fh> }; +close $fh; + +my $geojson = $json->decode( $text ); -my $xml = "OmLand.xml"; -if ( defined $xml ){ - $strips = XMLin($xml, KeyAttr => {strip => 'id'}); -} open CUP, "> OmLand.cup" or die "Couldn't open OmLand.cup: $!\n"; -print CUP "name,code,country,lat,lon,elev,style,rwdir,rwlen,freq,desc,userdata,pics\n"; +#name,code,country,lat,lon,elev,style,rwdir,rwlen,freq,desc,userdata,pics #"Ahuriri",0002,NZ,4414.000S,16936.000E,756.0m,1,,,,"Mouth of Canyon Creek",, +# Style is +# 1 Normal waypoint +# 2 Airfield grass +# 3 Outlanding +# 4 Glider site +# 5 Airfield sealed +# 6 Mountain Pass +# 7 Mountain Top +# 8 Sender +# 9 VOR +# 10 NDB +# 11 Cooling tower +# 12 Dam +# 13 Tunnel +# 14 Bridge +# 15 Power plant +# 16 Castle +# 17 Intersection + my %strips; -foreach my $stripnum ( keys %{$strips->{strip}} ){ - print CUP qq["$strips->{strip}->{$stripnum}->{name}",]; - print CUP qq["$stripnum",NZ,]; - $strips->{strip}->{$stripnum}->{lat} =~ s/://; - print CUP $strips->{strip}->{$stripnum}->{lat} . ","; - $strips->{strip}->{$stripnum}->{lng} =~ s/://; - print CUP $strips->{strip}->{$stripnum}->{lng} . ","; - print CUP $strips->{strip}->{$stripnum}->{elev} . "f,3,,,,"; - if ( defined $strips->{strip}->{$stripnum}->{location} ) { - print CUP qq{"$strips->{strip}->{$stripnum}->{location}"}; +foreach my $feature ( @{$geojson->{'features'}} ){ + if ( ! defined $feature->{'properties'}->{'aip'} ) { + print CUP cupstr($feature); } - print CUP ",,\r\n"; } + close CUP; + +exit; + +sub cupstr { + my $feature = shift(@_); + my $location = ""; + my $rwlen = ""; + my $rwdir = ""; + my $freq = ""; + if ( defined $feature->{'properties'}->{'location'} ) { + $location = $feature->{'properties'}->{'location'}; + } + if ( defined $feature->{'properties'}->{'rwlen'} ) { + $rwlen = $feature->{'properties'}->{'rwlen'}; + } + if ( defined $feature->{'properties'}->{'rwdir'} ) { + $rwdir = $feature->{'properties'}->{'rwdir'}; + } + if ( defined $feature->{'properties'}->{'freq'} ) { + $freq = qq{"$feature->{'properties'}->{'freq'}"}; + } + return sprintf( qq{"%s","L%s",NZ,%s,%s,%0.1fm,3,%s,%s,%s,"%s",,\r\n}, + $feature->{'properties'}->{'name'}, + $feature->{'id'}, + printlat($feature->{'geometry'}->{'coordinates'}->[1]), + printlon($feature->{'geometry'}->{'coordinates'}->[0]), + $feature->{'geometry'}->{'coordinates'}->[2], + $rwlen, + $rwdir, + $freq, + $location); +} + +sub printlat { + my $latitude = shift(@_); + my $minutes = 0; + my $degrees = 0; + my $northing = ""; + + if ($latitude < 0) { + $northing = "S"; + $latitude = 0 - $latitude; + } + else{ + $northing = "N"; + } + $degrees = int($latitude); + $minutes = ($latitude - $degrees) * 60; + return sprintf("%d%06.3f%s",$degrees,$minutes,$northing); +} + +sub printlon { + my $longitude = shift(@_); + my $minutes = 0; + my $degrees = 0; + my $westing = ""; + if ($longitude < 0) { + $westing = "W"; + $longitude = 0 - $longitude; + } + else{ + $westing = "E"; + } + $degrees = int($longitude); + $minutes = ($longitude - $degrees) * 60; + return sprintf("%d%06.3f%s",$degrees,$minutes,$westing); +} +