JSONAttractionGenerator.java 4.88 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
26
27
28
29
30
31
32
33
/*
 * 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.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
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;

34
import de.tud.kom.p2psim.api.topology.Topology;
35
import de.tud.kom.p2psim.impl.topology.movement.modularosm.GPSCalculation;
36
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
37
import de.tudarmstadt.maki.simonstrator.api.Binder;
38
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.AttractionPoint;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

/**
 * 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 JSONAttractionGenerator implements IAttractionGenerator {

	private PositionVector worldDimensions;

	private List<AttractionPoint> attractionPoints;
	
	private String placementJsonFile;
	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

	/**
60
61
	 * 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]:
62
		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	 
63
64
	 */
	public JSONAttractionGenerator() {
65
66
		this.worldDimensions =  Binder.getComponentOrNull(Topology.class)
				.getWorldDimensions();
67
		attractionPoints = new LinkedList<AttractionPoint>();
68
69
70
71
72
		
		latLeft = GPSCalculation.getLatLower();
		latRight = GPSCalculation.getLatUpper();
		lonLeft = GPSCalculation.getLonLeft();
		lonRight = GPSCalculation.getLonRight();
73
74
75
76
77
78
79
80
81
82
	}

	/**
	 * 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);
83
84
		//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);
85
86
		return new PositionVector(x, y);
	}
Björn Richerzhagen's avatar
Björn Richerzhagen committed
87
	
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
	@Override
	public List<AttractionPoint> getAttractionPoints() {
		if(attractionPoints.size() == 0) {
			String poiString = "";
			JSONArray allPOI = null;
			FileInputStream inputStream;
			try {
				inputStream = new FileInputStream(placementJsonFile);
				poiString = IOUtils.toString(inputStream);
				JSONObject poiData = new JSONObject(poiString);
				allPOI = poiData.getJSONArray("elements");
			} catch (FileNotFoundException e) {
				e.printStackTrace();
		    } catch (IOException e) {
				e.printStackTrace();
			} catch (JSONException e) {
				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");
113
						AttractionPointImpl ap;
114
						if(lat > latLeft && lat < latRight &&
115
116
117
118
119
120
121
								lon > lonLeft && lon < lonRight) {
							ap = new AttractionPointImpl(barname, transformGPSWindowToOwnWorld(lat, lon));
							attractionPoints.add(ap);
							// the following is allowed to fail.
							ap.setWeight(allPOI.getJSONObject(i).getJSONObject("tags").getDouble("weight"));
							ap.setRadius(allPOI.getJSONObject(i).getJSONObject("tags").getDouble("radius"));
						}
122
123
124
125
126
127
					}
					catch (JSONException e) {
						//This bar had no name defined, so there was an error. Not so bad
					}
				}
			}
128
		}				
129
		
Björn Richerzhagen's avatar
Björn Richerzhagen committed
130
		return attractionPoints;
131
132
133
134
135
136
	}
	
	public void setPlacementJsonFile(String placementJsonFile) {
		this.placementJsonFile = placementJsonFile;
	}
}