-
Notifications
You must be signed in to change notification settings - Fork 0
/
country.go
98 lines (76 loc) · 2.71 KB
/
country.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
Package grump contains function to process data of a GRUMP file describing the country density
into a body file that can be fed into the simulator.
GRUMP is the acronym for "Global Rural-Urban Mapping Project"; this is a freely available data source
of population density.
*/
package grump
import (
"encoding/json"
"fmt"
"log"
"os"
)
// GrumpSpacing is side length in degrees of the input data unit square
const GrumpSpacing float64 = 0.0083333333333
// Country stores country meta data
type Country struct {
Name string
NCols, NRows int
// A country is defined by an encompassing square
// XllCorner is longitude of the lower left corner, in degress
XllCorner float64
// XYlCorner is latitude of the lower left corner, in degress
YllCorner float64
}
// Row2Lat converts from row index to lat
func (country *Country) Row2Lat(row int) (lat float64) {
// lat := float64( country.YllCorner) + (float64( country.NRows - row)*rowLatWidth)
lat = float64(country.YllCorner) + float64(row)*GrumpSpacing
return lat
}
// Serialize into a coord file
func (country *Country) Serialize() {
filename := fmt.Sprintf("conf-%s.coord", country.Name)
file, err := os.Create(filename)
if err != nil {
log.Fatal(err)
return
}
jsonCountry, _ := json.MarshalIndent(country, "", "\t")
file.Write(jsonCountry)
file.Close()
}
// Unserialize inits struct from a coord file
func (country *Country) Unserialize() {
filename := fmt.Sprintf("conf-%s.coord", country.Name)
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
return
}
jsonParser := json.NewDecoder(file)
if err = jsonParser.Decode(country); err != nil {
log.Fatal(fmt.Sprintf("parsing config file %s", err.Error()))
}
Info.Printf("(Grump) Unserialize country %s", country.Name)
Info.Printf("(Grump) Init Country orig lat %f lng %f size lat %f lng %f ",
float64(country.YllCorner),
float64(country.XllCorner),
float64(country.NRows)*GrumpSpacing,
float64(country.NCols)*GrumpSpacing)
file.Close()
}
// LatLng2XY gives from lat/lng, the relative coordinate within the country
func (country *Country) LatLng2XY(lat, lng float64) (x, y float64) {
// compute relative coordinates within the square
x = (lng - float64(country.XllCorner)) / (float64(country.NCols) * GrumpSpacing)
y = (lat - float64(country.YllCorner)) / (float64(country.NRows) * GrumpSpacing) // y is 0 at northest point and 1.0 at southest point
return x, y
}
// XY2LatLng gives from lat/lng, the relative coordinate within the country
func (country *Country) XY2LatLng(x, y float64) (lat, lng float64) {
lat = float64(country.YllCorner) + (y * float64(country.NRows) * GrumpSpacing)
lng = float64(country.XllCorner) + (x * float64(country.NCols) * GrumpSpacing)
return lat, lng
}