Skip to content

Commit bedc9db

Browse files
committed
Add vector rendering
1 parent e3a97b1 commit bedc9db

File tree

19 files changed

+830
-304
lines changed

19 files changed

+830
-304
lines changed

.github/workflows/tests.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Tests
2+
3+
on: [push, pull_request]
4+
5+
defaults:
6+
run:
7+
shell: bash -l {0}
8+
9+
jobs:
10+
tests:
11+
name: Unit Tests
12+
runs-on: ${{ matrix.os }}
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
os: [ubuntu-latest, macos-latest, windows-latest]
17+
python-version: [ '3.7' ]
18+
19+
steps:
20+
- name: Checkout
21+
uses: actions/checkout@v2
22+
23+
- name: Setup conda
24+
uses: conda-incubator/setup-miniconda@v2
25+
with:
26+
mamba-version: "*"
27+
channels: conda-forge
28+
29+
- name: Mamba install dependencies
30+
run: mamba install python=${{ matrix.python-version }} pip
31+
32+
- name: Install xarray-leaflet
33+
run: |
34+
pip install .[test]
35+
36+
- name: Check style
37+
run: |
38+
black --check xarray_leaflet tests
39+
flake8 xarray_leaflet tests
40+
# mypy xarray_leaflet
41+
42+
- name: Run tests
43+
run: |
44+
# TODO
45+
# pytest xarray_leaflet/tests -v

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ Using pip:
3232
pip install xarray_leaflet
3333
```
3434

35+
For development:
36+
37+
```bash
38+
pip install -e .
39+
# manually copy these files in your environment, e.g.:
40+
# cp etc/jupyter/jupyter_notebook_config.d/* ~/mambaforge/envs/xarray_leaflet/etc/jupyter/jupyter_notebook_config.d/
41+
# cp etc/jupyter/jupyter_server_config.d/* ~/mambaforge/envs/xarray_leaflet/etc/jupyter/jupyter_server_config.d/
42+
```
43+
3544
## Using xarray-leaflet with Voila
3645

3746
To work with xarray-leaflet, Voila has to be launched with the following command:

setup.cfg

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,48 @@
1-
[bumpversion]
2-
current_version = 0.1.15
3-
commit = True
4-
tag = True
5-
6-
[bumpversion:file:setup.py]
7-
search = version='{current_version}'
8-
replace = version='{new_version}'
9-
10-
[bumpversion:file:xarray_leaflet/__init__.py]
11-
search = __version__ = '{current_version}'
12-
replace = __version__ = '{new_version}'
1+
[metadata]
2+
name = xarray_leaflet
3+
version = attr: xarray_leaflet.__version__
4+
description = An xarray extension for map plotting
5+
long_description = file: README.md
6+
long_description_content_type = text/markdown
7+
license = MIT
8+
author = David Brochart
9+
author_email = david.brochart@gmail.com
10+
url = https://github.com/xarray-contrib/xarray_leaflet
11+
platforms = Windows, Linux, Mac OS X
12+
keywords = xarray leaflet
1313

1414
[bdist_wheel]
1515
universal = 1
1616

17-
[flake8]
18-
exclude = docs
17+
[options]
18+
include_package_data = True
19+
packages = find:
20+
python_requires = >=3.7
1921

20-
[aliases]
21-
# Define setup.py command aliases here
22+
install_requires =
23+
jupyter_server >=0.2.0
24+
rioxarray >=0.0.30
25+
ipyleaflet >=0.13.1
26+
pillow >=7
27+
matplotlib >=3
28+
affine >=2
29+
mercantile >=1
30+
ipyspin >=0.1.1
31+
ipyurl >=0.1.2
32+
geocube
33+
pygeos >=0.12,<1.0.0
34+
zarr >=2.0.0,<3.0.0
2235

36+
[options.extras_require]
37+
test =
38+
mypy
39+
flake8
40+
black
41+
pytest
42+
43+
[options.data_files]
44+
etc/jupyter/jupyter_server_config.d = etc/jupyter/jupyter_server_config.d/xarray_leaflet.json
45+
etc/jupyter/jupyter_notebook_config.d = etc/jupyter/jupyter_notebook_config.d/xarray_leaflet.json
46+
47+
[flake8]
48+
max-line-length = 100

setup.py

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,3 @@
1-
#!/usr/bin/env python
1+
import setuptools
22

3-
"""The setup script."""
4-
5-
from setuptools import setup, find_packages
6-
7-
def get_data_files():
8-
"""Get the data files for the package.
9-
"""
10-
data_files = [
11-
('etc/jupyter/jupyter_server_config.d', ['etc/jupyter/jupyter_server_config.d/xarray_leaflet.json']),
12-
('etc/jupyter/jupyter_notebook_config.d', ['etc/jupyter/jupyter_notebook_config.d/xarray_leaflet.json'])
13-
]
14-
return data_files
15-
16-
requirements = [
17-
'jupyter_server>=0.2.0',
18-
'rioxarray>=0.0.30',
19-
'ipyleaflet>=0.13.1',
20-
'pillow>=7',
21-
'matplotlib>=3',
22-
'affine>=2',
23-
'mercantile>=1',
24-
'ipyspin>=0.1.1',
25-
'ipyurl>=0.1.2',
26-
]
27-
28-
setup_requirements = [ ]
29-
30-
test_requirements = [ ]
31-
32-
setup(
33-
author="David Brochart",
34-
author_email='david.brochart@gmail.com',
35-
python_requires='>=3.5',
36-
classifiers=[
37-
'Development Status :: 2 - Pre-Alpha',
38-
'Intended Audience :: Developers',
39-
'License :: OSI Approved :: MIT License',
40-
'Natural Language :: English',
41-
'Programming Language :: Python :: 3',
42-
'Programming Language :: Python :: 3.5',
43-
'Programming Language :: Python :: 3.6',
44-
'Programming Language :: Python :: 3.7',
45-
'Programming Language :: Python :: 3.8',
46-
],
47-
description="An xarray extension for map plotting",
48-
install_requires=requirements,
49-
license="MIT license",
50-
long_description="An xarray extension for map plotting",
51-
include_package_data=True,
52-
keywords='xarray_leaflet',
53-
name='xarray_leaflet',
54-
packages=find_packages(include=['xarray_leaflet', 'xarray_leaflet.*']),
55-
setup_requires=setup_requirements,
56-
test_suite='tests',
57-
tests_require=test_requirements,
58-
url='https://github.com/davidbrochart/xarray_leaflet',
59-
version='0.1.15',
60-
zip_safe=False,
61-
data_files=get_data_files()
62-
)
3+
setuptools.setup()

tests/__init__.py

Whitespace-only changes.

tests/test_vector.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import filecmp
2+
from tempfile import TemporaryDirectory
3+
4+
import geopandas as gpd
5+
import mercantile
6+
from shapely.geometry import box
7+
from xarray_leaflet.vector import Zvect
8+
9+
from .utils import save_fig
10+
11+
12+
HEIGHT = WIDTH = 256
13+
14+
15+
def test_da_tile_full():
16+
x, y, z = 3, 4, 5
17+
bounds = mercantile.bounds(x, y, z)
18+
b = box(bounds.west, bounds.south, bounds.east, bounds.north)
19+
df = gpd.GeoDataFrame(geometry=gpd.GeoSeries([b]))
20+
df.set_crs(epsg=4326, inplace=True)
21+
measurement = "mask"
22+
df[measurement] = 1
23+
tile = mercantile.bounding_tile(*df.to_crs(epsg=4326).geometry.total_bounds)
24+
assert tile.x == x
25+
assert tile.y == y
26+
assert tile.z == z
27+
zvect = Zvect(df, measurement, WIDTH, HEIGHT)
28+
da_tile = zvect.get_da_tile(tile)
29+
assert len(da_tile.x) == WIDTH
30+
assert len(da_tile.y) == HEIGHT
31+
expected, result = save_fig(da_tile, "test_da_tile_full.png")
32+
assert filecmp.cmp(expected, result)
33+
34+
35+
def test_da_tile_nybb():
36+
path_to_data = gpd.datasets.get_path("nybb")
37+
df = gpd.read_file(path_to_data)
38+
measurement = "mask"
39+
df[measurement] = 1
40+
tile = mercantile.bounding_tile(*df.to_crs(epsg=4326).geometry.total_bounds)
41+
zvect = Zvect(df, measurement, WIDTH, HEIGHT)
42+
da_tile = zvect.get_da_tile(tile)
43+
assert len(da_tile.x) == WIDTH
44+
assert len(da_tile.y) == HEIGHT
45+
expected, result = save_fig(da_tile, "test_da_tile_nybb.png")
46+
assert filecmp.cmp(expected, result)
47+
48+
49+
def test_get_da_llbbox():
50+
path_to_data = gpd.datasets.get_path("nybb")
51+
df = gpd.read_file(path_to_data)
52+
measurement = "mask"
53+
df[measurement] = 1
54+
bounds = df.to_crs(epsg=4326).geometry.total_bounds
55+
tile = mercantile.bounding_tile(*bounds)
56+
llbbox = mercantile.LngLatBbox(*bounds)
57+
with TemporaryDirectory() as tmpdirname:
58+
zvect = Zvect(df, measurement, WIDTH, HEIGHT, tmpdirname)
59+
da = zvect.get_da_llbbox(llbbox, tile.z + 1)
60+
expected, result = save_fig(da, "test_get_da_llbbox.png")
61+
assert filecmp.cmp(expected, result)

tests/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from pathlib import Path
2+
3+
import matplotlib.pyplot as plt
4+
5+
6+
RESULTS = Path(__file__).parent / "results"
7+
RESULTS.mkdir(exist_ok=True)
8+
EXPECTED = Path(__file__).parent / "expected"
9+
EXPECTED.mkdir(exist_ok=True)
10+
11+
12+
def save_fig(da, name):
13+
result_path = RESULTS / name
14+
expected_path = EXPECTED / name
15+
da.plot()
16+
plt.savefig(result_path)
17+
plt.close()
18+
return result_path, expected_path

ui-tests/notebooks/test0.ipynb renamed to ui-tests/notebooks/test_raster.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
"outputs": [],
2121
"source": [
2222
"n = 10\n",
23-
"a = np.arange(n * n, dtype='float32').reshape(n, n)\n",
23+
"a = np.arange(n * n, dtype=\"float32\").reshape(n, n)\n",
2424
"x = np.arange(n)\n",
2525
"y = np.arange(0, -n, -1)\n",
26-
"da = xr.DataArray(a, dims=['y', 'x'], coords=[y, x])\n",
26+
"da = xr.DataArray(a, dims=[\"y\", \"x\"], coords=[y, x])\n",
2727
"da = da.rio.write_crs(4326)\n",
2828
"da = da.rio.write_nodata(np.nan)"
2929
]
@@ -34,7 +34,7 @@
3434
"metadata": {},
3535
"outputs": [],
3636
"source": [
37-
"m = Map(center=(-4.8282597468669755, 3.859859704971314), zoom=5, interpolation='nearest')\n",
37+
"m = Map(center=(-4.8282597468669755, 3.859859704971314), zoom=5, interpolation=\"nearest\")\n",
3838
"m"
3939
]
4040
},
@@ -64,7 +64,7 @@
6464
"name": "python",
6565
"nbconvert_exporter": "python",
6666
"pygments_lexer": "ipython3",
67-
"version": "3.10.1"
67+
"version": "3.10.5"
6868
}
6969
},
7070
"nbformat": 4,

ui-tests/notebooks/test_vector.ipynb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import geopandas\n",
10+
"import matplotlib.pyplot as plt\n",
11+
"from ipyleaflet import Map\n",
12+
"from xarray_leaflet import LeafletMap"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": null,
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"path_to_data = geopandas.datasets.get_path(\"nybb\")\n",
22+
"df = geopandas.read_file(path_to_data)\n",
23+
"df[\"mask\"] = 1"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": null,
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"m = Map(center=(40.68262616765372, -74.020909735734757), zoom=9, interpolation=\"nearest\")\n",
33+
"m"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": null,
39+
"metadata": {},
40+
"outputs": [],
41+
"source": [
42+
"l = LeafletMap(df=df).plot(m, fit_bounds=False, colormap=plt.cm.inferno, measurement=\"mask\")"
43+
]
44+
}
45+
],
46+
"metadata": {
47+
"kernelspec": {
48+
"display_name": "Python 3 (ipykernel)",
49+
"language": "python",
50+
"name": "python3"
51+
},
52+
"language_info": {
53+
"codemirror_mode": {
54+
"name": "ipython",
55+
"version": 3
56+
},
57+
"file_extension": ".py",
58+
"mimetype": "text/x-python",
59+
"name": "python",
60+
"nbconvert_exporter": "python",
61+
"pygments_lexer": "ipython3",
62+
"version": "3.10.5"
63+
}
64+
},
65+
"nbformat": 4,
66+
"nbformat_minor": 4
67+
}

ui-tests/tests/xarray-leaflet.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ async function renderMap(fileName: string, page: IJupyterLabPageFixture) {
1515
}
1616

1717
const notebookList = [
18-
"test0",
18+
"test_raster",
19+
"test_vector",
1920
];
2021

2122
test.describe("xarray-leaflet Visual Regression", () => {
Loading

xarray_leaflet/__init__.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
"""Top-level package for xarray-leaflet."""
1+
__version__ = "0.1.15"
22

3-
__author__ = """David Brochart"""
4-
__email__ = 'david.brochart@gmail.com'
5-
__version__ = '0.1.15'
6-
7-
from .xarray_leaflet import LeafletMap
8-
from .server_extension import _jupyter_server_extension_paths, _load_jupyter_server_extension, _jupyter_nbextension_paths
3+
from .xarray_leaflet import LeafletMap # noqa
4+
from .server_extension import _jupyter_server_extension_paths # noqa
5+
from .server_extension import _load_jupyter_server_extension
6+
from .server_extension import _jupyter_nbextension_paths # noqa
97

108

119
load_jupyter_server_extension = _load_jupyter_server_extension

0 commit comments

Comments
 (0)