Commit 669526fd authored by Martin Hellwig's avatar Martin Hellwig Committed by Björn Richerzhagen
Browse files

Added two classes to visualize images as background. One loads...

Added two classes to visualize images as background. One loads background-images at runtime; Also runtime-loading of POIs are now possible
parent 217aec88
......@@ -125,7 +125,7 @@ public class RealWorldStreetsMovement extends AbstractLocalMovementStrategy {
*/
private double[] transformOwnWorldWindowToGPS(double x, double y) {
double[] gps_coordinates = new double[2];
gps_coordinates[0] = latLeft + (latRight - latLeft) * y/worldDimensions.getY();
gps_coordinates[0] = latLeft + (latRight - latLeft) * (worldDimensions.getY() - y)/worldDimensions.getY();
gps_coordinates[1] = lonLeft + (lonRight - lonLeft) * x/worldDimensions.getX();
return gps_coordinates;
}
......@@ -138,7 +138,7 @@ public class RealWorldStreetsMovement extends AbstractLocalMovementStrategy {
*/
private PositionVector transformGPSWindowToOwnWorld(double lat, double lon) {
double x = worldDimensions.getX() * (lon - lonLeft)/(lonRight - lonLeft);
double y = worldDimensions.getY() * (lat - latLeft)/(latRight - latLeft);
double y = worldDimensions.getY() - worldDimensions.getY() * (lat - latLeft)/(latRight - latLeft);
return new PositionVector(x, y);
}
......
......@@ -29,6 +29,8 @@ import java.util.Random;
import java.util.Set;
import java.util.Vector;
import javax.swing.JComponent;
import de.tud.kom.p2psim.api.scenario.ConfigurationException;
import de.tud.kom.p2psim.api.topology.movement.MovementListener;
import de.tud.kom.p2psim.api.topology.movement.MovementModel;
......@@ -39,6 +41,7 @@ import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.Topology;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.IAttractionGenerator;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction.AttractionPoint;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.mapvisualization.IMapVisualization;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.transition.ITransitionStrategy;
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector;
import de.tud.kom.p2psim.impl.util.Either;
......@@ -94,6 +97,8 @@ public class ModularMovementModel implements MovementModel, EventHandler {
protected IAttractionGenerator attractionGenerator;
protected LocalMovementStrategy localMovementStrategy;
protected IMapVisualization mapVisualization;
private Set<MovementListener> movementListeners = new LinkedHashSet<MovementListener>();
......@@ -123,7 +128,10 @@ public class ModularMovementModel implements MovementModel, EventHandler {
if (!initialized) {
VisualizationInjector.injectComponent("AttractionPoints", -1,
new ModularMovementModelViz(this), false);
new ModularMovementModelViz(this), true, false);
VisualizationInjector.injectComponent("Real Map", -2,
(JComponent) mapVisualization, true, false);
checkConfiguration();
......@@ -267,6 +275,10 @@ public class ModularMovementModel implements MovementModel, EventHandler {
public void setITransitionStrategy(ITransitionStrategy transition) {
this.transition = transition;
}
public void setIMapVisualization(IMapVisualization mapVisualization) {
this.mapVisualization = mapVisualization;
}
@Override
public void eventOccurred(Object content, int type) {
......
......@@ -63,10 +63,10 @@ public class ModularMovementModelViz extends JComponent {
// draw border
g2.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(1f));
g2.drawOval(point.x - 15, point.y - 15, 30, 30);
g2.drawOval(point.x - 15, point.y - 15, 20, 20);
g2.drawString(aPoint.getName(), point.x - 15, point.y - 15);
g2.setColor(new Color(0.2f, 0.8f, 0.2f, 0.6f));
g2.fillOval(point.x - 15, point.y - 15, 30, 30);
g2.fillOval(point.x - 15, point.y - 15, 20, 20);
}
}
......
......@@ -58,8 +58,7 @@ public class JSONAttractionGenerator implements IAttractionGenerator {
/**
* You have to set a json-file, which has set some POIs
* Sample-query for "bar"-POIs in Darmstadt (Bounding Box from [49.4813, 8.5590] to [49.9088, 8,7736]:
http://overpass.osm.rambler.ru/cgi/interpreter?data=%5Bout:json%5D;node%5Bamenity=bar%5D%2849%2E4813%2C8%2E5590%2C49%2E9088%2C8%2E7736%29%3Bout%3B
* @param file
http://overpass-api.de/api/interpreter?data=%5Bout:json%5D;node%5Bamenity=bar%5D%2849%2E4813%2C8%2E5590%2C49%2E9088%2C8%2E7736%29%3Bout%3B
*/
public JSONAttractionGenerator() {
this.worldDimensions = Topology.getWorldDimension();
......@@ -74,7 +73,8 @@ public class JSONAttractionGenerator implements IAttractionGenerator {
*/
private PositionVector transformGPSWindowToOwnWorld(double lat, double lon) {
double x = worldDimensions.getX() * (lon - lonLeft)/(lonRight - lonLeft);
double y = worldDimensions.getY() * (lat - latLeft)/(latRight - latLeft);
//Invert the y value, because in Java Swing we start drawing in the upper left corner instead in the lower left one
double y = worldDimensions.getY() - worldDimensions.getY() * (lat - latLeft)/(latRight - latLeft);
return new PositionVector(x, y);
}
......
/*
* 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.modularosm.attraction;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.Topology;
/**
* Generates attraction points out of real data from osm
* The maximal number of attraction points can be set
*
* @author Martin Hellwig
* @version 1.0, 02.07.2015
*/
public class OnlineJSONAttractionGenerator implements IAttractionGenerator {
private PositionVector worldDimensions;
private List<AttractionPoint> attractionPoints;
private int maxNumberOfAttractionPoints;
private String amenity;
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
public OnlineJSONAttractionGenerator() {
this.worldDimensions = Topology.getWorldDimension();
attractionPoints = new LinkedList<AttractionPoint>();
}
/**
* 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);
//Invert the y value, because in Java Swing we start drawing in the upper left corner instead in the lower left one
double y = worldDimensions.getY() - worldDimensions.getY() * (lat - latLeft)/(latRight - latLeft);
return new PositionVector(x, y);
}
@Override
public void setNumberOfAttractionPoints(int numberOfAttractionPoints) {
this.maxNumberOfAttractionPoints = numberOfAttractionPoints;
}
@Override
public List<AttractionPoint> getAttractionPoints() {
if(attractionPoints.size() == 0) {
String poiString = "";
JSONArray allPOI = null;
InputStream in;
try {
in = new URL( "http://overpass-api.de/api/interpreter?data=%5Bout:json%5D;node%5Bamenity=" + amenity + "%5D%28" + latLeft + "%2C" + lonLeft + "%2C" + latRight + "%2C" + lonRight + "%29%3Bout%3B" ).openStream();
poiString = IOUtils.toString(in);
JSONObject poiData = new JSONObject(poiString);
allPOI = poiData.getJSONArray("elements");
} catch (JSONException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(allPOI != null) {
for(int i = 0; i < allPOI.length(); i++) {
try {
String barname = allPOI.getJSONObject(i).getJSONObject("tags").getString("name");
double lat = allPOI.getJSONObject(i).getDouble("lat");
double lon = allPOI.getJSONObject(i).getDouble("lon");
if(lat > latLeft && lat < latRight &&
lon > lonLeft && lon < lonRight) attractionPoints.add(new AttractionPoint(transformGPSWindowToOwnWorld(lat, lon), barname));
}
catch (JSONException e) {
//This bar had no name defined, so there was an error. Not so bad
}
}
}
}
if(maxNumberOfAttractionPoints == 0) maxNumberOfAttractionPoints = Integer.MAX_VALUE;
List<AttractionPoint> result = new LinkedList<AttractionPoint>();
for (int i = 0; (i < attractionPoints.size() && i < maxNumberOfAttractionPoints); i++) {
result.add(attractionPoints.get(i));
}
return result;
}
public void setAmenity(String amenity) {
this.amenity = amenity;
}
public void setLatLeft(double latLeft) {
this.latLeft = latLeft;
}
public void setLatRight(double latRight) {
this.latRight = latRight;
}
public void setLonLeft(double lonLeft) {
this.lonLeft = lonLeft;
}
public void setLonRight(double lonRight) {
this.lonRight = lonRight;
}
}
/*
* 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.modularosm.mapvisualization;
import java.awt.Graphics;
public interface IMapVisualization{
public void paint(Graphics g);
}
/*
* 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.modularosm.mapvisualization;
import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import javax.swing.JComponent;
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector;
public class ShowGoogleMapsMapViz extends JComponent implements IMapVisualization{
private String tempImageFilePath;
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
private boolean initialized = false;
public ShowGoogleMapsMapViz() {
setBounds(0, 0, VisualizationInjector.WORLD_X, VisualizationInjector.WORLD_Y);
setOpaque(true);
setVisible(true);
}
private void initializeImage() {
if(!initialized) {
double centerLat = 0.5*latRight + 0.5*latLeft;
double centerLon = 0.5*lonRight + 0.5*lonLeft;
try {
String imageUrl = "http://maps.google.com/maps/api/staticmap?center=" + centerLat + "," + centerLon + "&zoom=13&size=500x500&scale=2";
URL url = new URL(imageUrl);
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(tempImageFilePath);
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
initialized = true;
}
}
@Override
public void paint(Graphics g) {
initializeImage();
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));
Image img = Toolkit.getDefaultToolkit().getImage(tempImageFilePath);
g2.drawImage(img, 0, 0, this);
}
public void setTempImageFilePath(String tempImageFilePath) {
this.tempImageFilePath = tempImageFilePath;
}
public void setLatLeft(double latLeft) {
this.latLeft = latLeft;
}
public void setLatRight(double latRight) {
this.latRight = latRight;
}
public void setLonLeft(double lonLeft) {
this.lonLeft = lonLeft;
}
public void setLonRight(double lonRight) {
this.lonRight = lonRight;
}
}
/*
* 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.modularosm.mapvisualization;
import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import javax.swing.JComponent;
import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView.VisualizationInjector;
public class ShowImageMapViz extends JComponent implements IMapVisualization{
/*
* 1) To get an appropriate image, you have to go to http://www.mapquestapi.com/staticmap/ and add the bounding box (which you can find in the specification-file in simrunner)
* 2) You also have to define the size of the outcoming image. We work with a 1000x1000 image as standard
* 3) Now you can download the image and get the gps coordinates of the lower left and upper right (check those per hand via google maps)
* 4) change the bounding box values in the specification file to the observed values from 3)
*
* Notes:
* - We have to to this, because the result-image is not the bounding box we had requested
* - you can also use Google Static Maps, Here Maps or something else to retrieve the image
*/
private String imageFilePath;
public ShowImageMapViz() {
setBounds(0, 0, VisualizationInjector.WORLD_X,
VisualizationInjector.WORLD_Y);
setOpaque(true);
setVisible(true);
}
@Override
public void paint(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));
Image img = Toolkit.getDefaultToolkit().getImage(imageFilePath);
g2.drawImage(img, 0, 0, this);
}
public void setImageFilePath(String imageFilePath) {
this.imageFilePath = imageFilePath;
}
}
......@@ -341,42 +341,6 @@ public class SocialTransitionStrategy implements ITransitionStrategy,
return new Vector<AttractionPoint>(favoritePlaces.get(ms));
}
private AttractionPoint getRandomPlace(MovementSupported ms) {
Collections.shuffle(aPoints);
return aPoints.iterator().next();
}
private AttractionPoint getNearestPlace(MovementSupported ms,
List<AttractionPoint> aps) {
if (aps.isEmpty()) {
return null;
}
AttractionPoint nearest = aps.iterator().next();
for (AttractionPoint aPoint : aps) {
if (nearest.getRealPosition().getDistance(ms.getRealPosition()) > aPoint
.getRealPosition().getDistance(ms.getRealPosition())
&& !assignments.get(ms).equals(aPoint)) {
nearest = aPoint;
}
}
return nearest;
}
private AttractionPoint getNearestFavoritePlace(MovementSupported ms) {
Set<AttractionPoint> fps = favoritePlaces.get(ms);
AttractionPoint nearest = fps.iterator().next();
for (AttractionPoint aPoint : fps) {
if (nearest.getRealPosition().getDistance(ms.getRealPosition()) > aPoint
.getRealPosition().getDistance(ms.getRealPosition())
&& !assignments.get(ms).equals(aPoint)) {
nearest = aPoint;
}
}
return nearest;
}
private void assignFavoritePlaces(MovementSupported ms) {
Set<AttractionPoint> msFavoritePlaces = new HashSet<AttractionPoint>();
LinkedList<AttractionPoint> temp = new LinkedList<AttractionPoint>(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment