-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle conditional access tags for date ranges #6984
base: master
Are you sure you want to change the base?
Changes from 5 commits
f8f50df
7bd6e87
063dc20
323e584
a55b27f
bf2ca0a
8bcb9c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
-- Handle conditional access tags as described in the OSM wiki: | ||
-- https://wiki.openstreetmap.org/wiki/Conditional_restrictions | ||
|
||
-- Note that we only handle conditional tags for a date range, | ||
-- meant to be used for temporary restrictions, typically due to | ||
-- construction. We also require the date range to be at least a | ||
-- week long | ||
|
||
|
||
|
||
ConditionalAccess = {} | ||
|
||
|
||
local function parse_conditional_access(way, key) | ||
local conditional = way:get_value_by_key(key .. ':conditional') | ||
if not conditional then | ||
return nil | ||
end | ||
|
||
-- Examples of conditional tags: "no @ (2018 May 22-2018 Oct 7) | ||
ivarbrek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
-- or "no @ 2018 Jun 01-2018 Jul 23" | ||
local condition, time_range = conditional:match("([^@]+)@(.+)") | ||
if not condition or not time_range then | ||
return nil | ||
end | ||
|
||
local start_date_str, end_date_str = time_range:match("([^-]+)-(.+)") | ||
if not start_date_str or not end_date_str then | ||
return nil | ||
end | ||
|
||
local function parse_date(date_str) | ||
local year, month, day = date_str:match("(%d+)%s+(%a+)%s+(%d+)") | ||
|
||
local month_names = { | ||
Jan = 1, Feb = 2, Mar = 3, Apr = 4, May = 5, Jun = 6, | ||
Jul = 7, Aug = 8, Sep = 9, Oct = 10, Nov = 11, Dec = 12 | ||
} | ||
month = month_names[month] | ||
if not year or not month or not day then | ||
return nil | ||
end | ||
|
||
local numericYear = tonumber(year) | ||
local numericDay = tonumber(day) | ||
if numericYear and numericDay then | ||
return os.time({ year = numericYear, month = month, day = numericDay }) | ||
else | ||
return nil | ||
end | ||
end | ||
|
||
local start_date = parse_date(start_date_str) | ||
local end_date = parse_date(end_date_str) | ||
local current_date = os.time() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This probably needs some explanation that the extraction result depends on the date the extraction is run. Also, the date is not checked once but for each case if I am not mistaken. A little of folks run data updates over night and this may lead to edge cases. Consider pulling the date once at the start of extraction. Also, consider implementing an override, ie. supporting the use case of extracting data ahead of time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good points 👍 Note that the current suggestion excludes date ranges that span less than a week, partly to avoid weird cases like this. But that choice can be discussed I think. Anyways, I'll see if I manage to implement an overrride option in a reasonable way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found a relevant example here, will try to implement something similar 👍 |
||
|
||
-- Require start and end date to be more than a week apart | ||
if not start_date or not end_date or end_date - start_date < 60 * 60 * 24 * 7 then | ||
return nil | ||
end | ||
|
||
if current_date >= start_date and current_date <= end_date then | ||
return condition:match("%S+") | ||
else | ||
return nil | ||
end | ||
end | ||
|
||
function ConditionalAccess.parse_by_set(way, keys) | ||
for i, key in ipairs(keys) do | ||
local conditional = parse_conditional_access(way, key) | ||
if conditional then | ||
return conditional | ||
end | ||
end | ||
return nil | ||
end | ||
|
||
return ConditionalAccess |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this test break in 24 years? 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep 😄 I can extend the ranges so they don't break that soon, but I don't see any simple way to get completely rid of this problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, this can be solved doing something similar to this. I'll try to do that.