1. Dmitri Lebedev
  2. road-graph-cleaner

Commits

Dmitri Lebedev  committed 0835063

created a driver for osm, pyflakes

  • Participants
  • Parent commits 92b2a81
  • Branches refactoring

Comments (0)

Files changed (3)

File classes.py

View file
 	"""
 	_instances = {}
 
-	def __new__(cls, lat, lon):
-		c = (lat, lon)
+	@classmethod
+	def unique_key(*args):
+		"""
+		Used to generate the key to access the same object without re-instantiating it. Child classes can override this.
+		"""
+		return args
 
-		if c not in cls._instances:  # create a new instance only if none exists in this place
+	def __new__(cls, *args):
+		key = cls.unique_key(*args)
+		if key not in cls._instances:  # create a new instance only if none exists in this place
 			self = super(Vertex, cls).__new__(cls)
-			self.coords = lat, lon
-			cls._instances[c] = self
+			cls._instances[args] = self
 
-		return cls._instances[c]
+		return cls._instances[args]
 
 	@staticmethod
 	def get_3d(lat, lon):
 
 	def __init__(self, lat, lon):
 		# store co-ordinates in 3d space to ease distance calculations
+		self.coords = lat, lon
 		self.xyz = self.get_3d(lat, lon)
 
 	def __repr__(self):
 	def combine_lists(l1, l2):
 		return l1 + l2[1:]
 
+	@classmethod
+	def glue(cls, e1, e2):
+		if not set(e1.vertices) & set(e2.vertices):
+			raise ValueError('Edges have no common point')
+
+		if e1.vertices[0] == e2.vertices[1]:
+			e1, e2 = e2, e1
+
+		return cls(e1.vertices[0], e2.vertices[1])
+
 
 class Graph(object):
 	EdgeClass = Edge

File mp.py

View file
 from collections import defaultdict
-from itertools import izip, ifilter, starmap
+from itertools import izip
 
 from classes import Edge, Graph
 
 
 	@classmethod
 	def glue(cls, e1, e2):
-		if e1.ids[0] == e2.ids[1]:
-			e1, e2 = e2, e1
-
-		points = cls.combine_lists(e1.points, e2.points)
-		ids = (e1.ids[0], e2.ids[1])
-		data = cls.combine_lists(e1.data, e2.data)
-		return cls.from_points(points, ids, data)
+		new_edge = cls.__base__(e1, e2)
+		new_edge.points = cls.combine_lists(e1.points, e2.points)
+		new_edge.ids = (e1.ids[0], e2.ids[1])
+		new_edge.data = e1.data + e2.data
+		return new_edge
 
 
 class MpGraph(Graph):

File osm.py

View file
+from collections import defaultdict
+from itertools import izip, ifilter, starmap
+
+from classes import Edge, Graph, Vertex
+
+class OsmVertex(Vertex):
+	id_counter = 0
+
+	@classmethod
+	def unique_key(id, lat, lon):
+		"""
+		OSM vertices are identified by osm id, not lat & lon.
+		"""
+		return id
+
+	def __init__(self, id, lat, lon):
+		# store co-ordinates in 3d space to ease distance calculations
+		self.id = id
+		self.xyz = self.get_3d(lat, lon)
+
+	@classmethod
+	def create(cls, lat, lon):
+		cls.id_counter -= 1
+		return cls(cls.id_counter, lat, lon)
+
+
+class OsmEdge(Edge):
+	VertexClass = OsmVertex
+
+	def __init__(self, v1, v2):
+		self.vertice_ids = v1.id, v2.id
+
+	@classmethod
+	def from_nodes(cls, nodes, ids, data=None):
+		self = cls.from_coords(nodes[0], nodes[-1])
+
+		self.nodes = nodes
+		self.data = data  # list of dictionaries
+
+		return self
+
+	@classmethod
+	def glue(cls, e1, e2):
+		new_edge = cls.__base__(e1, e2)
+		new_edge.nodes = cls.combine_lists(e1.nodes, e2.nodes)
+		new_edge.data = e1.data + e2.data
+		return new_edge
+
+class OsmGraph(Graph):
+	EdgeClass = OsmEdge
+
+	def __init__(self, handle=()):
+		super(OsmGraph, self).__init__()
+
+		# parse osm file here
+
+		self.compact()