- 积分
- 55959
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-6-21
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 MeteoInfo 于 2011-8-24 14:33 编辑
“图文详解文本格式站点数据绘制等值线图”的帖子里降水等值线图绘制的脚本程序中用到了事先在MeteoInfo软件里编辑好的图例文件:
#---- 设置图例文件
myApp.SetLegendScheme(LegendDir + "rain1.lgs")
这个帖子讲讲用代码在脚本程序里生成一个图例LegendScheme的例子。需要最新的MeteoInfoC.dll文件(见置顶帖子)。
#---- 引用MeteoInfo的Legend和Shape命名空间和类
from MeteoInfoC.Legend import *
from MeteoInfoC.Shape import *
#---- 生成等值线值数组
vals = Array[Double]([0.1,1,2,5,10,20,25,50,100])
#---- 生成颜色数组(因为填色的需要,颜色数组要比等值线值数组多一个元素)
colors = Array.CreateInstance(Color, vals.Length + 1)
#---- 通过RGB值给定颜色,Color.FromArgb函数中第一个参数是alpha值(0是完全透明,255是完全不透明)
#---- 后面三个参数分别是R, G, B值
colors[0] = Color.White
colors[1] = Color.FromArgb(255,170,240,255)
colors[2] = Color.FromArgb(255,120,230,240)
colors[3] = Color.FromArgb(255,200,220,50)
colors[4] = Color.FromArgb(255,240,220,20)
colors[5] = Color.FromArgb(255,255,120,10)
colors[6] = Color.FromArgb(255,255,90,10)
colors[7] = Color.FromArgb(255,240,40,0)
colors[8] = Color.FromArgb(255,180,10,0)
colors[9] = Color.FromArgb(255,120,10,0)
#---- 生成图例,前两个参数是上面两个数组,第三个参数是图元类型(枚举类型,本例中是ShapetTypes.Polygon)
#---- 第4和5参数是最小值和最大值(本例中降水给的范围能包含所有可能数据即可)
#---- 第6个参数表明图例中是否有无效数据,最后一个参数是无效数据的值
aLS = LegendManage.CreateGraduatedLegendScheme(vals,colors,ShapeTypes.Polygon,0,1000,False,-9999.0)
#---- 不绘制图例中第一个Break
aLS.breakList[0].DrawFill = False
#---- 将生成的图例付给myApp
myApp.LegendScheme = aLS
myApp.UseDefaultLegendScheme = True
完整的脚本程序如下:
# This Python file uses the following encoding: GB2312
#---- 引入类库
import clr
clr.AddReferenceByPartialName("System")
clr.AddReferenceByPartialName("System.Windows.Forms")
clr.AddReferenceByPartialName("System.Drawing")
from System import *
from System.Windows.Forms import *
from System.Drawing import *
clr.AddReference("MeteoInfoC.dll")
from MeteoInfoC import *
from MeteoInfoC.Layout import *
from MeteoInfoC.Data import *
from MeteoInfoC.Legend import *
from MeteoInfoC.Shape import *
#---- 设置路径变量
BaseDir = "C:\\Program Files (x86)\\MeteoInfo\\"
MapDir = BaseDir + "Map\\"
LegendDir = BaseDir + "Legend\\"
DataDir = "E:\\Temp\\"
#---- 创建MIApp类的对象
myApp = MIApp()
#---- 添加图层
myApp.OpenLayer(MapDir + "bou2_4p.shp")
#---- 设置图层渲染
myApp.SetLegendBreak("bou2_4p.shp",0,Color.Yellow,Color.Gray,1,True,False,True)
myApp.OpenLayer(MapDir + "bou1_4l.shp")
myApp.SetLegendBreak("bou1_4l.shp",0,Color.Blue)
myApp.OpenLayer(MapDir + "china.shp")
myApp.SetLayerVisible("china.shp", False)
myApp.OpenLayer(MapDir + "res1_4m.shp")
myApp.SetLegendBreak("res1_4m.shp",0,5,Color.Red,Color.Black,True,True,True)
#---- 获取矢量图层对象,并设置标注
aLayer = myApp.GetVectorLayer("res1_4m.shp")
aLayer.LabelSet.FieldName = "NAME";
aLayer.LabelSet.LabelFont = Font("楷体", 10);
aLayer.LabelSet.Offset = 15;
aLayer.AddLabels();
#---- Lambert投影
myApp.ProjectLayers("+proj=lcc+lat_1=25+lat_2=47+lon_0=105")
#---- 按照经纬度范围缩放地图
myApp.ZoomLonLat(78,130,15,53)
#---- 设置屏蔽图层(只绘制中国境内图形)
myApp.SetMaskout("china.shp")
#---- 设置绘图类型为shaded
myApp.SetDrawType("shaded")
#---- 站点数据插值为格点数据的设置
myApp.SetInterpolation(60,140,-20,60,160,160,"IDW_Radius",3,1)
#---- 设置图例文件
#myApp.SetLegendScheme(LegendDir + "rain1.lgs")
vals = Array[Double]([0.1,1,2,5,10,20,25,50,100])
colors = Array.CreateInstance(Color, vals.Length + 1)
colors[0] = Color.White
colors[1] = Color.FromArgb(255,170,240,255)
colors[2] = Color.FromArgb(255,120,230,240)
colors[3] = Color.FromArgb(255,200,220,50)
colors[4] = Color.FromArgb(255,240,220,20)
colors[5] = Color.FromArgb(255,255,120,10)
colors[6] = Color.FromArgb(255,255,90,10)
colors[7] = Color.FromArgb(255,240,40,0)
colors[8] = Color.FromArgb(255,180,10,0)
colors[9] = Color.FromArgb(255,120,10,0)
aLS = LegendManage.CreateGraduatedLegendScheme(vals,colors,ShapeTypes.Polygon,0,1000,False,-9999.0)
aLS.breakList[0].DrawFill = False
myApp.LegendScheme = aLS
myApp.UseDefaultLegendScheme = True
#---- 设置DefalutLayoutMap(图层显示)
myApp.MapLayout.DefaultLayoutMap.DrawGridLine = False
myApp.MapLayout.DefaultLayoutMap.DrawGridLabel = False
myApp.MapLayout.DefaultLayoutMap.DrawNeatLine = False
myApp.MapLayout.DefaultLayoutMap.Left = 10
myApp.MapLayout.DefaultLayoutMap.Top = 10
myApp.MapLayout.DefaultLayoutMap.Width = 620
myApp.MapLayout.DefaultLayoutMap.Height = 450
#---- 设置DefaultIllustration(插图,这里是南海脚图)
myApp.MapLayout.DefaultIllustration.Visible = True
myApp.MapLayout.DefaultIllustration.Left = 40
myApp.MapLayout.DefaultIllustration.Top = 350
myApp.MapLayout.DefaultIllustration.Width = 85
myApp.MapLayout.DefaultIllustration.Height = 109
#---- 设置DefaultTitle(图形标题)
myApp.MapLayout.DefaultTitle.SetFont("黑体", 12)
myApp.MapLayout.DefaultTitle.Top = 40
myApp.MapLayout.DefaultTitle.Left = 80
#---- 设置DefaultLegend(图例)
myApp.MapLayout.DefaultLegend.LegendStyle = LegendStyleEnum.Normal
myApp.MapLayout.DefaultLegend.DrawNeatLine = False
myApp.MapLayout.DefaultLegend.Title = "降水量(毫米)"
myApp.MapLayout.DefaultLegend.BackColor = Color.White
myApp.MapLayout.DefaultLegend.Font = Font("Arial", 8)
myApp.MapLayout.DefaultLegend.Left = 575
myApp.MapLayout.DefaultLegend.Top = 250
#---- 设置时间
sTime = DateTime.Parse("2010-10-14 20:00")
aTime = sTime.AddHours(-6)
#---- 获取站点数据
myApp.RemoveDataLayers()
inFile = "China_Prec_" + sTime.ToString("yyyyMMddHH") + ".csv"
print inFile
#---- 打开文本格式站点数据文件
myApp.OpenLonLatData(DataDir + inFile)
#---- 获取累计降水站点数据
stData = myApp.GetStationData("Precipitation")
#---- 绘制累计降水图层
myApp.Display(stData)
#---- 调整图层顺序(以避免压盖)
myApp.MoveLayerToTop("bou2_4p.shp")
myApp.MoveLayerToTop("bou1_4l.shp")
myApp.MoveLayerToTop("res1_4m.shp")
#---- 设置标题名称
title = " 全国降水量实况图\n" + aTime.ToString("yyyy年MM月dd日HH时") + " - " + sTime.ToString("yyyy年MM月dd日HH时")
myApp.SetTitle(title)
#---- 设置图例名称
myApp.MapLayout.DefaultLegend.Title = "降水量(毫米)"
#---- 绘制图形
myApp.MapLayout.PaintGraphics()
#---- 输出图形为文件
outFile = "Prec_" + aTime.ToString("yyyyMMddHH") + "-" + sTime.ToString("yyyyMMddHH") + ".png"
print outFile
#myApp.SaveFigure(DataDir + outFile)
#---- 显示程序窗体(只是为了看效果,自动运行时不需要)
Application.Run(myApp)
|
|