public void DownloadTilesAsync() { TileBounds bounds = new TileBounds(Area); // Abort currently running tasks and increase generation worker.ClearTasks(); tasks.Clear(); nTasksForArea = 0; generation++; foreach (var tileAddress in bounds.TileAddressRange) { nTasksForArea++; } foreach (var tileAddress in bounds.TileAddressRange) { float offsetX = (tileAddress.x - bounds.min.x); float offsetY = (-tileAddress.y + bounds.min.y); float scaleRatio = (float)tileAddress.GetSizeMercatorMeters() * UnitsPerMeter; Matrix4x4 scale = Matrix4x4.Scale(new Vector3(scaleRatio, scaleRatio, scaleRatio)); Matrix4x4 translate = Matrix4x4.Translate(new Vector3(offsetX * scaleRatio, 0.0f, offsetY * scaleRatio)); Matrix4x4 transform = translate * scale; IEnumerable <FeatureCollection> featureCollections = tileCache.Get(tileAddress); if (featureCollections != null) { var task = new TileTask(Style, tileAddress, transform, generation); worker.RunAsync(() => { if (generation == task.Generation) { task.Start(featureCollections); tasks.Add(task); } }); } else { // Use a local generation variable to be used in IORequestCallback coroutine int requestGeneration = generation; var wrappedTileAddress = tileAddress.Wrapped(); var uri = new Uri(string.Format("https://tile.mapzen.com/mapzen/vector/v1/all/{0}/{1}/{2}.mvt?api_key={3}", wrappedTileAddress.z, wrappedTileAddress.x, wrappedTileAddress.y, ApiKey)); IO.IORequestCallback onTileFetched = (response) => { if (requestGeneration != generation) { // Another request has been made before the coroutine was triggered return; } if (response.hasError()) { Debug.Log("TileIO Error: " + response.error); return; } if (response.data.Length == 0) { Debug.Log("Empty Response"); return; } var task = new TileTask(Style, tileAddress, transform, generation); worker.RunAsync(() => { // Skip any tasks that have been generated for a different generation if (generation == task.Generation) { // var tileData = new GeoJsonTile(address, response); var mvtTile = new MvtTile(tileAddress, response.data); // Save the tile feature collections in the cache for later use tileCache.Add(tileAddress, mvtTile.FeatureCollections); task.Start(mvtTile.FeatureCollections); tasks.Add(task); } }); }; // Starts the HTTP request StartCoroutine(tileIO.FetchNetworkData(uri, onTileFetched)); } } }
public void DownloadTilesAsync() { TileBounds bounds = new TileBounds(Area); // Abort currently running tasks and increase generation worker.ClearTasks(); tasks.Clear(); nTasksForArea = 0; generation++; foreach (var tileAddress in bounds.TileAddressRange) { nTasksForArea++; tempTileAddress = tileAddress; } foreach (var tileAddress in bounds.TileAddressRange) { float offsetX = (tileAddress.x - bounds.min.x); float offsetY = (-tileAddress.y + bounds.min.y); float scaleRatio = (float)tileAddress.GetSizeMercatorMeters() * UnitsPerMeter; Matrix4x4 scale = Matrix4x4.Scale(new Vector3(scaleRatio, scaleRatio, scaleRatio)); Matrix4x4 translate = Matrix4x4.Translate(new Vector3(offsetX * scaleRatio, 0.0f, offsetY * scaleRatio)); Matrix4x4 transform = translate * scale; tempTransform = transform; IEnumerable <FeatureCollection> featureCollections = tileCache.Get(tileAddress); if (featureCollections != null) { var task = new TileTask(Style, tileAddress, transform, generation); worker.RunAsync(() => { if (generation == task.Generation) { task.Start(featureCollections); tasks.Add(task); Debug.Log("New Task to add is (1) " + task.Address.ToString()); } }); } else { // Use a local generation variable to be used in IORequestCallback coroutine int requestGeneration = generation; var wrappedTileAddress = tileAddress.Wrapped(); var uri = new Uri(string.Format("https://tile.nextzen.org/tilezen/vector/v1/all/{0}/{1}/{2}.mvt?api_key={3}", wrappedTileAddress.z, wrappedTileAddress.x, wrappedTileAddress.y, ApiKey)); IO.IORequestCallback onTileFetched = (response) => { if (requestGeneration != generation) { // Another request has been made before the coroutine was triggered return; } if (response.hasError()) { Debug.Log("TileIO Error: " + response.error); return; } if (response.data.Length == 0) { Debug.Log("Empty Response"); return; } var task = new TileTask(Style, tileAddress, transform, generation); worker.RunAsync(() => { // Skip any tasks that have been generated for a different generation if (generation == task.Generation) { // var tileData = new GeoJsonTile(address, response); var mvtTile = new MvtTile(tileAddress, response.data); // Save the tile feature collections in the cache for later use tileCache.Add(tileAddress, mvtTile.FeatureCollections); task.Start(mvtTile.FeatureCollections); tasks.Add(task); // Debug.Log("New Task (from else) to add is " + task.Address.ToString()); // Debug.Log("Data is " + task.Data.Count); // string result = System.Text.Encoding.UTF8.GetString(response.data); // Debug.Log("---->>> Response is " + result); } }); }; // Starts the HTTP request StartCoroutine(tileIO.FetchNetworkData(uri, onTileFetched)); } } //-------------------------------- var myTask = new TileTask(Style, tempTileAddress, tempTransform, generation); var binFormatter = new BinaryFormatter(); var mStream = new MemoryStream(); var vect3 = new List <Vector3> { new Vector3(0, 0, 0), new Vector3(100, 0, 0), new Vector3(100, 100, 0), new Vector3(0, 100, 0), new Vector3(0, 100, 100), new Vector3(100, 100, 100), new Vector3(100, 0, 100), new Vector3(0, 0, 100) }; // binFormatter.Serialize(mStream, vect3); // var myMvtTile = new MvtTile(tempTileAddress, mStream.ToArray()); // myTask.Start(myMvtTile.FeatureCollections); // tasks.Add(myTask); }