Subversion Repositories mkgmap

Rev

Rev 1752 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Copyright (C) 2006 Steve Ratcliffe
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *
 * Author: Steve Ratcliffe
 * Create date: 16-Dec-2006
 */

package uk.me.parabola.mkgmap.reader.osm.xml;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import uk.me.parabola.imgfmt.FormatException;
import uk.me.parabola.imgfmt.Utils;
import uk.me.parabola.mkgmap.reader.osm.OsmMapDataSource;

import org.xml.sax.SAXException;


/**
 * Read an OpenStreetMap data file in .osm version 0.5 format.  It is converted
 * into a generic format that the map is built from.
 * <p>The intermediate format is important as several passes are required to
 * produce the map at different zoom levels. At lower resolutions, some roads
 * will have fewer points or won't be shown at all.
 *
 * @author Steve Ratcliffe
 */

public class Osm5MapDataSource extends OsmMapDataSource {

        public boolean isFileSupported(String name) {
                // This is the default format so say supported if we get this far,
                // this one must always be last for this reason.
                return true;
        }

        /**
         * Load the .osm file and produce the intermediate format.
         *
         * @param name The filename to read.
         * @throws FileNotFoundException If the file does not exist.
         */

        public void load(String name) throws FileNotFoundException, FormatException {
                try {
                        InputStream is = Utils.openFile(name);
                        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
                        parserFactory.setXIncludeAware(true);
                        parserFactory.setNamespaceAware(true);
                        SAXParser parser = parserFactory.newSAXParser();

                        try {
                                Osm5XmlHandler handler = new Osm5XmlHandler(getConfig());
                                Osm5XmlHandler.SaxHandler saxHandler = handler.new SaxHandler();

                                setupHandler(handler);

                                // parse the xml file
                                parser.parse(is, saxHandler);
                                osmReadingHooks.end();

                                elementSaver.finishLoading();
                                // now convert the saved elements
                                elementSaver.convert(getConverter());

                                addBackground();

                        } catch (IOException e) {
                                throw new FormatException("Error reading file", e);
                        }
                } catch (SAXException e) {
                        throw new FormatException("Error parsing file", e);
                } catch (ParserConfigurationException e) {
                        throw new FormatException("Internal error configuring xml parser", e);
                }
        }
}