JSONAttractionGenerator.java 5.46 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.topology.movement.modularosm.attraction;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
26
import java.util.LinkedList;
27
28
29
30
import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
31
import de.tud.kom.p2psim.impl.topology.movement.modularosm.GPSCalculation;
32
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
33
34
35
36
37
38
39

/**
 * 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
40
41
42
43
44
 * 
 * Added an upper limit for the number of attraction points. Use 0 to allow all APs in the JSON file. 
 * @author Julian Zobel
 * @version 1.1, November 2018
 * 
45
46
47
48
 * You now can set an upper limit for the radius.
 * @author Julian Zobel
 * @version 1.2, January 2019
 *  
49
 * 
50
 */
51
public class JSONAttractionGenerator extends AbstractAttractionProvider {
52

53
54
	protected int numberOfAttractionPoints;
	protected String placementJsonFile = "";
Julian Zobel's avatar
Julian Zobel committed
55

56
	protected double maximumRadius = -1; // Values >= 0, or -1 if radius taken from file
57
	
58
	/**
59
60
	 * 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]:
61
		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	 
62
	 */
63
64
	@XMLConfigurableConstructor({"numberOfAttractionPoints", "maximumRadius", "placementJsonFile"})
	public JSONAttractionGenerator(int numberOfAttractionPoints, double maximumRadius, String placementJsonFile) {						
Julian Zobel's avatar
Julian Zobel committed
65
		this.numberOfAttractionPoints = numberOfAttractionPoints;		
66
67
68
69
		this.maximumRadius = maximumRadius;
		this.placementJsonFile = placementJsonFile;		
		
		createAttractionPoints();
70
	}
Björn Richerzhagen's avatar
Björn Richerzhagen committed
71
	
72
	@Override
73
	public LinkedList<AttractionPoint> getAttractionPoints() {
74
75
		if(super.getAttractionPoints().size() == 0) {
			createAttractionPoints();
76
		}
77
		
78
		return super.getAttractionPoints();
79
	}
80
81
82
	
	protected JSONArray getPOIArray() {
		assert !placementJsonFile.equals("");
83
		
84
85
86
87
88
89
90
91
92
93
94
95
96
97
		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();
98
		}
99
100
		
		return allPOI;
101
	}
102
	
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
	private void createAttractionPoints() {
		assert super.getAttractionPoints().size() == 0;
		
		JSONArray allPOI = getPOIArray();
		
		if(allPOI == null) {
			throw new UnsupportedOperationException("[JSONAttractionGenerator] POI Data Array cannot be NULL.");
		}
			
		int limit = 0;
		if(numberOfAttractionPoints == 0 || numberOfAttractionPoints > allPOI.length()) {
			limit = allPOI.length();
		}
		else {
			limit = numberOfAttractionPoints;
		}
		
		for(int i = 0; i < limit; 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");
				AttractionPoint ap;
				
				// check that the point is within the simulation boundaries
				if(GPSCalculation.isWithinGPSBoundaries(lat, lon)) {
					
					// initialize the attraction point with basic information, will be filled now...
					ap = new AttractionPoint(barname, GPSCalculation.transformGPSWindowToOwnWorld(lat, lon));
																																
					// the following setters are allowed to fail
					// AP weight
					if(allPOI.getJSONObject(i).getJSONObject("tags").has("weight")) {
						ap.setWeight(allPOI.getJSONObject(i).getJSONObject("tags").getDouble("weight"));
					}
					
					// AP radius
					if(allPOI.getJSONObject(i).getJSONObject("tags").has("radius")) {
						double radius = allPOI.getJSONObject(i).getJSONObject("tags").getDouble("radius");
						
						if(maximumRadius == -1) {
							ap.setRadius(radius);
						}
						else {
							ap.setRadius(Math.min(maximumRadius, radius));
						}
					}
					
					if(allPOI.getJSONObject(i).getJSONObject("tags").has("pauseTimeMin") && allPOI.getJSONObject(i).getJSONObject("tags").has("pauseTimeMax")) {
						ap.setPauseTime( allPOI.getJSONObject(i).getJSONObject("tags").getLong("pauseTimeMin"),  allPOI.getJSONObject(i).getJSONObject("tags").getLong("pauseTimeMax"));
					}		
					
					addAttractionPoint(ap);
				}
			}
			catch (JSONException e) {
				//This bar had no name defined, so there was an error. Not so bad
				System.out.println(e);
			}					
		}			
	}	
164
}