-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add geo.NewCoordinate and geo.NewBoundingBox; update geo.IsValid methods
- Loading branch information
thisisaaronland
committed
Dec 7, 2020
1 parent
de18044
commit 7e81878
Showing
2 changed files
with
94 additions
and
9 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package geo | ||
|
||
import ( | ||
"errors" | ||
"github.com/skelterjohn/geom" | ||
) | ||
|
||
func IsValidMinLatitude(lat float64) bool { | ||
return lat >= -90.00 | ||
} | ||
|
||
func IsValidMaxLatitude(lat float64) bool { | ||
return lat <= 90.00 | ||
} | ||
|
||
func IsValidLatitude(lat float64) bool { | ||
return IsValidMinLatitude(lat) && IsValidMaxLatitude(lat) | ||
} | ||
|
||
func IsValidMinLongitude(lon float64) bool { | ||
return lon >= -180.00 | ||
} | ||
|
||
func IsValidMaxLongitude(lon float64) bool { | ||
return lon <= 180.00 | ||
} | ||
|
||
func IsValidLongitude(lon float64) bool { | ||
return IsValidMinLongitude(lon) && IsValidMaxLongitude(lon) | ||
} | ||
|
||
func NewCoordinate(x float64, y float64) (*geom.Coord, error) { | ||
|
||
if !IsValidLatitude(y) { | ||
return nil, errors.New("Invalid latitude") | ||
} | ||
|
||
if !IsValidLongitude(y) { | ||
return nil, errors.New("Invalid longitude") | ||
} | ||
|
||
coord := &geom.Coord{ | ||
X: x, | ||
Y: y, | ||
} | ||
|
||
return coord, nil | ||
} | ||
|
||
func NewBoundingBox(minx float64, miny float64, maxx float64, maxy float64) (*geom.Rect, error) { | ||
|
||
if !IsValidLongitude(minx) { | ||
return nil, errors.New("Invalid min longitude") | ||
} | ||
|
||
if !IsValidLatitude(miny) { | ||
return nil, errors.New("Invalid min latitude") | ||
} | ||
|
||
if !IsValidLongitude(maxx) { | ||
return nil, errors.New("Invalid max longitude") | ||
} | ||
|
||
if !IsValidLatitude(maxy) { | ||
return nil, errors.New("Invalid max latitude") | ||
} | ||
|
||
if minx > maxx { | ||
return nil, errors.New("Min lon is greater than max lon") | ||
} | ||
|
||
if minx > maxx { | ||
return nil, errors.New("Min latitude is greater than max latitude") | ||
} | ||
|
||
min_coord, err := NewCoordinate(minx, miny) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
max_coord, err := NewCoordinate(maxx, maxy) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rect := &geom.Rect{ | ||
Min: *min_coord, | ||
Max: *max_coord, | ||
} | ||
|
||
return rect, nil | ||
} |