venerdì 27 gennaio 2012

Java - How to parse KML files

Parse KML files in Java with ximpleware lib:

package readkmlfile;

import com.ximpleware.AutoPilot;
import com.ximpleware.NavException;
import com.ximpleware.VTDGen;
import com.ximpleware.VTDNav;
import com.ximpleware.XPathEvalException;
import com.ximpleware.XPathParseException;

/**
 * @author Marco Berri marcoberri@gmail.com 
 * @see http://tecnicume.blogspot.com
 */
public class ReadKmlFile {

    /**
     * @param args the command line arguments
     * @throws XPathEvalException
     * @throws NavException  
     */
    public static void main(String[] args) throws XPathEvalException, NavException {

        VTDGen vg = new VTDGen();
        vg.parseFile("/Users/marco/Desktop/test.kml", true);

        System.out.println("Start");


        AutoPilot ap;
        VTDNav vn = vg.getNav();


        try {
            vn.toElement(VTDNav.ROOT);
            vn.matchElement("kml");
            ap = new AutoPilot(vn);
            ap.selectXPath("/kml/Placemark/*");

        } catch (XPathParseException ex) {
            System.out.println(ex.getMessage());
            return;
        } catch (NavException ex) {
            System.out.println(ex.getMessage());
            return;
        }


        int r = 0;
        while ((r = ap.evalXPath()) != -1) {
            System.out.println(vn.toString(r));

            if (vn.toString(r).equals("LineString")) {


                VTDNav vnls = vn.cloneNav();

                if (vnls.toElement(VTDNav.FC)) {

                    do {


                        if (vnls.toString(vnls.getCurrentIndex()).equals("coordinates")) {
                            String coordinates = vnls.toString(vnls.getText());

                            String[] lines = coordinates.split("\\n");

                            for (String l : lines) {
                                if (l.length() == 0) {
                                    continue;
                                }

                                String[] block = l.split(",");
                                System.out.println("lat:" + block[0].trim());
                                System.out.println("lon:" + block[1].trim());
                                System.out.println("ele:" + block[2].trim());
                            }


                        }

                    } while (vnls.toElement(VTDNav.NEXT_SIBLING));

                }

            }

        }

    }
}

giovedì 26 gennaio 2012

Java - How to parse GPX files

Parse GPX files in Java with ximpleware lib:




package readgpxfile;

import com.ximpleware.AutoPilot;
import com.ximpleware.NavException;
import com.ximpleware.VTDGen;
import com.ximpleware.VTDNav;
import com.ximpleware.XPathEvalException;
import com.ximpleware.XPathParseException;

/**
 * @author Marco Berri marcoberri@gmail.com 
 * @see http://tecnicume.blogspot.com
 */
public class ReadGpxFile {

    /**
     * @param args the command line arguments
     * @throws XPathEvalException
     * @throws NavException  
     */
    public static void main(String[] args) throws XPathEvalException, NavException {


        VTDGen vg = new VTDGen();
        vg.parseFile("/Users/marco/Desktop/test.gpx", true);

        System.out.println("Start");


        AutoPilot ap;
        VTDNav vn = vg.getNav();


        try {
            vn.toElement(VTDNav.ROOT);
            vn.matchElement("gpx");
            ap = new AutoPilot(vn);
            ap.selectXPath("/gpx/trk/trkseg/*");

        } catch (XPathParseException ex) {
            System.out.println(ex.getMessage());
            return;
        } catch (NavException ex) {
            System.out.println(ex.getMessage());
            return;
        }

        int r = 0;
        while ((r = ap.evalXPath()) != -1) {


            if (vn.toString(r).equals("trkpt")) {
                System.out.println("lat:" + vn.getAttrVal("lat"));
                System.out.println("lon:" + vn.getAttrVal("lon"));

                VTDNav vntrkp = vn.cloneNav();

                if (vntrkp.toElement(VTDNav.FC)) {

                    do {


                        if (vntrkp.toString(vntrkp.getCurrentIndex()).equals("ele")) {
                            System.out.println("ele:" + vntrkp.toString(vntrkp.getText()));
                        } else if (vntrkp.toString(vntrkp.getCurrentIndex()).equals("fix")) {
                            System.out.println("fix:" + vntrkp.toString(vntrkp.getText()));
                        } else if (vntrkp.toString(vntrkp.getCurrentIndex()).equals("sat")) {
                            System.out.println("sat:" + vntrkp.toString(vntrkp.getText()));
                        } else if (vntrkp.toString(vntrkp.getCurrentIndex()).equals("time")) {
                            System.out.println("time:" + vntrkp.toString(vntrkp.getText()));
                        }

                    } while (vntrkp.toElement(VTDNav.NEXT_SIBLING));

                }


            }
        }

    }
}


mercoledì 11 gennaio 2012

Spring MVC + MongoDB


Primo approccio con il framework Spring 3.1, integrazione tra MVC e MongoDB.

Per installare MongoDB basta scaricarlo, ed eseguire mongod dopo avere precedentemente creato la cartella \data\db.

In questo progetto di netbeans  ho voluto utilizzare il più possibile le annotation  per ridurre al minimo il codice java da scrivere.

Da quello che ho capito esiste ancora qualche problema con il GridFs nell'integrazione con Spring, ma sarà il prossimo passo.


link utili: