-
-
Save davidstrategicaci/52cdbc607dc37cf9705929e33e506bca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Query the graph, calculates the wegiht, then read the data the Dijstars Graph() class | |
graphQuery = """ | |
-- Forwards, from start to end | |
SELECT start_node as node1, end_node as node2, split_way_id, | |
CASE | |
WHEN json_extract(tags, '$.oneway') == '-1' THEN 10000000000.0 | |
ELSE length_m | |
END as cost | |
FROM osm_network_graph | |
WHERE json_extract(tags, '$.highway') IN {0} | |
UNION | |
-- Backwards, from end to start | |
SELECT end_node as node1, start_node as node2, split_way_id, | |
CASE | |
WHEN json_extract(tags, '$.oneway') == 'yes' THEN 10000000000.0 | |
ELSE length_m | |
END as cost | |
FROM osm_network_graph | |
WHERE json_extract(tags, '$.highway') IN {0};""".format(str(tuple(Vehicle))) | |
cursor.execute(graphQuery) | |
# Define Graph() Object | |
osmGraph = Graph() | |
[osmGraph.add_edge(item[0],item[1],{'edge_id':item[2],'cost':item[3]}) for item in cursor.fetchall()] | |
# Define a function that will be applied to the graph to return the cost of each edge | |
cost_func = cost_func = lambda u, v, e, prev_e: e['cost'] | |
# Solve from Starting Node To End Node | |
PointA = 3415159798 # National Stadium - Washington, DC | |
PointB = 2179757650 # John F. Kennedy Center, Washington DC | |
try: | |
path = find_path(graph=osmGraph, s=PointA,d=PointB,cost_func=cost_func) | |
# Collect Edge Ids | |
edges = [edge['edge_id'] for edge in path.edges] | |
# Create a GeoPackageView Of Route | |
routeSeqence = ''.join(['WHEN osm.split_way_id={} THEN {} '.format(edges[i],i) for i in range(len(edges))]) | |
try: | |
createView = """CREATE VIEW route AS | |
SELECT CASE {0} END AS route_sequence, osm.* | |
FROM osm_network_graph osm | |
WHERE osm.split_way_id IN {1}""".format(routeSeqence,str(tuple(edges))) | |
insertToGpkgContents = """INSERT INTO gpkg_contents (table_name, identifier, data_type, srs_id) | |
VALUES('route','route','features',4326)""" | |
insertToGpkgGeometryColumn ="""INSERT INTO gpkg_geometry_columns (table_name, column_name, geometry_type_name, srs_id, z, m) | |
VALUES ('route', 'geom', 'LINESTRING', 4326, 0, 0)""" | |
cursor.execute(createView) | |
cursor.execute(insertToGpkgGeometryColumn) | |
cursor.execute(insertToGpkgContents) | |
databaseConnection.commit() | |
except sqlite3.OperationalError: | |
print('View Currently Exists') | |
databaseConnection.close() | |
# Execpt error is no path is found | |
except NoPathError: | |
print('No Path Found') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment