def get_distritos(): distritos = loadShapeFile('data/distritos/DISTRITOS_20200101_MADRID.shp') pob_file = pd.read_excel('data/distritos/dist_pob.xls', header=4, usecols=['Distrito', 'Total']).rename(columns={ 'Total': 'Inhabitants', 'Distrito': 'Name' }) pob_file['Name'] = pob_file['Name'].str.capitalize() distritos = distritos.join(pob_file) return distritos
def get_barrios(): barrios = loadShapeFile('data/barrios/barrios.shp') names_b = [] for nom in barrios['DESBDT']: name = nom.split(' ', 1)[-1] names_b.append(name) barrios['Name'] = np.array(names_b) pob_file = pd.read_excel( 'data/barrios/barrios_pob.xls', header=4, usecols=['Total']).rename(columns={'Total': 'Inhabitants'}) barrios = barrios.join(pob_file) barrios = barrios.drop(['DESBDT'], axis=1) return barrios
def get_secciones_censales(): census = pd.read_csv('data/CensoMadrid_2019.csv', sep=';', header=0).rename(columns={ 'Total': 'Inhabitants', 'Edad (grupos quinquenales)': 'Edad' }) census = census.loc[census['Sexo'] == 'Ambos Sexos'].loc[ census['Sección'] != 'TOTAL'].loc[census['Edad'] == 'Total'] census = census.set_index('Sección') census['Inhabitants'] = pd.to_numeric(census['Inhabitants'].str.replace( '.', '')) # Combine the census population data with the ShapeFile of seccionesCensales seccionesCensales_shp = loadShapeFile( 'data/secciones_censales/SECC_CE_20200101_MADRID.shp') census = census['Inhabitants'] census = seccionesCensales_shp.join(census, on='CUSEC') return census
def get_distritos(census): distritos = loadShapeFile('data/distritos/DISTRITOS_20200101_MADRID.shp') dist_names = [ 'Centro', 'Arganzuela', 'Retiro', 'Salamanca', 'Chamartín', 'Tetuán', 'Chamberi', 'Fuencarral-El Pardo', 'Moncloa-Aravaca', 'Latina', 'Carabanchel', 'Usera', 'Puente de Vallecas', 'Moratalaz', 'Ciudad Lineal', 'Hortaleza', 'Villaverde', 'Villa de Vallecas', 'Vicálvaro', 'San-Blas Canillejas', 'Barajas' ] dist_inhabitants = [0] * 21 # Use the inhabitants from the census areas and their CDIS value to get inhabitants in districts for i in range(len(census.index)): dist_inhabitants[int(census['CDIS'][i]) - 1] += census['Inhabitants'][i] distritos['Name'] = dist_names distritos['Inhabitants'] = dist_inhabitants return distritos
def get_secciones_censales(): columns = pd.read_excel( 'data/secciones_censales/2801_CensoMadrid2019.xls').loc[5] census = pd.read_excel('data/secciones_censales/2801_CensoMadrid2019.xls', names=columns, header=8, index_col=0) census.index = census.index.str.strip() census = census.iloc[:4417] census = census.groupby(census.index).sum() # Combine the census population data with the ShapeFile of seccionesCensales seccionesCensales_shp = loadShapeFile( 'data/secciones_censales/SECC_CE_20200101_MADRID.shp') census_inhabitants = census.rename({'Total': 'Inhabitants'}, axis=1)['Inhabitants'] census_inhabitants = seccionesCensales_shp.join(census_inhabitants, on='CUSEC') return census_inhabitants
type=str, default='data/sentinel/sent_B08.jp2', help= "String containing the location to the sentinel2 image in the B08 band." ) args = parser.parse_args() return args if __name__ == '__main__': args = arg_parse() green_build_join(args.all_spaces, args.merge_green_spaces, args.merge_build_spaces) buildSpaces = loadShapeFile('data/buildSpaces.shp') greenSpaces = loadShapeFile('data/greenSpaces.shp') if args.green_proximity: greenSpaces = greenProxInd(greenSpaces) greenSpaces.to_file('data/greenSpaces.shp', driver='ESRI Shapefile') if args.get_census: print('Starting calculations for censal areas') census = get_secciones_censales() census = indexes_calc(census, greenSpaces, buildSpaces) census = proxToSection(census, greenSpaces) NDVI_img(args.sent_B04, args.sent_B08, args.NDVI_img_out) census = calc_NDVI_section(census, args.NDVI_img_out) # Save results into a new ShapeFile census = geopandas.GeoDataFrame(census, geometry='geometry')
ndviImage = rasterio.open(img_out,'w', driver='Gtiff', width=band4.width, height = band4.height, count=1, crs=band4.crs, transform=band4.transform, dtype='float64') ndviImage.write(ndvi,1) ndviImage.close() if __name__ == '__main__': # Load ShapeFiles of green areas and censal areas columns = pd.read_excel('data/secciones_censales/2801_CensoMadrid2019.xls').loc[5] census = pd.read_excel('data/secciones_censales/2801_CensoMadrid2019.xls', names=columns, header=8, index_col=0) census.index = census.index.str.strip() census = census.iloc[:4417] census = census.groupby(census.index).sum() # Combine the census population data with the ShapeFile of seccionesCensales seccionesCensales_shp = loadShapeFile('data/secciones_censales/SECC_CE_20200101_MADRID.shp') section = census.rename({'Total': 'Inhabitants'}, axis=1)['Inhabitants'] section = seccionesCensales_shp.join(section, on='CUSEC') img_NDVI = 'out/ndviImage.tiff' b4 = 'data/sentinel/sent_B04.jp2' b8 = 'data/sentinel/sent_B08.jp2' NDVI_img(b4, b8, img_NDVI) calc_NDVI_section(section, img_NDVI)
total = 0 num = 0 greenInSection = [ greenSpaces['proxIndex'][pos] for pos in idx.intersection(section.bounds) ] for ind in greenInSection: total += ind num += 1 try: prox = total / num except ZeroDivisionError: prox = 0 prox_avg.append(prox) sectionAreas['prox_avg'] = np.array(prox_avg) return sectionAreas if __name__ == '__main__': # Load ShapeFiles of green areas and section areas greenSpaces = loadShapeFile('data/greenSpaces.shp') sectionAreas = loadShapeFile('out/indexes.shp') tic = process_time() sect = proxToSection(sectionAreas, greenSpaces) toc = process_time() print('Time:' + str(toc - tic)) #greenSpaces.to_file('data/greenSpaces.shp', driver='ESRI Shapefile')
def green_build_join(all_spaces, merge_green_spaces, merge_build_spaces): if all_spaces: nums = [] buildSpaces = pd.DataFrame([]) greenSpaces = pd.DataFrame([]) for i in range(21): if i < 9: nums.append('D0' + str(i + 1)) else: nums.append('D' + str(i + 1)) print( 'Creating build spaces and green spaces from all the census areas') for folder in nums: is_green = 1 is_build = 1 const = [] greens = [] try: buildingsUnderConstruction_shp = loadShapeFile( 'data/carto2/' + folder + '/03_EDIFICIO_EN_CONSTRUCCION_P.shp') const.append(buildingsUnderConstruction_shp) except: is_build = 0 try: buildings_shp = loadShapeFile('data/carto2/' + folder + '/03_EDIFICIO_P.shp') const.append(buildings_shp) except: is_build = 0 try: undefinedBuildings_shp = loadShapeFile( 'data/carto2/' + folder + '/03_EDIFICIO_INDEFINIDO_P.shp') const.append(undefinedBuildings_shp) except: is_build = 0 if buildSpaces.empty: buildSpaces = pd.concat([ buildings_shp, buildingsUnderConstruction_shp, undefinedBuildings_shp ]) else: if is_build: buildSpaces = pd.concat([ buildSpaces, buildings_shp, buildingsUnderConstruction_shp, undefinedBuildings_shp ]) else: for shp in const: buildSpaces = pd.concat([buildSpaces, shp]) try: urbanGardens_shp = loadShapeFile('data/carto2/' + folder + '/11_HUERTO_URBANO_P.shp') greens.append(urbanGardens_shp) except: is_green = 0 try: gardenAreas_shp = loadShapeFile('data/carto2/' + folder + '/11_ZONA_AJARDINADA_P.shp') greens.append(gardenAreas_shp) except: is_green = 0 try: gardenInPatios_shp = loadShapeFile( 'data/carto2/' + folder + '/11_ZONA_AJARDINADA_SOBRE_PATIO_P.shp') greens.append(gardenInPatios_shp) except: is_green = 0 if greenSpaces.empty: greenSpaces = pd.concat([gardenAreas_shp, gardenInPatios_shp]) else: if is_green: greenSpaces = pd.concat([ greenSpaces, gardenAreas_shp, urbanGardens_shp, gardenInPatios_shp ]) else: for shp in greens: greenSpaces = pd.concat([greenSpaces, shp]) buildSpaces.to_file('data/buildSpaces.shp', driver='ESRI Shapefile') greenSpaces.to_file('data/greenSpaces.shp', driver='ESRI Shapefile') else: if merge_green_spaces: # Load shapefiles of green and constructed areas urbanGardens_shp = loadShapeFile( 'data/Carto_1000/11_HUERTO_URBANO_P.shp') gardenAreas_shp = loadShapeFile( 'data/Carto_1000/11_ZONA_AJARDINADA_P.shp') gardenInPatios_shp = loadShapeFile( 'data/Carto_1000/11_ZONA_AJARDINADA_SOBRE_PATIO_P.shp') greenSpaces = pd.concat( [urbanGardens_shp, gardenAreas_shp, gardenInPatios_shp]) greenSpaces.to_file('data/greenSpaces.shp', driver='ESRI Shapefile') if merge_build_spaces: buildings_shp = loadShapeFile( 'data/Carto_1000/03_EDIFICIO_EN_CONSTRUCCION_P.shp') buildingsUnderConstruction_shp = loadShapeFile( 'data/Carto_1000/03_EDIFICIO_INDEFINIDO_P.shp') undefinedBuildings_shp = loadShapeFile( 'data/Carto_1000/03_EDIFICIO_P.shp') buildSpaces = pd.concat([ buildings_shp, buildingsUnderConstruction_shp, undefinedBuildings_shp ]) buildSpaces.to_file('data/buildSpaces.shp', driver='ESRI Shapefile')