SciTools / cartopy

Cartopy - a cartographic python library with matplotlib support
https://scitools.org.uk/cartopy/docs/latest
BSD 3-Clause "New" or "Revised" License
1.44k stars 368 forks source link

deepcopy does not work for Projections #1336

Closed ochsnerd closed 4 years ago

ochsnerd commented 5 years ago

Description

copy.deepcopy doesn't work for classes defined in cartopy.crs. It returns a copy with all parameters set to their default values. As a result these projections are not usable with pickle or multiprocessing.

Code to reproduce

import cartopy.crs as ccrs

from copy import deepcopy

def test_proj(Projection, args=1):
    orig = Projection(args)
    copy = deepcopy(a)
    # copy should be the same as orig
    return {
        (key, (orig.proj4_params[key], copy.proj4_params[key]))
        for key in orig.proj4_params
        if orig.proj4_params[key] != copy.proj4_params[key]
    }

print(test_proj(ccrs.RotatedPole))
print(test_proj(ccrs.PlateCarree))

Traceback

There is no traceback, code just silently does the wrong projections.

Full environment definition Tested on two linux machines: ### pip list dask (0.17.5) easybuild-easyblocks (3.9.1) easybuild-easyconfigs (3.9.1) easybuild-framework (3.9.1) funcsigs (1.0.2) kiwisolver (1.0.1) Mako (1.0.8) MarkupSafe (1.1.1) matplotlib (2.2.2) more-itertools (4.1.0) mpi4py (2.0.0) netCDF4 (1.4.1) nose (1.3.7) numpy (1.14.3) Second machine, here I didn't manage to get a virtual environment with an up-to-date proj4 version running, so it's the whole messy shared environment: ### conda list # Name Version Build Channel _ipyw_jlab_nb_ext_conf 0.1.0 py37_0 affine 2.2.2 py_0 conda-forge alabaster 0.7.12 py_0 conda-forge amrs 2.0 pypi_0 pypi anaconda custom py37_0 anaconda-client 1.7.1 py_0 conda-forge anaconda-navigator 1.9.6 py37_0 anaconda-project 0.8.2 py_1 conda-forge asn1crypto 0.24.0 py37_1003 conda-forge astroid 2.1.0 py37_1000 conda-forge astropy 3.1.1 py37h14c3975_1000 conda-forge atk 2.25.90 hf2eb9ee_1001 conda-forge atomicwrites 1.2.1 py_0 conda-forge attrs 18.2.0 py_0 conda-forge babel 2.6.0 py_1 conda-forge backcall 0.1.0 py_0 conda-forge backports 1.0 py_2 conda-forge backports.os 0.1.1 py37_1000 conda-forge backports.shutil_get_terminal_size 1.0.0 py_3 conda-forge beautifulsoup4 4.7.1 py37_1001 conda-forge bitarray 0.8.3 py37h14c3975_1000 conda-forge bkcharts 0.2 py37_0 blas 1.0 mkl conda-forge blaze 0.11.3 py37_0 blinker 1.4 py_1 conda-forge blosc 1.15.1 hf484d3e_1002 conda-forge bokeh 1.0.4 py37_1000 conda-forge boost-cpp 1.68.0 h11c811c_1000 conda-forge boto 2.49.0 py_0 conda-forge boto3 1.9.86 py_0 conda-forge botocore 1.12.86 py_0 conda-forge bottleneck 1.2.1 py37h3010b51_1001 conda-forge bz2file 0.98 py_0 conda-forge bzip2 1.0.6 h14c3975_1002 conda-forge ca-certificates 2018.11.29 ha4d7672_0 conda-forge cairo 1.14.12 h80bd089_1005 conda-forge cartopy 0.17.0 py37h0aa2c8f_1004 conda-forge certifi 2018.11.29 py37_1000 conda-forge cffi 1.11.5 py37h9745a5d_1001 conda-forge cftime 1.0.3.4 py37h3010b51_1000 conda-forge chardet 3.0.4 py37_1003 conda-forge click 7.0 py_0 conda-forge click-plugins 1.0.4 py_0 conda-forge cligj 0.5.0 py_0 conda-forge cloudpickle 0.6.1 py_0 conda-forge clyent 1.2.2 py_1 conda-forge colorama 0.4.1 py_0 conda-forge conda-verify 3.1.1 py37_1000 conda-forge contextlib2 0.5.5 py_2 conda-forge cryptography 2.3.1 py37hb7f436b_1000 conda-forge curl 7.63.0 h646f8bb_1000 conda-forge cycler 0.10.0 py_1 conda-forge cython 0.29.3 py37hf484d3e_0 conda-forge cytoolz 0.9.0.1 py37h14c3975_1001 conda-forge dask 1.1.0 py_1 conda-forge dask-core 1.1.0 py_0 conda-forge datashape 0.5.4 py37_1 dbus 1.13.0 h4e0c4b3_1000 conda-forge decorator 4.3.2 py_0 conda-forge defusedxml 0.5.0 py_1 conda-forge distributed 1.25.2 py37_1000 conda-forge docutils 0.14 py37_1001 conda-forge entrypoints 0.3 py37_1000 conda-forge et_xmlfile 1.0.1 py37_0 expat 2.2.5 hf484d3e_1002 conda-forge f90nml 1.0.2 py37_1000 conda-forge fastcache 1.0.2 py37h14c3975_1001 conda-forge filelock 3.0.10 py_0 conda-forge flask 1.0.2 py_2 conda-forge flask-cors 3.0.7 py_0 conda-forge fontconfig 2.13.1 h2176d3f_1000 conda-forge freetype 2.9.1 h94bbf69_1005 conda-forge freexl 1.0.5 h14c3975_1002 conda-forge fribidi 1.0.5 h14c3975_1000 conda-forge future 0.17.1 py37_1000 conda-forge gdk-pixbuf 2.36.12 h4f1c04b_1001 conda-forge gensim 3.5.0 py37h04863e7_1000 conda-forge geos 3.7.1 hf484d3e_1000 conda-forge geotiff 1.4.3 h1105359_1000 conda-forge get_terminal_size 1.0.0 haa9412d_0 gettext 0.19.8.1 h9745a5d_1001 conda-forge gevent 1.4.0 py37h14c3975_0 conda-forge giflib 5.1.4 h14c3975_1001 conda-forge glib 2.56.2 had28632_1001 conda-forge glob2 0.6 py_0 conda-forge gmp 6.1.2 hf484d3e_1000 conda-forge gmpy2 2.0.8 py37hb20f59a_1002 conda-forge gobject-introspection 1.56.1 py37h9e29830_1001 conda-forge graphite2 1.3.13 hf484d3e_1000 conda-forge greenlet 0.4.15 py37h7b6447c_0 gst-plugins-base 1.12.5 h3865690_1000 conda-forge gstreamer 1.12.5 h0cc0488_1000 conda-forge gtk2 2.24.31 h5baeb44_1000 conda-forge h5py 2.9.0 nompi_py37hf008753_1102 conda-forge harfbuzz 1.9.0 he243708_1001 conda-forge hdf4 4.2.13 h9a582f1_1002 conda-forge hdf5 1.10.4 nompi_h11e915b_1105 conda-forge heapdict 1.0.0 py37_1000 conda-forge html5lib 1.0.1 py_0 conda-forge humanize 0.5.1 py_1 conda-forge icu 58.2 hf484d3e_1000 conda-forge idna 2.8 py37_1000 conda-forge imageio 2.4.1 py37_1000 conda-forge imagesize 1.1.0 py_0 conda-forge importlib_metadata 0.6 py37_0 conda-forge intel-openmp 2019.1 144 ipykernel 5.1.0 py37h24bf2e0_1001 conda-forge ipython 7.2.0 py37h24bf2e0_1000 conda-forge ipython_genutils 0.2.0 py_1 conda-forge ipywidgets 7.4.2 py_0 conda-forge isort 4.3.4 py37_1000 conda-forge itsdangerous 1.1.0 py_0 conda-forge jbig 2.1 h14c3975_2001 conda-forge jdcal 1.4 py_1 conda-forge jedi 0.13.2 py37_1000 conda-forge jeepney 0.4 py_0 conda-forge jinja2 2.10 py_1 conda-forge jmespath 0.9.3 py_1 conda-forge jpeg 9c h14c3975_1001 conda-forge json-c 0.13.1 h14c3975_1001 conda-forge jsonschema 3.0.0a3 py37_1000 conda-forge jupyter 1.0.0 py_1 conda-forge jupyter_client 5.2.4 py_1 conda-forge jupyter_console 6.0.0 py_0 conda-forge jupyter_core 4.4.0 py_0 conda-forge jupyterlab 0.35.4 py37_0 conda-forge jupyterlab_server 0.2.0 py_0 conda-forge kealib 1.4.10 he7154bc_1002 conda-forge keyring 17.1.1 py37_1000 conda-forge kiwisolver 1.0.1 py37h6bb024c_1002 conda-forge krb5 1.16.3 hc83ff2d_1000 conda-forge lazy-object-proxy 1.3.1 py37h14c3975_1000 conda-forge libarchive 3.3.3 ha149a29_1000 conda-forge libcurl 7.63.0 h01ee5af_1000 conda-forge libdap4 3.19.1 hd48c02d_1000 conda-forge libedit 3.1.20170329 hf8c457e_1001 conda-forge libffi 3.2.1 hf484d3e_1005 conda-forge libgcc-ng 7.3.0 hdf63c60_0 conda-forge libgdal 2.4.0 h982c1cc_1002 conda-forge libgfortran-ng 7.2.0 hdf63c60_3 conda-forge libiconv 1.15 h14c3975_1004 conda-forge libkml 1.3.0 h328b03d_1009 conda-forge liblief 0.9.0 h7725739_2 libnetcdf 4.6.2 hbdf4f91_1001 conda-forge libpng 1.6.36 h84994c4_1000 conda-forge libpq 10.6 h13b8bad_1000 conda-forge libsodium 1.0.16 h14c3975_1001 conda-forge libspatialite 4.3.0a hb5ec416_1026 conda-forge libssh2 1.8.0 h1ad7b7a_1003 conda-forge libstdcxx-ng 7.3.0 hdf63c60_0 conda-forge libtiff 4.0.10 h648cc4a_1001 conda-forge libtool 2.4.6 h14c3975_1002 conda-forge libuuid 2.32.1 h14c3975_1000 conda-forge libxcb 1.13 h14c3975_1002 conda-forge libxml2 2.9.8 h143f9aa_1005 conda-forge libxslt 1.1.32 h4785a14_1002 conda-forge llvmlite 0.26.0 py37hdbcaa40_1000 conda-forge locket 0.2.0 py_2 conda-forge lxml 4.3.0 py37h23eabaa_1000 conda-forge lz4 2.1.6 py37h7b6447c_0 lz4-c 1.8.3 hf484d3e_1001 conda-forge lzo 2.10 h14c3975_1000 conda-forge markupsafe 1.1.0 py37h14c3975_1000 conda-forge matplotlib 3.0.2 py37h8a2030e_1001 conda-forge matplotlib-base 3.0.2 py37h167e16e_1001 conda-forge mccabe 0.6.1 py_1 conda-forge mistune 0.8.4 py37h14c3975_1000 conda-forge mkl 2019.1 144 mkl-service 1.1.2 py37he904b0f_5 mkl_fft 1.0.10 py37h14c3975_1 conda-forge mkl_random 1.0.2 py37h637b7d7_2 conda-forge more-itertools 4.3.0 py37_1000 conda-forge mpc 1.1.0 hb20f59a_1006 conda-forge mpfr 4.0.1 ha14ba45_1000 conda-forge mpmath 1.1.0 py_0 conda-forge msgpack-python 0.6.1 py37h6bb024c_0 conda-forge multipledispatch 0.6.0 py_0 conda-forge navigator-updater 0.2.1 py37_0 nbconvert 5.3.1 py_1 conda-forge nbformat 4.4.0 py_1 conda-forge ncurses 6.1 hf484d3e_1002 conda-forge netcdf4 1.4.2 py37had69b76_1001 conda-forge networkx 2.2 py_1 conda-forge nltk 3.2.5 py_0 conda-forge nose 1.3.7 py37_1002 conda-forge notebook 5.7.4 py37_1000 conda-forge numba 0.41.0 py37h637b7d7_1000 conda-forge numexpr 2.6.9 py37h637b7d7_1000 conda-forge numpy 1.15.4 py37h7e9f1db_0 numpy-base 1.15.4 py37hde5b4d6_0 numpydoc 0.8.0 py_1 conda-forge oauthlib 2.1.0 py_0 conda-forge odo 0.5.1 py_1 conda-forge olefile 0.46 py_0 conda-forge openjpeg 2.3.0 hf38bd82_1003 conda-forge openpyxl 2.5.12 py_0 conda-forge openssl 1.0.2p h14c3975_1002 conda-forge owslib 0.17.1 py_0 conda-forge packaging 19.0 py_0 conda-forge pandas 0.24.0 py37hf484d3e_0 conda-forge pandoc 2.5 1 conda-forge pandocfilters 1.4.2 py_1 conda-forge pango 1.40.14 hf0c64fd_1003 conda-forge parso 0.3.2 py_0 conda-forge partd 0.3.9 py_0 conda-forge patchelf 0.9 hf484d3e_1002 conda-forge path.py 11.5.0 py_0 conda-forge pathlib2 2.3.3 py37_1000 conda-forge patsy 0.5.1 py_0 conda-forge pcre 8.41 hf484d3e_1003 conda-forge pep8 1.7.1 py_0 conda-forge pexpect 4.6.0 py37_1000 conda-forge pickleshare 0.7.5 py37_1000 conda-forge pillow 5.4.1 py37h00a061d_1000 conda-forge pip 19.0.1 py37_0 conda-forge pixman 0.34.0 h14c3975_1003 conda-forge pkginfo 1.5.0.1 py_0 conda-forge pluggy 0.8.1 py_0 conda-forge ply 3.11 py_1 conda-forge poppler 0.67.0 h2fc8fa2_1002 conda-forge poppler-data 0.4.9 1 conda-forge postgresql 10.6 h66cca7a_1000 conda-forge proj4 5.2.0 h14c3975_1001 conda-forge prometheus_client 0.5.0 py_0 conda-forge prompt_toolkit 2.0.7 py_0 conda-forge psutil 5.4.8 py37h14c3975_1000 conda-forge pthread-stubs 0.4 h14c3975_1001 conda-forge ptyprocess 0.6.0 py37_1000 conda-forge py 1.7.0 py_0 conda-forge py-lief 0.9.0 py37h7725739_2 pycodestyle 2.4.0 py_1 conda-forge pycosat 0.6.3 py37h14c3975_1001 conda-forge pycparser 2.19 py_0 conda-forge pycrypto 2.6.1 py37h14c3975_1002 conda-forge pycurl 7.43.0.2 py37hb7f436b_0 pyepsg 0.4.0 py_0 conda-forge pyflakes 2.1.0 py_0 conda-forge pygments 2.3.1 py_0 conda-forge pyjwt 1.7.1 py_0 conda-forge pykdtree 1.3.1 py37h3010b51_1002 conda-forge pylint 2.2.2 py37_1000 conda-forge pyodbc 4.0.24 py37hf484d3e_1000 conda-forge pyopenssl 18.0.0 py37_1000 conda-forge pyparsing 2.3.1 py_0 conda-forge pyproj 1.9.6 py37hc0953d3_1000 conda-forge pyqt 5.6.0 py37h13b7fb3_1008 conda-forge pyrsistent 0.14.9 py37h14c3975_1000 conda-forge pyshp 2.0.1 py_0 conda-forge pysocks 1.6.8 py37_1002 conda-forge pytables 3.4.4 py37h5d69a80_1004 conda-forge pytest 4.1.1 py37_1000 conda-forge pytest-arraydiff 0.3 py_0 conda-forge pytest-astropy 0.4.0 py_0 conda-forge pytest-doctestplus 0.1.3 py_0 conda-forge pytest-openfiles 0.3.1 py_0 conda-forge pytest-remotedata 0.3.1 py_0 conda-forge python 3.7.1 hd21baee_1000 conda-forge python-crfsuite 0.9.6 py37h6bb024c_1000 conda-forge python-dateutil 2.7.5 py_0 conda-forge python-libarchive-c 2.8 py37_1004 conda-forge pytz 2018.9 py_0 conda-forge pywavelets 1.0.1 py37h3010b51_1000 conda-forge pyyaml 3.13 py37h14c3975_1001 conda-forge pyzmq 17.1.2 py37h6afc9c9_1001 conda-forge qt 5.6.2 hbe13537_1012 conda-forge qtawesome 0.5.5 pyh8a2030e_0 conda-forge qtconsole 4.4.3 py_0 conda-forge qtpy 1.6.0 pyh8a2030e_0 conda-forge rasterio 1.0.14 py37h286bbcc_0 conda-forge readline 7.0 hf8c457e_1001 conda-forge requests 2.21.0 py37_1000 conda-forge requests-oauthlib 1.0.0 py_1 conda-forge rope 0.10.7 py_1 conda-forge ruamel_yaml 0.15.71 py37h14c3975_1000 conda-forge s3transfer 0.1.13 py37_1001 conda-forge scikit-image 0.14.2 py37hf484d3e_0 conda-forge scikit-learn 0.20.2 py37hd81dba3_0 scipy 1.2.0 py37h7c811a0_0 seaborn 0.9.0 py_0 conda-forge secretstorage 3.1.0 py37_1001 conda-forge send2trash 1.5.0 py_0 conda-forge setuptools 40.6.3 py37_0 conda-forge shapely 1.6.4 py37h092830f_1002 conda-forge simplegeneric 0.8.1 py_1 conda-forge singledispatch 3.4.0.3 py37_1000 conda-forge sip 4.18.1 py37hf484d3e_1000 conda-forge six 1.12.0 py37_1000 conda-forge smart_open 1.8.0 py_0 conda-forge snappy 1.1.7 hf484d3e_1002 conda-forge snowballstemmer 1.2.1 py_1 conda-forge snuggs 1.4.1 py_1 conda-forge sortedcollections 1.1.1 py_0 conda-forge sortedcontainers 2.1.0 py_0 conda-forge soupsieve 1.7.1 py37_1000 conda-forge sphinx 1.8.3 py37_1000 conda-forge sphinxcontrib 1.0 py37_1 sphinxcontrib-websupport 1.1.0 py_1 conda-forge spyder 3.3.2 py37_1001 conda-forge spyder-kernels 0.3.0 py37_1001 conda-forge sqlalchemy 1.2.16 py37h14c3975_1000 conda-forge sqlite 3.26.0 h67949de_1000 conda-forge statsmodels 0.9.0 py37h3010b51_1000 conda-forge sympy 1.3 py37_1000 conda-forge tblib 1.3.2 py_1 conda-forge terminado 0.8.1 py37_1001 conda-forge testpath 0.4.2 py37_1000 conda-forge tk 8.6.9 h84994c4_1000 conda-forge toolz 0.9.0 py_1 conda-forge tornado 5.1.1 py37h14c3975_1000 conda-forge tqdm 4.29.1 py_0 conda-forge traitlets 4.3.2 py37_1000 conda-forge twython 3.7.0 py_0 conda-forge tzcode 2018g h14c3975_1001 conda-forge unicodecsv 0.14.1 py_1 conda-forge unixodbc 2.3.7 h227dcee_1000 conda-forge urllib3 1.24.1 py37_1000 conda-forge wcwidth 0.1.7 py_1 conda-forge webencodings 0.5.1 py_1 conda-forge werkzeug 0.14.1 py_0 conda-forge wheel 0.32.3 py37_0 conda-forge widgetsnbextension 3.4.2 py37_1000 conda-forge wrapt 1.11.1 py37h14c3975_0 conda-forge wurlitzer 1.0.2 py37_1000 conda-forge xarray 0.11.3 py37_0 conda-forge xerces-c 3.2.2 hac72e42_1001 conda-forge xlrd 1.2.0 py_0 conda-forge xlsxwriter 1.1.2 py_0 conda-forge xlwt 1.3.0 py_1 conda-forge xorg-kbproto 1.0.7 h14c3975_1002 conda-forge xorg-libice 1.0.9 h14c3975_1004 conda-forge xorg-libsm 1.2.3 h4937e3b_1000 conda-forge xorg-libx11 1.6.6 h14c3975_1000 conda-forge xorg-libxau 1.0.8 h14c3975_1006 conda-forge xorg-libxdmcp 1.1.2 h14c3975_1007 conda-forge xorg-libxext 1.3.3 h14c3975_1004 conda-forge xorg-libxrender 0.9.10 h14c3975_1002 conda-forge xorg-libxt 1.1.5 h14c3975_1002 conda-forge xorg-renderproto 0.11.1 h14c3975_1002 conda-forge xorg-xextproto 7.3.0 h14c3975_1002 conda-forge xorg-xproto 7.0.31 h14c3975_1007 conda-forge xz 5.2.4 h14c3975_1001 conda-forge yaml 0.1.7 h14c3975_1001 conda-forge zeromq 4.2.5 hf484d3e_1006 conda-forge zict 0.1.3 py_0 conda-forge zlib 1.2.11 h14c3975_1004 conda-forge zstd 1.3.8 h0a35d5f_1001 conda-forge
dja001 commented 5 years ago

Same problem here. Projections are wrong after deepcopy.

Sleekery commented 4 years ago

Problem still exists. I'm trying to multiprocess to make a series of maps faster, but it's just non-stop errors. I get errors like this, although the errors change every time I run it and are non-repeatable.

---------------------------------------------------------------------------
RemoteTraceback                           Traceback (most recent call last)
RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/Users/Otter/anaconda3/lib/python3.7/site-packages/shapely/predicates.py", line 15, in __call__
    return self.fn(this._geom, other._geom, *args)
  File "/Users/Otter/anaconda3/lib/python3.7/site-packages/shapely/geos.py", line 548, in errcheck_predicate
    raise PredicateError("Failed to evaluate %s" % repr(func))
shapely.errors.PredicateError: Failed to evaluate <_FuncPtr object at 0xb1c0ef460>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Otter/anaconda3/lib/python3.7/multiprocessing/pool.py", line 121, in worker
    result = (True, func(*args, **kwds))
  File "/Users/Otter/anaconda3/lib/python3.7/multiprocessing/pool.py", line 44, in mapstar
    return list(map(*args))
  File "<ipython-input-15-83b2af84070c>", line 78, in MakePerCapitaMap
    ax.add_geometries([record.geometry],plottingprojection,lw=1,edgecolor='k',facecolor=lightgray,zorder=1)
  File "/Users/Otter/anaconda3/lib/python3.7/site-packages/cartopy/io/shapereader.py", line 179, in geometry
    self._shape)
  File "/Users/Otter/anaconda3/lib/python3.7/site-packages/cartopy/io/shapereader.py", line 113, in _make_geometry
    geometry = geometry_factory(shape)
  File "/Users/Otter/anaconda3/lib/python3.7/site-packages/cartopy/io/shapereader.py", line 101, in _create_polygon
    if outer_polygon.contains(inner_polygon):
  File "/Users/Otter/anaconda3/lib/python3.7/site-packages/shapely/geometry/base.py", line 691, in contains
    return bool(self.impl['contains'](self, other))
  File "/Users/Otter/anaconda3/lib/python3.7/site-packages/shapely/predicates.py", line 18, in __call__
    self._check_topology(err, this, other)
  File "/Users/Otter/anaconda3/lib/python3.7/site-packages/shapely/topology.py", line 38, in _check_topology
    self.fn.__name__, repr(geom)))
shapely.errors.TopologicalError: The operation 'GEOSContains_r' could not be performed. Likely cause is invalidity of the geometry <shapely.geometry.polygon.Polygon object at 0xb1c6a9810>
"""

The above exception was the direct cause of the following exception:

TopologicalError                          Traceback (most recent call last)
<ipython-input-18-2b82528bbac5> in <module>
      4 numprocesses=4
      5 pool=Pool(processes=numprocesses)
----> 6 pool.map(MakePerCapitaMap,loops[:2])
      7 pool.close()
      8 print(TimeElapsed(starttime,time.time()))

~/anaconda3/lib/python3.7/multiprocessing/pool.py in map(self, func, iterable, chunksize)
    266         in a list that is returned.
    267         '''
--> 268         return self._map_async(func, iterable, mapstar, chunksize).get()
    269 
    270     def starmap(self, func, iterable, chunksize=None):

~/anaconda3/lib/python3.7/multiprocessing/pool.py in get(self, timeout)
    655             return self._value
    656         else:
--> 657             raise self._value
    658 
    659     def _set(self, i, obj):

TopologicalError: The operation 'GEOSContains_r' could not be performed. Likely cause is invalidity of the geometry <shapely.geometry.polygon.Polygon object at 0xb1c6a9810>
image
dja001 commented 4 years ago

Yes, I was faced with the same problem. The workaround I found is to initialise the cartopy objects within each task of the multiprocessing job.

Sleekery commented 4 years ago

Yes, I was faced with the same problem. The workaround I found is to initialise the cartopy objects within each task of the multiprocessing job.

I'm a relative newbie at multiprocessing. Can you give an example? This is how I currently do it, with loops being my interable:

from multiprocessing import Pool
import multiprocessing as mp
numprocesses=4
pool=Pool(processes=numprocesses)
results=pool.map(MakeStatePerCapitaPlot,loops)
pool.close()

My "MakeStatePerCapitaPlot" function has these lines in them:

figureprojection=ccrs.PlateCarree()
plottingprojection=ccrs.PlateCarree()
fig=plt.figure(figsize=(24,12))
ax=fig.add_subplot(1,1,1,projection=figureprojection)

Which apparently doesn't count as initializing the cartopy objects.

pelson commented 4 years ago

I took a look at this in #1491. Please feel free to install and test locally, and add feedback on the pull request.

moss-xyz commented 8 months ago

Just wanted to chime in: I'm seeing an entirely different error when I try and deepcopy a Cartopy projection:

import cartopy
import copy

proj = cartopy.crs.CRS("3521")
proj_c = copy.deepcopy(proj)

The code above will create a correct projection object for proj, but will fail to create proj_c with the following error: TypeError: CRS.__init__() missing 1 required positional argument: 'proj4_params'

If anyone knows why this is the case, would love to hear it! Otherwise, I'll be implementing a workaround in my code for now

pelson commented 8 months ago

Looking briefly at the code I can't see why this would be the case. Suggest opening up a specific issue for this (and providing cartopy version, Python version, and a traceback) - I can't see that it will be a major implementation to fix a problem if it exists. :+1: