|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# regexp.py {NewTag} {Formula} |
| 5 | +# |
| 6 | +# Made from regexp.py ( Copyright 2011 Hind <foxhind@gmail.com> ) |
| 7 | +# by OverQuantum, 2014-12-06 - 2014-12-09 |
| 8 | +# |
| 9 | +# Formula = [ <fix string> ] [ #tag=<tag># ] [ <fix string> ] [ #p=<param># ] [ <fix string> ] |
| 10 | +# <tag> - tag name (key) of this object |
| 11 | +# <param> = lat / lon / uid / ver / user / chg / nodes / ways / rels |
| 12 | +# uid - user id; nodes - valid for ways and relations; lat and lon works only for nodes |
| 13 | +# note: <fix string> should contain "" to have " in result (due to command line interface) |
| 14 | +# |
| 15 | +# example: regexp.py "name" "Height #tag=ele#" |
| 16 | +# |
| 17 | +# This program is free software; you can redistribute it and/or modify |
| 18 | +# it under the terms of the GNU General Public License as published by |
| 19 | +# the Free Software Foundation; either version 2 of the License, or |
| 20 | +# (at your option) any later version. |
| 21 | +# |
| 22 | +# This program is distributed in the hope that it will be useful, |
| 23 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | +# GNU General Public License for more details. |
| 26 | +# |
| 27 | +# You should have received a copy of the GNU General Public License |
| 28 | +# along with this program; if not, write to the Free Software |
| 29 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 30 | +# MA 02110-1301, USA. |
| 31 | + |
| 32 | +import sys |
| 33 | +import re |
| 34 | +import copy |
| 35 | +import locale |
| 36 | +from OsmData import OsmData, ACTION, MODIFY, TAG, LAT, LON, UID, VERSION, USER, CHANGESET, REF, NODES, WAYS, RELATIONS |
| 37 | + |
| 38 | +if sys.version_info[0] < 3: |
| 39 | + reload(sys) |
| 40 | + sys.setdefaultencoding("utf-8") # a hack to support UTF-8 |
| 41 | + |
| 42 | +def process(objects, newtag, formula, objtype): |
| 43 | + num = 0 |
| 44 | + tagmatches=re.findall('#tag=[^#]*#',formula) #find tags |
| 45 | + parammatches=re.findall('#p=[^#]*#',formula) #find params |
| 46 | + for objid in objects.keys(): |
| 47 | + prevvalue='' |
| 48 | + if newtag in objects[objid][TAG]: |
| 49 | + prevvalue=objects[objid][TAG][newtag] |
| 50 | + result=formula |
| 51 | + for match in tagmatches: |
| 52 | + tagname=re.search('#tag=(.*)#',match).group(1) #extract tag name from match |
| 53 | + repl='' |
| 54 | + if tagname in objects[objid][TAG]: |
| 55 | + repl=objects[objid][TAG][tagname] #get tag value |
| 56 | + result=re.sub(match,repl,result) #replace match with tag value |
| 57 | + for match in parammatches: |
| 58 | + paramname=re.search('#p=(.*)#',match).group(1) #extract param name from match |
| 59 | + repl='' |
| 60 | + #switch on paramname |
| 61 | + if paramname == 'lat' and LAT in objects[objid]: |
| 62 | + repl=str(format(objects[objid][LAT])) |
| 63 | + elif paramname == 'lon' and LON in objects[objid]: |
| 64 | + repl=str(format(objects[objid][LON])) |
| 65 | + elif paramname == 'uid' and UID in objects[objid]: |
| 66 | + repl=str(objects[objid][UID]) |
| 67 | + elif paramname == 'ver' and VERSION in objects[objid]: |
| 68 | + repl=str(objects[objid][VERSION]) |
| 69 | + elif paramname == 'user' and USER in objects[objid]: |
| 70 | + repl=str(objects[objid][USER]) |
| 71 | + elif paramname == 'chg' and CHANGESET in objects[objid]: |
| 72 | + repl=str(objects[objid][CHANGESET]) |
| 73 | + elif paramname == 'nodes' and REF in objects[objid]: |
| 74 | + if objtype==2: |
| 75 | + repl=str(len(objects[objid][REF])) |
| 76 | + elif objtype==3: |
| 77 | + repl=str(len(objects[objid][REF][NODES])) |
| 78 | + elif paramname == 'ways' and objtype==3 and REF in objects[objid]: |
| 79 | + repl=str(len(objects[objid][REF][WAYS])) |
| 80 | + elif paramname == 'rels' and objtype==3 and REF in objects[objid]: |
| 81 | + repl=str(len(objects[objid][REF][RELATIONS])) |
| 82 | + result=re.sub(match,repl,result) #replace match with param value |
| 83 | + if result != prevvalue: #only if changed |
| 84 | + objects[objid][TAG][newtag] = result |
| 85 | + objects[objid][ACTION] = MODIFY |
| 86 | + num += 1 |
| 87 | + return num |
| 88 | + |
| 89 | +def main(): |
| 90 | + if len(sys.argv) != 3: |
| 91 | + return 0 |
| 92 | + rdata = OsmData() |
| 93 | + data = OsmData() |
| 94 | + rdata.read(sys.stdin) |
| 95 | + data.read(sys.stdin) |
| 96 | + |
| 97 | + codeset = locale.getdefaultlocale()[1] |
| 98 | + newtag = unicode(sys.argv[1], codeset) |
| 99 | + formula = unicode(sys.argv[2], codeset) |
| 100 | + |
| 101 | + nodes = process(data.nodes, newtag, formula,1) |
| 102 | + ways = process(data.ways, newtag, formula,2) |
| 103 | + relations = process(data.relations, newtag, formula,3) |
| 104 | + |
| 105 | + data.addcomment("Done. " + str(nodes) + " nodes, " + str(ways) + " ways and " + str(relations) + " relations changed.") |
| 106 | + data.write(sys.stdout) |
| 107 | + return 0 |
| 108 | + |
| 109 | +if __name__ == '__main__': |
| 110 | + main() |
0 commit comments