-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUTFGridGenerator.cs
321 lines (300 loc) · 12.7 KB
/
UTFGridGenerator.cs
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using Ionic.Zlib;
using Newtonsoft.Json;
namespace NatGeo.UTFGrid
{
// the thread data which is shared to all processing threads.
// this class defines mostly properties and will be passed as
// a parameter to each thread.
sealed class UTFGridGeneratorConfig
{
public bool GZip { get; set; }
public bool Overwrite { get; set; }
public bool Verbose { get; set; }
public string Destination { get; set; }
public HashSet<string> Fields { get; set; }
public string MapPath { get { return m_mapPath; } }
public int WorkerCount { get { return m_workers; } }
private string m_mapPath;
private IEnumerator<TileDescription> m_tiles;
private int m_workers;
public UTFGridGeneratorConfig (string mapPath, IEnumerable<TileDescription> tiles) {
m_mapPath = mapPath;
m_tiles = tiles.GetEnumerator();
m_workers = 0;
GZip = false;
Overwrite = false;
Destination = ".";
Fields = null;
}
public TileDescription NextTile () {
TileDescription result = null;
lock (m_tiles) {
if (m_tiles.MoveNext()) {
result = m_tiles.Current;
} else {
result = null;
}
}
return result;
}
public void WorkerStart () {
Interlocked.Increment(ref m_workers);
}
public void WorkerEnd () {
Interlocked.Decrement(ref m_workers);
}
}
class UTFGridGenerator
{
public static void Execute (object threadData) {
UTFGridGeneratorConfig config = threadData as UTFGridGeneratorConfig;
try {
config.WorkerStart();
IMap map = null;
try {
IMapDocument mapDocument = new MapDocumentClass();
mapDocument.Open(config.MapPath, null);
map = mapDocument.ActiveView as IMap;
if (map == null) {
map = mapDocument.get_Map(0);
}
mapDocument.Close();
} catch (Exception) { }
if (map == null) {
throw new Exception("Unable to open map at " + config.MapPath);
}
if ((map.SpatialReference.FactoryCode != 102113) &&
(map.SpatialReference.FactoryCode != 102100) &&
(map.SpatialReference.FactoryCode != 3785)) {
throw new Exception("Spatial reference of map must be Web Mercator (is " + map.SpatialReference.FactoryCode + ")");
}
while (true) {
TileDescription tile = config.NextTile();
if (tile == null) {
return;
}
string folder = String.Format("{0}\\{1}\\{2}", config.Destination, tile.Level, tile.Col);
if (!Directory.Exists(folder)) {
Directory.CreateDirectory(folder);
}
string file = String.Format("{0}\\{1}.grid.json", folder, tile.Row);
if (config.GZip) file += ".gz";
if ((!File.Exists(file)) || config.Overwrite) {
if (config.Verbose) {
Console.WriteLine(Thread.CurrentThread.Name + " generating tile " + tile.Level + ", " + tile.Row + ", " + tile.Col);
}
Dictionary<string, object> data = CollectData(map, tile.Extent, config.Fields);
if (data != null) {
if (config.Verbose) {
Console.WriteLine(Thread.CurrentThread.Name + " saving to " + file);
}
Stream fOut = new System.IO.FileStream(file, FileMode.Create);
if (config.GZip) {
fOut = new GZipStream(fOut, CompressionMode.Compress, CompressionLevel.BestCompression);
}
using (fOut) {
string json = JsonConvert.SerializeObject(data, Formatting.Indented);
Encoding utf8 = new UTF8Encoding(false, true);
byte[] encodedJson = utf8.GetBytes(json);
fOut.Write(encodedJson, 0, encodedJson.Length);
}
}
}
}
} finally {
config.WorkerEnd();
}
}
private static Dictionary<string, object> CollectData (IMap map, IEnvelope extent, HashSet<string> includeFields) {
const int tileSize = 128;
double cellWidth = extent.Width / (tileSize - 1);
double cellHeight = extent.Height / (tileSize - 1);
Dictionary<ValueList, List<Cell>> dataCells = new Dictionary<ValueList, List<Cell>>();
for (int y = 0; y < tileSize; y += 1) {
for (int x = 0; x < tileSize; x += 1) {
EnvelopeClass pixelExtent = new EnvelopeClass();
pixelExtent.XMin = extent.XMin + x * cellWidth;
pixelExtent.XMax = pixelExtent.XMin + cellWidth;
pixelExtent.YMax = extent.YMax - y * cellHeight;
pixelExtent.YMin = pixelExtent.YMax - cellHeight;
ValueList cellData = GetPixelData(map, pixelExtent, includeFields);
if (cellData.Count > 0) {
if (dataCells.ContainsKey(cellData)) {
dataCells[cellData].Add(new Cell(y, x));
} else {
List<Cell> cells = new List<Cell>();
cells.Add(new Cell(y, x));
dataCells[cellData] = cells;
}
}
}
}
StringBuilder[] grid = new StringBuilder[tileSize];
for (int i = 0; i < tileSize; i += 1)
grid[i] = new StringBuilder(new String(' ', tileSize));
List<string> keys = new List<string>();
keys.Add("");
Dictionary<string, object> data = new Dictionary<string, object>();
int keyIndex = 0;
foreach (KeyValuePair<ValueList, List<Cell>> d in dataCells) {
if (d.Key.Count > 0) {
char code = EncodeChar(keys.Count);
string key = keyIndex.ToString();
foreach (Cell cell in d.Value) {
grid[cell.Row][cell.Col] = code;
}
keys.Add(key);
data.Add(key, d.Key);
keyIndex += 1;
}
}
if (keys.Count > 1) {
Dictionary<string, object> result = new Dictionary<string, object>();
result.Add("grid", grid.Select(sb => sb.ToString()).ToArray());
result.Add("keys", keys.ToArray());
result.Add("data", data);
return result;
} else {
return null;
}
}
private static ValueList GetPixelData (IMap map, IEnvelope extent, HashSet<string> includeFields) {
ValueList result = new ValueList();
for (int i = 0; i < map.LayerCount; i += 1) {
ILayer layer = map.get_Layer(i);
if (!layer.Visible) {
continue;
}
IIdentify id = layer as IIdentify;
if (id == null) {
continue;
}
IArray data = id.Identify(extent);
if (data != null) {
for (int j = 0; j < data.Count; j += 1) {
object foundObj = data.get_Element(j);
IRasterIdentifyObj2 raster = foundObj as IRasterIdentifyObj2;
IRowIdentifyObject row = foundObj as IRowIdentifyObject;
if (raster != null) {
int propertyIndex = 0;
string property;
string value;
while (true) {
try {
raster.GetPropAndValues(propertyIndex, out property, out value);
if ((!"NoData".Equals(value)) &&
((includeFields == null) || includeFields.Contains(property))) {
result.Add(property, value);
}
propertyIndex += 1;
} catch {
break;
}
}
continue;
} else if (row != null) {
IFields fields = row.Row.Fields;
for (int k = 0; k < fields.FieldCount; k += 1) {
string fieldName = fields.get_Field(k).Name;
if ((includeFields == null) ? (!result.ContainsKey(fieldName)) : includeFields.Contains(fieldName)) {
result.Add(fieldName, row.Row.get_Value(k));
}
}
}
}
break;
}
}
return result;
}
private static char EncodeChar (int value) {
value += 32;
if (value >= 34)
value += 1;
if (value >= 92)
value += 1;
return (char)value;
}
}
internal class Cell
{
public readonly int Row;
public readonly int Col;
public Cell (int row, int col) {
Row = row;
Col = col;
}
public override bool Equals (object obj) {
return (obj is Cell) &&
(((Cell)obj).Row == Row) &&
(((Cell)obj).Col == Col);
}
public override int GetHashCode () {
unchecked { // Overflow is fine, just wrap
int hash = 17;
// Suitable nullity checks etc, of course :)
hash = hash * 23 + Row;
hash = hash * 23 + Col;
return hash;
}
}
}
internal class TileDescription : Cell
{
public readonly int Level;
public readonly IEnvelope Extent;
public TileDescription (int level, int row, int col, double xmin, double ymin, double xmax, double ymax)
: base(row, col) {
Level = level;
Extent = new EnvelopeClass();
Extent.XMin = xmin;
Extent.YMin = ymin;
Extent.XMax = xmax;
Extent.YMax = ymax;
}
}
internal class ValueList : SortedList<string, object>
{
public override bool Equals (object obj) {
if (obj is ValueList) {
IEnumerator<KeyValuePair<string, object>> self = this.GetEnumerator();
IEnumerator<KeyValuePair<string, object>> other = ((ValueList)obj).GetEnumerator();
try {
while (self.MoveNext() && other.MoveNext()) {
if ((!self.Current.Key.Equals(other.Current.Key)) ||
(!self.Current.Value.Equals(other.Current.Value))) {
return false;
}
}
} finally {
self.Dispose();
other.Dispose();
}
return true;
} else {
return false;
}
}
public override int GetHashCode () {
unchecked { // Overflow is fine, just wrap
int hash = 17;
foreach (KeyValuePair<string, object> item in this) {
hash = hash * 23 + item.Key.GetHashCode();
hash = hash * 23 + item.Value.GetHashCode();
return hash;
}
return hash;
}
}
}
}