Alejandro Acuña
2025-04-29 d1d736e487d9eb104dcae9def948066037afd2f0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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;
        }
 
 
    }
}