Fer-geo commited on
Commit
8e56908
·
1 Parent(s): 5aa5e3a
app.py CHANGED
@@ -10,5 +10,188 @@ import tempfile
10
  from io import BytesIO
11
 
12
 
13
- x = st.slider('Select a value')
14
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  from io import BytesIO
11
 
12
 
13
+
14
+ def convert_to_gdf(uploaded_file):
15
+ # Read the file using BytesIO
16
+ file_buffer = BytesIO(uploaded_file.read())
17
+
18
+ # Detect file type and load accordingly
19
+ if uploaded_file.name.endswith('.shp'):
20
+ gdf = gpd.read_file(file_buffer)
21
+ elif uploaded_file.name.endswith(('.geojson', '.json')):
22
+ gdf = gpd.read_file(file_buffer, driver='GeoJSON')
23
+ else:
24
+ raise ValueError("Unsupported file format")
25
+
26
+ return gdf
27
+
28
+ # add logo D:\Terradot\repos\crea-carbon-model\app\logo.jpg
29
+
30
+ st.sidebar.image('logo.jpg', width=200)
31
+ st.sidebar.title('Proyecto Crea')
32
+ st.sidebar.write('Solo uso interno')
33
+ # add sidebar with 2 upload buttons
34
+ st.sidebar.header('Upload Files')
35
+ uploaded_file = st.sidebar.file_uploader('Upload your shapefile', type=['shp', 'geojson', 'json'])
36
+ uploaded_file2 = st.sidebar.file_uploader('Upload your csv file', type=['csv'])
37
+
38
+ if uploaded_file is not None:
39
+ lotes_gdf = convert_to_gdf(uploaded_file)
40
+ st.write(lotes_gdf)
41
+
42
+ if uploaded_file2 is not None:
43
+ # read csv and create dataframe
44
+ obs_df_2023 = pd.read_csv(uploaded_file2)
45
+
46
+
47
+ # add Test button
48
+ test = True #st.sidebar.button('Test')
49
+
50
+
51
+
52
+
53
+ if test:
54
+
55
+ lotes_gdf = gpd.read_file('data/lotes espacio crea_empresa.shp', encoding='utf-8')
56
+ obs_df_2023 = pd.read_csv('data/obs_df_2023_12_1.csv')
57
+ obs_df_2023.fillna('-', inplace=True)
58
+ obs_df_2023.Campo = obs_df_2023.Campo.astype('str')
59
+
60
+ empresa_obs = obs_df_2023.EMPRESA.unique().tolist()
61
+ # create a state variable to hold the current value of key variable
62
+
63
+ if 'key' not in st.session_state:
64
+ st.session_state['key'] = None
65
+
66
+ col1, col2,col3 = st.columns(3)
67
+
68
+ with col1:
69
+ st.header('EMPRESA')
70
+ selected_company = st.selectbox(f'Seleccione empresa', empresa_obs, index= 0)
71
+
72
+ # filter dataframe by selected company
73
+ obs_df_2023 = obs_df_2023[obs_df_2023['EMPRESA'] == selected_company]
74
+ lotes_gdf = lotes_gdf[lotes_gdf['empresa'] == selected_company]
75
+
76
+ campo_obs = obs_df_2023.Campo.unique().tolist()
77
+ campo_gdf = lotes_gdf.campo.unique().tolist()
78
+ # Initialize an empty dictionary
79
+ similar_dict = {}
80
+ N = 3
81
+ CUTOFF = 0.72
82
+ # Loop through each item in the template list
83
+ for item in campo_gdf:
84
+ # normalize the stings to lowercase and remove punctuation in campo_obs
85
+ campo_obs_norm = [str(c).lower() for c in campo_obs]
86
+ campo_obs_norm = [c.replace('.', ' ') for c in campo_obs_norm]
87
+
88
+ # Find the most similar item in df_columns list
89
+ similar_items = get_close_matches(item, campo_obs_norm, N, CUTOFF)
90
+
91
+ # get the index of the most similar item
92
+ similar_items_idx = [campo_obs_norm.index(i) for i in similar_items]
93
+
94
+ # get the most similar item in the original list
95
+ similar_items = [campo_obs[i] for i in similar_items_idx]
96
+
97
+ # If a similar item is found, add to the dictionary
98
+ if similar_items:
99
+ similar_dict[item] = similar_items[0]
100
+ else:
101
+ # If no similar item is found, set value as "no match"
102
+ similar_dict[item] = "no match"
103
+
104
+
105
+
106
+ similar_dict_df = pd.DataFrame.from_dict(similar_dict, orient='index').reset_index()
107
+ similar_dict_df.columns = ['gdf','obs']
108
+ # campo_obs = [str(c) for c in campo_obs]
109
+
110
+
111
+ # campo_obs.sort(key=str.lower)
112
+ campo_obs.insert(0, 'no match')
113
+
114
+ all_keys = similar_dict_df['gdf'].unique().tolist()
115
+
116
+ # Fields
117
+
118
+ lotes_gdf['campo_obs'] = lotes_gdf['campo'].map(similar_dict)
119
+
120
+ cutoff = 0.3
121
+ for campo , df in lotes_gdf.groupby('campo'):
122
+ for i, row in df.iterrows():
123
+ c_obs = obs_df_2023.loc[obs_df_2023.Campo == row.campo_obs]
124
+ lote_obs = c_obs.Lote.unique()
125
+ # normalize the stings to lowercase and remove punctuation
126
+ lote_obs_norm = [str(c).lower() for c in lote_obs]
127
+ lote_obs_norm = [c.replace('.', ' ') for c in lote_obs_norm]
128
+ similar_items = get_close_matches(row.lote, lote_obs_norm, N, cutoff)
129
+ # get the index of the most similar item
130
+ similar_items_idx = [lote_obs_norm.index(i) for i in similar_items]
131
+
132
+ # get the most similar item in the original list
133
+ similar_items = [lote_obs[i] for i in similar_items_idx]
134
+
135
+ if similar_items:
136
+ lotes_gdf.loc[i, 'lote_obs'] = similar_items[0]
137
+ else:
138
+ lotes_gdf.loc[i, 'lote_obs'] = "no match"
139
+
140
+
141
+ def show_field(key):
142
+ with col3:
143
+ # st.header(st.session_state['key'])
144
+ st.header('Lote')
145
+ df_field = lotes_gdf[lotes_gdf['campo'] == st.session_state['key']]
146
+ fields = df_field['lote'].unique().tolist()
147
+ fields_obs = df_field['lote_obs'].unique().tolist()
148
+ with st.form("fields_form"):
149
+ for j,field in enumerate(fields):
150
+ default = df_field[df_field['lote']==field]['lote_obs'].values[0]
151
+ selected_value = st.multiselect(f'{field} (.shp):', fields_obs, default=default, key='field'+str(j))
152
+
153
+ if st.form_submit_button("Submit"):
154
+ for j,field in enumerate(fields):
155
+ if selected_value:
156
+ lotes_gdf.loc[(lotes_gdf['campo'] == st.session_state['key']) & (lotes_gdf['lote'] == field), 'lote_obs'] = selected_value[j]
157
+ else:
158
+ lotes_gdf.loc[(lotes_gdf['campo'] == st.session_state['key']) & (lotes_gdf['lote'] == field), 'lote_obs'] = 'no match'
159
+
160
+ # st.dataframe(df.drop(columns=['geometry','nombre','apellido','campo','campo_obs']))
161
+
162
+
163
+ def on_click(key):
164
+ def inner():
165
+ st.session_state['key'] = key
166
+ show_field(key)
167
+ return inner
168
+
169
+ with col2:
170
+ st.header('Campo')
171
+ for i, key in enumerate(all_keys):
172
+ # selected_value = st.selectbox(f'{key}:', campo_obs, index=campo_obs.index(similar_dict_df[similar_dict_df['gdf'] == key]['obs'].values[0]))
173
+ selected_value = st.multiselect(f'{key} (.shp):', campo_obs, default=similar_dict_df[similar_dict_df['gdf'] == key]['obs'].values[0], key=i)
174
+ if selected_value:
175
+ similar_dict_df.loc[similar_dict_df['gdf'] == key, 'obs'] = selected_value[0]
176
+ else:
177
+ similar_dict_df.loc[similar_dict_df['gdf'] == key, 'obs'] = 'no match'
178
+
179
+ st.button('Show Fields', key=key, on_click=on_click(key))
180
+
181
+
182
+ # st.dataframe(similar_dict_df)
183
+ # if st.button('Show Fields'):
184
+ # st.dataframe(similar_dict_df)
185
+
186
+ # add download button
187
+
188
+
189
+
190
+
191
+ st.sidebar.download_button(
192
+ label="Download GeoJSON",
193
+ data=lotes_gdf.to_json().encode('utf-8'),
194
+ file_name='lotes.geojson',
195
+ # mime='text/csv',
196
+ mime = 'application/json',
197
+ )
data/lotes espacio crea_empresa.dbf ADDED
Binary file (342 kB). View file
 
data/lotes espacio crea_empresa.prj ADDED
@@ -0,0 +1 @@
 
 
1
+ GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]
data/lotes espacio crea_empresa.shp ADDED
Binary file (704 kB). View file
 
data/lotes espacio crea_empresa.shx ADDED
Binary file (6.92 kB). View file
 
logo.jpg ADDED