def main(): from datetime import date import os global saved f = open(os.path.expanduser("~/.osmitstats"), "r") j = JsonReader() saved = j.read(f.readline()) print repr(saved) f.close() #saved["current"] = str(date.today()).replace('-', '') #today = str(date.today()).replace('-', '') saved["current"] = "20090531" q_regioni = """SELECT c.nome_reg, sum(c.pop2001), sum(length(transform(s.intersection, 3395))) AS highw FROM it_comuni c LEFT JOIN osm_stat_20090531 s ON c.pro_com =s.pro_com GROUP BY c.nome_reg ORDER BY nome_reg;""" f = open("page.html", "w") f.write(comuni_coperti_per_regione()) f.flush() f.close() # The script has finished now, touch our helper file f = open(os.path.expanduser("~/.osmitstats"), "w") j = JsonWriter() #saved["previous"] = saved["current"] saved["current"] = None f.write(j.write(saved)) f.close()
def save(nodes, ways, relations, tags): try: j = JsonReader() f = open(os.path.expanduser(stats_path), "r") stats = j.read(f.readline()) f.close() except (ReadException, IOError): f = open(os.path.expanduser(stats_path), "w") f.close() stats = {} pass nodes_count = 0 ways_count = 0 relations_count = 0 # let's count primitives for pair in mysort(nodes, False): nodes_count += pair[0] for pair in mysort(ways, False): ways_count += pair[0] for pair in mysort(relations, False): relations_count += pair[0] tags_count = defaultdict(int) for tag in tags: for pair in tags[tag]: tags_count[tag] += pair[0] p = re.compile(".*/italy_(\d{8})\.osm") m = p.match(sys.argv[1]) if m: date = m.groups()[0] else: date = today stats[date] = { "nodes" : nodes_count, "ways" : ways_count, "relations" : relations_count, "tags" : dict(tags_count), } f = open(os.path.expanduser(stats_path), "w") j = JsonWriter() f.write(j.write(stats)) f.close()
def main(): from optparse import OptionParser import os global saved parser = OptionParser() parser.add_option("--planet", dest="planet", action="store", default=None, help="The planet file to parse") parser.add_option("--key", dest="key", action="store", default=None, help="The key to generate statistics for") parser.add_option("--tag", dest="tag", action="store", default=None, help="The tag to generate statistics for") (options, args) = parser.parse_args() if not options.planet: parser.error("The --planet option is required.") if options.key and options.tag: parser.error("You cannot specify both --tag and --key.") if not options.key and not options.tag: parser.error("You must specify one between --tag and --key.") if options.key: options.tag = "%s=*" % options.key try: f = open(os.path.expanduser("~/.osmitstats"), "r") j = JsonReader() saved = j.read(f.readline()) f.close() except ReadException: pass saved["tagstats"] = { "20090617" : parse(options.planet, options.tag) } print repr(saved) f = open(os.path.expanduser("~/.osmitstats"), "w") j = JsonWriter() f.write(j.write(saved)) f.close()
def main(): from json import JsonReader, JsonWriter, ReadException from datetime import date from collections import defaultdict import os, sys try: f = open(os.path.expanduser(stats_path), "r") j = JsonReader() stats = j.read(f.readline()) f.close() except (ReadException, IOError): print "Cannot open JSON stats file %s!" % stats_path sys.exit(1) xcoords = [] ynodes, yways, yrelations = ([], [], []) tags = defaultdict(list) newtags = defaultdict(list) # print repr(stats) for date in stats: xcoords.append(date) for name in stats[date]: value = stats[date][name] if name == "nodes": ynodes.append([date,value]) elif name == "ways": yways.append([date,value]) elif name == "relations": yrelations.append([date,value]) elif name == "tags": for tag in value: tags[tag].append([date, value[tag]]) ynodes = sorted(ynodes) yways = sorted(yways) yrelations = sorted(yrelations) for tag in tags: newtags[tag] = zip(*sorted(tags[tag])) plot(newtags[tag][0], newtags[tag][1], tag, "g^-", label=tag) plot(zip(*ynodes)[0], zip(*ynodes)[1], "nodes", "bo-", label="Nodes") plot(zip(*yways)[0], zip(*yways)[1], "ways", "bo-", label="Ways") plot(zip(*yrelations)[0], zip(*yrelations)[1], "relations", "bo-", label="Relations")