public void ReadHeader() { List <long> tileOffsets = new List <long>(); _mapFile.StreamReader.Position = ZoomIntervalConfig.AbsStartPositionSubFile; MinX = (int)MapFile.GetTileX((double)_mapFile.Header.MinLon, ZoomIntervalConfig.BaseZoomLevel); MaxX = (int)MapFile.GetTileX((double)_mapFile.Header.MaxLon, ZoomIntervalConfig.BaseZoomLevel); MinY = (int)MapFile.GetTileY((double)_mapFile.Header.MaxLat, ZoomIntervalConfig.BaseZoomLevel); MaxY = (int)MapFile.GetTileY((double)_mapFile.Header.MinLat, ZoomIntervalConfig.BaseZoomLevel); if (_mapFile.Header.DebugInformationPresent) { TileIndexSignature = _mapFile.ReadFixedString(16); } else { TileIndexSignature = null; } long hTiles = MaxX - MinX + 1; long vTiles = MaxY - MinY + 1; long blockCount = hTiles * vTiles; long endOfIndex = ZoomIntervalConfig.AbsStartPositionSubFile + (blockCount * 5); //tile entries byte[] buffer = new byte[8]; while (_mapFile.StreamReader.Position < endOfIndex) { Array.Clear(buffer, 0, 8); _mapFile.StreamReader.Read(buffer, 3, 5); long offset; if ((buffer[3] & 0x80) == 0x80) { //in water offset = 0; } else { offset = BitConverter.ToInt64(MapFile.ReverseBytes(buffer, 0, 8), 0); } tileOffsets.Add(offset); } _tileAbsOffsets = tileOffsets.ToArray(); }
public bool Read() { try { FileStream fs = _mapFile.StreamReader; fs.Position = 0; MagicByte = _mapFile.ReadFixedString(20); if (MagicByte != "mapsforge binary OSM") { return(false); } HeaderSize = _mapFile.ReadInt32(); FileVersion = _mapFile.ReadInt32(); FileSize = _mapFile.ReadInt64(); CreationDate = new DateTime(1970, 1, 1).AddMilliseconds(_mapFile.ReadInt64()); //minLat, minLon, maxLat, maxLon MinLat = (double)_mapFile.ReadInt32() / 1000000.0; MinLon = (double)_mapFile.ReadInt32() / 1000000.0; MaxLat = (double)_mapFile.ReadInt32() / 1000000.0; MaxLon = (double)_mapFile.ReadInt32() / 1000000.0; TileSize = _mapFile.ReadInt16(); Projection = _mapFile.ReadVariableString(); byte b = (byte)fs.ReadByte(); DebugInformationPresent = (b & 0x80) == 0x80; MapStartPositionPresent = (b & 0x40) == 0x40; MapStartZoomLevelPresent = (b & 0x20) == 0x20; LanguagePreferencePresent = (b & 0x10) == 0x10; CommentPresent = (b & 0x80) == 0x80; CreatedByPresent = (b & 0x40) == 0x40; //todo if (MapStartPositionPresent) { MapStartLat = (double)_mapFile.ReadInt32() / 1000000.0; MapStartLon = (double)_mapFile.ReadInt32() / 1000000.0; } else { MapStartLat = null; MapStartLon = null; } if (MapStartZoomLevelPresent) { MapStartZoomLevel = fs.ReadByte(); } else { MapStartZoomLevel = null; } if (LanguagePreferencePresent) { LanguagePreference = _mapFile.ReadVariableString(); } else { LanguagePreference = null; } if (CommentPresent) { Comment = _mapFile.ReadVariableString(); } else { Comment = null; } if (CreatedByPresent) { CreatedBy = _mapFile.ReadVariableString(); } else { CreatedBy = null; } int cnt = _mapFile.ReadInt16(); POITags = new Tag[cnt]; for (int i = 0; i < cnt; i++) { string s = _mapFile.ReadVariableString(); string[] parts = s.Split(new char[] { '=' }); POITags[i] = new Tag(parts[0], parts[1]); } cnt = _mapFile.ReadInt16(); WayTags = new Tag[cnt]; for (int i = 0; i < cnt; i++) { string s = _mapFile.ReadVariableString(); string[] parts = s.Split(new char[] { '=' }); WayTags[i] = new Tag(parts[0], parts[1]); } ZoomIntervalCount = fs.ReadByte(); ZoomIntervalConfigurations = new ZoomIntervalConfiguration[ZoomIntervalCount]; for (int i = 0; i < ZoomIntervalCount; i++) { ZoomIntervalConfiguration zli = new ZoomIntervalConfiguration( fs.ReadByte(), fs.ReadByte(), fs.ReadByte(), _mapFile.ReadInt64(), _mapFile.ReadInt64()); ZoomIntervalConfigurations[i] = zli; } } catch { return(false); } return(true); }