Esempio n. 1
0
        public static int ReadBinary(byte[] buf, int ofs, out OsmRelation relation)
        {
            int   p = 0;
            ulong tmp;

            p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp);
            long id = (long)tmp;

            p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp);
            var values = new KeyValuePair <string, string> [tmp];

            for (int i = 0; i < values.Length; i++)
            {
                string key, val;
                p        += ProtoBuf.ReadString(buf, ofs + p, out key);
                p        += ProtoBuf.ReadString(buf, ofs + p, out val);
                values[i] = new KeyValuePair <string, string>(key, val);
            }
            p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp);
            var members = new OsmRelationMember[tmp];

            for (int i = 0; i < members.Length; i++)
            {
                p += OsmRelationMember.ReadBinary(buf, ofs + p, out members[i]);
            }
            relation = new OsmRelation(id, values, members);
            return(p);
        }
Esempio n. 2
0
        public static int ReadBinary(byte[] buf, int ofs, out OsmNode node)
        {
            int   p = 0;
            ulong tmp;

            p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp);
            long id = (long)tmp;

            p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp);
            int latCode = ProtoBuf.SignedInt32((uint)tmp);

            p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp);
            int lonCode = ProtoBuf.SignedInt32((uint)tmp);

            p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp);
            var values = tmp == 0 ? emptyValues : new KeyValuePair <string, string> [tmp];

            for (int i = 0; i < values.Length; i++)
            {
                string key, val;
                p        += ProtoBuf.ReadString(buf, ofs + p, out key);
                p        += ProtoBuf.ReadString(buf, ofs + p, out val);
                values[i] = new KeyValuePair <string, string>(key, val);
            }
            node = new OsmNode(id, latCode, lonCode, values);
            return(p);
        }
Esempio n. 3
0
        public static int ReadBinary(byte[] buf, int ofs, out OsmWay way)
        {
            int   p = 0;
            ulong tmp;

            p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp);
            long id = (long)tmp;

            p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp);
            var values = new KeyValuePair <string, string> [tmp];

            for (int i = 0; i < values.Length; i++)
            {
                string key, val;
                p        += ProtoBuf.ReadString(buf, ofs + p, out key);
                p        += ProtoBuf.ReadString(buf, ofs + p, out val);
                values[i] = new KeyValuePair <string, string>(key, val);
            }
            p  += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp);
            way = new OsmWay(id, values, new long[tmp]);
            for (int i = 0; i < way.nodeIds.Length; i++)
            {
                p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp);
                way.nodeIds[i] = (long)tmp;
            }
            return(p);
        }
Esempio n. 4
0
        /// <summary>
        /// Dekodiert die Werte aus einem PBF-Stream
        /// </summary>
        /// <param name="buf">Buffer, worraus die Werte gelesen werden sollen</param>
        /// <param name="ofs">Startposition innerhalb des Buffers</param>
        /// <param name="box">BBox-Struktur mit den ausgelesenen Werten</param>
        /// <returns>Anzahl der gelesenen Bytes aus dem Buffer</returns>
        public static int Decode(byte[] buf, int ofs, out HeaderBBox box)
        {
            /*****
            * message HeaderBBox
            * {
            *   required sint64 left = 1;
            *   required sint64 right = 2;
            *   required sint64 top = 3;
            *   required sint64 bottom = 4;
            * }
            *****/

            box = new HeaderBBox();

            ulong tmp;
            int   len    = ProtoBuf.ReadVarInt(buf, ofs, out tmp);
            int   endLen = len + (int)tmp;

            // --- required sint64 left = 1; ---
            if (buf[ofs + len++] != (1 << 3 | 0))
            {
                throw new PbfParseException();
            }
            len     += ProtoBuf.ReadVarInt(buf, ofs + len, out tmp);
            box.left = ProtoBuf.SignedInt64(tmp);

            // --- required sint64 right = 2; ---
            if (buf[ofs + len++] != (2 << 3 | 0))
            {
                throw new PbfParseException();
            }
            len      += ProtoBuf.ReadVarInt(buf, ofs + len, out tmp);
            box.right = ProtoBuf.SignedInt64(tmp);

            // --- required sint64 top = 3; ---
            if (buf[ofs + len++] != (3 << 3 | 0))
            {
                throw new PbfParseException();
            }
            len    += ProtoBuf.ReadVarInt(buf, ofs + len, out tmp);
            box.top = ProtoBuf.SignedInt64(tmp);

            // --- required sint64 bottom = 4; ---
            if (buf[ofs + len++] != (4 << 3 | 0))
            {
                throw new PbfParseException();
            }
            len       += ProtoBuf.ReadVarInt(buf, ofs + len, out tmp);
            box.bottom = ProtoBuf.SignedInt64(tmp);

            if (len != endLen)
            {
                throw new PbfParseException();
            }

            return(len);
        }
        static void DecodeNodeBlock(byte[] buf, int bufLen)
        {
            long id  = 0;
            long pos = 0;

            var keyDict = new Dictionary <string, int>();
            var valDict = new Dictionary <string, int>();

            var outputNodes  = new List <DecodeNodeBlockValue>();
            var outputValues = new List <int>();

            for (int p = 0; p < bufLen;)
            {
                DecodeNodeBlockValue nodeValue;
                ulong tmp;
                p            += ProtoBuf.ReadVarInt(buf, p, out tmp);
                id           += ProtoBuf.SignedInt64(tmp);
                nodeValue.id  = id;
                p            += ProtoBuf.ReadVarInt(buf, p, out tmp);
                pos          += ProtoBuf.SignedInt64(tmp);
                nodeValue.pos = pos;
                p            += ProtoBuf.ReadVarInt(buf, p, out tmp);
                int valueCount = (int)(uint)tmp;
                nodeValue.valueCount = valueCount;
                nodeValue.valuePos   = outputValues.Count / 2;
                for (int i = 0; i < valueCount; i++)
                {
                    string key, val;
                    p += ProtoBuf.ReadString(buf, p, out key);
                    if (!keyDict.ContainsKey(key))
                    {
                        keyDict.Add(key, keyDict.Count);
                    }
                    outputValues.Add(keyDict[key]);
                    p += ProtoBuf.ReadString(buf, p, out val);
                    if (!valDict.ContainsKey(val))
                    {
                        valDict.Add(val, valDict.Count);
                    }
                    outputValues.Add(valDict[val]);
                }
                outputNodes.Add(nodeValue);
            }

            // --- sort ---
            long lastId = outputNodes[outputNodes.Count - 1].id;

            Console.WriteLine(" sort: " + outputNodes.Count + " nodes");
            outputNodes.Sort((x, y) => x.pos.CompareTo(y.pos));

            // --- write ---
            Console.WriteLine("write: " + outputNodes.Count + " nodes");
            WriteNodeBlock(keyDict, valDict, outputValues, outputNodes, lastId);
        }
Esempio n. 6
0
        static int ReadNodesPath(byte[] buf, int ofs, out OsmNode[] nodes)
        {
            ulong tmp;
            int   p = ProtoBuf.ReadVarInt(buf, ofs, out tmp);

            nodes = new OsmNode[tmp];
            for (int i = 0; i < nodes.Length; i++)
            {
                p += OsmNode.ReadBinary(buf, ofs + p, out nodes[i]);
            }
            return(p);
        }
Esempio n. 7
0
        public static int ReadBinary(byte[] buf, int ofs, out OsmRelationMember member)
        {
            int   p = 0;
            ulong tmp;

            p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp);
            long   id   = (long)tmp;
            var    type = (MemberType)buf[ofs + p]; p++;
            string str;

            p     += ProtoBuf.ReadString(buf, ofs + p, out str);
            member = new OsmRelationMember(id, type, str);
            return(p);
        }
Esempio n. 8
0
        OsmRelation GetRelationData(long relationIndex, out OsmWay[] osmWays, out OsmNode[] osmNodes)
        {
            var buf = cacheBuffer;

            cacheData.Position = relationIndex;
            cacheData.Read(buf, 0, sizeof(int));

            int size = BitConverter.ToInt32(buf, 0) - sizeof(int);

            if (cacheData.Read(buf, sizeof(int), size) != size)
            {
                throw new IOException("EOF?");
            }

            int         p = sizeof(int);
            OsmRelation osmRelation;

            p += OsmRelation.ReadBinary(buf, p, out osmRelation);
            ulong tmp;

            p      += ProtoBuf.ReadVarInt(buf, p, out tmp);
            osmWays = new OsmWay[tmp];
            for (int i = 0; i < osmWays.Length; i++)
            {
                p += OsmWay.ReadBinary(buf, p, out osmWays[i]);
            }
            DecodeDelta(osmWays);
            p       += ProtoBuf.ReadVarInt(buf, p, out tmp);
            osmNodes = new OsmNode[tmp];
            for (int i = 0; i < osmNodes.Length; i++)
            {
                p += OsmNode.ReadBinary(buf, p, out osmNodes[i]);
            }
            DecodeDelta(osmNodes);
            if (p != size + sizeof(int))
            {
                throw new PbfParseException();
            }

            return(osmRelation);
        }
Esempio n. 9
0
        public OsmCache(OsmPbfReader pbf)
        {
            if (!File.Exists(CacheIndexFile))
            {
                File.WriteAllBytes(CacheIndexFile, new byte[0]);
            }
            if (!File.Exists(CacheDataFile))
            {
                File.WriteAllBytes(CacheDataFile, new byte[1]);
            }

            // --- Read Index ---
            relationsIndex = new Dictionary <long, long>();
            waysIndex      = new Dictionary <long, long>();
            nodesIndex     = new Dictionary <long, long>();
            var buf = File.ReadAllBytes(CacheIndexFile);

            for (int p = 0; p < buf.Length;)
            {
                var   type = (MemberType)buf[p++];
                ulong id, index;
                p += ProtoBuf.ReadVarInt(buf, p, out id);
                p += ProtoBuf.ReadVarInt(buf, p, out index);
                switch (type)
                {
                case MemberType.Relation: relationsIndex.Add((long)id, (long)index); break;

                case MemberType.Way: waysIndex.Add((long)id, (long)index); break;

                case MemberType.Node: nodesIndex.Add((long)id, (long)index); break;

                default: throw new Exception("unknown type?");
                }
            }

            cacheData = new FileStream(CacheDataFile, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
            pbfReader = pbf;
        }
Esempio n. 10
0
        /// <summary>
        /// Dekodiert die Werte aus einem PBF-Stream
        /// </summary>
        /// <param name="buf">Buffer, worraus die Werte gelesen werden sollen</param>
        /// <param name="ofs">Startposition innerhalb des Buffers</param>
        /// <param name="headerBlock">HeaderBlock-Struktur mit den ausgelesenen Werten</param>
        /// <returns>Anzahl der gelesenen Bytes aus dem Buffer</returns>
        public static int Decode(byte[] buf, int ofs, out HeaderBlock headerBlock)
        {
            /*****
            * 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;
            * }
            *****/

            int   len = 0;
            ulong tmp;
            var   tmps = new List <string>();

            headerBlock = new HeaderBlock();

            // --- optional HeaderBBox bbox = 1; ---
            if (buf[ofs + len] == (1 << 3 | 2))
            {
                len++;
                len += HeaderBBox.Decode(buf, ofs + len, out headerBlock.bbox);
            }

            // --- repeated string required_features = 4; ---
            tmps.Clear();
            while (buf[ofs + len] == (4 << 3 | 2))
            {
                len++;
                string feature;
                len += ProtoBuf.ReadString(buf, ofs + len, out feature);
                tmps.Add(feature);
                switch (feature)
                {
                case "OsmSchema-V0.6": break;

                case "DenseNodes": break;

                default: throw new PbfParseException("required feature not supported: \"" + feature + "\"");
                }
            }
            headerBlock.requiredFeatures = tmps.ToArray();

            // --- repeated string optional_features = 5; ---
            tmps.Clear();
            while (buf[ofs + len] == (5 << 3 | 2))
            {
                len++;
                string feature;
                len += ProtoBuf.ReadString(buf, ofs + len, out feature);
                tmps.Add(feature);
                switch (feature)
                {
                case "Has_Metadata": break;

                case "Sort.Type_then_ID": break;

                default: throw new PbfParseException("optional feature not supported: \"" + feature + "\"");
                }
            }
            headerBlock.optionalFeatures = tmps.ToArray();

            // --- optional string writingprogram = 16; ---
            if (ProtoBuf.PeekVarInt(buf, ofs + len) == (16 << 3 | 2))
            {
                len += 2;
                len += ProtoBuf.ReadString(buf, ofs + len, out headerBlock.writingprogram);
            }
            else
            {
                headerBlock.writingprogram = "";
            }

            // --- optional string source = 17; ---
            if (ProtoBuf.PeekVarInt(buf, ofs + len) == (17 << 3 | 2))
            {
                len += 2;
                len += ProtoBuf.ReadString(buf, ofs + len, out headerBlock.source);
            }
            else
            {
                headerBlock.source = "";
            }

            // --- optional int64 osmosis_replication_timestamp = 32; ---
            if (ProtoBuf.PeekVarInt(buf, ofs + len) == (32 << 3 | 0))
            {
                len += 2;
                len += ProtoBuf.ReadVarInt(buf, ofs + len, out tmp);
                headerBlock.replicationTimestamp = (long)tmp;
            }

            // --- optional int64 osmosis_replication_sequence_number = 33; ---
            if (ProtoBuf.PeekVarInt(buf, ofs + len) == (33 << 3 | 0))
            {
                len += 2;
                len += ProtoBuf.ReadVarInt(buf, ofs + len, out tmp);
                headerBlock.replicationSequenceNumber = (long)tmp;
            }

            // --- optional string osmosis_replication_base_url = 34; ---
            if (ProtoBuf.PeekVarInt(buf, ofs + len) == (34 << 3 | 2))
            {
                len += 2;
                len += ProtoBuf.ReadString(buf, ofs + len, out headerBlock.replicationBaseUrl);
            }
            else
            {
                headerBlock.replicationBaseUrl = "";
            }

            return(len);
        }
Esempio n. 11
0
        /// <summary>
        /// dekodiert nur den Header eines Blockes (benötigt mindestens 32 Bytes)
        /// </summary>
        /// <param name="buf">Buffer, welcher ausgelesen werden soll</param>
        /// <param name="ofs">Startposition innerhalb des Buffers</param>
        /// <param name="result">fertig gelesenes Ergebnis</param>
        /// <returns>Größe des gesamten Blockes in Bytes</returns>
        public static int DecodeQuick(byte[] buf, int ofs, out OsmBlob result)
        {
            /*****
            * message BlobHeader
            * {
            *   required string type = 1;
            *   optional bytes indexdata = 2;
            *   required int32 datasize = 3;
            * }
            *****/

            int len = 0;
            int blobHeaderLen;

            len   += ProtoBuf.ReadInt32Fix(buf, ofs + len, out blobHeaderLen);
            result = new OsmBlob();

            // --- required string type = 1; ---
            string type;

            if (buf[ofs + len++] != (1 << 3 | 2))
            {
                throw new PbfParseException();
            }
            len += ProtoBuf.ReadString(buf, ofs + len, out type);

            // --- optional bytes indexdata = 2; ---
            if (buf[ofs + len] == (2 << 3 | 2))
            {
                throw new NotImplementedException();
            }

            // --- required int32 datasize = 3; ---
            if (buf[ofs + len++] != (3 << 3 | 0))
            {
                throw new PbfParseException();
            }
            ulong tmp;

            len           += ProtoBuf.ReadVarInt(buf, ofs + len, out tmp);
            result.blobLen = len + (int)tmp;
            if (len - sizeof(int) != blobHeaderLen)
            {
                throw new PbfParseException();
            }

            /*****
            * message Blob
            * {
            *   optional bytes raw = 1; // keine Kompression
            *   optional int32 raw_size = 2; // Nur gesetzt, wenn komprimiert, auf die unkomprimierte Größe
            *   optional bytes zlib_data = 3;
            *   // optional bytes lzma_data = 4; // GEPLANT.
            *   // optional bytes OBSOLETE_bzip2_data = 5; // Veraltet.
            * }
            *****/

            // --- optional bytes raw = 1; ---
            if (buf[ofs + len] == (1 << 3 | 2))
            {
                throw new NotSupportedException();
            }

            // --- optional int32 raw_size = 2; ---
            if (buf[ofs + len] == (2 << 3 | 0))
            {
                len++;
                len           += ProtoBuf.ReadVarInt(buf, ofs + len, out tmp);
                result.rawSize = (int)tmp;
            }

            // --- optional bytes zlib_data = 3; ---
            if (buf[ofs + len] == (3 << 3 | 2))
            {
                len++;
                len           += ProtoBuf.ReadVarInt(buf, ofs + len, out tmp);
                result.zlibOfs = len;
                result.zlibLen = (int)tmp;
                len           += (int)tmp;
            }
            else
            {
                // --- optional bytes lzma_data = 4; ---
                if (buf[ofs + len] == (4 << 3 | 2))
                {
                    throw new NotSupportedException();
                }

                // --- optional bytes OBSOLETE_bzip2_data = 5; ---
                if (buf[ofs + len] == (5 << 3 | 2))
                {
                    throw new NotSupportedException();
                }
            }

            if (len != result.blobLen)
            {
                throw new PbfParseException();
            }

            switch (type)
            {
            case "OSMHeader": break;

            case "OSMData": break;

            default: throw new Exception("unknown Type: " + type);
            }

            return(len);
        }