OSMReader.java 8 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
34
35
36
37
38
39
40
/*
 * 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.util.geo.maps.osm;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import de.tud.kom.p2psim.api.scenario.ConfigurationException;
import de.tud.kom.p2psim.api.util.geo.maps.Node;
import de.tud.kom.p2psim.api.util.geo.maps.Way;
41
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
42
43
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.Monitor.Level;
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

public class OSMReader extends DefaultHandler {
	private static HashMap<String, OSMMap> loadedMaps = new HashMap<String, OSMMap>();
	
	private OSMMap map;
	private STATE state = STATE.INITIAL;
	private Vector<Long> idList = new Vector<Long>();
	private String name;
	private boolean boundsFound = false;
	private boolean firstNode = true;
	private HashMap<String, String> options = new HashMap<String, String>();
	private Long nodeId;
	
	public OSMReader() {
		map = new OSMMap();
	}
	
	public OSMReader(OSMMap osmMap) {
		this.map = osmMap;
	}

	public OSMMap getMap() {
		return map;
	}

	public static void loadMap(String filename, OSMMap map) {
		File osmFile = new File(filename);
		
		if (!osmFile.exists())
			throw new ConfigurationException("Couldn't find OSM file: " + filename);
		
		try {
	    	
			SAXParserFactory parserFactory = SAXParserFactory.newInstance();
		    SAXParser parser;
			parser = parserFactory.newSAXParser();

		    OSMReader reader = new OSMReader(map);
		    
		    File mapFile = new File(filename);
		    InputStream mapIn = new FileInputStream(mapFile);
		    
		    if (filename.endsWith(".bz2")) {
		    	mapIn = new BZip2CompressorInputStream(mapIn);
		    }
		    
			parser.parse(mapIn, reader);
			
			reader.postProcessing();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void postProcessing() {
		removeOutOfBoundNodes(map);
	}
	
	public void removeOutOfBoundNodes(OSMMap map) {			
		Iterator<Way> iter = map.getWays().iterator();

		int size = map.getWays().size();
		
		OSMWay way;
		while (iter.hasNext()) {
			way = (OSMWay)iter.next();
			
			for (Node node : way.getNodes()) {
				if (node == null || !checkBounds(map, node.getPosition())) {
					iter.remove();
					break;
				}
			}
		}

120
121
		Monitor.log(OSMReader.class, Level.INFO, "Removed "
				+ (size - map.getWays().size()) + " of " + size + " ways.");
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
	}

    private boolean checkBounds(OSMMap map, PositionVector pos) {
    	if (pos.getX() > map.getMaxPosition().getX()) return false;
    	if (pos.getY() > map.getMaxPosition().getY()) return false;
    	
    	if (pos.getX() < map.getMinPosition().getX()) return false;
    	if (pos.getY() < map.getMinPosition().getY()) return false;
    	
    	return true;
    }
	
	private static enum STATE {
		INITIAL,
		NODES,
		WAYS;
	}
	
	@Override
	public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
		if (state.equals(STATE.WAYS)) {
    		if ("nd".equals(qName)) {
    			idList.add(Long.parseLong(attrs.getValue("ref")));
    		} else if ("tag".equals(qName)) {
    			if (attrs.getValue("k").equals("name")) {
    				name = attrs.getValue("v");
    			}

				options.put(attrs.getValue("k"), attrs.getValue("v"));
    		}
    	} else if (state.equals(STATE.NODES)) {
    		if ("tag".equals(qName)) {
				options.put(attrs.getValue("k"), attrs.getValue("v"));
    		}
    	} else {
	    	if ("node".equals(qName)) {
	    		UTM utmCoord = UTMConversion.latLon2UTM(Double.parseDouble(attrs.getValue("lat")),
	    				Double.parseDouble(attrs.getValue("lon")));
	    		
	    		PositionVector nodePosition = utmCoord.getPosition();
	    		
	    		if (!boundsFound) {
	    			PositionVector minLonLat = map.getMinPosition();
	    			PositionVector maxLonLat = map.getMaxPosition();
	    			
	    			if (firstNode) {
	    				firstNode = false;
	    				minLonLat.replace(nodePosition);
	    				maxLonLat.replace(nodePosition);
	    			} else {
		    			if (nodePosition.getX() < minLonLat.getX()) minLonLat.setEntry(0, nodePosition.getX());
		    			if (nodePosition.getY() < minLonLat.getY()) minLonLat.setEntry(1, nodePosition.getY());

		    			if (nodePosition.getX() > maxLonLat.getX()) maxLonLat.setEntry(0, nodePosition.getX());
		    			if (nodePosition.getY() > maxLonLat.getY()) maxLonLat.setEntry(1, nodePosition.getY());

		    			map.setMinPosition(minLonLat);
		    			map.setMaxPosition(maxLonLat);
	    			}
	    		}
	    		
	    		nodeId = Long.parseLong(attrs.getValue("id"));

	    		options = new HashMap<String, String>();
	    		
	    		map.addNode(nodeId, nodePosition);

	    		state = STATE.NODES;
	    		
	    	} else if ("way".equals(qName)) {
	    		if (!boundsFound) {
	    			// Assume that all nodes come in front of the way definitions and make
	    			// the bounds final at this point to get a consistent map
	    			boundsFound = true;
	    		}

	    		idList = new Vector<Long>();
	    		options = new HashMap<String, String>();
	    		state = STATE.WAYS;
	    	} else if ("bounds".equals(qName)) {
	    		boundsFound = true;
	    		
	    		//<bounds minlat="49.8634100" minlon="8.6321500" maxlat="49.8837700" maxlon="8.6746000"/>
	    		
	    		UTM minUtmCoord = UTMConversion.latLon2UTM(Double.parseDouble(attrs.getValue("minlat")),
	    				Double.parseDouble(attrs.getValue("minlon")));
	    		
	    		UTM maxUtmCoord = UTMConversion.latLon2UTM(Double.parseDouble(attrs.getValue("maxlat")),
	    				Double.parseDouble(attrs.getValue("maxlon")));
	    		
	    		map.setMinPosition(minUtmCoord.getPosition());
	    		map.setMaxPosition(maxUtmCoord.getPosition());
	    	}
    	}
	}

	@Override
    public void endElement(String uri, String localName, String qName) throws SAXException {		
		if ("way".equals(qName)) {
    		state = STATE.INITIAL;
    		map.addWay(name, idList, options);
    	} else if ("node".equals(qName)) {
    		state = STATE.INITIAL;

    		map.getNode(nodeId).setAttributes(options);

    	}
	}
	
	public static void main(String... args) {
		OSMMap map = null;
		
		File osmFile = new File(args[0]);
		
		if (!osmFile.exists())
			throw new ConfigurationException("Couldn't find OSM file: " + args[0]);
		
		try {
	    	
			SAXParserFactory parserFactory = SAXParserFactory.newInstance();
		    SAXParser parser;
			parser = parserFactory.newSAXParser();

		    OSMReader reader = new OSMReader();
			parser.parse(osmFile, reader);
			
			reader.postProcessing();

			map = reader.getMap();

			System.out.println("Filename: " + args[0]);
			System.out.println("maxlat: " + map.getMaxPosition().getY());
			System.out.println("minlon: " + map.getMinPosition().getY());
			System.out.println("maxlat: " + map.getMaxPosition().getX());
			System.out.println("minlon: " + map.getMinPosition().getX());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}