爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 5428|回复: 7

MeteoInfo脚本教程(十三):LegendScheme

[复制链接]

新浪微博达人勋

发表于 2014-12-7 20:45:36 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

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):
  1. #-----------------------------------------------------
  2. # Author: Yaqiang Wang
  3. # Date: 2014-12-7
  4. # Purpose: Set single symbol legend scheme
  5. # Note: Sample
  6. #-----------------------------------------------------
  7. #---- Import classes
  8. print 'Import classes...'
  9. from org.meteoinfo.layout import MapLayout
  10. from org.meteoinfo.data.mapdata import MapDataManage
  11. from org.meteoinfo.data.meteodata import MeteoDataInfo, DrawMeteoData
  12. from org.meteoinfo.layout import LegendStyles
  13. from org.meteoinfo.legend import LegendScheme, LegendType, PointBreak
  14. from org.meteoinfo.drawing import PointStyle
  15. from org.meteoinfo.shape import ShapeTypes
  16. from org.meteoinfo.global import Extent
  17. import os
  18. from java.awt import Color
  19. from java.awt import Font
  20. from javax.swing import JFrame

  21. #---- Set directories
  22. print 'Set directories...'
  23. baseDir = 'D:/MyProgram/Distribution/java/MeteoInfo/MeteoInfo'
  24. dataDir = os.path.join(baseDir, 'sample')
  25. mapDir = os.path.join(baseDir, 'map')

  26. #---- Create MapLayout object
  27. mapLayout = MapLayout()
  28. mapFrame = mapLayout.getActiveMapFrame()

  29. #---- Load country layer
  30. print 'Load country layer...'
  31. countryLayer = MapDataManage.loadLayer(os.path.join(mapDir, 'country1.shp'))
  32. lb = countryLayer.getLegendScheme().getLegendBreaks().get(0)
  33. lb.setDrawFill(False)
  34. lb.setOutlineColor(Color.blue)
  35. mapFrame.addLayer(countryLayer)

  36. #---- Create MeteoDataInfo object
  37. mdi = MeteoDataInfo()

  38. #---- Open a MICAPS data file
  39. fn = os.path.join(dataDir, 'MICAPS/10101414.000')
  40. mdi.openMICAPSData(fn)

  41. #---- Get station info data
  42. sidata = mdi.getStationInfoData()

  43. #---- Create station info layer from the station info data
  44. ls = LegendScheme(ShapeTypes.Point)
  45. ls.setLegendType(LegendType.SingleSymbol)
  46. lbs = ls.getLegendBreaks()
  47. lb = PointBreak()
  48. lb.setSize(16)
  49. lb.setColor(Color.blue)
  50. lb.setOutlineColor(Color.red)
  51. lb.setStyle(PointStyle.Star)
  52. lbs.add(lb)

  53. layer = DrawMeteoData.createSTInfoLayer(sidata, ls, 'StationInfo')
  54. layer.setAvoidCollision(True)

  55. #---- Add layer
  56. mapFrame.addLayer(layer)
  57. mapFrame.moveLayer(layer, 0)

  58. #---- Add title
  59. title = mapLayout.addText('MeteoInfo script demo', 350, 30, 'Arial', 16)

  60. #---- Set layout map
  61. print 'Set layout map...'
  62. mapLayout.getActiveLayoutMap().setWidth(580)
  63. mapLayout.getActiveLayoutMap().zoomToExtentLonLatEx(Extent(70, 140, 15, 55))

  64. #---- Set mapframe
  65. mapFrame.setGridXDelt(10)
  66. mapFrame.setGridYDelt(10)

  67. #---- Add legend
  68. legend = mapLayout.addLegend(620, 150)
  69. legend.setLegendStyle(LegendStyles.Normal)
  70. legend.setLegendLayer(layer)
  71. legend.setTitle('Station')

  72. frame = JFrame('MeteoInfo Script Sample', size = (800, 600))
  73. frame.add(mapLayout)
  74. frame.visible = True
  75. print 'Finished!'

Image00082.png

示例脚本(ShapeTypes.Polyline、LegendType.UniqueValue、PolylineBreak):
  1. #-----------------------------------------------------
  2. # Author: Yaqiang Wang
  3. # Date: 2014-12-7
  4. # Purpose: Set unique value legend scheme
  5. # Note: Sample
  6. #-----------------------------------------------------
  7. #---- Import classes
  8. print 'Import classes...'
  9. from org.meteoinfo.layout import MapLayout
  10. from org.meteoinfo.data.mapdata import MapDataManage
  11. from org.meteoinfo.data.meteodata import MeteoDataInfo, DrawMeteoData
  12. from org.meteoinfo.legend import LegendManage, LegendType, LegendScheme, PolylineBreak
  13. from org.meteoinfo.layout import LegendStyles
  14. from org.meteoinfo.shape import ShapeTypes
  15. from org.meteoinfo.global import Extent
  16. import os.path
  17. from java.awt import Color
  18. from javax.swing import JFrame

  19. #---- Set directories
  20. print 'Set directories...'
  21. baseDir = 'D:/MyProgram/Distribution/java/MeteoInfo/MeteoInfo'
  22. dataDir = os.path.join(baseDir, 'sample/GrADS')
  23. mapDir = os.path.join(baseDir, 'map')
  24. figDir = 'D:/Temp/test'

  25. #---- Create MapLayout object
  26. mapLayout = MapLayout()
  27. mapFrame = mapLayout.getActiveMapFrame()

  28. #---- Load country layer
  29. print 'Load country layer...'
  30. countryLayer = MapDataManage.loadLayer(os.path.join(mapDir, 'country1.shp'))
  31. lb = countryLayer.getLegendScheme().getLegendBreaks().get(0)
  32. lb.setDrawFill(False)
  33. lb.setOutlineColor(Color.gray)
  34. mapFrame.addLayer(countryLayer)

  35. #---- Open GrADS data
  36. print 'Open GrADS data...'
  37. mdi = MeteoDataInfo()
  38. mdi.openGrADSData(os.path.join(dataDir, 'model.ctl'))

  39. #---- Set time index
  40. mdi.setTimeIndex(2)

  41. #---- Get pressure grid data
  42. gdata = mdi.getGridData('PS')
  43. gdata.extendToGlobal()

  44. #---- Create layer
  45. print 'Create layer...'
  46. ls = LegendScheme(ShapeTypes.Polyline)
  47. ls.setLegendType(LegendType.UniqueValue)
  48. values = [500, 550, 600, 650, 700, 750, 800, 850, 900, 950,1000]
  49. colors = [Color(110,0,220),Color(30,60,255),Color(0,160,255),Color(0,200,200),Color(0,220,0),
  50.   Color(160,230,50),Color(230,220,50),Color(230,175,45),Color(240,130,40),Color(250,60,60),Color(240,0,130)]
  51. lbs = ls.getLegendBreaks()
  52. for ii in range(0, len(colors)):
  53.   lb = PolylineBreak()
  54.   lb.setColor(colors[ii])
  55.   lb.setStartValue(values[ii])
  56.   lb.setEndValue(values[ii])
  57.   lb.setCaption(str(values[ii]))
  58.   lbs.add(lb)

  59. layer = DrawMeteoData.createContourLayer(gdata, ls, 'Pressure_contour', 'PS', True)

  60. #---- Add layer
  61. mapFrame.addLayer(layer)
  62. mapFrame.moveLayer(layer, 0)

  63. #---- Add title
  64. title = mapLayout.addText('MeteoInfo script demo', 350, 30, 'Arial', 16)

  65. #---- Zoom layout map
  66. print 'Zoom layout map...'
  67. mapLayout.getActiveLayoutMap().zoomToExtentLonLatEx(Extent(0, 360, -90, 90))

  68. #---- Set mapframe
  69. mapFrame.setGridXDelt(30)
  70. mapFrame.setGridYDelt(30)

  71. #---- Add legend
  72. legend = mapLayout.addLegend(150, 440)
  73. legend.setLegendStyle(LegendStyles.Bar_Horizontal)
  74. legend.setLegendLayer(layer)

  75. frame = JFrame('MeteoInfo Script Sample', size = (750, 540))
  76. frame.add(mapLayout)
  77. frame.visible = True
  78. print 'Finished!'

Image00083.png

示例脚本(ShapeTypes.Polygon、LegendType.GraduatedColor、PolygonBreak):
  1. #-----------------------------------------------------
  2. # Author: Yaqiang Wang
  3. # Date: 2014-12-7
  4. # Purpose: Set graduated color legend scheme
  5. # Note: Sample
  6. #-----------------------------------------------------
  7. #---- Import classes
  8. print 'Import classes...'
  9. from org.meteoinfo.layout import MapLayout
  10. from org.meteoinfo.data.mapdata import MapDataManage
  11. from org.meteoinfo.data.meteodata import MeteoDataInfo, DrawMeteoData
  12. from org.meteoinfo.legend import LegendManage, LegendType, LegendScheme, PolygonBreak
  13. from org.meteoinfo.layout import LegendStyles
  14. from org.meteoinfo.shape import ShapeTypes
  15. from org.meteoinfo.global import Extent
  16. import os.path
  17. from java.awt import Color
  18. from javax.swing import JFrame

  19. #---- Set directories
  20. print 'Set directories...'
  21. baseDir = 'D:/MyProgram/Distribution/java/MeteoInfo/MeteoInfo'
  22. dataDir = os.path.join(baseDir, 'sample/GrADS')
  23. mapDir = os.path.join(baseDir, 'map')
  24. figDir = 'D:/Temp/test'

  25. #---- Create MapLayout object
  26. mapLayout = MapLayout()
  27. mapFrame = mapLayout.getActiveMapFrame()

  28. #---- Load country layer
  29. print 'Load country layer...'
  30. countryLayer = MapDataManage.loadLayer(os.path.join(mapDir, 'country1.shp'))
  31. lb = countryLayer.getLegendScheme().getLegendBreaks().get(0)
  32. lb.setDrawFill(False)
  33. lb.setOutlineColor(Color.gray)
  34. mapFrame.addLayer(countryLayer)

  35. #---- Open GrADS data
  36. print 'Open GrADS data...'
  37. mdi = MeteoDataInfo()
  38. mdi.openGrADSData(os.path.join(dataDir, 'model.ctl'))

  39. #---- Set time index
  40. mdi.setTimeIndex(2)

  41. #---- Get pressure grid data
  42. gdata = mdi.getGridData('PS')
  43. gdata.extendToGlobal()

  44. #---- Create layer
  45. print 'Create layer...'
  46. ls = LegendScheme(ShapeTypes.Polygon)
  47. ls.setLegendType(LegendType.GraduatedColor)
  48. values = [450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950,1000, 1050]
  49. 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),
  50.   Color(160,230,50),Color(230,220,50),Color(230,175,45),Color(240,130,40),Color(250,60,60),Color(240,0,130)]
  51. lbs = ls.getLegendBreaks()
  52. for ii in range(0, len(colors)):
  53.   lb = PolygonBreak()
  54.   lb.setColor(colors[ii])
  55.   lb.setStartValue(values)
  56.   lb.setEndValue(values[ii + 1])
  57.   lb.setCaption(str(values) + ' ' + str(values[ii + 1]))
  58.   lb.setDrawOutline(False)
  59.   lbs.add(lb)

  60. layer = DrawMeteoData.createShadedLayer(gdata, ls, 'Pressure_Shaded', 'PS', True)

  61. #---- Add layer
  62. mapFrame.addLayer(layer)
  63. mapFrame.moveLayer(layer, 0)

  64. #---- Add title
  65. title = mapLayout.addText('MeteoInfo script demo', 350, 30, 'Arial', 16)

  66. #---- Zoom layout map
  67. print 'Zoom layout map...'
  68. mapLayout.getActiveLayoutMap().zoomToExtentLonLatEx(Extent(0, 360, -90, 90))

  69. #---- Set mapframe
  70. mapFrame.setGridXDelt(30)
  71. mapFrame.setGridYDelt(30)

  72. #---- Add legend
  73. legend = mapLayout.addLegend(150, 440)
  74. legend.setLegendStyle(LegendStyles.Bar_Horizontal)
  75. legend.setLegendLayer(layer)

  76. frame = JFrame('MeteoInfo Script Sample', size = (750, 540))
  77. frame.add(mapLayout)
  78. frame.visible = True
  79. print 'Finished!'

Image00084.png

LegendScheme可以在MeteoInfo桌面软件中输出为图例文件(.lgs),通过在脚本中加载图例文件来完成LegendScheme的设置,可以少写很多代码。
  1. ls = LegendScheme(ShapeTypes.Polygon)
  2. fn = 'D:/Temp/rain.lgs'
  3. ls.importFromXMLFile(fn)


LegendScheme还可以通过MeteoInfo软件路径中的colormaps目录下的.rgb文件来加载。比如:
  1. ls = LegendManage.createLegendScheme(gdata, 12, 'NCV_jet')

LegendManage.createLegendScheme方法中的第一个参数是格点数据对象,第二个参数是图例分级数目(比如12),第三个参数是.rgb文件名(不带后缀)。
Image00793.png

本帖被以下淘专辑推荐:

密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2014-12-8 06:44:58 | 显示全部楼层
老师太牛了
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2014-12-8 23:13:31 | 显示全部楼层
课程太多,学不过来了,哈哈!
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2015-2-24 16:37:04 | 显示全部楼层
学习了好久,才找到这些好东西
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2015-2-24 16:38:17 | 显示全部楼层
王老师有个问题,就是脚本程序怎么应用到C#的二次开发中,因为好多工作需要二次开发,
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2017-4-27 11:19:02 | 显示全部楼层
{:eb301:}{:eb502:}
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

发表于 2018-1-21 22:56:58 | 显示全部楼层
for i in range(0, len(colors)):
  lb = PolylineBreak()
  lb.setColor(colors<i>)
  </i>lb.setStartValue(values)
  lb.setEndValue(values)
  lb.setCaption(str(values))
  lbs.add(lb)

示例2和3都是在这几条语句有问题,我用的版本是1.14R10,出错信息
Traceback (most recent call last):
  File "<iostream>", line 61, in <module>
TypeError: setColor(): 1st arg can't be coerced to java.awt.Color
老师能不能告诉一下解决方法?
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2018-1-21 23:42:47 | 显示全部楼层
zhksoft 发表于 2018-1-21 22:56
for i in range(0, len(colors)):
  lb = PolylineBreak()
  lb.setColor(colors)

更新了1楼的脚本
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表