int main (int argc, const char * argv[]) { FILE *fl = fopen(argv[1], "r"); FILE *out = fopen("/tmp/out.json", "w"); fseek(fl, 0, SEEK_END); int file_size = ftell(fl); printf("File size: %d\n", file_size); fseek(fl, 0, SEEK_SET); int index = 0; Cursor* cursor = alloc_cursor(); clear_cursor(cursor); cursor->position = -1; read_osm_header(cursor, fl); OsmItem* item; do { item = read_osm_item(cursor, fl, file_size); if (item) { char* json_item_txt = encode_item(item); fputs(json_item_txt, out); free(json_item_txt); fputs("\n", out); } index += 1; } while (item != NULL); free_cursor(cursor); fclose(fl); fclose(out); }
void QcOsmPbfReader::read_blob() { // size of the following blob int32_t data_size = m_blob_header.datasize(); qDebug().nospace() << " datasize = " << data_size; // ensure the blob is smaller then MAX_BLOB_SIZE if (data_size > OSMPBF::max_uncompressed_blob_size) qCritical() << "blob-size is bigger then allowed (" << data_size << " > " << OSMPBF::max_uncompressed_blob_size << ")"; // read the blob from the file if (m_data_stream.readRawData(m_buffer, data_size) == -1) qCritical() << "unable to read blob from file"; // parse the blob from the read-buffer if (!m_blob.ParseFromArray(m_buffer, data_size)) qCritical() << "unable to parse blob"; // tell about the blob-header qDebug().nospace() << "Blob (" << data_size << " bytes)"; // set when we find at least one data stream bool found_data = false; // if the blob has uncompressed data if (m_blob.has_raw()) { // we have at least one datastream found_data = true; // size of the blob-data m_buffer_size = m_blob.raw().size(); // check that raw_size is set correctly if (m_buffer_size != m_blob.raw_size()) qWarning() << " reports wrong raw_size: " << m_blob.raw_size() << " bytes"; // tell about the blob-data qDebug().nospace() << " contains uncompressed data: " << m_buffer_size << " bytes"; // copy the uncompressed data over to the unpack_buffer memcpy(m_unpack_buffer, m_buffer, m_buffer_size); } // if the blob has zlib-compressed data if (m_blob.has_zlib_data()) { // issue a warning if there is more than one data steam, a blob may only contain one data stream if (found_data) qWarning() << " contains several data streams"; // we have at least one datastream found_data = true; // unpacked size zlib_inflate(); } // if the blob has lzma-compressed data if (m_blob.has_lzma_data()) { // issue a warning if there is more than one data steam, a blob may only contain one data stream if (found_data) qWarning() << " contains several data streams"; // we have at least one datastream found_data = true; // unpacked size lzma_inflate(); } // check we have at least one data-stream if (!found_data) qCritical() << " does not contain any known data stream"; // switch between different blob-types auto blob_type = m_blob_header.type(); if (blob_type == "OSMHeader") // The Blob contains a serialized HeaderBlock message. // Every fileblock must have one of these blocks before the first 'OSMData' block. read_osm_header(); else if (blob_type == "OSMData") // The Blob contains a serialized PrimitiveBlock message. read_osm_data(); else // unknown blob type qWarning() << " unknown blob type: " << m_blob_header.type().c_str(); }