コード例 #1
0
 def endElement(self, name):
     if name == 'node':
         n = Vertex(id=self.id, pt=Point(self.geometry[0], self.geometry[1]))            
         n.save()
         self.vertexs[self.id] = n
         
         self.id = None
         self.geometry = None
         self.tags = None
コード例 #2
0
ファイル: utils.py プロジェクト: meomap/Hanoi-Restaurants-Map
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