get gdf
Browse files- app copy.py +202 -0
- app.py +152 -117
- data/lotes espacio crea_empresa.dbf +0 -0
- data/obs_df_2023_12_1.csv +2 -2
app copy.py
ADDED
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import warnings
|
3 |
+
warnings.filterwarnings('ignore')
|
4 |
+
import pandas as pd
|
5 |
+
import geopandas as gpd
|
6 |
+
from difflib import get_close_matches
|
7 |
+
import tempfile
|
8 |
+
|
9 |
+
|
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'], disabled = True)
|
36 |
+
uploaded_file2 = st.sidebar.file_uploader('Upload your csv file', type=['csv'], disabled = True)
|
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 |
+
if 'key' not in st.session_state:
|
51 |
+
st.session_state['key'] = None
|
52 |
+
|
53 |
+
if 'lote_gdf' not in st.session_state:
|
54 |
+
st.session_state['lote_gdf'] = None
|
55 |
+
|
56 |
+
if test:
|
57 |
+
|
58 |
+
lotes_gdf = gpd.read_file('data/lotes espacio crea_empresa.shp', encoding='utf-8')
|
59 |
+
obs_df_2023 = pd.read_csv('data/obs_df_2023_12_1.csv')
|
60 |
+
obs_df_2023.fillna('-', inplace=True)
|
61 |
+
obs_df_2023.Campo = obs_df_2023.Campo.astype('str')
|
62 |
+
|
63 |
+
empresa_obs = obs_df_2023.EMPRESA.unique().tolist()
|
64 |
+
# create a state variable to hold the current value of key variable
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
col1, col2,col3 = st.columns(3)
|
69 |
+
|
70 |
+
with col1:
|
71 |
+
st.header('EMPRESA')
|
72 |
+
selected_company = st.selectbox(f'Seleccione empresa', empresa_obs, index= 0)
|
73 |
+
|
74 |
+
# filter dataframe by selected company
|
75 |
+
obs_df_2023 = obs_df_2023[obs_df_2023['EMPRESA'] == selected_company]
|
76 |
+
|
77 |
+
if st.session_state['lote_gdf'] is not None:
|
78 |
+
lotes_gdf = st.session_state['lote_gdf']
|
79 |
+
else:
|
80 |
+
lotes_gdf = lotes_gdf[lotes_gdf['empresa'] == selected_company]
|
81 |
+
st.session_state['lote_gdf'] = lotes_gdf
|
82 |
+
|
83 |
+
|
84 |
+
campo_obs = obs_df_2023.Campo.unique().tolist()
|
85 |
+
campo_gdf = lotes_gdf.campo.unique().tolist()
|
86 |
+
# Initialize an empty dictionary
|
87 |
+
similar_dict = {}
|
88 |
+
N = 3
|
89 |
+
CUTOFF = 0.72
|
90 |
+
# Loop through each item in the template list
|
91 |
+
for item in campo_gdf:
|
92 |
+
# normalize the stings to lowercase and remove punctuation in campo_obs
|
93 |
+
campo_obs_norm = [str(c).lower() for c in campo_obs]
|
94 |
+
campo_obs_norm = [c.replace('.', ' ') for c in campo_obs_norm]
|
95 |
+
|
96 |
+
# Find the most similar item in df_columns list
|
97 |
+
similar_items = get_close_matches(item, campo_obs_norm, N, CUTOFF)
|
98 |
+
|
99 |
+
# get the index of the most similar item
|
100 |
+
similar_items_idx = [campo_obs_norm.index(i) for i in similar_items]
|
101 |
+
|
102 |
+
# get the most similar item in the original list
|
103 |
+
similar_items = [campo_obs[i] for i in similar_items_idx]
|
104 |
+
|
105 |
+
# If a similar item is found, add to the dictionary
|
106 |
+
if similar_items:
|
107 |
+
similar_dict[item] = similar_items[0]
|
108 |
+
else:
|
109 |
+
# If no similar item is found, set value as "no match"
|
110 |
+
similar_dict[item] = "no match"
|
111 |
+
|
112 |
+
|
113 |
+
|
114 |
+
similar_dict_df = pd.DataFrame.from_dict(similar_dict, orient='index').reset_index()
|
115 |
+
similar_dict_df.columns = ['gdf','obs']
|
116 |
+
# campo_obs = [str(c) for c in campo_obs]
|
117 |
+
|
118 |
+
|
119 |
+
# campo_obs.sort(key=str.lower)
|
120 |
+
campo_obs.insert(0, 'no match')
|
121 |
+
|
122 |
+
all_keys = similar_dict_df['gdf'].unique().tolist()
|
123 |
+
|
124 |
+
# Fields
|
125 |
+
|
126 |
+
lotes_gdf['campo_obs'] = lotes_gdf['campo'].map(similar_dict)
|
127 |
+
|
128 |
+
cutoff = 0.3
|
129 |
+
|
130 |
+
def on_click_field(*args):
|
131 |
+
# key, field, selected_value = key
|
132 |
+
def inner():
|
133 |
+
# st.session_state['key'] = key
|
134 |
+
print(args)
|
135 |
+
|
136 |
+
return inner
|
137 |
+
|
138 |
+
def show_field(key):
|
139 |
+
key, selected_value = key
|
140 |
+
lote_obs = obs_df_2023[obs_df_2023['Campo'] == selected_value]['Lote'].unique().tolist()
|
141 |
+
lote_obs.insert(0, 'no match')
|
142 |
+
with col3:
|
143 |
+
# st.header(st.session_state['key'])
|
144 |
+
st.header('Lote')
|
145 |
+
df_field = lotes_gdf[lotes_gdf['campo'] == key]
|
146 |
+
fields = df_field['lote'].unique().tolist()
|
147 |
+
|
148 |
+
for j,field in enumerate(fields):
|
149 |
+
similar_items = get_close_matches(field, lote_obs, 3, 0.70)
|
150 |
+
default = similar_items[0] if similar_items else 0
|
151 |
+
# selected_value = st.multiselect(f'{field} (.shp):', lote_obs, default=default, key='field'+str(j))
|
152 |
+
selected_value = st.selectbox(f'{field} (.shp):', lote_obs, index = lote_obs.index(default) , key='field'+str(j), on_change=on_click_field(key, field, selected_value))
|
153 |
+
|
154 |
+
lotes_gdf.loc[(lotes_gdf['campo'] == key) & (lotes_gdf['lote'] == field), 'lote_obs'] = selected_value
|
155 |
+
|
156 |
+
# st.session_state['lote_gdf'] = lotes_gdf
|
157 |
+
|
158 |
+
|
159 |
+
|
160 |
+
def on_click(key):
|
161 |
+
def inner():
|
162 |
+
st.session_state['key'] = key
|
163 |
+
show_field(key)
|
164 |
+
return inner
|
165 |
+
|
166 |
+
with col2:
|
167 |
+
st.header('Campo')
|
168 |
+
for i, key in enumerate(all_keys):
|
169 |
+
selected_value = st.selectbox(f'{key}:', campo_obs, index=campo_obs.index(similar_dict_df[similar_dict_df['gdf'] == key]['obs'].values[0]))
|
170 |
+
# selected_value = st.multiselect(f'{key} (.shp):', campo_obs, default=similar_dict_df[similar_dict_df['gdf'] == key]['obs'].values[0], key=i)
|
171 |
+
lotes_gdf.loc[(lotes_gdf['campo'] == key) & (lotes_gdf['empresa'] == selected_company), 'campo_obs'] = similar_dict_df[similar_dict_df['gdf'] == key]['obs'].values[0]
|
172 |
+
|
173 |
+
if selected_value:
|
174 |
+
similar_dict_df.loc[similar_dict_df['gdf'] == key, 'obs'] = selected_value
|
175 |
+
value = selected_value
|
176 |
+
lotes_gdf.loc[(lotes_gdf['campo'] == key) & (lotes_gdf['empresa'] == selected_company), 'campo_obs'] = selected_value
|
177 |
+
|
178 |
+
else:
|
179 |
+
# similar_dict_df.loc[similar_dict_df['gdf'] == key, 'obs'] = 'no match'
|
180 |
+
value = similar_dict_df.loc[similar_dict_df['gdf'] == key, 'obs']
|
181 |
+
lotes_gdf.loc[(lotes_gdf['campo'] == key) & (lotes_gdf['empresa'] == selected_company), 'campo_obs'] = value
|
182 |
+
|
183 |
+
st.session_state['lote_gdf'] = lotes_gdf
|
184 |
+
st.button('Show Fields', key=key, on_click=on_click([key,value]))
|
185 |
+
|
186 |
+
|
187 |
+
# st.dataframe(similar_dict_df)
|
188 |
+
# if st.button('Show Fields'):
|
189 |
+
# st.dataframe(similar_dict_df)
|
190 |
+
|
191 |
+
# add download button
|
192 |
+
|
193 |
+
|
194 |
+
|
195 |
+
|
196 |
+
st.sidebar.download_button(
|
197 |
+
label="Download GeoJSON",
|
198 |
+
data=lotes_gdf.to_json().encode('utf-8'),
|
199 |
+
file_name=f'{selected_company}.geojson',
|
200 |
+
# mime='text/csv',
|
201 |
+
mime = 'application/json',
|
202 |
+
)
|
app.py
CHANGED
@@ -47,162 +47,197 @@ if uploaded_file2 is not None:
|
|
47 |
# add Test button
|
48 |
test = True #st.sidebar.button('Test')
|
49 |
|
|
|
|
|
50 |
|
|
|
|
|
|
|
|
|
|
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
if test:
|
54 |
|
55 |
-
|
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 |
-
|
61 |
-
|
|
|
62 |
|
63 |
-
|
64 |
-
st.session_state['key'] = None
|
65 |
|
66 |
col1, col2,col3 = st.columns(3)
|
67 |
|
68 |
with col1:
|
69 |
st.header('EMPRESA')
|
70 |
-
|
|
|
|
|
|
|
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 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
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 |
-
|
89 |
-
|
90 |
|
91 |
-
|
92 |
-
|
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 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
similar_dict[item] = "no match"
|
103 |
|
|
|
|
|
104 |
|
|
|
|
|
105 |
|
106 |
-
|
107 |
-
|
108 |
-
# campo_obs = [str(c) for c in campo_obs]
|
109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
-
|
112 |
-
|
113 |
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
-
|
|
|
|
|
|
|
117 |
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
-
|
121 |
-
|
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 |
-
key, selected_value = key
|
143 |
-
lote_obs = obs_df_2023[obs_df_2023['Campo'] == selected_value]['Lote'].unique().tolist()
|
144 |
-
lote_obs.insert(0, 'no match')
|
145 |
-
with col3:
|
146 |
-
# st.header(st.session_state['key'])
|
147 |
st.header('Lote')
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
with st.form("fields_form"):
|
152 |
-
|
153 |
-
for j,field in enumerate(fields):
|
154 |
-
similar_items = get_close_matches(field, lote_obs, 3, 0.70)
|
155 |
-
default = similar_items[0] if similar_items else 'no match'
|
156 |
-
# selected_value = st.multiselect(f'{field} (.shp):', lote_obs, default=default, key='field'+str(j))
|
157 |
-
selected_value = st.selectbox(f'{field} (.shp):', lote_obs, index = lote_obs.index(default) , key='field'+str(j))
|
158 |
-
|
159 |
-
# if selected_value:
|
160 |
-
# lotes_gdf.loc[(lotes_gdf['campo'] == key) & (lotes_gdf['lote'] == field), 'lote_obs'] = selected_value
|
161 |
-
# else:
|
162 |
-
lotes_gdf.loc[(lotes_gdf['campo'] == key) & (lotes_gdf['lote'] == field), 'lote_obs'] = default
|
163 |
-
|
164 |
-
if st.form_submit_button("Submit"):
|
165 |
-
st.dataframe(lotes_gdf.drop(columns=['geometry','nombre','apellido','campo','campo_obs']))
|
166 |
|
|
|
|
|
|
|
167 |
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
show_field(key)
|
172 |
-
return inner
|
173 |
|
174 |
-
|
175 |
-
|
176 |
-
for i, key in enumerate(all_keys):
|
177 |
-
selected_value = st.selectbox(f'{key}:', campo_obs, index=campo_obs.index(similar_dict_df[similar_dict_df['gdf'] == key]['obs'].values[0]))
|
178 |
-
# selected_value = st.multiselect(f'{key} (.shp):', campo_obs, default=similar_dict_df[similar_dict_df['gdf'] == key]['obs'].values[0], key=i)
|
179 |
-
lotes_gdf.loc[(lotes_gdf['campo'] == key) & (lotes_gdf['empresa'] == selected_company), 'campo_obs'] = similar_dict_df[similar_dict_df['gdf'] == key]['obs'].values[0]
|
180 |
-
|
181 |
-
if selected_value:
|
182 |
-
similar_dict_df.loc[similar_dict_df['gdf'] == key, 'obs'] = selected_value
|
183 |
-
value = selected_value
|
184 |
-
lotes_gdf.loc[(lotes_gdf['campo'] == key) & (lotes_gdf['empresa'] == selected_company), 'campo_obs'] = selected_value
|
185 |
else:
|
186 |
-
|
187 |
-
|
188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
|
190 |
-
|
|
|
|
|
|
|
|
|
|
|
191 |
|
|
|
|
|
192 |
|
193 |
-
|
194 |
-
|
195 |
-
|
|
|
|
|
|
|
196 |
|
197 |
-
|
198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
|
200 |
-
|
201 |
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
|
|
|
47 |
# add Test button
|
48 |
test = True #st.sidebar.button('Test')
|
49 |
|
50 |
+
if 'key' not in st.session_state:
|
51 |
+
st.session_state['key'] = None
|
52 |
|
53 |
+
if 'lote_gdf' not in st.session_state:
|
54 |
+
gdf = gpd.read_file('data/lotes espacio crea_empresa.shp', encoding='utf-8')
|
55 |
+
gdf['campo_obs'] = None
|
56 |
+
gdf['lote_obs'] = None
|
57 |
+
st.session_state['lote_gdf'] = gdf
|
58 |
|
59 |
+
if 'show_field' not in st.session_state:
|
60 |
+
st.session_state['show_field'] = None
|
61 |
+
|
62 |
+
if 'selected_company' not in st.session_state:
|
63 |
+
st.session_state['selected_company'] = None
|
64 |
+
|
65 |
+
if 'selected_farm' not in st.session_state:
|
66 |
+
st.session_state['selected_farm'] = None
|
67 |
+
|
68 |
+
if 'estado_farm' not in st.session_state:
|
69 |
+
st.session_state['estado_farm'] = []
|
70 |
+
|
71 |
+
if 'estado_field' not in st.session_state:
|
72 |
+
st.session_state['estado_field'] = []
|
73 |
+
|
74 |
+
def show_text():
|
75 |
+
def inner_func():
|
76 |
+
st.write('recuerde guardar')
|
77 |
+
return inner_func
|
78 |
|
79 |
if test:
|
80 |
|
81 |
+
gdf = st.session_state['lote_gdf']
|
|
|
|
|
|
|
82 |
|
83 |
+
obs_df = pd.read_csv('data/obs_df_2023_12_1.csv')
|
84 |
+
obs_df.fillna('-', inplace=True)
|
85 |
+
obs_df.Campo = obs_df.Campo.astype('str')
|
86 |
|
87 |
+
##### Columns Section #####
|
|
|
88 |
|
89 |
col1, col2,col3 = st.columns(3)
|
90 |
|
91 |
with col1:
|
92 |
st.header('EMPRESA')
|
93 |
+
# comp_list = obs_df.EMPRESA.unique().tolist()
|
94 |
+
comp_list = gdf.empresa.unique().tolist()
|
95 |
+
selected_company = st.selectbox(f'Seleccione empresa', comp_list, index= 0)
|
96 |
+
st.session_state['selected_company'] = selected_company
|
97 |
|
|
|
|
|
|
|
98 |
|
99 |
+
with col2:
|
100 |
+
st.header('Campo')
|
101 |
+
selected_company = st.session_state['selected_company']
|
102 |
+
obs_df_comp = obs_df[obs_df['EMPRESA'] == selected_company]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
+
farm_obs_names = obs_df_comp.Campo.unique().tolist()
|
105 |
+
farm_obs_names.insert(0, 'no match')
|
106 |
|
107 |
+
farm_gdf = gdf[gdf['empresa'] == selected_company]
|
108 |
+
farm_gdf_names = farm_gdf.campo.unique().tolist()
|
109 |
|
|
|
|
|
110 |
|
111 |
+
similar_dict = {}
|
112 |
+
for item in farm_gdf_names:
|
113 |
+
# normalize the stings to lowercase and remove punctuation in campo_obs
|
114 |
+
farm_obs_norm = [str(c).lower() for c in farm_obs_names]
|
115 |
+
farm_obs_norm = [c.replace('.', ' ') for c in farm_obs_norm]
|
|
|
116 |
|
117 |
+
# Find the most similar item in df_columns list
|
118 |
+
similar_items = get_close_matches(item, farm_obs_norm, 3, 0.72)
|
119 |
|
120 |
+
# get the index of the most similar item
|
121 |
+
similar_items_idx = [farm_obs_norm.index(i) for i in similar_items]
|
122 |
|
123 |
+
# get the most similar item in the original list
|
124 |
+
similar_items = [farm_obs_names[i] for i in similar_items_idx]
|
|
|
125 |
|
126 |
+
# If a similar item is found, add to the dictionary
|
127 |
+
if similar_items:
|
128 |
+
similar_dict[item] = similar_items[0]
|
129 |
+
else:
|
130 |
+
# If no similar item is found, set value as "no match"
|
131 |
+
similar_dict[item] = "no match"
|
132 |
|
133 |
+
similar_dict_df = pd.DataFrame.from_dict(similar_dict, orient='index').reset_index()
|
134 |
+
similar_dict_df.columns = ['gdf','obs']
|
135 |
|
136 |
+
with st.form(key='farm_name'):
|
137 |
+
sel_farm_name = {}
|
138 |
+
for i, farm in enumerate(farm_gdf_names):
|
139 |
+
cll_val = farm_gdf[farm_gdf['campo'] == farm]['campo_obs'].unique()[0]
|
140 |
+
if cll_val == None:
|
141 |
+
index=farm_obs_names.index(similar_dict_df[similar_dict_df['gdf'] == farm]['obs'].values[0])
|
142 |
+
else:
|
143 |
+
index=farm_obs_names.index(cll_val)
|
144 |
|
145 |
+
selected_value = st.selectbox(f'{farm}:', \
|
146 |
+
farm_obs_names, index=index)
|
147 |
+
|
148 |
+
sel_farm_name[farm] = selected_value
|
149 |
|
150 |
+
submitted = st.form_submit_button(label='Guardar')
|
151 |
+
if selected_company not in st.session_state['estado_farm']:
|
152 |
+
st.write('sin guardar')
|
153 |
+
else:
|
154 |
+
st.write('guardado')
|
155 |
+
if submitted:
|
156 |
+
st.session_state['estado_farm'].append(selected_company)
|
157 |
+
st.write('guardado')
|
158 |
+
for key, value in sel_farm_name.items():
|
159 |
+
similar_dict_df.loc[similar_dict_df['gdf'] == key, 'obs'] = value
|
160 |
+
# farm_gdf.loc[farm_gdf['campo'] == key, 'campo_obs'] = value
|
161 |
+
gdf.loc[(gdf['campo'] == key) & (gdf['empresa']), 'campo_obs'] = value
|
162 |
|
163 |
+
st.session_state['lote_gdf'] = gdf
|
164 |
+
st.session_state['show_field'] = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
|
|
|
|
|
166 |
|
|
|
|
|
|
|
|
|
167 |
|
168 |
+
with col3:
|
169 |
+
if st.session_state['show_field']:
|
170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
st.header('Lote')
|
172 |
+
gdf = st.session_state['lote_gdf']
|
173 |
+
selected_company = st.session_state['selected_company']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
|
175 |
+
farm_list = gdf[gdf['empresa'] == selected_company]['campo'].unique().tolist()
|
176 |
+
selected_farm = st.selectbox(f'Seleccione campo', farm_list, index=0)
|
177 |
+
selected_obs_farm = gdf[(gdf['empresa'] == selected_company)&(gdf['campo'] == selected_farm)]['campo_obs'].unique()[0]
|
178 |
|
179 |
+
field_gdf_names = gdf[(gdf['empresa'] == selected_company)&(gdf['campo'] == selected_farm)]['lote'].unique().tolist()
|
180 |
+
field_obs_names = obs_df[(obs_df['EMPRESA'] == selected_company)&(obs_df['Campo'] == selected_obs_farm)]['Lote'].unique().tolist()
|
181 |
+
field_obs_names.insert(0, 'no match')
|
|
|
|
|
182 |
|
183 |
+
if selected_farm not in st.session_state['estado_field']:
|
184 |
+
st.write('sin guardar')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
else:
|
186 |
+
st.write('guardado')
|
187 |
+
|
188 |
+
similar_dict = {}
|
189 |
+
for item in field_gdf_names:
|
190 |
+
# normalize the stings to lowercase and remove punctuation in campo_obs
|
191 |
+
field_obs_norm = [str(c).lower() for c in field_obs_names]
|
192 |
+
field_obs_norm = [c.replace('.', ' ') for c in field_obs_norm]
|
193 |
+
|
194 |
+
# Find the most similar item in df_columns list
|
195 |
+
similar_items = get_close_matches(item.lower(), field_obs_norm, 3, 0.50)
|
196 |
+
|
197 |
+
# get the index of the most similar item
|
198 |
+
similar_items_idx = [field_obs_norm.index(i) for i in similar_items]
|
199 |
+
|
200 |
+
# get the most similar item in the original list
|
201 |
+
similar_items = [field_obs_names[i] for i in similar_items_idx]
|
202 |
|
203 |
+
# If a similar item is found, add to the dictionary
|
204 |
+
if similar_items:
|
205 |
+
similar_dict[item] = similar_items[0]
|
206 |
+
else:
|
207 |
+
# If no similar item is found, set value as "no match"
|
208 |
+
similar_dict[item] = "no match"
|
209 |
|
210 |
+
field_similar_dict_df = pd.DataFrame.from_dict(similar_dict, orient='index').reset_index()
|
211 |
+
field_similar_dict_df.columns = ['gdf','obs']
|
212 |
|
213 |
+
with st.form(key='field_name'):
|
214 |
+
sel_field_name = {}
|
215 |
+
for i, field in enumerate(field_gdf_names):
|
216 |
+
selected_field = st.selectbox(f'{field}:', \
|
217 |
+
field_obs_names, index=field_obs_names.index(field_similar_dict_df[field_similar_dict_df['gdf'] == field]['obs'].values[0]))
|
218 |
+
sel_field_name[field] = selected_field
|
219 |
|
220 |
+
submitted = st.form_submit_button(label='Submit')
|
221 |
+
|
222 |
+
if submitted:
|
223 |
+
st.session_state['estado_field'].append(selected_farm)
|
224 |
+
st.write('guardado')
|
225 |
+
for key, value in sel_field_name.items():
|
226 |
+
field_similar_dict_df.loc[field_similar_dict_df['gdf'] == key, 'obs'] = value
|
227 |
+
# farm_gdf.loc[farm_gdf['campo'] == key, 'campo_obs'] = value
|
228 |
+
gdf.loc[(gdf['empresa'] == selected_company) & (gdf['campo'] == selected_farm) & (gdf['lote'] == key), 'lote_obs'] = value
|
229 |
+
|
230 |
+
st.session_state['lote_gdf'] = gdf
|
231 |
+
|
232 |
+
st.session_state['show_field'] = True
|
233 |
+
|
234 |
|
|
|
235 |
|
236 |
+
##### Download Section #####
|
237 |
+
st.sidebar.download_button(
|
238 |
+
label="Download GeoJSON",
|
239 |
+
data=gdf.to_json().encode('utf-8'),
|
240 |
+
file_name=f'{selected_company}.geojson',
|
241 |
+
# mime='text/csv',
|
242 |
+
mime = 'application/json',
|
243 |
+
)
|
data/lotes espacio crea_empresa.dbf
CHANGED
Binary files a/data/lotes espacio crea_empresa.dbf and b/data/lotes espacio crea_empresa.dbf differ
|
|
data/obs_df_2023_12_1.csv
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9f032f24a2e7234e3217d1a0bbcadea6947d710b64169b89d705a2cb3353c7e4
|
3 |
+
size 12155827
|