Search in sources :

Example 1 with FormatException

use of uk.me.parabola.imgfmt.FormatException in project mkgmap by openstreetmap.

the class MapMaker method makeMap.

public String makeMap(CommandArgs args, String filename) {
    try {
        LoadableMapDataSource src = loadFromFile(args, filename);
        sort = args.getSort();
        if (createOverviewFiles) {
            if (src.overviewMapLevels() != null) {
                makeMap(args, src, OverviewBuilder.OVERVIEW_PREFIX);
            } else {
                String fname = OverviewBuilder.getOverviewImgName(args.getMapname());
                File f = new File(fname);
                if (f.exists()) {
                    if (f.isFile())
                        f.delete();
                    else {
                    // TODO: error message ?
                    }
                }
            }
        }
        return makeMap(args, src, "");
    } catch (FormatException e) {
        System.err.println("Bad file format: " + filename);
        System.err.println(e.getMessage());
        return filename;
    } catch (FileNotFoundException e) {
        System.err.println("Could not open file: " + filename);
        return filename;
    }
}
Also used : LoadableMapDataSource(uk.me.parabola.mkgmap.general.LoadableMapDataSource) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FormatException(uk.me.parabola.imgfmt.FormatException)

Example 2 with FormatException

use of uk.me.parabola.imgfmt.FormatException 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 3 with FormatException

use of uk.me.parabola.imgfmt.FormatException in project mkgmap by openstreetmap.

the class BoundaryUtil method loadQuadTreeFromStream.

/**
 * Create and fill a BoundaryQuadTree. Read the header of the stream to detect
 * the proper reading routine for the different supported formats.
 * @param stream an already opened InputStream
 * @param fname the file name of the corresponding *.bnd file
 * @param searchBbox a bounding box or null. If not null, area info outside of this
 * bounding box is ignored.
 * @param props properties to be used or null
 * @return on success it returns a new BoundaryQuadTree, else null
 * @throws IOException
 */
private static BoundaryQuadTree loadQuadTreeFromStream(InputStream stream, String fname, uk.me.parabola.imgfmt.app.Area searchBbox, EnhancedProperties props) throws IOException {
    BoundaryQuadTree bqt = null;
    uk.me.parabola.imgfmt.app.Area qtBbox = getBbox(fname);
    try (DataInputStream inpStream = new DataInputStream(new BufferedInputStream(stream, 1024 * 1024))) {
        try {
            // 1st read the mkgmap release the boundary file is created by
            String mkgmapRel = "?";
            String firstId = inpStream.readUTF();
            if ("BND".equals(firstId) == false) {
                throw new FormatException("Unsupported boundary data type " + firstId);
            }
            int format = UNKNOWN_DATA_FORMAT;
            long createTime = inpStream.readLong();
            int headerLength = inpStream.readInt();
            byte[] header = new byte[headerLength];
            int bytesRead = 0;
            while (bytesRead < headerLength) {
                int nBytes = inpStream.read(header, bytesRead, headerLength - bytesRead);
                if (nBytes < 0) {
                    throw new IOException("Cannot read header with size " + headerLength);
                }
                bytesRead += nBytes;
            }
            ByteArrayInputStream rawHeaderStream = new ByteArrayInputStream(header);
            DataInputStream headerStream = new DataInputStream(rawHeaderStream);
            String dataFormat = (rawHeaderStream.available() > 0 ? headerStream.readUTF() : "RAW");
            int recordVersion = (rawHeaderStream.available() > 0 ? headerStream.readInt() : RAW_DATA_FORMAT_V1);
            mkgmapRel = (rawHeaderStream.available() > 0 ? headerStream.readUTF() : "unknown");
            if ("RAW".equals(dataFormat) && recordVersion == 1)
                format = RAW_DATA_FORMAT_V1;
            else if ("QUADTREE".equals(dataFormat) && recordVersion == 1)
                format = QUADTREE_DATA_FORMAT_V1;
            if (log.isDebugEnabled()) {
                log.debug("File created by mkgmap release", mkgmapRel, "at", new Date(createTime));
            }
            switch(format) {
                case QUADTREE_DATA_FORMAT_V1:
                    bqt = new BoundaryQuadTree(inpStream, qtBbox, searchBbox, props);
                    break;
                case RAW_DATA_FORMAT_V1:
                    List<Boundary> boundaryList = readStreamRawFormat(inpStream, fname, searchBbox);
                    if (boundaryList == null || boundaryList.isEmpty())
                        return null;
                    boundaryList = mergePostalCodes(boundaryList);
                    bqt = new BoundaryQuadTree(qtBbox, boundaryList, props);
                    break;
                default:
                    throw new FormatException("Unsupported boundary file format: " + format);
            }
        } catch (EOFException exp) {
        // it's always thrown at the end of the file
        // log.error("Got EOF at the end of the file");
        } catch (FormatException exp) {
            log.error("Failed to read boundary file " + fname + " " + exp.getMessage());
        }
    }
    return bqt;
}
Also used : IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) FormatException(uk.me.parabola.imgfmt.FormatException) Date(java.util.Date) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) EOFException(java.io.EOFException)

Example 4 with FormatException

use of uk.me.parabola.imgfmt.FormatException in project mkgmap by openstreetmap.

the class PolishMapDataSource method load.

@Override
public void load(String name, boolean addBackground) throws FileNotFoundException, FormatException {
    Reader reader;
    try {
        reader = new InputStreamReader(Utils.openFile(name), READING_CHARSET);
    } catch (UnsupportedEncodingException e) {
        // Java is required to support iso-8859-1 so this is unlikely
        throw new FormatException("Unrecognised charset " + READING_CHARSET);
    }
    // If no code page is given then we read labels in utf-8
    dec = Charset.forName("utf-8").newDecoder();
    dec.onUnmappableCharacter(CodingErrorAction.REPLACE);
    BufferedReader in = new BufferedReader(reader);
    try {
        String line;
        while ((line = in.readLine()) != null) {
            ++lineNo;
            line = line.trim();
            if (line.isEmpty() || line.charAt(0) == ';')
                continue;
            if (line.toUpperCase().startsWith("[END"))
                endSection();
            else if (line.charAt(0) == '[')
                sectionStart(line);
            else
                processLine(line);
        }
        // Add all restrictions to the map after reading the full map.
        // The reason being, the restrictions section appear in the beginning of the map.
        // All the nodes will only be read later on.
        // Required to pass the road helper instance as it contains all node data.
        restrictionHelper.processAndAddRestrictions(roadHelper, mapper);
    } catch (IOException e) {
        throw new FormatException("Reading file failed", e);
    }
    if (addBackground && !havePolygon4B)
        addBackground();
    coordMap = null;
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) FormatException(uk.me.parabola.imgfmt.FormatException)

Example 5 with FormatException

use of uk.me.parabola.imgfmt.FormatException in project mkgmap by openstreetmap.

the class Rgb method write.

public void write(ImgFileWriter writer, byte type) {
    if (type != 0x10)
        throw new FormatException("Invalid color deep");
    writer.put((byte) b);
    writer.put((byte) g);
    writer.put((byte) r);
}
Also used : FormatException(uk.me.parabola.imgfmt.FormatException)

Aggregations

FormatException (uk.me.parabola.imgfmt.FormatException)9 IOException (java.io.IOException)5 FileNotFoundException (java.io.FileNotFoundException)3 File (java.io.File)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 SAXParser (javax.xml.parsers.SAXParser)2 SAXParserFactory (javax.xml.parsers.SAXParserFactory)2 SAXException (org.xml.sax.SAXException)2 EnhancedProperties (uk.me.parabola.util.EnhancedProperties)2 BlockInputStream (crosby.binary.file.BlockInputStream)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 EOFException (java.io.EOFException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Date (java.util.Date)1