Search in sources :

Example 1 with CBZip2InputStream

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 ");
    }
}
Also used : CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 2 with CBZip2InputStream

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());
}
Also used : Entity(net.osmand.osm.edit.Entity) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) InputStream(java.io.InputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) Node(net.osmand.osm.edit.Node) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) FileInputStream(java.io.FileInputStream) Way(net.osmand.osm.edit.Way) BufferedInputStream(java.io.BufferedInputStream) OsmBaseStorage(net.osmand.osm.io.OsmBaseStorage) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 3 with CBZip2InputStream

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();
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) OsmStorageWriter(net.osmand.osm.io.OsmStorageWriter) CBZip2OutputStream(org.apache.tools.bzip2.CBZip2OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OsmBaseStorage(net.osmand.osm.io.OsmBaseStorage) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) OutputStream(java.io.OutputStream) CBZip2OutputStream(org.apache.tools.bzip2.CBZip2OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) ConsoleProgressImplementation(net.osmand.impl.ConsoleProgressImplementation) FileInputStream(java.io.FileInputStream)

Example 4 with CBZip2InputStream

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();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CBZip2InputStream(org.apache.hadoop.io.compress.bzip2.CBZip2InputStream) InputStream(java.io.InputStream) CBZip2InputStream(org.apache.hadoop.io.compress.bzip2.CBZip2InputStream) IOException(java.io.IOException)

Example 5 with CBZip2InputStream

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;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) Instances(weka.core.Instances) MultiLabelInstances(mulan.data.MultiLabelInstances) InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) Matcher(java.util.regex.Matcher) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ObjectInputStream(java.io.ObjectInputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream)

Aggregations

InputStream (java.io.InputStream)12 CBZip2InputStream (org.apache.tools.bzip2.CBZip2InputStream)12 FileInputStream (java.io.FileInputStream)10 BufferedInputStream (java.io.BufferedInputStream)9 GZIPInputStream (java.util.zip.GZIPInputStream)6 File (java.io.File)5 InputStreamReader (java.io.InputStreamReader)4 OsmBaseStorage (net.osmand.osm.io.OsmBaseStorage)4 IOException (java.io.IOException)3 Reader (java.io.Reader)3 BufferedReader (java.io.BufferedReader)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 OutputStream (java.io.OutputStream)2 SAXParser (javax.xml.parsers.SAXParser)2 ConsoleProgressImplementation (net.osmand.impl.ConsoleProgressImplementation)2 Entity (net.osmand.osm.edit.Entity)2 InputSource (org.xml.sax.InputSource)2 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)1 JsonStatusCorpusReader (io.anserini.document.twitter.JsonStatusCorpusReader)1 Status (io.anserini.document.twitter.Status)1