-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHanan_Stats.py
74 lines (64 loc) · 2.38 KB
/
Hanan_Stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import geopandas as gpd
import pandas as pd
import glob
import os
from rasterstats import zonal_stats
rsterDir=r'C:\Students\Hanan\Thesis_work\Data\Processing\Lebanon\LB_Planet\LB_Rasters'
mydir=r'C:\Students\Hanan\Thesis_work\Data\Processing\Lebanon\LB_Planet\LB_Rasters'
shpDir=r'C:\Students\Hanan\Thesis_work\Data\Processing\Lebanon\LB_Planet\shp'
procDir=r'C:\Students\Hanan\Thesis_work\Data\Processing\Idaho\ID_Planet\GridStats'
os.chdir(shpDir)
for shp in glob.glob('*.shp'):
print shp
shpNmeList=shp.split('.')
shpNme=shpNmeList[0]
rasterList=[]
gdf=gpd.read_file(shp)
gdf['FID']=None
counterShp=0
while counterShp<len(gdf):
gdf.loc[counterShp,'FID']=shpNme+'_'+str(counterShp)
counterShp+=1
os.chdir(rsterDir)
## for rast in glob.glob('*'):
## rasterList.append(rast)
statOpsList=['mean','sum']
for statVar in statOpsList:
print statVar
cols=[]
cols.append('FID')
for rster in rasterList:
rster=rster.split('.')
rster=rster[0]
cols.append(rster)
df=pd.DataFrame(columns=cols)
counter=0
while counter<len(gdf):
df.loc[counter,'FID']=gdf.loc[counter,'FID']
counter+=1
for rster in rasterList:
os.chdir(rsterDir)
stats=zonal_stats(gdf,rster,stats=statVar)
rster=rster.split('.')
rster=rster[0]
print rster
counter1=0
while counter1<len(stats):
stat=stats[counter1]
fld=df.loc[counter1,'FID']
counter2=0
while counter2<len(df):
if df.loc[counter2,'FID']==fld:
df.loc[counter2,rster]=stat[statVar]
counter2+=1
counter1+=1
os.chdir(mydir)
df.to_csv(shpNme+'_'+statVar+'.csv')
if statVar=='mean':
for col in cols:
if col!='FID':
df[col]=df[col].astype(float)
gdfFinal=gdf.join(df.set_index('FID'),on='FID')
print list(gdfFinal)
gdfFinal.to_file(shpNme+'_stats.shp',driver='ESRI Shapefile')
os.chdir(shpDir)