In [12]:
import shapefile
import geopandas as gpd

In [84]:
shapefiles_cns_2016 = '/home/tono/Downloads/bseccenv10sh1f1_2002a2016_0/bseccenv10sh1f1_20160101_0/bseccenv10sh1f1_20160101_0.shp'
shapefiles_cns_2015 = '/home/tono/Downloads/bseccenv10sh1f1_2002a2016_0/bseccenv10sh1f1_20150101_0/bseccenv10sh1f1_20150101_0.shp'
data_censo2016 = gpd.read_file(shapefiles_cns)

In [77]:
d = {'080734': 'Cornella', '080771': 'Esplugues',
     '081017': 'Hospitalet'}
cmun_tr = {'080734': '08073', '080771': '08077',
           '081017': '08101'}
f_filter = lambda x: x in list(d.keys())

trio_geodata2016 = data_censo2016[data_censo2016['MUNICIPI'].apply(f_filter)]
trio_geodata2016.loc[:, 'CUSEC'] =\
    (data_censo2016['MUNICIPI'].replace(cmun_tr) +
     data_censo2016['DISTRICTE'] + data_censo2016['SECCIO'])
trio_geodata2016.reset_index(drop=True, inplace=True)

In [80]:


In [ ]:


In [83]:
trio_geodata2016.to_file('data/trio_llobregat_2016.geojson')

In [98]:
def get_trio_llobregat(filename):
    data_censo = gpd.read_file(shapefiles_cns)
    d = {'080734': 'Cornella', '080771': 'Esplugues',
         '081017': 'Hospitalet'}
    cmun_tr = {'080734': '08073', '080771': '08077',
               '081017': '08101'}
    f_filter = lambda x: x in list(d.keys())

    trio_geodata = data_censo2016[data_censo['MUNICIPI'].apply(f_filter)]
    trio_geodata.loc[:, 'CUSEC'] =\
        (data_censo['MUNICIPI'].replace(cmun_tr) +
         data_censo['DISTRICTE'] + data_censo['SECCIO'])
    trio_geodata.reset_index(drop=True, inplace=True)
    trio_geodata.to_crs(epsg=4326, inplace=True)
    return trio_geodata


trio_geodata2015 = get_trio_llobregat(shapefiles_cns_2015)
trio_geodata2015.to_file('data/trio_llobregat_2015.geojson', driver='GeoJSON')
trio_geodata2016 = get_trio_llobregat(shapefiles_cns_2016)
trio_geodata2016.to_file('data/trio_llobregat_2016.geojson', driver='GeoJSON')


/usr/local/lib/python2.7/dist-packages/geopandas/geodataframe.py:149: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  frame[geo_column_name] = level

In [86]:
help(trio_geodata2015.to_file)


Help on method to_file in module geopandas.geodataframe:

to_file(self, filename, driver='ESRI Shapefile', schema=None, **kwargs) method of geopandas.geodataframe.GeoDataFrame instance
    Write this GeoDataFrame to an OGR data source
    
    A dictionary of supported OGR providers is available via:
    >>> import fiona
    >>> fiona.supported_drivers
    
    Parameters
    ----------
    filename : string
        File path or file handle to write to.
    driver : string, default 'ESRI Shapefile'
        The OGR format driver used to write the vector file.
    schema : dict, default None
        If specified, the schema dictionary is passed to Fiona to
        better control how the file is written.
    
    The *kwargs* are passed to fiona.open and can be used to write
    to multi-layer data, store data within archives (zip files), etc.


In [97]:
help(trio_geodata2015.to_crs)


Help on method to_crs in module geopandas.geodataframe:

to_crs(self, crs=None, epsg=None, inplace=False) method of geopandas.geodataframe.GeoDataFrame instance
    Transform geometries to a new coordinate reference system
    
    This method will transform all points in all objects.  It has
    no notion or projecting entire geometries.  All segments
    joining points are assumed to be lines in the current
    projection, not geodesics.  Objects crossing the dateline (or
    other projection boundary) will have undesirable behavior.
    
    `to_crs` passes the `crs` argument to the `Proj` function from the
    `pyproj` library (with the option `preserve_units=True`). It can
    therefore accept proj4 projections in any format
    supported by `Proj`, including dictionaries, or proj4 strings.


In [ ]: