| Home | zeGraph lib | Lua lib | Custom lib | Tutorials | Notes | XML Script | C-Talk | Z-Script |
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19

Lesson 14 Binary IO

The zeBIO object is designed for general binary input and output of data. We will gradually add functions to read special binary files. At present there is a functions for reading global coastline from the Global Self-consistent, Hierarchical, High-resolution Shoreline Database (GSHHS) and a function for reading thin and regular grid GRIB data. If your work involves drawing maps, the gshhs() function will be very useful:

require("register")

render, scene, node, line, xyz
    = zeGrf.new("render", "scene", "node", "line", "vertex")

render:add(scene)
scene:set{node = node}
node:add(line)
line:set{vertex = xyz, color = {0, 0, 1, 1}, type = "lines"}

arr, vec, bio = zeUtl.new("double", "double", "bio")

-- Get the coastline data.

bio:open("gshhs_c.b")    -- You must have this file
bio:gshhs("land", 0, 360, -90, 90, arr)

-- Append z-coordinate to arr.

n = arr:size()
vec:resize(n, 1)
vec:fill(0)
arr:insert(2, vec)

xyz:add(arr)    ---- Transfer data to the vertex object.

node:translate(-180, 0, 0)

render:tofile("global_map.png")

global_map.png

Here we used the coarse version of GSHHS data. Be careful in selecting a data file. The high-resolution files are huge and reading their data will take a long time. High-resolution map data are good for a regional plot as shown bellow.

require("register")

render, scene, node, line, xyz
    = zeGrf.new("render", "scene", "node", "line", "vertex")

render:add(scene)
scene:set{node = node}
node:add(line)
line:set{vertex = xyz, color = {0, 0, 1, 1}, type = "lines"}

arr, vec, bio = zeUtl.new("double", "double", "bio")

bio:open("gshhs_h.b")    -- You must have this file
bio:gshhs("land", 90, 130, -15, 15, arr)

-- Scale the array

arr:mul(10)

-- Add the z-component

n = arr:size()
vec:resize(n, 1)
vec:fill(0)
arr:insert(2, vec)

xyz:add(arr)

node:translate(-1100, 0, 0)

render:tofile("regional_map.png")

regional_map.png

The zeArray and zeMath packages have enough functionalities for designing various map projections. Here is an example:

require("register")

render, scene, node, line, xyz
    = zeGrf.new("render", "scene", "node", "line", "vertex")

render:add(scene)
scene:set{node = node}
node:add(line)
line:set{vertex = xyz, color = {0, 0, 1, 1}, type = "lines"}

arr, x, y, p, bio = zeUtl.new("double", "double", "double", "double", "bio")

bio:open("gshhs_c.b")    -- you must have this file
bio:gshhs("land", 0, 360, -90, 90, arr)

-- Get longitude and latitude data

arr:getarr(0, x)
arr:getarr(1, y)

-- Make longitude varies from -180 to 180.

x:add(-180)
y:copy(p)     -- Copy y to p

-- Transform x according to Foucaut projection.
-- proj.maptools.org

zeMath.deg2rad(p)
zeMath.cos(p)
x:mul(p)

-- Scale y to yield a proper aspect ratio of y to x.

y:mul(1.5)

-- Set x-y back to arr.

arr:setarr(0, x);
arr:setarr(1, y);

-- Add the z-component.

arr:insert(2, 0);

xyz:add(arr)	-- Array data to vertex.

render:tofile("foucaut_map.png")

foucaut_map.png