forked from NilimaKD/lila-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInput_template.py
147 lines (128 loc) · 4.06 KB
/
Input_template.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:percent
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.14.0
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# %%
import geopandas as gpd
import pandas as pd
import numpy as np
from osgeo import gdal
# %%
def get_rooted(stem):
return "D:\\LiLa_Nagapattinam\\" + stem
def read_df_UT(stem):
return gpd.read_file(get_rooted(stem)).to_crs(epsg = 4326)
# %% [markdown]
# ### define func for creating Area_acres and area_class
# %%
def area_acres(df):
crs_utm = 32644
df = df.to_crs(crs_utm)
df["area_acres"] = (((df.geometry.area)/10**6)*247.105)
a = df["area_acres"].max()
def area_class(df):
if 5<= df["area_acres"] < 20:
return "A"
elif 20<= df["area_acres"] < 100:
return "B"
elif 100<= df["area_acres"] <= a:
return "C"
else:
return "D"
df["area_class"] =df.apply(area_class, axis=1)
df = df.to_crs(4326)
print("Total area: ",df.area_acres.sum(),"length : ",len(df))
print(df.groupby(["area_class"])["area_acres"].agg(["sum","count"]))
return (df)
# %% [markdown]
# ### define func for creating Area_hect and area_class
# %%
def area_hect(df):
crs_utm = 32644
df = df.to_crs(crs_utm)
df["area_hect"] = ((df.geometry.area)/10**4)
a = df["area_hect"].max()
def area_class(df):
if 5 <= df["area_hect"] < 20:
return "A"
elif 20<= df["area_hect"] < 100:
return "B"
elif 100<= df["area_hect"] <= a:
return "C"
else:
return "D"
df["area_class"] =df.apply(area_class, axis=1)
df = df.to_crs(4326)
print("Total area: ",df.area_hect.sum(),"length : ",len(df))
print(df.groupby(["area_class"])["area_hect"].agg(["sum","count"]))
return (df)
# %% [markdown]
# ### def fun for Calculating overlap area
# %%
def find_overlap_area(df,tag,fdf2):
crs_utm = 32644
df = df.to_crs(crs_utm)
df1 = pd.DataFrame(columns = ['olap%'+tag,'olaparea'+tag])
df1['olap%'+tag]=df1['olap%'+tag].astype('object')
df1['olaparea'+tag]=df1['olaparea'+tag].astype('object')
fdf2=fdf2.to_crs(crs_utm)
#set spatial index for data for faster processing
sindex = fdf2.sindex
for i in range(len(df)):
geometry = df.iloc[i]['geometry']
fids = list(sindex.intersection(geometry.bounds))
if fids:
olaparea = ((fdf2.iloc[fids]['geometry'].intersection(geometry)).area).sum()
count = (fdf2.iloc[fids]['geometry'].intersection(geometry)).count()
olap_perc = olaparea*100/geometry.area
olaparea = (olaparea/10**6)*247.1
else:
olaparea = 0
olap_perc = 0
df1.at[i,'olap%'+tag] = olap_perc
df1.at[i,'olaparea'+tag] = olaparea
df1.at[i,'count'+tag] = count
df = df.to_crs(4326)
return pd.concat([df,df1], axis= 1)
# %% [markdown]
# ### def fun for top 15 land
# %%
def top15(df,df1):
a = df.sort_values(by=["area_acres"],ascending = False)
b = df1.sort_values(by=["area_acres"],ascending = False)
c = gpd.pd.concat([a,b])
c = c[:15]
c = c.reset_index()
return (c)
# %% [markdown]
# ### def fun for water-runoff criteria
# %%
def water_runoff(df):
crs_utm = 32644
df = df.to_crs(crs_utm)
df["area_acres"] = (((df.geometry.area)/10**6)*247.105)
def water_runoff_class(df):
if 0<= df["Run_Tot"] < 70:
return "A"
elif 70<= df["Run_Tot"] < 200:
return "B"
elif 200<= df["Run_Tot"]:
return "C"
else:
return "D"
df["water_runoff_class"] =df.apply(water_runoff_class, axis=1)
df = df.to_crs(4326)
print("Total Area : ", df.area_acres.sum(),"Length :",len(df))
print("Total Runoff : ", df.Run_Tot.sum())
print(df.groupby(["water_runoff_class"])["area_acres"].agg(["sum","count"]))
return (df)