- 积分
- 127
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2024-9-11
- 最后登录
- 1970-1-1
|
本帖最后由 zhangbote 于 2025-8-25 10:15 编辑
数据不太好分享是多个1公里的格点数据累加的结果。我想了解一下,使用格点数据绘图的时候,是否也会内置先插值一下?完整的绘图Util的代码如下- @Component
- @AllArgsConstructor
- public class RollRainMeteoUtils {
- private LocalFileProp fileProp;
- 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);
- checkCityName(hebeiMap);
- checkCountyName(nameMap);
- // 读取色阶
- 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);
- view.addLayer(nameMap);
- // 叠加图层
- 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;
- }
- private void addBaseInfo(String pngPath, String timeTitle, String publishTitle) throws Exception {
- // 读取原始的PNG图片
- File file = new File(pngPath);
- BufferedImage image = ImageIO.read(file);
- int width = image.getWidth();
- int height = image.getHeight();
- // 创建Graphics2D对象
- Graphics2D g2d = image.createGraphics();
- // 启用抗锯齿
- g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- this.addCenterContent(g2d, "河北省降水量预报", Color.BLACK,
- getChineseFont(30),
- width, 40);
- this.addCenterContent(g2d, timeTitle, Color.BLACK,
- getChineseFont(20),
- width, 70);
- this.addContent(g2d, "图例(单位:毫米)", Color.BLACK,
- getChineseFont(15), 470, 750);
- this.addContent(g2d, "雨", Color.BLACK,
- getChineseFont(15), 470, 795);
- this.addContent(g2d, "雨夹雪或", Color.BLACK,
- getChineseFont(15), 590, 780);
- this.addContent(g2d, "雨转雪", Color.BLACK,
- getChineseFont(15), 590, 795);
- this.addContent(g2d, "雪", Color.BLACK,
- getChineseFont(15), 710, 795);
- this.addContent(g2d, publishTitle, Color.BLACK,
- getChineseFont(17), 520, 960);
- // 释放Graphics2D对象
- g2d.dispose();
- BufferedImage newImage = this.addLegend(image, LegendUtil.createRainSnowLegend());
- // 保存新的图片到文件
- File outputfile = new File(pngPath);
- File parentFile = outputfile.getParentFile();
- if (!parentFile.exists()) {
- parentFile.mkdirs();
- }
- if (outputfile.exists()) {
- outputfile.delete();
- }
- ImageIO.write(newImage, "png", outputfile);
- }
- /**
- * 添加图例
- * @param baseImage 底图
- * @param legendImage 图例
- * @return
- */
- private BufferedImage addLegend(BufferedImage baseImage, BufferedImage legendImage) {
- int width = baseImage.getWidth();
- int height = baseImage.getHeight();
- BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
- Graphics2D newG2d = newImage.createGraphics();
- // 绘制底层图像
- newG2d.drawImage(baseImage, 0, 0, null);
- // 计算图例的位置(右下角)
- int overlayX = width - legendImage.getWidth() - 50;
- int overlayY = height - legendImage.getHeight() - 115;
- // 绘制图例图像
- newG2d.drawImage(legendImage, overlayX, overlayY, null);
- newG2d.dispose();
- return newImage;
- }
- /**
- * 图片图例
- * @param values 图例范围数据
- * @param colorStrArr 颜色RGB码值
- * @return 图例
- */
- 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);
- }
- /**
- * 创建地图
- * @param mapPath 地图路径
- * @return 地图layer
- * @throws Exception
- */
- public VectorLayer createMapLayer(String mapPath) throws Exception {
- // 读取地图
- VectorLayer map = MapDataManage.readMapFile_ShapeFile(mapPath);
- // 描述地图边界线
- PolygonBreak pb = (PolygonBreak) map.getLegendScheme().getLegendBreak(0);
- // 是否设置填充
- pb.setDrawFill(false);
- // 设置轮廓大小
- pb.setOutlineSize(2f);
- // 设置轮廓颜色
- pb.setOutlineColor(Color.black);
- return map;
- }
- /**
- * 设置图例
- * @param layout 视图
- * @param layer 图层
- * @param height 图例高度
- */
- public void setLegend(MapLayout layout, VectorLayer layer, int height) {
- // 设置图例
- Rectangle bounds = layout.getActiveMapFrame().getLayoutBounds();
- LayoutLegend legend = layout.addLegend(bounds.x + 150, bounds.y + bounds.height + 40);
- legend.setLegendStyle(LegendStyles.BAR_HORIZONTAL);
- legend.setHeight(30);
- legend.setWidth(bounds.width - 150);
- legend.setLegendLayer(layer);
- }
- private void checkCityName(VectorLayer nameMap) {
- AttributeTable attributeTable = nameMap.getAttributeTable();
- DataTable table = attributeTable.getTable();
- int rowCount = table.getRowCount();
- for (int i = 0; i < rowCount; i++) {
- Object oldNameObj = table.getValue(i, "NAME");
- String oldName = String.valueOf(oldNameObj);
- if (oldName.contains("aaa")) {
- table.setValue(i, "NAME", "雄安新区");
- }
- }
- attributeTable.setTable(table);
- nameMap.setAttributeTable(attributeTable);
- LabelSet ls = new LabelSet();
- ls.setFieldName("NAME");
- ls.setLabelFont(new Font("黑体", Font.PLAIN, 18));
- nameMap.setLabelSet(ls);
- nameMap.addLabels();
- }
- private void checkCountyName(VectorLayer nameMap) {
- AttributeTable attributeTable = nameMap.getAttributeTable();
- DataTable table = attributeTable.getTable();
- int rowCount = table.getRowCount();
- for (int i = 0; i < rowCount; i++) {
- Object oldNameObj = table.getValue(i, "XZQMC");
- String oldName = String.valueOf(oldNameObj);
- String newName = oldName.replace("满族自治县", "")
- .replace("回族自治县", "")
- .replace("蒙古族自治县", "")
- .replace("鹰手营子矿区", "")
- .replace("井陉矿区", "")
- .replace("渤海新区", "")
- .replace("芦台县", "")
- .replace("滦 县", "")
- .replace("汉沽管理区", "");
- table.setValue(i, "XZQMC", newName);
- }
- attributeTable.setTable(table);
- nameMap.setAttributeTable(attributeTable);
- LabelSet ls = new LabelSet();
- ls.setFieldName("XZQMC");
- ls.setLabelFont(new Font("宋体", Font.PLAIN, 10));
- ls.setLabelColor(Color.GRAY);
- nameMap.setLabelSet(ls);
- nameMap.addLabels();
- }
- /**
- * 添加居中文字
- * @param g2d 图片
- * @param content 文字内容
- * @param color 颜色
- * @param font 字体
- * @param width 横向宽度
- * @param y y的位置
- */
- private void addCenterContent(Graphics2D g2d, String content, Color color, Font font, int width, int y) {
- g2d.setFont(font);
- g2d.setColor(color);
- FontMetrics fm = g2d.getFontMetrics();
- // 设置文字位置和内容
- int x = width / 2 - fm.stringWidth(content) / 2;
- // 绘制文字
- g2d.drawString(content, x, y);
- }
- /**
- * 添加文字
- * @param g2d 图片
- * @param content 文字内容
- * @param color 颜色
- * @param font 字体
- * @param x x的位置
- * @param y y的位置
- */
- private void addContent(Graphics2D g2d, String content, Color color, Font font, int x, int y) {
- g2d.setFont(font);
- g2d.setColor(color);
- // 绘制文字
- g2d.drawString(content, x, y);
- }
- /**
- * 获取中文字体
- * @return
- */
- private Font getChineseFont(int size){
- return new Font("宋体", Font.PLAIN, size);
- }
- }
复制代码
|
|