Hi to all of you,
i am new at the whole OSM thing and from Germany. I am a student and 26 years old. At the moment i am writing a programm, that compares gps tracks with the osm data. Therefor i want to extract the nodes, the latitudes and longitudes forum a .osm file which i want to save in a list of “wegpunkt”. The wegpunkt.java looks like this:
public class Wegpunkt {
public double x;
public double y;
public double altitude;
public double distance;
public Wegpunkt(double x1, double y1, double altitude1, double dist1) {
this.x = x1;
this.y = y1;
this.altitude = altitude1;
this.distance = dist1;
}
public Wegpunkt(){
this.x = 0;
this.y = 0;
this.altitude = 0;
this.distance = 0;
}
public double getAbstand(Wegpunkt p) {
double ergebnis = Math.sqrt((p.x-x)*(p.x-x) + (p.y-y)*(p.y-y));
double test = p.distance - distance;
return ergebnis;
}
public double getx(){
return this.x;
}
@Override
public String toString(){
String s = "";
s+= "x: " + this.x;
s+= " y: " + this.y;
s+= " hoehe: " + this.altitude;
s+= " distance: " + this.distance;
return s;
}
public static void main(String[] args) {
double a = 1;
double b = 2;
double c = 2;
double d = 2;
Wegpunkt w = new Wegpunkt(a, b, c, d );
System.out.println(w.toString());
}
}
I tried a lot the last 2 days and the result is not that good.
I have two other classes:
package Osmparser;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
public class Main {
public static void main(String[] args) {
try {
// XMLReader erzeugen
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
// Pfad zur XML Datei
FileReader reader = new FileReader("E:/Uni Hannover/projektarbeit/Eilenriede.osm");
InputSource inputSource = new InputSource(reader);
// DTD kann optional übergeben werden
// inputSource.setSystemId("X:\\personen.dtd");
// PersonenContentHandler wird übergeben
xmlReader.setContentHandler(new PersonenContentHandler());
// Parsen wird gestartet
xmlReader.parse(inputSource);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
}
and
package Osmparser;
import parser.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
public class PersonenContentHandler implements ContentHandler {
private ArrayList<Wegpunkt> alleWegpunkte = new ArrayList<Wegpunkt>();
private String currentValue;
private Wegpunkt wegpunkt;
// Aktuelle Zeichen die gelesen werden, werden in eine Zwischenvariable
// gespeichert
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
currentValue = new String(ch, start, length);
}
// Methode wird aufgerufen wenn der Parser zu einem Start-Tag kommt
@Override
public void startElement(String uri, String localName, String qName,
Attributes atts) throws SAXException {
if (localName.equals("node")) {
System.out.println("anfang erreicht");
// Neuer Wegpunkt erzeugt
wegpunkt = new Wegpunkt();
}
}
// Methode wird aufgerufen wenn der Parser zu einem End-Tag kommt
public void endElement(String uri, String localName, String qName)
throws SAXException {
// latitude setzen
if (localName.equals("lon")) {
System.out.println("Lon set");
double d = Double.parseDouble(currentValue);
wegpunkt.setX(d);
}
// longitutde setzen
if (localName.equals("lon")) {
System.out.println("Lon set");
double d = Double.parseDouble(currentValue);
wegpunkt.setY(d);
}
// Wegpunkt in Wegpunktliste abspeichern falls Wegpunkt End-Tag erreicht
// wurde.
if (localName.equals("node")) {
alleWegpunkte.add(wegpunkt);
System.out.println("ende erreicht");
}
}
public void endDocument() throws SAXException {}
public void endPrefixMapping(String prefix) throws SAXException {}
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {}
public void processingInstruction(String target, String data)
throws SAXException {}
public void setDocumentLocator(Locator locator) { }
public void skippedEntity(String name) throws SAXException {}
public void startDocument() throws SAXException {}
public void startPrefixMapping(String prefix, String uri)
throws SAXException {}
}
The osm i want to parse looks like:
<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="CGImap 0.0.2" copyright="OpenStreetMap and contributors" attribution="http://www.openstreetmap.org/copyright" license="http://opendatacommons.org/licenses/odbl/1-0/">
<bounds minlat="52.3727000" minlon="9.7523000" maxlat="52.3988000" maxlon="9.7913000"/>
<node id="21306291" lat="52.3734343" lon="9.7892360" user="BurnyB" uid="83876" visible="true" version="7" changeset="7753029" timestamp="2011-04-03T11:38:56Z"/>
<node id="25753554" lat="52.3820175" lon="9.7661638" user="Langlaeufer" uid="34953" visible="true" version="15" changeset="6542608" timestamp="2010-12-04T19:46:58Z">
<tag k="highway" v="traffic_signals"/>
<tag k="TMC:cid_58:tabcd_1:Class" v="Point"/>
<tag k="TMC:cid_58:tabcd_1:Direction" v="both"/>
<tag k="TMC:cid_58:tabcd_1:LCLversion" v="9.00"/>
<tag k="TMC:cid_58:tabcd_1:LocationCode" v="47541"/>
<tag k="TMC:cid_58:tabcd_1:NextLocationCode" v="47542"/>
<tag k="TMC:cid_58:tabcd_1:PrevLocationCode" v="47540"/>
</node>
<node id="25753555" lat="52.3783739" lon="9.7682939" user="Langlaeufer" uid="34953" visible="true" version="4" changeset="6542608" timestamp="2010-12-04T19:46:40Z"/>
<node id="25753557" lat="52.3783062" lon="9.7630994" user="Kurt Krampmeier" uid="41889" visible="true" version="2" changeset="9639237" timestamp="2011-10-23T23:41:57Z"/>
<node id="25753558" lat="52.3778321" lon="9.7681972" user="Langlaeufer" uid="34953" visible="true" version="6" changeset="6548958" timestamp="2010-12-05T10:57:01Z">
<tag k="access" v="request"/>
<tag k="highway" v="traffic_signals"/>
<tag k="tactile_paving" v="no"/>
<tag k="traffic_signals:sound" v="no"/>
<tag k="traffic_signals:vibration" v="no"/>
<tag k="wheelchair" v="yes"/>
</node>
<node id="25753559" lat="52.3767687" lon="9.7684331" user="Langlaeufer" uid="34953" visible="true" version="3" changeset="6542608" timestamp="2010-12-04T19:46:30Z"/>
<node id="25753560" lat="52.3779688" lon="9.7689394" user="Langlaeufer" uid="34953" visible="true" version="4" changeset="6550852" timestamp="2010-12-05T13:47:28Z"/>
<node id="25753561" lat="52.3769012" lon="9.7693910" user="Langlaeufer" uid="34953" visible="true" version="5" changeset="6542608" timestamp="2010-12-04T19:46:33Z">
<tag k="TMC:cid_58:tabcd_1:Class" v="Point"/>
<tag k="TMC:cid_58:tabcd_1:Direction" v="negative"/>
<tag k="TMC:cid_58:tabcd_1:LCLversion" v="9.00"/>
<tag k="TMC:cid_58:tabcd_1:LocationCode" v="47542"/>
<tag k="TMC:cid_58:tabcd_1:NextLocationCode" v="47543"/>
<tag k="TMC:cid_58:tabcd_1:PrevLocationCode" v="47541"/>
</node>
...
Maybe i am totally at the wrong way, because running the main.java do not give a result for the latitude and longitude values.
So, i heard that osmosis is a good tool for osm parsing. Do you say its easier to restart programming using this? and, if so, how do i do that?
Greetings and excuses for the bad english
Matti