TileWebController.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package cn.com.taiji.web.controller;
  2. import cn.com.taiji.cn.com.taiji.common.domain.Result;
  3. import cn.com.taiji.cql.model.GeoDataSource;
  4. import cn.com.taiji.cql.model.GeoRequestParam;
  5. import cn.com.taiji.tile.service.ITileService;
  6. import cn.com.taiji.web.service.IGeoDataSourceService;
  7. import com.alibaba.fastjson.JSONObject;
  8. import io.swagger.annotations.Api;
  9. import io.swagger.annotations.ApiOperation;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.web.bind.annotation.GetMapping;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RequestMethod;
  15. import org.springframework.web.bind.annotation.RestController;
  16. import javax.servlet.ServletOutputStream;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import java.util.Enumeration;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. /**
  24. * 图层通用查询控制器
  25. *
  26. * @author makejava
  27. * @since 2021-04-22 16:53:35
  28. */
  29. @Slf4j
  30. @RestController
  31. @Api("图层通用查询控制器")
  32. public class TileWebController {
  33. @Autowired
  34. private ITileService tileService;
  35. @Autowired
  36. private IGeoDataSourceService geoDataSourceService;
  37. @ApiOperation("面向瓦片的点击查询")
  38. @GetMapping("/queryElements")
  39. public Result<List<?>> queryElements(String bbox,Integer type,String layerName){
  40. List<?> list=tileService.queryElements(bbox,type,layerName);
  41. return Result.ok();
  42. }
  43. @RequestMapping(value = {"/queryTile"}, method = {RequestMethod.GET, RequestMethod.POST})
  44. public void queryTile(HttpServletRequest request, HttpServletResponse response) {
  45. GeoRequestParam geoRequestParam = JSONObject.parseObject(JSONObject.toJSONString(getParameterMap(request)), GeoRequestParam.class);
  46. if(geoRequestParam.getLayers() !=null && !"".equals(geoRequestParam.getLayers())){
  47. try{
  48. GeoDataSource geoDs = geoDataSourceService.layerCodeGeoDataSource(geoRequestParam.getLayers());
  49. if(geoDs !=null){
  50. byte[] ret = tileService.tileData(geoRequestParam,geoDs);
  51. responseWrite(response, "image/png", ret);
  52. }else{
  53. log.info("根据图层代码:" + geoRequestParam.getLayers() + "获取图层对应的数据源为空或者为null");
  54. }
  55. }catch (Exception exception){
  56. exception.printStackTrace();
  57. log.error("根据图层代码:" + geoRequestParam.getLayers() +"获取数据源信息失败!"+exception);
  58. }
  59. }else{
  60. log.info("图层代码layers参数为空!");
  61. }
  62. }
  63. private void responseWrite(HttpServletResponse response, String contentType, byte[] data) {
  64. response.setStatus(200);
  65. response.setContentType(contentType);
  66. if (data != null) {
  67. response.setContentLength(data.length);
  68. ServletOutputStream out = null;
  69. try {
  70. out = response.getOutputStream();
  71. out.write(data);
  72. out.flush();
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. } finally {
  76. if (out != null)
  77. try {
  78. out.close();
  79. } catch (Exception e2) {
  80. e2.printStackTrace();
  81. }
  82. }
  83. }
  84. }
  85. /**
  86. * 将请求参数转化为Map
  87. *
  88. * @param request
  89. * @return
  90. */
  91. public static Map<String, Object> getParameterMap(HttpServletRequest request) {
  92. Map<String, Object> param = new HashMap<>();
  93. try {
  94. Enumeration<String> em = request.getParameterNames();
  95. while (em.hasMoreElements()) {
  96. String key = em.nextElement();
  97. param.put(key.toLowerCase(), request.getParameter(key));
  98. }
  99. } catch (Exception e) {
  100. e.printStackTrace();
  101. }
  102. return param;
  103. }
  104. }