/
Modeling.java
149 lines (120 loc) · 5.26 KB
/
Modeling.java
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
34
35
36
37
38
39
40
41
42
43
44
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
79
80
81
82
83
84
85
86
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
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jcan2;
import java.util.ArrayList;
import org.openstreetmap.gui.jmapviewer.Coordinate;
/**
* Class responsible for the pollution modelling.
* @author Micha
*/
public class Modeling {
private boolean rendered = false;
private OsmAPI api = new OsmAPI();
private ArrayList<Segment> segments = new ArrayList<>();
private static final boolean MODEL_DEBUG = true;
private static final int MIN_POINTS_SEGMENT = 10;
public Modeling(){
}
public boolean isRendered(){return rendered;}
public void render(ArrayList<DataPoint> pointList) throws Exception{
if(!rendered){
//render
//save bar values
int barMax = BeMapEditor.mainWindow.getBarMax();
int barVal = BeMapEditor.mainWindow.getBarVal();
//status
BeMapEditor.mainWindow.setBarMax(pointList.size());
Segment lastSegment = new Segment();
OsmNode lastNode = new OsmNode();
boolean firstSegmentCreated = false;
int position = 0;
for (DataPoint dp : pointList) {
ArrayList<OsmNode> nodes = api.getOSMSegments(dp.lat(),dp.lon());
if(nodes.size()>=2){
//find two nearest points, create segment if it doesn't exist yet
//todo: check if segment exists
OsmNode nearest = findNearest(new Coordinate(dp.lat(),dp.lon()),nodes);
if(nearest.isInit()){
if(!firstSegmentCreated){
//create first segment
lastNode = nearest;
lastSegment = new Segment(nearest,dp);
firstSegmentCreated = true;
if(MODEL_DEBUG) BeMapEditor.mainWindow.append("\nFirst Segment created");
}
else{
//check if next point is different
if(OsmNode.equals(nearest,lastNode) || lastSegment.getPointNumber() < MIN_POINTS_SEGMENT){
//no --> add to existing segment
lastSegment.addDataPoint(dp);
if(MODEL_DEBUG) BeMapEditor.mainWindow.append("\nPoint added to existing Segment");
}
else{
//yes --> end segment, start new one
lastSegment.segmentEnd(nearest, dp);
segments.add(lastSegment);
lastSegment = new Segment(nearest,dp);
lastNode = nearest;
if(MODEL_DEBUG) BeMapEditor.mainWindow.append("\nNew Segment created");
}
}
}
else {
if(MODEL_DEBUG) BeMapEditor.mainWindow.append("\nSegment ignored");
}
}
else{
//else ignore points or put them in front of Federal Palace
lastSegment.addDataPoint(dp);
if(MODEL_DEBUG) BeMapEditor.mainWindow.append("\nPoint added to existing Segment");
}
//adapt status bar
Thread.sleep(1);
BeMapEditor.mainWindow.setBar(position);
position++;
}
//close last segment
if(lastSegment.getPointNumber()>MIN_POINTS_SEGMENT){
lastSegment.segmentEnd(lastNode,pointList.get(pointList.size()-1));
segments.add(lastSegment);
}
else lastSegment = null; //will be deleted by garbage collector
BeMapEditor.mainWindow.getMap().removeAllMapMarkers();
//restitue memory bar
BeMapEditor.mainWindow.setBar(barVal);
BeMapEditor.mainWindow.setBarMax(barMax);
}
}
/**
* Returns only the nearest two nodes, where the point with lat and lon lies inbetween.
* @param lat
* @param lon
* @param allNodes
* @return
*/
private OsmNode findNearest(Coordinate c, ArrayList<OsmNode> allNodes){
OsmNode near = new OsmNode();
for (OsmNode n : allNodes) {
if(calculateDistance(n.getCoord(),c) < calculateDistance(near.getCoord(),c)){
//replace the nearest node
near=n;
}
}
ArrayList<OsmNode> finalList = new ArrayList<>();
return near;
}
/**
* Calculates the distance between 2 coordinates in decimal degrees.
* @param c1 Coordinate 1
* @param c2 Coordinate 2
* @return
*/
private double calculateDistance(Coordinate c1, Coordinate c2){
double dLon = c1.getLat()-c2.getLat();
double dLat = c1.getLon()-c2.getLon();
return Math.sqrt(dLon*dLon+dLat*dLat);
}
}