-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtest_cli.py
233 lines (193 loc) · 8.99 KB
/
test_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""Tests of the morecantile CLI"""
import json
import pytest
from click.testing import CliRunner
from morecantile.scripts.cli import cli
def test_cli_shapes():
"""
Test shapes.
From https://github.com/mapbox/mercantile/blob/master/tests/test_cli.py
"""
runner = CliRunner()
result = runner.invoke(cli, ["shapes", "--precision", "6"], "[106, 193, 9]")
assert result.exit_code == 0
assert (
result.output
== '{"bbox": [-105.46875, 39.909736, -104.765625, 40.446947], "geometry": {"coordinates": [[[-105.46875, 39.909736], [-105.46875, 40.446947], [-104.765625, 40.446947], [-104.765625, 39.909736], [-105.46875, 39.909736]]], "type": "Polygon"}, "id": "(106, 193, 9)", "properties": {"title": "XYZ tile (106, 193, 9)", "tms": "WebMercatorQuad", "tms_crs": "http://www.opengis.net/def/crs/EPSG/0/3857"}, "type": "Feature"}\n'
)
result = runner.invoke(
cli, ["shapes", "--precision", "6", "--geographic"], "[106, 193, 9]"
)
assert result.exit_code == 0
assert (
result.output
== '{"bbox": [-105.46875, 39.909736, -104.765625, 40.446947], "geometry": {"coordinates": [[[-105.46875, 39.909736], [-105.46875, 40.446947], [-104.765625, 40.446947], [-104.765625, 39.909736], [-105.46875, 39.909736]]], "type": "Polygon"}, "id": "(106, 193, 9)", "properties": {"title": "XYZ tile (106, 193, 9)", "tms": "WebMercatorQuad", "tms_crs": "http://www.opengis.net/def/crs/EPSG/0/3857"}, "type": "Feature"}\n'
)
# With TMS's CRS
with pytest.warns(UserWarning):
result = runner.invoke(
cli, ["shapes", "--precision", "6", "--projected"], "[106, 193, 9]"
)
assert result.exit_code == 0
feature = json.loads(result.output)
assert feature["crs"]
# geographic CRS (non WGS84)
with pytest.warns(UserWarning):
result = runner.invoke(
cli, ["shapes", "--precision", "6", "--crs", "epsg:4150"], "[106, 193, 9]"
)
assert result.exit_code == 0
feature = json.loads(result.output)
assert feature["crs"]
# tile as arg
result = runner.invoke(cli, ["shapes", "[106, 193, 9]", "--precision", "6"])
assert result.exit_code == 0
assert (
result.output
== '{"bbox": [-105.46875, 39.909736, -104.765625, 40.446947], "geometry": {"coordinates": [[[-105.46875, 39.909736], [-105.46875, 40.446947], [-104.765625, 40.446947], [-104.765625, 39.909736], [-105.46875, 39.909736]]], "type": "Polygon"}, "id": "(106, 193, 9)", "properties": {"title": "XYZ tile (106, 193, 9)", "tms": "WebMercatorQuad", "tms_crs": "http://www.opengis.net/def/crs/EPSG/0/3857"}, "type": "Feature"}\n'
)
# buffer
result = runner.invoke(
cli, ["shapes", "[106, 193, 9]", "--buffer", "1.0", "--precision", "6"]
)
assert result.exit_code == 0
assert (
result.output
== '{"bbox": [-106.46875, 38.909736, -103.765625, 41.446947], "geometry": {"coordinates": [[[-106.46875, 38.909736], [-106.46875, 41.446947], [-103.765625, 41.446947], [-103.765625, 38.909736], [-106.46875, 38.909736]]], "type": "Polygon"}, "id": "(106, 193, 9)", "properties": {"title": "XYZ tile (106, 193, 9)", "tms": "WebMercatorQuad", "tms_crs": "http://www.opengis.net/def/crs/EPSG/0/3857"}, "type": "Feature"}\n'
)
# Output is compact
result = runner.invoke(cli, ["shapes", "--compact"], "[106, 193, 9]")
assert result.exit_code == 0
assert '"type":"Feature"' in result.output.strip()
# Output is indented
result = runner.invoke(cli, ["shapes", "--indent", "8"], "[106, 193, 9]")
assert result.exit_code == 0
assert ' "type": "Feature"' in result.output.strip()
# Shapes are collected into a feature collection
result = runner.invoke(cli, ["shapes", "--collect", "--feature"], "[106, 193, 9]")
assert result.exit_code == 0
assert "FeatureCollection" in result.output
# geojson is in WebMercator Projection
with pytest.warns(UserWarning):
result = runner.invoke(
cli,
["shapes", "[106, 193, 9]", "--extents", "--projected", "--precision", "3"],
)
assert result.exit_code == 0
assert result.output == "-11740727.545 4852834.052 -11662456.028 4931105.569\n"
with pytest.warns(UserWarning):
# JSON text sequences of bboxes are output.
result = runner.invoke(
cli,
[
"shapes",
"[106, 193, 9]",
"--seq",
"--bbox",
"--projected",
"--precision",
"3",
],
)
assert result.exit_code == 0
assert (
result.output
== "\x1e\n[-11740727.545, 4852834.052, -11662456.028, 4931105.569]\n"
)
# shapes_props_fid
result = runner.invoke(
cli,
[
"shapes",
'{"tile": [106, 193, 9], "properties": {"title": "foo"}, "id": "42"}',
],
)
assert result.exit_code == 0
assert '"title": "foo"' in result.output
assert '"id": "42"' in result.output
def test_cli_shapesWGS84():
"""Test shapes with other projection."""
runner = CliRunner()
result = runner.invoke(
cli,
["shapes", "--precision", "6", "--identifier", "WorldMercatorWGS84Quad"],
"[106, 193, 9]",
)
assert result.exit_code == 0
assert (
result.output
== '{"bbox": [-105.46875, 40.099155, -104.765625, 40.636956], "geometry": {"coordinates": [[[-105.46875, 40.099155], [-105.46875, 40.636956], [-104.765625, 40.636956], [-104.765625, 40.099155], [-105.46875, 40.099155]]], "type": "Polygon"}, "id": "(106, 193, 9)", "properties": {"title": "XYZ tile (106, 193, 9)", "tms": "WorldMercatorWGS84Quad", "tms_crs": "http://www.opengis.net/def/crs/EPSG/0/3395"}, "type": "Feature"}\n'
)
def test_cli_tiles_ok():
"""Test tile with correct bounds."""
runner = CliRunner()
result = runner.invoke(cli, ["tiles", "14"], "[-105, 39.99, -104.99, 40]")
assert result.exit_code == 0
assert result.output == "[3413, 6202, 14]\n[3413, 6203, 14]\n"
def test_cli_tiles_bad_bounds():
"""Bounds of len 3 are bad."""
runner = CliRunner()
result = runner.invoke(cli, ["tiles", "14"], "[-105, 39.99, -104.99]")
assert result.exit_code == 2
def test_cli_tiles_multi_bounds():
"""A LF-delimited sequence can be used as input."""
runner = CliRunner()
result = runner.invoke(
cli, ["tiles", "14"], "[-105, 39.99, -104.99, 40]\n[-105, 39.99, -104.99, 40]"
)
assert result.exit_code == 0
assert len(result.output.strip().split("\n")) == 4
def test_cli_tiles_multi_bounds_seq():
"""A JSON text sequence can be used as input."""
runner = CliRunner()
result = runner.invoke(
cli,
["tiles", "14"],
"\x1e\n[-105, 39.99, -104.99, 40]\n\x1e\n[-105, 39.99, -104.99, 40]",
)
assert result.exit_code == 0
assert len(result.output.strip().split("\n")) == 4
def test_cli_tiles_implicit_stdin():
"""stdin."""
runner = CliRunner()
result = runner.invoke(cli, ["tiles", "14"], "[-105, 39.99, -104.99, 40]")
assert result.exit_code == 0
assert result.output == "[3413, 6202, 14]\n[3413, 6203, 14]\n"
def test_cli_tiles_arg():
"""tiles arg."""
runner = CliRunner()
result = runner.invoke(cli, ["tiles", "14", "[-105, 39.99, -104.99, 40]"])
assert result.exit_code == 0
assert result.output == "[3413, 6202, 14]\n[3413, 6203, 14]\n"
def test_cli_tiles_geosjon():
"""Geojson input."""
collection = '{"features": [{"geometry": {"coordinates": [[[-105.46875, 39.909736], [-105.46875, 40.446947], [-104.765625, 40.446947], [-104.765625, 39.909736], [-105.46875, 39.909736]]], "type": "Polygon"}, "id": "(106, 193, 9)", "properties": {"title": "XYZ tile (106, 193, 9)"}, "type": "Feature"}], "type": "FeatureCollection"}'
runner = CliRunner()
result = runner.invoke(cli, ["tiles", "9"], collection)
assert result.exit_code == 0
assert result.output == "[106, 193, 9]\n[106, 194, 9]\n"
def test_cli_strict_overlap_contain():
"""Input from shapes."""
runner = CliRunner()
result1 = runner.invoke(cli, ["shapes"], "[2331,1185,12]")
assert result1.exit_code == 0
result2 = runner.invoke(cli, ["tiles", "12"], result1.output)
assert result2.exit_code == 0
assert result2.output == "[2331, 1185, 12]\n"
def test_cli_tiles_seq():
"""return a sequence of tiles."""
runner = CliRunner()
result = runner.invoke(cli, ["tiles", "14", "--seq"], "[14.0859, 5.798]")
assert result.exit_code == 0
assert result.output == "\x1e\n[8833, 7927, 14]\n"
def test_cli_tiles_points():
"""Create tile from a point."""
runner = CliRunner()
result = runner.invoke(cli, ["tiles", "14"], "[14.0859, 5.798]")
assert result.exit_code == 0
assert result.output == "[8833, 7927, 14]\n"
result = runner.invoke(
cli, ["tiles", "14"], '{"type":"geometry","coordinates":[14.0859, 5.798]}'
)
assert result.exit_code == 0
assert result.output == "[8833, 7927, 14]\n"