PanierAvide/BasicOSMParser

map library

sonador3255 opened this issue · 3 comments

Hello PanierAvide,
I want to use your java library in my NetBeans program I wrote this code but have some errors that
"can not fine symbol class map" and " can not find symbol calss IOException" and "can not find sambol class SAXException"?
Did I do something?

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import info.pavie.basicosmparser.controller.OSMParser;
import info.pavie.basicosmparser.BasicOSMParser;
import info.pavie.basicosmparser.controller.TestOSMParser;
import info.pavie.basicosmparser.model.Element;
import info.pavie.basicosmparser.model.Node;
import info.pavie.basicosmparser.model.Relation;
import info.pavie.basicosmparser.model.Way;

public class ReadOSMfile {


    public static void main(String[] args) {
        OSMParser p = new OSMParser();                      //Initialization of the parser
File osmFile = new File("/path/to/your/data.osm");  //Create a file object for your OSM XML file

try {

Map<String,Element> result = p.parse(osmFile);      //Parse OSM data, and put result in a Map         object

} catch (IOException | SAXException e) {
e.printStackTrace();                                //Input/output errors management
}
    // TODO code application logic here
}

}

Hello,
You seem to have missed some imports :

import org.xml.sax.SAXException;
import java.io.IOException;

Try with them and tell me if it's working ;)

Hi thank you,
but one question:
your library each map have string and element:

public abstract class Element {
protected long id;
protected String user;
protected long uid;
protected String timestamp;
protected boolean visible;
protected int version;
protected long changeset;
protected Map<String, String> tags;

in your map when the element is way just of it and its attribute keep in map, but the tag didn't keep.is it right?
I want to know that way was contained what node or the relation was contained what member, is it possible?

I'm not sure to understand what you're meaning. The result Map contains all read elements from OSM XML file, and strings keys are OSM IDs (with the first letter the kind of object).
To have the list of nodes a way contains, you have getNodes() method in Way class. To have the list of elements a relation contains, you have getMembers() method in Relation class. To access these methods you should cast your Element object to Way or Relation like that :

Map<String,Element> result = p.parse(osmFile);      //Parse OSM data, and put result in a Map object

for(Element e : result.values()) {
    if(e.getClass().equals(Way.class)) {
        List<Node> nodes = ((Way) e).getNodes();
        // do stuff
    }
    else if(e.getClass().equals(Relation.class)) {
        List<Element> members = ((Relation) e).getMembers();
         // do stuff
    }
    else {
        // do stuff
    }
}

Don't hesitate to reformulate your question if needed ;)