- 积分
- 127
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2024-9-11
- 最后登录
- 1970-1-1
|
本帖最后由 zhangbote 于 2025-8-27 15:36 编辑
先读json,转成GridData
- public void createFire() {
- FileReader fileReader = new FileReader("D:\\project\\fire\\1.json");
- FireJsonVO fireJsonVO = JSON.parseObject(fileReader.readString(), FireJsonVO.class);
- GridData gridData = FireJsonVO.toGridData(fireJsonVO);
- gridData.setMissingValue(9999.0);
- try {
- fireForecastUtils.createPngByGrid(getPngInfo(), gridData, "D:\\project\\fire\\png\", "1.png", "1", "1");
- } catch (Exception e) {
- log.error("生成png失败!", e);
- }
- }
复制代码 然后根据GridData进行绘图
- public String createPngByGrid(MeteoDataVO info, GridData grid, String outputPath, String outputName, String timeTitle, String publishTitle) throws Exception {
- if (!outputPath.endsWith("/")) {
- outputPath += "/";
- }
- VectorLayer hebeiMap = this.createMapLayer(fileProp.getShpPath() + "hebei(1).shp");
- hebeiMap.setLayerName("jjjMap");
- VectorLayer nameMap = MapDataManage.readMapFile_ShapeFile(fileProp.getShpPath() + "county.shp");
- // 描述地图边界线
- PolygonBreak pb = (PolygonBreak) nameMap.getLegendScheme().getLegendBreak(0);
- // 是否设置填充
- pb.setDrawFill(false);
- pb.setDrawOutline(false);
- // 读取色阶
- LegendScheme als = this.createLegendScheme(info.getLegendValues(), info.getColorStrArr(), grid.getDoubleMissingValue());
- // 绘制图层
- VectorLayer layer = DrawMeteoData.createShadedLayer(grid, als, "", "", false);
- layer.setMaskout(true);
- // 创建视图
- MapView view = new MapView();
- // 叠加图层
- view.addLayer(layer);
- view.addLayer(hebeiMap);
- // 叠加图层
- if (info.getIfZoom()) {
- if (info.getZoomXY() != null && info.getZoomXY().length != 0) {
- view.zoomToExtentLonLatEx(new Extent(info.getZoomXY()[0], info.getZoomXY()[1], info.getZoomXY()[2], info.getZoomXY()[3]));
- }
- }
- // 改变投影
- String projStr = "+datum=WGS84 +a=6378137.0 +rf=298.257223563 +proj=longlat +ellps=WGS84 ";
- // String projStr = "+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +R=6378137 +units=m +no_defs";
- ProjectionInfo aProjInfo = ProjectionInfo.factory(projStr);
- view.projectLayers(aProjInfo);
- // 视图设置
- MapLayout layout = new MapLayout();
- // 获取地图框
- MapFrame frame = layout.getActiveMapFrame();
- frame.setMapView(view);
- view.setAntiAlias(false);
- layout.setAntiAlias(false);
- Extent extent = view.getExtent();
- int rectangleWidth = info.getPngSize() == 0 ? 800 : info.getPngSize();
- int rectangleHeight = (int) (rectangleWidth * 1D / extent.getWidth() * extent.getHeight());
- Rectangle rectangle = new Rectangle(rectangleWidth, rectangleHeight);
- // 设置地图区域大小和外边距
- int width = rectangle.width;
- int height = rectangle.height;
- // 设置页面边界
- int left = 20;
- int right = 20;
- int top = 100;
- int bottom = 100;
- layout.setPageBounds(new Rectangle(0, 0, width + left + right, height + top + bottom));
- MapView mapView = frame.getMapView();
- mapView.getMaskOut().setMask(true);
- mapView.getMaskOut().setMaskLayer(hebeiMap.getLayerName());
- // 图形边框
- frame.setDrawNeatLine(false);
- frame.setDrawGridLabel(false);
- // 设置布局边界
- frame.setLayoutBounds(new Rectangle(left, top, width, height));
- // 绘制网格刻度线
- frame.setDrawGridLine(false);
- // 设置网格间隔值
- frame.setBackColor(new Color(255, 255, 255));
- layout.setPageBackColor(new Color(255, 255, 255));
- this.setLegend(layout, layer, height);
- FileUtils.forceMkdir(new File(outputPath));
- String totalPath = outputPath + outputName;
- // 将字符串路径转换为 Path 对象
- Path path = Paths.get(totalPath);
- if (Files.exists(path)) {
- System.out.println(totalPath + " 文件存在,正在删除...");
- // 删除文件
- Files.delete(path);
- }
- layout.exportToPicture(totalPath);
- this.addBaseInfo(totalPath, timeTitle, publishTitle);
- return totalPath;
- }
复制代码 下面是创建图例的方法- public LegendScheme createLegendScheme(double[] values, String[] colorStrArr, double missValue) {
- // 创建图例
- Color[] colors = Arrays.stream(colorStrArr)
- .map(str -> Color.decode(String.valueOf(Integer.parseInt(str, 16))))
- .toArray(Color[]::new);
- List<Color> newList = new ArrayList<>();
- for (Color color : colors) {
- if (color.getRed() == 255 && color.getGreen() == 255 && color.getBlue() == 255) {
- newList.add(new Color(color.getRed(), color.getGreen(), color.getBlue(), 0));
- } else {
- newList.add(new Color(color.getRed(), color.getGreen(), color.getBlue()));
- }
- }
- Color[] newColor = newList.toArray(new Color[0]);
- double minData = values[0];
- double maxData = values[values.length - 1];
- // 渐变值
- return LegendManage.createGraduatedLegendScheme(values, newColor, ShapeTypes.POLYGON, minData, maxData, false, missValue);
- }
复制代码
|
|