def makeEdges(self): name = "" if "name" in self.tags: name = self.tags["name"] for i in range(len(self.geometry)-1): # retrieve related Vertexs startVertex = self.vertexs[self.geometry[i]] endVertex = self.vertexs[self.geometry[i+1]] # additional attributes reverse = True if 'oneway' in self.tags: if self.tags['oneway'] == "yes" or self.tags['oneway'] == "1": reverse = False cost = distance(startVertex.pt, endVertex.pt) # make edge edge = Edge(name=name, cost=cost, reverse=reverse, line=LineString([startVertex.pt, endVertex.pt]), vertex_start=startVertex, vertex_end=endVertex) edge.save()
def match_vertex(location): """ Return the corresponding vertex for a given geometry. """ cursor = connection.cursor() # Find the nearest edges for a given point sql = ( "SELECT id, ST_Distance(GeomFromText('POINT(%s %s)',4326),line) AS myLineDistance FROM route_edge ORDER BY myLineDistance LIMIT 3;" % (location.x, location.y) ) cursor.execute(sql) row = cursor.fetchone() edge_id = row[0] print "nearest road is ", Edge.objects.get(id=edge_id).name # Find the closest point on the previous edge sql = ( "SELECT AsText(ST_Line_Interpolate_Point(line,ST_Line_Locate_Point(line,GeomFromText('POINT(%s %s)', 4326)))) FROM route_edge WHERE id = %s;" % (location.x, location.y, row[0]) ) cursor.execute(sql) row = cursor.fetchone() closestpt = row[0] # Add this point to graph id = (Vertex.objects.latest("id").id) + 1 print "id is ", id v = Vertex(id=id, pt=closestpt) v.save() # Make edges connect to this point e = Edge.objects.get(id=edge_id) e_start = e.vertex_start e_end = e.vertex_end cost1 = distance(e_start.pt, v.pt) e1 = Edge( name=e.name, cost=cost1, reverse=e.reverse, line=LineString([e_start.pt, v.pt]), vertex_start=e_start, vertex_end=v, ) e1.save() cost2 = distance(v.pt, e_end.pt) e2 = Edge( name=e.name, cost=cost2, reverse=e.reverse, line=LineString([v.pt, e_end.pt]), vertex_start=v, vertex_end=e_end ) e2.save() return v