private void Work() { Tools.DebugLog("StaticMapService:Work() queue:" + queue.Count); if (queue.TryDequeue(out Tuple <int, int, int, int, StaticMapType, StaticMapMode> request)) { Tools.DebugLog($"StaticMapService:Work() request:{request.Item5} {request.Item1}->{request.Item2}"); int padding_x = 12; int padding_y = 12; int width = request.Item3 > 0 ? request.Item3 : 240; int height = request.Item4 > 0 ? request.Item4 : (int)(width / 1.618033); int tileSize = 256; List <Tuple <double, double> > coords = TripToCoords(request); Tuple <double, double, double, double> extent = DetermineExtent(coords); // calculate center point of map double lat_center = (extent.Item1 + extent.Item3) / 2; double lng_center = (extent.Item2 + extent.Item4) / 2; int zoom = 19; // max zoom double x_center = LonToTileX(lng_center, zoom); double y_center = LatToTileY(lat_center, zoom); if (request.Item5 == StaticMapType.Trip && coords.Count > 1) { // trip zoom = CalculateZoom(coords, padding_x, padding_y, width, height, tileSize); x_center = LonToTileX(lng_center, zoom); y_center = LatToTileY(lat_center, zoom); } using (Bitmap map = DrawMap(request, width, height, tileSize, coords, zoom, x_center, y_center)) { if (coords.Count == 1 && (request.Item5 == StaticMapType.Park || request.Item5 == StaticMapType.Charge)) { // park or charge StaticMapIcon icon = StaticMapIcon.Charge; if (request.Item5 == StaticMapType.Park) { icon = StaticMapIcon.Park; } DrawIcon(map, coords.First(), icon, zoom, x_center, y_center, tileSize); } else if (request.Item5 == StaticMapType.Trip && coords.Count > 1) { // trip DrawTrip(map, coords, zoom, x_center, y_center, tileSize); } else { Tools.DebugLog("StaticMapService:Work() request unknown type: " + request.Item5); return; } try { map.Save(FileManager.GetMapCachePath() + $"map_{request.Item1}_{request.Item2}.png.tmp"); File.Move(FileManager.GetMapCachePath() + $"map_{request.Item1}_{request.Item2}.png.tmp", FileManager.GetMapCachePath() + $"map_{request.Item1}_{request.Item2}.png"); } catch (Exception ex) { Logfile.Log(ex.ToString()); } } } }
private Bitmap DownloadTile(int zoom, int tile_x, int tile_y) { string localMapCacheFilePath = Path.Combine(FileManager.GetMapCachePath(), $"{zoom}_{tile_x}_{tile_y}.png"); if (MapFileExistsOrIsTooOld(localMapCacheFilePath, 8)) { // cached file too old or does not exist yet int retries = 0; while (!File.Exists(localMapCacheFilePath) && retries < 10) { retries++; int num = random.Next(0, 3); char abc = (char)('a' + num); Uri url = new Uri($"http://{abc}.tile.osm.org/{zoom}/{tile_x}/{tile_y}.png"); Tools.DebugLog("DownloadTile() url: " + url); try { using (WebClient wc = new WebClient()) { wc.Headers["User-Agent"] = this.GetType().ToString(); wc.DownloadFile(url, localMapCacheFilePath); wc.Dispose(); } } catch (WebException) { Tools.DebugLog("DownloadTile() failed for url: " + url); } } } else { Tools.DebugLog("DownloadTile() use cached local file " + localMapCacheFilePath); } try { using (Image img = Image.FromFile(localMapCacheFilePath)) { return(new Bitmap(img)); } } catch (Exception) { return(new Bitmap(tileSize, tileSize)); } }
public override void CreateTripMap(DataTable coords, int width, int height, MapMode mapmode, MapSpecial special, string filename) { if (coords == null) { return; } // workaround for linux mono libgdiplus memory leak Dictionary <string, object> job = new Dictionary <string, object>(); Tuple <double, double, double, double> extent = DetermineExtent(coords); if (extent == null) { return; } // calculate center point of map double lat_center = (extent.Item1 + extent.Item3) / 2; double lng_center = (extent.Item2 + extent.Item4) / 2; int zoom = CalculateZoom(extent, width, height); job.Add("zoom", zoom); double x_center = LngToTileX(lng_center, zoom); job.Add("x_center", x_center); double y_center = LatToTileY(lat_center, zoom); job.Add("y_center", y_center); job.Add("filename", filename); job.Add("width", width); job.Add("height", height); job.Add("mapmode", mapmode); job.Add("tileSize", tileSize); job.Add("MapCachePath", FileManager.GetMapCachePath()); List <double> latlng = new List <double>(); for (int row = 0; row < coords.Rows.Count; row++) { latlng.Add(Convert.ToDouble(coords.Rows[row]["lat"], Tools.ciDeDE)); latlng.Add(Convert.ToDouble(coords.Rows[row]["lng"], Tools.ciDeDE)); } job.Add("latlng", latlng.ToArray()); string tempfile = Path.GetTempFileName(); File.WriteAllText(tempfile, new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(job), Encoding.UTF8); GetOSMMapGeneratorFilename(out string fileName, out string arguments); arguments += "-jobfile " + tempfile + (Program.VERBOSE ? " - debug" : ""); using (Process process = new Process { StartInfo = new ProcessStartInfo { FileName = fileName, Arguments = arguments, UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true } }) { process.Start(); while (!process.StandardOutput.EndOfStream) { Logfile.Log(process.StandardOutput.ReadLine()); } process.WaitForExit(); } if (File.Exists(tempfile)) { File.Delete(tempfile); } }
public override void CreateParkingMap(double lat, double lng, int width, int height, MapMode mapmode, MapSpecial special, string filename) { int zoom = 16; // workaround for linux mono libgdiplus memory leak Dictionary <string, object> job = new Dictionary <string, object>(); double x_center = LngToTileX(lng, zoom); double y_center = LatToTileY(lat, zoom); job.Add("zoom", zoom); job.Add("x_center", x_center); job.Add("y_center", y_center); job.Add("filename", filename); job.Add("width", width); job.Add("height", height); job.Add("mapmode", mapmode); job.Add("tileSize", tileSize); job.Add("MapCachePath", FileManager.GetMapCachePath()); job.Add("poi", "park"); job.Add("lat", lat); job.Add("lng", lng); string tempfile = Path.GetTempFileName(); GetOSMMapGeneratorFilename(out string fileName, out string arguments); arguments += "-jobfile " + tempfile + (Program.VERBOSE ? " - debug" : ""); File.WriteAllText(tempfile, new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(job), Encoding.UTF8); using (Process process = new Process { StartInfo = new ProcessStartInfo { FileName = fileName, Arguments = arguments, UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true } }) { process.Start(); while (!process.StandardOutput.EndOfStream) { Logfile.Log(process.StandardOutput.ReadLine()); } process.WaitForExit(); } if (File.Exists(tempfile)) { File.Delete(tempfile); } /* * using (Bitmap map = DrawMap(width, height, 19, x_center, y_center, mapmode)) * { * // map has background tiles, OSM attribution and dark mode, if enabled * DrawIcon(map, lat, lng, MapIcon.Park, 19, x_center, y_center); * SaveImage(map, filename); * map.Dispose(); * } */ }