public void DrawHeat(string filePath, string sheetName)
{
string ParamPalettePath = @"D:\Ventuz5\01\Images\HeatMap/palette.png";//颜色板
DataTable dt = GetExcelTable(filePath, sheetName,2);
List<double> lon = new List<double>();
List<double> lat = new List<double>();
List<double> weight = new List<double>();
for (int i = 0; i < dt.Rows.Count; i++)
{
string[] lonAndLat = dt.Rows[i]["坐标值"].ToString().Split(',');
double lonStr = Convert.ToDouble(lonAndLat[0]);
double latStr = Convert.ToDouble(lonAndLat[1]);
double weightStr = Convert.ToDouble(dt.Rows[i]["权重值"].ToString());
lon.Add(lonStr);lat.Add(latStr);weight.Add(weightStr);
}
double latRange = Math.Abs(lat.Max() - lat.Min());
double lngRange = Math.Abs(lon.Max() - lon.Min());
int width = 0;
int height = 0;
double scale = 1;
if (lngRange > latRange)
{
width = 500;
scale = lngRange / 500;
height = (int)(latRange / lngRange * 500);
}
else
{
height =500;
scale = latRange / 500;
width = (int)(lngRange / latRange * 500);
}
Bitmap bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
RayHeatMap heatMap = new RayHeatMap();
heatMap.BrushStop = 0.3f;
heatMap.Radius = 30;
for (int i = 0; i < lat.Count; i++)
{
int y = (int)((lat[i] - lat.Min()) / scale);
int x = (int)((lon[i] - lon.Min()) / scale);
int weightInt = (int)((double)weight[i] / weight.Max() * 255);
heatMap.AddPoint(new Point(x, y), weightInt);
}
heatMap.Draw(ref bitmap);
heatMap.Colorize(ref bitmap, ParamPalettePath);
DirectoryInfo dir = new DirectoryInfo(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(ParamPalettePath), "ShenZhen"));
if (!dir.Exists)
dir.Create();
string name = DateTime.Now.ToString("yyyyMMddHHmm") + ".png";
string filePaths = System.IO.Path.Combine(dir.ToString(), name);
FileStream fs = File.Open(filePaths, FileMode.Create);
bitmap.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
fs.Close();
}
本文介绍了一种使用C#从Excel文件中读取坐标及权重数据,并基于这些数据生成热力图的方法。该方法首先定义了一个名为DrawHeat的过程,通过指定的文件路径和工作表名称加载数据,然后提取经纬度和权重值,计算比例尺和图像尺寸,最终创建并保存热力图。
&spm=1001.2101.3001.5002&articleId=81533979&d=1&t=3&u=49b24c487f864f7fa361026887bdc653)
6840

被折叠的 条评论
为什么被折叠?



