MapLoader.java 2.86 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
23
24
25
/*
 * 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.util.geo.maps;

import com.google.common.collect.Maps;

import de.tud.kom.p2psim.api.util.geo.maps.Map;
26
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
27
28
29
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView;
import de.tud.kom.p2psim.impl.util.geo.maps.AbstractMap.Axis;
import de.tud.kom.p2psim.impl.util.geo.maps.osm.OSMMap;
30
31
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.Monitor.Level;
32
33
34
35
36
37
38
39
40
41
42
import de.tudarmstadt.maki.simonstrator.api.component.Component;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;

public class MapLoader implements Component {
	private java.util.Map<String, Map> maps = Maps.newHashMap();
	private PositionVector dimensions = null;
	
	@XMLConfigurableConstructor({"worldX", "worldY"})
	public MapLoader(double worldX, double worldY) {
		dimensions = new PositionVector(worldX, worldY);

43
44
		VisualizationTopologyView.VisualizationInjector
				.setWorldCoordinates((int) worldX, (int) worldY);
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
	}

    /**
     * Returns the dimensions the maps are scaled to. This should match the
     * visual world size configured in the visualization topology.
     *
     * @return
     */
    public PositionVector getScaledDimensions() {
        return dimensions;
    }
	
	public void setMap(Map map) {
		maps.put(map.getName(), map);
	}
	
	public Map getMap(String name) {
		Map map = maps.get(name);
		
		if (map == null)
			return null;
		
		if (!map.isLoaded()) {
			map.loadMap();
			
			configureToScenario(map);
		}
		
		return map;
	}

	private void configureToScenario(Map map) {
		// Map the longitude and latitude to width and height of the simulated world 
		if (map instanceof AbstractMap) {
79
80
			Monitor.log(MapLoader.class, Level.INFO, "Map " + map.getName()
					+ " is being scaled to " + dimensions);
81
82
83
84
85
86
87
88
89
			((AbstractMap)map).mapToWorld(dimensions);
		}
		if (map instanceof OSMMap) {
			if (!((AbstractMap)map).isSwapped(Axis.Y_AXIS)) {
				((AbstractMap)map).swapWorld(Axis.Y_AXIS, dimensions.getY());
			}
		}
	}
}