|
9 | 9 |
|
10 | 10 | from titiler.xarray.extensions import DatasetMetadataExtension, VariablesExtension |
11 | 11 | from titiler.xarray.factory import TilerFactory |
| 12 | +from titiler.xarray.io import FsReader, fs_open_dataset |
12 | 13 |
|
13 | 14 | prefix = os.path.join(os.path.dirname(__file__), "fixtures") |
14 | 15 |
|
@@ -61,7 +62,30 @@ def test_tiler_factory(): |
61 | 62 | @pytest.fixture |
62 | 63 | def app(): |
63 | 64 | """App fixture.""" |
64 | | - md = TilerFactory(router_prefix="/md", extensions=[DatasetMetadataExtension()]) |
| 65 | + md = TilerFactory( |
| 66 | + router_prefix="/md", |
| 67 | + extensions=[ |
| 68 | + DatasetMetadataExtension(dataset_opener=fs_open_dataset), |
| 69 | + ], |
| 70 | + reader=FsReader, |
| 71 | + ) |
| 72 | + assert len(md.router.routes) == 22 |
| 73 | + |
| 74 | + app = FastAPI() |
| 75 | + app.include_router(md.router, prefix="/md") |
| 76 | + with TestClient(app) as client: |
| 77 | + yield client |
| 78 | + |
| 79 | + |
| 80 | +@pytest.fixture |
| 81 | +def app_zarr(): |
| 82 | + """App fixture.""" |
| 83 | + md = TilerFactory( |
| 84 | + router_prefix="/md", |
| 85 | + extensions=[ |
| 86 | + DatasetMetadataExtension(), |
| 87 | + ], |
| 88 | + ) |
65 | 89 | assert len(md.router.routes) == 22 |
66 | 90 |
|
67 | 91 | app = FastAPI() |
@@ -393,7 +417,7 @@ def test_zarr_group(group, app): |
393 | 417 | def test_preview(filename): |
394 | 418 | """App fixture.""" |
395 | 419 | with pytest.warns(UserWarning): |
396 | | - md = TilerFactory(add_preview=True) |
| 420 | + md = TilerFactory(add_preview=True, reader=FsReader) |
397 | 421 |
|
398 | 422 | app = FastAPI() |
399 | 423 | app.include_router(md.router) |
@@ -437,3 +461,125 @@ def test_preview(filename): |
437 | 461 | with mem.open() as dst: |
438 | 462 | assert dst.width == 1024 |
439 | 463 | assert dst.height == 1024 |
| 464 | + |
| 465 | + |
| 466 | +@pytest.mark.parametrize( |
| 467 | + "filename", |
| 468 | + [dataset_3d_zarr], |
| 469 | +) |
| 470 | +def test_app_zarr(filename, app_zarr): |
| 471 | + """Test endpoints with Zarr Reader.""" |
| 472 | + resp = app_zarr.get("/md/dataset/keys", params={"url": filename}) |
| 473 | + assert resp.status_code == 200 |
| 474 | + assert resp.headers["content-type"] == "application/json" |
| 475 | + assert resp.json() == ["dataset"] |
| 476 | + |
| 477 | + resp = app_zarr.get("/md/dataset/dict", params={"url": filename}) |
| 478 | + assert resp.status_code == 200 |
| 479 | + assert resp.headers["content-type"] == "application/json" |
| 480 | + assert resp.json()["data_vars"]["dataset"] |
| 481 | + |
| 482 | + resp = app_zarr.get("/md/dataset/", params={"url": filename}) |
| 483 | + assert resp.status_code == 200 |
| 484 | + assert "text/html" in resp.headers["content-type"] |
| 485 | + |
| 486 | + resp = app_zarr.get("/md/bounds", params={"url": filename, "variable": "dataset"}) |
| 487 | + assert resp.status_code == 200 |
| 488 | + assert resp.headers["content-type"] == "application/json" |
| 489 | + |
| 490 | + resp = app_zarr.get("/md/info", params={"url": filename, "variable": "dataset"}) |
| 491 | + assert resp.status_code == 200 |
| 492 | + assert resp.headers["content-type"] == "application/json" |
| 493 | + |
| 494 | + resp = app_zarr.get( |
| 495 | + "/md/tiles/WebMercatorQuad/0/0/0", |
| 496 | + params={"url": filename, "variable": "dataset", "rescale": "0,500", "bidx": 1}, |
| 497 | + ) |
| 498 | + assert resp.status_code == 200 |
| 499 | + assert resp.headers["content-type"] == "image/png" |
| 500 | + |
| 501 | + resp = app_zarr.get( |
| 502 | + "/md/WebMercatorQuad/tilejson.json", |
| 503 | + params={"url": filename, "variable": "dataset", "rescale": "0,500", "bidx": 1}, |
| 504 | + ) |
| 505 | + assert resp.status_code == 200 |
| 506 | + assert resp.headers["content-type"] == "application/json" |
| 507 | + |
| 508 | + resp = app_zarr.get( |
| 509 | + "/md/point/0,0", params={"url": filename, "variable": "dataset"} |
| 510 | + ) |
| 511 | + assert resp.status_code == 200 |
| 512 | + assert resp.headers["content-type"] == "application/json" |
| 513 | + |
| 514 | + feat = { |
| 515 | + "type": "Feature", |
| 516 | + "properties": {}, |
| 517 | + "geometry": { |
| 518 | + "type": "Polygon", |
| 519 | + "coordinates": [ |
| 520 | + [ |
| 521 | + (-100.0, -25.0), |
| 522 | + (40.0, -25.0), |
| 523 | + (40.0, 60.0), |
| 524 | + (-100.0, 60.0), |
| 525 | + (-100.0, -25.0), |
| 526 | + ] |
| 527 | + ], |
| 528 | + }, |
| 529 | + } |
| 530 | + |
| 531 | + resp = app_zarr.post( |
| 532 | + "/md/statistics", params={"url": filename, "variable": "dataset"}, json=feat |
| 533 | + ) |
| 534 | + assert resp.status_code == 200 |
| 535 | + assert resp.headers["content-type"] == "application/geo+json" |
| 536 | + |
| 537 | + feat = { |
| 538 | + "type": "Feature", |
| 539 | + "properties": {}, |
| 540 | + "geometry": { |
| 541 | + "type": "Polygon", |
| 542 | + "coordinates": [ |
| 543 | + [ |
| 544 | + (-100.0, -25.0), |
| 545 | + (40.0, -25.0), |
| 546 | + (40.0, 60.0), |
| 547 | + (-100.0, 60.0), |
| 548 | + (-100.0, -25.0), |
| 549 | + ] |
| 550 | + ], |
| 551 | + }, |
| 552 | + } |
| 553 | + |
| 554 | + resp = app_zarr.post( |
| 555 | + "/md/feature", |
| 556 | + params={"url": filename, "variable": "dataset", "rescale": "0,500", "bidx": 1}, |
| 557 | + json=feat, |
| 558 | + ) |
| 559 | + assert resp.status_code == 200 |
| 560 | + assert resp.headers["content-type"] == "image/jpeg" |
| 561 | + |
| 562 | + |
| 563 | +@pytest.mark.parametrize( |
| 564 | + "group", |
| 565 | + [0, 1, 2], |
| 566 | +) |
| 567 | +def test_group_open_zarr(group, app_zarr): |
| 568 | + """Test /tiles endpoints.""" |
| 569 | + resp = app_zarr.get( |
| 570 | + f"/md/tiles/WebMercatorQuad/{group}/0/0.tif", |
| 571 | + params={"url": zarr_pyramid, "variable": "dataset", "group": str(group)}, |
| 572 | + ) |
| 573 | + assert resp.status_code == 200 |
| 574 | + # see src/titiler/xarray/tests/fixtures/generate_fixtures.ipynb |
| 575 | + # for structure of zarr pyramid |
| 576 | + with MemoryFile(resp.content) as mem: |
| 577 | + with mem.open() as dst: |
| 578 | + arr = dst.read(1) |
| 579 | + assert arr.max() == group * 2 + 1 |
| 580 | + |
| 581 | + resp = app_zarr.get( |
| 582 | + "/md/point/0,0", |
| 583 | + params={"url": zarr_pyramid, "variable": "dataset", "group": str(group)}, |
| 584 | + ) |
| 585 | + assert resp.json()["values"] == [group * 2 + 1] |
0 commit comments