Search in sources :

Example 1 with OsmConverter

use of uk.me.parabola.mkgmap.reader.osm.OsmConverter in project mkgmap by openstreetmap.

the class StyleTester method runTest.

private static void runTest(String stylefile, String mapfile) {
    PrintingMapCollector collector = new PrintingMapCollector();
    OsmConverter normal;
    try {
        normal = new StyleTester(stylefile, collector, reference);
    } catch (FileNotFoundException e) {
        System.err.println("Could not open style file " + stylefile);
        return;
    }
    try {
        InputStream is = Utils.openFile(mapfile);
        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
        parserFactory.setXIncludeAware(true);
        parserFactory.setNamespaceAware(true);
        SAXParser parser = parserFactory.newSAXParser();
        try {
            EnhancedProperties props = new EnhancedProperties();
            props.put("preserve-element-order", "1");
            ElementSaver saver = new ElementSaver(props);
            OsmXmlHandler handler = new OsmXmlHandler();
            SaxHandler saxHandler = handler.new SaxHandler();
            handler.setElementSaver(saver);
            parser.parse(is, saxHandler);
            saver.finishLoading();
            saver.convert(normal);
            System.err.println("Conversion time " + (System.currentTimeMillis() - collector.getStart()) + "ms");
        } 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);
    } catch (FileNotFoundException e) {
        System.err.println("Cannot open file " + mapfile);
    }
}
Also used : InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) SaxHandler(uk.me.parabola.mkgmap.reader.osm.xml.OsmXmlHandler.SaxHandler) FormatException(uk.me.parabola.imgfmt.FormatException) SAXException(org.xml.sax.SAXException) EnhancedProperties(uk.me.parabola.util.EnhancedProperties) ElementSaver(uk.me.parabola.mkgmap.reader.osm.ElementSaver) SAXParser(javax.xml.parsers.SAXParser) OsmConverter(uk.me.parabola.mkgmap.reader.osm.OsmConverter) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) OsmXmlHandler(uk.me.parabola.mkgmap.reader.osm.xml.OsmXmlHandler) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 2 with OsmConverter

use of uk.me.parabola.mkgmap.reader.osm.OsmConverter in project mkgmap by openstreetmap.

the class StyleTester method runSimpleTest.

/**
 * Run a simple test with a combined test file.
 * @param filename The test file contains text way definitions and a style
 * file all in one.
 */
public static void runSimpleTest(String filename) {
    try {
        FileReader reader = new FileReader(filename);
        BufferedReader br = new BufferedReader(reader);
        List<Way> ways = readSimpleTestFile(br);
        List<String> givenList = readGivenResults();
        boolean noStrict = false;
        if (!givenList.isEmpty() && Objects.equals(givenList.get(0), "NO-STRICT")) {
            givenList.remove(0);
            noStrict = true;
        }
        List<MapElement> strictResults = new ArrayList<>();
        List<MapElement> results = new ArrayList<>();
        List<String> actual = new ArrayList<>();
        List<String> expected = new ArrayList<>();
        for (Way w : ways) {
            OsmConverter normal = new StyleTester("styletester.style", new LocalMapCollector(results), false);
            String prefix = "WAY " + w.getId() + ": ";
            normal.convertWay(w.copy());
            normal.end();
            actual.addAll(formatResults(prefix, results));
            results.clear();
            if (!noStrict) {
                OsmConverter strict = new StyleTester("styletester.style", new LocalMapCollector(strictResults), true);
                strict.convertWay(w.copy());
                strict.end();
                expected.addAll(formatResults(prefix, strictResults));
                strictResults.clear();
            }
        }
        printResult("", actual);
        if (!noStrict && !Objects.equals(actual, expected)) {
            out.println("ERROR expected result is:");
            printResult("|", expected);
        }
        if ((!givenList.isEmpty() || forceUseOfGiven) && !Objects.equals(actual, givenList)) {
            out.println("ERROR given results were:");
            printResult("", givenList);
        }
    } catch (FileNotFoundException e) {
        System.err.println("Cannot open test file " + filename);
    } catch (IOException e) {
        System.err.println("Failure while reading test file " + filename);
    }
}
Also used : MapElement(uk.me.parabola.mkgmap.general.MapElement) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Way(uk.me.parabola.mkgmap.reader.osm.Way) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) OsmConverter(uk.me.parabola.mkgmap.reader.osm.OsmConverter)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 OsmConverter (uk.me.parabola.mkgmap.reader.osm.OsmConverter)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXParser (javax.xml.parsers.SAXParser)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 SAXException (org.xml.sax.SAXException)1 FormatException (uk.me.parabola.imgfmt.FormatException)1 MapElement (uk.me.parabola.mkgmap.general.MapElement)1 ElementSaver (uk.me.parabola.mkgmap.reader.osm.ElementSaver)1 Way (uk.me.parabola.mkgmap.reader.osm.Way)1 OsmXmlHandler (uk.me.parabola.mkgmap.reader.osm.xml.OsmXmlHandler)1 SaxHandler (uk.me.parabola.mkgmap.reader.osm.xml.OsmXmlHandler.SaxHandler)1 EnhancedProperties (uk.me.parabola.util.EnhancedProperties)1