diff --git a/clip_RequiredData.py b/clip_RequiredData.py index 3e07f14a274cab5b49935d4d816a686050137773..ebd2fc69a046d9849d4382939901ed233739eb43 100644 --- a/clip_RequiredData.py +++ b/clip_RequiredData.py @@ -16,11 +16,16 @@ import pandas as pd # Watersheds # shp_stations_filepath = 'E:/DonneesLaura/BanqueHydro/Shapes/StationBHYDRO_L93.shp' df_stations_filepath = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/BanqueHydro/StationsNonInfluenceesExplore2/Synthèse analyses/Synthèse_meta_selection_624.csv' -shp_contour_filepath = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/BanqueHydro/Shapes/BassinsVersantsMetropole/BV_4207_stations.shp' +shp_contour_filepath = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/BanqueHydro/Shapes/BassinsVersantsMetropole/BV_4207_stations.shp' +shp_BV_IV_path ='C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/BV_InvalidesValides/BVsInvalidesValides.shp' df_stations = pd.read_csv(df_stations_filepath, sep = ';', encoding='latin-1') shp_contour = gpd.read_file(shp_contour_filepath) +shp_BV_IV = gpd.read_file(shp_BV_IV_path) +# shp_BV_VV = gpd.read_file('C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/BV_InvalidesValides/BVsValidesValides.shp') + +shp_foldername = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/GEOMETRY/' # Banque Hydro banquehydro_foldername = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/BanqueHydro/Export2020/' @@ -65,12 +70,39 @@ no_geometry = shp_watersheds[shp_watersheds['geometry'] == None] studied_watersheds = shp_watersheds.loc[shp_watersheds.loc[:,'Code'].isin(no_duplicated.loc[:,'Code'])] complete_watersheds = studied_watersheds[studied_watersheds['geometry'] != None] +# complete_watersheds.to_file('C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/complete_df_wrong_geometries.shp') + +complete_watersheds_noElle = complete_watersheds.drop(complete_watersheds.loc[complete_watersheds.loc[:, 'Code'] == 'J4742020'].index[0]) + +## Dealing with BV_IV +df_BV_IV = no_duplicated.loc[no_duplicated.loc[:,'Code'].isin(shp_BV_IV.loc[:,'Code'])] +shp_BV_IV = shp_BV_IV.loc[:, ['Code', 'S_km2', 'dt_pstn', 'geometry']] +bv_IV = df_BV_IV.merge(shp_BV_IV, on = 'Code', how = 'outer') +bv_IV = gpd.GeoDataFrame(bv_IV) + +# bv_IV.to_file('C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/BV_IV.shp') + +## Replacing problematic watersheds'geometry in final df +index_BV_Inv = complete_watersheds.loc[complete_watersheds.loc[:,'Code'].isin(bv_IV.loc[:,'Code'])].index +new_geometries = bv_IV.loc[:, ['Code', 'geometry']] +# for i in index_BV_Inv: +# print(i) +# geometry = bv_IV.loc[bv_IV.loc[:, 'Code'] == complete_watersheds.loc[i, 'Code']] +# print(geometry) +# # print(geometry.__dict__) +# complete_watersheds = complete_watersheds.loc[i,:].replace(geometry.__dict__) +# print(complete_watersheds.loc[i, 'geometry']) + + # *************************** Create watersheds' shapefiles and rasters ************************************ # List of the stations'codes -watershed_code = df_stations.loc[:,'Code'] +watershed_code = complete_watersheds.loc[:,'Code'] +# watershed_code_SAFRAN = complete_watersheds_noElle.loc[:,'Code'] +watershed_code_IV = shp_BV_IV.loc[shp_BV_IV.loc[:,'Code'].isin(watershed_code)].loc[:, 'Code'] + # Get a sample of them for test -code_for_test = watershed_code.loc[170:171] +code_for_test = watershed_code.loc[0:3] # code_for_test = ['K9341810'] # code_for_test = ['J4742020'] @@ -80,9 +112,10 @@ code_for_test = watershed_code.loc[170:171] for i in watershed_code: ## Get shp - shpfile_contour_i = shp_contour[shp_contour.loc[:, 'Code'] == i] + shpfile_contour_i = gpd.read_file(shp_foldername+i+'.shp') + # shpfile_contour_i = shp_contour[shp_contour.loc[:, 'Code'] == i] # And save it in a file (identifier = its code) - shpfile_contour_i.to_file('C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/GEOMETRY/'+i+'.shp') + # shpfile_contour_i.to_file('C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/GEOMETRY/'+i+'.shp') ## Get watershed's geologic properties and save them in file @@ -92,7 +125,7 @@ for i in watershed_code: except: shpfile_BDLisa = gpd.overlay(BDLisa_shp, shpfile_contour_i) - shpfile_BDLisa.to_file('C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/GEOL/'+i+'_BDLisa.shp') + shpfile_BDLisa.to_file('C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/GEOL/BDLisa/'+i+'_BDLisa.shp') # BRGM geol try: @@ -100,16 +133,17 @@ for i in watershed_code: except: shpfile_BRGM = gpd.overlay(BRGM_shp, shpfile_contour_i) - shpfile_BRGM.to_file('C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/GEOL/'+i+'_BRGM.shp') + shpfile_BRGM.to_file('C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/GEOL/BRGM/'+i+'_BRGM.shp') ## SAFRAN csv -for i in code_for_test: -# for i in watershed_code: +# for i in code_for_test: +for i in watershed_code_IV: ## Get shp - shpfile_contour_i = shp_watersheds[shp_watersheds.loc[:, 'Code'] == i] + shpfile_contour_i = complete_watersheds[complete_watersheds.loc[:, 'Code'] == i] + # shpfile_contour_i = bv_IV[bv_IV.loc[:, 'Code'] == i] # And save it in a file (identifier = its code) # shpfile_contour_i.to_file('C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/GEOMETRY/'+i+'.shp') @@ -120,7 +154,7 @@ for i in code_for_test: HCF.intersect_safran_gpd_and_contour(Rain_gpd, shpfile_contour_i, 'Rain') HCF.safran_timeseries['Ptot'] = HCF.safran_timeseries['Snow'] + HCF.safran_timeseries['Rain'] - HCF.safran_timeseries.to_csv('C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/SAFRAN/'+i+'_safran_timeseries.csv', index=False) + # HCF.safran_timeseries.to_csv('C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/SAFRAN/'+i+'_safran_timeseries.csv', index=False)