-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Open
ivarbrek
wants to merge
7
commits into
Project-OSRM:master
Choose a base branch
from
ivarbrek:ivarbrek/conditional_access
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f8f50df
Handle conditional access tags for time ranges
ivarbrek 7bd6e87
Add changelog entry
ivarbrek 063dc20
Add a few test cases
ivarbrek 323e584
Fix tests
ivarbrek a55b27f
Add newline at end of file
ivarbrek bf2ca0a
Rename access_conditional to conditional_access
ivarbrek 8bcb9c2
Improve readability of example conditional tags
ivarbrek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
-- 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)" 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() | ||
|
||
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.