-
Notifications
You must be signed in to change notification settings - Fork 1
/
UTFGrid.cs
152 lines (140 loc) · 6.83 KB
/
UTFGrid.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using ESRI.ArcGIS;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using NDesk.Options;
namespace NatGeo.UTFGrid
{
class UTFGrid
{
[STAThread()]
static void Main (string[] args) {
bool showHelp = false;
bool gzip = false;
bool overwrite = false;
bool verbose = false;
string destination = ".";
int[] levels = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
HashSet<string> fields = null;
int threadCount = System.Environment.ProcessorCount;
OptionSet p = new OptionSet() {
{ "d|dir=", "destination directory (defaults to current directory)", d => destination = d },
{ "l|levels=",
"list of scale levels [0-19], separated by commas",
l => levels = l.Split(new char[] { ',' }).Select(s => Convert.ToInt32(s)).ToArray() },
{ "f|fields=",
"list of field names to include in UTFGrid data",
f => fields = new HashSet<string>(f.Split(new char[] { ',' })) },
{ "t|threads=",
"number of threads to use (defaults to number of processors)",
t => threadCount = Convert.ToInt32(t) },
{ "z|zip", "zip the json files using gzip compression before saving", z => gzip = z != null },
{ "o|overwrite", "overwrite existing files", o => overwrite = o != null },
{ "v|verbose", "verbose output", v => verbose = v != null },
{ "h|help", "show this message and exit", h => showHelp = h != null }
};
List<string> extra;
try {
extra = p.Parse(args);
} catch (OptionException e) {
Console.Write("utfgrid");
Console.WriteLine(e.Message);
Console.WriteLine("Try `utfgrid --help' for more information.");
return;
}
if (showHelp) {
Console.WriteLine("Usage: utfgrid [OPTIONS]+ mxd_document");
Console.WriteLine("Generate UTFGrid files from the given map document");
Console.WriteLine();
Console.WriteLine("Options:");
p.WriteOptionDescriptions(Console.Out);
return;
} else if (extra.Count < 1) {
Console.WriteLine("utfgrid: no map document specified");
Console.WriteLine("Try `utfgrid --help' for more information.");
return;
}
RuntimeManager.BindLicense(ProductCode.EngineOrDesktop);
IMap map = null;
try {
IMapDocument mapDocument = new MapDocumentClass();
mapDocument.Open(extra[0], null);
map = mapDocument.ActiveView as IMap;
if (map == null) {
map = mapDocument.get_Map(0);
}
mapDocument.Close();
} catch (Exception) { }
if (map == null) {
Console.WriteLine("Unable to open map at " + extra[0]);
return;
}
if ((map.SpatialReference.FactoryCode != 102113) &&
(map.SpatialReference.FactoryCode != 102100) &&
(map.SpatialReference.FactoryCode != 3785)) {
Console.WriteLine("Spatial reference of map must be Web Mercator (is " + map.SpatialReference.FactoryCode + ")");
return;
}
IActiveView activeView = map as IActiveView;
// get the extent from the active view
IEnvelope fullExtent = activeView.FullExtent;
Console.WriteLine("starting utfgrid generator with " + threadCount + " threads");
UTFGridGeneratorConfig config = new UTFGridGeneratorConfig(extra[0], DescribeTiles(levels, activeView.FullExtent));
config.GZip = gzip;
config.Overwrite = overwrite;
config.Verbose = verbose;
config.Destination = destination;
Thread[] workerThreads = new Thread[threadCount];
for (int i = 0; i < threadCount; i++) {
workerThreads[i] = new Thread(new ParameterizedThreadStart(UTFGridGenerator.Execute));
workerThreads[i].SetApartmentState(ApartmentState.STA);
workerThreads[i].IsBackground = true;
workerThreads[i].Priority = ThreadPriority.BelowNormal;
workerThreads[i].Name = "UTFGridGenerator " + (i + 1).ToString();
workerThreads[i].Start(config);
}
foreach (Thread t in workerThreads) {
t.Join();
}
workerThreads = null;
}
private static IEnvelope ClampExtent (IEnvelope extent) {
EnvelopeClass result = new EnvelopeClass();
double origin = Math.PI * 6378137;
result.XMin = Math.Max(extent.XMin, -origin);
result.YMin = Math.Max(extent.YMin, -origin);
result.XMax = Math.Min(extent.XMax, origin - 1);
result.YMax = Math.Min(extent.YMax, origin - 1);
return result;
}
private static Cell MetersToTile (int level, double mx, double my) {
double resolution = (2 * Math.PI * 6378137.0 / 256.0) / Math.Pow(2, level);
double origin = Math.PI * 6378137;
double px = (mx + origin) / resolution;
double py = (origin - my) / resolution;
int tx = (int)Math.Floor(px / 256.0);
int ty = (int)Math.Floor(py / 256.0);
return new Cell(ty, tx);
}
private static IEnumerable<TileDescription> DescribeTiles (int[] levels, IEnvelope extent) {
extent = ClampExtent(extent);
foreach (int level in levels) {
double resolution = (2 * Math.PI * 6378137.0 / 256.0) / Math.Pow(2, level);
double origin = Math.PI * 6378137;
double tileSizeM = resolution * 256;
Cell topLeft = MetersToTile(level, extent.XMin, extent.YMax);
Cell botRight = MetersToTile(level, extent.XMax, extent.YMin);
for (int y = topLeft.Row; y <= botRight.Row; y += 1) {
for (int x = topLeft.Col; x <= botRight.Col; x += 1) {
double mx = -origin + tileSizeM * x;
double my = origin - tileSizeM * y;
yield return new TileDescription(level, y, x, mx, my - tileSizeM, mx + tileSizeM, my);
}
}
}
}
}
}