use of org.apache.tools.bzip2.CBZip2InputStream in project OsmAnd-tools by osmandapp.
the class BasemapProcessor method constructBitSetInfo.
protected void constructBitSetInfo(String datFile) {
try {
InputStream dis;
if (datFile == null) {
InputStream stream = BasemapProcessor.class.getResourceAsStream("oceantiles_12.dat.bz2");
if (stream.read() != 'B' || stream.read() != 'Z') {
throw new RuntimeException(// $NON-NLS-1$
"The source stream must start with the characters BZ if it is to be read as a BZip2 stream.");
}
dis = new CBZip2InputStream(stream);
} else {
dis = new FileInputStream(datFile);
}
int currentByte;
for (int i = 0; i < BITS_COUNT / 4; i++) {
currentByte = dis.read();
if (((currentByte >> 6) & BITMASK) == SEA) {
seaTileInfo.set(i * 4);
} else if (((currentByte >> 6) & BITMASK) == LAND) {
landTileInfo.set(i * 4);
}
if (((currentByte >> 4) & BITMASK) == SEA) {
seaTileInfo.set(i * 4 + 1);
} else if (((currentByte >> 4) & BITMASK) == LAND) {
landTileInfo.set(i * 4 + 1);
}
if (((currentByte >> 2) & BITMASK) == SEA) {
seaTileInfo.set(i * 4 + 2);
} else if (((currentByte >> 2) & BITMASK) == LAND) {
landTileInfo.set(i * 4 + 2);
}
if ((currentByte & BITMASK) == SEA) {
seaTileInfo.set(i * 4 + 3);
} else if ((currentByte & BITMASK) == LAND) {
landTileInfo.set(i * 4 + 3);
}
}
} catch (IOException e) {
throw new RuntimeException("File with coastline tiles was not found ");
}
}
use of org.apache.tools.bzip2.CBZip2InputStream in project OsmAnd-tools by osmandapp.
the class OceanTilesCreator method createTilesFile.
public static void createTilesFile(String coastlinesInput, String result) throws IOException, XmlPullParserException {
if (result == null) {
result = "oceantiles_12.dat";
}
File readFile = new File(coastlinesInput);
InputStream stream = new BufferedInputStream(new FileInputStream(readFile), 8192 * 4);
InputStream streamFile = stream;
long st = System.currentTimeMillis();
if (readFile.getName().endsWith(".bz2")) {
// $NON-NLS-1$
if (stream.read() != 'B' || stream.read() != 'Z') {
// throw new RuntimeException("The source stream must start with the characters BZ if it is to be read as a BZip2 stream."); //$NON-NLS-1$
} else {
stream = new CBZip2InputStream(stream);
}
}
OsmBaseStorage bs = new OsmBaseStorage();
bs.parseOSM(stream, IProgress.EMPTY_PROGRESS);
int c = 0;
int ns = 0;
TLongObjectHashMap<OceanTileInfo> map = new TLongObjectHashMap<OceanTileInfo>();
for (Entity e : bs.getRegisteredEntities().values()) {
if (e instanceof Way) {
Way w = (Way) e;
List<Node> nodes = w.getNodes();
for (int i = 1; i < nodes.size(); i++) {
double tx = MapUtils.getTileNumberX(TILE_ZOOMLEVEL, nodes.get(i).getLongitude());
double ty = MapUtils.getTileNumberY(TILE_ZOOMLEVEL, nodes.get(i).getLatitude());
double px = MapUtils.getTileNumberX(TILE_ZOOMLEVEL, nodes.get(i - 1).getLongitude());
double py = MapUtils.getTileNumberY(TILE_ZOOMLEVEL, nodes.get(i - 1).getLatitude());
for (int x = (int) Math.min(tx, px); x <= Math.max(tx, px); x++) {
for (int y = (int) Math.min(ty, py); y <= Math.max(ty, py); y++) {
// check if intersects (x-1,y+0.5) & (x,y+0.5)
long key = ((long) x << TILE_ZOOMLEVEL) + (long) y;
if (intersect2Segments(tx, ty, px, py, x, y + 0.5d, x + 1, y + 0.5d)) {
getOrCreate(map, key).linesIntersectMedian++;
getOrCreate(map, key).type = OceanTileInfo.MIXED;
} else if (intersect2Segments(tx, ty, px, py, x, y, x + 1, y)) {
getOrCreate(map, key).type = OceanTileInfo.MIXED;
} else if (intersect2Segments(tx, ty, px, py, x, y + 1, x + 1, y + 1)) {
getOrCreate(map, key).type = OceanTileInfo.MIXED;
} else if (intersect2Segments(tx, ty, px, py, x, y, x, y + 1)) {
getOrCreate(map, key).type = OceanTileInfo.MIXED;
} else if (intersect2Segments(tx, ty, px, py, x + 1, y, x + 1, y + 1)) {
getOrCreate(map, key).type = OceanTileInfo.MIXED;
}
}
}
}
c++;
ns += w.getNodeIds().size();
}
}
writeResult(map, result);
System.out.println(c + " " + ns + " coastlines " + map.size());
}
use of org.apache.tools.bzip2.CBZip2InputStream in project OsmAnd-tools by osmandapp.
the class GenerateRegionTags method process.
private static void process(File inputFile, File targetFile, OsmandRegions or) throws IOException, XmlPullParserException, XMLStreamException {
InputStream fis = new FileInputStream(inputFile);
if (inputFile.getName().endsWith(".gz")) {
fis = new GZIPInputStream(fis);
} else if (inputFile.getName().endsWith(".bz2")) {
if (fis.read() != 'B' || fis.read() != 'Z') {
throw new RuntimeException(// $NON-NLS-1$
"The source stream must start with the characters BZ if it is to be read as a BZip2 stream.");
}
fis = new CBZip2InputStream(fis);
}
OsmBaseStorage bs = new OsmBaseStorage();
bs.parseOSM(fis, new ConsoleProgressImplementation());
LOG.info("File was read");
iterateOverEntities(bs.getRegisteredEntities(), or);
OsmStorageWriter w = new OsmStorageWriter();
OutputStream output = new FileOutputStream(targetFile);
if (targetFile.getName().endsWith(".gz")) {
output = new GZIPOutputStream(output);
} else if (targetFile.getName().endsWith(".bz2")) {
output.write("BZ".getBytes());
output = new CBZip2OutputStream(output);
}
LOG.info("Entities processed. About to save the file.");
w.saveStorage(output, bs, null, true);
output.close();
fis.close();
}
use of org.apache.tools.bzip2.CBZip2InputStream in project SilverKing by Morgan-Stanley.
the class BZip2 method decompress.
public byte[] decompress(byte[] value, int offset, int length, int uncompressedLength) throws IOException {
CBZip2InputStream bzip2is;
InputStream inStream;
byte[] uncompressedValue;
// System.out.println(value.length +" "+ offset +" "+ length);
// System.out.println(StringUtil.byteArrayToHexString(value, offset, length));
uncompressedValue = new byte[uncompressedLength];
inStream = new ByteArrayInputStream(value, offset, length);
try {
int b;
b = inStream.read();
if (b != 'B') {
throw new IOException("Invalid bzip2 value");
}
b = inStream.read();
if (b != 'Z') {
throw new IOException("Invalid bzip2 value");
}
bzip2is = new CBZip2InputStream(inStream);
try {
int totalRead;
totalRead = 0;
do {
int numRead;
numRead = bzip2is.read(uncompressedValue, totalRead, uncompressedLength - totalRead);
if (numRead < 0) {
throw new RuntimeException("panic");
}
totalRead += numRead;
} while (totalRead < uncompressedLength);
return uncompressedValue;
} finally {
bzip2is.close();
}
} finally {
inStream.close();
}
}
use of org.apache.tools.bzip2.CBZip2InputStream in project dkpro-tc by dkpro.
the class WekaUtils method getInstances.
/**
* Read instances from uncompressed or compressed arff files. Compression is determined by
* filename suffix. For bz2 files, it is expected that the first two bytes mark the compression
* types (BZ) - thus, the first bytes of the stream are skipped. <br>
* For arff files with single-label outcome, the class attribute is expected at the end of the
* attribute set. For arff files with multi-label outcome, the class attribute is expected at
* the beginning of the attribute set; additionally the number of class labels must be specified
* in the relation tag behind a "-C" argument, e.g. "-C 3".
*
* @param instancesFile
* arff File
* @param multiLabel
* whether this arff file contains single- or multi-label outcome
* @return instances with class attribute set
* @throws FileNotFoundException
* if file is not found
* @throws IOException
* if an exception occurs
*/
public static Instances getInstances(File instancesFile, boolean multiLabel) throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream(instancesFile);
BufferedInputStream bufStr = new BufferedInputStream(fis);
InputStream underlyingStream = null;
if (instancesFile.getName().endsWith(".gz")) {
underlyingStream = new GZIPInputStream(bufStr);
} else if (instancesFile.getName().endsWith(".bz2")) {
// skip bzip2 prefix that we added manually
fis.read();
fis.read();
underlyingStream = new CBZip2InputStream(bufStr);
} else {
underlyingStream = bufStr;
}
Reader reader = new InputStreamReader(underlyingStream, "UTF-8");
Instances trainData = new Instances(reader);
if (multiLabel) {
String relationTag = trainData.relationName();
// for multi-label classification, class labels are expected at beginning of attribute
// set and their number must be specified with the -C parameter in the relation tag
Matcher m = Pattern.compile("-C\\s\\d+").matcher(relationTag);
m.find();
trainData.setClassIndex(Integer.parseInt(m.group().split("-C ")[1]));
} else {
// for single-label classification, class label expected as last attribute
trainData.setClassIndex(trainData.numAttributes() - 1);
}
reader.close();
return trainData;
}
Aggregations