0

I'm trying to retrieve speed limits using OSM/Overpass. The XML below is returned in a file called "interpreter" after querying the Overpass API. I cannot retrieve the "maxspeed" or "60" tag from this xml using Android.

Could anyone offer any advice or point me in the right direction as I can't find any tutorials.

There are no errors when the file is run.

XML:

 <?xml version="1.0" encoding="UTF-8"?>
    <osm version="0.6" generator="Overpass API">

    <node id="768053039" lat="54.9526671" lon="-7.7273348"/>
  <node id="768053040" lat="54.9498094" lon="-7.7176056"/>
  <node id="768053041" lat="54.9497066" lon="-7.7173174"/>
  <node id="768053043" lat="54.9495658" lon="-7.7170937"/>
  <node id="768053044" lat="54.9495035" lon="-7.7169816"/>
  <node id="791492493" lat="54.9494183" lon="-7.7168205"/>
  <node id="795319854" lat="54.9510427" lon="-7.7218262"/>
  <node id="795320324" lat="54.9509153" lon="-7.7213706"/>
  <node id="1922546572" lat="54.9502165" lon="-7.7190169"/>
  <node id="1922546679" lat="54.9504739" lon="-7.7199078"/>
  <node id="1922546692" lat="54.9500860" lon="-7.7185174"/>
  <node id="1922602861" lat="54.9517250" lon="-7.7241644"/>
  <node id="1922622063" lat="54.9514357" lon="-7.7231690"/>
  <node id="2673934802" lat="54.9498543" lon="-7.7177617"/>
  <way id="64273241">
    <nd ref="768053039"/>
    <nd ref="1922602861"/>
    <nd ref="1922622063"/>
    <nd ref="795319854"/>
    <nd ref="795320324"/>
    <tag k="highway" v="secondary"/>
    <tag k="maxspeed" v="60"/>
    <tag k="name" v="Port Road"/>
    <tag k="oneway" v="no"/>
    <tag k="ref" v="R229"/>
  </way>
  <way id="64887990">
    <nd ref="795320324"/>
    <nd ref="1922546679"/>
    <nd ref="1922546572"/>
    <nd ref="1922546692"/>
    <nd ref="2673934802"/>
    <nd ref="768053040"/>
    <nd ref="768053041"/>
    <nd ref="768053043"/>
    <nd ref="768053044"/>
    <nd ref="791492493"/>
    <tag k="highway" v="secondary"/>
    <tag k="maxspeed" v="60"/>
    <tag k="name" v="Port Road"/>
    <tag k="oneway" v="no"/>
    <tag k="ref" v="R229"/>
  </way>

</osm>

Java:

//Location listener
class mylocationListener implements LocationListener
{

    @Override
    public void onLocationChanged(Location location) {
        if(location != null)
        {
            //Get position
            double lon = location.getLongitude();
            double lat = location.getLatitude();
            txtLong.setText("Longitude: " +lon);
            txtLat.setText("Latitude: " + lat);

            //Get speed
            double speed = location.getSpeed();
            txtSpeed.setText(speed + " km/h");

            //Send request to recieve information regarding Speed zones everytime there is a change in location
            //sendRequest(lat+"", lon+"");
            //sendGetRequest(lat+"", lon+"");
            try {
                readXML2(lat+"", lon+"");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }




public void readXML2(String lat, String lon) throws IOException, ParserConfigurationException, SAXException
{

    //InputStream input = getDataViaOverpass(query2);

    try {
        String query = "http://overpass-api.de/api/interpreter?data=way(around:40,"
                +lon+","+lat+")" + " [\"highway\"] [\"maxspeed\"]; ( ._; >; ); out;";

        String query2 = "?data=way(around:40,"
                +lon+","+lat+")" + " [\"highway\"] [\"maxspeed\"]; ( ._; >; ); out;";

        String hostname = OVERPASS_API;

        URL osm = new URL(hostname);
        HttpURLConnection connection = (HttpURLConnection) osm.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        //connection.setReadTimeout(2*60*1000);//2 minute timeout

        DataOutputStream printout = new DataOutputStream(connection.getOutputStream());
        printout.writeBytes("data=" + URLEncoder.encode(query, "utf-8"));
        printout.flush();
        printout.close();

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        org.w3c.dom.Document document;
        document = documentBuilder.parse(connection.getInputStream());

        Element rootElement = document.getDocumentElement();

        NodeList nodeNodeList = rootElement.getElementsByTagName("way");

        for (int i = 0; i < nodeNodeList.getLength(); i++) {

            Node nNode = nodeNodeList.item(i);

            String limit = nNode.getAttributes().getNamedItem("maxspeed").getNodeValue();
            TextView curLimitText = (TextView)findViewById(R.id.curLimitTxt);
            curLimitText.setText(limit +" km/h");


            System.out.println(nNode.getAttributes().getNamedItem("maxspeed").getNodeValue());
            System.out.println(nNode.getAttributes().getNamedItem("lon").getNodeValue());

        }

    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}
Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
  • 1
    Why are you posting so much irrelevant code? You don't have to post your whole activity. Just post the relevant code. – greenapps Mar 09 '15 at 20:49
  • Just giving as much information as I can. – user3572739 Mar 09 '15 at 20:59
  • Wrong. That is no extra information. Concentrate on your problem and dont ask us to read all kind of code that has nothing to do with your problem. – greenapps Mar 09 '15 at 21:03
  • You should try to add debugging messages to your code. For example print all attributes of the nodes you are looking at. That will help you try to understand what is going on and what you are doing wrong. – scai Mar 10 '15 at 17:18
  • When I debugged and stepped over each line, it didn't get as far as the for loop containing the nodes. Not sure why – user3572739 Mar 10 '15 at 17:53
  • Then what is the return value of `nodeNodeList.getLength()`? – scai Mar 12 '15 at 20:29
  • In debug it gets as far as "document = documentBuilder.parse...." and skips to "catch (IOException e). "e.printStackTrace()" reports that the exception is that the File is not found. Any suggestions? – user3572739 Mar 15 '15 at 14:01

0 Answers0