File size: 2,947 Bytes
1de816b
 
f2b47af
 
6da159e
13b74f6
f2b47af
6da159e
546ee3a
8f313d5
53273f3
 
6da159e
 
13b74f6
 
 
f2b47af
13b74f6
f2b47af
 
 
13b74f6
f2b47af
13b74f6
 
f2b47af
6da159e
1de816b
13b74f6
 
1de816b
13b74f6
7f16220
 
 
6da159e
4927a3a
7f16220
 
 
 
 
 
6da159e
7f16220
6973350
8e30173
 
e37fc77
7f16220
1de816b
 
fb84159
ba12014
f9ac11e
fb84159
7f16220
f1d5eef
1de816b
e37fc77
6700061
8e30173
1de816b
 
 
d88e63d
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
import gradio as gr
import pandas as pd
from scipy.spatial.distance import cdist

# Read the base DataFrame
base_df = pd.read_excel('app_david.xlsx')

selected_column = {
    "Proximidade Estabelecimentos": 'v_estab',
    "Concentração de Comércio": 'v_ccom',
    "Concentração de Linhas de Ônibus": 'v_linhas',
    "Proximidade Rua dos Andradas": 'v_ruas'
}

def get_value_at_closest_coordinates(base_df, x_value, y_value, selected_column):
    # Convert 'X' and 'Y' columns of the base DataFrame to a 2D array
    xy_coords = base_df[['X', 'Y']].values
    
    # Calculate Euclidean distances between the input coordinates and base DataFrame coordinates
    distances = cdist([(x_value, y_value)], xy_coords)
    
    # Find the index of the minimum distance
    min_distance_index = distances.argmin(axis=1)
    
    # Get the values from the selected column at the index of the minimum distance
    values_at_closest_coords = base_df.loc[min_distance_index, selected_column].values
    
    return values_at_closest_coords[0]  # Return the value as a scalar, not a single-element array

def aplicar_funcao(planilha, coluna):
    # Read the uploaded DataFrame with the new coordinates
    df = pd.read_excel(planilha.name)
    
    # Create a new DataFrame for the output
    output_df = pd.DataFrame(columns=['X', 'Y', coluna])
    
    # Calculate the closest values using the base DataFrame for each row
    for index, row in df.iterrows():
        selected_value = get_value_at_closest_coordinates(base_df, row['X'], row['Y'], selected_column[coluna])
        output_df.loc[index] = [row['X'], row['Y'], selected_value]
    
    # Save the output DataFrame as an XLS file
    output_file = 'output.xlsx'
    output_df.to_excel(output_file, index=False)
    
    return output_file

theme=gr.themes.Base(primary_hue="teal",secondary_hue="teal",
                         neutral_hue="gray")

# Interface do Gradio com output como arquivo XLS
interface = gr.Interface(
    fn=aplicar_funcao,
    inputs=[
        gr.inputs.File(label="Escolha a planilha XLS ou XLSX", type="file"),
        gr.inputs.Dropdown(list(selected_column.keys()), label="Escolha uma variável", default="Proximidade Estabelecimentos")
    ],
    outputs=gr.outputs.File(label="Baixar planilha XLS"),
    live=False,
    capture_session=True,
    theme=theme,
    title="Aplicativo de Atribuição de Variáveis",
    description="Faça o upload de uma planilha XLS ou XLSX e escolha uma variável para aplicar nela. A planilha deve conter as colunas X e Y com os valores de coordenadas em WGS 84 (World Geodetic System 1984) - 'EPSG:4326’ - expressos em graus decimais de latitude e longitude. Para um exemplo de estrutura de planilha, você pode baixar <a href='https://huggingface.co/spaces/fschwartzer/App_V_centro/resolve/main/teste.xlsx' download='teste.xlsx'>aqui</a>.")

# Executar o aplicativo Gradio
if __name__ == "__main__":
    interface.launch()