Browse Source

显示出来图片

gushoubang 10 months ago
parent
commit
b5dcf8296b

+ 40 - 2
onemap-modules/onemap-spatial/pom.xml

@@ -2,10 +2,9 @@
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <parent>
+        <artifactId>onemap-modules</artifactId>
         <groupId>com.onemap</groupId>
-        <artifactId>onemap</artifactId>
         <version>3.6.1</version>
-        <relativePath>../../pom.xml</relativePath>
     </parent>
     <artifactId>onemap-spatial</artifactId>
     <name>Archetype - onemap-spatial</name>
@@ -50,6 +49,14 @@
             <version>${org.postgresql.version}</version>
         </dependency>
 
+        <!--mybatis-plus-->
+        <dependency>
+            <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-core</artifactId>
+            <version>3.5.2</version>
+            <scope>compile</scope>
+        </dependency>
+
         <!--geotools-->
         <dependency>
             <groupId>org.geotools</groupId>
@@ -76,12 +83,43 @@
             <artifactId>gt-render</artifactId>
             <version>${org.geotools.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.geotools</groupId>
+            <artifactId>gt-opengis</artifactId>
+            <version>${org.geotools.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.geotools</groupId>
+            <artifactId>gt-referencing</artifactId>
+            <version>${org.geotools.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.geotools</groupId>
+            <artifactId>gt-geojson</artifactId>
+            <version>${org.geotools.version}</version>
+        </dependency>
         <dependency>
             <groupId>org.locationtech.jts</groupId>
             <artifactId>jts-core</artifactId>
             <version>1.19.0</version>
         </dependency>
 
+        <!--        l;jkjmj;jkl;-->
+
+        <!-- SpringCloud Alibaba Nacos -->
+        <dependency>
+            <groupId>com.alibaba.cloud</groupId>
+            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
+        </dependency>
+
+        <!-- SpringCloud Alibaba Nacos Config -->
+        <dependency>
+            <groupId>com.alibaba.cloud</groupId>
+            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
+        </dependency>
+
+
+
     </dependencies>
 
     <repositories>

+ 4 - 0
onemap-modules/onemap-spatial/src/main/java/com/onemap/spatial/OneMapSpatialApplication.java

@@ -2,6 +2,8 @@ package com.onemap.spatial;
 
 import com.onemap.common.security.annotation.EnableCustomConfig;
 import com.onemap.common.security.annotation.EnableRyFeignClients;
+import com.onemap.common.swagger.annotation.EnableCustomSwagger2;
+import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.scheduling.annotation.EnableScheduling;
@@ -13,8 +15,10 @@ import org.springframework.scheduling.annotation.EnableScheduling;
  */
 @EnableScheduling
 @EnableCustomConfig
+@EnableCustomSwagger2
 @EnableRyFeignClients
 @SpringBootApplication
+// @MapperScan("com.onemap.spatial.mapper")
 public class OneMapSpatialApplication {
     public static void main(String[] args) {
         SpringApplication.run(OneMapSpatialApplication.class, args);

+ 1 - 1
onemap-modules/onemap-spatial/src/main/java/com/onemap/spatial/controller/ImageController.java

@@ -19,7 +19,7 @@ public class ImageController {
     private IImageService imageService;
 
     @GetMapping("/sensing")
-    public RequestResult getImage(@RequestParam(value = "geom") String geom) throws IOException, CQLException, ParseException {
+    public RequestResult getImage(@RequestParam(value = "geom") String geom) throws Exception {
         return RequestResult.success(imageService.getSensingImage(geom));
     }
 }

+ 4 - 1
onemap-modules/onemap-spatial/src/main/java/com/onemap/spatial/domain/ImageRaster.java

@@ -1,11 +1,14 @@
 package com.onemap.spatial.domain;
 
+import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableName;
 import lombok.Data;
+import org.postgresql.util.PGobject;
 
 @TableName("image_16")
 @Data
 public class ImageRaster {
     private Integer rid;
-    private byte[] rast;
+    @TableField("rast")
+    private PGobject rast;
 }

+ 9 - 2
onemap-modules/onemap-spatial/src/main/java/com/onemap/spatial/mapper/ImageMapper.java

@@ -4,8 +4,15 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.onemap.spatial.domain.ImageRaster;
 import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.annotations.Select;
+import org.postgresql.util.PGobject;
+
+import java.util.List;
 
 public interface ImageMapper extends BaseMapper<ImageRaster> {
-    @Select("SELECT rid, ST_AsPNG(rast) as rast FROM image WHERE ST_Intersects(rast, ST_GeomFromText(#{wkt}, 4326))")
-    ImageRaster selectRasterByWkt(@Param("wkt") String wkt);
+    @Select("SELECT rast FROM image_16 WHERE public.ST_Intersects(rast, public.ST_GeomFromText(#{wkt}, 4326))")
+    List<PGobject> getRasterDataByWKT(@Param("wkt") String wkt);
+
+    // @Select("SELECT rid FROM image_16 WHERE rid=0")
+    // @Master
+    List<Integer> getRids();
 }

+ 1 - 7
onemap-modules/onemap-spatial/src/main/java/com/onemap/spatial/service/IImageService.java

@@ -1,11 +1,5 @@
 package com.onemap.spatial.service;
 
-import org.geotools.filter.text.cql2.CQLException;
-import org.locationtech.jts.io.ParseException;
-
-import java.io.FileNotFoundException;
-import java.io.IOException;
-
 public interface IImageService {
-    String getSensingImage(String geomWkt) throws IOException, CQLException, ParseException;
+    String getSensingImage(String geomWkt) throws Exception;
 }

+ 118 - 45
onemap-modules/onemap-spatial/src/main/java/com/onemap/spatial/service/impl/ImageServiceImpl.java

@@ -1,43 +1,41 @@
 package com.onemap.spatial.service.impl;
 
-import com.onemap.spatial.domain.ImageRaster;
 import com.onemap.spatial.mapper.ImageMapper;
 import com.onemap.spatial.service.IImageService;
-import org.geotools.coverage.grid.GridCoverage2D;
-import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;
-import org.geotools.coverage.grid.io.AbstractGridFormat;
-import org.geotools.coverage.grid.io.GridFormatFinder;
 import org.geotools.data.DataUtilities;
-import org.geotools.data.simple.SimpleFeatureCollection;
+import org.geotools.data.collection.ListFeatureCollection;
+import org.geotools.data.simple.SimpleFeatureSource;
 import org.geotools.factory.CommonFactoryFinder;
-import org.geotools.filter.text.cql2.CQL;
-import org.geotools.filter.text.cql2.CQLException;
+import org.geotools.feature.DefaultFeatureCollection;
+import org.geotools.feature.simple.SimpleFeatureBuilder;
+import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
+import org.geotools.geojson.feature.FeatureJSON;
 import org.geotools.geometry.jts.JTSFactoryFinder;
-import org.geotools.geometry.jts.ReferencedEnvelope;
 import org.geotools.map.FeatureLayer;
-import org.geotools.map.GridReaderLayer;
-import org.geotools.map.Layer;
+import org.geotools.referencing.crs.DefaultGeographicCRS;
+import org.locationtech.jts.geom.Envelope;
+import org.opengis.style.Fill;
 import org.geotools.map.MapContent;
+import org.geotools.map.MapViewport;
 import org.geotools.renderer.lite.StreamingRenderer;
 import org.geotools.styling.SLD;
 import org.geotools.styling.Style;
+import org.geotools.styling.StyleFactory;
 import org.locationtech.jts.geom.Geometry;
-import org.locationtech.jts.io.ParseException;
 import org.locationtech.jts.io.WKTReader;
 import org.opengis.feature.simple.SimpleFeature;
-import org.opengis.filter.Filter;
-import org.opengis.filter.FilterFactory2;
-import org.opengis.geometry.DirectPosition;
+import org.opengis.feature.simple.SimpleFeatureType;
+import org.postgresql.util.PGobject;
 import org.springframework.stereotype.Service;
+import org.opengis.filter.FilterFactory2;
 
 import javax.annotation.Resource;
 import javax.imageio.ImageIO;
 import java.awt.*;
 import java.awt.image.BufferedImage;
-import java.io.*;
-import java.nio.file.Paths;
-import java.util.Base64;
-import java.util.UUID;
+import java.io.File;
+import java.io.IOException;
+import java.io.StringWriter;
 
 /**
  * 生成图片服务
@@ -54,42 +52,117 @@ public class ImageServiceImpl implements IImageService {
      * @return
      */
     @Override
-    public String getSensingImage(String wkt) throws ParseException, IOException {
-        ImageRaster imageEntity = imageMapper.selectRasterByWkt(wkt);
-        // Convert WKT to Geometry
+    public String getSensingImage(String wkt) throws Exception {
+        wktToImage(wkt);
+
+        // // QueryWrapper<ImageRaster> wrapper = new QueryWrapper<>();
+        // // wrapper.eq("rid", 0);
+        // //
+        // // imageMapper.selectOne(wrapper);
+        // // List<Integer> rids = imageMapper.getRids();
+        // List<PGobject> rasterDatas = imageMapper.getRasterDataByWKT(wkt);
+        //
+        // if (rasterDatas.isEmpty()) {
+        //     throw new IllegalArgumentException("No raster data found for the given WKT.");
+        // }
+        //
+        // // Convert raster data to BufferedImage (adjust based on your raster format)
+        // // ByteArrayInputStream bis = new ByteArrayInputStream(rasterData);
+        // ByteArrayInputStream bis = new ByteArrayInputStream(null);
+        // BufferedImage image = ImageIO.read(bis);
+        // bis.close();
+        //
+        // // Convert BufferedImage to byte[]
+        // ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        // ImageIO.write(image, "png", bos);
+        // bos.close();
+        //
+        // // Save image to file system or return image URL
+        // // For example, save to a temporary file
+        // File tempFile = File.createTempFile("raster_image", ".png");
+        // ImageIO.write(image, "png", tempFile);
+
+        // return tempFile.getAbsolutePath();
+        return "";
+    }
+
+    private byte[] processRasterToImage(PGobject rast) {
+        // 使用适当的库将 PGobject 转换为图像
+        // 这里的具体实现取决于你的栅格数据格式和需求
+        // 可以使用 JAI、ImageIO 或其他图像处理库
+        return null;
+    }
+
+    private void wktToImage(String wkt) throws Exception {
+        // 创建一个 WKTReader 对象
         WKTReader reader = new WKTReader(JTSFactoryFinder.getGeometryFactory());
         Geometry geometry = reader.read(wkt);
 
+        // 定义SimpleFeatureType
+        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
+        builder.setName("Location");
+        builder.add("the_geom", Geometry.class);// 添加空间信息
+        builder.add("name", String.class);// 添加到属性字段里
+        final SimpleFeatureType TYPE = builder.buildFeatureType();
 
-        BufferedImage image = null;
-        if (imageEntity != null && imageEntity.getRast() != null) {
-            image = ImageIO.read(new ByteArrayInputStream(imageEntity.getRast()));
-        }
+        // 创建SimpleFeature
+        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
+        featureBuilder.add(geometry);
+        featureBuilder.add("Polygon1");
+        SimpleFeature feature = featureBuilder.buildFeature(null);
 
-        // Overlay WKT on the image
-        if (image != null) {
-            Graphics2D g = image.createGraphics();
-            g.setColor(Color.RED);
-            g.drawRect((int) geometry.getEnvelopeInternal().getMinX(),
-                    (int) geometry.getEnvelopeInternal().getMinY(),
-                    (int) geometry.getEnvelopeInternal().getWidth(),
-                    (int) geometry.getEnvelopeInternal().getHeight());
-            g.dispose();
+        // 创建 FeatureCollection 并添加 Feature
+        DefaultFeatureCollection featureCollection = new DefaultFeatureCollection("internal", TYPE);
+        featureCollection.add(feature);
 
-            // Save image to local file system
-            String fileName = "overlay_" + UUID.randomUUID() + ".png";
-            String filePath = Paths.get(System.getProperty("user.dir"), "images", fileName).toString();
-            File outputFile = new File(filePath);
+        // String geojson = convertToGeoJson(featureCollection);
 
-            // Create directory if it doesn't exist
-            outputFile.getParentFile().mkdirs();
+        // 创建 MapContent
+        MapContent map = new MapContent();
+        map.setTitle("WKT to Image");
 
-            ImageIO.write(image, "png", outputFile);
+        // 创建一个样式
+        Color outlineColor = Color.BLUE;
+        Color fillColor = Color.RED;
+        Style style = SLD.createPolygonStyle(outlineColor, fillColor, 0.5F);
+
+        // 创建一个图层并添加到 MapContent
+        org.geotools.map.FeatureLayer layer = new org.geotools.map.FeatureLayer(featureCollection, style);
+        map.addLayer(layer);
+
+        // 计算几何图形的包络
+        Envelope envelope = geometry.getEnvelopeInternal();
+
+        // 设置 MapViewport
+        MapViewport viewport = new MapViewport();
+        viewport.setBounds(new org.geotools.geometry.jts.ReferencedEnvelope(envelope, null));
+        map.setViewport(viewport);
+
+        // 创建一个 StreamingRenderer
+        StreamingRenderer renderer = new StreamingRenderer();
+        renderer.setMapContent(map);
+
+        // 创建 BufferedImage
+        BufferedImage image = new BufferedImage(800, 600, BufferedImage.TYPE_INT_ARGB);
+        Graphics2D graphics = image.createGraphics();
+        renderer.paint(graphics, new Rectangle(800, 600), viewport.getBounds());
+
+        // 保存图像
+        File file = new File("output.png");
+        ImageIO.write(image, "png", file);
+
+        System.out.println("Image saved to " + file.getAbsolutePath());
+    }
 
-            // Return the file path as the response
-            return filePath;
+    private static String convertToGeoJson(DefaultFeatureCollection featureCollection) {
+        StringWriter writer = new StringWriter();
+        FeatureJSON fjson = new FeatureJSON();
+        try {
+            fjson.writeFeatureCollection(featureCollection, writer);
+        } catch (IOException e) {
+            e.printStackTrace();
         }
-        return "No image found for the provided WKT.";
+        return writer.toString();
     }
 }
 

+ 10 - 0
onemap-modules/onemap-spatial/src/main/resources/mapper/postgresql/ImageMapper.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="com.onemap.spatial.mapper.ImageMapper">
+    <select id="getRids" resultType="Integer">
+        SELECT rid
+        FROM image_16
+        WHERE rid = 0;
+    </select>
+</mapper>

+ 1 - 0
onemap-modules/pom.xml

@@ -15,6 +15,7 @@
         <module>onemap-file</module>
         <module>onemap-apply</module>
         <module>onemap-analyse</module>
+        <module>onemap-spatial</module>
 <!--        <module>onemap-vector</module>-->
     </modules>
     <dependencies>

BIN
output.png