- 积分
- 701
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2017-5-6
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 ertian 于 2018-7-5 11:19 编辑
王老师您好,我在使用MeteoinfoLib java类库做等值面图生成开发时,因为涉及到“批量出图且出图速度要求尽量快”的需求,所以采用了多线程调用类库来生成图片,但是发现类库在多线程下运行不正常,会出现图片之间的图形元素错乱。我写了一个用类库来执行多线程的java类,已通过附件的形式提供,,烦请王老师帮忙看下。
多线程下目前发现有两处问题:
1.各线程之间图例叠加出错,比如线程1的图上本应该叠加线程1的图例,但实际上很大概率会叠加到其它线程的图例。
2.maskOut对图做处理时,会一定几率抛出Exception in thread "线程0" java.util.ConcurrentModificationException异常,导致此线程中止。
java代码类- package cc.htdf.meteoinfo.temp;
- import org.meteoinfo.data.GridData;
- import org.meteoinfo.data.StationData;
- import org.meteoinfo.data.mapdata.MapDataManage;
- import org.meteoinfo.data.meteodata.DrawMeteoData;
- import org.meteoinfo.data.meteodata.GridDataSetting;
- import org.meteoinfo.geoprocess.analysis.InterpolationMethods;
- import org.meteoinfo.geoprocess.analysis.InterpolationSetting;
- import org.meteoinfo.global.Extent;
- import org.meteoinfo.layer.VectorLayer;
- import org.meteoinfo.layout.*;
- import org.meteoinfo.legend.MapFrame;
- import org.meteoinfo.legend.PolygonBreak;
- import org.meteoinfo.map.MapView;
- import javax.print.PrintException;
- import java.awt.*;
- import java.io.File;
- import java.io.IOException;
- import java.util.Random;
- import java.util.UUID;
- /**
- * meteoinfo测试类
- *
- * @date 2018/7/4
- */
- public class TestMi implements Runnable {
- private void threadMi(VectorLayer shapeLayer, StationData stationData) throws IOException, PrintException {
- String threadName = Thread.currentThread().getName();
- System.out.println("初始化!" + threadName);
- //图片分辨率
- int[] imageSize = {775, 555};
- //画布大小
- int[] layoutSize = {595, 410};
- //画布偏移
- int[] layoutOffset = {20, 105};
- //创建画图
- //初始化
- MapLayout mapLayout = new MapLayout();
- mapLayout.setBackground(new Color(255, 255, 255, 0));
- mapLayout.setFont(new Font("微软雅黑", Font.BOLD, 12));
- mapLayout.getLayoutMaps().get(0);
- //图片分辨率
- mapLayout.setSize(imageSize[0], imageSize[1]);
- mapLayout.setPageBounds(new Rectangle(0, 0, imageSize[0], imageSize[1]));
- mapLayout.getActiveLayoutMap().setLeft(layoutOffset[0]);
- mapLayout.getActiveLayoutMap().setTop(layoutOffset[1]);
- mapLayout.getActiveLayoutMap().setWidth(layoutSize[0]);
- mapLayout.getActiveLayoutMap().setHeight(layoutSize[1]);
- LayoutMap layoutMap = mapLayout.getActiveLayoutMap();
- layoutMap.setDrawGridLine(false);
- layoutMap.setDrawNeatLine(false);
- layoutMap.setDrawGridLabel(false);
- layoutMap.setDrawGridTickLine(false);
- //创建mapView对象
- MapFrame mapFrame = mapLayout.getActiveMapFrame();
- MapView mapView = mapFrame.getMapView();
- //格点配置
- GridDataSetting gridDataSetting = new GridDataSetting();
- gridDataSetting.dataExtent = shapeLayer.getExtent();
- gridDataSetting.xNum = 100;
- gridDataSetting.yNum = 100;
- //站点插值配置
- InterpolationSetting interpolationSetting = new InterpolationSetting();
- interpolationSetting.setInterpolationMethod(InterpolationMethods.IDW_Neighbors);
- interpolationSetting.setMinPointNum(3);
- interpolationSetting.setGridDataSetting(gridDataSetting);
- //站点插值成格点数据
- GridData gridData = stationData.interpolateData(interpolationSetting);
- //等值面分析
- VectorLayer shadedLayer = DrawMeteoData.createShadedLayer(gridData, "图例(" + threadName + ")", "Data", true);
- //图例配置
- LayoutLegend aLegend = mapLayout.addLegend(640, 0);
- aLegend.setLegendLayer(shadedLayer);
- aLegend.setFont(new Font("宋体", Font.BOLD, 12));
- aLegend.setLegendStyle(LegendStyles.Normal);
- aLegend.setTop(mapLayout.getHeight() - aLegend.getHeight() - 30);
- //裁剪
- PolygonBreak aPGB = (PolygonBreak) shapeLayer.getLegendScheme().getLegendBreaks().get(0);
- aPGB.setDrawFill(false);
- aPGB.setOutlineColor(new Color(128, 128, 128));
- aPGB.setOutlineSize(2);
- mapView.addLayer(shapeLayer);
- mapView.addLayer(shadedLayer);
- //开启//加入掩膜
- shadedLayer.setMaskout(true);
- mapView.getMaskOut().setMask(true);
- mapView.getMaskOut().setMaskLayer(shapeLayer.getLayerName());
- mapView.zoomToExtent(shapeLayer.getExtent());
- //加入标题
- LayoutGraphic lTitle = mapLayout.addText("等值面(" + threadName + ")", mapLayout.getWidth() / 2, 40, 40);
- lTitle.setTop(15);
- //刷新配置
- mapView.paintLayers();
- mapView.repaint();
- //输出图片
- String outPutFileUrl = "E:\\OUT_PUT\" + UUID.randomUUID().toString().substring(0, 7) + "_" + threadName + ".png";
- //建立父目录
- new File(outPutFileUrl).mkdirs();
- mapLayout.exportToPicture(outPutFileUrl);
- System.out.println("生成成功!" + Thread.currentThread().getName());
- }
- @Override
- public void run() {
- //任意导入一个省份的轮廓shape图层
- String shapeUrl = "E:\\shp\\河南省_province.shp";
- //获取数据
- VectorLayer shapeLayer = null;
- try {
- shapeLayer = MapDataManage.readMapFile_ShapeFile(shapeUrl);
- } catch (Exception e) {
- e.printStackTrace();
- }
- if (shapeLayer == null) {
- System.out.println("shape读取出错!");
- return;
- }
- //经纬度范围
- Extent extent = shapeLayer.getExtent();
- //随机站点数据
- int sNum = 100;
- StationData stationData = new StationData();
- for (int i = 0; i < sNum; i++) {
- double lon = new Random().nextDouble() * (extent.maxX - extent.minX) + extent.minX;
- double lat = new Random().nextDouble() * (extent.maxY - extent.minY) + extent.minY;
- double value = new Random().nextInt(70) + 15;
- stationData.addData("", "", lon, lat, value);
- }
- //生成图形
- try {
- threadMi(shapeLayer, stationData);
- } catch (IOException | PrintException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) throws Exception {
- int threadNum = 8;
- TestMi testMi = new TestMi();
- for (int i = 0; i < threadNum; i++) {
- Thread thread = new Thread(testMi);
- thread.setName("线程" + i);
- thread.start();
- Thread.sleep(2000);
- }
- }
- }
复制代码
|
|