UjjwalKGupta commited on
Commit
cc028f1
1 Parent(s): 1f25e7b

Update CRS

Browse files
Files changed (1) hide show
  1. app.py +56 -3
app.py CHANGED
@@ -78,6 +78,58 @@ with st.expander("EVI/EVI2 Parameters"):
78
  ############################################
79
  # Functions
80
  ############################################
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  def daterange_str_to_dates(daterange_str):
82
  start_date, end_date = daterange_str.split("-")
83
  start_date = pd.to_datetime(start_date)
@@ -101,10 +153,10 @@ def shape_3d_to_2d(shape):
101
  else:
102
  return shape
103
 
104
-
105
  def preprocess_gdf(gdf):
106
- gdf = gdf.to_crs(epsg=7761) # epsg for Gujarat
107
  gdf["geometry"] = gdf["geometry"].apply(shape_3d_to_2d)
 
 
108
  gdf["geometry"] = gdf.buffer(0) # Fixes some invalid geometries
109
  return gdf
110
 
@@ -368,10 +420,11 @@ if isinstance(file_url, str):
368
 
369
  buffer = st.number_input("Buffer (m)", value=50, min_value=0, step=1)
370
 
371
- input_gdf = preprocess_gdf(gpd.read_file(file_url))
372
  if len(input_gdf) > 1:
373
  st.warning(f"Only the first polygon in the KML will be processed; all other geometries will be ignored.")
374
 
 
375
  # input_geometry_idx = st.selectbox("Select the geometry", input_gdf.index, format_func=format_fn)
376
 
377
  for i in range(len(input_gdf)):
 
78
  ############################################
79
  # Functions
80
  ############################################
81
+
82
+ # Function of find best suited statewise EPSG code
83
+ def find_best_epsg(geometry):
84
+ if geometry.geom_type == 'Polygon':
85
+ centroid = geometry.centroid
86
+ else:
87
+ st.error("Geometry is not Polygon !!!")
88
+ st.stop()
89
+ common_epsg_codes = [7756, #Andhra Pradesh
90
+ 7757, #Arunachal Pradesh
91
+ 7758, #Assam
92
+ 7759, #Bihar
93
+ 7760, #Delhi
94
+ 7761, #Gujarat
95
+ 7762, #Haryana
96
+ 7763, #HimachalPradesh
97
+ 7764, #JammuAndKashmir
98
+ 7765, #Jharkhand
99
+ 7766, #MadhyaPradesh
100
+ 7767, #Maharastra
101
+ 7768, #Manipur
102
+ 7769, #Meghalaya
103
+ 7770, #Nagaland
104
+ 7772, #Orissa
105
+ 7773, #Punjab
106
+ 7774, #Rajasthan
107
+ 7775, #UttarPradesh
108
+ 7776, #Uttaranchal
109
+ 7777, #A&N
110
+ 7778, #Chattisgarh
111
+ 7779, #Goa
112
+ 7780, #Karnataka
113
+ 7781, #Kerala
114
+ 7782, #Lakshadweep
115
+ 7783, #Mizoram
116
+ 7784, #Sikkim
117
+ 7785, #TamilNadu
118
+ 7786, #Tripura
119
+ 7787, #WestBengal
120
+ 7771, #NE India
121
+ 7755, #India
122
+ ]
123
+
124
+
125
+ for epsg in common_epsg_codes:
126
+ crs = pyproj.CRS.from_epsg(epsg)
127
+ area_of_use = crs.area_of_use.bounds # Get the bounding box of the area of use
128
+
129
+ #check if centroid of polygon lies in teh bounds of the crs
130
+ if (area_of_use[0] <= centroid.x <= area_of_use[2]) and (area_of_use[1] <= centroid.y <= area_of_use[3]):
131
+ return epsg # Return the best suitable EPSG code
132
+
133
  def daterange_str_to_dates(daterange_str):
134
  start_date, end_date = daterange_str.split("-")
135
  start_date = pd.to_datetime(start_date)
 
153
  else:
154
  return shape
155
 
 
156
  def preprocess_gdf(gdf):
 
157
  gdf["geometry"] = gdf["geometry"].apply(shape_3d_to_2d)
158
+ epsg_code = find_best_epsg(gdf["geometry"])
159
+ gdf = gdf.to_crs(epsg=int(epsg_code)) #Find best EPSG code
160
  gdf["geometry"] = gdf.buffer(0) # Fixes some invalid geometries
161
  return gdf
162
 
 
420
 
421
  buffer = st.number_input("Buffer (m)", value=50, min_value=0, step=1)
422
 
423
+
424
  if len(input_gdf) > 1:
425
  st.warning(f"Only the first polygon in the KML will be processed; all other geometries will be ignored.")
426
 
427
+ input_gdf = preprocess_gdf(gpd.read_file(file_url))
428
  # input_geometry_idx = st.selectbox("Select the geometry", input_gdf.index, format_func=format_fn)
429
 
430
  for i in range(len(input_gdf)):