mirror of
https://github.com/Luzifer/gpxhydrant.git
synced 2024-12-23 04:31:18 +00:00
Fix: Followed linter advice
This commit is contained in:
parent
205dd8c4a7
commit
bb2661f03d
1 changed files with 15 additions and 2 deletions
17
osm/osm.go
17
osm/osm.go
|
@ -12,10 +12,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
liveAPIBaseURL = "http://api-openstreetmap-org-ue6z91hlm5oj.runscope.net/api/0.6" // "http://api.openstreetmap.org/api/0.6"
|
liveAPIBaseURL = "http://api.openstreetmap.org/api/0.6"
|
||||||
devAPIBaseURL = "http://api06.dev.openstreetmap.org/api/0.6"
|
devAPIBaseURL = "http://api06.dev.openstreetmap.org/api/0.6"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Client represents an OSM client which is capable of RW operations on the OpenStreetMap
|
||||||
type Client struct {
|
type Client struct {
|
||||||
username string
|
username string
|
||||||
password string
|
password string
|
||||||
|
@ -25,6 +26,7 @@ type Client struct {
|
||||||
CurrentUser *User
|
CurrentUser *User
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New instantiates a new client and retrieves information about the current user. Set useDevServer to true to change the API URL to the api06.dev.openstreetmap.org server.
|
||||||
func New(username, password string, useDevServer bool) (*Client, error) {
|
func New(username, password string, useDevServer bool) (*Client, error) {
|
||||||
out := &Client{
|
out := &Client{
|
||||||
username: username,
|
username: username,
|
||||||
|
@ -74,7 +76,7 @@ func (c *Client) do(method, path string, body io.Reader) (io.ReadCloser, error)
|
||||||
if res.StatusCode != http.StatusOK {
|
if res.StatusCode != http.StatusOK {
|
||||||
d, e := ioutil.ReadAll(res.Body)
|
d, e := ioutil.ReadAll(res.Body)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return nil, fmt.Errorf("OSM API responded with status code %d and reading response failed.", res.StatusCode)
|
return nil, fmt.Errorf("OSM API responded with status code %d and reading response failed", res.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
res.Body.Close()
|
res.Body.Close()
|
||||||
|
@ -98,6 +100,8 @@ func (c *Client) doParse(method, path string, body io.Reader, output interface{}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wrap is a mostly internal used struct which holds requests to / responses from the API.
|
||||||
|
// You will get a Wrap object when querying map objects from the API
|
||||||
type Wrap struct {
|
type Wrap struct {
|
||||||
XMLName xml.Name `xml:"osm"`
|
XMLName xml.Name `xml:"osm"`
|
||||||
User *User `xml:"user,omitempty"`
|
User *User `xml:"user,omitempty"`
|
||||||
|
@ -105,6 +109,7 @@ type Wrap struct {
|
||||||
Nodes []*Node `xml:"node,omitempty"`
|
Nodes []*Node `xml:"node,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Changeset contains information about a changeset in the API. You need to create a changeset before submitting any changes to the API.
|
||||||
type Changeset struct {
|
type Changeset struct {
|
||||||
XMLName xml.Name `xml:"changeset"`
|
XMLName xml.Name `xml:"changeset"`
|
||||||
ID int64 `xml:"id,attr,omitempty"`
|
ID int64 `xml:"id,attr,omitempty"`
|
||||||
|
@ -122,6 +127,7 @@ type Changeset struct {
|
||||||
Tags []Tag `xml:"tag"`
|
Tags []Tag `xml:"tag"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetMyChangesets retrieves a list of (open) changesets from the API
|
||||||
func (c *Client) GetMyChangesets(onlyOpen bool) ([]*Changeset, error) {
|
func (c *Client) GetMyChangesets(onlyOpen bool) ([]*Changeset, error) {
|
||||||
urlPath := fmt.Sprintf("/changesets?user=%d&open=%s", c.CurrentUser.ID, strconv.FormatBool(onlyOpen))
|
urlPath := fmt.Sprintf("/changesets?user=%d&open=%s", c.CurrentUser.ID, strconv.FormatBool(onlyOpen))
|
||||||
|
|
||||||
|
@ -129,6 +135,7 @@ func (c *Client) GetMyChangesets(onlyOpen bool) ([]*Changeset, error) {
|
||||||
return r.Changesets, c.doParse("GET", urlPath, nil, r)
|
return r.Changesets, c.doParse("GET", urlPath, nil, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateChangeset creates a new changeset
|
||||||
func (c *Client) CreateChangeset() (*Changeset, error) {
|
func (c *Client) CreateChangeset() (*Changeset, error) {
|
||||||
body := bytes.NewBuffer([]byte{})
|
body := bytes.NewBuffer([]byte{})
|
||||||
if err := xml.NewEncoder(body).Encode(Wrap{Changesets: []*Changeset{{}}}); err != nil {
|
if err := xml.NewEncoder(body).Encode(Wrap{Changesets: []*Changeset{{}}}); err != nil {
|
||||||
|
@ -152,6 +159,7 @@ func (c *Client) CreateChangeset() (*Changeset, error) {
|
||||||
return cs.Changesets[0], nil
|
return cs.Changesets[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SaveChangeset updates or creates a changeset
|
||||||
func (c *Client) SaveChangeset(cs *Changeset) error {
|
func (c *Client) SaveChangeset(cs *Changeset) error {
|
||||||
urlPath := "/changeset/create"
|
urlPath := "/changeset/create"
|
||||||
|
|
||||||
|
@ -170,12 +178,14 @@ func (c *Client) SaveChangeset(cs *Changeset) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RetrieveMapObjects queries all objects within the passed bounds. You need to ensure the min values are below the max values.
|
||||||
func (c *Client) RetrieveMapObjects(minLat, minLon, maxLat, maxLon float64) (*Wrap, error) {
|
func (c *Client) RetrieveMapObjects(minLat, minLon, maxLat, maxLon float64) (*Wrap, error) {
|
||||||
urlPath := fmt.Sprintf("/map?bbox=%.7f,%.7f,%.7f,%.7f", minLat, minLon, maxLat, maxLon)
|
urlPath := fmt.Sprintf("/map?bbox=%.7f,%.7f,%.7f,%.7f", minLat, minLon, maxLat, maxLon)
|
||||||
res := &Wrap{}
|
res := &Wrap{}
|
||||||
return res, c.doParse("GET", urlPath, nil, res)
|
return res, c.doParse("GET", urlPath, nil, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// User contains information about an User in the OpenStreetMap
|
||||||
type User struct {
|
type User struct {
|
||||||
XMLName xml.Name `xml:"user"`
|
XMLName xml.Name `xml:"user"`
|
||||||
ID int64 `xml:"id,attr"`
|
ID int64 `xml:"id,attr"`
|
||||||
|
@ -185,6 +195,7 @@ type User struct {
|
||||||
Description string `xml:"description"`
|
Description string `xml:"description"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Node represents one node in the OpenStreetMap
|
||||||
type Node struct {
|
type Node struct {
|
||||||
XMLName xml.Name `xml:"node"`
|
XMLName xml.Name `xml:"node"`
|
||||||
ID int64 `xml:"id,attr,omitempty"`
|
ID int64 `xml:"id,attr,omitempty"`
|
||||||
|
@ -198,6 +209,7 @@ type Node struct {
|
||||||
Tags []Tag `xml:"tag"`
|
Tags []Tag `xml:"tag"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SaveNode creates or updates a node with an association to the passed changeset which needs to be open and known to the API.
|
||||||
func (c *Client) SaveNode(n *Node, cs *Changeset) error {
|
func (c *Client) SaveNode(n *Node, cs *Changeset) error {
|
||||||
if n.ID > 0 && n.Version == 0 {
|
if n.ID > 0 && n.Version == 0 {
|
||||||
return fmt.Errorf("When an ID is set the version must be present")
|
return fmt.Errorf("When an ID is set the version must be present")
|
||||||
|
@ -222,6 +234,7 @@ func (c *Client) SaveNode(n *Node, cs *Changeset) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tag represents a key-value pair used in all objects inside OpenStreetMap
|
||||||
type Tag struct {
|
type Tag struct {
|
||||||
XMLName xml.Name `xml:"tag"`
|
XMLName xml.Name `xml:"tag"`
|
||||||
Key string `xml:"k,attr"`
|
Key string `xml:"k,attr"`
|
||||||
|
|
Loading…
Reference in a new issue