Projection error following groupby

Hi everyone,

I’m experiencing projection errors following a groupby on geodataframe. Below you will find the libraries that I am using:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import holoviews as hv
from holoviews import opts
import panel as pn
from bokeh.resources import INLINE
import geopandas as gpd
import geoviews as gv
from cartopy import crs
hv.extension('bokeh', 'matplotlib')
gv.extension('bokeh')
pd.options.plotting.backend = 'holoviews'

Whilst these are the versions of some key libraries:
bokeh 2.1.1
geopandas 0.6.1
geoviews 1.8.1
holoviews 1.13.3

I have concatenated 3 shapefiles to build a polygon picture of UK healthcare boundaries (links to files provided if needed). Unfortunately, from what i have found the UK doesn’t produce one file that combines all of those, so have had to merge the shape files from the 3 individual countries i’m interested in. The 3 shape files have a size of:

shape file 1 = (https://www.opendatani.gov.uk/dataset/department-of-health-trust-boundaries)

shape file 2 = (https://geoportal.statistics.gov.uk/datasets/5252644ec26e4bffadf9d3661eef4826_4)

shape file 3 = I’m a new user so I can’t post the 3rd link, sorry! But it is the Welsh CCG boundries

My code to concat them together is below:

England_CCG.drop(['objectid', 'bng_e', 'bng_n', 'long', 'lat', 'st_areasha', 'st_lengths'], inplace = True, axis = 1 )
Wales_HB.drop(['objectid', 'bng_e', 'bng_n', 'long', 'lat', 'st_areasha', 'st_lengths', 'lhb16nmw'], inplace = True, axis = 1 )
Scotland_HB.drop(['Shape_Leng', 'Shape_Area'], inplace = True, axis = 1)
#NI_HB.drop(['Shape_Leng', 'Shape_Area'], inplace = True, axis = 1 )

England_CCG.rename(columns={'ccg20cd': 'CCG_Code', 'ccg20nm': 'CCG_Name'}, inplace = True )
Wales_HB.rename(columns={'lhb16cd': 'CCG_Code', 'lhb16nm': 'CCG_Name'}, inplace = True )
Scotland_HB.rename(columns={'HBCode': 'CCG_Code', 'HBName': 'CCG_Name'}, inplace = True )
#NI_HB.rename(columns={'TrustCode': 'CCG_Code', 'TrustName': 'CCG_Name'}, inplace = True )

UK_shape = [England_CCG, Wales_HB, Scotland_HB]

Merged_Shapes = gpd.GeoDataFrame(pd.concat(UK_shape))

Each of the files has the same esri projection once joined, and the shape plots perfectly as one when I run:

Test= gv.Polygons(Merged_Shapes, vdims=[('CCG_Name')], crs=crs.OSGB())

This gives me a polygon plot of the UK, with all the area boundaries for each ccg.

To my geodataframe, I then add a new column, called ‘Country’ which attributes each CCG to whatever the country they belong to. So, all the Welsh CCGs are attributed to Wales, all the English ones to England and all the Scottish ones to Scotland.

What I want to achieve is to have a dropdown next to the polygon map I am making, that will show all the CCGs in a particular country when it is selected from the drop down widget. I understand that one to to do this is by a groupby. However, when I use the following code to achieve this:

c1 = gv.Polygons(Merged_Shapes, vdims=[('CCG_Name','Country')], crs=crs.OSGB()).groupby(['Country'])

I get a long list of projection errors stating:

“WARNING:param.project_path: While projecting a Polygons element from a PlateCarree coordinate reference system (crs) to a Mercator projection none of the projected paths were contained within the bounds specified by the projection. Ensure you have specified the correct coordinate system for your data.”

To which I am left without a map but I retain the widget. Does anyone know what is going wrong here and what a possible solution would be? its been driving me crazy!

Kind regards,

For some reason geoviews doesn’t like the OSGB projection then followed by a groupby, as it tries to default back to platecarree projection for the country data, I am thinking anyway.

The way I fixed it was to just make the entire data set project in epsg:4326, so didnt need to specify a projection for the data in code, as geoviews can then use its default platecarree method for projection. For anyone who also runs into this problem, code below (it is a well documented solution):

    Merged_Shapes.to_crs({'init': 'epsg:4326'},inplace=True) 
    gv.Polygons(Merged_Shapes, vdims=[('CCG_Name'),('Country')]).groupby('Country')

The groupby works fine after this. I think for anyone using geoviews, I think it might be worth converting all your data to platecarree before plotting ect. It might be worth considering in the future, if we could have any functionality whereby we can change the default projection from platecarree to any other valid projection we like (as long as Mercator can project it).

Great library through! Apologies if my question wasn’t very easy to follow!

For some reason geoviews doesn’t like the OSGB projection then followed by a groupby, as it tries to default back to platecarree projection for the country data

Can you post what you tried here? groupby should definitely keep the crs parameter you set on the original data. If not that’s a bug.

It might be worth considering in the future, if we could have any functionality whereby we can change the default projection from platecarree

Do you mean the default projection for the data or the default projection the data is plotted in?

Can you post what you tried here? groupby should definitely keep the crs parameter you set on the original data. If not that’s a bug.

Sure, code below, as the data was defaulted as OSGB, I called the projection method as part of the gv.Polygons statement. When ruinning this code I would get the error “While projecting a Polygons element from a PlateCarree coordinate reference system (crs) to a Mercator projection”. Way I fixed it was to make the entire dataset project as platecarree so wouldnt need to call a specific projection in the gv.Polygons statement.

c1 = gv.Polygons(Merged_Shapes, vdims=[('CCG_Name','Country')], crs=crs.OSGB()).groupby(['Country'])

Do you mean the default projection for the data or the default projection the data is plotted in?

Yes so being able to change the default projection of geoviews from platecarree to OSGB. This might help with the groupby issue I faced.

Kind regards