Exemple #1
0
def add_data_to_municipality(  
                        counts,
                        map_year=2019,
                        codmun_col='CODMUNRES',
                        title_cols=['SEXO','IDADE_ANOS'],
                        value_col='CONTAGEM'):

    """
    Adiciona dados de mortalidade aos seus respectivos municípios. Gera um GeoDataFrame do GeoPandas.
    :param counts: dataframe contendo os dados a serem agregados.
    :param map_year: ano do mapa a ser usado (biblioteca geobr).
    :param codmun_col: coluna com geocode do município
    :param title_cols: colunas que serão utilizadas para formar o título das colunas no GeoDataFrame.
    :param value_col: coluna com o valor a ser adicionado ao GeoDataFrame
    :return:
    """

    # Extrai código do estado dos municípios.
    # 2 primeiros dígitos do código são o estado
    states = counts[counts[codmun_col] != 'nan'][codmun_col].apply(lambda x: str(x)[:2]).unique()
    geo_df = read_municipality(code_muni=states[0],year=map_year)
    
    if(len(states) > 1):
        for state in states[1:]:
            geo_df = geo_df.append(read_municipality(code_muni=state,year=map_year))

    column_names = column_name_list(counts,title_cols)
    geo_df[column_names] = 0

    for i, mun in geo_df.iterrows():
        data = counts[(counts[codmun_col] == mun['code_muni']) & (counts[value_col] > 0.0)]
        for _, item in data.iterrows():
            geo_df.loc[i, column_name(item,title_cols)] = item[value_col]

    return geo_df.fillna(0)
Exemple #2
0
def total_veh_domain(lon1, lat1, lon2, lat2, year, UF, veh_sep18):
    '''
    Parameters
    ----------
    lon1 : float number
        longitude 1 of the south-west.
    lat1 : float number
        latitude 1 of the south-west
    lon2 : float number
        longitude 2 of the north-east.
    lat2 : float number
        latitude 2 of the north-east.
    year : integer
        year analized.
    UF : string list
        Unidades Federais de Brasil
    veh : Pandas DataFrame
        Number of vehicle types by UF and municipality.

    Returns
    -------
    Number of vehicles inside the modeling domain area 

    '''
    mun = geobr.read_municipality(code_muni='all', year=2018)
    polygon = Polygon([(lon1, lat1), (lon1, lat2), (lon2, lat2), (lon2, lat1),
                       (lon1, lat1)])

    munDomain = gpd.clip(mun, polygon)  ## This is very important
    munDomain = munDomain.sort_values(by='abbrev_state')
    munnames = list(munDomain.name_muni.values)
    munNAMES = [x.upper() for x in munnames]
    munNAMES = [strip_accents(x) for x in munNAMES]
    munDomain['MUNICIPIO'] = munNAMES

    UF = ['MG', 'ES', 'RJ', 'SP', 'PR', 'SC', 'RS', 'MS', 'GO']
    vehUF = veh_sep18[veh_sep18.UF.isin(UF)]
    vehs = [
        'AUTOMOVEL', 'CAMINHAO', 'CAMINHAO TRATOR', 'CAMINHONETE', 'CAMIONETA',
        'CICLOMOTOR', 'MICRO-ONIBUS', 'MOTOCICLETA', 'MOTONETA', 'ONIBUS',
        'TRATOR ESTEI', 'TRATOR RODAS', 'UTILITARIO'
    ]

    vehUF = vehUF.filter(['UF', 'MUNICIPIO'] + vehs)
    vehUF['TOTAL'] = vehUF.sum(axis=1)

    vehMunDom1 = vehUF[vehUF.MUNICIPIO.isin(munNAMES)]
    return vehMunDom1, vehUF
def get_geobr():
    for k, v in initials.items():
        ufs = v.upper()
        df = read_municipality(code_muni=ufs, year=2019)
        cols = ['code_muni', 'code_state', 'code_region']
        df[cols] = df[cols].applymap(np.int64)
        result = df.to_json()
        parsed = json.loads(result)
        fname = f'{v}-municipalities.json'
        to_path = os.path.join(path_, fname)

        with open(to_path, 'w') as f:
            json.dump(parsed, f)
            logger.warning(f'Saving the JSON to {k} state in {to_path}')

    print('\n')
    logger.warning('All files were downloaded successfully!')
@author: mgavidia
"""

import pandas as pd
import geopandas as gpd
import geobr as gb
import xarray as xr
import matplotlib.pyplot as plt
from shapely.geometry import Polygon

# States in domain
states_codes = ["SP", "RJ", "PR", "MG", "ES", "SC", "MS"]

# Saving shapefile in a dict
states_in_domain = {
    code: gb.read_municipality(code_muni=code, year=2014)
    for code in states_codes
}

# Merging municipalities in domain
mun_gdf = pd.concat([state for code, state in states_in_domain.items()],
                    ignore_index=True)

# Reading veicule number from
# https://www.gov.br/infraestrutura/pt-br/assuntos/transito/conteudo-denatran/frota-de-veiculos-2014
veic = pd.read_excel("./frota_por_municipio_e_tipo_out_2014.xlsx",
                     sheet_name="OUT_2014",
                     header=3)

# Clip shapefiles to WRF domain
wrf = xr.open_dataset("./geo_em.d01.nc")
Exemple #5
0
def test_read_municipality():

    assert isinstance(read_municipality(), gpd.geodataframe.GeoDataFrame)

    assert isinstance(read_municipality(code_muni="AC", year=1991),
                      gpd.geodataframe.GeoDataFrame)
    assert isinstance(read_municipality(code_muni="AC", year=2010),
                      gpd.geodataframe.GeoDataFrame)
    assert isinstance(read_municipality(code_muni=11, year=1991),
                      gpd.geodataframe.GeoDataFrame)
    assert isinstance(read_municipality(code_muni=11, year=2010),
                      gpd.geodataframe.GeoDataFrame)
    assert isinstance(read_municipality(code_muni="all", year=1991),
                      gpd.geodataframe.GeoDataFrame)
    assert isinstance(read_municipality(code_muni="all", year=2010),
                      gpd.geodataframe.GeoDataFrame)

    with pytest.raises(Exception):
        read_municipality(year=9999999)

        read_municipality(code_muni="RJ_ABC")
Exemple #6
0
               crs=ccrs.PlateCarree())

# Put a background image on for nice sea rendering.
ax1.stock_img()

# Create a feature for States/Admin 1 regions at 1:50m from Natural Earth
states_provinces = cfeature.NaturalEarthFeature(
    category='cultural',
    name='admin_1_states_provinces_lines',
    scale='50m',
    facecolor='none')

ax1.add_feature(cfeature.LAND)
ax1.add_feature(cfeature.COASTLINE)
ax1.add_feature(states_provinces, edgecolor='black')
mun = geobr.read_municipality(code_muni='all', year=2018)
mun.plot(facecolor="none", alpha=1, edgecolor='gray', ax=ax1)
polygon = Polygon([(lon1, lat1), (lon1, lat2), (lon2, lat2), (lon2, lat1),
                   (lon1, lat1)])
poly_gdf = gpd.GeoDataFrame([1], geometry=[polygon], crs=mun.crs)
poly_gdf.boundary.plot(ax=ax1, color="red")

munDomain = gpd.clip(mun, polygon)  ## This is very important
munDomain = munDomain.sort_values(by='abbrev_state')

ax2 = plt.subplot(1, 2, 2, projection=ccrs.PlateCarree())
munDomain.plot(ax=ax2, color="purple", alpha=0.5)
munDomain.boundary.plot(ax=ax2)
poly_gdf.boundary.plot(ax=ax2, color="red")
ax2.set_title("Clipped", fontsize=20)
plt.savefig('04_output/emissions/fig/clip.png',
def plot_map(df, data_type, title, cmap, cbar_title, UF=None):
    #df must be in format pd.DataFrame({'id': [...] ,'z':[...]})
    year = 2018
    var = df.columns[-1]
    if data_type == 'state':
        df1 = geobr.read_state(code_state='all', year=year)
        df1 = df1.sort_values('abbrev_state').reset_index(drop=True)

        gdf = gpd.GeoDataFrame(df1[['abbrev_state', 'geometry']])
        jdf = json.loads(df1[['abbrev_state', 'geometry']].to_json())

        try:
            assert list(df1['abbrev_state'].values) == list(df['state'])
        except:
            print(
                "df is not in the same order of IBGE data, try to put it on state name alphabetical order "
            )

        plot_data = dict(type='choropleth',
                         geojson=jdf,
                         locations=gdf.index.astype(str),
                         locationmode='geojson-id',
                         colorscale=cmap,
                         text=gdf['abbrev_state'].astype(str),
                         z=df[var].astype(float),
                         colorbar={'title': cbar_title})
        layout = dict(title_text=title, geo={'scope': 'south america'})
        fig = go.Figure(data=plot_data, layout=layout)
        fig.update_geos(fitbounds="locations", visible=False)

    elif data_type == 'city':
        df1 = geobr.read_municipality(code_muni=UF, year=year)
        df1 = df1.sort_values('name_muni').reset_index(drop=True)
        df1['code_muni'] = df1['code_muni'].astype(int)

        idx = [
            pd.Index(df1['code_muni']).get_loc(i) for i in df['city_id'].values
        ]
        idx = np.array(idx)
        idxx = list(set(range(len(df1))).difference(idx))

        z = np.ones(len(df1))
        z[idx] = df[var]
        z[idxx] = np.nan

        gdf = gpd.GeoDataFrame(df1[['name_muni', 'geometry']])
        jdf = json.loads(df1[['name_muni', 'geometry']].to_json())

        try:
            assert list(df1.loc[idx]['code_muni'].values) == list(
                df['city_id'])
        except:
            print(
                "df isnt in the same order of IBGE data, try to put it on city name alphabetical order "
            )

        plot_data = dict(type='choropleth',
                         geojson=jdf,
                         locations=gdf.index.astype(str),
                         locationmode='geojson-id',
                         colorscale=cmap,
                         text=gdf['name_muni'].astype(str),
                         z=z.astype(float),
                         colorbar={'title': cbar_title})
        layout = dict(title_text=title, geo={'scope': 'south america'})
        fig = go.Figure(data=plot_data, layout=layout)
        fig.update_geos(fitbounds="locations", visible=False)

    plotly.offline.iplot(fig)
Exemple #8
0
import pandas as pd                                                                                                                                          
from geobr import read_municipality
import sys
from colorama import Fore,Style

try:
    sys.argv[1]
except:
    raise  ValueError(Fore.RED+"Please inform the year"+Style.RESET_ALL)

ano=int(sys.argv[1])
df=pd.read_csv('/home/lucas/projects/cartpy/cartpy/data/counties_1872_1991.csv')                                                                             
mun = read_municipality(code_muni="all", year=ano)                                                                                                          
mun.columns=['codigo','nome','estado_code','estado','geometry']                                                                                              
mun['ano']=ano                                                                                                                                              
new=df.append(mun)                                                                                                                                           
new['nome']=[k.title().replace('ç','c').replace('ã','a').replace('á','a').replace('à','a').replace('â','a')\
    .replace('ẽ','e').replace('é','e').replace('è','e').replace('ê','e').replace('ĩ','i').\
        replace('í','i').replace('ì','i').replace('î','i').replace('õ','o').replace('ó','o').\
            replace('ò','o').replace('ô','o').replace('ũ','u').replace('ú','u').\
                replace('ù','u').replace('û','u')\
       if isinstance(k,str) else k for k in new['nome']]

new.to_csv('/home/lucas/projects/cartpy/cartpy/data/counties_1872_1991.csv',index=False)

print("The year {} was added successfully".format(ano))