github.com/omniscale/go-osm@v0.3.1/replication/changeset/example_test.go (about)

     1  package changeset_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"log"
     6  	"os"
     7  	"time"
     8  
     9  	"github.com/omniscale/go-osm/replication/changeset"
    10  )
    11  
    12  func Example() {
    13  	// This example shows how to automatically download OSM changeset files.
    14  
    15  	// We store all changesets in a temporary directory for this example.
    16  	changesetDir, err := ioutil.TempDir("", "")
    17  	if err != nil {
    18  		log.Fatal("unable to create changeset dir:", err)
    19  	}
    20  	defer os.RemoveAll(changesetDir)
    21  
    22  	// Where do we fetch our changesets from?
    23  	url := "https://planet.openstreetmap.org/replication/changesets/"
    24  
    25  	// Query the ID of the latest changeset.
    26  	seqID, err := changeset.CurrentSequence(url)
    27  	if err != nil {
    28  		log.Fatal("unable to fetch current sequence:", err)
    29  	}
    30  
    31  	// Start downloader to fetch changesets. Start with a previous sequence ID, so
    32  	// that we don't have to wait till the files are available.
    33  	dl := changeset.NewDownloader(changesetDir, url, seqID-5, time.Minute)
    34  
    35  	// Iterate all changesets as they are downloaded
    36  	downloaded := 0
    37  	for seq := range dl.Sequences() {
    38  
    39  		if seq.Error != nil {
    40  			// Error is set if an error occurred during download (network issues, etc.).
    41  			// Filename and Time is not set, but you can access the Sequence and Error.
    42  			log.Printf("error while downloading changeset #%d: %s", seq.Sequence, seq.Error)
    43  			// The downloader automatically retries after a short delay, so we
    44  			// can just continue.
    45  			continue
    46  		}
    47  
    48  		downloaded++
    49  
    50  		// seq contains the Filename of the downloaded changeset file. You can use parser/changeset to parse the content.
    51  		log.Printf("downloaded changeset #%d with changes till %s to %s", seq.Sequence, seq.Time, seq.Filename)
    52  
    53  		if downloaded == 3 {
    54  			// Stop downloading after 3 changesets for this example.
    55  			// (Stop() closes the channel from dl.Sequences and for our loop will stop).
    56  			dl.Stop()
    57  		}
    58  	}
    59  	// Output:
    60  }