Root/nanonote-files/example-files/data/Examples/lua-plplot-examples/x19.lua

1--[[ $Id: x19.lua 10293 2009-08-19 07:57:43Z smekal $
2
3    Illustrates backdrop plotting of world, US maps.
4    Contributed by Wesley Ebisuzaki.
5
6  Copyright (C) 2008 Werner Smekal
7
8  This file is part of PLplot.
9
10  PLplot is free software you can redistribute it and/or modify
11  it under the terms of the GNU General Library Public License as published
12  by the Free Software Foundation either version 2 of the License, or
13  (at your option) any later version.
14
15  PLplot is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Library General Public License for more details.
19
20  You should have received a copy of the GNU Library General Public License
21  along with PLplot if not, write to the Free Software
22  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23--]]
24
25-- initialise Lua bindings for PLplot examples.
26dofile("plplot_examples.lua")
27
28--------------------------------------------------------------------------
29-- mapform19
30--
31-- Defines specific coordinate transformation for example 19.
32-- Not to be confused with mapform in src/plmap.c.
33-- x[], y[] are the coordinates to be plotted.
34--------------------------------------------------------------------------
35
36function mapform19(n, x, y)
37  for i = 1, n do
38    radius = 90 - y[i]
39    xp = radius * math.cos(x[i] * math.pi / 180)
40    yp = radius * math.sin(x[i] * math.pi / 180)
41    x[i] = xp
42    y[i] = yp
43  end
44  
45  return x, y
46end
47
48-- "Normalize" longitude values so that they always fall between
49-- -180.0 and 180.0
50function normalize_longitude(lon)
51    if lon>=-180 and lon<=180 then
52        return lon;
53    else
54            times = math.floor((math.abs(lon)+180)/360)
55            if lon<0 then
56                return lon+360*times
57            else
58                return lon-360*times
59            end
60    end
61end
62
63-- A custom axis labeling function for longitudes and latitudes.
64function geolocation_labeler(axis, value)
65    if axis==pl.PL_Y_AXIS then
66        label_val = value
67        if label_val>0 then
68            direction_label = " N"
69        else
70            if label_val<0 then
71                direction_label = " S"
72            else
73                direction_label = "Eq"
74            end
75        end
76    else
77        if axis==pl.PL_X_AXIS then
78            label_val = normalize_longitude(value);
79            if label_val>0 then
80                direction_label = " E"
81            else
82                if label_val<0 then
83                    direction_label = " W"
84                else
85                    direction_label = ""
86                end
87            end
88        end
89    end
90    
91    if axis==pl.PL_Y_AXIS and value==0 then
92        -- A special case for the equator
93        label = direction_label
94    else
95        label = math.abs(label_val) .. direction_label
96    end
97    
98    return label
99end
100
101--------------------------------------------------------------------------
102-- main
103--
104-- Shows two views of the world map.
105--------------------------------------------------------------------------
106
107-- Parse and process command line arguments
108
109pl.parseopts(arg, pl.PL_PARSE_FULL)
110
111-- Longitude (x) and latitude (y)
112
113miny = -70
114maxy = 80
115
116pl.init()
117
118-- Cartesian plots
119-- Most of world
120
121minx = 190
122maxx = 190+360
123
124-- Setup a custom latitude and longitude-based scaling function.
125pl.slabelfunc("geolocation_labeler");
126
127pl.col0(1)
128pl.env(minx, maxx, miny, maxy, 1, 70)
129pl.map(nil, "usaglobe", minx, maxx, miny, maxy)
130
131-- The Americas
132
133minx = 190
134maxx = 340
135
136pl.col0(1)
137pl.env(minx, maxx, miny, maxy, 1, 70)
138pl.map(nil, "usaglobe", minx, maxx, miny, maxy)
139
140-- Clear the labeling function
141pl.slabelfunc(nil);
142
143-- Polar, Northern hemisphere
144
145minx = 0
146maxx = 360
147
148pl.env(-75., 75., -75., 75., 1, -1)
149pl.map("mapform19", "globe", minx, maxx, miny, maxy)
150
151pl.lsty(2)
152pl.meridians("mapform19", 10, 10, 0, 360, -10, 80)
153pl.plend()
154

Archive Download this file



interactive