示例#1
0
def county2code(county_names):
    """Convert county_name to abbrevation code.

    Parameters
    ----------
    county_names : list of str
        list containing county_names.

    Returns
    -------
    list of str containing abbrevations.

    """
    [DIROK, DIRMAP] = download_resources()

    df=pd.read_csv(CITYCODE, sep=';', encoding='latin1')
    try:
        [dfmatch,_] = deepStringMatching(df.Country, county_names, methodtype='FUZZY', verbose=0)
        citymatch = dfmatch.idxmax(axis=0).values
        dfmatch.index = list(df.code)
        citycode = list(dfmatch.idxmax(axis=0).values)
    except:
        idx = np.where(np.isin(df.Country.str.lower(), county_names.lower()))[0]
        if len(idx)!=0:
            citycode = df.code.iloc[idx].values[0]
            citymatch = df.Country.iloc[idx].values[0]

    return(citycode, citymatch)
示例#2
0
def _matchmap(map_name):
    out=''
    # Get all map names from directory
    [getcounty_names,getfiles] = list_map_names()

    # Match name
    try:
        [dfmatch,_]=deepStringMatching(getcounty_names, map_name, methodtype='FUZZY', verbose=0)
        getfiles=np.array(getfiles)
        out=getfiles[np.isin(getcounty_names, dfmatch.idxmax(axis=0)[0])][0]
    except:
        idx = np.where(np.isin(getcounty_names, map_name.lower()))[0]
        if len(idx)!=0:
            out = np.array(getfiles)[idx][0]

    return(out)
示例#3
0
def plot(county_names, map_name='world', opacity=[], cmap='Set1', filename='custom_map.svg', showfig=True, verbose=True):
    """Color countries.

    Parameters
    ----------
    county_names : list of str
        Names of countries or regions within a county to be colored.
    map_name : str, optional
        Name of the county to be colored. The default is 'world'.
    opacity : list of float [0-1], optional
        Set the opacity for each of the map_name(s). The default is [].
        If values are >1, it is scaled between [0-1]
        0 = full transparancy, 1= no transparancy
    cmap : str, optional
        Colormap to be used. Colors is set on the unique county_names. The default is 'Set1'. All colormaps can be reverted using the "_r": e.g., binary_r
        ['#ffffff','#1f1f1f']: Each map with a specified color
        ['#ff0000']: All maps have a red color
        'Set1'     : Discrete colors (default)
        'Pastel1'  : Discrete colors
        'Paired'   : Discrete colors
        'bwr'      : Blue-white-red
        'RdBu'     : Red-white-Blue
        'binary'   : black-white
        'seismic'  : Blue-white-red
        'rainbow'  : all coors
        'Blues'    : white-to-blue
    filename : str, optional
        filepath to write the output file. The default is 'custom_map.svg'.
    showfig : bool, optional
        Open figure in browser. The default is False.
    verbose : int, optional
        print message to screen. The default is True.

    Returns
    -------
    tuple containing dict (out) with results and the filename (out, filename).

    """
    Param = {}
    Param['county_names'] = county_names
    Param['opacity'] = opacity
    Param['verbose'] = verbose
    Param['map_name'] = map_name.lower()
    Param['filename'] = filename
    Param['showfig'] = showfig
    Param['cmap'] = cmap

    # FILES
    [DIROK, DIRMAP] = download_resources()
    if not DIROK: return

    # SETUP COLORS SCHEMES
    if Param['opacity']==[]:
        Param['opacity']=np.ones(len(Param['county_names']))
    else:
        if np.any(np.array(Param['opacity'])>1):
            if Param['verbose']: print('[worldmap] Scaling opacity between [0-1]')
            Param['opacity'] = minmax_scale(np.append([0,np.max(Param['opacity']) * 1.5],Param['opacity']))
            Param['opacity'] = Param['opacity'][2:]

    # Get color schemes
    if 'str' in str(type(Param['cmap'])):
        import colourmap
        getcolors=np.array(sns.color_palette(Param['cmap'],len(Param['county_names'])).as_hex())
        # getcolors=colourmap.generate(len(Param['county_names']), cmap=Param['cmap'])
    elif 'list' in str(type(Param['cmap'])) and len(Param['cmap'])==1:
        getcolors=np.repeat(Param['cmap'], len(Param['county_names']))
    else:
        getcolors=Param['cmap']

    # READ MAP FROM DIRECTORY AND MATCH WITH DESIRED MAP
    getsvg = _matchmap(Param['map_name'])

    # PARSE SVG
    [paths, attributes] = svg2paths(os.path.join(DIRMAP,getsvg))
    if Param['verbose']: print('[worldmap] Map loaded and parsed: %s' %(getsvg))

    # Extract city names to color them according the scores
    d = {'county_names':Param['county_names'], 'SVGcity':np.nan, 'opacity': Param['opacity'], 'fill':getcolors, 'attr':np.nan}
    df = pd.DataFrame(d)

    # Retrieve all city/county names
    SVGcounty_names=[]
    for i in range(0,len(attributes)):
        SVGcounty_names = np.append(SVGcounty_names, attributes[i]['title'])

    # Match with best input-names
    [dfmatch,_] = deepStringMatching(SVGcounty_names,Param['county_names'], methodtype='FUZZY', verbose=0)
    # IN
    SVGcolorCity = dfmatch.idxmax(axis=0).index.values
    dfmatch.reset_index(inplace=True, drop=True)
    idx = dfmatch.idxmax(axis=0).values
    attributesIN = np.array(attributes)[idx]
    # OUT
    idxOUT=np.setdiff1d(np.arange(0,dfmatch.shape[0]),idx)
    attributesOUT = np.array(attributes)[idxOUT]

    # STORE
    df['SVGcity'] = SVGcolorCity
    df['SVGcity'] = df['SVGcity'].str.replace(' ','')
    df['attr'] = attributesIN

    if Param['verbose']: print('[worldmap] %.0f out of %.0f cities/counties detected and processed.' %(df.shape[0], len(Param['county_names'])))

    # DFOUT
    SVGcounty_namesOUT=[]
    for i in range(0,len(attributesOUT)):
        SVGcounty_namesOUT = np.append(SVGcounty_namesOUT, attributesOUT[i]['title'])
    d = {'county_names':SVGcounty_namesOUT, 'SVGcity':SVGcounty_namesOUT, 'opacity': 1, 'fill':'#CCCCCC', 'attr':attributesOUT}
    dfout = pd.DataFrame(d)
    dfout=pd.concat((df,dfout), axis=0)

    # WRITE TO FILE
    _to_svg(df, attributesOUT, Param)

    # Open figures
    if Param['showfig']: webbrowser.open(os.path.abspath(Param['filename']), new=2)

    del dfout['attr']
    return(dfout, filename)