// Copyright 2008 - Paul den Dulk (Geodan)
//
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System.IO;
using System;
using System.Collections.Generic;
namespace BruTile{
public static class Utilities
{
/// <summary>
/// Reads data from a stream until the end is reached. The
/// data is returned as a byte array. An IOException is
/// thrown if any of the underlying IO calls fail.
/// </summary>
/// <param name="stream">The stream to read data from</param>
public static byte[] ReadFully(Stream stream)
{
//thanks to: http://www.yoda.arachsys.com/csharp/readbinary.html
byte[] buffer = new byte[32768];
using (MemoryStream ms = new MemoryStream())
{
while (true)
{
int read = stream.Read(buffer, 0, buffer.Length);
if (read <= 0)
{
return ms.ToArray();
}
ms.Write(buffer, 0, read);
}
}
}
public static int GetNearestLevel(IList<double> resolutions, double resolution)
{
if (resolutions.Count == 0)
{
throw new ArgumentException("No tile resolutions");
}
//smaller than smallest
if (resolutions[resolutions.Count - 1] > resolution) return resolutions.Count - 1;
//bigger than biggest
if (resolutions[0] < resolution) return 0;
int result = 0;
double resultDistance = double.MaxValue;
for (int i = 0; i < resolutions.Count; i++)
{
double distance = Math.Abs(resolutions[i] - resolution);
if (distance < resultDistance)
{
result = i;
resultDistance = distance;
}
}
return result;
}
}
}
|