public TileDescription NextTile() { TileDescription result = null; lock (m_tiles) { if (m_tiles.MoveNext()) { result = m_tiles.Current; } else { result = null; } } return(result); }
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(); } }