using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.IO;
|
using System.Drawing;
|
using System.Collections;
|
using System.Threading;
|
using System.Net;
|
|
|
|
namespace tileserver
|
{
|
class MetaReader
|
{
|
|
public static void fillTiles(String path, Tiles tiles)
|
{
|
long t0 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
|
|
List<TileReader> lreader = new List<TileReader>();
|
|
for (int i = 0; i < tiles.ListTiles.Length; i++)
|
{
|
TileReader reader = new TileReader(path, tiles, tiles.ListTiles[i]);
|
lreader.Add(reader);
|
}
|
|
for (int i = 0; i < lreader.Count; i++)
|
{
|
TileReader reader = lreader.ElementAt(i);
|
reader.join();
|
}
|
|
long t1 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
|
}
|
|
|
|
|
private class TileReader
|
{
|
private String path;
|
private Tiles tiles;
|
private Tile tile;
|
private Thread thread = null;
|
|
public TileReader(String path, Tiles tiles, Tile tile)
|
{
|
this.path = path;
|
this.tiles = tiles;
|
this.tile = tile;
|
thread = new Thread(work);
|
thread.Start();
|
}
|
|
|
public void join()
|
{
|
thread.Join();
|
}
|
|
public void getTiles(Tiles tiles, Tile tile)
|
{
|
this.tiles = tiles;
|
this.tile = tile;
|
thread = new Thread(work);
|
thread.Start();
|
}
|
|
|
private void work()
|
{
|
List<byte[]> ldata = new List<byte[]>();
|
|
for (int j = 0; j < tiles.Layers.Length; j++)
|
{
|
|
byte[] data = getTile(path, tiles.Layers[j], tiles.Zoom, tile.X, tile.Y);
|
ldata.Add(data);
|
}
|
|
ImageFactory.merge(tiles, tile, ldata);
|
|
}
|
|
}
|
|
|
|
|
|
private static byte[] getTile(string path, string layer, int zoom, int tilex, int tiley)
|
{
|
int A = (tilex & 0xF0000) >> 16;
|
int B = (tilex & 0x0F000) >> 12;
|
int C = (tilex & 0x00F00) >> 8;
|
int D = (tilex & 0x000F0) >> 4;
|
int E = (tilex & 0x00008) >> 3;
|
int M = (tilex & 0x00007);
|
|
int a = (tiley & 0xF0000) >> 16;
|
int b = (tiley & 0x0F000) >> 12;
|
int c = (tiley & 0x00F00) >> 8;
|
int d = (tiley & 0x000F0) >> 4;
|
int e = (tiley & 0x00008) >> 3;
|
int m = (tiley & 0x00007);
|
|
int D1 = (16 * A) + a;
|
int D2 = (16 * B) + b;
|
int D3 = (16 * C) + c;
|
int D4 = (16 * D) + d;
|
int F = 8 * ((16 * E) + e);
|
|
int indice = (8 * M) + m;
|
|
|
try
|
{
|
string fileName = path + "/" + layer + "/" + zoom + "/" + D1 + "/" + D2 + "/" + D3 + "/" + D4 + "/" + F + Program.fileExtension;
|
byte[] data = getTileFromMeta(fileName, layer, zoom, indice);
|
return data;
|
}
|
catch (Exception)
|
{
|
}
|
|
return new byte[0];
|
}
|
|
|
|
|
private static byte[] getTileFromMeta(String fileName, String layer, int zoom, int indice)
|
{
|
|
byte[] data = new byte[0];
|
|
FileStream fileStream = null;
|
BufferedStream bufferedStream = null;
|
|
try
|
{
|
fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
|
bufferedStream = new BufferedStream(fileStream);
|
int length = (int)fileStream.Length;
|
data = new byte[length];
|
bufferedStream.Read(data, 0, length);
|
bufferedStream.Close();
|
fileStream.Close();
|
}
|
catch (Exception)
|
{
|
}
|
|
try { bufferedStream.Close(); } catch (Exception) { }
|
try { fileStream.Close(); } catch (Exception) { }
|
|
return getData(data, indice);
|
|
|
|
}
|
|
|
|
private static byte[] getData(byte[] data, int indice)
|
{
|
try
|
{
|
int apuntador = 0;
|
|
// TAG : 4 bytes
|
// tiles : 4 bytes
|
// x : 4 bytes
|
// y : 4 bytes
|
// z : 4 bytes
|
|
apuntador = apuntador + 20 + (indice * 8);
|
int offset = readInt(data, apuntador);
|
apuntador = apuntador + 4;
|
int longitud = readInt(data, apuntador);
|
|
byte[] result = new byte[longitud];
|
Array.Copy(data, offset, result, 0, longitud);
|
return result;
|
}
|
catch (Exception)
|
{
|
}
|
|
return new byte[0];
|
}
|
|
|
|
|
private byte[] getTileWMS(String url, int zoom, int tilex, int tiley)
|
{
|
byte[] buffer = new byte[65536];
|
byte[] data = new byte[0];
|
Stream dataStream = null;
|
|
|
try
|
{
|
|
WebRequest request = WebRequest.Create(url);
|
|
request.Credentials = CredentialCache.DefaultCredentials;
|
|
// Get the response.
|
|
WebResponse response = request.GetResponse();
|
|
// Get the stream containing content returned by the server.
|
|
if (((HttpWebResponse)response).StatusCode == HttpStatusCode.OK)
|
{
|
dataStream = response.GetResponseStream();
|
|
// Un arxiu PNG comença com una firma de 8 bytes, els valors en hexadecimals són: 89 50 4E 47 0D 0D 0A 1A 0A, els valors decimals són: 137 80 78 71 13 10
|
|
int count = dataStream.Read(buffer, 0, buffer.Length);
|
data = new byte[count];
|
Array.Copy(buffer, data, count);
|
}
|
|
// Clean up the streams and the response.
|
|
response.Close();
|
|
}
|
catch (Exception)
|
{
|
}
|
|
try { dataStream.Close(); } catch (Exception) { };
|
|
return data;
|
|
}
|
|
|
|
|
private static String readString(byte[] datos, int apuntador, int longitud)
|
{
|
byte[] caracteres = new byte[longitud];
|
Array.Copy(datos, apuntador, caracteres, 0, longitud);
|
string result = System.Text.Encoding.UTF8.GetString(caracteres);
|
return result;
|
}
|
|
|
|
private static int readInt(byte[] datos, int apuntador)
|
{
|
int resultado = datos[apuntador++] & 0xFF;
|
resultado = resultado + ((int)(datos[apuntador++] & 0xFF) << 8);
|
resultado = resultado + ((int)(datos[apuntador++] & 0xFF) << 16);
|
resultado = resultado + ((int)(datos[apuntador++] & 0xFF) << 32);
|
return resultado;
|
}
|
|
|
}
|
}
|