zoukankan      html  css  js  c++  java
  • C#中使用Surfer

    做地理信息或者绘制等值线,都会选择Surfer这个软件。这个软件对我们的作用有两个(1)插值(2)绘图。

    软件:Windows 7 x64,Microsoft Visual Studio 2012 RC,Surfer 10

    一、添加引用,Surfer在COM组件中。

    image

    然后在代码中加入 using Surfer;

    二、创建一组原始数据文件命名为data.dat

    A B C
    120.12 	31.52 	89.79 
    119.95 	31.32 	83.26 
    120.47 	31.22 	81.31 
    120.30 	31.10 	86.61 
    120.38 	30.97 	83.15 
    120.10 	30.95 	84.17 
    120.65 	31.20 	80.17 
    119.93 	30.28 	90.50 
    120.08 	30.55 	91.64 
    120.47 	30.90 	79.73 
    120.18 	30.48 	84.60 
    120.43 	30.53 	83.61 
    120.73 	30.75 	87.01 
    120.72 	30.88 	80.97 
    120.18 	30.72 	83.37 
    120.28 	30.62 	80.03 
    120.32 	30.78 	87.27 
    120.50 	30.75 	79.74 
    120.43 	30.88 	81.81 
    120.27 	30.45 	86.04 
    120.53 	30.63 	87.13 
    120.68 	30.52 	86.14 
    120.92 	30.43 	79.39 
    120.85 	30.53 	81.73 
    121.03 	30.68 	95.00 
    120.92 	30.85 	87.40 
    120.83 	30.75 	92.30 
    120.83 	31.02 	81.77 
    120.18 	31.65 	104.16 
    120.25 	31.75 	97.80 
    120.25 	31.92 	95.63 
    120.42 	31.95 	98.16 
    120.53 	31.73 	85.53 
    120.67 	31.98 	93.80 
    120.38 	31.80 	100.58 
    120.75 	31.63 	83.27 
    121.03 	31.70 	87.42 
    120.73 	31.48 	77.56 
    121.27 	31.50 	96.19 
    120.97 	31.38 	94.84 
    120.63 	31.00 	75.23 
    121.10 	31.45 	102.03 
    120.92 	31.12 	76.40 
    120.95 	31.32 	94.21 
    119.35 	31.45 	90.56 
    119.52 	31.43 	88.80 
    119.83 	31.37 	103.36 
    119.57 	31.75 	97.85 
    119.53 	31.68 	96.52 
    119.55 	31.23 	92.77 
    119.98 	31.52 	85.24 
    119.25 	31.55 	101.62 
    119.43 	31.23 	90.09 
    119.75 	31.68 	102.12 
    119.57 	31.63 	98.45 
    119.57 	31.98 	93.84 
    119.63 	30.27 	92.78 
    119.68 	30.23 	92.63 
    119.80 	30.25 	100.69 
    119.97 	30.40 	92.96 
    119.90 	30.53 	95.44 
    119.72 	30.73 	90.87 
    119.87 	30.87 	86.82 
    119.48 	30.53 	96.67 
    119.57 	30.43 	152.66 
    119.58 	30.35 	100.68 
    119.52 	30.30 	84.11 
    119.63 	30.18 	102.20 
    119.75 	30.33 	90.42 
    119.87 	30.62 	92.62 
    119.78 	30.53 	107.69 
    119.83 	30.52 	107.95 
    120.00 	30.78 	107.96 
    119.25 	30.53 	92.84 
    119.33 	30.63 	98.59 
    119.38 	30.58 	83.97 
    119.42 	30.42 	94.15 
    119.38 	30.48 	93.54 
    119.47 	30.45 	102.92 
    119.52 	30.43 	124.86 
    119.63 	30.50 	117.88 
    119.70 	30.63 	92.68 
    119.58 	30.75 	99.43 
    119.60 	30.83 	94.63 
    119.80 	30.70 	106.83 
    119.80 	31.05 	93.04 
    119.92 	31.00 	91.79 
    119.55 	30.92 	87.18 
    119.68 	31.12 	100.53 
    119.78 	31.15 	115.56 
    121.15 	30.97 	78.28 
    120.98 	31.08 	78.57 
    121.12 	31.12 	78.25 
    121.23 	31.27 	96.45 
    121.15 	31.33 	98.21 
    121.73 	30.98 	81.56 
    

    三、下面对上面的原始文件进行插值

    Application app = new Application();
    var path = AppDomain.CurrentDomain.BaseDirectory;
    app.GridData2(DataFile: path + "data.dat",			//数据文件地址
    	xCol: 1,										//x为第一列数据
    	yCol: 2,										//y为第二列数据
    	zCol: 3,										//z为第三列数据
    	DupMethod: Surfer.SrfDupMethod.srfDupNone,
    	xMin: 117.742635,								//x最小值
    	xMax: 122.452486,								//x最大者
    	yMin: 29.418809,								//y最小值
    	yMax: 32.463007,								//y最大值
    	Algorithm: Surfer.SrfGridAlgorithm.srfKriging,	//插值算法Kriging
    	NumCols: (122.452486 - 117.742635) / 0.01,		//x方向插值数据量
    	NumRows: (32.463007 - 29.418809) / 0.01,		//y方向插值数据量
    	OutGrid: path + "grid.grd",						//返回文件为gridfile
    	OutFmt: Surfer.SrfGridFormat.srfGridFmtAscii);	//返回文件编码为Ascii
    app.Quit();
    System.GC.Collect(System.GC.GetGeneration(app));
    

    插值算法有很多,大家可以去API中查找SrfGridAlgorithm,这里就不多说了。

    生成的文件格式也有很多种,有种比较精简的是srfGridFmtBinary。

    四、查看目录下的grid.grd文件,插值完成

    DSAA
    471 304
    117.742635 122.452486
    29.418809 32.463007
    75.361336919208 149.92207125061
    91.83441632763108 91.81999394510157 91.80551937407597 91.79099305953743 91.77641547089857 91.76178710274148 91.74710847558691 91.73238013667276 91.71760266076672 91.70277665099104 
    91.68790273967451 91.67298158922344 91.65801389301778 91.64300037633481 91.62794179728556 91.61283894778934 91.59769265456495 91.58250378014809 91.56727322394012 91.55200192327513 
    

    五、生成图片

    生成图片有很多很多种类,这里就只讲解2种一个是ContourMap另一个是BaseMap代码如下,就不细致讲解了。

    Application app = new Application();
    IDocuments docs = app.Documents;
    IPlotDocument Doc = (IPlotDocument)docs.Add(SrfDocTypes.srfDocPlot);	//创建一个空白绘图文档
    IShapes Shapes = Doc.Shapes;
    var path = AppDomain.CurrentDomain.BaseDirectory;
    
    #region 添加等值面
    IMapFrame contourMapFrame = Shapes.AddContourMap(path + "grid.grd");	//加载网格文件
    for (int i = 1; i <= contourMapFrame.Axes.Count; i++)
    {
    	contourMapFrame.Axes.Item(i).Visible = false;
    	contourMapFrame.Axes.Item(i).MajorTickType = SrfTickType.srfTickNone;
    	contourMapFrame.Axes.Item(i).ShowLabels = false;
    }
    contourMapFrame.SetLimits(xMin: 117.742635,	//x最小值
    				xMax: 122.452486,			//x最大者
    				yMin: 29.418809,			//y最小值
    				yMax: 32.463007				//y最大值
    );
    contourMapFrame.xMapPerPU = 0.25;			//设置比例
    contourMapFrame.yMapPerPU = 0.25;			//设置比例
    
    IContourMap contourMap = (IContourMap)contourMapFrame.Overlays.Item(1);
    
    //contourMap.ShowColorScale = true;										// 显示对应色柱
    //contourMap.ColorScale.Top = 8.48;										//色柱y方向位置
    //contourMap.ColorScale.Left = contourMap.Left + contourMap.Width + 0.8;//色柱x方向位置
    //contourMap.ColorScale.Width = 0.8;									//色柱宽度
    //contourMap.ColorScale.Height = 8;										//色柱高度
    
    contourMap.FillContours = true;//添加颜色填充
    //通过文件加载颜色
    //ILevels levels = contourMap.Levels;
    //levels.LoadFile(Server.MapPath("Data\\Desert.lvl"));
    
    //加载系统颜色
    contourMap.FillForegroundColorMap.LoadFile("C:\\Program Files\\Golden Software\\Surfer 10\\ColorScales\\Rainbow.clr");
    contourMap.ApplyFillToLevels(1, 1, 0);
    
    //使用灰色
    //contourMap.Levels.AutoGenerate(contourMap.Grid.zMin,contourMap.Grid.zMax,10);
    
    for (int i = 0; i < contourMap.Levels.Count; i++)
    {
    	contourMap.Levels.Item(i + 1).ShowLabel = true;					//显示等值线上的数值
    	contourMap.Levels.Item(i + 1).ShowHach = false;					//
    	contourMap.Levels.Item(i + 1).Line.Style = "Invisible";			//不显示线
    }
    
    contourMap.SmoothContours = SrfConSmoothType.srfConSmoothNone;   //平滑等值线边界当前设置不平滑
    #endregion
    
    
    #region 添加边界
    //后添加的会覆盖在先前添加的图片之上
    IMapFrame boundryMapFrame = Shapes.AddBaseMap(path + "boundry.bln", "Defaults=1");
    for (int i = 1; i <= boundryMapFrame.Axes.Count; i++)
    {
    	boundryMapFrame.Axes.Item(i).Visible = false;							//隐藏轴线
    	boundryMapFrame.Axes.Item(i).MajorTickType = SrfTickType.srfTickNone;	//隐藏边线
    	boundryMapFrame.Axes.Item(i).ShowLabels = false;						//隐藏轴线上的坐标
    }
    boundryMapFrame.SetLimits(xMin: 117.742635,	//x最小值
    				xMax: 122.452486,			//x最大者
    				yMin: 29.418809,			//y最小值
    				yMax: 32.463007				//y最大值
    );
    boundryMapFrame.xMapPerPU = 0.25;
    boundryMapFrame.yMapPerPU = 0.25;
    
    IBaseMap boundryBaseMap = (IBaseMap)boundryMapFrame.Overlays.Item(1);
    boundryBaseMap.Line.Width = 0.01;			//设置边线宽度
    #endregion
    
    string strWH = string.Format("width = {0:f0}, height = {1:f0}, KeepAspect = 1, ColorDepth = 32", 1024, 768);//设置输出图片的高度和宽度
    Doc.Export2(path + "Image.png", SelectionOnly: false, Options: strWH, FilterId: "png");//设置输出图片格式名
    Doc.Close(SrfSaveTypes.srfSaveChangesNo);	//不生成srf文件
    app.Quit();
    System.GC.Collect(System.GC.GetGeneration(app));
    

    六、生成的Image.png

    image

    七、发布

    发布到WinForm没有任何问题,如果发布到Web必须修改应用连接池权限。Surfer8及之前的版本无此问题。

    八、总结

    这个软件的API其实还是蛮大的,不过文档都是VB的,所以很多地方需要自己摸索,特别是发布为Web,找了很多办法才解决。

    主体上来说代码部分就上面所写的了。

    谢谢观赏

  • 相关阅读:
    搭建一个属于私人博客
    Python正则表达式的匹配规则
    CentOS 使用yum 安装node.js
    一个单词a,如果通过交换单词中字母的顺序可以得到另外的单词b,那么定义b是a的兄弟单词。现在有一个字典,用户输入一个单词,从字典找出这个单词有多少个兄弟单词
    Clion报错 CMake Error at CMakeLists.txt:1 (cmake_minimum_required): CMake 3.
    给定一个整数sum,从n个有序的元素的数组中寻找a,b,使得a+b的结果最接近sum,最快的时间复杂度?
    Go语言通过Docker Go语言SDK获取docker stats的信息
    通过GO程序获取docker version的基本信息
    Go语言实现通过Docker SDK获取docker ps 命令信息&SDK 中docker ps源码解析
    Docker监控docker stats命令的使用与返回参数的意思
  • 原文地址:https://www.cnblogs.com/sorex/p/2620188.html
Copyright © 2011-2022 走看看