/// <summary> /// Processes the block signature, if present. /// </summary> /// <returns> true if the block signature could be processed successfully, false otherwise. </returns> private bool ProcessBlockSignature(ReadBuffer readBuffer) { if (this.mapFileHeader.MapFileInfo.DebugFile) { // get and check the block signature string signatureBlock = readBuffer.ReadUTF8EncodedString(SIGNATURE_LENGTH_BLOCK); if (!signatureBlock.StartsWith("###TileStart", StringComparison.Ordinal)) { Logger.Log(LogLevel.Warning, "invalid block signature: " + signatureBlock); return(false); } } return(true); }
private IList <Way> ProcessWays(QueryParameters queryParameters, int numberOfWays, BoundingBox boundingBox, bool filterRequired, double tileLatitude, double tileLongitude, ReadBuffer readBuffer) { IList <Way> ways = new List <Way>(); Tag[] wayTags = this.mapFileHeader.MapFileInfo.WayTags; // Extend BoundingBox about wayFilterDistance meters, so that ways crossing the border added double verticalExpansion = PointUtils.LatitudeDistance(wayFilterDistance); double horizontalExpansion = PointUtils.LongitudeDistance(wayFilterDistance, Math.Max(Math.Abs(boundingBox.MinY), Math.Abs(boundingBox.MaxY))); double minLat = Math.Max(MercatorProjection.LATITUDE_MIN, boundingBox.MinY - verticalExpansion); double minLon = Math.Max(-180, boundingBox.MinX - horizontalExpansion); double maxLat = Math.Min(MercatorProjection.LATITUDE_MAX, boundingBox.MaxY + verticalExpansion); double maxLon = Math.Min(180, boundingBox.MaxX + horizontalExpansion); BoundingBox wayFilterBbox = new BoundingBox(minLon, minLat, maxLon, maxLat); for (int elementCounter = numberOfWays; elementCounter != 0; --elementCounter) { if (this.mapFileHeader.MapFileInfo.DebugFile) { // get and check the way signature string signatureWay = readBuffer.ReadUTF8EncodedString(SIGNATURE_LENGTH_WAY); if (!signatureWay.StartsWith("---WayStart", StringComparison.Ordinal)) { Logger.Log(LogLevel.Warning, "invalid way signature: " + signatureWay); return(null); } } // get the size of the way (VBE-U) int wayDataSize = readBuffer.ReadUnsignedInt(); if (wayDataSize < 0) { Logger.Log(LogLevel.Warning, "invalid way data size: " + wayDataSize); return(null); } if (queryParameters.useTileBitmask) { // get the way tile bitmask (2 bytes) int tileBitmask = readBuffer.ReadShort(); // check if the way is inside the requested tile if ((queryParameters.queryTileBitmask & tileBitmask) == 0) { // skip the rest of the way and continue with the next way readBuffer.SkipBytes(wayDataSize - 2); continue; } } else { // ignore the way tile bitmask (2 bytes) readBuffer.SkipBytes(2); } // get the special byte which encodes multiple flags sbyte specialByte = readBuffer.ReadByte(); // bit 1-4 represent the layer sbyte layer = (sbyte)((int)((uint)(specialByte & WAY_LAYER_BITMASK) >> WAY_LAYER_SHIFT)); // bit 5-8 represent the number of tag IDs sbyte numberOfTags = (sbyte)(specialByte & WAY_NUMBER_OF_TAGS_BITMASK); IList <Tag> tags = new List <Tag>(); for (sbyte tagIndex = numberOfTags; tagIndex != 0; --tagIndex) { int tagId = readBuffer.ReadUnsignedInt(); if (tagId < 0 || tagId >= wayTags.Length) { Logger.Log(LogLevel.Warning, "invalid way tag ID: " + tagId); return(null); } tags.Add(wayTags[tagId]); } // get the feature bitmask (1 byte) sbyte featureByte = readBuffer.ReadByte(); // bit 1-6 enable optional features bool featureName = (featureByte & WAY_FEATURE_NAME) != 0; bool featureHouseNumber = (featureByte & WAY_FEATURE_HOUSE_NUMBER) != 0; bool featureRef = (featureByte & WAY_FEATURE_REF) != 0; bool featureLabelPosition = (featureByte & WAY_FEATURE_LABEL_POSITION) != 0; bool featureWayDataBlocksByte = (featureByte & WAY_FEATURE_DATA_BLOCKS_BYTE) != 0; bool featureWayDoubleDeltaEncoding = (featureByte & WAY_FEATURE_DOUBLE_DELTA_ENCODING) != 0; // check if the way has a name if (featureName) { tags.Add(new Tag(TAG_KEY_NAME, ExtractLocalized(readBuffer.ReadUTF8EncodedString()))); } // check if the way has a house number if (featureHouseNumber) { tags.Add(new Tag(TAG_KEY_HOUSE_NUMBER, readBuffer.ReadUTF8EncodedString())); } // check if the way has a reference if (featureRef) { tags.Add(new Tag(TAG_KEY_REF, readBuffer.ReadUTF8EncodedString())); } Point labelPosition = ReadOptionalLabelPosition(tileLatitude, tileLongitude, featureLabelPosition, readBuffer); int wayDataBlocks = ReadOptionalWayDataBlocksByte(featureWayDataBlocksByte, readBuffer); if (wayDataBlocks < 1) { Logger.Log(LogLevel.Warning, "invalid number of way data blocks: " + wayDataBlocks); return(null); } for (int wayDataBlock = 0; wayDataBlock < wayDataBlocks; ++wayDataBlock) { List <List <Point> > wayNodes = ProcessWayDataBlock(tileLatitude, tileLongitude, featureWayDoubleDeltaEncoding, readBuffer); if (wayNodes != null) { if (filterRequired && wayFilterEnabled) { double minX = double.MaxValue; double minY = double.MaxValue; double maxX = 0; double maxY = 0; for (int i = 0; i < wayNodes.Count; i++) { for (int j = 0; j < wayNodes[i].Count; j++) { minX = Math.Min(minX, wayNodes[i][j].X); minY = Math.Min(minY, wayNodes[i][j].Y); maxX = Math.Max(maxX, wayNodes[i][j].X); maxY = Math.Max(maxY, wayNodes[i][j].Y); } } if (!wayFilterBbox.Intersects(new BoundingBox(minX, minY, maxX, maxY))) { continue; } } ways.Add(new Way(layer, tags, wayNodes, labelPosition)); } } } return(ways); }
private IList <PointOfInterest> ProcessPOIs(double tileLatitude, double tileLongitude, int numberOfPois, BoundingBox boundingBox, bool filterRequired, ReadBuffer readBuffer) { IList <PointOfInterest> pois = new List <PointOfInterest>(); Tag[] poiTags = this.mapFileHeader.MapFileInfo.PoiTags; for (int elementCounter = numberOfPois; elementCounter != 0; --elementCounter) { if (this.mapFileHeader.MapFileInfo.DebugFile) { // get and check the POI signature string signaturePoi = readBuffer.ReadUTF8EncodedString(SIGNATURE_LENGTH_POI); if (!signaturePoi.StartsWith("***POIStart", StringComparison.Ordinal)) { Logger.Log(LogLevel.Warning, "invalid POI signature: " + signaturePoi); return(null); } } // get the POI latitude offset (VBE-S) double latitude = tileLatitude + PointUtils.MicrodegreesToDegrees(readBuffer.ReadSignedInt()); // get the POI longitude offset (VBE-S) double longitude = tileLongitude + PointUtils.MicrodegreesToDegrees(readBuffer.ReadSignedInt()); // get the special byte which encodes multiple flags sbyte specialByte = readBuffer.ReadByte(); // bit 1-4 represent the layer with sbyte layer = (sbyte)((int)((uint)(specialByte & POI_LAYER_BITMASK) >> POI_LAYER_SHIFT)); // bit 5-8 represent the number of tag IDs sbyte numberOfTags = (sbyte)(specialByte & POI_NUMBER_OF_TAGS_BITMASK); IList <Tag> tags = new List <Tag>(); // get the tag IDs (VBE-U) for (sbyte tagIndex = numberOfTags; tagIndex != 0; --tagIndex) { int tagId = readBuffer.ReadUnsignedInt(); if (tagId < 0 || tagId >= poiTags.Length) { Logger.Log(LogLevel.Warning, "invalid POI tag ID: " + tagId); return(null); } tags.Add(poiTags[tagId]); } // get the feature bitmask (1 byte) sbyte featureByte = readBuffer.ReadByte(); // bit 1-3 enable optional features bool featureName = (featureByte & POI_FEATURE_NAME) != 0; bool featureHouseNumber = (featureByte & POI_FEATURE_HOUSE_NUMBER) != 0; bool featureElevation = (featureByte & POI_FEATURE_ELEVATION) != 0; // check if the POI has a name if (featureName) { tags.Add(new Tag(TAG_KEY_NAME, ExtractLocalized(readBuffer.ReadUTF8EncodedString()))); } // check if the POI has a house number if (featureHouseNumber) { tags.Add(new Tag(TAG_KEY_HOUSE_NUMBER, readBuffer.ReadUTF8EncodedString())); } // check if the POI has an elevation if (featureElevation) { tags.Add(new Tag(TAG_KEY_ELE, Convert.ToString(readBuffer.ReadSignedInt()))); } Point position = new Point(longitude, latitude); // depending on the zoom level configuration the poi can lie outside // the tile requested, we filter them out here if (!filterRequired || boundingBox.Contains(position)) { pois.Add(new PointOfInterest(layer, tags, position)); } } return(pois); }