/// <summary>
        /// Gets the elevation.
        /// </summary>
        /// <returns>
        /// The height. Null, if elevation is not available.
        /// </returns>
        /// <param name='coordinates'>
        /// Coordinates.
        /// </param>
        /// <exception cref='Exception'>
        /// Represents errors that occur during application execution.
        /// </exception>
        public int?GetElevation(IGeographicalCoordinates coordinates)
        {
            int localLat = (int)((coordinates.Latitude - Latitude) * PointsPerCell);
            int localLon = (int)(((coordinates.Longitude - Longitude)) * PointsPerCell);
            int bytesPos = ((PointsPerCell - localLat - 1) * PointsPerCell * 2) + localLon * 2;

            if (bytesPos < 0 || bytesPos > PointsPerCell * PointsPerCell * 2)
            {
                throw new ArgumentOutOfRangeException("Coordinates out of range.", "coordinates");
            }

            if ((HgtData [bytesPos] == 0x80) && (HgtData [bytesPos + 1] == 0x00))
            {
                return(null);
            }

            // Motorola "big-endian" order with the most significant byte first
            return((HgtData [bytesPos]) << 8 | HgtData [bytesPos + 1]);
        }
Exemple #2
0
        /// <summary>
        /// Gets the elevation.
        /// </summary>
        /// <returns>
        /// The height. Null, if elevation is not available.
        /// </returns>
        /// <param name='coordinates'>
        /// Coordinates.
        /// </param>
        /// <exception cref='Exception'>
        /// Represents errors that occur during application execution.
        /// </exception>
        public int?GetElevation(IGeographicalCoordinates coordinates)
        {
            int cellLatitude = (int)Math.Floor(Math.Abs(coordinates.Latitude));

            if (coordinates.Latitude < 0)
            {
                cellLatitude *= -1;
                cellLatitude -= 1;                 // because negative so in bottom tile
            }

            int cellLongitude = (int)Math.Floor(Math.Abs(coordinates.Longitude));

            if (coordinates.Longitude < 0)
            {
                cellLongitude *= -1;
                cellLongitude -= 1;                 // because negative so in left tile
            }

            SrtmDataCell dataCell = DataCells.Where(dc => dc.Latitude == cellLatitude && dc.Longitude == cellLongitude).FirstOrDefault();

            if (dataCell != null)
            {
                return(dataCell.GetElevation(coordinates));
            }

            string filename = string.Format("{0}{1:D2}{2}{3:D3}.hgt",
                                            cellLatitude < 0 ? "S" : "N",
                                            Math.Abs(cellLatitude),
                                            cellLongitude < 0 ? "W" : "E",
                                            Math.Abs(cellLongitude));

            string filePath = Path.Combine(DataDirectory, filename);

            if (!File.Exists(filePath))
            {
                throw new Exception("SRTM data cell not found: " + filePath);
            }

            dataCell = new SrtmDataCell(filePath);
            DataCells.Add(dataCell);
            return(dataCell.GetElevation(coordinates));
        }
		/// <summary>
		/// Gets the height.
		/// </summary>
		/// <returns>
		/// The height.
		/// </returns>
		/// <param name='coordinates'>
		/// Coordinates.
		/// </param>
		/// <exception cref='ArgumentOutOfRangeException'>
		/// Is thrown when an argument passed to a method is invalid because it is outside the allowable range of values as
		/// specified by the method.
		/// </exception>
		public int GetHeight (IGeographicalCoordinates coordinates)
		{
			int localLat = (int)((coordinates.Latitude - Latitude) * PointsPerCell);
			int localLon = (int)(((coordinates.Longitude - Longitude)) * PointsPerCell);
			int bytesPos = ((PointsPerCell - localLat - 1) * PointsPerCell * 2) + localLon * 2;

			Console.WriteLine (bytesPos);
			
			if (bytesPos < 0 || bytesPos > PointsPerCell * PointsPerCell * 2)
				throw new ArgumentOutOfRangeException ("Coordinates out of range.", "coordinates");
			
			// Motorola "big-endian" order with the most significant byte first
			return (HgtData [bytesPos]) << 8 | HgtData [bytesPos + 1];
		}
		/// <summary>
		/// Gets the height.
		/// </summary>
		/// <returns>
		/// The height.
		/// </returns>
		/// <param name='coordinates'>
		/// Coordinates.
		/// </param>
		/// <exception cref='Exception'>
		/// Represents errors that occur during application execution.
		/// </exception>
		public int GetHeight (IGeographicalCoordinates coordinates)
		{
			int cellLatitude = (int)Math.Floor (Math.Abs (coordinates.Latitude));
			if (coordinates.Latitude < 0)
				cellLatitude *= -1;
			
			int cellLongitude = (int)Math.Floor (Math.Abs (coordinates.Longitude));
			if (coordinates.Longitude < 0)
				cellLongitude *= -1;

			SrtmDataCell dataCell = DataCells.Where (dc => dc.Latitude == cellLatitude && dc.Longitude == cellLongitude).FirstOrDefault ();
			if (dataCell != null)
				return dataCell.GetHeight (coordinates);
			
			string filename = string.Format ("{0}{1:D2}{2}{3:D3}.hgt",
				cellLatitude < 0 ? "S" : "N",
				Math.Abs (cellLatitude),
				cellLongitude < 0 ? "W" : "E",
				Math.Abs (cellLongitude));
			
			string filePath = Path.Combine (DataDirectory, filename);
			
			if (!File.Exists (filePath))
				throw new Exception ("SRTM data cell not found: "+filePath);
			
			dataCell = new SrtmDataCell (filePath);
			DataCells.Add (dataCell);
			return dataCell.GetHeight (coordinates);
		}