Skip to content

Commit

Permalink
Add vector rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Aug 3, 2022
1 parent e3a97b1 commit ab83f30
Show file tree
Hide file tree
Showing 19 changed files with 836 additions and 304 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Tests

on:
push:
branches:
- main
pull_request:
branches:
- main

defaults:
run:
shell: bash -l {0}

jobs:
tests:
name: Unit Tests
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: [ '3.7' ]

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup conda
uses: conda-incubator/setup-miniconda@v2
with:
mamba-version: "*"
channels: conda-forge

- name: Mamba install dependencies
run: mamba install python=${{ matrix.python-version }} pip gdal

- name: Install xarray-leaflet
run: |
pip install .[test]
- name: Check style
run: |
black --check xarray_leaflet tests
flake8 xarray_leaflet tests
# mypy xarray_leaflet
- name: Run tests
run: |
# TODO
# pytest xarray_leaflet/tests -v
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ Using pip:
pip install xarray_leaflet
```

For development:

```bash
pip install -e .
# manually copy these files in your environment, e.g.:
# cp etc/jupyter/jupyter_notebook_config.d/* ~/mambaforge/envs/xarray_leaflet/etc/jupyter/jupyter_notebook_config.d/
# cp etc/jupyter/jupyter_server_config.d/* ~/mambaforge/envs/xarray_leaflet/etc/jupyter/jupyter_server_config.d/
```

## Using xarray-leaflet with Voila

To work with xarray-leaflet, Voila has to be launched with the following command:
Expand Down
58 changes: 42 additions & 16 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
[bumpversion]
current_version = 0.1.15
commit = True
tag = True

[bumpversion:file:setup.py]
search = version='{current_version}'
replace = version='{new_version}'

[bumpversion:file:xarray_leaflet/__init__.py]
search = __version__ = '{current_version}'
replace = __version__ = '{new_version}'
[metadata]
name = xarray_leaflet
version = attr: xarray_leaflet.__version__
description = An xarray extension for map plotting
long_description = file: README.md
long_description_content_type = text/markdown
license = MIT
author = David Brochart
author_email = [email protected]
url = https://github.com/xarray-contrib/xarray_leaflet
platforms = Windows, Linux, Mac OS X
keywords = xarray leaflet

[bdist_wheel]
universal = 1

[flake8]
exclude = docs
[options]
include_package_data = True
packages = find:
python_requires = >=3.7

[aliases]
# Define setup.py command aliases here
install_requires =
jupyter_server >=0.2.0
rioxarray >=0.0.30
ipyleaflet >=0.13.1
pillow >=7
matplotlib >=3
affine >=2
mercantile >=1
ipyspin >=0.1.1
ipyurl >=0.1.2
geocube
pygeos >=0.12,<1.0.0
zarr >=2.0.0,<3.0.0

[options.extras_require]
test =
mypy
flake8
black
pytest

[options.data_files]
etc/jupyter/jupyter_server_config.d = etc/jupyter/jupyter_server_config.d/xarray_leaflet.json
etc/jupyter/jupyter_notebook_config.d = etc/jupyter/jupyter_notebook_config.d/xarray_leaflet.json

[flake8]
max-line-length = 100
63 changes: 2 additions & 61 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,3 @@
#!/usr/bin/env python
import setuptools

"""The setup script."""

from setuptools import setup, find_packages

def get_data_files():
"""Get the data files for the package.
"""
data_files = [
('etc/jupyter/jupyter_server_config.d', ['etc/jupyter/jupyter_server_config.d/xarray_leaflet.json']),
('etc/jupyter/jupyter_notebook_config.d', ['etc/jupyter/jupyter_notebook_config.d/xarray_leaflet.json'])
]
return data_files

requirements = [
'jupyter_server>=0.2.0',
'rioxarray>=0.0.30',
'ipyleaflet>=0.13.1',
'pillow>=7',
'matplotlib>=3',
'affine>=2',
'mercantile>=1',
'ipyspin>=0.1.1',
'ipyurl>=0.1.2',
]

setup_requirements = [ ]

test_requirements = [ ]

setup(
author="David Brochart",
author_email='[email protected]',
python_requires='>=3.5',
classifiers=[
'Development Status :: 2 - Pre-Alpha',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Natural Language :: English',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
],
description="An xarray extension for map plotting",
install_requires=requirements,
license="MIT license",
long_description="An xarray extension for map plotting",
include_package_data=True,
keywords='xarray_leaflet',
name='xarray_leaflet',
packages=find_packages(include=['xarray_leaflet', 'xarray_leaflet.*']),
setup_requires=setup_requirements,
test_suite='tests',
tests_require=test_requirements,
url='https://github.com/davidbrochart/xarray_leaflet',
version='0.1.15',
zip_safe=False,
data_files=get_data_files()
)
setuptools.setup()
Empty file added tests/__init__.py
Empty file.
61 changes: 61 additions & 0 deletions tests/test_vector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import filecmp
from tempfile import TemporaryDirectory

import geopandas as gpd
import mercantile
from shapely.geometry import box
from xarray_leaflet.vector import Zvect

from .utils import save_fig


HEIGHT = WIDTH = 256


def test_da_tile_full():
x, y, z = 3, 4, 5
bounds = mercantile.bounds(x, y, z)
b = box(bounds.west, bounds.south, bounds.east, bounds.north)
df = gpd.GeoDataFrame(geometry=gpd.GeoSeries([b]))
df.set_crs(epsg=4326, inplace=True)
measurement = "mask"
df[measurement] = 1
tile = mercantile.bounding_tile(*df.to_crs(epsg=4326).geometry.total_bounds)
assert tile.x == x
assert tile.y == y
assert tile.z == z
zvect = Zvect(df, measurement, WIDTH, HEIGHT)
da_tile = zvect.get_da_tile(tile)
assert len(da_tile.x) == WIDTH
assert len(da_tile.y) == HEIGHT
expected, result = save_fig(da_tile, "test_da_tile_full.png")
assert filecmp.cmp(expected, result)


def test_da_tile_nybb():
path_to_data = gpd.datasets.get_path("nybb")
df = gpd.read_file(path_to_data)
measurement = "mask"
df[measurement] = 1
tile = mercantile.bounding_tile(*df.to_crs(epsg=4326).geometry.total_bounds)
zvect = Zvect(df, measurement, WIDTH, HEIGHT)
da_tile = zvect.get_da_tile(tile)
assert len(da_tile.x) == WIDTH
assert len(da_tile.y) == HEIGHT
expected, result = save_fig(da_tile, "test_da_tile_nybb.png")
assert filecmp.cmp(expected, result)


def test_get_da_llbbox():
path_to_data = gpd.datasets.get_path("nybb")
df = gpd.read_file(path_to_data)
measurement = "mask"
df[measurement] = 1
bounds = df.to_crs(epsg=4326).geometry.total_bounds
tile = mercantile.bounding_tile(*bounds)
llbbox = mercantile.LngLatBbox(*bounds)
with TemporaryDirectory() as tmpdirname:
zvect = Zvect(df, measurement, WIDTH, HEIGHT, tmpdirname)
da = zvect.get_da_llbbox(llbbox, tile.z + 1)
expected, result = save_fig(da, "test_get_da_llbbox.png")
assert filecmp.cmp(expected, result)
18 changes: 18 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from pathlib import Path

import matplotlib.pyplot as plt


RESULTS = Path(__file__).parent / "results"
RESULTS.mkdir(exist_ok=True)
EXPECTED = Path(__file__).parent / "expected"
EXPECTED.mkdir(exist_ok=True)


def save_fig(da, name):
result_path = RESULTS / name
expected_path = EXPECTED / name
da.plot()
plt.savefig(result_path)
plt.close()
return result_path, expected_path
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
"outputs": [],
"source": [
"n = 10\n",
"a = np.arange(n * n, dtype='float32').reshape(n, n)\n",
"a = np.arange(n * n, dtype=\"float32\").reshape(n, n)\n",
"x = np.arange(n)\n",
"y = np.arange(0, -n, -1)\n",
"da = xr.DataArray(a, dims=['y', 'x'], coords=[y, x])\n",
"da = xr.DataArray(a, dims=[\"y\", \"x\"], coords=[y, x])\n",
"da = da.rio.write_crs(4326)\n",
"da = da.rio.write_nodata(np.nan)"
]
Expand All @@ -34,7 +34,7 @@
"metadata": {},
"outputs": [],
"source": [
"m = Map(center=(-4.8282597468669755, 3.859859704971314), zoom=5, interpolation='nearest')\n",
"m = Map(center=(-4.8282597468669755, 3.859859704971314), zoom=5, interpolation=\"nearest\")\n",
"m"
]
},
Expand Down Expand Up @@ -64,7 +64,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.1"
"version": "3.10.5"
}
},
"nbformat": 4,
Expand Down
67 changes: 67 additions & 0 deletions ui-tests/notebooks/test_vector.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import geopandas\n",
"import matplotlib.pyplot as plt\n",
"from ipyleaflet import Map\n",
"from xarray_leaflet import LeafletMap"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"path_to_data = geopandas.datasets.get_path(\"nybb\")\n",
"df = geopandas.read_file(path_to_data)\n",
"df[\"mask\"] = 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m = Map(center=(40.68262616765372, -74.020909735734757), zoom=9, interpolation=\"nearest\")\n",
"m"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"l = LeafletMap(df=df).plot(m, fit_bounds=False, colormap=plt.cm.inferno, measurement=\"mask\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
3 changes: 2 additions & 1 deletion ui-tests/tests/xarray-leaflet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ async function renderMap(fileName: string, page: IJupyterLabPageFixture) {
}

const notebookList = [
"test0",
"test_raster",
"test_vector",
];

test.describe("xarray-leaflet Visual Regression", () => {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ab83f30

Please sign in to comment.