User:Ant/Scripts

From OpenStreetMap Wiki
Jump to navigation Jump to search

A python script used to create a table of bus and tram stops in Bremen:

# -*- coding: utf-8 -*-

from urllib import urlopen

bsag = file("lines201011")
osm = [file("../bremen.osm"), file("../niedersachsen-bbox.osm")]
out = file("stop_relations", "w")

class stop:
	def __init__(self, name):
		self.name = name
		self.aliases = []
		self.relID = []
		self.nodeID = []
		self.lines = []
		self.tram = False
		self.bus = False
		self.tram_stops_found = 0
		self.bus_stops_found = 0
	def __cmp__(self, other):
		return cmp((self.name, self.aliases), (other.name, other.aliases))
		
class line:
	def __init__(self, ref):
		self.ref = ref
		self.stops = []
		self.stops_total = 0
		self.stops_missing = 0

Input = []
Output = []
ref = ""
ID = ""
name = ""
stop_area = False
tram_stop = False
bus_stop = False
stops = []
lines = []
tram_lines = ["1", "1S", "2", "3", "3S", "4", "4S", "5", "6", "8", "10", "N1", "N4", "N10"]
bus_lines = ["20", "21", "22", "24", "25", "26", "27", "28", "29", "30", "32", "33", "34", "37", "38", "40", "41", "41S", "42", "44", "51", "52", "53", "55", "57", "58", "61", "62", "63", "65", "66", "70S", "71", "73", "74", "74S", "75", "76", "78", "79", "80", "81", "677", "N2", "N3", "N5", "N6", "N7", "N8", "N9"]
one_way_stops = ["Otto-Hahn-Allee", "Achterdiek", "Hermann-Frese-Straße", "Gravestraße", "Nedderland", "Mackensenweg", "Im Achterkamp", "Waldau-Theater", "Weserwehr"]

# import bus stops from BSAG list

for x in bsag.readlines():
	if x != "\n":
		if x[:1] != " ":
			ref = x[:-1]
			currentLine = line(ref)
			lines.append(currentLine)
		else:
			name = x[6:-1]
			nameIsNew = True
			for s in stops:
				if s.name == name:
					nameIsNew = False
					currentStop = s
					break
			if nameIsNew:
				currentStop = stop(name)
				stops.append(currentStop)
			if ref in tram_lines:
				currentStop.tram = True
			if ref in bus_lines:
				currentStop.bus = True
			if currentLine not in currentStop.lines:
				currentStop.lines.append(currentLine)
			currentLine.stops.append(currentStop)
			currentLine.stops_total += 1

bsag.close()

# blacklist: false friends inside bbox
blacklist = ["286610295", "286611173", "299511831", "299511862", "286075036", "286075157", "288334758", "288334933", "288335023", "286078545", "286078483"]

# aliases
aliases = dict()
aliases["Am Spreeken"] = "Schwanewede, Am Spreeken"
aliases["Am Stackkamp"] = "Stackkamp"
aliases["Bahnhof Aumund"] = "Bf Aumund"
aliases["Bahnhof Blumenthal"] = "Bf Blumenthal"
aliases["Bahnhof Bremen Farge"] = "Bf Farge"
aliases["Bahnhof Burg"] = "Bf Burg"
aliases["Bahnhof Farge"] = "Bf Farge"
aliases["Bahnhof Lesum"] = "Bf Lesum"
aliases["Bahnhof Sebaldsbrück"] = "Bf Sebaldsbrück"
aliases["Bahnhof Turnerstraße"] = "Bf Turnerstraße"
aliases["Borgfeld - Mitte"] = "Borgfeld-Mitte"
aliases["Brinkum Nord"] = "Brinkum-Nord"
aliases["Berufsbildungswerk (Technologiepark)"] = "Berufsbildungswerk"
aliases["Blumenthalstraße (Messe)"] = "Blumenthalstraße"
aliases["Dammschule"] = "Schwanewede, Dammschule"
aliases["Diako"] = "DIAKO Bremen"
aliases["Doventorsteinweg (Agentur für Arbeit)"] = "Doventorsteinweg"
aliases["D.-Bonhoeffer-Straße"] = "Dietrich-Bonhoeffer-Straße"
aliases["Findorff / Weidedamm III"] = "Weidedamm III"
aliases["Findorffallee (Torfhafen)"] = "Findorffallee"
aliases["Flachsberg"] = "Schwanewede, Flachsberg"
aliases["Flughafen/Airport"] = "Flughafen"
aliases["Fuchsberg"] = "Schwanewede-Meyenburg, Fuchsberg"
aliases["Herdentor (Musical)"] = "Herdentor"
aliases["Grohn Markt"] = "Grohn/Markt"
aliases["G.-Heinemann-Bürgerhaus"] = "Gustav-Heinemann-Bürgerhaus"
aliases["Heiligenberg Straße"] = "Heiligenbergstraße"
aliases["Heiligenberg-Straße"] = "Heiligenbergstraße"
aliases["Joseph-Böhm-Straße"] = "Josef-Böhm-Straße"
aliases["Jospeh-Böhm-Straße"] = "Josef-Böhm-Straße"
aliases["Kattenturm Mitte"] = "Kattenturm-Mitte"
aliases["Kattrepeler Landstraße"] = "Katrepeler Landstraße"
aliases["Kirchweg (Werdersee)"] = "Kirchweg"
aliases["Klagenfurter Straße (Universität/IW3)"] = "Klagenfurter Straße"
aliases["Klinikum links der Weser"] = "Klinikum Links der Weser"
aliases["Kreinsloger"] = "Bf Kreinsloger"
aliases["Lesum / Kirche"] = "Lesum/Kirche"
aliases["Margarethenallee"] = "Margaretenallee"
aliases["Neuenlander Kämpe / Polizeiinspektion Neustadt"] = "Neuenlander Kämpe"
aliases["Norderlander Straße"] = "Norderländer Straße"
aliases["Ortsamt / Kirche"] = "Ortsamt/Kirche"
aliases["Panrepel Nord"] = "Panrepel-Nord"
aliases["Panrepel Süd"] = "Panrepel-Süd"
aliases["Phillip-Scheidemann-Straße"] = "Philipp-Scheidemann-Straße"
aliases["Radio Bremen (Volkshochschule)"] = "Radio Bremen"
aliases["Rennbahn"] = "Rennplatz"
aliases["Roland Center"] = "Roland-Center"
aliases["Sankt-Joseph-Stift"] = "St.-Joseph-Stift"
aliases["Schwanenberg"] = "Schwanewede-Meyenburg, Schwanenberg"
aliases["Siethlandswehr"] = "Schwanewede-Meyenburg, Siethlandswehr"
aliases["Stahlwerk, Tor 1"] = "Stahlwerk Tor 1"
aliases["Stehnkenshoff"] = "Stehnckenshoff"
aliases["Steubenstraße (Stadtamt)"] = "Steubenstraße"
aliases["Tenever Zentrum"] = "Tenever-Zentrum"
aliases["Wachendorf"] = "Schwanewede-Meyenburg, Wachendorf"
aliases["Waller Friedhof (Eissporthalle)"] = "Waller Friedhof"
aliases["Waller Ring (Hochschule für Künste)"] = "Waller Ring"
aliases["Wetterungsweg (Tierheim)"] = "Wetterungsweg"
aliases["Wilhelm-Busch-Straße"] = "Wilhelm-Busch-Weg"
aliases["Wilh.-Leuschner-Straße"] = "Wilhelm-Leuschner-Straße"
aliases["Wulwesstraße (Ulrichsplatz)"] = "Wulwesstraße"

# find OSM stops

for f in osm:
	for x in f.readlines():
		i = x.find('<node id="')
		if i > -1:
			j = i + 10
			ID = x[j:x.find('"', j)]
		i = x.find('k="name" v="')
		if i > -1:
			j = i + 12
			name = x[j:x.find('"', j)]
		if x.find('<tag k="railway" v="tram_stop"') > -1:
			tram_stop = True
		if x.find('<tag k="highway" v="bus_stop"') > -1:
			bus_stop = True
		if x.find("</node>") > -1 and (tram_stop or bus_stop) and ID not in blacklist:
			if name != "":
				for s in stops:
					if s.name == aliases.get(name):
						if name not in s.aliases:
							s.aliases.append(name)
					if s.name == name or s.name == aliases.get(name):
						if ID not in s.nodeID:
							s.nodeID.append(ID)
						else:
							break
						if tram_stop:
							s.tram_stops_found += 1
						if bus_stop:
							s.bus_stops_found += 1
						break
			name = ""
			tram_stop = False
			bus_stop = False
	f.close()

# find OSM stop area relations

#<snip>

stops.sort()

# create output

countStops = 0
countStopsMissing = 0
countStopsRel = 0

for s in stops:
	nameOut = s.name
	linesOut = ""
	noteOut = ""
	relOut = ""
	nodesOut = ""
	countStops += 1
	serving_lines = []
	for l in lines:
		if s in l.stops:
			serving_lines.append(l)
	if s.tram:
		nameOut += " [[Image:De TRAM-logo.png|20px]]"
	if s.bus:
		nameOut += " [[Image:De BUS-Logo-purpur.png|20px]]"
	if s.lines != []:
		for n in s.lines:
			linesOut += n.ref + ", "
		linesOut = linesOut[:-2] + " "
	if s.aliases != []:
		for n in s.aliases:
			noteOut += n + "; "
		noteOut = noteOut[:-2] + " "
	if s.relID == [] and s.nodeID == []:
		if noteOut != "":
			noteOut += "<br/>"
		noteOut += "{{nok}} fehlt "
		countStopsMissing += 1
		for l in serving_lines:
			l.stops_missing += 1
	#elif s.relID != []:
	elif s.tram and s.tram_stops_found == 0:
		if noteOut != "":
			noteOut += "<br/>"
		noteOut += "{{nok}} Tramhaltestelle fehlt "
		countStopsMissing += 1
		for l in serving_lines:
			if l.ref in tram_lines:
				l.stops_missing += 1
	elif s.bus:
		if noteOut != "":
			noteOut += "<br/>"
		if s.bus_stops_found == 0:
			noteOut += "{{nok}} Bushaltestelle fehlt "
			countStopsMissing += 1
			for l in serving_lines:
				if l.ref in bus_lines:
					l.stops_missing += 1
		elif s.bus_stops_found == 1 and s.name not in one_way_stops:
			noteOut += "{{nok}} Bushaltestelle unvollständig "
			countStopsMissing += 1
			for l in serving_lines:
				if l.ref in bus_lines:
					l.stops_missing += 0.5
	#if s.relID != []:
	#	for n in s.relID:
	#		relOut += "{{relation|" + n + "}}, "
	#	relOut = relOut[:-2] + " "
	#	countStopsRel += 1
	if s.nodeID != []:
		for n in s.nodeID:
			nodesOut += "{{node|" + n + "}}, "
		nodesOut = nodesOut[:-2]
	Output.append("|%s || %s|| %s|| %s" % (nameOut, linesOut, noteOut, nodesOut))

for x in Output:
	out.write("|-\n")
	out.write(x)
	out.write("\n")
	
for l in lines:
	p = 1 - (l.stops_missing + .0) / l.stops_total
	out.write("\n" + l.ref + ": {{State Route|r=3|h=%d}} (%d" % ((int)(p*3), round(100*p)) + " %)")
	
out.write("\n")
out.write("\nVon insgesamt '''%s''' Haltestellen sind '''%s''' in OSM vorhanden, davon '''%s''' als Relation." % (countStops, countStops - countStopsMissing, countStopsRel))
out.write("\n")

out.close()