| 1 | # | ||
| 2 | package PDL::Demos::Cartography_demo; | ||
| 3 | |||
| 4 | use PDL; | ||
| 5 | use PDL::Graphics::PGPLOT::Window; | ||
| 6 | use PDL::Transform::Cartography; | ||
| 7 | |||
| 8 | use File::Spec; | ||
| 9 | |||
| 10 | PDL::Demos::Routines->import(); | ||
| 11 | sub comment($); | ||
| 12 | sub act($); | ||
| 13 | sub output; | ||
| 14 | |||
| 15 | sub run { | ||
| 16 | local($PDL::debug) = 0; | ||
| 17 | local($PDL::verbose) = 0; | ||
| 18 | |||
| 19 | ##$ENV{PGPLOT_XW_WIDTH}=0.6; | ||
| 20 | $ENV{PGPLOT_DEV} = $^O =~ /MSWin32/ ? '/GW' : | ||
| 21 | defined($ENV{PGPLOT_DEV}) ? $ENV{PGPLOT_DEV} : "/XWIN"; | ||
| 22 | |||
| 23 | |||
| 24 | unless( PDL->rpiccan('JPEG') ) { | ||
| 25 | comment q| | ||
| 26 | This demo illustrates the PDL::Transform::Cartography module. | ||
| 27 | |||
| 28 | It requires PGPLOT and also the ability to read/write JPEG images. | ||
| 29 | |||
| 30 | You don't seem to have that ability at the moment -- this is likely | ||
| 31 | because you do not have NetPBM installed. See the man page for PDL::IO::Pic. | ||
| 32 | |||
| 33 | I'll continue with the demo anyway, but it will likely crash on the | ||
| 34 | earth_image('day') call on the next screen. | ||
| 35 | |||
| 36 | | | ||
| 37 | } | ||
| 38 | |||
| 39 | comment q| | ||
| 40 | |||
| 41 | This demo illustrates the PDL::Transform::Cartography module. | ||
| 42 | It also requires PGPLOT support: you must have PGPLOT installed to run it. | ||
| 43 | |||
| 44 | PDL::Transform::Cartography includes a global earth vector coastline map | ||
| 45 | and night and day world image maps, as well as the infrastructure for | ||
| 46 | transforming them to different coordinate systems. | ||
| 47 | |||
| 48 | If you are using an RGB-enabled version of PGPLOT, then the map | ||
| 49 | images will appear in color on your screen. Otherwise they will | ||
| 50 | appear in greyscale. (The vector data will appear in color | ||
| 51 | regardless). | ||
| 52 | |||
| 53 | |; | ||
| 54 | |||
| 55 | act q| | ||
| 56 | ### Load the necessary modules | ||
| 57 | use PDL::Graphics::PGPLOT::Window; | ||
| 58 | use PDL::Transform::Cartography; | ||
| 59 | |||
| 60 | ### Get the vector coastline map (and a lon/lat grid), and load the Earth | ||
| 61 | ### RGB daytime image -- both of these are built-in to the module. The | ||
| 62 | ### coastline map is a set of (X,Y,Pen) vectors. | ||
| 63 | $coast = earth_coast() -> glue( 1, scalar graticule(15,1) ); | ||
| 64 | print "Coastline data are a collection of vectors: ", | ||
| 65 | join("x",$coast->dims),"\n"; | ||
| 66 | |||
| 67 | $map = earth_image('day'); | ||
| 68 | print "Map data are RGB: ",join("x",$map->dims),"\n\n"; | ||
| 69 | |; | ||
| 70 | |||
| 71 | act q& | ||
| 72 | ### Map data are stored natively in Plate Caree format. | ||
| 73 | ### The image contains a FITS header that contains coordinate system info. | ||
| 74 | print "FITS HEADER INFORMATION:\n"; | ||
| 75 | for $_(keys %{$map->hdr}){ | ||
| 76 | next if(m/SIMPLE/ || m/HISTORY/ || m/COMMENT/); | ||
| 77 | printf (" %8s: %10s%s", $_, $map->hdr->{$_}, (++$i%3) ? " " : "\n"); | ||
| 78 | } | ||
| 79 | print "\n"; | ||
| 80 | |||
| 81 | $dev = $^O =~ /MSWin32/ ? '/GW' : | ||
| 82 | defined($ENV{PGPLOT_DEV}) ? $ENV{PGPLOT_DEV} : "/XW"; | ||
| 83 | # $dev = $^O =~ /MSWin/i ? '/GW' : '/xw'; | ||
| 84 | $w = pgwin(Dev=> $dev, size=>[8,6]); | ||
| 85 | $w->fits_imag($map, {Title=>"NASA/MODIS Earth Map (Plate Caree)",J=>0}); | ||
| 86 | &; | ||
| 87 | |||
| 88 | act q& | ||
| 89 | ### The map data are co-aligned with the vector data, which can be drawn | ||
| 90 | ### on top of the window with the "->lines" PGPLOT method. The | ||
| 91 | ### clean_lines method breaks lines that pass over the map's singularity | ||
| 92 | ### at the 180th parallel. | ||
| 93 | |||
| 94 | $w->hold; | ||
| 95 | $w->lines( $coast -> clean_lines ); | ||
| 96 | $w->release; | ||
| 97 | |||
| 98 | &; | ||
| 99 | |||
| 100 | act q& | ||
| 101 | ### There are a large number of map projections -- to list them all, | ||
| 102 | ### say "??cartography" in the perldl or pdl2 shell. Here are four | ||
| 103 | ### of them: | ||
| 104 | |||
| 105 | $w->close; # Close old window | ||
| 106 | undef $w; | ||
| 107 | |||
| 108 | #$dev = $^O =~ /MSWin/i ? '/GW' : '/xw'; | ||
| 109 | $dev = $^O =~ /MSWin32/ ? '/GW' : | ||
| 110 | defined($ENV{PGPLOT_DEV}) ? $ENV{PGPLOT_DEV} : "/XW"; | ||
| 111 | $w = pgwin( Dev=> $dev, size=>[8,6], nx=>2, ny=>2 ) ; | ||
| 112 | |||
| 113 | sub draw { | ||
| 114 | ($tx, $t, $px, $opt ) = @_; | ||
| 115 | $w->fits_imag( $map->map( $tx, $px, $opt ),{Title=>$t, CharSize=>1.5, J=>1} ); | ||
| 116 | $w->hold; $w->lines( $coast -> apply( $tx ) -> clean_lines ); $w->release; | ||
| 117 | } | ||
| 118 | |||
| 119 | ## (The "or" option specifies the output range of the mapping) | ||
| 120 | draw( t_mercator, "Mercator Projection", [400,300] ); | ||
| 121 | draw( t_aitoff, "Aitoff / Hammer", [400,300] ); | ||
| 122 | draw( t_gnomonic, "Gnomonic", [400,300],{or=>[[-3,3],[-2,2]]} ); | ||
| 123 | draw( t_lambert, "Lambert Conformal Conic",[400,300],{or=>[[-3,3],[-2,2]]} ); | ||
| 124 | |||
| 125 | |||
| 126 | &; | ||
| 127 | |||
| 128 | act q| | ||
| 129 | ### You can create oblique projections by feeding in a different origin. | ||
| 130 | ### Here, the origin is centered over North America. | ||
| 131 | |||
| 132 | draw( t_mercator( o=>[-90,40] ), "Mercator Projection", [400,300] ); | ||
| 133 | draw( t_aitoff ( o=>[-90,40] ), "Aitoff / Hammer", [400,300] ); | ||
| 134 | draw( t_gnomonic( o=>[-90,40] ), "Gnomonic",[400,300],{or=>[[-3,3],[-2,2]]} ); | ||
| 135 | draw( t_lambert( o=>[-90,40] ), "Lambert ",[400,300],{or=>[[-3,3],[-2,2]]} ); | ||
| 136 | |||
| 137 | |; | ||
| 138 | |||
| 139 | act q| | ||
| 140 | ### There are three main perspective projections (in addition to special | ||
| 141 | ### cases like stereographic and gnomonic projection): orthographic, | ||
| 142 | ### vertical, and true perspective. The true perspective has options for | ||
| 143 | ### both downward-looking and aerial-view coordinate systems. | ||
| 144 | |||
| 145 | draw( t_orthographic( o=>[-90,40] ), | ||
| 146 | "Orthographic", [400,300]); | ||
| 147 | |||
| 148 | draw( t_vertical( r0=> (2 + 1), o=>[-90,40] ), | ||
| 149 | "Vertical (Altitude = 2 r\\\\de\\\\u)", [400,300]); | ||
| 150 | |||
| 151 | draw( t_perspective( r0=> (2 + 1), o=>[-90,40] ), | ||
| 152 | "True Perspective (Altitude= 2 r\\\\de\\\\u)", [400,300]); | ||
| 153 | |||
| 154 | # Observer is 0.1 earth-radii above surface, lon 117W, lat 31N (over Tijuana). | ||
| 155 | # view is 45 degrees below horizontal, azimuth -22 (338) degrees. | ||
| 156 | draw( t_perspective( r0=> 1.1, o=>[-117,31], cam=>[-22,-45,0] ), | ||
| 157 | "Aerial view of West Coast of USA", [400,300], | ||
| 158 | {or=>[[-60,60],[-45,45]], method=>'linear'}); | ||
| 159 | |||
| 160 | |; | ||
| 161 | |||
| 162 | comment q| | ||
| 163 | |||
| 164 | That concludes the basic cartography demo. Numerous other transforms | ||
| 165 | are available. | ||
| 166 | |||
| 167 | Because PDL's cartographic transforms work with in the Transform module | ||
| 168 | and are invertible, it's easy to use them both forwards and backwards. | ||
| 169 | In particular, the perspective transformation is useful for ingesting | ||
| 170 | scientific image data of the Earth or other planets, and converting to | ||
| 171 | a map of the imaged body. | ||
| 172 | |||
| 173 | Similarly, scanned images of map data can easily be converted into | ||
| 174 | lat/lon coordinates or reprojected to make other projections. | ||
| 175 | |||
| 176 | |; | ||
| 177 | |||
| 178 | $w->close; | ||
| 179 | undef $w; | ||
| 180 | |||
| 181 | } | ||
| 182 | |||
| 183 | 1; |