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");
}
}
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);
}
}
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());
}
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;
}
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 };
}
Aggregations