参数
textfiled,属性字段
layername,表名
geo,wkt格式范围
<select id="selectGeoList" parameterType="java.lang.String" resultType="java.lang.String">
select
<choose>
<when test = "textfiled!=null and textfiled!=''">
concat(st_astext(geom),' ',${textfiled})
when>
<otherwise>
st_astext(geom)
otherwise>
choose>
from ${layername}
where geom is not null and st_intersects(ST_GeomFromText('${geo}',4326), geom)
select>
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.hx.service.CommonService;
import com.hx.util.image.ImageUtil;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
/**
* @author jdy
*
* @date 2023-02-16
*/
@RequestMapping("wms")
@RestController()
public class WmsController {
@Autowired
private CommonService commonService;
@RequestMapping(value = "/wmslayer", method = RequestMethod.GET)
public void getLayer(@RequestParam(value = "LAYER") String LAYER,
@RequestParam(value = "WIDTH")Integer WIDTH,
@RequestParam(value = "HEIGHT")Integer HEIGHT,
@RequestParam(value = "BBOX")String BBOX,
@RequestParam(value = "type")String type,
@RequestParam(value = "textfiled")String textfiled,HttpServletResponse response ) {
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
image = g2d.getDeviceConfiguration().createCompatibleImage(WIDTH, HEIGHT,
Transparency.TRANSLUCENT);
g2d.dispose();
g2d = image.createGraphics();
String layername=LAYER;
String type= type;
List<String> geoData =null;
if(type.equals("text")) {
geoData = commonService.selectGeoList(layername,textfiled,BBOX);
}else {
geoData = commonService.selectGeoList(layername,null,BBOX);
}
if(type.equals("point")) {
String fileurl= "D:\\a.png";
ImageUtil.drawPointByImage(g2d,fileurl,geoData, WIDTH, HEIGHT,BBOX);
}else if(type.equals("line")) {
ImageUtil.drawLine(g2d, geoData, WIDTH, HEIGHT,BBOX);
}else if(type.equals("poygon")) {
ImageUtil.drawPoylgon(g2d, geoData, WIDTH, HEIGHT,BBOX);
}else if(type.equals("text")) {
ImageUtil.drawString(g2d, geoData, WIDTH, HEIGHT,BBOX);
}
// 释放对象
g2d.dispose();
response.setContentType("image/png");
// 保存文件
try {
OutputStream os = response.getOutputStream();
ImageIO.write(image, "png", os);
os.flush();
os.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
package com.hx.util.image;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Transparency;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.springframework.stereotype.Component;
public class ImageUtil {
/**
* @param g2d 画板
* @param imgurl 图片
* @param list 点位坐标集
* @param w 画板宽度
* @param h 画板高度
* @param box 坐标范围
*/
public static void drawPointByImage(Graphics2D g2d, String imgurl, List<String> list, Integer w, Integer h,
String box) {
String[] extent = box.split(",");
double xmin = Double.parseDouble(extent[1]), ymin = Double.parseDouble(extent[0]),
xmax = Double.parseDouble(extent[3]), ymax = Double.parseDouble(extent[2]);
double scalex = ((xmax - xmin) * 3600) / w, scaley = ((ymax - ymin) * 3600) / h;
try {
for (String str : list) {
String[] lonLat =str.replaceAll("POINT[(]", "").replaceAll("[)]", "").split(" ");
String lon=lonLat[0], lat = lonLat[1];
double x = Double.parseDouble(lon), y = Double.parseDouble(lat);
double scrx = (x - xmin) * 3600 / scalex, scry = (ymax - y) * 3600 / scaley;
Image img = ImageIO.read(new File(imgurl));
g2d.drawImage(img, (int) scrx, (int) scry, 20,20,null);
}
g2d.setStroke(new BasicStroke(100));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param g2d 画板
* @param size 圆点大小
* @param list 点位坐标集
* @param w 画板宽度
* @param h 画板高度
* @param box 坐标范围
*/
public static void drawPointByCirle(Graphics2D g2d, Integer size, List<String> list, Integer w, Integer h,
String box) {
String[] extent = box.split(",");
double xmin = Double.parseDouble(extent[1]), ymin = Double.parseDouble(extent[0]),
xmax = Double.parseDouble(extent[3]), ymax = Double.parseDouble(extent[2]);
double scalex = ((xmax - xmin) * 3600) / w, scaley = ((ymax - ymin) * 3600) / h;
try {
for (String str : list) {
String[] lonLat =str.replaceAll("POINT[(]", "").replaceAll("[)]", "").split(" ");
String lon=lonLat[0], lat = lonLat[1];
double x = Double.parseDouble(lon), y = Double.parseDouble(lat);
double scrx = (x - xmin) * 3600 / scalex, scry = (ymax - y) * 3600 / scaley;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 1. 绘制一个圆: 圆的外切矩形 左上角坐标为(0, 0), 宽高为100
//g2d.drawOval((int) scrx, (int) scry, size, size);
g2d.setColor(Color.red);
// 2. 填充一个椭圆
g2d.fillOval((int) scrx, (int) scry, size, size);
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param g2d
* @param list
* @param w
* @param h
* @param box
*/
public static void drawLine(Graphics2D g2d, List<String> list, Integer w, Integer h, String box) {
String[] extent = box.split(",");
double xmin = Double.parseDouble(extent[1]), ymin = Double.parseDouble(extent[0]),
xmax = Double.parseDouble(extent[3]), ymax = Double.parseDouble(extent[2]);
double scalex = ((xmax - xmin) * 3600) / w, scaley = ((ymax - ymin) * 3600) / h;
try {
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);// 抗锯齿
g2d.setColor(Color.RED);// 设置画笔颜色
g2d.setStroke(new BasicStroke(5));
for (String str : list) {
if (str.indexOf("MULTILINESTRING") > -1) {
String[] str1s = str.replaceAll("MULTILINESTRING[(][(]", "").replaceAll("[)][)]", "")
.split("[)],[(]");
for (String str1 : str1s) {
String[] lonlats = str1.split(",");
addLine(g2d, xmin, ymax, scalex, scaley, lonlats);
}
} else {
String[] lonlats = str.replaceAll("LINESTRING[(]", "").replaceAll("[)]", "").split(",");
addLine(g2d, xmin, ymax, scalex, scaley, lonlats);
}
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void addLine(Graphics2D g2d, double xmin, double ymax, double scalex, double scaley,
String[] lonlats) {
int nPoints = lonlats.length;
int[] xPoints = new int[nPoints];
int[] yPoints = new int[nPoints];
for (int i = 0; i < lonlats.length; i++) {
String[] lonlat = lonlats[i].split(" ");
double x = Double.parseDouble(lonlat[0]), y = Double.parseDouble(lonlat[1]);
double scrx = (x - xmin) * 3600 / scalex, scry = (ymax - y) * 3600 / scaley;
xPoints[i] = (int) scrx;
yPoints[i] = (int) scry;
}
g2d.drawPolyline(xPoints, yPoints, nPoints);
}
/**
* @param g2d
* @param list
* @param w
* @param h
* @param box
*/
public static void drawPoylgon(Graphics2D g2d, List<String> list, Integer w, Integer h, String box) {
String[] extent = box.split(",");
double xmin = Double.parseDouble(extent[1]), ymin = Double.parseDouble(extent[0]),
xmax = Double.parseDouble(extent[3]), ymax = Double.parseDouble(extent[2]);
double scalex = ((xmax - xmin) * 3600) / w, scaley = ((ymax - ymin) * 3600) / h;
try {
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);// 抗锯齿
for (String str : list) {
if (str.indexOf("MULTIPOLYGON") > -1) {
String[] str1s = str.replaceAll("MULTIPOLYGON[(][(][(]", "").replaceAll("[)][)][)]", "")
.split("[)][)],[(][(]");
for (String str1 : str1s) {
if(str1.indexOf("),(") > -1) {
String[] str2s = str1.split("[)],[(]");
// Polygon polygon =new Polygon();
// Area polygonShap = new Area(polygon);
// Polygon hole =new Polygon();
// Area holeShap = new Area(hole);
// polygonShap.subtract(holeShap);
//
for (String str2 : str2s) {
String[] lonlats = str2.split(",");
addPoylgon(g2d, xmin, ymax, scalex, scaley, lonlats);
}
}else {
String[] lonlats = str1.split(",");
addPoylgon(g2d, xmin, ymax, scalex, scaley, lonlats);
}
}
} else {
String[] lonlats = str.replaceAll("POLYGON[(][(]", "").replaceAll("[)][)]", "").split(",");
addPoylgon(g2d, xmin, ymax, scalex, scaley, lonlats);
}
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void addPoylgon(Graphics2D g2d, double xmin, double ymax, double scalex, double scaley,
String[] lonlats) {
int nPoints = lonlats.length;
int[] xPoints = new int[nPoints];
int[] yPoints = new int[nPoints];
for (int i = 0; i < lonlats.length; i++) {
String[] lonlat = lonlats[i].split(" ");
double x = Double.parseDouble(lonlat[0]), y = Double.parseDouble(lonlat[1]);
double scrx = (x - xmin) * 3600 / scalex, scry = (ymax - y) * 3600 / scaley;
xPoints[i] = (int) scrx;
yPoints[i] = (int) scry;
}
g2d.setColor(Color.RED);// 设置画笔颜色
g2d.setStroke(new BasicStroke(5));
g2d.drawPolygon(xPoints, yPoints, nPoints);
g2d.setColor(Color.GRAY);// 设置画笔颜色
g2d.fillPolygon(xPoints, yPoints, nPoints);
}
/**
* @param g2d
* @param list
* @param w
* @param h
* @param box
*/
public static void drawString(Graphics2D g2d,List<String> list, Integer w, Integer h, String box) {
g2d.setColor(Color.BLACK);// 设置画笔颜色
String[] extent = box.split(",");
double xmin = Double.parseDouble(extent[1]), ymin = Double.parseDouble(extent[0]),
xmax = Double.parseDouble(extent[3]), ymax = Double.parseDouble(extent[2]);
double scalex = ((xmax - xmin) * 3600) / w, scaley = ((ymax - ymin) * 3600) / h;
for (String str : list) {
String[] lonLat =str.replaceAll("POINT[(]", "").replaceAll("[)]", "").split(" ");
String lon=lonLat[0], lat = lonLat[1];
String text=lonLat[2];
double x = Double.parseDouble(lon), y = Double.parseDouble(lat);
double scrx = (x - xmin) * 3600 / scalex, scry = (ymax - y) * 3600 / scaley;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 设置字体样式, null 表示使用默认字体, Font.PLAIN 为普通样式, 大小为 25px
g2d.setFont(new Font(null, Font.PLAIN, 16));
// 绘制文本, 其中坐标参数指的是文本绘制后的 左下角 的位置
// 首次绘制需要初始化字体, 可能需要较耗时
g2d.drawString(text, ((int) scrx)-text.length()*16/2, (int) scry);
}
}