Search in sources :

Example 21 with ExitException

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

the class MdrBuilder method init.

/**
 * Create the mdr file and initialise.
 * It has a name that is based on the overview-mapname option, as does
 * the associated MDX file.
 *
 * @param args The command line arguments.
 */
public void init(CommandArgs args) {
    String name = args.get("overview-mapname", "osmmap");
    String outputDir = args.getOutputDir();
    outputName = Utils.joinPath(outputDir, name + "_mdr.img");
    ImgChannel mdrChan;
    try {
        // Create the .img file system/archive
        FileSystemParam params = new FileSystemParam();
        tmpName = File.createTempFile("mdr", null, new File(outputDir));
        tmpName.deleteOnExit();
        imgfs = ImgFS.createFs(tmpName.getPath(), params);
        // Create the MDR file within the .img
        mdrChan = imgfs.create(name.toUpperCase(Locale.ENGLISH) + ".MDR");
    } catch (IOException e) {
        throw new ExitException("Could not create global index file");
    }
    // Create the sort description
    Sort sort = SrtTextReader.sortForCodepage(args.getCodePage());
    // Set the options that we are using for the mdr.
    MdrConfig config = new MdrConfig();
    config.setHeaderLen(568);
    config.setWritable(true);
    config.setForDevice(false);
    config.setOutputDir(outputDir);
    config.setSort(sort);
    config.setIndexOptions(args);
    // Wrap the MDR channel with the MDRFile object
    mdrFile = new MDRFile(mdrChan, config);
    try {
        ImgChannel srtChan = imgfs.create(name.toUpperCase(Locale.ENGLISH) + ".SRT");
        SRTFile srtFile = new SRTFile(srtChan);
        srtFile.setSort(sort);
        srtFile.write();
    // Do not close srtFile here
    } catch (FileExistsException e) {
        throw new ExitException("Could not create SRT file within index file");
    }
}
Also used : ImgChannel(uk.me.parabola.imgfmt.fs.ImgChannel) SRTFile(uk.me.parabola.imgfmt.app.srt.SRTFile) FileSystemParam(uk.me.parabola.imgfmt.FileSystemParam) MdrConfig(uk.me.parabola.imgfmt.app.mdr.MdrConfig) Sort(uk.me.parabola.imgfmt.app.srt.Sort) MDRFile(uk.me.parabola.imgfmt.app.mdr.MDRFile) IOException(java.io.IOException) SRTFile(uk.me.parabola.imgfmt.app.srt.SRTFile) File(java.io.File) MDRFile(uk.me.parabola.imgfmt.app.mdr.MDRFile) ExitException(uk.me.parabola.imgfmt.ExitException) FileExistsException(uk.me.parabola.imgfmt.FileExistsException)

Example 22 with ExitException

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

the class OverviewBuilder method readFileIntoOverview.

/**
 * Add an individual .img file to the overview map.
 *
 * @param finfo Information about an individual map.
 */
private void readFileIntoOverview(FileInfo finfo) throws FileNotFoundException {
    addMapCoverageArea(finfo);
    MapReader mapReader = null;
    String filename = finfo.getFilename();
    if (codepage == null) {
        codepage = finfo.getCodePage();
    }
    if (codepage != finfo.getCodePage()) {
        System.err.println("WARNING: input file " + filename + " has different code page " + finfo.getCodePage());
    }
    try {
        mapReader = new MapReader(filename);
        if (encodingType == null) {
            encodingType = mapReader.getEncodingType();
        }
        if (encodingType != mapReader.getEncodingType()) {
            System.err.println("WARNING: input file " + filename + " has different charset type " + encodingType);
        }
        String[] msgs = mapReader.getCopyrights();
        boolean found = false;
        for (String[] block : copyrightMsgs) {
            if (Arrays.deepEquals(block, msgs)) {
                found = true;
                break;
            }
        }
        if (!found)
            copyrightMsgs.add(msgs);
        msgs = finfo.getLicenseInfo();
        found = false;
        for (String[] block : licenseInfos) {
            if (Arrays.deepEquals(block, msgs)) {
                found = true;
                break;
            }
        }
        if (!found)
            licenseInfos.add(msgs);
        Zoom[] levels = mapReader.getLevels();
        if (wantedLevels == null) {
            LevelInfo[] mapLevels;
            if (isOverviewImg(filename)) {
                mapLevels = new LevelInfo[levels.length - 1];
                for (int i = 1; i < levels.length; i++) {
                    mapLevels[i - 1] = new LevelInfo(levels[i].getLevel(), levels[i].getResolution());
                }
            } else {
                mapLevels = new LevelInfo[1];
                mapLevels[0] = new LevelInfo(levels[1].getLevel(), levels[1].getResolution());
            }
            wantedLevels = mapLevels;
        }
        if (isOverviewImg(filename)) {
            readPoints(mapReader);
            readLines(mapReader);
            readShapes(mapReader);
        }
    } catch (FileNotFoundException e) {
        throw new ExitException("Could not open " + filename + " when creating overview file");
    } finally {
        Utils.closeFile(mapReader);
    }
}
Also used : MapReader(uk.me.parabola.imgfmt.app.map.MapReader) LevelInfo(uk.me.parabola.mkgmap.general.LevelInfo) FileNotFoundException(java.io.FileNotFoundException) Zoom(uk.me.parabola.imgfmt.app.trergn.Zoom) ExitException(uk.me.parabola.imgfmt.ExitException) Point(uk.me.parabola.imgfmt.app.trergn.Point) MapPoint(uk.me.parabola.mkgmap.general.MapPoint)

Example 23 with ExitException

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

the class StructuredOutputStream method writeString.

/**
 * Writes a string including a terminating null byte.
 *
 * For each character in the string the low-order byte is written.
 *
 * @param s The string to write.
 * @throws IOException If the write fails.
 */
public void writeString(String s) throws IOException {
    if (encoder == null)
        throw new ExitException("tdbfile: character encoding is null");
    EncodedText encodedText = encoder.encodeText(s);
    if (encodedText == NO_TEXT)
        return;
    out.write(encodedText.getCtext(), 0, encodedText.getLength());
}
Also used : EncodedText(uk.me.parabola.imgfmt.app.labelenc.EncodedText) ExitException(uk.me.parabola.imgfmt.ExitException)

Example 24 with ExitException

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

the class SeaGenerator method init.

/**
 * Sort out options from the command line.
 * Returns true only if the option to generate the sea is active, so that
 * the whole thing is omitted if not used.
 */
public boolean init(ElementSaver saver, EnhancedProperties props) {
    this.saver = saver;
    String precompSea = props.getProperty("precomp-sea", null);
    if (precompSea != null) {
        precompSeaDir = new File(precompSea);
        if (precompSeaDir.exists()) {
            if (precompIndex.get() == null) {
                PrecompData precompData = null;
                String internalPath = null;
                InputStream indexStream = null;
                String indexFileName = "index.txt.gz";
                ZipFile zipFile = null;
                try {
                    if (precompSeaDir.isDirectory()) {
                        File indexFile = new File(precompSeaDir, indexFileName);
                        if (indexFile.exists() == false) {
                            // check if the unzipped index file exists
                            indexFileName = "index.txt";
                            indexFile = new File(precompSeaDir, indexFileName);
                        }
                        if (indexFile.exists()) {
                            indexStream = new FileInputStream(indexFile);
                        }
                    } else if (precompSea.endsWith(".zip")) {
                        zipFile = new ZipFile(precompSeaDir);
                        internalPath = "sea/";
                        ZipEntry entry = zipFile.getEntry(internalPath + indexFileName);
                        if (entry == null) {
                            indexFileName = "index.txt";
                            entry = zipFile.getEntry(internalPath + indexFileName);
                        }
                        if (entry == null) {
                            internalPath = "";
                            indexFileName = "index.txt.gz";
                            entry = zipFile.getEntry(internalPath + indexFileName);
                        }
                        if (entry != null) {
                            indexStream = zipFile.getInputStream(entry);
                        } else
                            log.error("Don't know how to read " + precompSeaDir);
                    } else {
                        log.error("Don't know how to read " + precompSeaDir);
                    }
                    if (indexStream != null) {
                        if (indexFileName.endsWith(".gz")) {
                            indexStream = new GZIPInputStream(indexStream);
                        }
                        precompData = loadIndex(indexStream);
                        if (precompData != null) {
                            if (zipFile != null) {
                                precompData.precompZipFileInternalPath = internalPath;
                                precompData.zipFile = zipFile;
                            }
                            precompIndex.set(precompData);
                        }
                        indexStream.close();
                    }
                } catch (IOException exp) {
                    log.error("Cannot read index file", indexFileName, "in", precompSea, exp);
                    // exp.printStackTrace();
                    throw new ExitException("Failed to read required index file in " + precompSeaDir);
                }
                precompIndex.set(precompData);
            }
        } else {
            log.error("Directory or zip file with precompiled sea does not exist: " + precompSea);
            System.err.println("Directory or zip file with precompiled sea does not exist: " + precompSea);
            precompSeaDir = null;
        }
    }
    String gs = props.getProperty("generate-sea", null);
    boolean generateSea = gs != null || precompSea != null;
    if (gs != null) {
        for (String o : gs.split(",")) {
            if ("no-mp".equals(o) || "polygon".equals(o) || "polygons".equals(o))
                generateSeaUsingMP = false;
            else if ("multipolygon".equals(o))
                generateSeaUsingMP = true;
            else if (o.startsWith("land-tag="))
                landTag = o.substring(9).split("=");
            else if (precompSea == null) {
                // the other options are valid only if not using precompiled sea data
                if (o.startsWith("close-gaps="))
                    maxCoastlineGap = (int) Double.parseDouble(o.substring(11));
                else if ("no-sea-sectors".equals(o))
                    allowSeaSectors = false;
                else if ("extend-sea-sectors".equals(o)) {
                    allowSeaSectors = false;
                    extendSeaSectors = true;
                } else if ("floodblocker".equals(o))
                    floodblocker = true;
                else if (o.startsWith("fbgap="))
                    fbGap = (int) Double.parseDouble(o.substring("fbgap=".length()));
                else if (o.startsWith("fbratio="))
                    fbRatio = Double.parseDouble(o.substring("fbratio=".length()));
                else if (o.startsWith("fbthres="))
                    fbThreshold = (int) Double.parseDouble(o.substring("fbthres=".length()));
                else if ("fbdebug".equals(o))
                    fbDebug = true;
                else {
                    printOptionHelpMsg(precompSea != null, o);
                }
            } else if (o.isEmpty())
                continue;
            else {
                printOptionHelpMsg(precompSea != null, o);
            }
        }
        // if precompSea is not set
        if (precompSea == null) {
            if (floodblocker) {
                try {
                    fbRules = new StyleImpl(null, "floodblocker");
                } catch (FileNotFoundException e) {
                    log.error("Cannot load file floodblocker rules. Continue floodblocking disabled.");
                    floodblocker = false;
                }
            }
            String coastlineFileOpt = props.getProperty("coastlinefile", null);
            if (coastlineFileOpt != null) {
                coastlineFilenames = coastlineFileOpt.split(",");
                CoastlineFileLoader.getCoastlineLoader().setCoastlineFiles(coastlineFilenames);
                CoastlineFileLoader.getCoastlineLoader().loadCoastlines();
                log.info("Coastlines loaded");
            } else {
                coastlineFilenames = null;
            }
        }
    }
    return generateSea;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ZipFile(java.util.zip.ZipFile) StyleImpl(uk.me.parabola.mkgmap.osmstyle.StyleImpl) ZipFile(java.util.zip.ZipFile) File(java.io.File) ExitException(uk.me.parabola.imgfmt.ExitException)

Example 25 with ExitException

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

the class OsmMapDataSource method copyrightMessages.

/**
 * There are no copyright messages in the OSM files themselves.  So we
 * include a fixed set of strings on the assumption that .osm files
 * are probably going to have the OSM copyright statements.
 *
 * @return A list of copyright messages as a String array.
 */
public String[] copyrightMessages() {
    String copyrightFileName = getConfig().getProperty("copyright-file", null);
    if (copyrightFileName != null) {
        List<String> copyrightArray = new ArrayList<>();
        try {
            File file = new File(copyrightFileName);
            copyrightArray = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
        } catch (Exception e) {
            throw new ExitException("Error reading copyright file " + copyrightFileName, e);
        }
        if ((copyrightArray.size() > 0) && copyrightArray.get(0).startsWith("\ufeff"))
            copyrightArray.set(0, copyrightArray.get(0).substring(1));
        UnaryOperator<String> replaceVariables = s -> s.replace("$MKGMAP_VERSION$", Version.VERSION).replace("$JAVA_VERSION$", System.getProperty("java.version")).replace("$YEAR$", Integer.toString(now.getYear())).replace("$LONGDATE$", now.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG))).replace("$SHORTDATE$", now.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT))).replace("$TIME$", now.toLocalTime().toString().substring(0, 5));
        copyrightArray.replaceAll(replaceVariables);
        String[] copyright = new String[copyrightArray.size()];
        copyrightArray.toArray(copyright);
        return copyright;
    }
    String note = getConfig().getProperty("copyright-message", "OpenStreetMap.org contributors. See: http://wiki.openstreetmap.org/index.php/Attribution");
    return new String[] { note };
}
Also used : MapperBasedMapDataSource(uk.me.parabola.mkgmap.reader.MapperBasedMapDataSource) FormatStyle(java.time.format.FormatStyle) OsmXmlHandler(uk.me.parabola.mkgmap.reader.osm.xml.OsmXmlHandler) LocalDateTime(java.time.LocalDateTime) HashMap(java.util.HashMap) UnaryOperator(java.util.function.UnaryOperator) ExitException(uk.me.parabola.imgfmt.ExitException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Map(java.util.Map) Logger(uk.me.parabola.log.Logger) O5mBinHandler(uk.me.parabola.mkgmap.reader.osm.o5m.O5mBinHandler) EnhancedProperties(uk.me.parabola.util.EnhancedProperties) Utils(uk.me.parabola.imgfmt.Utils) StyleImpl(uk.me.parabola.mkgmap.osmstyle.StyleImpl) Version(uk.me.parabola.mkgmap.Version) Files(java.nio.file.Files) Set(java.util.Set) IOException(java.io.IOException) LevelInfo(uk.me.parabola.mkgmap.general.LevelInfo) FormatException(uk.me.parabola.imgfmt.FormatException) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) StyledConverter(uk.me.parabola.mkgmap.osmstyle.StyledConverter) LoadableMapDataSource(uk.me.parabola.mkgmap.general.LoadableMapDataSource) NameFinder(uk.me.parabola.mkgmap.osmstyle.NameFinder) DateTimeFormatter(java.time.format.DateTimeFormatter) OsmBinHandler(uk.me.parabola.mkgmap.reader.osm.bin.OsmBinHandler) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) File(java.io.File) ExitException(uk.me.parabola.imgfmt.ExitException) ExitException(uk.me.parabola.imgfmt.ExitException) IOException(java.io.IOException) FormatException(uk.me.parabola.imgfmt.FormatException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

ExitException (uk.me.parabola.imgfmt.ExitException)25 IOException (java.io.IOException)10 FileNotFoundException (java.io.FileNotFoundException)9 File (java.io.File)7 ArrayList (java.util.ArrayList)5 FileInputStream (java.io.FileInputStream)4 Zoom (uk.me.parabola.imgfmt.app.trergn.Zoom)4 InputStream (java.io.InputStream)3 ByteBuffer (java.nio.ByteBuffer)3 Map (uk.me.parabola.imgfmt.app.map.Map)3 LevelInfo (uk.me.parabola.mkgmap.general.LevelInfo)3 OutOfMemoryError (java.lang.OutOfMemoryError)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Files (java.nio.file.Files)2 LocalDateTime (java.time.LocalDateTime)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 FormatStyle (java.time.format.FormatStyle)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2