1
0
Fork 0
mirror of https://github.com/Luzifer/gpxhydrant.git synced 2024-12-22 20:21:17 +00:00

Only create a changeset when there are changes to make

This commit is contained in:
Knut Ahlers 2016-05-07 16:29:42 +02:00
parent bdbd2857d2
commit 6acc30b546
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

23
main.go
View file

@ -30,6 +30,8 @@ var (
}{} }{}
version = "dev" version = "dev"
changeset *osm.Changeset
errWrongGPXComment = errors.New("GPX comment does not match expected format") errWrongGPXComment = errors.New("GPX comment does not match expected format")
) )
@ -107,6 +109,10 @@ func hydrantsFromGPXFile() ([]*hydrant, bounds) {
} }
func createChangeset(osmClient *osm.Client) *osm.Changeset { func createChangeset(osmClient *osm.Client) *osm.Changeset {
if changeset != nil {
return changeset
}
cs, err := osmClient.CreateChangeset() cs, err := osmClient.CreateChangeset()
if err != nil { if err != nil {
log.Fatalf("Unable to create changeset: %s", err) log.Fatalf("Unable to create changeset: %s", err)
@ -123,6 +129,8 @@ func createChangeset(osmClient *osm.Client) *osm.Changeset {
log.Fatalf("Unable to save changeset: %s", err) log.Fatalf("Unable to save changeset: %s", err)
} }
changeset = cs
return cs return cs
} }
@ -157,16 +165,13 @@ func main() {
log.Fatalf("Unable to log into OSM: %s", err) log.Fatalf("Unable to log into OSM: %s", err)
} }
// Create a changeset for this import
cs := createChangeset(osmClient)
// Retrieve currently available information from OSM // Retrieve currently available information from OSM
availableHydrants := getHydrantsFromOSM(osmClient, bds) availableHydrants := getHydrantsFromOSM(osmClient, bds)
updateOrCreateHydrants(hydrants, availableHydrants, cs, osmClient) updateOrCreateHydrants(hydrants, availableHydrants, osmClient)
} }
func updateOrCreateHydrants(hydrants, availableHydrants []*hydrant, cs *osm.Changeset, osmClient *osm.Client) { func updateOrCreateHydrants(hydrants, availableHydrants []*hydrant, osmClient *osm.Client) {
for _, h := range hydrants { for _, h := range hydrants {
var found *hydrant var found *hydrant
for _, a := range availableHydrants { for _, a := range availableHydrants {
@ -179,9 +184,9 @@ func updateOrCreateHydrants(hydrants, availableHydrants []*hydrant, cs *osm.Chan
if found == nil { if found == nil {
// No matched hydrant: Lets create one // No matched hydrant: Lets create one
doNoOp( doNoOp(
fmt.Sprintf("[NOOP] Would send a create to OSM (Changeset %d): %#v", cs.ID, h.ToNode()), fmt.Sprintf("[NOOP] Would send a create to OSM (Changeset %d): %#v", createChangeset(osmClient).ID, h.ToNode()),
func() { func() {
if err := osmClient.SaveNode(h.ToNode(), cs); err != nil { if err := osmClient.SaveNode(h.ToNode(), createChangeset(osmClient)); err != nil {
log.Fatalf("Unable to create node using the OSM API: %s", err) log.Fatalf("Unable to create node using the OSM API: %s", err)
} }
logDebugf("Created a hydrant: %s", h.Name) logDebugf("Created a hydrant: %s", h.Name)
@ -204,9 +209,9 @@ func updateOrCreateHydrants(hydrants, availableHydrants []*hydrant, cs *osm.Chan
h.ID = found.ID h.ID = found.ID
h.Version = found.Version h.Version = found.Version
doNoOp( doNoOp(
fmt.Sprintf("[NOOP] Would send a change to OSM (Changeset %d): To=%#v From=%#v", cs.ID, h.ToNode(), found.ToNode()), fmt.Sprintf("[NOOP] Would send a change to OSM (Changeset %d): To=%#v From=%#v", createChangeset(osmClient).ID, h.ToNode(), found.ToNode()),
func() { func() {
if err := osmClient.SaveNode(h.ToNode(), cs); err != nil { if err := osmClient.SaveNode(h.ToNode(), createChangeset(osmClient)); err != nil {
log.Fatalf("Unable to create node using the OSM API: %s", err) log.Fatalf("Unable to create node using the OSM API: %s", err)
} }
logDebugf("Changed a hydrant: %s", h.Name) logDebugf("Changed a hydrant: %s", h.Name)