pax_global_header00006660000000000000000000000064120575450150014516gustar00rootroot0000000000000052 comment=d9be2d10249a1a4997fe3dc794adad8d4cc0b718 OSM-binary-1.3.0/000077500000000000000000000000001205754501500134375ustar00rootroot00000000000000OSM-binary-1.3.0/.gitignore000066400000000000000000000001021205754501500154200ustar00rootroot00000000000000include/osmpbf/*.pb.h src/*.pb.h src/*.pb.o src/libosmpbf.a *.swp OSM-binary-1.3.0/COPYING.osmpbf000066400000000000000000000167431205754501500157720ustar00rootroot00000000000000 GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. OSM-binary-1.3.0/README000066400000000000000000000007541205754501500143250ustar00rootroot00000000000000 OSM PBF ======= See http://wiki.openstreetmap.org/wiki/PBF_Format . There is a Java and a C version of the PBF library code here. C Version ========= To compile: cd src make To install: cd src make install To build the Debian/Ubuntu package call: debuild -I -us -uc To install the Debian/Ubuntu package call: sudo dpkg --install ../libosmpbf-dev*.deb To include in your program use: #include and link with: -lpthread -lz -lprotobuf-lite -losmpbf OSM-binary-1.3.0/ReleaseNotes.txt000066400000000000000000000023411205754501500165710ustar00rootroot00000000000000---------------------------------------- Release notes for 1.3.0 -- 2012-12-04 ---------------------------------------- Add osmosis replication fields into the header. -- Changes from Frederick Ramm ---------------------------------------- Release notes for 1.2.1 -- 2011-10-29 ---------------------------------------- -------------------------------------- Release notes for 1.2.0 -- NOT RELEASED -------------------------------------- * Bad tag, so not releasable. * Switched to the lite (non-introspective) version of protocol buffers for a smaller library. * Includes support for "visible" flag on OSM objects. This allows PBF to handle OSM history files. * Namespace and include conventions changes for the C++ library. Everything is in the OSMPBF namespace. You now do: #include * Added stuff needed to build Debian/Ubuntu libosmpbf-dev package containing the C++ library. * Added osmpbf-outline tool that shows internals of a PBF file for debugging. * Added magic file that can recognize OSM PBF files. -- Changes from Jochen Topf Peter * Added a pom.xml -- Changes from Zsombor Welker OSM-binary-1.3.0/build.sh000066400000000000000000000001531205754501500150710ustar00rootroot00000000000000protoc --java_out=generated.java src/osmformat.proto protoc --java_out=generated.java src/fileformat.proto OSM-binary-1.3.0/build.xml000066400000000000000000000020251205754501500152570ustar00rootroot00000000000000 OSM-binary-1.3.0/debian/000077500000000000000000000000001205754501500146615ustar00rootroot00000000000000OSM-binary-1.3.0/debian/changelog000066400000000000000000000016501205754501500165350ustar00rootroot00000000000000libosmpbf-dev (1.3.0) maverick; urgency=low * Added fields into OSMHeader for OSM replication. * Version number change to reflect official new version -- Scott Crosby Wed, 04 Dec 2012 21:29:37 -0500 libosmpbf-dev (1.2.1) maverick; urgency=low * Version number change to reflect official new version -- Jochen Topf Wed, 19 Oct 2011 09:29:37 +0100 libosmpbf-dev (1.1.1j2) maverick; urgency=low * Add manpage for osmpbf-outline tool -- Jochen Topf Fri, 6 Jun 2011 10:30:12 +0100 libosmpbf-dev (1.1.1j1) maverick; urgency=low * Add support for OSM history file * Add some often used constants to OSMPBF namespace * Add osmpbf-outline testing tool -- Jochen Topf Fri, 3 Jun 2011 11:36:17 +0100 libosmpbf-dev (0.1) maverick; urgency=low * Initial debian package -- Jochen Topf Wed, 13 Apr 2011 21:50:15 +0100 OSM-binary-1.3.0/debian/compat000066400000000000000000000000021205754501500160570ustar00rootroot000000000000007 OSM-binary-1.3.0/debian/control000066400000000000000000000007201205754501500162630ustar00rootroot00000000000000Source: libosmpbf-dev Section: libdevel Priority: optional Maintainer: Jochen Topf Build-Depends: debhelper (>= 7), libprotobuf-dev, protobuf-compiler Standards-Version: 3.9.1 Homepage: http://wiki.openstreetmap.org/wiki/PBF_Format Package: libosmpbf-dev Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends} Description: Library for reading OSM PBF files. Interface definition and library for reading binary OpenStreetMap data files. OSM-binary-1.3.0/debian/copyright000066400000000000000000000014171205754501500166170ustar00rootroot00000000000000Copyright (c) 2010 Scott A. Crosby. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . On Debian systems, the license text can usually be found in /usr/share/common-licenses. OSM-binary-1.3.0/debian/docs000066400000000000000000000000601205754501500155300ustar00rootroot00000000000000src/fileformat.proto src/osmformat.proto README OSM-binary-1.3.0/debian/libosmpbf-dev.manpages000066400000000000000000000000271205754501500211260ustar00rootroot00000000000000tools/osmpbf-outline.1 OSM-binary-1.3.0/debian/rules000077500000000000000000000035631205754501500157500ustar00rootroot00000000000000#!/usr/bin/make -f # -*- makefile -*- # Sample debian/rules that uses debhelper. # This file was originally written by Joey Hess and Craig Small. # As a special exception, when this file is copied by dh-make into a # dh-make output file, you may use that output file without restriction. # This special exception was added by Craig Small in version 0.37 of dh-make. # Uncomment this to turn on verbose mode. #export DH_VERBOSE=1 configure: configure-stamp configure-stamp: dh_testdir # Add here commands to configure the package. touch configure-stamp build: build-stamp build-stamp: configure-stamp dh_testdir # Add here commands to compile the package. $(MAKE) -C src $(MAKE) -C tools touch $@ clean: dh_testdir dh_testroot rm -f build-stamp configure-stamp # Add here commands to clean up after the build process. $(MAKE) -C src clean || /bin/true $(MAKE) -C tools clean || /bin/true dh_clean install: build dh_testdir dh_testroot dh_prep dh_installdirs # Add here commands to install the package into debian/libosmpbf-dev. $(MAKE) -C src DESTDIR=$(CURDIR)/debian/libosmpbf-dev install $(MAKE) -C tools DESTDIR=$(CURDIR)/debian/libosmpbf-dev install # Build architecture-independent files here. binary-indep: build install # We have nothing to do by default. # Build architecture-dependent files here. binary-arch: build install dh_testdir dh_testroot dh_installchangelogs dh_installdocs dh_installexamples dh_install # dh_installmenu # dh_installdebconf # dh_installlogrotate # dh_installemacsen # dh_installpam # dh_installmime # dh_python # dh_installinit # dh_installcron # dh_installinfo dh_installman dh_link dh_strip dh_compress dh_fixperms # dh_perl # dh_makeshlibs dh_installdeb dh_shlibdeps dh_gencontrol dh_md5sums dh_builddeb binary: binary-indep binary-arch .PHONY: build clean binary-indep binary-arch binary install configure OSM-binary-1.3.0/debian/source/000077500000000000000000000000001205754501500161615ustar00rootroot00000000000000OSM-binary-1.3.0/debian/source/format000066400000000000000000000000151205754501500173700ustar00rootroot000000000000003.0 (native) OSM-binary-1.3.0/include/000077500000000000000000000000001205754501500150625ustar00rootroot00000000000000OSM-binary-1.3.0/include/osmpbf/000077500000000000000000000000001205754501500163505ustar00rootroot00000000000000OSM-binary-1.3.0/include/osmpbf/osmpbf.h000066400000000000000000000012731205754501500200120ustar00rootroot00000000000000#ifndef OSMPBF_H #define OSMPBF_H // this describes the low-level blob storage #include // this describes the high-level OSM objects #include namespace OSMPBF { // library version const char *version = "1.3.0"; // the maximum size of a blob header in bytes const int max_blob_header_size = 64 * 1024; // 64 kB // the maximum size of an uncompressed blob in bytes const int max_uncompressed_blob_size = 32 * 1024 * 1024; // 32 MB // resolution for longitude/latitude used for conversion // between representation as double and as int const int lonlat_resolution = 1000 * 1000 * 1000; } #endif // OSMPBF_H OSM-binary-1.3.0/magic000066400000000000000000000001571205754501500144450ustar00rootroot00000000000000# Magic data for file(1) command. # Format is described in magic(5). 6 string OSMHeader OpenStreetMap PBF OSM-binary-1.3.0/pom.xml000066400000000000000000000046221205754501500147600ustar00rootroot00000000000000 4.0.0 crosby.binary osmpbf jar 1.3.0 OSM-Binary Library for the OpenStreetMap PBF format https://github.com/scrosby/OSM-binary GNU General Lesser Public License (LGPL) version 3.0 http://www.gnu.org/licenses/lgpl-3.0.txt repo https://github.com/scrosby/OSM-binary https://github.com/scrosby/OSM-binary.git ${basedir}/src.java ${basedir}/build org.apache.maven.plugins maven-compiler-plugin 1.5 1.5 com.google.protobuf.tools maven-protoc-plugin generate-sources compile generate-sources ${basedir}/src **/*.proto dtrott http://maven.davidtrott.com/repository junit junit 3.8.1 test com.google.protobuf protobuf-java 2.4.1 OSM-binary-1.3.0/src.java/000077500000000000000000000000001205754501500151465ustar00rootroot00000000000000OSM-binary-1.3.0/src.java/crosby/000077500000000000000000000000001205754501500164475ustar00rootroot00000000000000OSM-binary-1.3.0/src.java/crosby/binary/000077500000000000000000000000001205754501500177335ustar00rootroot00000000000000OSM-binary-1.3.0/src.java/crosby/binary/BinaryParser.java000066400000000000000000000125271205754501500232060ustar00rootroot00000000000000/** Copyright (c) 2010 Scott A. Crosby. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package crosby.binary; import java.util.Date; import java.util.List; import com.google.protobuf.InvalidProtocolBufferException; import crosby.binary.Osmformat; import crosby.binary.file.BlockReaderAdapter; import crosby.binary.file.FileBlock; import crosby.binary.file.FileBlockPosition; public abstract class BinaryParser implements BlockReaderAdapter { protected int granularity; private long lat_offset; private long lon_offset; protected int date_granularity; private String strings[]; /** Take a Info protocol buffer containing a date and convert it into a java Date object */ protected Date getDate(Osmformat.Info info) { if (info.hasTimestamp()) { return new Date(date_granularity * (long) info.getTimestamp()); } else return NODATE; } public static final Date NODATE = new Date(-1); /** Get a string based on the index used. * * Index 0 is reserved to use as a delimiter, therefore, index 1 corresponds to the first string in the table * @param id * @return */ protected String getStringById(int id) { return strings[id]; } @Override public void handleBlock(FileBlock message) { // TODO Auto-generated method stub try { if (message.getType().equals("OSMHeader")) { Osmformat.HeaderBlock headerblock = Osmformat.HeaderBlock .parseFrom(message.getData()); parse(headerblock); } else if (message.getType().equals("OSMData")) { Osmformat.PrimitiveBlock primblock = Osmformat.PrimitiveBlock .parseFrom(message.getData()); parse(primblock); } } catch (InvalidProtocolBufferException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new Error("ParseError"); // TODO } } @Override public boolean skipBlock(FileBlockPosition block) { // System.out.println("Seeing block of type: "+block.getType()); if (block.getType().equals("OSMData")) return false; if (block.getType().equals("OSMHeader")) return false; System.out.println("Skipped block of type: " + block.getType()); return true; } /** Convert a latitude value stored in a protobuf into a double, compensating for granularity and latitude offset */ public double parseLat(long degree) { // Support non-zero offsets. (We don't currently generate them) return (granularity * degree + lat_offset) * .000000001; } /** Convert a longitude value stored in a protobuf into a double, compensating for granularity and longitude offset */ public double parseLon(long degree) { // Support non-zero offsets. (We don't currently generate them) return (granularity * degree + lon_offset) * .000000001; } /** Parse a Primitive block (containing a string table, other paramaters, and PrimitiveGroups */ public void parse(Osmformat.PrimitiveBlock block) { Osmformat.StringTable stablemessage = block.getStringtable(); strings = new String[stablemessage.getSCount()]; for (int i = 0; i < strings.length; i++) { strings[i] = stablemessage.getS(i).toStringUtf8(); } granularity = block.getGranularity(); lat_offset = block.getLatOffset(); lon_offset = block.getLonOffset(); date_granularity = block.getDateGranularity(); for (Osmformat.PrimitiveGroup groupmessage : block .getPrimitivegroupList()) { // Exactly one of these should trigger on each loop. parseNodes(groupmessage.getNodesList()); parseWays(groupmessage.getWaysList()); parseRelations(groupmessage.getRelationsList()); if (groupmessage.hasDense()) parseDense(groupmessage.getDense()); } } /** Parse a list of Relation protocol buffers and send the resulting relations to a sink. */ protected abstract void parseRelations(List rels); /** Parse a DenseNode protocol buffer and send the resulting nodes to a sink. */ protected abstract void parseDense(Osmformat.DenseNodes nodes); /** Parse a list of Node protocol buffers and send the resulting nodes to a sink. */ protected abstract void parseNodes(List nodes); /** Parse a list of Way protocol buffers and send the resulting ways to a sink. */ protected abstract void parseWays(List ways); /** Parse a header message. */ protected abstract void parse(Osmformat.HeaderBlock header); }OSM-binary-1.3.0/src.java/crosby/binary/BinarySerializer.java000066400000000000000000000124631205754501500240620ustar00rootroot00000000000000/** Copyright (c) 2010 Scott A. Crosby. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package crosby.binary; import java.io.IOException; import java.util.ArrayList; import java.util.List; import crosby.binary.Osmformat.PrimitiveGroup; import crosby.binary.file.BlockOutputStream; import crosby.binary.file.FileBlock; /** * Generic serializer common code * * Serialize a set of blobs and process them. Subclasses implement handlers for * different API's (osmosis, mkgmap, splitter, etc.) * * All data is converted into PrimGroupWriterInterface objects, which are then * ordered to process their data at the appropriate time. * */ public class BinarySerializer { /** * Interface used to write a group of primitives. One of these for each * group type (Node, Way, Relation, DenseNode, Changeset) */ protected interface PrimGroupWriterInterface { /** This callback is invoked on each group that is going into the fileblock in order to give it a chance to * add to the stringtable pool of strings. */ public void addStringsToStringtable(); /** * This callback is invoked to request that the primgroup serialize itself into the given protocol buffer object. */ public Osmformat.PrimitiveGroup serialize(); } /** Set the granularity (precision of lat/lon, measured in unites of nanodegrees. */ public void configGranularity(int granularity) { this.granularity = granularity; } /** Set whether metadata is to be omitted */ public void configOmit(boolean omit_metadata) { this.omit_metadata = omit_metadata; } /** Configure the maximum number of entities in a batch */ public void configBatchLimit(int batch_limit) { this.batch_limit = batch_limit; } // Paramaters affecting the output size. protected final int MIN_DENSE = 10; protected int batch_limit = 4000; // Parmaters affecting the output. protected int granularity = 100; protected int date_granularity = 1000; protected boolean omit_metadata = false; /** How many primitives have been seen in this batch */ protected int batch_size = 0; protected int total_entities = 0; private StringTable stringtable = new StringTable(); protected List groups = new ArrayList(); protected BlockOutputStream output; public BinarySerializer(BlockOutputStream output) { this.output = output; } public StringTable getStringTable() { return stringtable; } public void flush() throws IOException { processBatch(); output.flush(); } public void close() throws IOException { flush(); output.close(); } long debug_bytes = 0; public void processBatch() { // System.out.format("Batch of %d groups: ",groups.size()); if (groups.size() == 0) return; Osmformat.PrimitiveBlock.Builder primblock = Osmformat.PrimitiveBlock .newBuilder(); stringtable.clear(); // Preprocessing: Figure out the stringtable. for (PrimGroupWriterInterface i : groups) i.addStringsToStringtable(); stringtable.finish(); // Now, start serializing. for (PrimGroupWriterInterface i : groups) { PrimitiveGroup group = i.serialize(); if (group != null) primblock.addPrimitivegroup(group); } primblock.setStringtable(stringtable.serialize()); primblock.setGranularity(this.granularity); primblock.setDateGranularity(this.date_granularity); // Only generate data with offset (0,0) // Osmformat.PrimitiveBlock message = primblock.build(); // System.out.println(message); debug_bytes += message.getSerializedSize(); // if (message.getSerializedSize() > 1000000) // System.out.println(message); try { output.write(FileBlock.newInstance("OSMData", message .toByteString(), null)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new Error(e); } finally { batch_size = 0; groups.clear(); } // System.out.format("\n"); } /** Convert from a degrees represented as a double into the serialized offset in nanodegrees.. */ public long mapRawDegrees(double degrees) { return (long) ((degrees / .000000001)); } /** Convert from a degrees represented as a double into the serialized offset. */ public int mapDegrees(double degrees) { return (int) ((degrees / .0000001) / (granularity / 100)); } } OSM-binary-1.3.0/src.java/crosby/binary/StringTable.java000066400000000000000000000126571205754501500230270ustar00rootroot00000000000000/** Copyright (c) 2010 Scott A. Crosby. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package crosby.binary; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import com.google.protobuf.ByteString; /** * Class for mapping a set of strings to integers, giving frequently occuring * strings small integers. */ public class StringTable { public StringTable() { clear(); } private HashMap counts; private HashMap stringmap; private String set[]; public void incr(String s) { if (counts.containsKey(s)) { counts.put(s, new Integer(counts.get(s).intValue() + 1)); } else { counts.put(s, new Integer(1)); } } /** After the stringtable has been built, return the offset of a string in it. * * Note, value '0' is reserved for use as a delimiter and will not be returned. * @param s * @return */ public int getIndex(String s) { return stringmap.get(s).intValue(); } public void finish() { Comparator comparator = new Comparator() { @Override public int compare(final String s1, String s2) { int diff = counts.get(s2) - counts.get(s1); return diff; } }; /* Sort the stringtable */ /* When a string is referenced, strings in the stringtable with indices: 0 : Is reserved (used as a delimiter in tags A: 1 to 127 : Uses can be represented with 1 byte B: 128 to 128**2-1 : Uses can be represented with 2 bytes, C: 128*128 to X : Uses can be represented with 3 bytes in the unlikely case we have >16k strings in a block. No block will contain enough strings that we'll need 4 bytes. There are goals that will improve compression: 1. I want to use 1 bytes for the most frequently occurring strings, then 2 bytes, then 3 bytes. 2. I want to use low integers as frequently as possible (for better entropy encoding out of deflate) 3. I want the stringtable to compress as small as possible. Condition 1 is obvious. Condition 2 makes deflate compress stringtable references more effectively. When compressing entities, delta coding causes small positive integers to occur more frequently than larger integers. Even though a stringtable references to indices of 1 and 127 both use one byte in a decompressed file, the small integer bias causes deflate to use fewer bits to represent the smaller index when compressed. Condition 3 is most effective when adjacent strings in the stringtable have a lot of common substrings. So, when I decide on the master stringtable to use, I put the 127 most frequently occurring strings into A (accomplishing goal 1), and sort them by frequency (to accomplish goal 2), but for B and C, which contain the less progressively less frequently encountered strings, I sort them lexiconographically, to maximize goal 3 and ignoring goal 2. Goal 1 is the most important. Goal 2 helped enough to be worth it, and goal 3 was pretty minor, but all should be re-benchmarked. */ set = counts.keySet().toArray(new String[0]); if (set.length > 0) { // Sort based on the frequency. Arrays.sort(set, comparator); // Each group of keys that serializes to the same number of bytes is // sorted lexiconographically. // to maximize deflate compression. // Don't sort the first array. There's not likely to be much benefit, and we want frequent values to be small. //Arrays.sort(set, Math.min(0, set.length-1), Math.min(1 << 7, set.length-1)); Arrays.sort(set, Math.min(1 << 7, set.length-1), Math.min(1 << 14, set.length-1)); Arrays.sort(set, Math.min(1 << 14, set.length-1), Math.min(1 << 21, set.length-1), comparator); } stringmap = new HashMap(2 * set.length); for (int i = 0; i < set.length; i++) { stringmap.put(set[i], new Integer(i+1)); // Index 0 is reserved for use as a delimiter. } counts = null; } public void clear() { counts = new HashMap(100); stringmap = null; set = null; } public Osmformat.StringTable.Builder serialize() { Osmformat.StringTable.Builder builder = Osmformat.StringTable .newBuilder(); builder.addS(ByteString.copyFromUtf8("")); // Add a unused string at offset 0 which is used as a delimiter. for (int i = 0; i < set.length; i++) builder.addS(ByteString.copyFromUtf8(set[i])); return builder; } } OSM-binary-1.3.0/src.java/crosby/binary/file/000077500000000000000000000000001205754501500206525ustar00rootroot00000000000000OSM-binary-1.3.0/src.java/crosby/binary/file/BlockInputStream.java000066400000000000000000000025701205754501500247470ustar00rootroot00000000000000/** Copyright (c) 2010 Scott A. Crosby. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package crosby.binary.file; import java.io.EOFException; import java.io.IOException; import java.io.InputStream; public class BlockInputStream { // TODO: Should be seekable input stream! public BlockInputStream(InputStream input, BlockReaderAdapter adaptor) { this.input = input; this.adaptor = adaptor; } public void process() throws IOException { try { while (true) { FileBlock.process(input, adaptor); } } catch (EOFException e) { adaptor.complete(); } } public void close() throws IOException { input.close(); } InputStream input; BlockReaderAdapter adaptor; } OSM-binary-1.3.0/src.java/crosby/binary/file/BlockOutputStream.java000066400000000000000000000043401205754501500251450ustar00rootroot00000000000000/** Copyright (c) 2010 Scott A. Crosby. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package crosby.binary.file; import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; enum CompressFlags { NONE, DEFLATE } public class BlockOutputStream { public BlockOutputStream(OutputStream output) { this.outwrite = new DataOutputStream(output); this.compression = CompressFlags.DEFLATE; } public void setCompress(CompressFlags flag) { compression = flag; } public void setCompress(String s) { if (s.equals("none")) compression = CompressFlags.NONE; else if (s.equals("deflate")) compression = CompressFlags.DEFLATE; else throw new Error("Unknown compression type: " + s); } /** Write a block with the stream's default compression flag */ public void write(FileBlock block) throws IOException { this.write(block, compression); } /** Write a specific block with a specific compression flags */ public void write(FileBlock block, CompressFlags compression) throws IOException { FileBlockPosition ref = block.writeTo(outwrite, compression); writtenblocks.add(ref); } public void flush() throws IOException { outwrite.flush(); } public void close() throws IOException { outwrite.flush(); outwrite.close(); } OutputStream outwrite; List writtenblocks = new ArrayList(); CompressFlags compression; } OSM-binary-1.3.0/src.java/crosby/binary/file/BlockReaderAdapter.java000066400000000000000000000026701205754501500252000ustar00rootroot00000000000000/** Copyright (c) 2010 Scott A. Crosby. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package crosby.binary.file; /** An adaptor that receives blocks from an input stream */ public interface BlockReaderAdapter { /** * Does the reader understand this block? Does it want the data in it? * * A reference contains the metadata about a block and can saved --- or * stored ---- for future random access. However, during a strea read of the * file, does the user want this block? * * handleBlock will be called on all blocks that are not skipped, in file * order. * * */ boolean skipBlock(FileBlockPosition message); /** Called with the data in the block. */ void handleBlock(FileBlock message); /** Called when the file is fully read. */ void complete(); } OSM-binary-1.3.0/src.java/crosby/binary/file/FileBlock.java000066400000000000000000000126331205754501500233540ustar00rootroot00000000000000/** Copyright (c) 2010 Scott A. Crosby. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package crosby.binary.file; import java.io.DataOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Arrays; import java.util.zip.Deflater; import com.google.protobuf.ByteString; import crosby.binary.Fileformat; import crosby.binary.Fileformat.BlobHeader; /** A full fileblock object contains both the metadata and data of a fileblock */ public class FileBlock extends FileBlockBase { /** Contains the contents of a block for use or further processing */ ByteString data; // serialized Format.Blob /** Don't be noisy unless the warning occurs somewhat often */ static int warncount = 0; private FileBlock(String type, ByteString blob, ByteString indexdata) { super(type, indexdata); this.data = blob; } public static FileBlock newInstance(String type, ByteString blob, ByteString indexdata) { if (blob != null && blob.size() > MAX_BODY_SIZE/2) { System.err.println("Warning: Fileblock has body size too large and may be considered corrupt"); if (blob != null && blob.size() > MAX_BODY_SIZE-1024*1024) { throw new Error("This file has too many entities in a block. Parsers will reject it."); } } if (indexdata != null && indexdata.size() > MAX_HEADER_SIZE/2) { System.err.println("Warning: Fileblock has indexdata too large and may be considered corrupt"); if (indexdata != null && indexdata.size() > MAX_HEADER_SIZE-512) { throw new Error("This file header is too large. Parsers will reject it."); } } return new FileBlock(type, blob, indexdata); } protected void deflateInto(crosby.binary.Fileformat.Blob.Builder blobbuilder) { int size = data.size(); Deflater deflater = new Deflater(); deflater.setInput(data.toByteArray()); deflater.finish(); byte out[] = new byte[size]; deflater.deflate(out); if (!deflater.finished()) { // Buffer wasn't long enough. Be noisy. ++warncount; if (warncount > 10 && warncount%100 == 0) System.out.println("Compressed buffers are too short, causing extra copy"); out = Arrays.copyOf(out, size + size / 64 + 16); deflater.deflate(out, deflater.getTotalOut(), out.length - deflater.getTotalOut()); if (!deflater.finished()) { throw new Error("Internal error in compressor"); } } ByteString compressed = ByteString.copyFrom(out, 0, deflater .getTotalOut()); blobbuilder.setZlibData(compressed); deflater.end(); } public FileBlockPosition writeTo(OutputStream outwrite, CompressFlags flags) throws IOException { BlobHeader.Builder builder = Fileformat.BlobHeader .newBuilder(); if (indexdata != null) builder.setIndexdata(indexdata); builder.setType(type); Fileformat.Blob.Builder blobbuilder = Fileformat.Blob.newBuilder(); if (flags == CompressFlags.NONE) { blobbuilder.setRaw(data); blobbuilder.setRawSize(data.size()); } else { blobbuilder.setRawSize(data.size()); if (flags == CompressFlags.DEFLATE) deflateInto(blobbuilder); else throw new Error("Compression flag not understood"); } Fileformat.Blob blob = blobbuilder.build(); builder.setDatasize(blob.getSerializedSize()); Fileformat.BlobHeader message = builder.build(); int size = message.getSerializedSize(); // System.out.format("Outputed header size %d bytes, header of %d bytes, and blob of %d bytes\n", // size,message.getSerializedSize(),blob.getSerializedSize()); (new DataOutputStream(outwrite)).writeInt(size); message.writeTo(outwrite); long offset = -1; if (outwrite instanceof FileOutputStream) offset = ((FileOutputStream) outwrite).getChannel().position(); blob.writeTo(outwrite); return FileBlockPosition.newInstance(this, offset, size); } /** Reads or skips a fileblock. */ static void process(InputStream input, BlockReaderAdapter callback) throws IOException { FileBlockHead fileblock = FileBlockHead.readHead(input); if (callback.skipBlock(fileblock)) { // System.out.format("Attempt to skip %d bytes\n",header.getDatasize()); fileblock.skipContents(input); } else { callback.handleBlock(fileblock.readContents(input)); } } public ByteString getData() { return data; } } OSM-binary-1.3.0/src.java/crosby/binary/file/FileBlockBase.java000066400000000000000000000036031205754501500241440ustar00rootroot00000000000000/** Copyright (c) 2010 Scott A. Crosby. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package crosby.binary.file; import com.google.protobuf.ByteString; /** * Base class that contains the metadata about a fileblock. * * Subclasses of this include additional fields, such as byte offsets that let a * fileblock be read in a random-access fashion, or the data itself. * * @author crosby * */ public class FileBlockBase { /** If a block header is bigger than this, fail. We use excessively large header size as an indication of corrupt files */ static final int MAX_HEADER_SIZE = 64*1024; /** If a block's size is bigger than this, fail. We use excessively large block sizes as an indication of corrupt files */ static final int MAX_BODY_SIZE = 32*1024*1024; protected FileBlockBase(String type, ByteString indexdata) { this.type = type; this.indexdata = indexdata; } /** Identifies the type of the data within a block */ protected final String type; /** * Block metadata, stored in the index block and as a prefix for every * block. */ protected final ByteString indexdata; public String getType() { return type; } public ByteString getIndexData() { return indexdata; } } OSM-binary-1.3.0/src.java/crosby/binary/file/FileBlockHead.java000066400000000000000000000066241205754501500241410ustar00rootroot00000000000000/** Copyright (c) 2010 Scott A. Crosby. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package crosby.binary.file; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import com.google.protobuf.ByteString; import crosby.binary.Fileformat; /** * Intermediate representation of the header of a fileblock when a set of * fileblocks is read as in a stream. The data in the fileblock must be either * skipped (where the returned value is a reference to the fileblock) or parsed. * * @author crosby * */ public class FileBlockHead extends FileBlockReference { protected FileBlockHead(String type, ByteString indexdata) { super(type, indexdata); } /** * Read the header. After reading the header, either the contents must be * skipped or read */ static FileBlockHead readHead(InputStream input) throws IOException { DataInputStream datinput = new DataInputStream(input); int headersize = datinput.readInt(); // System.out.format("Header size %d %x\n",headersize,headersize); if (headersize > MAX_HEADER_SIZE) { throw new FileFormatException("Unexpectedly long header "+MAX_HEADER_SIZE+ " bytes. Possibly corrupt file."); } byte buf[] = new byte[headersize]; datinput.readFully(buf); // System.out.format("Read buffer for header of %d bytes\n",buf.length); Fileformat.BlobHeader header = Fileformat.BlobHeader .parseFrom(buf); FileBlockHead fileblock = new FileBlockHead(header.getType(), header .getIndexdata()); fileblock.datasize = header.getDatasize(); if (header.getDatasize() > MAX_BODY_SIZE) { throw new FileFormatException("Unexpectedly long body "+MAX_BODY_SIZE+ " bytes. Possibly corrupt file."); } fileblock.input = input; if (input instanceof FileInputStream) fileblock.data_offset = ((FileInputStream) input).getChannel() .position(); return fileblock; } /** * Assumes the stream is positioned over at the start of the data, skip over * it. * * @throws IOException */ void skipContents(InputStream input) throws IOException { if (input.skip(getDatasize()) != getDatasize()) assert false : "SHORT READ"; } /** * Assumes the stream is positioned over at the start of the data, read it * and return the complete FileBlock * * @throws IOException */ FileBlock readContents(InputStream input) throws IOException { DataInputStream datinput = new DataInputStream(input); byte buf[] = new byte[getDatasize()]; datinput.readFully(buf); return parseData(buf); } } OSM-binary-1.3.0/src.java/crosby/binary/file/FileBlockPosition.java000066400000000000000000000074221205754501500251010ustar00rootroot00000000000000/** Copyright (c) 2010 Scott A. Crosby. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package crosby.binary.file; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.zip.DataFormatException; import java.util.zip.Inflater; import com.google.protobuf.ByteString; import com.google.protobuf.InvalidProtocolBufferException; import crosby.binary.Fileformat; /** * Stores the position in the stream of a fileblock so that it can be easily * read in a random-access fashion. * * We can turn this into a 'real' block by appropriately seeking into the file * and doing a 'read'. * * */ public class FileBlockPosition extends FileBlockBase { protected FileBlockPosition(String type, ByteString indexdata) { super(type, indexdata); } /** Parse out and decompress the data part of a fileblock helper function. */ FileBlock parseData(byte buf[]) throws InvalidProtocolBufferException { FileBlock out = FileBlock.newInstance(type, null, indexdata); Fileformat.Blob blob = Fileformat.Blob.parseFrom(buf); if (blob.hasRaw()) { out.data = blob.getRaw(); } else if (blob.hasZlibData()) { byte buf2[] = new byte[blob.getRawSize()]; Inflater decompresser = new Inflater(); decompresser.setInput(blob.getZlibData().toByteArray()); // decompresser.getRemaining(); try { decompresser.inflate(buf2); } catch (DataFormatException e) { e.printStackTrace(); throw new Error(e); } assert (decompresser.finished()); decompresser.end(); out.data = ByteString.copyFrom(buf2); } return out; } public int getDatasize() { return datasize; } /* * Given any form of fileblock and an offset/length value, return a * reference that can be used to dereference and read the contents. */ static FileBlockPosition newInstance(FileBlockBase base, long offset, int length) { FileBlockPosition out = new FileBlockPosition(base.type, base.indexdata); out.datasize = length; out.data_offset = offset; return out; } public FileBlock read(InputStream input) throws IOException { if (input instanceof FileInputStream) { ((FileInputStream) input).getChannel().position(data_offset); byte buf[] = new byte[getDatasize()]; (new DataInputStream(input)).readFully(buf); return parseData(buf); } else { throw new Error("Random access binary reads require seekability"); } } /** * TODO: Convert this reference into a serialized representation that can be * stored. */ public ByteString serialize() { throw new Error("TODO"); } /** TODO: Parse a serialized representation of this block reference */ static FileBlockPosition parseFrom(ByteString b) { throw new Error("TODO"); } protected int datasize; /** Offset into the file of the data part of the block */ long data_offset; } OSM-binary-1.3.0/src.java/crosby/binary/file/FileBlockReference.java000066400000000000000000000032521205754501500251700ustar00rootroot00000000000000/** Copyright (c) 2010 Scott A. Crosby. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package crosby.binary.file; import java.io.IOException; import java.io.InputStream; import com.google.protobuf.ByteString; /** * A FileBlockPosition that remembers what file this is so that it can simply be * dereferenced */ public class FileBlockReference extends FileBlockPosition { /** * Convenience cache for storing the input this reference is contained * within so that it can be cached */ protected InputStream input; protected FileBlockReference(String type, ByteString indexdata) { super(type, indexdata); } public FileBlock read() throws IOException { return read(input); } static FileBlockPosition newInstance(FileBlockBase base, InputStream input, long offset, int length) { FileBlockReference out = new FileBlockReference(base.type, base.indexdata); out.datasize = length; out.data_offset = offset; out.input = input; return out; } } OSM-binary-1.3.0/src.java/crosby/binary/file/FileFormatException.java000066400000000000000000000017411205754501500254270ustar00rootroot00000000000000/** Copyright (c) 2010 Scott A. Crosby. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package crosby.binary.file; import java.io.IOException; public class FileFormatException extends IOException { public FileFormatException(String string) { super(string); } /** * */ private static final long serialVersionUID = -8128010128748910923L; } OSM-binary-1.3.0/src.java/crosby/binary/test/000077500000000000000000000000001205754501500207125ustar00rootroot00000000000000OSM-binary-1.3.0/src.java/crosby/binary/test/BuildTestFile.java000066400000000000000000000217131205754501500242600ustar00rootroot00000000000000/** Copyright (c) 2010 Scott A. Crosby. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package crosby.binary.test; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arrays; import java.util.Collections; import com.google.protobuf.ByteString; import crosby.binary.Fileformat.Blob; import crosby.binary.Fileformat.BlobHeader; import crosby.binary.Osmformat.DenseNodes; import crosby.binary.Osmformat.HeaderBlock; import crosby.binary.Osmformat.Info; import crosby.binary.Osmformat.Node; import crosby.binary.Osmformat.PrimitiveBlock; import crosby.binary.Osmformat.PrimitiveBlock.Builder; import crosby.binary.Osmformat.PrimitiveGroup; import crosby.binary.Osmformat.Relation; import crosby.binary.Osmformat.Relation.MemberType; import crosby.binary.Osmformat.StringTable; import crosby.binary.Osmformat.Way; import crosby.binary.file.BlockOutputStream; import crosby.binary.file.FileBlock; public class BuildTestFile { BlockOutputStream output; public static final long BILLION = 1000000000L; StringTable makeStringTable(String prefix) { return StringTable.newBuilder() .addS(ByteString.copyFromUtf8("")) // Never used. .addS(ByteString.copyFromUtf8(prefix+"Offset1")) .addS(ByteString.copyFromUtf8(prefix+"Offset2")) .addS(ByteString.copyFromUtf8(prefix+"Offset3")) .addS(ByteString.copyFromUtf8(prefix+"Offset4")) .addS(ByteString.copyFromUtf8(prefix+"Offset5")) .addS(ByteString.copyFromUtf8(prefix+"Offset6")) .addS(ByteString.copyFromUtf8(prefix+"Offset7")) .addS(ByteString.copyFromUtf8(prefix+"Offset8")) .build(); } void makeSimpleFileBlock1() throws IOException { PrimitiveBlock.Builder b1 = PrimitiveBlock.newBuilder(); b1.setStringtable(makeStringTable("B1")); b1.addPrimitivegroup( PrimitiveGroup.newBuilder() .addNodes(Node.newBuilder() .setId(101).setLat(13*10*1000*1000).setLon(-14*10*1000*1000) .addKeys(1).addVals(2)) .addNodes(Node.newBuilder() .setId(101).setLat(12345678).setLon(-23456789)) // Should be 1.2345678 degrees lat and -2.3456789 lon. ); b1.addPrimitivegroup( PrimitiveGroup.newBuilder() .addWays(Way.newBuilder() .setId(201) .addRefs(101).addRefs(1).addRefs(-1).addRefs(10).addRefs(-20) // Delta coded. Should be 101, 102, 101, 111, 91. .addKeys(2).addVals(1).addKeys(3).addVals(4)) .addWays(Way.newBuilder() .setId(-301) .addRefs(211).addRefs(1).addRefs(-1).addRefs(10).addRefs(-300) // Delta coded. Should be 211, 212, 211, 221, -79 .addKeys(4).addVals(3).addKeys(5).addVals(6)) .addWays(Way.newBuilder() .setId(401).addRefs(211).addRefs(1)) .addWays(Way.newBuilder() .setId(501)) ); b1.addPrimitivegroup( PrimitiveGroup.newBuilder() .addRelations(Relation.newBuilder() .setId(601) .addTypes(MemberType.NODE).addMemids(50).addRolesSid(2) .addTypes(MemberType.NODE).addMemids(3).addRolesSid(3) .addTypes(MemberType.WAY).addMemids(3).addRolesSid(4) .addTypes(MemberType.RELATION).addMemids(3).addRolesSid(5)) .addRelations(Relation.newBuilder() .setId(701) .addTypes(MemberType.RELATION).addMemids(60).addRolesSid(6) .addTypes(MemberType.RELATION).addMemids(5).addRolesSid(7) .addKeys(1).addVals(2))); b1.addPrimitivegroup( PrimitiveGroup.newBuilder() .setDense(DenseNodes.newBuilder() .addId(1001).addId(110).addId(-2000).addId(8889) .addLat(12*10000000).addLat(1500000).addLat(-12*10000000).addLat(-12*10000000) .addLon(-12*10000000).addLon(2500000).addLon(13*10000000).addLon(2*10000000) .addKeysVals(1).addKeysVals(2).addKeysVals(0) .addKeysVals(0) .addKeysVals(2).addKeysVals(3).addKeysVals(4).addKeysVals(5).addKeysVals(0) .addKeysVals(3).addKeysVals(3).addKeysVals(0) )); output.write(FileBlock.newInstance("OSMData", b1.build().toByteString(),null)); PrimitiveBlock.Builder b2 = PrimitiveBlock.newBuilder(); b2.setLatOffset(10*BILLION + 109208300) .setLonOffset(20*BILLION + 901802700) .setGranularity(1200); b2.setStringtable(makeStringTable("B2")); // Test out granularity stuff. b2.addPrimitivegroup( PrimitiveGroup.newBuilder() .addNodes(Node.newBuilder().setId(100000).setLat(0).setLon(0)) .addNodes(Node.newBuilder().setId(100001).setLat(1000).setLon(2000)) .addNodes(Node.newBuilder().setId(100002).setLat(1001).setLon(2001)) .addNodes(Node.newBuilder().setId(100003).setLat(1002).setLon(2002)) .addNodes(Node.newBuilder().setId(100004).setLat(1003).setLon(2003)) .addNodes(Node.newBuilder().setId(100005).setLat(1004).setLon(2004))); output.write(FileBlock.newInstance("OSMData", b2.build().toByteString(),null)); } BuildTestFile(String name, String compress) throws IOException { output = new BlockOutputStream(new FileOutputStream(name)); output.setCompress(compress); HeaderBlock.Builder b = HeaderBlock.newBuilder(); b.addRequiredFeatures("OsmSchema-V0.6").addRequiredFeatures("DenseNodes").setSource("QuickBrownFox"); output.write(FileBlock.newInstance("OSMHeader",b.build().toByteString(),null)); } public static void main(String [] args) { try { BuildTestFile out1a = new BuildTestFile("TestFile1-deflate.osm.pbf","deflate"); out1a.makeSimpleFileBlock1(); out1a.output.close(); BuildTestFile out1b = new BuildTestFile("TestFile1-none.osm.pbf","none"); out1b.makeSimpleFileBlock1(); out1b.output.close(); BuildTestFile out2 = new BuildTestFile("TestFile2-uncom.osm.pbf","deflate"); out2.makeGranFileBlock1(); out2.output.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } void makeGranFileBlock1() throws IOException { PrimitiveBlock.Builder b1 = PrimitiveBlock.newBuilder(); b1.setLatOffset(10*BILLION + 109208300) .setLonOffset(20*BILLION + 901802700) .setGranularity(1200) .setDateGranularity(2500); b1.setStringtable(makeStringTable("C1")); b1.addPrimitivegroup( PrimitiveGroup.newBuilder() .addNodes(Node.newBuilder() .setId(100001) .setLat(1000).setLon(2000) .setInfo(Info.newBuilder() .setTimestamp(1001) .setChangeset(-12) .setUid(21) .setUserSid(6) .build()) .build()) .addNodes(Node.newBuilder() .setId(100002) .setLat(1001).setLon(2001) .setInfo(Info.newBuilder() .setVersion(102) .setTimestamp(1002) .setChangeset(12) .setUid(-21) .setUserSid(5) .build()) .build()) .addNodes(Node.newBuilder() .setId(100003) .setLat(1003).setLon(2003) .setInfo(Info.newBuilder() .setVersion(103) .setUserSid(4) .build()) .build()) ) ; // The same, but with different granularities. PrimitiveBlock.Builder b2 = PrimitiveBlock.newBuilder(); b2.setLatOffset(12*BILLION + 303) .setLonOffset(22*BILLION + 404) .setGranularity(1401) .setDateGranularity(3003); b2.setStringtable(makeStringTable("C2")); b2.addPrimitivegroup( PrimitiveGroup.newBuilder() .addNodes(Node.newBuilder() .setId(100001) .addKeys(1).addVals(2) .addKeys(1).addVals(3) // Support multiple vals for a key. .addKeys(3).addVals(4) .setLat(1000).setLon(2000) .build()) .addNodes(Node.newBuilder() .setId(100002) .setLat(1001).setLon(2001) .build()) .addNodes(Node.newBuilder() .setId(100003) .setLat(1003).setLon(2003) .addKeys(5).addVals(6) .build()) ); output.write(FileBlock.newInstance("OSMData", b1.build().toByteString(),null)); output.write(FileBlock.newInstance("OSMData", b2.build().toByteString(),null)); } } OSM-binary-1.3.0/src/000077500000000000000000000000001205754501500142265ustar00rootroot00000000000000OSM-binary-1.3.0/src/Makefile000066400000000000000000000015731205754501500156740ustar00rootroot00000000000000 CXX = g++ CXXFLAGS = -O3 AR = ar all: libosmpbf.a ../include/osmpbf/fileformat.pb.h ../include/osmpbf/osmformat.pb.h libosmpbf.a: fileformat.pb.o osmformat.pb.o $(AR) -cr $@ fileformat.pb.o osmformat.pb.o %.pb.o: %.pb.cc $(CXX) $(CXXFLAGS) -c -o $@ $< %.pb.cc ../include/osmpbf/%.pb.h: %.proto protoc --proto_path=. --cpp_out=. $< cp *.pb.h ../include/osmpbf/ install: install -m 755 -g root -o root -d $(DESTDIR)/usr/lib install -m 644 -g root -o root libosmpbf.a $(DESTDIR)/usr/lib install -m 755 -g root -o root -d $(DESTDIR)/usr/include/osmpbf install -m 644 -g root -o root ../include/osmpbf/osmpbf.h $(DESTDIR)/usr/include/osmpbf install -m 644 -g root -o root ../include/osmpbf/fileformat.pb.h $(DESTDIR)/usr/include/osmpbf install -m 644 -g root -o root ../include/osmpbf/osmformat.pb.h $(DESTDIR)/usr/include/osmpbf clean: rm -f *.pb.h *.pb.cc *.pb.o libosmpbf.a OSM-binary-1.3.0/src/fileformat.proto000066400000000000000000000032621205754501500174460ustar00rootroot00000000000000/** Copyright (c) 2010 Scott A. Crosby. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ option optimize_for = LITE_RUNTIME; option java_package = "crosby.binary"; package OSMPBF; //protoc --java_out=../.. fileformat.proto // // STORAGE LAYER: Storing primitives. // message Blob { optional bytes raw = 1; // No compression optional int32 raw_size = 2; // When compressed, the uncompressed size // Possible compressed versions of the data. optional bytes zlib_data = 3; // PROPOSED feature for LZMA compressed data. SUPPORT IS NOT REQUIRED. optional bytes lzma_data = 4; // Formerly used for bzip2 compressed data. Depreciated in 2010. optional bytes OBSOLETE_bzip2_data = 5 [deprecated=true]; // Don't reuse this tag number. } /* A file contains an sequence of fileblock headers, each prefixed by their length in network byte order, followed by a data block containing the actual data. types staring with a "_" are reserved. */ message BlobHeader { required string type = 1; optional bytes indexdata = 2; required int32 datasize = 3; } OSM-binary-1.3.0/src/osmformat.proto000066400000000000000000000210011205754501500173140ustar00rootroot00000000000000/** Copyright (c) 2010 Scott A. Crosby. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ option optimize_for = LITE_RUNTIME; option java_package = "crosby.binary"; package OSMPBF; /* OSM Binary file format This is the master schema file of the OSM binary file format. This file is designed to support limited random-access and future extendability. A binary OSM file consists of a sequence of FileBlocks (please see fileformat.proto). The first fileblock contains a serialized instance of HeaderBlock, followed by a sequence of PrimitiveBlock blocks that contain the primitives. Each primitiveblock is designed to be independently parsable. It contains a string table storing all strings in that block (keys and values in tags, roles in relations, usernames, etc.) as well as metadata containing the precision of coordinates or timestamps in that block. A primitiveblock contains a sequence of primitive groups, each containing primitives of the same type (nodes, densenodes, ways, relations). Coordinates are stored in signed 64-bit integers. Lat&lon are measured in units nanodegrees. The default of granularity of 100 nanodegrees corresponds to about 1cm on the ground, and a full lat or lon fits into 32 bits. Converting an integer to a lattitude or longitude uses the formula: $OUT = IN * granularity / 10**9$. Many encoding schemes use delta coding when representing nodes and relations. */ ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// /* Contains the file header. */ message HeaderBlock { optional HeaderBBox bbox = 1; /* Additional tags to aid in parsing this dataset */ repeated string required_features = 4; repeated string optional_features = 5; optional string writingprogram = 16; optional string source = 17; // From the bbox field. /* Tags that allow continuing an Osmosis replication */ // replication timestamp, expressed in seconds since the epoch, // otherwise the same value as in the "timestamp=..." field // in the state.txt file used by Osmosis optional int64 osmosis_replication_timestamp = 32; // replication sequence number (sequenceNumber in state.txt) optional int64 osmosis_replication_sequence_number = 33; // replication base URL (from Osmosis' configuration.txt file) optional string osmosis_replication_base_url = 34; } /** The bounding box field in the OSM header. BBOX, as used in the OSM header. Units are always in nanodegrees -- they do not obey granularity rules. */ message HeaderBBox { required sint64 left = 1; required sint64 right = 2; required sint64 top = 3; required sint64 bottom = 4; } /////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////// message PrimitiveBlock { required StringTable stringtable = 1; repeated PrimitiveGroup primitivegroup = 2; // Granularity, units of nanodegrees, used to store coordinates in this block optional int32 granularity = 17 [default=100]; // Offset value between the output coordinates coordinates and the granularity grid in unites of nanodegrees. optional int64 lat_offset = 19 [default=0]; optional int64 lon_offset = 20 [default=0]; // Granularity of dates, normally represented in units of milliseconds since the 1970 epoch. optional int32 date_granularity = 18 [default=1000]; // Proposed extension: //optional BBox bbox = XX; } // Group of OSMPrimitives. All primitives in a group must be the same type. message PrimitiveGroup { repeated Node nodes = 1; optional DenseNodes dense = 2; repeated Way ways = 3; repeated Relation relations = 4; repeated ChangeSet changesets = 5; } /** String table, contains the common strings in each block. Note that we reserve index '0' as a delimiter, so the entry at that index in the table is ALWAYS blank and unused. */ message StringTable { repeated bytes s = 1; } /* Optional metadata that may be included into each primitive. */ message Info { optional int32 version = 1 [default = -1]; optional int64 timestamp = 2; optional int64 changeset = 3; optional int32 uid = 4; optional uint32 user_sid = 5; // String IDs // The visible flag is used to store history information. It indicates that // the current object version has been created by a delete operation on the // OSM API. // When a writer sets this flag, it MUST add a required_features tag with // value "HistoricalInformation" to the HeaderBlock. // If this flag is not available for some object it MUST be assumed to be // true if the file has the required_features tag "HistoricalInformation" // set. optional bool visible = 6; } /** Optional metadata that may be included into each primitive. Special dense format used in DenseNodes. */ message DenseInfo { repeated int32 version = 1 [packed = true]; repeated sint64 timestamp = 2 [packed = true]; // DELTA coded repeated sint64 changeset = 3 [packed = true]; // DELTA coded repeated sint32 uid = 4 [packed = true]; // DELTA coded repeated sint32 user_sid = 5 [packed = true]; // String IDs for usernames. DELTA coded // The visible flag is used to store history information. It indicates that // the current object version has been created by a delete operation on the // OSM API. // When a writer sets this flag, it MUST add a required_features tag with // value "HistoricalInformation" to the HeaderBlock. // If this flag is not available for some object it MUST be assumed to be // true if the file has the required_features tag "HistoricalInformation" // set. repeated bool visible = 6 [packed = true]; } // THIS IS STUB DESIGN FOR CHANGESETS. NOT USED RIGHT NOW. // TODO: REMOVE THIS? message ChangeSet { required int64 id = 1; // // // Parallel arrays. // repeated uint32 keys = 2 [packed = true]; // String IDs. // repeated uint32 vals = 3 [packed = true]; // String IDs. // // optional Info info = 4; // optional int64 created_at = 8; // optional int64 closetime_delta = 9; // optional bool open = 10; // optional HeaderBBox bbox = 11; } message Node { required sint64 id = 1; // Parallel arrays. repeated uint32 keys = 2 [packed = true]; // String IDs. repeated uint32 vals = 3 [packed = true]; // String IDs. optional Info info = 4; // May be omitted in omitmeta required sint64 lat = 8; required sint64 lon = 9; } /* Used to densly represent a sequence of nodes that do not have any tags. We represent these nodes columnwise as five columns: ID's, lats, and lons, all delta coded. When metadata is not omitted, We encode keys & vals for all nodes as a single array of integers containing key-stringid and val-stringid, using a stringid of 0 as a delimiter between nodes. ( ( )* '0' )* */ message DenseNodes { repeated sint64 id = 1 [packed = true]; // DELTA coded //repeated Info info = 4; optional DenseInfo denseinfo = 5; repeated sint64 lat = 8 [packed = true]; // DELTA coded repeated sint64 lon = 9 [packed = true]; // DELTA coded // Special packing of keys and vals into one array. May be empty if all nodes in this block are tagless. repeated int32 keys_vals = 10 [packed = true]; } message Way { required int64 id = 1; // Parallel arrays. repeated uint32 keys = 2 [packed = true]; repeated uint32 vals = 3 [packed = true]; optional Info info = 4; repeated sint64 refs = 8 [packed = true]; // DELTA coded } message Relation { enum MemberType { NODE = 0; WAY = 1; RELATION = 2; } required int64 id = 1; // Parallel arrays. repeated uint32 keys = 2 [packed = true]; repeated uint32 vals = 3 [packed = true]; optional Info info = 4; // Parallel arrays repeated int32 roles_sid = 8 [packed = true]; repeated sint64 memids = 9 [packed = true]; // DELTA encoded repeated MemberType types = 10 [packed = true]; } OSM-binary-1.3.0/tools/000077500000000000000000000000001205754501500145775ustar00rootroot00000000000000OSM-binary-1.3.0/tools/.gitignore000066400000000000000000000000171205754501500165650ustar00rootroot00000000000000osmpbf-outline OSM-binary-1.3.0/tools/Makefile000066400000000000000000000005441205754501500162420ustar00rootroot00000000000000 CXX = g++ CXXFLAGS = -g -I../include LDFLAGS = -L../src -lpthread -lz -lprotobuf-lite -losmpbf all: osmpbf-outline osmpbf-outline: osmpbf-outline.cpp $(CXX) $(CXXFLAGS) -o $@ $< $(LDFLAGS) install: install -m 755 -g root -o root -d $(DESTDIR)/usr/bin install -m 644 -g root -o root -s osmpbf-outline $(DESTDIR)/usr/bin clean: rm -f osmpbf-outline OSM-binary-1.3.0/tools/osmpbf-outline.1000066400000000000000000000023021205754501500176210ustar00rootroot00000000000000.TH osmpbf-outline 1 LOCAL .SH NAME osmpbf-outline - outline the content of an .osm.pbf or .osh.pbf file .SH SYNOPSIS .B osmpbf-outline [ .B --color ] .I filename .SH DESCRIPTION .I osmpbf-outline generates an colored outline an .osm.pbf or .osh.pbf file, revealing its inner structure. It does not decode all information nor does it check for of possible mistakes, but it will warn on some bad mistakes one can make when generating .osm.pbf files. This tool can be used by authors of .osm.pbf readers and writers and is useful to understand the inner blob/block structure of .osm.pbf files. Its source code is heavily documented and can be used as a good start on how to read- and write .osm.pbf files with C++. .SH OPTIONS .TP 5 .B -c --color usually colorization is disabled when writing to a file descriptor that is no tty (eg. when the output is piped to a file or to a pager like to more or less). Some pagers can handle ANSI color code ( .I more can, less can when invoked as .I less -R ). To enforce colorization when working with such pagers, specify the .B --color flag. .SH EXAMPLES osmpbf-outline germany.osm.pbf .SH AUTHOR Peter Koerner Jochen Topf OSM-binary-1.3.0/tools/osmpbf-outline.cpp000066400000000000000000000276761205754501500202700ustar00rootroot00000000000000// used for va_list in debug-print methods #include // file io lib #include // getopt is used to check for the --color-flag #include // zlib compression is used inside the pbf blobs #include // netinet provides the network-byte-order conversion function #include // this is the header to pbf format #include // should the output use color? bool usecolor = false; // buffer for reading a compressed blob from file char buffer[OSMPBF::max_uncompressed_blob_size]; // buffer for decompressing the blob char unpack_buffer[OSMPBF::max_uncompressed_blob_size]; // pbf struct of a BlobHeader OSMPBF::BlobHeader blobheader; // pbf struct of a Blob OSMPBF::Blob blob; // pbf struct of an OSM HeaderBlock OSMPBF::HeaderBlock headerblock; // pbf struct of an OSM PrimitiveBlock OSMPBF::PrimitiveBlock primblock; // prints a formatted message to stdout, optionally color coded void msg(const char* format, int color, va_list args) { if(usecolor) { fprintf(stdout, "\x1b[0;%dm", color); } vfprintf(stdout, format, args); if(usecolor) { fprintf(stdout, "\x1b[0m\n"); } else { fprintf(stdout, "\n"); } } // prints a formatted message to stderr, color coded to red void err(const char* format, ...) { va_list args; va_start(args, format); msg(format, 31, args); va_end(args); exit(1); } // prints a formatted message to stderr, color coded to yellow void warn(const char* format, ...) { va_list args; va_start(args, format); msg(format, 33, args); va_end(args); } // prints a formatted message to stderr, color coded to green void info(const char* format, ...) { va_list args; va_start(args, format); msg(format, 32, args); va_end(args); } // prints a formatted message to stderr, color coded to white void debug(const char* format, ...) { va_list args; va_start(args, format); msg(format, 37, args); va_end(args); } // application main method int main(int argc, char *argv[]) { // check if the output is a tty so we can use colors usecolor = isatty(1); static struct option long_options[] = { {"color", no_argument, 0, 'c'}, {0,0,0,0} }; while (1) { int c = getopt_long(argc, argv, "c", long_options, 0); if (c == -1) break; switch (c) { case 'c': usecolor = true; break; default: exit(1); } } // check for proper command line args if(optind != argc-1) err("usage: %s [--color] file.osm.pbf", argv[0]); // open specified file FILE *fp = fopen(argv[optind], "r"); // read while the file has not reached its end while(!feof(fp)) { // storage of size, used multiple times int32_t sz; // read the first 4 bytes of the file, this is the size of the blob-header if(fread(&sz, sizeof(sz), 1, fp) != 1) break; // end of file reached // convert the size from network byte-order to host byte-order sz = ntohl(sz); // ensure the blob-header is smaller then MAX_BLOB_HEADER_SIZE if(sz > OSMPBF::max_blob_header_size) err("blob-header-size is bigger then allowed (%u > %u)", sz, OSMPBF::max_blob_header_size); // read the blob-header from the file if(fread(buffer, sz, 1, fp) != 1) err("unable to read blob-header from file"); // parse the blob-header from the read-buffer if(!blobheader.ParseFromArray(buffer, sz)) err("unable to parse blob header"); // tell about the blob-header info("\nBlobHeader (%d bytes)", sz); debug(" type = %s", blobheader.type().c_str()); // size of the following blob sz = blobheader.datasize(); debug(" datasize = %u", sz); // optional indexdata if(blobheader.has_indexdata()) debug(" indexdata = %u bytes", blobheader.indexdata().size()); // ensure the blob is smaller then MAX_BLOB_SIZE if(sz > OSMPBF::max_uncompressed_blob_size) err("blob-size is bigger then allowed (%u > %u)", sz, OSMPBF::max_uncompressed_blob_size); // read the blob from the file if(fread(buffer, sz, 1, fp) != 1) err("unable to read blob from file"); // parse the blob from the read-buffer if(!blob.ParseFromArray(buffer, sz)) err("unable to parse blob"); // tell about the blob-header info("Blob (%d bytes)", sz); // set when we find at least one data stream bool found_data = false; // if the blob has uncompressed data if(blob.has_raw()) { // we have at least one datastream found_data = true; // size of the blob-data sz = blob.raw().size(); // check that raw_size is set correctly if(sz != blob.raw_size()) warn(" reports wrong raw_size: %u bytes", blob.raw_size()); // tell about the blob-data debug(" contains uncompressed data: %u bytes", sz); // copy the uncompressed data over to the unpack_buffer memcpy(unpack_buffer, buffer, sz); } // if the blob has zlib-compressed data if(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) warn(" contains several data streams"); // we have at least one datastream found_data = true; // the size of the compressesd data sz = blob.zlib_data().size(); // tell about the compressed data debug(" contains zlib-compressed data: %u bytes", sz); debug(" uncompressed size: %u bytes", blob.raw_size()); // zlib information z_stream z; // next byte to decompress z.next_in = (unsigned char*) blob.zlib_data().c_str(); // number of bytes to decompress z.avail_in = sz; // place of next decompressed byte z.next_out = (unsigned char*) unpack_buffer; // space for decompressed data z.avail_out = blob.raw_size(); // misc z.zalloc = Z_NULL; z.zfree = Z_NULL; z.opaque = Z_NULL; if(inflateInit(&z) != Z_OK) { err(" failed to init zlib stream"); } if(inflate(&z, Z_FINISH) != Z_STREAM_END) { err(" failed to inflate zlib stream"); } if(inflateEnd(&z) != Z_OK) { err(" failed to deinit zlib stream"); } // unpacked size sz = z.total_out; } // if the blob has lzma-compressed data if(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) warn(" contains several data streams"); // we have at least one datastream found_data = true; // tell about the compressed data debug(" contains lzma-compressed data: %u bytes", blob.lzma_data().size()); debug(" uncompressed size: %u bytes", blob.raw_size()); // issue a warning, lzma compression is not yet supported err(" lzma-decompression is not supported"); } // check we have at least one data-stream if(!found_data) err(" does not contain any known data stream"); // switch between different blob-types if(blobheader.type() == "OSMHeader") { // tell about the OSMHeader blob info(" OSMHeader"); // parse the HeaderBlock from the blob if(!headerblock.ParseFromArray(unpack_buffer, sz)) err("unable to parse header block"); // tell about the bbox if(headerblock.has_bbox()) { OSMPBF::HeaderBBox bbox = headerblock.bbox(); debug(" bbox: %.7f,%.7f,%.7f,%.7f", (double)bbox.left() / OSMPBF::lonlat_resolution, (double)bbox.bottom() / OSMPBF::lonlat_resolution, (double)bbox.right() / OSMPBF::lonlat_resolution, (double)bbox.top() / OSMPBF::lonlat_resolution); } // tell about the required features for(int i = 0, l = headerblock.required_features_size(); i < l; i++) debug(" required_feature: %s", headerblock.required_features(i).c_str()); // tell about the optional features for(int i = 0, l = headerblock.optional_features_size(); i < l; i++) debug(" required_feature: %s", headerblock.optional_features(i).c_str()); // tell about the writing program if(headerblock.has_writingprogram()); debug(" writingprogram: %s", headerblock.writingprogram().c_str()); // tell about the source if(headerblock.has_source()) debug(" source: %s", headerblock.source().c_str()); } else if(blobheader.type() == "OSMData") { // tell about the OSMData blob info(" OSMData"); // parse the PrimitiveBlock from the blob if(!primblock.ParseFromArray(unpack_buffer, sz)) err("unable to parse primitive block"); // tell about the block's meta info debug(" granularity: %u", primblock.granularity()); debug(" lat_offset: %u", primblock.lat_offset()); debug(" lon_offset: %u", primblock.lon_offset()); debug(" date_granularity: %u", primblock.date_granularity()); // tell about the stringtable debug(" stringtable: %u items", primblock.stringtable().s_size()); // number of PrimitiveGroups debug(" primitivegroups: %u groups", primblock.primitivegroup_size()); // iterate over all PrimitiveGroups for(int i = 0, l = primblock.primitivegroup_size(); i < l; i++) { // one PrimitiveGroup from the the Block OSMPBF::PrimitiveGroup pg = primblock.primitivegroup(i); bool found_items=false; // tell about nodes if(pg.nodes_size() > 0) { found_items = true; debug(" nodes: %d", pg.nodes_size()); if(pg.nodes(0).has_info()) debug(" with meta-info"); } // tell about dense nodes if(pg.has_dense()) { found_items = true; debug(" dense nodes: %d", pg.dense().id_size()); if(pg.dense().has_denseinfo()) debug(" with meta-info"); } // tell about ways if(pg.ways_size() > 0) { found_items = true; debug(" ways: %d", pg.ways_size()); if(pg.ways(0).has_info()) debug(" with meta-info"); } // tell about relations if(pg.relations_size() > 0) { found_items = true; debug(" relations: %d", pg.relations_size()); if(pg.relations(0).has_info()) debug(" with meta-info"); } if(!found_items) warn(" contains no items"); } } else { // unknown blob type warn(" unknown blob type: %s", blobheader.type().c_str()); } } // close the file pointer fclose(fp); // clean up the protobuf lib google::protobuf::ShutdownProtobufLibrary(); }