Last active
November 23, 2015 05:05
-
-
Save leite/90ad29931554b2be11dc to your computer and use it in GitHub Desktop.
find equal ref (from, to) in members of relation
This file contains hidden or 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
| #!/usr/bin/env lua | |
| -- | |
| -- find equal ref (from, to) in members of relation | |
| -- | |
| -- @author leite <[email protected]> | |
| -- @license GPL version 3 | |
| -- | |
| assert(arg[1], 'argument should be a file path') | |
| local io, string, table = require 'io', require 'string', require 'table' | |
| local i, last, lines, dump = 1, 1, {}, '' | |
| local handler = io.open(arg[1], 'r') | |
| function parse_chunk (chunk) | |
| local _last | |
| while true do | |
| local index, offset, context = string.find(chunk, '<relation(.-)</relation>', _last) | |
| if not index then | |
| break | |
| end | |
| _last = offset | |
| local index, __, id, ref0, role0, ref1, role1 = string.find( | |
| context, " id='([^']+)' .- ref='([^']+)' role='([fromt]+)'.- ref='([^']+)' role='([fromt]+)'" | |
| ) | |
| if index then | |
| id, ref0, ref1 = tonumber(id), tonumber(ref0), tonumber(ref1) | |
| if ref0 == ref1 then | |
| io.write(string.format("relation %d duplicated\n", id)) | |
| end | |
| end | |
| end | |
| return _last or #chunk - 500 | |
| end | |
| for line in handler:lines() do | |
| table.insert(lines, line) | |
| if i > 500 then | |
| dump = string.sub(dump, last) .. table.concat(lines, '') | |
| i, lines, last = 1, {}, parse_chunk(dump) | |
| collectgarbage() | |
| end | |
| i = i + 1 | |
| end | |
| handler:close() | |
| parse_chunk(string.sub(dump, last) .. table.concat(lines, '')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
luajit profiler ...
real 0m0.592s
user 0m0.584s
sys 0m0.008s
lua -jp=a relations.lua /home/leite/Downloads/caray.osm [...] ====== relations.lua ====== @@ 19 @@ | while true do | local index, offset, context = string.find(chunk, '<relation(.-)</relation>', _last) | 59% | if not index then | break | end | @@ 29 @@ | context, " id='([^']+)' .- ref='([^']+)' role='([fromt]+)'.- ref='([^']+)' role='([fromt]+)'" | ) | 10% | if index then | id, ref0, ref1 = tonumber(id), tonumber(ref0), tonumber(ref1) | | if ref0 == ref1 then @@ 43 @@ | | | 17% | for line in handler:lines() do | table.insert(lines, line) | if i > 500 then | dump = string.sub(dump, last) .. table.concat(lines, '') | i, lines, last = 1, {}, parse_chunk(dump) | collectgarbage() | end 8% | i = i + 1 | end | | handler:close()