RealWorldStreetsMovement.java 8.77 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package de.tud.kom.p2psim.impl.topology.movement.local;

23
import java.util.HashMap;
24
import java.util.Locale;
25
import java.util.UUID;
26
27
28
29
30
31

import com.graphhopper.GHRequest;
import com.graphhopper.GHResponse;
import com.graphhopper.GraphHopper;
import com.graphhopper.routing.util.EncodingManager;
import com.graphhopper.util.PointList;
32
import com.graphhopper.util.shapes.GHPoint;
33
34
import com.graphhopper.util.shapes.GHPoint3D;

35
import de.tud.kom.p2psim.api.topology.Topology;
36
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
37
import de.tud.kom.p2psim.impl.topology.PositionVector;
38
import de.tud.kom.p2psim.impl.topology.movement.modularosm.GPSCalculation;
39
40
import de.tud.kom.p2psim.impl.util.Either;
import de.tud.kom.p2psim.impl.util.Left;
41
import de.tudarmstadt.maki.simonstrator.api.Binder;
42
import de.tudarmstadt.maki.simonstrator.api.Monitor;
43
import de.tudarmstadt.maki.simonstrator.api.Monitor.Level;
44
45
46
47
48
49
50
51
52
53
54
55
56

/**
 * This movement strategy uses the data from osm and navigates the nodes throught streets to the destination
 * 
 * @author Martin Hellwig
 * @version 1.0, 07.07.2015
 */
public class RealWorldStreetsMovement extends AbstractLocalMovementStrategy {
	
	private PositionVector worldDimensions;
	private GraphHopper hopper;
	private boolean init = false;
	
57
	private static HashMap<SimLocationActuator, RealWorldMovementPoints> movementPoints = new HashMap<>();
58
	
59
60
61
	private String osmFileLocation; //use pbf-format, because osm-format causes problems (xml-problems)
	private String graphFolderFiles;
	private String movementType; //car, bike or foot
Clemens Krug's avatar
Clemens Krug committed
62
	private String defaultMovement;
63
	private String navigationalType; //fastest,
64
65
66
67
	private double latLeft; //Values from -90 to 90; always smaller than latRight
	private double latRight; //Values from -90 to 90
	private double lonLeft; //Values from -180 to 180; Always smaller than lonRight
	private double lonRight; //Values from -180 to 180
68
	private boolean uniqueFolders;
69

70
71
72
73
74
75
	/**
	 * Tolerance in meters (if the node reached a waypoint up to "tolerance"
	 * meters, it will select the next waypoint in the path.
	 */
	private double tolerance = 1;

76
	public RealWorldStreetsMovement() {
77
78
		this.worldDimensions = Binder.getComponentOrNull(Topology.class)
				.getWorldDimensions();
79
80
81
82
		latLeft = GPSCalculation.getLatLower();
		latRight = GPSCalculation.getLatUpper();
		lonLeft = GPSCalculation.getLonLeft();
		lonRight = GPSCalculation.getLonRight();
83
84
85
86
87
88
	}
	
	private void init() {
		hopper = new GraphHopper().forServer();
		hopper.setOSMFile(osmFileLocation);
		// where to store graphhopper files?
89
90
91
92
93
94
95
96
97
98
		if (uniqueFolders) {
			Monitor.log(RealWorldStreetsMovement.class, Level.WARN,
					"Using per-simulation unique folders for GraphHopper temporary data in %s. Remember to delete them to prevent your disk from filling up.",
					graphFolderFiles);
			hopper.setGraphHopperLocation(graphFolderFiles + "/"
					+ UUID.randomUUID().toString());
		} else {
			hopper.setGraphHopperLocation(graphFolderFiles + "/"
					+ osmFileLocation.hashCode() + movementType);
		}
99
100
101
102
103
		hopper.setEncodingManager(new EncodingManager(movementType));
		hopper.importOrLoad();
		init = true;
	}

104
105
    public Either<PositionVector, Boolean> nextPosition(SimLocationActuator comp, PositionVector destination)
    {
Clemens Krug's avatar
Clemens Krug committed
106
        return nextPosition(comp, destination, defaultMovement);
107
108
    }

109
	public Either<PositionVector, Boolean> nextPosition(SimLocationActuator comp,
110
			PositionVector destination, String movementType) {
Clemens Krug's avatar
Clemens Krug committed
111
112
113
114
115

		if(movementType == null || movementType.equals("") || !this.movementType.contains(movementType))
			throw new AssertionError("Invalid movement type: " + movementType);


116
        if(!init) init();
117
		PositionVector newPosition = null;
118
        if (destination
Björn Richerzhagen's avatar
Björn Richerzhagen committed
119
				.distanceTo(comp.getRealPosition()) > getMovementSpeed(comp)) {
120
121
			//if not set already for this node or new destination is different than last one
			PointList pointList;
Björn Richerzhagen's avatar
Björn Richerzhagen committed
122
			if(!movementPoints.containsKey(comp) || destination.distanceTo(movementPoints.get(comp).getDestination()) > 1.0) {
123
124
125
126
127
128
129
				double[] startPosition = transformOwnWorldWindowToGPS(comp.getRealPosition().getX(), comp.getRealPosition().getY());
				double[] destinationPosition = transformOwnWorldWindowToGPS(destination.getX(), destination.getY());
				GHRequest req = new GHRequest(startPosition[0], startPosition[1], destinationPosition[0], destinationPosition[1]).
					    setWeighting(navigationalType).
					    setVehicle(movementType).
					    setLocale(Locale.GERMANY);
				GHResponse rsp = hopper.route(req);
130
131
				//If the requested point is not in the map data, simple return the destination as next point
				if(rsp.hasErrors()) {
132
133
134
					Monitor.log(this.getClass(), Monitor.Level.ERROR, "Routing request for Host %s with starting point (" +
							"%f,%f) and destination (%f,%f) failed with error: %s.", comp.getHost().getId().valueAsString(),startPosition[0], startPosition[1],
							destinationPosition[0], destinationPosition[1], rsp.getErrors());
135
136
137
					
					pointList = new PointList();
					pointList.add(new GHPoint(destination.getLatitude(), destination.getLongitude()));
Björn Richerzhagen's avatar
Björn Richerzhagen committed
138
					movementPoints.put(comp, new RealWorldMovementPoints(comp.getRealPosition(), destination, pointList, 0));
139
140
141
				}
				else {
					pointList = rsp.getPoints();
Björn Richerzhagen's avatar
Björn Richerzhagen committed
142
					movementPoints.put(comp, new RealWorldMovementPoints(comp.getRealPosition(), destination, pointList, 0));
143
				}
144
145
			}
			else {
Björn Richerzhagen's avatar
Björn Richerzhagen committed
146
				pointList = movementPoints.get(comp).getPointList();
147
			}
148
			
Björn Richerzhagen's avatar
Björn Richerzhagen committed
149
			int actualIndex = movementPoints.get(comp).getActualIndex();
150
			int i = 0;
151
			for(GHPoint3D temp : pointList) {
152
153
154
155
				if(i==actualIndex) {
					PositionVector nextPoint = transformGPSWindowToOwnWorld(temp.getLat(), temp.getLon());
					newPosition = comp.getRealPosition().moveStep(nextPoint, getMovementSpeed(comp));
					
156
157
158
159
					if (nextPoint
							.distanceTo(comp.getRealPosition()) < tolerance) {
						actualIndex++;
					}
160
161
				}
				i++;
162
163
			}
			
Björn Richerzhagen's avatar
Björn Richerzhagen committed
164
			movementPoints.put(comp, new RealWorldMovementPoints(movementPoints.get(comp).getStart(), destination, pointList, actualIndex));
165
166
167
168
169
170
171
172
173
174
175
176
		}
		return new Left<PositionVector, Boolean>(newPosition);
	}
	
	/**
	 * Projects the world coordinates in the given gps window to the gps-coordinates
	 * @param x
	 * @param y
	 * @return The projected position in gps-coordinates (lat, long)
	 */
	private double[] transformOwnWorldWindowToGPS(double x, double y) {
		double[] gps_coordinates = new double[2];
177
		gps_coordinates[0] = latLeft + (latRight - latLeft) * (worldDimensions.getY() - y)/worldDimensions.getY();
178
179
180
181
182
183
184
185
186
187
188
189
		gps_coordinates[1] = lonLeft + (lonRight - lonLeft) * x/worldDimensions.getX();
		return gps_coordinates;
	}
	
	/**
	 * Projects the gps coordinates in the given gps window to the world-coordinates given in world-dimensions
	 * @param lat
	 * @param lon
	 * @return The projected position in world-dimensions
	 */
	private PositionVector transformGPSWindowToOwnWorld(double lat, double lon) {
		double x = worldDimensions.getX() * (lon - lonLeft)/(lonRight - lonLeft);
190
		double y = worldDimensions.getY() - worldDimensions.getY() * (lat - latLeft)/(latRight - latLeft);
191
192
193
194
		x = Math.max(0, x);
		x = Math.min(worldDimensions.getX(), x);
		y = Math.max(0, y);
		y = Math.min(worldDimensions.getY(), y);
195
196
197
198
199
200
201
202
203
204
205
206
207
		return new PositionVector(x, y);
	}
	
	public void setOsmFileLocation(String osmFileLocation) {
		this.osmFileLocation = osmFileLocation;
	}

	public void setGraphFolderFiles(String graphFolderFiles) {
		this.graphFolderFiles = graphFolderFiles;
	}

	public void setMovementType(String movementType) {
		this.movementType = movementType;
Clemens Krug's avatar
Clemens Krug committed
208
		defaultMovement = movementType.split(",")[0];
209
210
211
212
213
	}

	public void setNavigationalType(String navigationalType) {
		this.navigationalType = navigationalType;
	}
214
	
215
216
	public void setWaypointTolerance(double tolerance) {
		this.tolerance = tolerance;
217
	}
218

219
220
221
222
223
	/**
	 * For large batch simulations, we need to prevent same-time access to
	 * garphhopper temp data. Therefore, this flag creates unique folders for
	 * each run (which, obviously, wastes a lot of space and comp-resources and
	 * should not be used in standalone, single-threaded demo mode...)
224
	 *
225
226
227
228
229
	 * @param uniqueFolders
	 */
	public void setCreateUniqueFolders(boolean uniqueFolders) {
		this.uniqueFolders = uniqueFolders;
	}
230
}