--- /usr/bin/old_gpsfetchmap 2008-11-15 18:45:20.000000000 +0100 +++ /usr/bin/gpsfetchmap 2009-11-26 13:38:44.000000000 +0100 @@ -3,3 +3,3 @@ # gpsfetchmap -# Copyright (C) 2002-2007 Gpsdrive Development Team +# Copyright (C) 2002-2008 Gpsdrive Development Team # @@ -34,3 +34,3 @@ -my $Version = '$Revision: 1743 $'; +my $Version = '$Revision: 2202 $'; $Version =~ s/\$Revision:\s*(\d+)\s*\$/$1/; @@ -48,2 +48,3 @@ modified (Oct 2007) by Andreas Putzo +modified (Jan 2008) by Gernot Hillier (added Openstreetmap support) Version svn-$Version @@ -77,4 +78,5 @@ my @SCALES = (1000,1500,2000,3000,5000,7500,10000,15000,20000,30000,50000,75000, - 100000,150000,200000,300000,500000,750000,1000000,1500000,2000000,3000000, - 5000000,7500000,10000000,15000000,20000000,30000000,50000000,75000000); + 100000,150000,200000,250000,300000,500000,750000,1000000,1500000, + 2000000,2500000,3000000,5000000,7500000,10000000,15000000,20000000, + 30000000,50000000,75000000); @@ -134,12 +136,37 @@ landsat => { - 2000 => 2*6.4, - 5000 => 5*6.4, - 10000 => 10*6.4, - 50000 => 50*6.4, - 100000 => 100*6.4, - 500000 => 500*6.4, - 1000000 => 1000*6.4, - 5000000 => 5000*6.4, - 10000000 =>10000*6.4, - 50000000 =>50000*6.4, + 50000 => 50000, + 75000 => 75000, + 100000 => 100000, + 250000 => 250000, + 500000 => 500000, +# wider scales than 1:500k switch to "top_" Plate Carrée projection. +# It would be more accurate to switch nearer to 1:125k, but map_ is prettier +# so we hold on longer than we should. Distortion grows the further you get +# from lon_0; e.g. UTM is only "valid" in a 6deg wide band. Usage beyond half +# the next band is not recommended, and by the time you get to +/-90deg from +# lon_0 it completely breaks. + 1000000 => 1000000, + 2500000 => 2500000, + 5000000 => 5000000, + 10000000 =>10000000, + 50000000 =>50000000, + }, + openstreetmap_tah => { + 256*576000 => 1, + 128*576000 => 2, + 64*576000 => 3, + 32*576000 => 4, + 16*576000 => 5, + 8*576000 => 6, + 4*576000 => 7, + 2*576000 => 8, + 576000 => 9, + 288000 => 10, + 144000 => 11, + 72000 => 12, + 36000 => 13, + 18000 => 14, + 9000 => 15, + 4500 => 16, + 2250 => 17 } @@ -170,2 +197,3 @@ my $GPSTOOL_MAP_FILE = "$ENV{'HOME'}/.gpsmap/maps.txt"; +my $fileext = 'gif'; my $FILEPREFIX = 'map_'; @@ -183,3 +211,3 @@ -GetOptions ( 'lat=f' => \$lat, 'lon=f' => \$lon, +GetOptions ( 'lat=f' => \$lat, 'lon=f' => \$lon, 'start-lat=f' => \$slat, 'end-lat=f' => \$endlat, @@ -202,3 +230,3 @@ 'PROXY=s' => \$PROXY, - 'debug+' => \$debug, 'MAN' => \$man, + 'debug+' => \$debug, 'MAN' => \$man, 'help|x' => \$help, 'version' => \$version @@ -230,6 +258,9 @@ if ( $mapserver eq 'landsat') { - $FILEPREFIX = 'top_'; - $FILEPREFIX = 'map_'; + #print "Map prefix will be set automatically based on scale\n" + $fileext = 'jpg'; +} elsif ( $mapserver eq 'openstreetmap_tah') { + $fileext = 'png'; } + @SCALES = sort {$a <=> $b} keys %{$Scale2Zoom->{$mapserver}}; @@ -260,8 +291,14 @@ + # Setup up some constants my $DIFF = 0.0000028; -my $RADIUS_KM = 6371.01; -my $LAT_DIST_KM = 110.87; -my $KM2NAUTICAL = 0.54; +my $RADIUS_KM = 6378137.0/1000; # WGS84's "a" +my $LAT_DIST_KM = 1852*60/1000; +my $KM2NAUTICAL = 0.5399568; my $KM2MILES = 0.62137119; +my $PIXELFACT = 2817.947378; # Mapscale/pixelfact is meter/pixel +my $MAPWIDTH = 1280; +my $MAPHEIGHT = 1024; +my $PI = 3.14159265358979323846; +my $D2R = $PI / 180; @@ -299,3 +336,2 @@ - # Verify that we have the options that we need @@ -382,5 +418,13 @@ }elsif ( $mapserver eq 'landsat' ){ + print "+----------------------------------------------------------------+\n"; + print "| Landsat maps are courtesy JPL/NASA's OnEarth WMS Global Mosaic |\n"; + print "| Map prefix will be set automatically based on scale. |\n"; + # By law, US Government data is without copyright. +}elsif ( $mapserver eq 'openstreetmap_tah' ){ print "+-----------------------------------------------------------+\n"; - print "| Landsat Maps are Copyright, ..... |\n"; - print "| They are free for non commercial use. |\n"; + print "| OpenStreetmap Maps are Copyright by the OpenStreetmap |\n"; + print "| project. |\n"; + print "| They are free for use under the terms of the |\n"; + print "| Creative Commons \"Attribution-Share Alike 2.0 Generic\" |\n"; + print "| license. See http://www.openstreetmap.org for details. |\n"; } elsif ( ! $force) { @@ -408,4 +452,6 @@ #print ":". join(" ", @longs ) . "\t"; + if( $debug ) { print "\n"; } for my $long ( @longs ) { print wget_map($scale,$lati,$long); + if( $debug ) { print "\n"; } } @@ -615,3 +661,3 @@ ."/".int($long) - ."/$FILEPREFIX$scale-$lati-$long.gif"; + ."/$FILEPREFIX$scale-$lati-$long.$fileext"; printf("Filename(%.0f,%.5f,%.5f): $filename\n",$scale,$lati,$long) @@ -634,4 +680,15 @@ #"/".int($long)."/$FILEPREFIX$scale-$lati-$long.gif"; + + # redundant?? + if ($mapserver eq 'landsat') + { + if ($scale <= 500000) { + $FILEPREFIX = 'map_'; # cartesian space (UTM-like) + } else { + $FILEPREFIX = 'top_'; # Plate Carrée + } + } + my $filename = map_filename($scale,$lati,$long); - + if ( $mapserver =~ m/^eniro_(se|dk|no|fi)$/) @@ -655,2 +712,6 @@ ($url,$mapscale)=landsat_url($lati,$long,$scale); + } + elsif ( $mapserver eq 'openstreetmap_tah') + { + ($url,$mapscale)=openstreetmap_tah_url($lati,$long,$scale); } @@ -968,12 +1029,56 @@ my $scale = shift; - + my $factor = $Scale2Zoom->{'landsat'}->{$scale}; - my $deltalat = 0.0005; - my $deltalon = 0.001; # Gives ratio 1.2 in meter + my $meters_per_pixel = $scale/$PIXELFACT; - my $lon1 = $lon-$deltalon*$factor/2; - my $lat1 = $lat-$deltalat*$factor/2; - my $lon2 = $lon+$deltalon*$factor/2; - my $lat2 = $lat+$deltalat*$factor/2; + # lat + my $dist_to_edge_m = $meters_per_pixel * $MAPHEIGHT/2; + my $dist_to_edge_deg = $dist_to_edge_m / (1852.0*60); + # 1852m/naut mile (arc-minute of LAT) + my $lat1 = $lat - $dist_to_edge_deg; + my $lat2 = $lat + $dist_to_edge_deg; + + # lon + $dist_to_edge_m = $meters_per_pixel * $MAPWIDTH/2; + if ($scale <= 500000) { +# $FILEPREFIX = 'map_'; # cartesian space (UTM-like) + $dist_to_edge_deg = $dist_to_edge_m / (1852.0*60*cos($lat * $D2R)); + # 1852m/naut mile (arc-minute of LAT), lon:lat ratio = cos(lat) + } else { +# $FILEPREFIX = 'top_'; # Plate Carrée + $dist_to_edge_deg = $dist_to_edge_m / (1852.0*60); + } + + my $lon1 = $lon - $dist_to_edge_deg; + my $lon2 = $lon + $dist_to_edge_deg; + + # sanitize + # bump has problem if > 90N-90S spans >,< 90 lat is valid + # for the server, but not useful for us + if ( $lat1 < -90 || $lat2 > 90 ) { + if ( $lat1 < -90 ) { + my $bump = $lat1 +90; + $lat1 -= $bump; + $lat2 -= $bump; + } + if ( $lat2 > 90 ) { + my $bump = $lat2 -90; + $lat1 -= $bump; + $lat2 -= $bump; + } + # recalc lon extent based on new lat + $lat = ($lat1 + $lat2)/2; + if ( $FILEPREFIX =~ /map/ ) { + $dist_to_edge_deg = $dist_to_edge_m / (1852.0*60*cos($lat * $D2R)); + } + $lon1 = $lon - $dist_to_edge_deg; + $lon2 = $lon + $dist_to_edge_deg; + } + + # valid: -180 to 360 deg + if ( $lon1 < -180 ) { + $lon1 += 360; + $lon2 += 360; + } @@ -983,4 +1088,4 @@ debug( "Calculated Lat2 $lat2"); - debug( "Calculated Lon1 $lon1"); - debug( "Calculated Lon2 $lon2"); + debug( "Calculated Lon1 $lon1"); + debug( "Calculated Lon2 $lon2"); @@ -995,3 +1100,2 @@ $url .= '&srs=EPSG:4326'; -# $url .= '&format=image/gif'; $url .= '&format=image/jpeg'; @@ -1009,2 +1113,33 @@ +############################################################################# +sub openstreetmap_tah_url($$$){ + my $lati = shift; + my $long = shift; + my $scale = shift; + + my $mapscale = $scale; + my $zoom = undef; + for my $s ( sort keys %{$Scale2Zoom->{openstreetmap_tah}} ) { + next unless $s == $scale; + $zoom = $Scale2Zoom->{openstreetmap_tah}->{$s}; + $mapscale = $s; + last; + } + + unless ( $zoom ) { + print "Error calculating Zoomlevel for Scale: $scale\n"; + return (undef,undef); + } + + if ($debug) { + print "\n"; + print "Using openstreetmap_tah zoom ", $zoom, " for requested scale ", $scale, ":1 actual scale ", $mapscale, ":1\n"; + print "lat: $lati\n"; + print "lon: $long\n"; + } + + my $url = "http://tah.openstreetmap.org/MapOf/?lat=$lati&long=$long&z=$zoom&w=1280&h=1024&format=png"; +# print "$url\n"; + return ($url,$mapscale); +} @@ -1139,13 +1274,13 @@ - # make the starting points for the loop $slat and $slon - # snap into a grid with a Size depending on the scale. - # The result is $snapped_start_lat and $snapped_start_lon - # The grid allows maps in each direction to - # overlapp by 1/$overlap of the size of one map - # With snap to grid we would have to download the exact same maps - # for slightly different starting points. This way we can - # circumvent downloads of almost completely overlaping maps + # Make the starting points for the loop $slat and $slon + # snap into a grid with a Size depending on the scale. + # The result is $snapped_start_lat and $snapped_start_lon + # The grid allows maps in each direction to + # overlapp by 1/$overlap of the size of one map + # With snap to grid we would have to download the exact same maps + # for slightly different starting points. This way we can + # circumvent downloads of almost completely overlaping maps my $overlap = 1; my $flat = $delta_lat / $overlap; - my $snapped_start_lat = int ( $slat / $flat ) * $flat; + my $snapped_start_lat = int ( $slat / $flat ) * $flat; my $flon = $delta_lon / $overlap; @@ -1155,3 +1290,3 @@ printf " lati: %6.5f(%6.5f) +=%5.5f ... %6.5f\n", - $snapped_start_lat,$slat,$delta_lat,$elat; + $snapped_start_lat, $slat, $delta_lat, $elat; @@ -1163,4 +1298,4 @@ printf " %5.5f:",$lati; - printf "\tlong: %6.4f(%6.4f) +=%5.4f ... %6.4f" - ,$snapped_start_lon,$slon,$delta_lon,$elon; + printf "\tlong: %6.4f(%6.4f) +=%5.4f ... %6.4f", + $snapped_start_lon, $slon, $delta_lon, $elon; printf "\t\t"; @@ -1170,2 +1305,11 @@ + if ($mapserver eq 'landsat') + { + if ($scale <= 500000) { + $FILEPREFIX = 'map_'; # cartesian space (UTM-like) + } else { + $FILEPREFIX = 'top_'; # Plate Carrée + } + } + if ( $local_debug ) { @@ -1272,5 +1416,5 @@ my $lon_offset = calc_offset($unit,$lon_dist,\$lon_dist_km); - -# print "LAT_OFFSET = $$lat_offset LON_OFFSET = $$lon_offset \n" if ($debug); - + + debug ( "LAT_OFFSET=$lat_offset LON_OFFSET=$lon_offset\n"); + # Ok subtract the offset for the start point @@ -1282,3 +1426,3 @@ my $elon = $lon + $lon_offset; - + return ($slat,$slon,$elat,$elon); @@ -1295,3 +1439,3 @@ } - + # The offset for the coordinate is the distance to travel divided by @@ -1299,3 +1443,3 @@ my $offset = sprintf("%.7f", ($area / 2) / $$dist_per_degree); - + #print "-\n".Dumper($area,\$dist_per_degree,$offset); @@ -1422,8 +1566,5 @@ my $lat = shift; - - my $PI = 3.141592654; - my $dr = $PI / 180; # calculate the circumference of the small circle at latitude - my $cos = cos($lat * $dr); # convert degrees to radians + my $cos = cos($lat * $D2R); # convert degrees to radians my $circ_km = sprintf("%.2f",($PI * 2 * $RADIUS_KM * $cos)); @@ -1752,3 +1893,3 @@ -B Version 1.04 +B $Revision: 2202 $ @@ -1764,5 +1905,5 @@ -gpsfetchmap -la -lo -sc -a <#> -p +gpsfetchmap -la -lo -sc -a <#> -p -gpsfetchmap -sla -endla -slo -endlo -sc -a <#> -p +gpsfetchmap -sla -endla -slo -endlo -sc -a <#> -p @@ -1773,5 +1914,5 @@ gpsfetchmap [-w ] - [-la ] [-lo ] - [-sla ] [-endla ] - [-slo ] [-endlo ] + [-la ] [-lo ] + [-sla ] [-endla ] + [-slo ] [-endlo ] [-sc ] [-a <#>] [-p] [-m ] @@ -1794,28 +1935,39 @@ -=item B<-la, --lat > +=item B<-la, --lat > -Takes a latitude in format DD.MMMM and uses that as the latitude for the centerpoint of the area -to be covered. Will be overriden by the latitude of waypoint if '-w' is used. This and '-lo', '-w' or '-sla', '-ela', '-slo', '-elo' is required. +Takes a latitude in format DD.DDDD and uses that as the latitude for the +centerpoint of the area to be covered. Will be overriden by the latitude +of waypoint if '-w' is used. This and '-lo', '-w' or '-sla', '-ela', '-slo', +'-elo' is required. -=item B<-lo, --lon > +=item B<-lo, --lon > -Takes a longitude in format DD.MMMM and uses that as the longitude for the centerpoint of the area -to be covered. Will be overriden by the longitude of waypoint if '-w' is used. This and '-la', '-w' or '-sla', '-ela', '-slo', '-elo' is required. +Takes a longitude in format DD.DDDD and uses that as the longitude for the +centerpoint of the area to be covered. Will be overriden by the longitude of +waypoint if '-w' is used. This and '-la', '-w' or '-sla', '-ela', '-slo', +'-elo' is required. -=item B<-sla --start-lat > +=item B<-sla --start-lat > -Takes a latitude in format DD.MMMM and uses that as the start latitude for the area to be covered. Will override '-la' and '-lo' but will be overriden by '-w'. This, '-ela', '-slo' and '-elo' or '-w' or '-la' and '-lo' is required. +Takes a latitude in format DD.DDDD and uses that as the start latitude for +the area to be covered. Will override '-la' and '-lo' but will be overriden +by '-w'. This, '-ela', '-slo' and '-elo' or '-w' or '-la' and '-lo' is required. -=item B<-ela --end-lat > +=item B<-ela --end-lat > -Takes a latitude in format DD.MMMM and uses that as the end latitude for the area to be covered. Will override '-la' and '-lo' but will be overriden by '-w'. +Takes a latitude in format DD.DDDD and uses that as the end latitude for the +area to be covered. Will override '-la' and '-lo' but will be overriden by '-w'. This, '-sla', '-slo' and '-elo' or '-w' or '-la' and '-lo' is required. -=item B<-slo --start-lon > +=item B<-slo --start-lon > -Takes a longitude in format DD.MMMM and uses that as the start longitude for the area to be covered. Will override '-la' and '-lo' but will be overriden by '-w'. This, '-sla', '-ela' and '-elo' or '-w' or '-la' and '-lo' is required. +Takes a longitude in format DD.DDDD and uses that as the start longitude for +the area to be covered. Will override '-la' and '-lo' but will be overriden +by '-w'. This, '-sla', '-ela' and '-elo' or '-w' or '-la' and '-lo' is required. -=item B<-elo --end-lon > +=item B<-elo --end-lon > -Takes a longitude in format DD.MMMM and uses that as the end longitude for the area to be covered. Will override '-la' and '-lo' but will be overriden by '-w'. This, '-sla', '-ela' and '-slo' or '-w' or '-la' and '-lo' is required. +Takes a longitude in format DD.DDDD and uses that as the end longitude for the +area to be covered. Will override '-la' and '-lo' but will be overriden by '-w'. +This, '-sla', '-ela' and '-slo' or '-w' or '-la' and '-lo' is required. @@ -1844,6 +1996,7 @@ -Area to cover. # of 'units' size square around the centerpoint. You can use a single number -for square area. Or you can use '#x#' to do a rectangle, where the first number is distance -latitude and the second number is distance of longitude. 'units' is read from the configuration -file (-C) or as defined by (-u). +Area to cover. # of 'units' size square around the centerpoint. You can use +a single number for square area. Or you can use '#x#' to do a rectangle, +where the first number is distance latitude and the second number is distance +of longitude. 'units' is read from the configuration file (-C) or as defined +by (-u). @@ -1851,4 +2004,4 @@ -This causes the program to sleep one second between downloads to be polite to the mapserver. -Takes an optional value of number of seconds to sleep. +This causes the program to sleep one second between downloads to be polite +to the mapserver. Takes an optional value of number of seconds to sleep. @@ -1857,3 +2010,3 @@ Mapserver to download from. Default: 'landsat'. -Currently can use: landsat +Currently usable: landsat, openstreetmap_tah. @@ -1867,2 +2020,5 @@ +openstreetmap_tah: Free maps from the OpenStreetmap Tiles@Home project, see + http://www.openstreetmap.org and http://tah.openstreetmap.org. + gov_au is for Australia @@ -1883,4 +2039,4 @@ -The measurement system to use. Default is read from configuration file <-C>. Possibles are: -miles, nautical, kilometers. +The measurement system to use. Default is read from configuration file <-C>. +Possibles are: miles, nautical, kilometers. @@ -1896,3 +2052,4 @@ -Download maps that are along a saved track. File is a standard track filed saved from GpsDrive. +Download maps that are along a saved track. File is a standard track file +saved from GpsDrive. @@ -1900,3 +2057,4 @@ -Download maps that are along a route defined by waypoints. You must give a list of waypoints as parameters separated with space. +Download maps that are along a route defined by waypoints. You must give a +list of waypoints as parameters separated with space. @@ -1904,3 +2062,4 @@ -File to read for GPSDrive configuration information. Default: '~/.gpsdrive/gpsdriverc'. +File to read for GPSDrive configuration information. +Default: '~/.gpsdrive/gpsdriverc'.