Package: scgraph
scgraphSupply chain graph package for PythonDocumentationGetting Started:https://github.com/connor-makowski/scgraphLow Level:https://connor-makowski.github.io/scgraph/core.htmlKey FeaturesCalculate the shortest path between two points on earth using a latitude / longitude pairInputs:A latitude / longitude pair for the originA latitude / longitude pair for the destinationCalculation:Algorithms:Dijkstra's algorithm (Modified for sparse networks)Modified to support sparse network data structuresMakowski's Modified Sparse Dijkstra algorithmModified for O(n) performance on particularly sparse networksPossible future support for other algorithmsDistances:Uses thehaversine formulato calculate the distance between two points on earthReturns:path:A list of dictionaries (latitudeandlongitude) that make up the shortest pathlength:The distance in kilometers between the two pointsAntimeridian supportArbitrary start and end pointsArbitrary network data setsSetupMake sure you have Python 3.6.x (or higher) installed on your system. You can download ithere.Installationpip install scgraphUse with Google ColabSee the examplehereGetting StartedBasic UsageGet the shortest path between two points on earth using a latitude / longitude pair
In this case, calculate the shortest maritime path between Shanghai, China and Savannah, Georgia, USA.# Use a maritime network geographfromscgraph.geographs.marnetimportmarnet_geograph# Get the shortest path betweenoutput=marnet_geograph.get_shortest_path(origin_node={"latitude":31.23,"longitude":121.47},destination_node={"latitude":32.08,"longitude":-81.09},output_units='km')print('Length: ',output['length'])#=> Length:  19596.4653In the above example, theoutputvariable is a dictionary with three keys:length,coordinate_pathandpath.length: The distance between the passed origin and destination when traversing the graph along the shortest pathNotes:This will be in the units specified by theoutput_unitsparameter.output_unitsoptions:km(kilometers - default)m(meters)mi(miles)ft(feet)coordinate_path: A list of dictionaries (latitudeandlongitude) that make up the shortest pathpath: A list of node ids (from the network data set) that make up the shortest pathIn general, these are only relevant if you are providing your own custom network data setTo get the latitude / longitude pairs that make up the shortest path, as a list of lists, you could do something like the following:# Use a maritime network geographfromscgraph.geographs.marnetimportmarnet_geograph# Get the shortest path betweenoutput=marnet_geograph.get_shortest_path(origin_node={"latitude":31.23,"longitude":121.47},destination_node={"latitude":32.08,"longitude":-81.09})print(str([[i['latitude'],i['longitude']]foriinoutput['coordinate_path']]))Included GeoGraphsmarnet_geograph:What: A maritime network data set from searouteUse:from scgraph.geographs.marnet import marnet_geographAttribution:searouteLength Measurement: KilometersMarnet Pictureoak_ridge_maritime_geograph:What: A maritime data set from the Oak Ridge National Laboratory campusUse:from scgraph.geographs.oak_ridge_maritime import oak_ridge_maritime_geographAttribution:Oak Ridge National Laboratorywith data fromGeocommonsLength Measurement: KilometersOak Ridge Maritime Picturenorth_america_rail_geograph:What: Class 1 Rail network for North AmericaUse:from scgraph.geographs.north_america_rail import north_america_rail_geographAttribution:U.S. Department of Transportation: ArcGIS OnlineLength Measurement: KilometersNorth America Rail Pictureus_freeway_geograph:What: Freeway network for the United StatesUse:from scgraph.geographs.us_freeway import us_freeway_geographAttribution:U.S. Department of Transportation: ArcGIS OnlineLength Measurement: KilometersUS Freeway Picturescgraph_datageographs:What: Additional geographs are available in thescgraph_datapackageNote: These include larger geographs like the world highways geograph and world railways geograph.Installation:pip install scgraph_dataUse:from scgraph_data.world_highways import world_highways_geographSee:scgraph_datafor more information and all available geographs.Advanced UsageUsingscgraph_datageographs:Note: Make sure to install thescgraph_datapackage before using these geographsfromscgraph_data.world_railwaysimportworld_railways_geograph# Get the shortest path between Kalamazoo Michigan and Detroit Michigan by Trainoutput=world_railways_geograph.get_shortest_path(origin_node={"latitude":42.29,"longitude":-85.58},destination_node={"latitude":42.33,"longitude":-83.05})Get a geojson line path of an output for easy visualization:Note:mapshaper.organdgeojson.ioare good tools for visualizing geojson filesfromscgraph.geographs.marnetimportmarnet_geographfromscgraph.utilsimportget_line_path# Get the shortest sea path between Sri Lanka and Somaliaoutput=marnet_geograph.get_shortest_path(origin_node={"latitude":7.87,"longitude":80.77},destination_node={"latitude":5.15,"longitude":46.20})# Write the output to a geojson fileget_line_path(output,filename='output.geojson')You can specify your own custom graphs for direct access to the solving algorithms. This requires the use of the low levelGraphclassfromscgraphimportGraph# Define a graph# See the graph definitions here:# https://connor-makowski.github.io/scgraph/core.htmlgraph={0:{1:5,2:1},1:{0:5,2:2,3:1},2:{0:1,1:2,3:4,4:8},3:{1:1,2:4,4:3,5:6},4:{2:8,3:3},5:{3:6}}# Optional: Validate your graphGraph.validate_graph(graph=graph)# Get the shortest path between 0 and 5output=Graph.dijkstra_makowski(graph=graph,origin_id=0,destination_id=5)#=> {'path': [0, 2, 1, 3, 5], 'length': 10}You can also use a slightly higher levelGeoGraphclass to work with latitude / longitude pairsfromscgraphimportGeoGraph# Define nodes# See the nodes definitions here:# https://connor-makowski.github.io/scgraph/core.htmlnodes={0:{"latitude":0,"longitude":0},1:{"latitude":0,"longitude":1},2:{"latitude":1,"longitude":0},3:{"latitude":1,"longitude":1},4:{"latitude":1,"longitude":2},5:{"latitude":2,"longitude":1}}# Define a graph# See the graph definitions here:# https://connor-makowski.github.io/scgraph/core.htmlgraph={0:{1:5,2:1},1:{0:5,2:2,3:1},2:{0:1,1:2,3:4,4:8},3:{1:1,2:4,4:3,5:6},4:{2:8,3:3},5:{3:6}}# Create a GeoGraph objectmy_geograph=GeoGraph(nodes=nodes,graph=graph)# Optional: Validate your graphmy_geograph.validate_graph()# Optional: Validate your nodesmy_geograph.validate_nodes()# Get the shortest path between two pointsoutput=my_geograph.get_shortest_path(origin_node={'latitude':0,'longitude':0},destination_node={'latitude':2,'longitude':1})#=># {#     "path": [6, 0, 2, 1, 3, 5, 7],#     "coordinate_path": [#         {'latitude': 0, 'longitude': 0},#         {'latitude': 0, 'longitude': 0},#         {'latitude': 1, 'longitude': 0},#         {'latitude': 0, 'longitude': 1},#         {'latitude': 1, 'longitude': 1},#         {'latitude': 2, 'longitude': 1},#         {'latitude': 2, 'longitude': 1}#     ],#     "length": 10# }Attributions and ThanksOriginally inspired bysearouteincluding the use of one of theirdatasetsthat has been modified to work properly with this package.