Skip to content

Instantly share code, notes, and snippets.

@pelson
Last active November 2, 2020 17:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pelson/80e8038fad80e1febdfb91f9a262aaa8 to your computer and use it in GitHub Desktop.
Save pelson/80e8038fad80e1febdfb91f9a262aaa8 to your computer and use it in GitHub Desktop.
Example of cartopy source in mapproxy
from cartopy.examples.arrows import sample_data
def arrows(ax):
x, y, u, v, vector_crs = sample_data()
ax.quiver(x, y, u, v, transform=vector_crs)
#!/usr/bin/env bash
set -x
mkdir -p "$1" "$2" "$3"
build=$(cd "$1/" && pwd)
cache=$(cd "$2/" && pwd)
env_dir=$(cd "$3/" && pwd)
# -------
wget --quiet https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
bash miniconda.sh -b -p $HOME/.conda
export PATH=$HOME/.conda/bin:$PATH
# -------
# Build the environment.
source create_env.sh $HOME/env
# Copy the environment to the Heroku build directory.
cp -rf $HOME/env $build/
#!/usr/bin/env bash
# Makes the lightest weight environment possible to run mapproxy with cartopy. If there is no size constraint,
# we would just use conda directly, but as we want to run in Heroku, we must realise an environment <=300 Mb.
export ENV=$1
conda create --yes -c conda-forge -p ${ENV} nomkl python pip pillow pyyaml cartopy
source activate $ENV
pip install git+https://github.com/pelson/mapproxy/@cartopy_source
python -c "import mapproxy; print(mapproxy);"
export PREFIX=$ENV
du -hs $PREFIX
rm -rf $PREFIX/share/doc $PREFIX/share/gtk-doc
rm -rf $PREFIX/lib/libicu* $PREFIX/lib/libxml* $PREFIX/lib/libQt*
rm -rf $PREFIX/bin/sqlite3
export SP_DIR=$PREFIX/lib/python*/site-packages
rm -rf $SP_DIR/scipy/stats
du -hs $PREFIX
import cartopy.crs as ccrs
import matplotlib.textpath
import matplotlib.patches
from matplotlib.font_manager import FontProperties
import numpy as np
def main(ax):
# Comes from the cartopy gallery: http://scitools.org.uk/cartopy/docs/latest/examples/logo.html
# generate a matplotlib path representing the word "cartopy"
fp = FontProperties(family='Bitstream Vera Sans', weight='bold')
logo_path = matplotlib.textpath.TextPath((-175, -35), 'cartopy',
size=80, prop=fp)
# scale the letters up to sensible longitude and latitude sizes
logo_path._vertices *= np.array([1, 2])
im = ax.stock_img()
def on_draw(event=None):
"""
Hooks into matplotlib's event mechanism to define the clip path of the
background image.
"""
plate_carree_transform = ccrs.PlateCarree()._as_mpl_transform(ax)
im.set_clip_path(logo_path, transform=plate_carree_transform)
# Register the on_draw method and call it once now.
ax.figure.canvas.mpl_connect('draw_event', on_draw)
on_draw()
# add the path as a patch, drawing black outlines around the text
patch = matplotlib.patches.PathPatch(logo_path,
antialiased=True,
facecolor='none', edgecolor='black',
transform=ccrs.PlateCarree())
ax.add_patch(patch)
services:
# demo:
# tms:
# use_grid_names: true
# # origin for /tiles service
# origin: 'nw'
# kml:
# use_grid_names: true
# wmts:
wms:
md:
title: Cartopy WMS demo
abstract: Cartopy demonstration WMS.
layers:
- name: wind
title: Wind arrows
sources: [wind]
- name: logo
title: Cartopy logo
sources: [logo]
- name: coastlines
title: Cartopy coastlines
sources: [coastlines]
sources:
wind:
type: cartopy
module: arrows.py
function: arrows
logo:
type: cartopy
module: logo.py
function: main
coastlines:
type: cartopy
module: misc.py
function: coastlines
def coastlines(ax):
ax.coastlines('10m')
web: env/bin/mapproxy-util serve-develop mapproxy.yaml --bind 0.0.0.0:${PORT:=8080}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment