public Texture2D generateTexture(MapProvider provider, BBox bbox, int i, int j, string OSMFileName) { string[] proj = OSMFileName.Split(new char[] { '/', '\\' }); string projectName = proj[proj.Length - 1]; if (File.Exists(tileFolder + "/final/" + provider.ToString("G") + "_" + projectName + "_" + i + "_" + j + ".png")) { byte[] fileData; fileData = File.ReadAllBytes(tileFolder + "/final/" + provider.ToString("G") + "_" + projectName + "_" + i + "_" + j + ".png"); Texture2D texture = new Texture2D(2, 2); texture.LoadImage(fileData); return(texture); } Texture2D uncroppedTexture = generateUncroppedTexture(bbox, provider, i, j); Rect cropWindow = generateCroppingRect(bbox); Texture2D finalTexture = CropTexture(uncroppedTexture, cropWindow); if (!Directory.Exists(tileFolder + "/final/")) { Directory.CreateDirectory(tileFolder + "/final/"); } var tex = new Texture2D(finalTexture.width, finalTexture.height); tex.SetPixels32(finalTexture.GetPixels32()); tex.Apply(false); File.WriteAllBytes(tileFolder + "/final/" + provider.ToString("G") + "_" + projectName + "_" + i + "_" + j + ".png", tex.EncodeToPNG()); return(finalTexture); }
public Texture2D generateTexture(MapProvider provider, BBox bbox, int i, int j, string OSMFileName) { string[] proj = OSMFileName.Split(new char[] {'/', '\\'}); string projectName = proj[proj.Length - 1]; if (File.Exists(tileFolder + "/final/" + provider.ToString("G") + "_" + projectName + "_" + i + "_" + j + ".png")) { byte[] fileData; fileData = File.ReadAllBytes(tileFolder + "/final/" + provider.ToString("G") + "_" + projectName + "_" + i + "_" + j + ".png"); Texture2D texture = new Texture2D(2, 2); texture.LoadImage(fileData); return texture; } Texture2D uncroppedTexture = generateUncroppedTexture(bbox, provider, i, j); Rect cropWindow = generateCroppingRect(bbox); Texture2D finalTexture = CropTexture(uncroppedTexture, cropWindow); if (!Directory.Exists(tileFolder + "/final/")) Directory.CreateDirectory(tileFolder + "/final/"); var tex = new Texture2D(finalTexture.width, finalTexture.height); tex.SetPixels32(finalTexture.GetPixels32()); tex.Apply(false); File.WriteAllBytes(tileFolder + "/final/" + provider.ToString("G") + "_" + projectName + "_" + i + "_" + j + ".png", tex.EncodeToPNG()); return finalTexture; }
/// <summary> /// Returns the name of the cached image that corresponds with the parameters passed across. /// </summary> /// <param name="mapProvider"></param> /// <param name="tileServerName"></param> /// <param name="zoom"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="retina"></param> /// <returns></returns> private static string BuildFileName(Options options, MapProvider mapProvider, string tileServerName, string zoom, string x, string y, string retina, string extension) { var rootFolder = GetRootFolder(options); var result = Path.Combine(rootFolder, SanitisePathPart(mapProvider.ToString())); result = Path.Combine(result, SanitisePathPart(tileServerName)); result = Path.Combine(result, SanitisePathPart(zoom)); result = Path.Combine(result, SanitisePathPart(x)); result = Path.Combine(result, SanitisePathPart($"{y}{retina}{extension}")); result = Path.GetFullPath(result); if (result.Length < rootFolder.Length) { result = null; } else if (!String.Equals(rootFolder, result.Substring(0, rootFolder.Length), StringComparison.OrdinalIgnoreCase)) { result = null; } return(result); }
private Texture2D DownloadTile(int tilex, int tiley, MapProvider provider) { string _URL = ""; if (!Directory.Exists(tileFolder + provider.ToString("G"))) { Directory.CreateDirectory(tileFolder + provider.ToString("G")); } string savedfileName = +zoomLevel + "_" + tilex + "_" + tiley; switch (provider) { case MapProvider.MapQuest: _URL = "http://otile1.mqcdn.com/tiles/1.0.0/osm/" + zoomLevel.ToString() + "/" + tilex.ToString() + "/" + tiley.ToString() + ".png"; savedfileName = savedfileName + ".png"; break; case MapProvider.OpenStreetMap: _URL = "http://" + "a" + ".tile.openstreetmap.org/" + zoomLevel.ToString() + "/" + tilex.ToString() + "/" + tiley.ToString() + ".png"; savedfileName = savedfileName + ".png"; break; case MapProvider.OpenStreetMapNoLabel: _URL = "http://a.tiles.wmflabs.org/osm-no-labels/" + zoomLevel.ToString() + "/" + tilex.ToString() + "/" + tiley.ToString() + ".png"; savedfileName = savedfileName + ".png"; break; case MapProvider.BingMapStreet: _URL = "http://ecn.t" + ((tilex + tiley) % 7).ToString() + ".tiles.virtualearth.net/tiles/" + "r"; for (int i = zoomLevel - 1; i >= 0; i--) { _URL = _URL + (((((tiley >> i) & 1) << 1) + ((tilex >> i) & 1))); } _URL = _URL + ".png" + "?g=409&mkt=en-us"; savedfileName = savedfileName + ".png"; break; case MapProvider.BingMapAerial: _URL = "http://ecn.t" + ((tilex + tiley) % 7).ToString() + ".tiles.virtualearth.net/tiles/" + "a"; for (int i = zoomLevel - 1; i >= 0; i--) { _URL = _URL + (((((tiley >> i) & 1) << 1) + ((tilex >> i) & 1))); } _URL = _URL + ".jpeg" + "?g=409&mkt=en-us"; savedfileName = savedfileName + ".jpg"; break; default: return(null); } try { FileDownloader.downloadfromURL(_URL, tileFolder + provider.ToString("G") + "/" + savedfileName); } catch { if (provider == MapProvider.OpenStreetMapNoLabel) { _URL = "http://" + "a" + ".tile.openstreetmap.org/" + zoomLevel.ToString() + "/" + tilex.ToString() + "/" + tiley.ToString() + ".png"; FileDownloader.downloadfromURL(_URL, tileFolder + provider.ToString("G") + "/" + savedfileName); } } byte[] fileData; fileData = File.ReadAllBytes(tileFolder + provider.ToString("G") + "/" + savedfileName); Texture2D texture = new Texture2D(2, 2); texture.LoadImage(fileData); return(texture); }
private Texture2D DownloadTile(int tilex,int tiley, MapProvider provider) { string _URL = ""; if(!Directory.Exists(tileFolder + provider.ToString("G"))) Directory.CreateDirectory(tileFolder+ provider.ToString("G")); string savedfileName = + zoomLevel + "_" + tilex + "_" + tiley; switch (provider) { case MapProvider.MapQuest: _URL = "http://otile1.mqcdn.com/tiles/1.0.0/osm/" + zoomLevel.ToString() + "/" + tilex.ToString() + "/" + tiley.ToString() + ".png"; savedfileName = savedfileName + ".png"; break; case MapProvider.OpenStreetMap: _URL = "http://" + "a" + ".tile.openstreetmap.org/" + zoomLevel.ToString() + "/" + tilex.ToString() + "/" + tiley.ToString() + ".png"; savedfileName = savedfileName + ".png"; break; case MapProvider.OpenStreetMapNoLabel: _URL = "http://a.tiles.wmflabs.org/osm-no-labels/" + zoomLevel.ToString() + "/" + tilex.ToString() + "/" + tiley.ToString() + ".png"; savedfileName = savedfileName + ".png"; break; case MapProvider.BingMapStreet: _URL = "http://ecn.t" + ((tilex + tiley) % 7).ToString() + ".tiles.virtualearth.net/tiles/" + "r"; for (int i = zoomLevel - 1; i >= 0; i--) { _URL = _URL + (((((tiley >> i) & 1) << 1) + ((tilex >> i) & 1))); } _URL = _URL + ".png" + "?g=409&mkt=en-us"; savedfileName = savedfileName + ".png"; break; case MapProvider.BingMapAerial: _URL = "http://ecn.t" + ((tilex + tiley) % 7).ToString() + ".tiles.virtualearth.net/tiles/" + "a"; for (int i = zoomLevel - 1; i >= 0; i--) { _URL = _URL + (((((tiley >> i) & 1) << 1) + ((tilex >> i) & 1))); } _URL = _URL + ".jpeg" + "?g=409&mkt=en-us"; savedfileName = savedfileName + ".jpg"; break; default: return null; } try { FileDownloader.downloadfromURL(_URL, tileFolder + provider.ToString("G") + "/" + savedfileName); } catch { if(provider == MapProvider.OpenStreetMapNoLabel) { _URL = "http://" + "a" + ".tile.openstreetmap.org/" + zoomLevel.ToString() + "/" + tilex.ToString() + "/" + tiley.ToString() + ".png"; FileDownloader.downloadfromURL(_URL, tileFolder + provider.ToString("G") + "/" + savedfileName); } } byte[] fileData; fileData = File.ReadAllBytes(tileFolder + provider.ToString("G") + "/" + savedfileName); Texture2D texture = new Texture2D(2, 2); texture.LoadImage(fileData); return texture; }