diff --git a/main.go b/main.go index a19bd9d..b2f8bee 100644 --- a/main.go +++ b/main.go @@ -27,7 +27,7 @@ var ( UseDev bool `flag:"osm-dev" default:"false" description:"Switch to dev API"` } MachRange int64 `flag:"match-range" default:"20" description:"Range of meters to match GPX hydrants to OSM nodes"` - Comment string `flag:"comment,c" description:"Comment for the changeset"` + Comment string `flag:"comment,c" default:"Added hydrants from GPX file" description:"Comment for the changeset"` }{} version = "dev" @@ -233,7 +233,7 @@ func main() { if len(changeSets) > 0 { cs = changeSets[0] } else { - cs, err = osmClient.CreateChangeset(cfg.Comment) + cs, err = osmClient.CreateChangeset() if err != nil { log.Fatalf("Unable to create changeset: %s", err) } @@ -243,6 +243,15 @@ func main() { log.Printf("Working on Changeset %d", cs.ID) } + cs.Tags = []osm.Tag{ + {Key: "comment", Value: cfg.Comment}, + {Key: "created_by", Value: fmt.Sprintf("gpxhydrant %s", version)}, + } + + if err := osmClient.SaveChangeset(cs); err != nil { + log.Fatalf("Unable to save changeset: %s", err) + } + border := 0.0009 // Equals ~100m using haversine formula mapData, err := osmClient.RetrieveMapObjects(minLon-border, minLat-border, maxLon+border, maxLat+border) if err != nil { diff --git a/osm/osm.go b/osm/osm.go index 1b72de4..2756856 100644 --- a/osm/osm.go +++ b/osm/osm.go @@ -12,7 +12,7 @@ import ( ) const ( - liveAPIBaseURL = "http://api.openstreetmap.org/api/0.6" + liveAPIBaseURL = "http://api-openstreetmap-org-ue6z91hlm5oj.runscope.net/api/0.6" // "http://api.openstreetmap.org/api/0.6" devAPIBaseURL = "http://api06.dev.openstreetmap.org/api/0.6" ) @@ -129,7 +129,7 @@ func (c *Client) GetMyChangesets(onlyOpen bool) ([]*Changeset, error) { return r.Changesets, c.doParse("GET", urlPath, nil, r) } -func (c *Client) CreateChangeset(comment string) (*Changeset, error) { +func (c *Client) CreateChangeset() (*Changeset, error) { body := bytes.NewBuffer([]byte{}) if err := xml.NewEncoder(body).Encode(Wrap{Changesets: []*Changeset{{}}}); err != nil { return nil, err @@ -152,6 +152,24 @@ func (c *Client) CreateChangeset(comment string) (*Changeset, error) { return cs.Changesets[0], nil } +func (c *Client) SaveChangeset(cs *Changeset) error { + urlPath := "/changeset/create" + + if cs.ID > 0 { + urlPath = fmt.Sprintf("/changeset/%d", cs.ID) + } + + data := Wrap{Changesets: []*Changeset{cs}} + + body := bytes.NewBuffer([]byte{}) + if err := xml.NewEncoder(body).Encode(data); err != nil { + return err + } + + _, err := c.doPlain("PUT", urlPath, body) + return err +} + func (c *Client) RetrieveMapObjects(minLat, minLon, maxLat, maxLon float64) (*Wrap, error) { urlPath := fmt.Sprintf("/map?bbox=%.7f,%.7f,%.7f,%.7f", minLat, minLon, maxLat, maxLon) res := &Wrap{}