Commit fdcb71f8 authored by Martin Hellwig's avatar Martin Hellwig
Browse files

Changed from BoundingBox to Center-Location and Zoomlevel; Caching of GMaps-loaded images

parent 01999f17
......@@ -33,6 +33,7 @@ import com.graphhopper.util.shapes.GHPoint3D;
import de.tud.kom.p2psim.api.topology.movement.MovementSupported;
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.GPSCalculation;
import de.tud.kom.p2psim.impl.util.Either;
import de.tud.kom.p2psim.impl.util.Left;
......@@ -63,6 +64,10 @@ public class RealWorldStreetsMovement extends AbstractLocalMovementStrategy {
public RealWorldStreetsMovement() {
this.worldDimensions = Topology.getWorldDimension();
latLeft = GPSCalculation.getLatLower();
latRight = GPSCalculation.getLatUpper();
lonLeft = GPSCalculation.getLonLeft();
lonRight = GPSCalculation.getLonRight();
}
private void init() {
......@@ -157,22 +162,6 @@ public class RealWorldStreetsMovement extends AbstractLocalMovementStrategy {
public void setNavigationalType(String navigationalType) {
this.navigationalType = navigationalType;
}
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;
}
public void setMaxDistanceNextPoint(double maxDistanceNextPoint) {
this.maxDistanceNextPoint = maxDistanceNextPoint;
......
/*
* 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;
public class GPSCalculation {
private static double latCenter;
private static double lonCenter;
private static int zoom;
private static double scaleFactor;
public GPSCalculation() {
}
private void setScaleFactor() {
this.scaleFactor = Math.pow(2.0d, (13-zoom));
}
public static double getLatCenter() {
return latCenter;
}
public static double getLonCenter() {
return lonCenter;
}
public static int getZoom() {
return zoom;
}
public static double getLatUpper() {
return latCenter + scaleFactor*0.027613;
}
public static double getLatLower() {
return latCenter - scaleFactor*0.027613;
}
public static double getLonLeft() {
return lonCenter - scaleFactor*0.0419232;
}
public static double getLonRight() {
return lonCenter + scaleFactor*0.0419232;
}
public void setLatCenter(double latCenter) {
this.latCenter = latCenter;
}
public void setLonCenter(double lonCenter) {
this.lonCenter = lonCenter;
}
public void setZoom(int zoom) {
this.zoom = zoom;
setScaleFactor();
}
}
......@@ -33,6 +33,7 @@ import org.json.JSONObject;
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.GPSCalculation;
/**
* Generates attraction points out of real data from osm
......@@ -63,6 +64,11 @@ public class JSONAttractionGenerator implements IAttractionGenerator {
public JSONAttractionGenerator() {
this.worldDimensions = Topology.getWorldDimension();
attractionPoints = new LinkedList<AttractionPoint>();
latLeft = GPSCalculation.getLatLower();
latRight = GPSCalculation.getLatUpper();
lonLeft = GPSCalculation.getLonLeft();
lonRight = GPSCalculation.getLonRight();
}
/**
......@@ -129,20 +135,4 @@ public class JSONAttractionGenerator implements IAttractionGenerator {
public void setPlacementJsonFile(String placementJsonFile) {
this.placementJsonFile = placementJsonFile;
}
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;
}
}
......@@ -34,6 +34,7 @@ import org.json.JSONObject;
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.GPSCalculation;
/**
* Generates attraction points out of real data from osm
......@@ -59,6 +60,11 @@ public class OnlineJSONAttractionGenerator implements IAttractionGenerator {
public OnlineJSONAttractionGenerator() {
this.worldDimensions = Topology.getWorldDimension();
attractionPoints = new LinkedList<AttractionPoint>();
latLeft = GPSCalculation.getLatLower();
latRight = GPSCalculation.getLatUpper();
lonLeft = GPSCalculation.getLonLeft();
lonRight = GPSCalculation.getLonRight();
}
/**
......@@ -127,20 +133,4 @@ public class OnlineJSONAttractionGenerator implements IAttractionGenerator {
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;
}
}
......@@ -26,6 +26,8 @@ import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
......@@ -34,15 +36,12 @@ import java.net.URL;
import javax.swing.JComponent;
import de.tud.kom.p2psim.impl.topology.movement.modularosm.GPSCalculation;
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;
......@@ -54,27 +53,37 @@ public class ShowGoogleMapsMapViz extends JComponent implements IMapVisualizatio
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);
tempImageFilePath = tempImageFilePath +
"googlemaps" +
GPSCalculation.getLatCenter() +
GPSCalculation.getLonCenter() +
GPSCalculation.getZoom() + ".jpg";
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();
}
//Check if the file with same properties (same location) already exists
File f = new File(tempImageFilePath);
if(!(f.exists() && !f.isDirectory())) {
try {
String imageUrl = "http://maps.google.com/maps/api/staticmap?center=" +
GPSCalculation.getLatCenter() + "," +
GPSCalculation.getLonCenter() + "&zoom=" +
GPSCalculation.getZoom() + "&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;
}
}
......@@ -88,27 +97,33 @@ public class ShowGoogleMapsMapViz extends JComponent implements IMapVisualizatio
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);
Image imageToDraw = resize(Toolkit.getDefaultToolkit().getImage(tempImageFilePath), VisualizationInjector.WORLD_X, VisualizationInjector.WORLD_Y);
g2.drawImage(imageToDraw , 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;
}
/**
* Resizes the given image to the given width and height
*
* @param originalImage
* @param width
* @param height
*/
private Image resize(Image originalImage, int width, int height) {
int type = BufferedImage.TYPE_INT_ARGB;
BufferedImage resizedImage = new BufferedImage(width, height, type);
Graphics2D g = resizedImage.createGraphics();
public void setLonLeft(double lonLeft) {
this.lonLeft = lonLeft;
}
g.setComposite(AlphaComposite.Src);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
public void setLonRight(double lonRight) {
this.lonRight = lonRight;
g.drawImage(originalImage, 0, 0, width, height, this);
g.dispose();
return resizedImage;
}
}
/*
* 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;
}
}
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