/
OsmAPI.java
148 lines (105 loc) · 5.12 KB
/
OsmAPI.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
/*
* 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.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import org.json.JSONObject;
import javax.xml.parsers.*;
import org.openstreetmap.gui.jmapviewer.MapMarkerDot;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
/**
*
* @author Micha
*/
public class OsmAPI {
//0.0001 is a good value (about 5m)
private static final double NODE_SEARCH_RADIUS = 0.0003;
private final String USER_AGENT = "Mozilla/5.0";
private final String DOMAIN = "http://api.openstreetmap.org";
private final boolean SERVER_DEBUG = false;
public OsmAPI(){
}
public ArrayList<OsmNode> getOSMSegments(double lat, double lon) throws Exception {
ArrayList<OsmNode> OsmNodeList = new ArrayList<>();
double left = lon - NODE_SEARCH_RADIUS;
double bottom = lat - NODE_SEARCH_RADIUS;
double right = lon + NODE_SEARCH_RADIUS;
double top = lat + NODE_SEARCH_RADIUS;
String url = DOMAIN + "/api/0.6/map?bbox="
+String.format( "%.10f", left )+","+String.format( "%.10f", bottom )+
","+String.format( "%.10f", right )+","+String.format( "%.10f", top );
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
if(SERVER_DEBUG) BeMapEditor.mainWindow.append("Sending 'GET' request to URL : " + url + "\n");
if(SERVER_DEBUG) BeMapEditor.mainWindow.append("Response Code : " + responseCode + "\n");
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
String xml = response.toString();
//if(SERVER_DEBUG) BeMapEditor.mainWindow.append(xml);
Document doc = loadXMLFromString(xml);
NodeList nList = doc.getElementsByTagName("node");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
//System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
//System.out.println(eElement.getAttribute("id"));
//System.out.println(eElement.getAttribute("lat"));
//System.out.println(eElement.getAttribute("lon"));
NodeList childList = eElement.getChildNodes();
for(int i = 0; i < childList.getLength();i++){
Node childNode = childList.item(i);
if (childNode.getNodeType() == Node.ELEMENT_NODE) {
Element child = (Element) childNode;
if("highway".equals(child.getAttribute("k"))){
double lat1 = Double.parseDouble(eElement.getAttribute("lat"));
double lon1 = Double.parseDouble(eElement.getAttribute("lon"));
long id = Long.parseLong(eElement.getAttribute("id"));
OsmNode s = new OsmNode(id,lat1,lon1);
OsmNodeList.add(s);
//for debug
/*
MapMarkerDot m = new MapMarkerDot(lat1,lon1);
BeMapEditor.mainWindow.getMap().addMapMarker(m);
m.setVisible(true);
*/
}
}
}
}
}
return OsmNodeList;
}
public static Document loadXMLFromString(String xml) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
return builder.parse(is);
}
}