Move from countrycodes to maintained pycountry
This commit is contained in:
parent
1fa62b9aab
commit
04a291b91b
@ -19,6 +19,7 @@ dependencies:
|
|||||||
- xlrd
|
- xlrd
|
||||||
- scikit-learn
|
- scikit-learn
|
||||||
- pytables
|
- pytables
|
||||||
|
- pycountry
|
||||||
|
|
||||||
# Include ipython so that one does not inadvertently drop out of the conda
|
# Include ipython so that one does not inadvertently drop out of the conda
|
||||||
# environment by calling ipython
|
# environment by calling ipython
|
||||||
@ -41,4 +42,4 @@ dependencies:
|
|||||||
- git+https://github.com/FRESNA/atlite.git
|
- git+https://github.com/FRESNA/atlite.git
|
||||||
#- git+https://github.com/FRESNA/powerplantmatching.git
|
#- git+https://github.com/FRESNA/powerplantmatching.git
|
||||||
#- https://software.ecmwf.int/wiki/download/attachments/56664858/ecmwf-api-client-python.tgz
|
#- https://software.ecmwf.int/wiki/download/attachments/56664858/ecmwf-api-client-python.tgz
|
||||||
- countrycode
|
|
||||||
|
@ -9,10 +9,14 @@ import geopandas as gpd
|
|||||||
from shapely.geometry import MultiPolygon, Polygon
|
from shapely.geometry import MultiPolygon, Polygon
|
||||||
from shapely.ops import cascaded_union
|
from shapely.ops import cascaded_union
|
||||||
|
|
||||||
try:
|
import pycountry as pyc
|
||||||
from countrycode.countrycode import countrycode
|
|
||||||
except ImportError:
|
def _get_country(target, **keys):
|
||||||
from countrycode import countrycode
|
assert len(keys) == 1
|
||||||
|
try:
|
||||||
|
return getattr(pyc.countries.get(**keys), target)
|
||||||
|
except KeyError:
|
||||||
|
return np.nan
|
||||||
|
|
||||||
def _simplify_polys(polys, minarea=0.1, tolerance=0.01, filterremote=True):
|
def _simplify_polys(polys, minarea=0.1, tolerance=0.01, filterremote=True):
|
||||||
if isinstance(polys, MultiPolygon):
|
if isinstance(polys, MultiPolygon):
|
||||||
@ -44,11 +48,9 @@ def countries():
|
|||||||
return s
|
return s
|
||||||
|
|
||||||
def eez(country_shapes):
|
def eez(country_shapes):
|
||||||
cntries = snakemake.config['countries']
|
|
||||||
cntries3 = frozenset(countrycode(cntries, origin='iso2c', target='iso3c'))
|
|
||||||
df = gpd.read_file(snakemake.input.eez)
|
df = gpd.read_file(snakemake.input.eez)
|
||||||
df = df.loc[df['ISO_3digit'].isin(cntries3)]
|
df = df.loc[df['ISO_3digit'].isin([_get_country('alpha_3', alpha_2=c) for c in snakemake.config['countries']])]
|
||||||
df['name'] = countrycode(df['ISO_3digit'], origin='iso3c', target='iso2c')
|
df['name'] = df['ISO_3digit'].map(lambda c: _get_country('alpha_2', alpha_3=c))
|
||||||
s = df.set_index('name').geometry.map(lambda s: _simplify_polys(s, filterremote=False))
|
s = df.set_index('name').geometry.map(lambda s: _simplify_polys(s, filterremote=False))
|
||||||
return gpd.GeoSeries({k:v for k,v in s.iteritems() if v.distance(country_shapes[k]) < 1e-3})
|
return gpd.GeoSeries({k:v for k,v in s.iteritems() if v.distance(country_shapes[k]) < 1e-3})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user