cartopy icon indicating copy to clipboard operation
cartopy copied to clipboard

contourf plot from csv file on cartopy map - Error: TopologyException: side location conflict at

Open jwynnsmith opened this issue 5 years ago • 8 comments

Description

I am using cartopy to produce a contour plot on a grid. The csv file contains positive and negative float values. I have used code nearly identical to this but have never gotten this error below.

Error

opologyException: side location conflict at 1.0537325376034968e+29 1.0537325376034968e+29 Traceback (most recent call last): File "/home/jwsmith/.conda/envs/glmval/lib/python3.6/site-packages/shapely/predicates.py", line 15, in call return self.fn(this._geom, other._geom, *args) File "/home/jwsmith/.conda/envs/glmval/lib/python3.6/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 0x7fa3209f6cc8>

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "statsplots_corr_map.py", line 58, in mm = plt.contourf(xlon, ylat, grid, levels = levs, zorder=2, cmap=cmap) File "/home/jwsmith/.conda/envs/glmval/lib/python3.6/site-packages/matplotlib/pyplot.py", line 2530, in contourf **kwargs) File "/home/jwsmith/.conda/envs/glmval/lib/python3.6/site-packages/cartopy/mpl/geoaxes.py", line 1404, in contourf for col in result.collections File "/home/jwsmith/.conda/envs/glmval/lib/python3.6/site-packages/cartopy/mpl/geoaxes.py", line 1405, in if col.get_paths()]) File "/home/jwsmith/.conda/envs/glmval/lib/python3.6/site-packages/matplotlib/collections.py", line 188, in get_datalim paths = [transform.transform_path_non_affine(p) for p in paths] File "/home/jwsmith/.conda/envs/glmval/lib/python3.6/site-packages/matplotlib/collections.py", line 188, in paths = [transform.transform_path_non_affine(p) for p in paths] File "/home/jwsmith/.conda/envs/glmval/lib/python3.6/site-packages/matplotlib/transforms.py", line 2390, in transform_path_non_affine return self._a.transform_path_non_affine(path) File "/home/jwsmith/.conda/envs/glmval/lib/python3.6/site-packages/cartopy/mpl/geoaxes.py", line 199, in transform_path_non_affine getattr(self, 'force_path_ccw', False)) File "/home/jwsmith/.conda/envs/glmval/lib/python3.6/site-packages/cartopy/mpl/patch.py", line 189, in path_to_geos collection[-1][0].contains(geom.exterior)): File "/home/jwsmith/.conda/envs/glmval/lib/python3.6/site-packages/shapely/geometry/base.py", line 691, in contains return bool(self.impl['contains'](self, other)) File "/home/jwsmith/.conda/envs/glmval/lib/python3.6/site-packages/shapely/predicates.py", line 18, in call self._check_topology(err, this, other) File "/home/jwsmith/.conda/envs/glmval/lib/python3.6/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 0x7f9d05adce48>

If you are proposing an enhancement/new feature, provide links to related articles, reference examples, etc.

If you are asking a question, please ask on StackOverflow and use the cartopy tag. All cartopy questions on StackOverflow can be found at https://stackoverflow.com/questions/tagged/cartopy

Code to reproduce

import numpy as np import cartopy.crs as ccrs import cartopy.feature as cfeature from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER import matplotlib.pylab as plt from matplotlib import ticker, cm import pyproj import glob

xcoords = np.loadtxt('./xvals_withheight.csv', delimiter=',', unpack=True) ycoords = np.loadtxt('./yvals_withheight.csv', delimiter=',', unpack=True) ycoords = np.flip(ycoords) xx, yy = np.meshgrid(xcoords,ycoords)

vmax = 1.0 vmin = -1.0 xtcks = np.linspace(vmin, vmax, num=21) levs = np.linspace(vmin, vmax, num=201)

grid = np.load('r_jja_test.npy') grid = np.ma.masked_where(grid < -0.9999, grid)

proj4string = '+units=m +lat_0=0.0 +lon_0=-75.0 +sweep=x +h=35786023.0 +a=6378137 +b=6356752.31414 +proj=geos' proj = pyproj.Proj(proj4string)

xlon, ylat = proj(xx,yy, inverse=True) states_provinces = cfeature.NaturalEarthFeature( category='cultural', name='admin_1_states_provinces_lines', scale='10m', facecolor='none') cmap = cm.seismic plt.figure(figsize=(13,6.2)) ax = plt.subplot(111, projection=ccrs.PlateCarree()) ax.set_extent([-126, -54, 14, 53] , crs=ccrs.PlateCarree()) ax.add_feature(cfeature.LAKES, edgecolor='k', zorder=1) mm = plt.contourf(xlon, ylat, grid, levels = levs, zorder=2, cmap=cmap) print('plot generated and map features being added!') ax.add_feature(states_provinces, edgecolor='k', linewidth= 0.5, zorder=3) ax.add_feature(cfeature.BORDERS, edgecolor='k', zorder=4)

plt.title('JJA 2019 fedminutes and sumfed Correlation Map') plt.colorbar(mm, orientation='vertical',ticks=xtcks,shrink=0.8, format='%1.2f',pad=0.015, spacing='proportional') ax.coastlines('10m', color='k', linewidth=0.5, zorder=5); plt.savefig('./fedminutes_sumfed_r_jja.png', bbox_inches='tight', dpi=600)

plt.clf() ``


#### Traceback 


<details>
 <summary>Full environment definition</summary>

<!-- fill in the following information as appropriate -->

### Operating system


### Cartopy version
0.17.0

### conda list


### pip list


</details>

jwynnsmith avatar Apr 30 '20 16:04 jwynnsmith

This is highly dependent on input data, which you haven't provided.

QuLogic avatar May 01 '20 06:05 QuLogic

I just ran into a similar issue. For me it was: TopologyException: side location conflict at 0 0. I don't know what's meant by location 0 0, but I looked at the value in my array at [0,0] and it was -3.832079e-24. The exception went away when I set all values below a threshold to zero (for me the threshold was 0.1, since all values below that are white in my colormap).

It's worth mentioning that I was using scipy.ndimage.zoom on my data to add some more points, and the interpolation often results in many extremely small, but nonzero data values (like the point at 0,0). Until now, I've never had a problem with anything of this sort for lots of different data, colormaps, map domains, and projections, so I would say this is isolated occurrence. Definitely a strange bug to say the least!

karlwx avatar Aug 10 '20 20:08 karlwx

My mother is facing a similar issue when using contourf() on a cartopy map. For her, it was 'TopologyException: side location conflict at 67 56.' I am not sure if these are latitude/longitude values or index values for an array. Interestingly, we tried running contour() instead of contourf() and it was able to run successfully. I don't think it is an issue with the values of our Z array itself because there are no outliers. From looking at the same error for PosGIS, it seems like an issue with an invalid polygon that contourf() cannot fill. Perhaps, there is an intersecting line but we don't know. Please let us know if you have any updates! Although it is reassuring that contour() is working, we need to use contourf() for our tasks. Thank you. Python 3.6.3

am-thomas avatar Jun 22 '21 14:06 am-thomas

Description I am using cartopy to produce a contour fill plot on a grid. The script computes probability values from input data set given as a csv file. The code has been successfully running fro two years and has failed 2 times.

Error

TopologyException: side location conflict at 199 45 Traceback (most recent call last): File "/usrx/local/prod/packages/python/3.6.3/lib/python3.6/site-packages/shapely/predicates.py", line 15, in call return self.fn(this._geom, other._geom, *args) File "/usrx/local/prod/packages/python/3.6.3/lib/python3.6/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 0x2b0758a6f1d8>

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "prob_gefs_northpac.py", line 278, in transform=ccrs.PlateCarree(),colors=colours,extend='max') File "/usrx/local/prod/packages/python/3.6.3/lib/python3.6/site-packages/matplotlib/pyplot.py", line 2933, in contourf ret = ax.contourf(*args, **kwargs) File "/usrx/local/prod/packages/python/3.6.3/lib/python3.6/site-packages/cartopy/mpl/geoaxes.py", line 1396, in contourf for col in result.collections File "/usrx/local/prod/packages/python/3.6.3/lib/python3.6/site-packages/cartopy/mpl/geoaxes.py", line 1397, in if col.get_paths()]) File "/usrx/local/prod/packages/python/3.6.3/lib/python3.6/site-packages/matplotlib/collections.py", line 196, in get_datalim paths = [transform.transform_path_non_affine(p) for p in paths] File "/usrx/local/prod/packages/python/3.6.3/lib/python3.6/site-packages/matplotlib/collections.py", line 196, in paths = [transform.transform_path_non_affine(p) for p in paths] File "/usrx/local/prod/packages/python/3.6.3/lib/python3.6/site-packages/matplotlib/transforms.py", line 2499, in transform_path_non_affine return self._a.transform_path_non_affine(path) File "/usrx/local/prod/packages/python/3.6.3/lib/python3.6/site-packages/cartopy/mpl/geoaxes.py", line 189, in transform_path_non_affine getattr(self, 'force_path_ccw', False)) File "/usrx/local/prod/packages/python/3.6.3/lib/python3.6/site-packages/cartopy/mpl/patch.py", line 212, in path_to_geos collection[-1][0].contains(geom.exterior)): File "/usrx/local/prod/packages/python/3.6.3/lib/python3.6/site-packages/shapely/geometry/base.py", line 691, in contains return bool(self.impl['contains'](self, other)) File "/usrx/local/prod/packages/python/3.6.3/lib/python3.6/site-packages/shapely/predicates.py", line 18, in call self._check_topology(err, this, other) File "/usrx/local/prod/packages/python/3.6.3/lib/python3.6/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 0x2b076099cc18>

Code to reproduce

The code and input data is attached.


#### Traceback 


<details>
 <summary>Full environment definition</summary>

<!-- fill in the following information as appropriate -->

### Operating system
Linux
Python 3.6.3

### Cartopy version


### conda list


### pip list


</details>
[prob_gefs_northpac.txt](https://github.com/SciTools/cartopy/files/8218490/prob_gefs_northpac.txt)
[prob_storm_data_file.txt](https://github.com/SciTools/cartopy/files/8218492/prob_storm_data_file.txt)

anuappu avatar Mar 09 '22 22:03 anuappu

Hi QuLogic: I have a similar issue as stated by jwynnsmith. I have attached the sample program and a test data. Thanks in advance.

anuappu avatar Mar 09 '22 22:03 anuappu

This error has recently been coming up for me as well. It only seems to happen when I am using rectangular projections, specifically Mercator.

wxbinge avatar Mar 18 '22 14:03 wxbinge

I had a case when it failed for LambertConformal projection too.

anuappu avatar Mar 18 '22 14:03 anuappu

For me the error came up when I changed projection from PlateCarree where it worked, to LambertConformal, where the error occurred without be changing anything else in the Code.

veni-vidi-vici-dormivi avatar Jan 28 '24 14:01 veni-vidi-vici-dormivi

I tried to run the example from @anuappu with current package versions but it failed before it got to the plotting step. Perhaps because pandas has changed something. Since there are no other reproducing examples here, we cannot investigate the issue. So I'm going to close this.

If anyone still has the problem with the latest packages, please open a new issue with a minimal reproducing example.

rcomer avatar Feb 27 '24 17:02 rcomer