CsvPlacement.java 4.56 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
/*
 * 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.placement;

import java.awt.geom.Point2D;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Vector;

import de.tud.kom.p2psim.api.topology.TopologyComponent;
import de.tud.kom.p2psim.api.topology.placement.PlacementModel;
import de.tud.kom.p2psim.impl.network.modular.common.GeoToolkit;
33
34
import de.tud.kom.p2psim.impl.topology.util.ExtendedPositionVector;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
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
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;

/**
 * Read positions from a CSV-file
 * 
 * @author Bjoern Richerzhagen
 * @version 1.0, 29.03.2012
 */
public class CsvPlacement implements PlacementModel {

	private List<PositionVector> positions;

	private final String SEP = ";";

	private int numberOfComponents = 0;

	private int positionIndex = 0;

	private PositionVector world;

	private String file;

	/**
	 * 
	 * @param filename
	 */
	@XMLConfigurableConstructor({ "file" })
	public CsvPlacement(String file) {
		positions = new Vector<PositionVector>();
		this.file = file;
	}

	@Override
	public void addComponent(TopologyComponent comp) {
		numberOfComponents++;
		if (world == null) {
			world = comp.getTopology().getWorldDimensions();
		}
	}

	@Override
	public PositionVector place(TopologyComponent comp) {
		if (positions.isEmpty() || positionIndex >= positions.size()) {
			readData();
			positionIndex = 0;
		}
		PositionVector vec = positions.get(positionIndex);
		positionIndex++;
		return vec;
	}

	private void readData() {
		positions.clear();
		boolean entrySuccessfullyRead = false;
		BufferedReader csv = null;
		try {
			csv = new BufferedReader(new FileReader(file));

			while (csv.ready()) {
				String line = csv.readLine();

				if (line.indexOf(SEP) > -1) {
					String[] parts = line.split(SEP);

					if (parts.length == 2) {
						try {
							Double lon = Double.parseDouble(parts[0]);
							Double lat = Double.parseDouble(parts[1]);

							// Reference Point Dijon in France
							Point2D.Double XY = GeoToolkit.transformToXY(
									GeoToolkit.STANDARD_REF_POINT_GERMANY,
									new Point2D.Double(lon, lat));
							
//							System.out.println(lon + "," + lat + "-->" + XY.getX() + "," +  XY.getY());

							if (XY.getX() > world.getX() || XY.getY() > world.getY() || XY.getX() < 0
									|| XY.getY() < 0) {
113
114
//								 System.err.println("Skipped entry " + x + ";"
//								 + y);
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
164
165
166
167
168
169
170
								continue;
							}

							positions.add(new ExtendedPositionVector(XY.getX(), XY.getY(), lon, lat));
							entrySuccessfullyRead = true;
						} catch (NumberFormatException e) {
							// Ignore leading comments
							if (entrySuccessfullyRead) {
								// System.err.println("CSV ParseError " + line);
							}
						}
					} else if (parts.length == 4) {
						try {
							Double x = Double.parseDouble(parts[0]);
							Double y = Double.parseDouble(parts[1]);
							// in case we need the lon und lat for later
							// plotting
							Double lon = Double.parseDouble(parts[2]);
							Double lat = Double.parseDouble(parts[3]);

							if (x > world.getX() || y > world.getY() || x < 0
									|| y < 0) {
								// System.err.println("Skipped entry " + x + ";"
								// + y);
								continue;
							}

							positions.add(new ExtendedPositionVector(x, y, lon,
									lat));
							entrySuccessfullyRead = true;
						} catch (NumberFormatException e) {
							// Ignore leading comments
							if (entrySuccessfullyRead) {
								// System.err.println("CSV ParseError " + line);
							}
						}
					} else {
						throw new AssertionError("To many columns in CSV.");
					}
				}
			}

		} catch (Exception e) {
			System.err.println(e.toString());
		} finally {
			if (csv != null) {
				try {
					csv.close();
				} catch (IOException e) {
					//
				}
			}
		}
	}

}