Skip to content

Commit

Permalink
Adicionando integração com o .json
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasLopesFrazao committed Oct 13, 2023
1 parent a85c38a commit 5c80efe
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 56 deletions.
32 changes: 31 additions & 1 deletion api/gerarGrafos.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,41 @@
"# Salve a rede para um arquivo .graphml\n",
"ox.save_graphml(G_DF, filepath=\"private_false_distrito_federal.graphml\")"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import osmnx as ox\n",
"\n",
"# Baixe a rede de ruas para todo o Distrito Federal\n",
"place = \"Distrito Federal, Brazil\"\n",
"G_DF = ox.graph.graph_from_place(place, network_type='drive', simplify=False, retain_all=False, truncate_by_edge=False, which_result=None, buffer_dist=None, clean_periphery=None, custom_filter=None)\n",
"\n",
"# Salve a rede para um arquivo .graphml\n",
"ox.save_graphml(G_DF, filepath=\"drive_true_distrito_federal.graphml\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
},
"orig_nbformat": 4
},
Expand Down
190 changes: 154 additions & 36 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,17 @@
import osmnx as ox
import networkx as nx
import pickle
from typing import List
import roteamento as rt

app = FastAPI()

# Definição do modelo Pydantic para entrada
class Coordinates(BaseModel):
lat_origin: float
lon_origin: float

class BusStop(BaseModel):
lat: float
lon: float

# Modelo Pydantic atualizado
class InputData(BaseModel):
current_location: BusStop
paradas: List[BusStop]

destiny_location: BusStop
# Carregando o grafo das paradas de ônibus
with open('grafoDirecional.grafo', 'rb') as f:
bus_stops_graph = pickle.load(f)
Expand All @@ -44,43 +36,169 @@ async def root():

@app.post('/calculate_route', tags=['Route'], summary='Calcula as rotas de baldeação e veicular')
async def get_route(data: InputData):
all_stops = [
{
"id_stop": 7907,
"lat": -15.963291999999997,
"lon": -48.022833000000006,
"linha": "0.205"
},
{
"id_stop": 7908,
"lat": -15.959593999999997,
"lon": -48.026028,
"linha": "0.205"
},
{
"id_stop": 6350,
"lat": -15.95956741758501,
"lon": -48.02601882854514,
"linha": "0.205"
},
{
"id_stop": 3809,
"lat": -15.956576488628638,
"lon": -48.02855193528977,
"linha": "0.255"
},
{
"id_stop": 3810,
"lat": -15.953939223131423,
"lon": -48.030795914341546,
"linha": "0.205"
},
{
"id_stop": 6354,
"lat": -15.95117649533479,
"lon": -48.03315367341592,
"linha": "0.205"
},
{
"id_stop": 7909,
"lat": -15.948016999999997,
"lon": -48.03582800000001,
"linha": "0.205"
},
{
"id_stop": 3811,
"lat": -15.9480060886309,
"lon": -48.03584505006028,
"linha": "0.205"
},
{
"id_stop": 7910,
"lat": -15.943535999999998,
"lon": -48.039344,
"linha": "0.205"
},
{
"id_stop": 3813,
"lat": -15.938393849213748,
"lon": -48.04209305412461,
"linha": "0.205"
},
{
"id_stop": 7911,
"lat": -15.938388999999995,
"lon": -48.042086,
"linha": "0.205"
},
{
"id_stop": 7912,
"lat": -15.933691999999997,
"lon": -48.0437,
"linha": "0.255"
},
{
"id_stop": 3814,
"lat": -15.9336829394337,
"lon": -48.043679856954,
"linha": "0.255"
},
{
"id_stop": 7913,
"lat": -15.925732999999996,
"lon": -48.046239,
"linha": "0.255"
},
{
"id_stop": 3816,
"lat": -15.920460708461938,
"lon": -48.04791146325488,
"linha": "0.255"
},
{
"id_stop": 7914,
"lat": -15.920452999999997,
"lon": -48.047928,
"linha": "0.255"
},
{
"id_stop": 7915,
"lat": -15.917502999999998,
"lon": -48.048872,
"linha": "0.205"
},
{
"id_stop": 3817,
"lat": -15.917478165997663,
"lon": -48.048870808929316,
"linha": "0.205"
},
{
"id_stop": 7917,
"lat": -15.908972,
"lon": -48.051611,
"linha": "0.255"
},
{
"id_stop": 7922,
"lat": -15.891674999999996,
"lon": -48.057164,
"linha": "0.205"
},
{
"id_stop": 7923,
"lat": -15.891674999999996,
"lon": -48.057164,
"linha": "0.205"
},
{
"id_stop": 5391,
"lat": -15.874655767236849,
"lon": -48.0394544647222,
"linha": "0.205"
},
{
"id_stop": 4975,
"lat": -15.872196498773105,
"lon": -48.02948710262496,
"linha": "0.205"
},
{
"id_stop": 4976,
"lat": -15.867699286908332,
"lon": -48.031872129100485,
"linha": "0.205"
}
]

# 1. Rota de Baldeação (Caminhada)
start_point = (data.current_location.lat, data.current_location.lon)
# Pegue a primeira parada de ônibus da lista
first_bus_stop = data.paradas[0]
end_point = (first_bus_stop.lat, first_bus_stop.lon)

first_stop = all_stops[0]
end_point = (first_stop["lat"], first_stop["lon"])

# Baixar o mapa da área de interesse para cálculo da rota de caminhada
G = ox.graph_from_point(start_point, dist=2000, network_type='walk')

## --------------------------------------------------------------------
# Mapear start_point e end_point para os nós no Grafo
## Ler o grafo de um arquivo usando pickle
# with open('grafoDirecional.grafo', 'rb') as f:
# G_Direcional = pickle.load(f)

# raio = 0.007 # Raio desejado em metros

# nos_origem = rt.calculaNosProximos(G_Direcional, start_point[1], start_point[0], raio)
# no_destino = rt.calculaNoProximo(G_Direcional, end_point[1], end_point[0])

# paradas, linhas_usadas = rt.calculaRotaOtima(G_Direcional, nos_origem, no_destino, raio)

# paradas_info_json = rt.paradasELinhasToJson(G_Direcional, paradas, linhas_usadas)

## ----------------------------------------------------------------------

# Calcular a rota de baldeação
baldeacao_coords = calculate_route(start_point, end_point, G)

# 2. Rota Veicular
G_DF = ox.load_graphml("private_false_distrito_federal.graphml")

# Começando da primeira parada de ônibus e seguindo para as outras paradas
all_stops = data.paradas
G_DF = ox.load_graphml("drive_true_distrito_federal.graphml")

nearest_nodes = [ox.distance.nearest_nodes(
G_DF, stop.lon, stop.lat) for stop in all_stops]
nearest_nodes = [ox.distance.nearest_nodes(G_DF, stop["lon"], stop["lat"]) for stop in all_stops]

all_routes = []
for i in range(len(nearest_nodes) - 1):
Expand All @@ -95,4 +213,4 @@ async def get_route(data: InputData):
return {
"baldeacao": baldeacao_coords,
"rota_veicular": veicular_coords
}
}
15 changes: 9 additions & 6 deletions api/teste.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@
API_URL = "http://127.0.0.1:8000/calculate_route"

# Coordenadas de origem (Você pode alterá-las conforme necessário)
lat_origin = -15.989377
lon_origin = -48.044908
lat_origin = -15.962060
lon_origin = -48.023865

lat_destiny = -15.987927
lon_destiny = -48.044675

# Definindo as paradas no body da requisição
body = {
"current_location": {
"lat": lat_origin,
"lon": lon_origin
},
"paradas": [
{"lat": -15.987927, "lon": -48.044693},
{"lat": -15.842100, "lon": -48.045521}
]
"destiny_location": {
"lat": lat_destiny,
"lon": lon_destiny
},
}

# Fazendo a requisição para a API
Expand Down
Loading

0 comments on commit 5c80efe

Please sign in to comment.