Merge pull request #1 from pes2425q1-1-gei-upc/feature/process_weights

Primer commit
This commit is contained in:
Bart Zanen 2024-12-02 10:13:04 +01:00 committed by GitHub
commit 3f51181e79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,4 +1,6 @@
-- Foot profile -- Foot profile
local http = require("socket.http") -- LuaSocket for HTTP requests
local json = require("cjson")
api_version = 2 api_version = 2
@ -7,6 +9,40 @@ Sequence = require('lib/sequence')
Handlers = require("lib/way_handlers") Handlers = require("lib/way_handlers")
find_access_tag = require("lib/access").find_access_tag find_access_tag = require("lib/access").find_access_tag
function fetch_pollution_data(location)
-- Convert location userdata to a string
local location_str = tostring(location)
-- Check if the location is "(undefined,undefined)"
if location_str == "(undefined,undefined)" then
print("Location is undefined. Skipping pollution data fetch.")
return 0 -- Return 0 for undefined locations
end
-- Extract lat and lon from the location string
local lat, lon = location_str:match("%(([^,]+),([^%)]+)%)")
if lat and lon then
lat = tonumber(lat)
lon = tonumber(lon)
else
print("Invalid location format: " .. location_str)
return 0 -- Return 0 if the format is invalid
end
-- Fetch pollution data
local url = string.format("http://localhost:8008/routes/api/pollution?lat=%f&lon=%f", lat, lon)
local response, status = http.request(url)
if status == 200 then
local data = json.decode(response)
return data.pollution -- Return the pollution weight from the backend
else
print("Failed to fetch pollution data. HTTP status: " .. tostring(status))
return 0 -- Return 0 if the request fails
end
end
function setup() function setup()
local walking_speed = 5 local walking_speed = 5
return { return {
@ -155,6 +191,11 @@ function process_node(profile, node, result)
end end
end end
end end
local location = node:location()
local pollution = fetch_pollution_data(location)
print(pollution)
--result.weight = result.weight + pollution
-- check if node is a traffic light -- check if node is a traffic light
local tag = node:get_value_by_key("highway") local tag = node:get_value_by_key("highway")
@ -180,17 +221,16 @@ function process_way(profile, way, result)
-- data table for storing intermediate values during processing -- data table for storing intermediate values during processing
local data = { local data = {
-- prefetch tags -- prefetch tags
highway = way:get_value_by_key('highway'), highway = way:get_value_by_key("highway"),
bridge = way:get_value_by_key('bridge'),
route = way:get_value_by_key('route'),
leisure = way:get_value_by_key('leisure'),
man_made = way:get_value_by_key('man_made'),
railway = way:get_value_by_key('railway'),
platform = way:get_value_by_key('platform'),
amenity = way:get_value_by_key('amenity'),
public_transport = way:get_value_by_key('public_transport')
} }
-- Verificar si el objeto `way` está definido
if not way then
print("Error: way is nil.")
return
end
-- perform an quick initial check and abort if the way is -- perform an quick initial check and abort if the way is
-- obviously not routable. here we require at least one -- obviously not routable. here we require at least one
-- of the prefetched tags to be present, ie. the data table -- of the prefetched tags to be present, ie. the data table
@ -238,7 +278,7 @@ function process_way(profile, way, result)
WayHandlers.names, WayHandlers.names,
-- set weight properties of the way -- set weight properties of the way
WayHandlers.weights WayHandlers.weights,
} }
WayHandlers.run(profile, way, result, data, handlers) WayHandlers.run(profile, way, result, data, handlers)
@ -267,4 +307,4 @@ return {
process_way = process_way, process_way = process_way,
process_node = process_node, process_node = process_node,
process_turn = process_turn process_turn = process_turn
} }