Skip to content

Commit d33b7e8

Browse files
committed
Init commit
0 parents  commit d33b7e8

File tree

9 files changed

+348
-0
lines changed

9 files changed

+348
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
target/*
2+
*/*.class
3+
osm_work_dir/*
4+
!osm_work_dir/.gitkeep
5+
.DS_Store

Readme.md

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# pyGraphhopper
2+
3+
A utility which allows you to use openstreetmap using [graphhopper](https://github.com/graphhopper/graphhopper) in python.
4+
It uses py4j to bridge between java and python.
5+
6+
### Prerequisites
7+
8+
1. Java
9+
2. Python
10+
3. py4j python module globally
11+
4. The pbf file of open street map for the locality you want to work on. (e.g. http://download.geofabrik.de/asia/indonesia-latest.osm.pbf)
12+
13+
### Installing
14+
15+
1. Get the pbf file desired location. e.g.
16+
```
17+
wget http://download.geofabrik.de/asia/indonesia-latest.osm.pbf
18+
```
19+
20+
2. Install the java dependencies
21+
22+
```
23+
mvn clean
24+
mvn install dependency:copy-dependencies
25+
mvn install
26+
```
27+
28+
3. Start the java gateway server:
29+
30+
```
31+
java -cp ./target/route_distance-1.0-SNAPSHOT.jar:./target/dependency/* pygraphhopper.Gateway
32+
```
33+
34+
4. You can call the APIs from python now.
35+
36+
```
37+
from py4j.java_gateway import JavaGateway
38+
#Creating bridge to Java gateway!
39+
gateway = JavaGateway()
40+
routeCalculator = gateway.entry_point.getRCObject("osm_work_dir", "car", "indonesia-latest.osm.pbf")
41+
routeCalculator.getDistance(orig_lat, orig_lon, dest_lat, dest_lon, "car")
42+
```
43+
44+
45+
## APIs:
46+
47+
1. `getRouteDistance: routeCalculator.getDistance`
48+
49+
This function returns the actual route distance between two geolocations given the type of compute (walk/car)
50+
51+
2. `processFiles: routeCalculator.mainHelper`
52+
53+
To calculate the route distance for a set of geolocations in a csv.
54+
55+
## TODO:
56+
Add more APIs provided by graphhopper, only calculate route distance is being provided right now.
57+
58+
## Contribution
59+
Be the part of this `revolutionary world changing project` :P . Pick something from TODO list and raise PR. :)
60+
61+
Earn as many thanks as possible.
62+
63+
## License
64+
65+
This project is licensed under the MIT License.
66+
67+
## Acknowledgments
68+
69+
* [Graphhopper](https://github.com/graphhopper/graphhopper)

osm_work_dir/.gitkeep

Whitespace-only changes.

pom.xml

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>route_distance</groupId>
5+
<artifactId>route_distance</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<dependencies>
9+
10+
<dependency>
11+
<groupId>com.graphhopper</groupId>
12+
<artifactId>graphhopper-core</artifactId>
13+
<version>0.8.2</version>
14+
</dependency>
15+
16+
<dependency>
17+
<groupId>org.optaplanner</groupId>
18+
<artifactId>optaplanner-core</artifactId>
19+
<version>6.1.0.Final</version>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>com.opencsv</groupId>
24+
<artifactId>opencsv</artifactId>
25+
<version>3.9</version>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>com.graphhopper</groupId>
30+
<artifactId>graphhopper-reader-osm</artifactId>
31+
<version>0.8.2</version>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>py4j0.10.5</groupId>
36+
<artifactId>py4j0.10.5</artifactId>
37+
<scope>system</scope>
38+
<version>1.0</version>
39+
<systemPath>${basedir}/src/lib/py4j0.10.5.jar</systemPath>
40+
</dependency>
41+
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-assembly-plugin</artifactId>
49+
<executions>
50+
<execution>
51+
<phase>package</phase>
52+
<goals>
53+
<goal>single</goal>
54+
</goals>
55+
<configuration>
56+
<archive>
57+
<manifest>
58+
<mainClass>pygraphhopper.Gateway</mainClass>
59+
</manifest>
60+
</archive>
61+
<descriptorRefs>
62+
<descriptorRef>jar-with-dependencies</descriptorRef>
63+
</descriptorRefs>
64+
</configuration>
65+
</execution>
66+
</executions>
67+
</plugin>
68+
</plugins>
69+
70+
</build>
71+
</project>

setup.sh

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
4+
5+
#if [ ! -f indonesia-latest.osm.pbf ]; then
6+
# echo "Downloading the OSM MAP"
7+
# wget http://download.geofabrik.de/asia/indonesia-latest.osm.pbf
8+
#fi
9+
10+
mvn clean
11+
mvn install dependency:copy-dependencies
12+
mvn install
13+
14+
echo "setup done"

src/lib/py4j0.10.5.jar

112 KB
Binary file not shown.
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package pygraphhopper;
2+
3+
import py4j.GatewayServer;
4+
5+
public class Gateway {
6+
7+
public route_calculation getRCObject(String workDir, String vehicle, String osmFile) {
8+
route_calculation rc = new route_calculation(workDir, vehicle, osmFile);
9+
return rc;
10+
}
11+
12+
public static void main(String[] args) {
13+
GatewayServer gatewayServer = new GatewayServer(new Gateway());
14+
gatewayServer.start();
15+
System.out.println("Gateway Server Started!");
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/**
2+
*Created by dariusaudryc on 28/2/17.
3+
*/
4+
5+
package pygraphhopper;
6+
7+
import com.graphhopper.GHRequest;
8+
import com.graphhopper.GHResponse;
9+
import com.opencsv.CSVReader;
10+
import com.opencsv.CSVWriter;
11+
import com.graphhopper.GraphHopper;
12+
import com.graphhopper.reader.osm.GraphHopperOSM;
13+
import com.graphhopper.routing.util.EncodingManager;
14+
import com.graphhopper.PathWrapper;
15+
import java.util.ArrayList;
16+
import java.io.FileReader;
17+
import java.io.FileWriter;
18+
import java.util.List;
19+
import static java.lang.Math.sqrt;
20+
21+
public class route_calculation {
22+
23+
private static GraphHopper graphHopper;
24+
25+
public route_calculation(String workDir, String vehicle, String osmFile) {
26+
// create one GraphHopper instance
27+
graphHopper = new GraphHopperOSM().forServer();
28+
graphHopper.setGraphHopperLocation(workDir); // "when graphhopper is calculating the route, it creates nodes. workDir is a temporary directory to store the nodes"
29+
graphHopper.setEncodingManager(new EncodingManager(vehicle));
30+
graphHopper.setDataReaderFile(osmFile); // "osmFile is the open street map data that allows us to create nodes for routing"
31+
graphHopper.importOrLoad();//The nodes will be called if the nodes had been created in this syntax, else the nodes will be created
32+
}
33+
34+
public static double getDistance(double orig_lat, double orig_lon,
35+
double dest_lat, double dest_lon, String vehicle) {
36+
37+
//System.out.println("Calculating route distance for following lat/long pair:"
38+
// + orig_lat + ", " + orig_lon + " & " + dest_lat + ", " + dest_lon + " " + vehicle);
39+
40+
41+
GHRequest request = new GHRequest(orig_lat, orig_lon, dest_lat, dest_lon);// simple configuration of the request object
42+
request.setWeighting("fastest");
43+
request.setVehicle(vehicle);
44+
45+
try {
46+
GHResponse route = graphHopper.route(request);
47+
PathWrapper path = route.getBest();// use the best path, see the GHResponse class for more possibilities.
48+
return path.getDistance() / 1000;
49+
} catch(Exception e){
50+
//TODO: find a better approach
51+
System.out.println("Exception caught: " + e.getMessage());
52+
System.out.println("Calculating RMS value for following lat/long pair:"
53+
+ orig_lat + ", " + orig_lon + " & " + dest_lat + ", " + dest_lon);
54+
55+
double
56+
dlat = dest_lat - orig_lat,
57+
dlon = dest_lon - orig_lon,
58+
dist2 = dlat * dlat + dlon * dlon,
59+
dist = sqrt(dist2);
60+
return(dist);
61+
}
62+
}
63+
64+
public static void mainHelper(String FileInput, String FileOutput, String originInputLat, String originInputLong,
65+
String destInputLat, String destInputLong, String vehicle) {
66+
67+
68+
CSVReader reader = null;
69+
CSVWriter writer = null;
70+
71+
System.out.println("Going to process file: " + FileInput + "...........\n");
72+
73+
try {
74+
reader = new CSVReader(new FileReader(FileInput));
75+
writer = new CSVWriter(new FileWriter(FileOutput), ',');
76+
} catch (Exception e) {
77+
78+
System.out.println("Arguments are not defined correctly"
79+
+ e.getMessage());
80+
81+
}
82+
83+
String[] entries = null;
84+
Boolean firstRow = true;
85+
String[] header = null;
86+
87+
int originLat = -1;
88+
int originLon = -1;
89+
int destLat = -1;
90+
int destLon = -1;
91+
92+
try {
93+
while ((entries = reader.readNext()) != null) {
94+
95+
List<String> val = new ArrayList<String>();
96+
97+
if (firstRow) {
98+
firstRow = false;
99+
for (int i = 0; i < entries.length; i++) {
100+
if (entries[i].equals(originInputLat)) {
101+
originLat = i;
102+
}
103+
if (entries[i].equals(originInputLong)) {
104+
originLon = i;
105+
}
106+
if (entries[i].equals(destInputLat)) {
107+
destLat = i;
108+
}
109+
if (entries[i].equals(destInputLong)) {
110+
destLon = i;
111+
}
112+
val.add(entries[i]);
113+
}
114+
115+
val.add("customer_route_dist");
116+
String[] new_val = new String[val.size()];
117+
val.toArray(new_val);
118+
119+
writer.writeNext(new_val);
120+
continue;
121+
}
122+
123+
for (int i = 0; i < entries.length; i++)
124+
val.add(entries[i]);
125+
126+
Double mul = new Double(-1.0);
127+
128+
if (originLat != -1 && originLon != -1 && destLat != -1 && destLon != -1) {
129+
mul = getDistance(Double.parseDouble(entries[originLat]), Double.parseDouble(entries[originLon]),
130+
Double.parseDouble(entries[destLat]), Double.parseDouble(entries[destLon]), vehicle);
131+
}
132+
133+
val.add(mul.toString());
134+
135+
String[] new_val = new String[val.size()];
136+
val.toArray(new_val);
137+
writer.writeNext(new_val);
138+
}
139+
140+
} catch (Exception e) {
141+
System.out.println("Error in running the routing function"
142+
+ e.getMessage());
143+
}
144+
try {
145+
writer.close();
146+
} catch (Exception e) {
147+
System.out.println("Error in Writing to csv"
148+
+ e.getMessage());
149+
}
150+
}
151+
152+
//To be used form command line.
153+
public static void main(String[] args) {
154+
155+
String
156+
FileInput = args[0],
157+
FileOutput = args[1],
158+
originInputLat = args[2],
159+
originInputLong = args[3],
160+
destInputLat = args[4],
161+
destInputLong = args[5],
162+
workDir = args[6],
163+
osmFile = args[7],
164+
vehicle = args[8];
165+
166+
mainHelper(FileInput, FileOutput, originInputLat, originInputLong,
167+
destInputLat, destInputLong, vehicle);
168+
169+
}
170+
}

startGateway.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
java -cp ./target/route_distance-1.0-SNAPSHOT.jar:./target/dependency/* pygraphhopper.Gateway

0 commit comments

Comments
 (0)