use of uk.me.parabola.imgfmt.ExitException in project mkgmap by openstreetmap.
the class StyleImpl method readStyle.
/**
* Evaluate the style options and try to read the style.
*
* The option --style-file give the location of an alternate file or
* directory containing styles rather than the default built in ones.
*
* The option --style gives the name of a style, either one of the
* built in ones or selects one from the given style-file.
*
* If there is no name given, but there is a file then the file should
* just contain one style.
*
* @param props the program properties
* @return A style instance or null in case of error.
*/
public static Style readStyle(EnhancedProperties props) {
String loc = props.getProperty("style-file");
if (loc == null)
loc = props.getProperty("map-features");
String name = props.getProperty("style");
if (loc == null && name == null)
name = "default";
if (name == null) {
StyleFileLoader loader = null;
try {
loader = StyleFileLoader.createStyleLoader(loc, null);
int numEntries = loader.list().length;
if (numEntries > 1)
throw new ExitException("Style file " + loc + " contains multiple styles, use option --style to select one.");
} catch (FileNotFoundException e) {
throw new ExitException("Could not open style file " + loc);
} finally {
Utils.closeFile(loader);
}
}
Style style;
try {
style = new StyleImpl(loc, name, props, WITHOUT_CHECKS);
} catch (SyntaxException e) {
System.err.println("Error in style: " + e.getMessage());
throw new ExitException("Could not open style " + (name == null ? "" : name));
} catch (FileNotFoundException e) {
String msg = "Could not open style ";
if (name != null) {
msg += name;
if (loc != null)
msg += " in " + loc;
} else
msg += loc + " . Make sure that it points to a style or add the --style option.";
throw new ExitException(msg);
}
return style;
}
use of uk.me.parabola.imgfmt.ExitException in project mkgmap by openstreetmap.
the class AbstractTestMap method makeMap.
protected void makeMap(String[] args) {
// Default to nowhere in particular.
double lat = 51.724;
double lng = 0.2487;
// Arguments allow you to place the map where ever you wish.
if (args.length > 1) {
lat = Double.valueOf(args[0]);
lng = Double.valueOf(args[1]);
}
log.debug("this is a test make map program. Start", lat, '/', lng);
FileSystemParam params = new FileSystemParam();
params.setBlockSize(512);
params.setMapDescription("OSM street map");
Map map;
try {
map = Map.createMap("32860003", ".", params, "32860003", SrtTextReader.sortForCodepage(1252));
} catch (FileExistsException e) {
throw new ExitException("File exists already", e);
} catch (FileNotWritableException e) {
throw new ExitException("Could not create or write file", e);
}
map.addInfo("Program released under the GPL");
map.addInfo("This map data is made available under the Open Database License:");
// There has to be (at least) two copyright messages or else the map
// does not show up. The second one will be displayed at startup,
// although the conditions where that happens are not known.
map.addCopyright("program licenced under GPL v2");
// This one gets shown when you switch on, so put the actual
// map copyright here. This is made up data, so no copyright applies.
map.addCopyright("No copyright");
Area area = new Area(lat, lng, lat + 1, lng + 1);
map.setBounds(area);
// There must always be an empty zoom level at the least detailed level.
log.info("area " + area);
log.info(" or " + lat + '/' + lng);
Zoom z1 = map.createZoom(1, 24);
Subdivision topdiv = map.topLevelSubdivision(area, z1);
// Create a most detailed view
Zoom z = map.createZoom(0, 24);
Subdivision div = map.createSubdivision(topdiv, area, z);
div.startDivision();
drawTestMap(map, div, lat, lng);
map.close();
}
use of uk.me.parabola.imgfmt.ExitException in project mkgmap by openstreetmap.
the class Main method endOptions.
public void endOptions(CommandArgs args) {
fileOptions(args);
log.info("Start tile processors");
int threadCount = maxJobs;
int taskCount = futures.size();
Runtime runtime = Runtime.getRuntime();
if (threadPool == null) {
if (threadCount == 0) {
threadCount = 1;
if (taskCount > 2) {
// run one task to see how much memory it uses
log.info("Max Memory: " + runtime.maxMemory());
futures.get(0).run();
long maxMemory = 0;
for (MemoryPoolMXBean mxBean : ManagementFactory.getMemoryPoolMXBeans()) {
if (mxBean.getType() == MemoryType.HEAP) {
MemoryUsage memoryUsage = mxBean.getPeakUsage();
log.info("Max: " + memoryUsage.getMax());
log.info("Used: " + memoryUsage.getUsed());
if (memoryUsage.getMax() > maxMemory && memoryUsage.getUsed() != 0) {
maxMemory = memoryUsage.getMax();
threadCount = (int) (memoryUsage.getMax() / memoryUsage.getUsed());
}
}
}
threadCount = Math.max(threadCount, 1);
threadCount = Math.min(threadCount, runtime.availableProcessors());
System.out.println("Setting max-jobs to " + threadCount);
}
}
log.info("Creating thread pool with " + threadCount + " threads");
threadPool = Executors.newFixedThreadPool(threadCount);
}
// process all input files
for (FilenameTask task : futures) {
threadPool.execute(task);
}
List<FilenameTask> filenames = new ArrayList<>();
int numMapFailedExceptions = 0;
if (threadPool != null) {
threadPool.shutdown();
while (!futures.isEmpty()) {
try {
try {
// don't call get() until a job has finished
if (futures.get(0).isDone()) {
FilenameTask future = futures.remove(0);
// Provoke any exceptions by calling get and then
// save the result for later use
future.setFilename(future.get());
filenames.add(future);
} else
Thread.sleep(100);
} catch (ExecutionException e) {
// Re throw the underlying exception
Throwable cause = e.getCause();
if (cause instanceof Exception)
// noinspection ProhibitedExceptionThrown
throw (Exception) cause;
else if (cause instanceof Error)
// noinspection ProhibitedExceptionThrown
throw (Error) cause;
else
throw e;
}
} catch (OutOfMemoryError | ExitException e) {
throw e;
} catch (MapFailedException mfe) {
// System.err.println(mfe.getMessage()); // already printed via log
numMapFailedExceptions++;
setProgramRC(-1);
} catch (Throwable t) {
t.printStackTrace();
if (!args.getProperties().getProperty("keep-going", false)) {
throw new ExitException("Exiting - if you want to carry on regardless, use the --keep-going option");
}
}
}
}
System.out.println("Number of MapFailedExceptions: " + numMapFailedExceptions);
if ((taskCount > threadCount + 1) && (maxJobs == 0) && (threadCount < runtime.availableProcessors())) {
System.out.println("To reduce the run time, consider increasing the amnount of memory available for use by mkgmap by using the Java -Xmx flag to set the memory to more than " + 100 * (1 + ((runtime.maxMemory() * runtime.availableProcessors()) / (threadCount * 1024 * 1024 * 100))) + " MB, providing this is less than the amount of physical memory installed.");
}
if (combiners.isEmpty())
return;
boolean hasFiles = false;
for (FilenameTask file : filenames) {
if (file == null || file.isCancelled() || file.getFilename() == null) {
if (args.getProperties().getProperty("keep-going", false))
continue;
else
throw new ExitException("Exiting - if you want to carry on regardless, use the --keep-going option");
}
hasFiles = true;
}
if (!hasFiles) {
log.warn("nothing to do for combiners.");
return;
}
log.info("Combining maps");
args.setSort(getSort(args));
// Get them all set up.
for (Combiner c : combiners) c.init(args);
filenames.sort(new Comparator<FilenameTask>() {
public int compare(FilenameTask o1, FilenameTask o2) {
if (!o1.getFilename().endsWith(".img") || !o2.getFilename().endsWith(".img"))
return o1.getFilename().compareTo(o2.getFilename());
// Both end in .img
try {
int id1 = FileInfo.getFileInfo(o1.getFilename()).getHexname();
int id2 = FileInfo.getFileInfo(o2.getFilename()).getHexname();
if (id1 == id2)
return 0;
else if (id1 < id2)
return -1;
else
return 1;
} catch (FileNotFoundException ignored) {
}
return 0;
}
});
// will contain img files for which an additional ovm file was found
HashSet<String> foundOvmFiles = new HashSet<>();
// try OverviewBuilder with special files
if (tdbBuilderAdded) {
for (FilenameTask file : filenames) {
if (file == null || file.isCancelled())
continue;
try {
String fileName = file.getFilename();
if (!fileName.endsWith(".img"))
continue;
fileName = OverviewBuilder.getOverviewImgName(fileName);
log.info(" " + fileName);
FileInfo fileInfo = FileInfo.getFileInfo(fileName);
fileInfo.setArgs(file.getArgs());
// add the real input file
foundOvmFiles.add(file.getFilename());
for (Combiner c : combiners) {
if (c instanceof OverviewBuilder)
c.onMapEnd(fileInfo);
}
} catch (FileNotFoundException ignored) {
}
}
}
// Tell them about each filename (OverviewBuilder excluded)
for (FilenameTask file : filenames) {
if (file == null || file.isCancelled())
continue;
try {
log.info(" " + file);
FileInfo fileInfo = FileInfo.getFileInfo(file.getFilename());
fileInfo.setArgs(file.getArgs());
for (Combiner c : combiners) {
if (c instanceof OverviewBuilder && foundOvmFiles.contains(file.getFilename()))
continue;
c.onMapEnd(fileInfo);
}
} catch (FileNotFoundException e) {
throw new MapFailedException("could not open file " + e.getMessage());
}
}
// All done, allow tidy up or file creation to happen
for (Combiner c : combiners) c.onFinish();
if (tdbBuilderAdded && args.getProperties().getProperty("remove-ovm-work-files", false)) {
for (String fName : foundOvmFiles) {
String ovmFile = OverviewBuilder.getOverviewImgName(fName);
log.info("removing " + ovmFile);
new File(ovmFile).delete();
}
}
}
use of uk.me.parabola.imgfmt.ExitException in project mkgmap by openstreetmap.
the class TypSaver method makeMap.
public String makeMap(CommandArgs args, String filename) {
String outfilename = filename;
// These are the family and product id's that are wanted.
int familyId = args.get("family-id", CommandArgs.DEFAULT_FAMILYID);
int productId = args.get("product-id", CommandArgs.DEFAULT_PRODUCTID);
try (FileInputStream in = new FileInputStream(filename)) {
byte[] buf = new byte[256];
int n = in.read(buf);
ByteBuffer buffer = ByteBuffer.wrap(buf);
buffer.order(ByteOrder.LITTLE_ENDIAN);
// Get the product and family id's that are actually in the supplied TYP file.
int foundFamily = buffer.getChar(0x2f);
int foundProduct = buffer.getChar(0x31);
if (familyId != foundFamily || productId != foundProduct) {
buffer.putChar(0x2f, (char) familyId);
buffer.putChar(0x31, (char) productId);
outfilename = makeOutName(filename);
writeAlteredTyp(outfilename, in, buf, n);
}
} catch (IOException e) {
throw new ExitException("TYP file cannot be opened or read: " + filename);
}
return outfilename;
}
use of uk.me.parabola.imgfmt.ExitException in project mkgmap by openstreetmap.
the class BoundaryUtil method getBoundaryDirContent.
/**
* Check content of directory or zip file with precompiled boundary data,
* dirName Name has to be a directory or a zip file.
* @param dirName : path to a directory or a zip file containing the *.bnd files
* @return the available *.bnd files in dirName.
*/
public static List<String> getBoundaryDirContent(String dirName) {
List<String> names = new ArrayList<>();
File boundaryDir = new File(dirName);
if (!boundaryDir.exists())
log.error("boundary directory/zip does not exist: " + dirName);
else {
if (boundaryDir.isDirectory()) {
// boundaryDir.list() is much quicker than boundaryDir.listFiles(FileFilter)
String[] allNames = boundaryDir.list();
for (String name : allNames) {
if (name.endsWith(".bnd"))
names.add(name);
}
} else if (boundaryDir.getName().endsWith(".zip")) {
try (ZipFile zipFile = new ZipFile(boundaryDir)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
boolean isFlat = true;
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (entry.isDirectory()) {
isFlat = false;
}
if (entry.getName().endsWith(".bnd"))
names.add(entry.getName());
}
if (!isFlat) {
log.error("boundary zip file contains directories. Files in directories will be ignored." + dirName);
}
} catch (IOException ioe) {
log.error("Cannot read", dirName, ioe);
// ioe.printStackTrace();
throw new ExitException("Failed to read required file " + dirName);
}
}
}
return names;
}
Aggregations