Skip to content

Commit 0ef8b6b

Browse files
committed
Add support for --output and --timeout flags
1 parent 10795f3 commit 0ef8b6b

File tree

6 files changed

+88
-24
lines changed

6 files changed

+88
-24
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@ Usage:
1919
wms-tiles-downloader get [flags]
2020
2121
Flags:
22-
-b, --bbox float64Slice comma-separated list of bbox coords (default [])
23-
--concurrency int limit of concurrent requests to the WMS server (default 16)
24-
--format string tile format (default "image/png")
25-
--height int tile height (default 256)
22+
-b, --bbox float64Slice Comma-separated list of bbox coords (default [])
23+
--concurrency int Limit of concurrent requests to the WMS server (default 16)
24+
--format string Tile format (default "image/png")
25+
--height int Tile height (default 256)
2626
-h, --help help for get
27-
-l, --layer string layer name
28-
-s, --style string layer style
27+
-l, --layer string Layer name
28+
-o, --output string Output directory for downloaded tiles
29+
-s, --style string Layer style
30+
-t, --timeout int HTTP request timeout (in milliseconds) (default 10000)
2931
-u, --url string WMS server url
3032
--version string WMS server version (default "1.3.0")
31-
--width int lile width (default 256)
32-
-z, --zoom ints comma-separated list of zooms
33+
--width int Tile width (default 256)
34+
-z, --zoom ints Comma-separated list of zooms
3335
```
3436

3537
### Examples

cmd/get.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ var getCmd = &cobra.Command{
6767
if err != nil {
6868
fmt.Printf("ERR: %s\n", err)
6969
}
70+
output, err := cmd.Flags().GetString("output")
71+
if err != nil {
72+
fmt.Printf("ERR: %s\n", err)
73+
}
74+
timeout, err := cmd.Flags().GetInt("timeout")
75+
if err != nil {
76+
fmt.Printf("ERR: %s\n", err)
77+
}
7078
for _, tileID := range tileIDs {
7179
sem <- true
7280
go func(tileID mercantile.TileID) {
@@ -75,11 +83,13 @@ var getCmd = &cobra.Command{
7583
tile, err := WMSClient.GetTile(
7684
ctx,
7785
tileID,
86+
timeout,
7887
wms.WithLayers(layer),
7988
wms.WithStyles(style),
8089
wms.WithWidth(width),
8190
wms.WithHeight(height),
8291
wms.WithFormat(format),
92+
wms.WithOutputDir(output),
8393
)
8494
if err != nil {
8595
fmt.Printf("ERR: %s\n", err)
@@ -136,6 +146,12 @@ func init() {
136146
getCmd.Flags().String(
137147
"version", "1.3.0", "WMS server version",
138148
)
149+
getCmd.Flags().StringP(
150+
"output", "o", "", "Output directory for downloaded tiles",
151+
)
152+
getCmd.Flags().IntP(
153+
"timeout", "t", 10000, "HTTP request timeout (in milliseconds)",
154+
)
139155
getCmd.Flags().Int(
140156
"concurrency", 16, "Limit of concurrent requests to the WMS server",
141157
)

wms/client.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ func (c *Client) BaseURL() string {
8484
return u.String()
8585
}
8686

87-
func (c *Client) GetTile(ctx context.Context, tileID mercantile.TileID, params ...TileOption) (*Tile, error) {
87+
func (c *Client) GetTile(ctx context.Context, tileID mercantile.TileID, timeout int, params ...TileOption) (*Tile, error) {
8888
tile := NewTile(tileID, params...)
8989

9090
tileURL, err := tile.url(c.BaseURL())
9191
if err != nil {
9292
return nil, err
9393
}
9494

95-
body, err := c.request(ctx, http.MethodGet, tileURL)
95+
body, err := c.request(ctx, http.MethodGet, tileURL, timeout)
9696
if err != nil {
9797
return nil, err
9898
}
@@ -102,13 +102,15 @@ func (c *Client) GetTile(ctx context.Context, tileID mercantile.TileID, params .
102102
}
103103

104104
func (c *Client) SaveTile(tile *Tile) error {
105-
err := os.MkdirAll(tile.Path(), os.ModePerm)
105+
outputPath := path.Join(tile.outputdir, tile.Path())
106+
107+
err := os.MkdirAll(outputPath, os.ModePerm)
106108
if err != nil {
107109
return err
108110
}
109111

110112
err = ioutil.WriteFile(
111-
path.Join(tile.Path(), tile.Name()), tile.Body(), os.ModePerm,
113+
path.Join(outputPath, tile.Name()), tile.Body(), os.ModePerm,
112114
)
113115
if err != nil {
114116
return err
@@ -117,8 +119,8 @@ func (c *Client) SaveTile(tile *Tile) error {
117119
return nil
118120
}
119121

120-
func (c *Client) request(ctx context.Context, method string, url string) ([]byte, error) {
121-
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
122+
func (c *Client) request(ctx context.Context, method string, url string, timeout int) ([]byte, error) {
123+
ctx, cancel := context.WithTimeout(ctx, time.Duration(timeout)*time.Millisecond)
122124
defer cancel()
123125

124126
req, err := http.NewRequestWithContext(ctx, method, url, nil)

wms/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func TestClient_GetTile(t *testing.T) {
126126
transport.RegisterResponder(http.MethodGet, "", test.Resp)
127127

128128
tileID := mercantile.TileID{X: 17, Y: 10, Z: 5}
129-
tile, err := WMSClient.GetTile(context.Background(), tileID)
129+
tile, err := WMSClient.GetTile(context.Background(), tileID, 10000)
130130

131131
assert.Equal(t, test.ExpectedError, err)
132132
if err != nil {

wms/tile.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@ import (
44
"fmt"
55
"github.com/lmikolajczak/wms-tiles-downloader/mercantile"
66
"net/url"
7+
"path"
8+
"path/filepath"
79
"strconv"
810
)
911

1012
type Tile struct {
11-
id mercantile.TileID
12-
name string
13-
path string
14-
body []byte
15-
layers string
16-
styles string
17-
format string
18-
width int
19-
height int
13+
id mercantile.TileID
14+
name string
15+
path string
16+
body []byte
17+
layers string
18+
styles string
19+
format string
20+
width int
21+
height int
22+
outputdir string
2023
}
2124

2225
type TileOption func(t *Tile)
@@ -51,6 +54,15 @@ func WithHeight(height int) TileOption {
5154
}
5255
}
5356

57+
func WithOutputDir(dir string) TileOption {
58+
if !path.IsAbs(dir) {
59+
dir, _ = filepath.Abs(dir)
60+
}
61+
return func(t *Tile) {
62+
t.outputdir = dir
63+
}
64+
}
65+
5466
func NewTile(id mercantile.TileID, options ...TileOption) *Tile {
5567
t := &Tile{
5668
id: id,

wms/tile_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"github.com/lmikolajczak/wms-tiles-downloader/mercantile"
66
"github.com/stretchr/testify/assert"
7+
"os"
8+
"path"
79
"testing"
810
)
911

@@ -47,6 +49,36 @@ func TestWithHeight(t *testing.T) {
4749
assert.Equal(t, expected, tile.height)
4850
}
4951

52+
func TestWithOutputDir(t *testing.T) {
53+
cwd, _ := os.Getwd()
54+
tests := map[string]struct {
55+
OutputDir string
56+
Expected string
57+
}{
58+
"no output path provided - use current working directory": {
59+
OutputDir: "",
60+
Expected: cwd,
61+
},
62+
"relative output path provided": {
63+
OutputDir: "path/wms-tiles-downloader",
64+
Expected: path.Join(cwd, "path/wms-tiles-downloader"),
65+
},
66+
"absolute output path provided": {
67+
OutputDir: "/path/tiles",
68+
Expected: "/path/tiles",
69+
},
70+
}
71+
72+
for name, test := range tests {
73+
t.Run(name, func(t *testing.T) {
74+
tile := &Tile{}
75+
WithOutputDir(test.OutputDir)(tile)
76+
77+
assert.Equal(t, test.Expected, tile.outputdir)
78+
})
79+
}
80+
}
81+
5082
func TestTile_Name(t *testing.T) {
5183
x, y, z := 17, 10, 5
5284
tile := NewTile(mercantile.TileID{X: x, Y: y, Z: z})

0 commit comments

Comments
 (0)