- 积分
- 55954
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-6-21
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 MeteoInfo 于 2018-1-21 23:42 编辑
MeteoInfo中图层是根据其LegendScheme对象来绘制的,是MeteoInfo绘图中非常重要的内容。LegendScheme类创建时需要指定图元类型:ShapeTypes.Point(点)、ShpeTypes.Polyline(线)、ShapeTypes.Polygon(面)等,还需要设定LegendScheme类的图例类型(setLegendType(LegendType lt)方法),主要有:LegendType.SingleSymbol(单个图例)、LegendType.UniqueValue(不同属性值用不同图例)、LegendType.GradatedColor(不同区间的属性值用不同的图例)。后两者都要用到图层的某个属性字段中的值来确定每个图元用什么图例来绘制,需要设置LegendScheme中的字段名(用setFieldName(String fieldName)方法)。LegendScheme中包含了一个图例列表,用getLegendBreaks()方法获取。图例的基类是ColorBreak,根据图元类型不同,继承自ColorBreak的图例类包括:PointBreak、PolylineBreak、PolygonBreak等。看起来比较复杂,但是可以做出非常丰富的图形。
之前用到DrawMeteoData类中生成各类图层的方法中如果没有LegendScheme参数,会根据格点或者站点数据自动生成彩虹色的图例。对于生成等值线和等值线填色图层来说,如果自动生成的图例不满足要求,必须在生成图层时给出自定义的LegendScheme,因为等值线追踪时需要其中的一些设置。其它类型图层可以在图层生成后再修改LegendScheme,当然也可以在生成图层时指定LegendScheme。
示例脚本(ShapeTypes.Point、LegendType.SingleSymbol、PointBreak):
- #-----------------------------------------------------
- # Author: Yaqiang Wang
- # Date: 2014-12-7
- # Purpose: Set single symbol legend scheme
- # Note: Sample
- #-----------------------------------------------------
- #---- Import classes
- print 'Import classes...'
- from org.meteoinfo.layout import MapLayout
- from org.meteoinfo.data.mapdata import MapDataManage
- from org.meteoinfo.data.meteodata import MeteoDataInfo, DrawMeteoData
- from org.meteoinfo.layout import LegendStyles
- from org.meteoinfo.legend import LegendScheme, LegendType, PointBreak
- from org.meteoinfo.drawing import PointStyle
- from org.meteoinfo.shape import ShapeTypes
- from org.meteoinfo.global import Extent
- import os
- from java.awt import Color
- from java.awt import Font
- from javax.swing import JFrame
- #---- Set directories
- print 'Set directories...'
- baseDir = 'D:/MyProgram/Distribution/java/MeteoInfo/MeteoInfo'
- dataDir = os.path.join(baseDir, 'sample')
- mapDir = os.path.join(baseDir, 'map')
- #---- Create MapLayout object
- mapLayout = MapLayout()
- mapFrame = mapLayout.getActiveMapFrame()
- #---- Load country layer
- print 'Load country layer...'
- countryLayer = MapDataManage.loadLayer(os.path.join(mapDir, 'country1.shp'))
- lb = countryLayer.getLegendScheme().getLegendBreaks().get(0)
- lb.setDrawFill(False)
- lb.setOutlineColor(Color.blue)
- mapFrame.addLayer(countryLayer)
- #---- Create MeteoDataInfo object
- mdi = MeteoDataInfo()
- #---- Open a MICAPS data file
- fn = os.path.join(dataDir, 'MICAPS/10101414.000')
- mdi.openMICAPSData(fn)
- #---- Get station info data
- sidata = mdi.getStationInfoData()
- #---- Create station info layer from the station info data
- ls = LegendScheme(ShapeTypes.Point)
- ls.setLegendType(LegendType.SingleSymbol)
- lbs = ls.getLegendBreaks()
- lb = PointBreak()
- lb.setSize(16)
- lb.setColor(Color.blue)
- lb.setOutlineColor(Color.red)
- lb.setStyle(PointStyle.Star)
- lbs.add(lb)
- layer = DrawMeteoData.createSTInfoLayer(sidata, ls, 'StationInfo')
- layer.setAvoidCollision(True)
- #---- Add layer
- mapFrame.addLayer(layer)
- mapFrame.moveLayer(layer, 0)
- #---- Add title
- title = mapLayout.addText('MeteoInfo script demo', 350, 30, 'Arial', 16)
- #---- Set layout map
- print 'Set layout map...'
- mapLayout.getActiveLayoutMap().setWidth(580)
- mapLayout.getActiveLayoutMap().zoomToExtentLonLatEx(Extent(70, 140, 15, 55))
- #---- Set mapframe
- mapFrame.setGridXDelt(10)
- mapFrame.setGridYDelt(10)
- #---- Add legend
- legend = mapLayout.addLegend(620, 150)
- legend.setLegendStyle(LegendStyles.Normal)
- legend.setLegendLayer(layer)
- legend.setTitle('Station')
- frame = JFrame('MeteoInfo Script Sample', size = (800, 600))
- frame.add(mapLayout)
- frame.visible = True
- print 'Finished!'
示例脚本(ShapeTypes.Polyline、LegendType.UniqueValue、PolylineBreak):
- #-----------------------------------------------------
- # Author: Yaqiang Wang
- # Date: 2014-12-7
- # Purpose: Set unique value legend scheme
- # Note: Sample
- #-----------------------------------------------------
- #---- Import classes
- print 'Import classes...'
- from org.meteoinfo.layout import MapLayout
- from org.meteoinfo.data.mapdata import MapDataManage
- from org.meteoinfo.data.meteodata import MeteoDataInfo, DrawMeteoData
- from org.meteoinfo.legend import LegendManage, LegendType, LegendScheme, PolylineBreak
- from org.meteoinfo.layout import LegendStyles
- from org.meteoinfo.shape import ShapeTypes
- from org.meteoinfo.global import Extent
- import os.path
- from java.awt import Color
- from javax.swing import JFrame
- #---- Set directories
- print 'Set directories...'
- baseDir = 'D:/MyProgram/Distribution/java/MeteoInfo/MeteoInfo'
- dataDir = os.path.join(baseDir, 'sample/GrADS')
- mapDir = os.path.join(baseDir, 'map')
- figDir = 'D:/Temp/test'
- #---- Create MapLayout object
- mapLayout = MapLayout()
- mapFrame = mapLayout.getActiveMapFrame()
- #---- Load country layer
- print 'Load country layer...'
- countryLayer = MapDataManage.loadLayer(os.path.join(mapDir, 'country1.shp'))
- lb = countryLayer.getLegendScheme().getLegendBreaks().get(0)
- lb.setDrawFill(False)
- lb.setOutlineColor(Color.gray)
- mapFrame.addLayer(countryLayer)
- #---- Open GrADS data
- print 'Open GrADS data...'
- mdi = MeteoDataInfo()
- mdi.openGrADSData(os.path.join(dataDir, 'model.ctl'))
- #---- Set time index
- mdi.setTimeIndex(2)
- #---- Get pressure grid data
- gdata = mdi.getGridData('PS')
- gdata.extendToGlobal()
- #---- Create layer
- print 'Create layer...'
- ls = LegendScheme(ShapeTypes.Polyline)
- ls.setLegendType(LegendType.UniqueValue)
- values = [500, 550, 600, 650, 700, 750, 800, 850, 900, 950,1000]
- colors = [Color(110,0,220),Color(30,60,255),Color(0,160,255),Color(0,200,200),Color(0,220,0),
- Color(160,230,50),Color(230,220,50),Color(230,175,45),Color(240,130,40),Color(250,60,60),Color(240,0,130)]
- lbs = ls.getLegendBreaks()
- for ii in range(0, len(colors)):
- lb = PolylineBreak()
- lb.setColor(colors[ii])
- lb.setStartValue(values[ii])
- lb.setEndValue(values[ii])
- lb.setCaption(str(values[ii]))
- lbs.add(lb)
- layer = DrawMeteoData.createContourLayer(gdata, ls, 'Pressure_contour', 'PS', True)
- #---- Add layer
- mapFrame.addLayer(layer)
- mapFrame.moveLayer(layer, 0)
- #---- Add title
- title = mapLayout.addText('MeteoInfo script demo', 350, 30, 'Arial', 16)
- #---- Zoom layout map
- print 'Zoom layout map...'
- mapLayout.getActiveLayoutMap().zoomToExtentLonLatEx(Extent(0, 360, -90, 90))
- #---- Set mapframe
- mapFrame.setGridXDelt(30)
- mapFrame.setGridYDelt(30)
- #---- Add legend
- legend = mapLayout.addLegend(150, 440)
- legend.setLegendStyle(LegendStyles.Bar_Horizontal)
- legend.setLegendLayer(layer)
- frame = JFrame('MeteoInfo Script Sample', size = (750, 540))
- frame.add(mapLayout)
- frame.visible = True
- print 'Finished!'
示例脚本(ShapeTypes.Polygon、LegendType.GraduatedColor、PolygonBreak):
- #-----------------------------------------------------
- # Author: Yaqiang Wang
- # Date: 2014-12-7
- # Purpose: Set graduated color legend scheme
- # Note: Sample
- #-----------------------------------------------------
- #---- Import classes
- print 'Import classes...'
- from org.meteoinfo.layout import MapLayout
- from org.meteoinfo.data.mapdata import MapDataManage
- from org.meteoinfo.data.meteodata import MeteoDataInfo, DrawMeteoData
- from org.meteoinfo.legend import LegendManage, LegendType, LegendScheme, PolygonBreak
- from org.meteoinfo.layout import LegendStyles
- from org.meteoinfo.shape import ShapeTypes
- from org.meteoinfo.global import Extent
- import os.path
- from java.awt import Color
- from javax.swing import JFrame
- #---- Set directories
- print 'Set directories...'
- baseDir = 'D:/MyProgram/Distribution/java/MeteoInfo/MeteoInfo'
- dataDir = os.path.join(baseDir, 'sample/GrADS')
- mapDir = os.path.join(baseDir, 'map')
- figDir = 'D:/Temp/test'
- #---- Create MapLayout object
- mapLayout = MapLayout()
- mapFrame = mapLayout.getActiveMapFrame()
- #---- Load country layer
- print 'Load country layer...'
- countryLayer = MapDataManage.loadLayer(os.path.join(mapDir, 'country1.shp'))
- lb = countryLayer.getLegendScheme().getLegendBreaks().get(0)
- lb.setDrawFill(False)
- lb.setOutlineColor(Color.gray)
- mapFrame.addLayer(countryLayer)
- #---- Open GrADS data
- print 'Open GrADS data...'
- mdi = MeteoDataInfo()
- mdi.openGrADSData(os.path.join(dataDir, 'model.ctl'))
- #---- Set time index
- mdi.setTimeIndex(2)
- #---- Get pressure grid data
- gdata = mdi.getGridData('PS')
- gdata.extendToGlobal()
- #---- Create layer
- print 'Create layer...'
- ls = LegendScheme(ShapeTypes.Polygon)
- ls.setLegendType(LegendType.GraduatedColor)
- values = [450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950,1000, 1050]
- colors = [Color(160,0,200),Color(110,0,220),Color(30,60,255),Color(0,160,255),Color(0,200,200),Color(0,220,0),
- Color(160,230,50),Color(230,220,50),Color(230,175,45),Color(240,130,40),Color(250,60,60),Color(240,0,130)]
- lbs = ls.getLegendBreaks()
- for ii in range(0, len(colors)):
- lb = PolygonBreak()
- lb.setColor(colors[ii])
- lb.setStartValue(values)
- lb.setEndValue(values[ii + 1])
- lb.setCaption(str(values) + ' ' + str(values[ii + 1]))
- lb.setDrawOutline(False)
- lbs.add(lb)
- layer = DrawMeteoData.createShadedLayer(gdata, ls, 'Pressure_Shaded', 'PS', True)
- #---- Add layer
- mapFrame.addLayer(layer)
- mapFrame.moveLayer(layer, 0)
- #---- Add title
- title = mapLayout.addText('MeteoInfo script demo', 350, 30, 'Arial', 16)
- #---- Zoom layout map
- print 'Zoom layout map...'
- mapLayout.getActiveLayoutMap().zoomToExtentLonLatEx(Extent(0, 360, -90, 90))
- #---- Set mapframe
- mapFrame.setGridXDelt(30)
- mapFrame.setGridYDelt(30)
- #---- Add legend
- legend = mapLayout.addLegend(150, 440)
- legend.setLegendStyle(LegendStyles.Bar_Horizontal)
- legend.setLegendLayer(layer)
- frame = JFrame('MeteoInfo Script Sample', size = (750, 540))
- frame.add(mapLayout)
- frame.visible = True
- print 'Finished!'
LegendScheme可以在MeteoInfo桌面软件中输出为图例文件(.lgs),通过在脚本中加载图例文件来完成LegendScheme的设置,可以少写很多代码。
- ls = LegendScheme(ShapeTypes.Polygon)
- fn = 'D:/Temp/rain.lgs'
- ls.importFromXMLFile(fn)
LegendScheme还可以通过MeteoInfo软件路径中的colormaps目录下的.rgb文件来加载。比如:
- ls = LegendManage.createLegendScheme(gdata, 12, 'NCV_jet')
LegendManage.createLegendScheme方法中的第一个参数是格点数据对象,第二个参数是图例分级数目(比如12),第三个参数是.rgb文件名(不带后缀)。
|
|