Benutzer-Werkzeuge

Webseiten-Werkzeuge


Seitenleiste

ss16:radwege:dateistruktur:mapfunctions

MapFunctions.py

# -*- coding: utf8 -*-
from __future__ import division, print_function
from GraphFunctions import *
import time
 
"""
Funktion um die Datei(.osm) 'src' einzulesen und im gegebenen Graphen 'g' zu speichern.
"""
def readMap(src, g):
	if(not isinstance(g, Graph)): return None
	nodemap = {}
	with open(src) as f:
		lines = f.readlines();
	wayID = ""
	nodeID = ""
	nodeBuffer = []
	wayTyp = 5
	i = 0
	for line in lines:
		print(str(i) + "/" + str(len(lines)))
		if(line.find("<node") >= 0):
			name = line[line.index("id=\"")+4:]
			name = name[:name.index("\"")]
			lat = line[line.index("lat=\"")+5:]
			lat = lat[:lat.index("\"")]
			lon = line[line.index("lon=\"")+5:]
			lon = lon[:lon.index("\"")]
			nodemap[name] = len(g.nodes)
			g.addNode(name, float(lat), float(lon))
		if(line.find("<way") >= 0):
			wayID = line[line.index("id=\"")+4:]
			wayID = wayID[:wayID.index("\"")]
		if(line.find("/way>") >= 0):
			nodeID = ""
			wayID = ""
			for (n1, n2) in nodeBuffer:
				print("Added edge of type: %d" %wayTyp)
				g.addEdge(g.nodes[nodemap[n1]], g.nodes[nodemap[n2]], wayTyp)
			nodeBuffer = []
			wayTyp = 5
		if((line.find("<nd ") >= 0) and (not wayID == "")):
			newID = line[line.index("ref=\"")+5:]
			newID = newID[:newID.index("\"")]
			if(not nodeID == ""):
				if nodeID in nodemap and newID in nodemap:
					nodeBuffer.append((nodeID, newID))
			nodeID = newID
		if((line.find("<tag") >= 0) and (not wayID == "")):
			if line[(line.index("k=")+3):(line.index(" v=")-1)] == "highway":
				#Strassentyp
				v = line[(line.index("v=\"")+3):(line.index("\"/>"))]
				if v == "primary":
					#Primaerstrasse
					wayTyp = 10
				elif v == "secondary":
					#Sekundaerstrasse
					wayTyp = 8
		i = i + 1
	return g
 
"""
Funktion um den gegebenen Graphen 'g' im Kartenformat(.osm) in die Datei 'des' zu schreiben.
"""
def printMap(g, des="klein.osm"):
	if(not isinstance(g, Graph)): return None
	f = open(des, "w+")
	print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", file=f)
	print("<osm version=\"0.6\" generator=\"Overpass API\">", file=f)
	for n in g.nodes:
		if n.ancestor == None:
			print("<node id=\"%s\" lat=\"%2.7f\" lon=\"%2.7f\"/>" %(n.name, n.gnb, n.gel), file=f)
		else:
			print("<node id=\"%s\" lat=\"%2.7f\" lon=\"%2.7f\">" %(n.name, n.gnb, n.gel), file=f)
			print("<tag k=\"path\" v=\"true\"/>", file=f)
			print("</node>", file=f)
	edgeID = 0
	for e in g.edges:
		print("<way id=\"%i\">" %edgeID, file=f)
		print("<nd ref=\"%s\"/>" %e.node1.name, file=f)
		print("<nd ref=\"%s\"/>" %e.node2.name, file=f)
		lineType = 1
		lineType = e.typ-typeMin
		print("<tag k=\"line\" v=\"%d\"/>" %lineType, file=f)
		print("</way>", file=f)
		edgeID += 1
	print("</osm>", file=f)
 
"""
Funktion um den gegebenen Graphen 'g' im Kartenformat(.osm) mit markiertem Pfad zu speichern.
Sollte nach dem Wegfinde-Algorithmus aufgerufen werden
Nodes die einen Ancestor haben werden einen "path" tag kriegen
Bsp:
<node id="458330" lat="52.5112015" lon="13.2861468">
    <tag k="path" v="true"/>
</node>
"""
def printMapWithPath(g, endNode=None):
	if(not isinstance(g, Graph)): return None
 
	aktZeit=time.localtime();
	datum="%04i%02i%02i" % (aktZeit[0], aktZeit[1], aktZeit[2])
	zeit="%02i%02i%02i" % (aktZeit[3], aktZeit[4], aktZeit[5])
	desFile=("Wegkarte_%s_%s.osm") % (datum, zeit)
 
	f = open(desFile, "w+")
	print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", file=f)
	print("<osm version=\"0.6\" generator=\"Overpass API\">", file=f)
	for n in g.nodes:
		if n.ancestor == None:
			print("<node id=\"%s\" lat=\"%2.7f\" lon=\"%2.7f\"/>" %(n.name, n.gnb, n.gel), file=f)
		else:
			print("<node id=\"%s\" lat=\"%2.7f\" lon=\"%2.7f\">" %(n.name, n.gnb, n.gel), file=f)
			print("<tag k=\"path\" v=\"true\"/>", file=f)
			print("</node>", file=f)
	edgeID = 0
	#Hier werden edges des Pfad mit dem Attribut 'path' markiert.
	if(isinstance(endNode, Node)):
		for e in g.edges:
			e.path = False
		cur = endNode
		while(not (cur == None)):
			for e in cur.cEdges:
				if((e.node1 == cur and e.node2 == cur.ancestor) or (e.node2 == cur and e.node1 == cur.ancestor)):
					e.path = True
			cur = cur.ancestor
	for e in g.edges:
		print("<way id=\"%i\">" %edgeID, file=f)
		print("<nd ref=\"%s\"/>" %e.node1.name, file=f)
		print("<nd ref=\"%s\"/>" %e.node2.name, file=f)
		if(e.path):
			print("<tag k=\"highway\" v=\"primary\"/>", file=f)
		else:
			print("<tag k=\"highway\" v=\"residential\"/>", file=f)
		print("</way>", file=f)
		edgeID += 1
	print("</osm>", file=f)
 
#######################################################################################
#Funktionen die mit dem selbst erstellten Dateiformat (.save) arbeiten.
 
"""
Funktion um den gegebenen Graphen 'g' in der Datei(.save) 'des' zu speichern.
"""
def save(g, des):
	if(not isinstance(g, Graph)): return None
	f = open(des, "w+")
	sv = 0
	for n in g.nodes:
		print("n %s %f %f" %(n.name, n.gnb, n.gel), file=f, end="")
		print(" %f %s %s" %(n.dist, str(n.ancestor), str(n.unvisited)), file=f, end="")
		print("", file=f)
		sv = sv + 1
	sv = 0
	for e in g.edges:
		print("e %s %s %d %f" %(e.node1.name, e.node2.name, e.typ, e.entf), file=f)
		sv = sv + 1
 
"""
Funktion um einen Graphen aus der gegebenen Datei(.save) 'src' zu erstellen.
"""
def load(src):
	g = Graph()
	nodemap = {}
	with open(src) as f:
		lines = f.readlines();
	for line in lines:
		splitted = line.split(" ")
		splitted[len(splitted)-1] = splitted[len(splitted)-1][:-1]
		if(splitted[0] == "n"):#Node
			nodemap[splitted[1]] = len(g.nodes)
			anc = None
			if(splitted[5] != "None"):anc = g.nodes[nodemap[splitted[5]]]
			g.addNode(splitted[1], float(splitted[2]), float(splitted[3]), float(splitted[4]), anc, bool(splitted[6]))
		elif(splitted[0] == "e"):#Edge
			g.addEdge(g.nodes[nodemap[splitted[1]]], g.nodes[nodemap[splitted[2]]], float(splitted[3]), entf = float(splitted[4]))
	return g
ss16/radwege/dateistruktur/mapfunctions.txt · Zuletzt geändert: 2016/09/27 17:48 von fence