Ejemplo n.º 1
0
        private void TryParseBoundingBox(Stream stream)
        {
            var markerByte = this._stream.ReadByte();

            if (markerByte == -1)
            {
                throw new FormatException("Cannot read further to the o5m-file!");
            }
            var marker = (O5MFileByteMarker)markerByte;

            if (marker != O5MFileByteMarker.BoundingBox)
            {
                stream.Position -= 1;
                return;
            }

            var length    = VarInt.ParseUInt64(stream);
            var buffer    = new byte[length];
            var readBytes = this._stream.Read(buffer, 0, buffer.Length);

            if (readBytes != buffer.Length)
            {
                throw new FormatException("The .o5m-file has an invalid bounding-box-length!");
            }

            var offset = 0;

            this._longitudeMin = VarInt.ParseInt64(buffer, ref offset) / POINT_DIVIDER;
            this._latitudeMin  = VarInt.ParseInt64(buffer, ref offset) / POINT_DIVIDER;
            this._longitudeMax = VarInt.ParseInt64(buffer, ref offset) / POINT_DIVIDER;
            this._latitudeMax  = VarInt.ParseInt64(buffer, ref offset) / POINT_DIVIDER;
        }
Ejemplo n.º 2
0
        private void TryParseFileTimestamp(Stream stream)
        {
            var markerByte = this._stream.ReadByte();

            if (markerByte == -1)
            {
                throw new FormatException("Cannot read further to the o5m-file!");
            }
            var marker = (O5MFileByteMarker)markerByte;

            if (marker != O5MFileByteMarker.FileTimestamp)
            {
                stream.Position -= 1;
                return;
            }

            var length    = VarInt.ParseUInt64(stream);
            var buffer    = new byte[length];
            var readBytes = this._stream.Read(buffer, 0, buffer.Length);

            if (readBytes != buffer.Length)
            {
                throw new FormatException("The .o5m-file has an invalid timestamp-length!");
            }
            var timestamp = VarInt.ParseInt64(buffer);

            this._fileTimestamp = UNIX_START.AddSeconds(timestamp);
        }
Ejemplo n.º 3
0
        private OSMWay ParseWayData(
            byte[] data
#if DEBUG
            , ElementDebugInfos debugInfos
#endif
            )
        {
            var bufferOffset = 0;

            var wayId  = VarInt.ParseInt64(data, ref bufferOffset) + this._lastWayId;
            var o5mWay = new OSMWay((ulong)wayId);

            this._lastWayId = (long)o5mWay.Id;

#if DEBUG
            this.ParseVersionData(data, o5mWay, ref bufferOffset, debugInfos);
#else
            this.ParseVersionData(data, o5mWay, ref bufferOffset);
#endif

            if (data[bufferOffset] > 0)
            {
                var referenceLength      = VarInt.ParseUInt64(data, ref bufferOffset);
                var referenceNodesBuffer = new byte[referenceLength];
                Array.Copy(data, bufferOffset, referenceNodesBuffer, 0, referenceNodesBuffer.Length);
                var referenceBufferOffset = 0;
                var bytesUnread           = referenceNodesBuffer.Length;
                while (bytesUnread > 0)
                {
                    var currentReferenceId = VarInt.ParseInt64(referenceNodesBuffer, ref referenceBufferOffset) + this._lastReferenceId;
                    o5mWay.NodeRefs.Add((ulong)currentReferenceId);
                    this._lastReferenceId = currentReferenceId;
                    bytesUnread           = referenceNodesBuffer.Length - referenceBufferOffset;
                }
                bufferOffset += (int)referenceLength;
            }

#if DEBUG
            this.ParseTagsData(data, o5mWay, ref bufferOffset, debugInfos);
#else
            this.ParseTagsData(data, o5mWay, ref bufferOffset);
#endif

            return(o5mWay);
        }
Ejemplo n.º 4
0
        private OSMNode ParseNodeData(
            byte[] data
#if DEBUG
            , ElementDebugInfos debugInfos
#endif
            )
        {
            var bufferOffset = 0;

            var nodeId  = VarInt.ParseInt64(data, ref bufferOffset) + this._lastNodeId;
            var o5mNode = new OSMNode((ulong)nodeId);

            this._lastNodeId = (long)o5mNode.Id;

#if DEBUG
            this.ParseVersionData(data, o5mNode, ref bufferOffset, debugInfos);
#else
            this.ParseVersionData(data, o5mNode, ref bufferOffset);
#endif

            var parsedInt     = VarInt.ParseInt32(data, ref bufferOffset);
            var longLongitude = parsedInt + this._lastLongitude;
            var longLatitude  = VarInt.ParseInt32(data, ref bufferOffset) + this._lastLatitude;
            this._lastLongitude = longLongitude;
            this._lastLatitude  = longLatitude;
            o5mNode.Longitude   = longLongitude / POINT_DIVIDER;
            o5mNode.Latitude    = longLatitude / POINT_DIVIDER;

#if DEBUG
            this.ParseTagsData(data, o5mNode, ref bufferOffset, debugInfos);
#else
            this.ParseTagsData(data, o5mNode, ref bufferOffset);
#endif

            return(o5mNode);
        }
Ejemplo n.º 5
0
        private void ParseVersionData(
            byte[] data,
            OSMElement element,
            ref int bufferOffset
#if DEBUG
            , ElementDebugInfos debugInfos
#endif
            )
        {
#if DEBUG
            var versionPosition = (uint)bufferOffset + 1;
#endif
            element.Version = VarInt.ParseUInt64(data, ref bufferOffset);
#if DEBUG
            debugInfos.Add(versionPosition, "start of 'version' (" + element.Version + ").");
#endif
            if (element.Version == 0)
            {
                return;
            }

#if DEBUG
            var timestampOffset = (uint)bufferOffset + 1;
#endif
            var timestampDiff = VarInt.ParseInt64(data, ref bufferOffset);
#if DEBUG
            debugInfos.Add(timestampOffset, "start of 'timestamp' (diff: " + timestampDiff + ").");
#endif
            var unixTimestamp = timestampDiff + this._lastTimestamp;
            this._lastTimestamp = unixTimestamp;
            element.Timestamp   = UNIX_START.AddSeconds(unixTimestamp);
            if (unixTimestamp == 0)
            {
                return;
            }

#if DEBUG
            var changesetPosition = (uint)bufferOffset + 1;
#endif
            element.Changeset = (ulong)(VarInt.ParseInt64(data, ref bufferOffset) + this._lastChangeset);
#if DEBUG
            debugInfos.Add(changesetPosition, "start of 'changeset' (" + element.Changeset + ").");
#endif
            this._lastChangeset = (long)element.Changeset;

            KeyValuePair <byte[], byte[]>?keyValuePair;
            if (data[bufferOffset] == 0)
            {
#if DEBUG
                debugInfos.Add((uint)bufferOffset + 1, "start of 'uid/user'-pair (raw).");
#endif
                keyValuePair = StringPair.ParseToByteArrayPair(data, ref bufferOffset);
                if (keyValuePair.HasValue)
                {
                    var keyValuePairValue = keyValuePair.Value;
                    element.UserId   = VarInt.ParseUInt64(keyValuePairValue.Key);
                    element.UserName = Encoding.UTF8.GetString(keyValuePairValue.Value);
                    if (element.UserId != 0)
                    {
                        if (keyValuePairValue.Key.Length + keyValuePairValue.Value.Length <= 250)
                        {
                            this._storedStringPairs.Insert(0, keyValuePairValue);
                        }
                    }
                }
                else
                {
                    this._storedStringPairs.Insert(0, new KeyValuePair <byte[], byte[]>(new byte[0], new byte[0]));
                }
            }
            else
            {
#if DEBUG
                debugInfos.Add((uint)bufferOffset + 1, "start of 'uid/user'-pair (stored).");
#endif
                var storedPosition = VarInt.ParseUInt32(data, ref bufferOffset);
                if (this._storedStringPairs.ElementExistsAtPosition((int)storedPosition))
                {
                    keyValuePair     = this._storedStringPairs[(int)storedPosition - 1];
                    element.UserId   = VarInt.ParseUInt64(keyValuePair?.Key);
                    element.UserName = Encoding.UTF8.GetString(keyValuePair?.Value);
                }
            }
        }
Ejemplo n.º 6
0
        private OSMRelation ParseRelationData(
            byte[] data
#if DEBUG
            , ElementDebugInfos debugInfos
#endif
            )
        {
            var bufferOffset = 0;

#if DEBUG
            var idPosition = (uint)bufferOffset + 1;
#endif
            var relationId = VarInt.ParseInt64(data, ref bufferOffset) + this._lastRelationId;
#if DEBUG
            debugInfos.Add(idPosition, "start of 'id' (" + relationId + ").");
#endif
            var o5mRelation = new OSMRelation((ulong)relationId);
            this._lastRelationId = (long)o5mRelation.Id;

#if DEBUG
            this.ParseVersionData(data, o5mRelation, ref bufferOffset, debugInfos);
#else
            this.ParseVersionData(data, o5mRelation, ref bufferOffset);
#endif

            if (data[bufferOffset] > 0)
            {
                KeyValuePair <byte[], byte[]>?typeAndRole;
#if DEBUG
                var referenceLengthPosition = (uint)bufferOffset + 1;
#endif
                var referenceLength = VarInt.ParseUInt64(data, ref bufferOffset);
#if DEBUG
                debugInfos.Add(referenceLengthPosition, "start of 'length of reference section' (" + referenceLength + ").");
#endif
                var startOffset = bufferOffset;
                var bytesUnread = referenceLength;
                while (bytesUnread > 0)
                {
#if DEBUG
                    var referenceIdPosition = (uint)bufferOffset + 1;
#endif
                    var currentReferenceId = VarInt.ParseInt64(data, ref bufferOffset) + this._lastReferenceId;
#if DEBUG
                    debugInfos.Add(referenceIdPosition, "start of 'reference-id' (" + currentReferenceId + ").");
#endif
                    this._lastReferenceId = currentReferenceId;
                    if (data[bufferOffset] == 0)
                    {
#if DEBUG
                        debugInfos.Add((uint)bufferOffset + 1, "start of 'type/role'-pair (raw).");
#endif
                        typeAndRole = StringPair.ParseToTypeRoleByteArray(data, ref bufferOffset);
                        if (typeAndRole.HasValue)
                        {
                            var typeAndRoleValue = typeAndRole.Value;
                            if (typeAndRoleValue.Key.Length + typeAndRoleValue.Value.Length <= 250)
                            {
                                this._storedStringPairs.Insert(0, typeAndRoleValue);
                            }
                        }
                    }
                    else
                    {
#if DEBUG
                        var typeRolePosition = (uint)bufferOffset + 1;
#endif
                        var storedPosition = VarInt.ParseUInt32(data, ref bufferOffset);
#if DEBUG
                        debugInfos.Add(typeRolePosition, "start of stored 'type/role'-pair (" + storedPosition + ").");
#endif
                        typeAndRole = this._storedStringPairs[(int)storedPosition - 1];
                    }
                    if (typeAndRole?.Key.Length > 0)
                    {
                        var typeValue = (MemberType)(typeAndRole.Value.Key[0] - 0x30);
                        var roleValue = Encoding.UTF8.GetString(typeAndRole.Value.Value);
                        o5mRelation.Members.Add(new OSMMember(typeValue, (ulong)currentReferenceId, roleValue));
                    }
                    bytesUnread = referenceLength - ((ulong)bufferOffset - (ulong)startOffset);
                }
            }

#if DEBUG
            this.ParseTagsData(data, o5mRelation, ref bufferOffset, debugInfos);
#else
            this.ParseTagsData(data, o5mRelation, ref bufferOffset);
#endif

            return(o5mRelation);
        }