Skip to content

Commit

Permalink
Changed to more go like approach
Browse files Browse the repository at this point in the history
Signed-off-by: Arno Broekhof <[email protected]>
  • Loading branch information
arnobroekhof committed Dec 22, 2019
1 parent 3d2f781 commit 33467de
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 49 deletions.
30 changes: 10 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,11 @@ import (
)

func main() {

rd := rd2wgs84.RDCoordinates{
X: 122202,
Y: 487250,
}

wgs84 := rd.ToWGS84()

fmt.Println(wgs84.Lat) //52.37214383811702
fmt.Println(wgs84.Lon) //4.905597604352241
wgs84 := rd2wgs84.NewWSG84Coordinates(52.37214383811702, 4.905597604352241)
rd := wgs84.ToRD()

fmt.Println(rd.X) //122202
fmt.Println(rd.Y) //487250

}

Expand All @@ -47,16 +42,11 @@ import (
)

func main() {
wgs84 := rd2wgs84.WGS84Coordinates{
Lat: 52.37214383811702,
Lon: 4.905597604352241,
}

rd := wgs84.ToRD()

fmt.Println(rd.X) //122202
fmt.Println(rd.Y) //487250

rd := rd2wgs84.NewRDCoordinates(122202, 487250)
wgs84 := rd.ToWGS84()

fmt.Println(wgs84.Lat) //52.37214383811702
fmt.Println(wgs84.Lon) //4.905597604352241

}
```
12 changes: 3 additions & 9 deletions cmd/rd2wgs84.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func main() {
c := cli.NewCLI("rd2wgs84", "0.1")
c := cli.NewCLI("rd2wgs84", "0.2")
c.Args = os.Args[1:]

c.Commands = map[string]cli.CommandFactory{
Expand Down Expand Up @@ -46,10 +46,7 @@ func (r *RD) Run(args []string) int {
return 1
}

wgs := rd2wgs84.WGS84Coordinates{
Lat: cor[0],
Lon: cor[1],
}
wgs := rd2wgs84.NewWSG84Coordinates(cor[0], cor[1])

rd := wgs.ToRD()
fmt.Printf("X: %v Y: %v\n", rd.X, rd.Y)
Expand Down Expand Up @@ -78,10 +75,7 @@ func (r *WGS84) Run(args []string) int {
return 1
}

rd := rd2wgs84.RDCoordinates{
X: cor[0],
Y: cor[1],
}
rd := rd2wgs84.NewRDCoordinates(cor[0], cor[1])

wgs := rd.ToWGS84()
fmt.Printf("lat: %v lon: %v\n", wgs.Lat, wgs.Lon)
Expand Down
18 changes: 16 additions & 2 deletions converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,25 @@ const (
lon0 = 5.387206
)

func NewWSG84Coordinates(lat, lon float64) WGS84Coordinates {
return WGS84Coordinates{
Lat: lat,
Lon: lon,
}
}

type WGS84Coordinates struct {
Lat float64
Lon float64
}

func NewRDCoordinates(x, y float64) RDCoordinates {
return RDCoordinates{
X: x,
Y: y,
}
}

type RDCoordinates struct {
X float64
Y float64
Expand Down Expand Up @@ -70,11 +84,11 @@ func (w WGS84Coordinates) ToRD() RDCoordinates {
lat := 0.36 * (w.Lat - lat0)
lon := 0.36 * (w.Lon - lon0)

for r, _ := range rpq {
for r := range rpq {
x = x + (rpq[r] * math.Pow(lat, rp[r]) * math.Pow(lon, rq[r]))
}

for s, _ := range spq {
for s := range spq {
y = y + (spq[s] * math.Pow(lat, sp[s]) * math.Pow(lon, sq[s]))
}

Expand Down
24 changes: 16 additions & 8 deletions converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import (
)

func TestRDCoords_ToWGS84Coords(t *testing.T) {
rdcoords := RDCoordinates{
X: 122202,
Y: 487250,
}
rdcoords := NewRDCoordinates(122202, 487250)

wgs84Coords := rdcoords.ToWGS84()
assert.NotNil(t, wgs84Coords)
Expand All @@ -21,10 +18,7 @@ func TestRDCoords_ToWGS84Coords(t *testing.T) {
}

func TestWGS84Coords_toRDCoords(t *testing.T) {
wgs84Coords := WGS84Coordinates{
Lat: 52.37214383811702,
Lon: 4.905597604352241,
}
wgs84Coords := NewWSG84Coordinates(52.37214383811702, 4.905597604352241)

rdCoords := wgs84Coords.ToRD()
assert.NotNil(t, rdCoords)
Expand All @@ -37,3 +31,17 @@ func TestWGS84Coords_toRDCoords(t *testing.T) {
t.Logf("Found X: %v", rdCoords.X)
t.Logf("Found Y: %v", rdCoords.Y)
}

func BenchmarkRDCoordinates_ToWGS84(b *testing.B) {
for i := 0; i < b.N; i++ {
rdcoords := NewRDCoordinates(122202, 487250)
rdcoords.ToWGS84()
}
}

func BenchmarkWGS84Coordinates_ToRD(b *testing.B) {
for i := 0; i < b.N; i++ {
wgs84Coords := NewWSG84Coordinates(52.37214383811702, 4.905597604352241)
wgs84Coords.ToRD()
}
}
8 changes: 2 additions & 6 deletions examples/rd2wgs84.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ import (

func convertToWGS84() {

rd := rd2wgs84.RDCoordinates{
X: 122202,
Y: 487250,
}

rd := rd2wgs84.NewRDCoordinates(122202, 487250)
wgs84 := rd.ToWGS84()

fmt.Println(wgs84.Lat) //52.37214383811702
fmt.Println(wgs84.Lon) //4.905597604352241
fmt.Println(wgs84.Lon) //4.905597604352241

}
5 changes: 1 addition & 4 deletions examples/wgs2rd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import (
)

func convertToRD() {
wgs84 := rd2wgs84.WGS84Coordinates{
Lat: 52.37214383811702,
Lon: 4.905597604352241,
}
wgs84 := rd2wgs84.NewWSG84Coordinates(52.37214383811702, 4.905597604352241)

rd := wgs84.ToRD()

Expand Down

0 comments on commit 33467de

Please sign in to comment.