|
@@ -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();
|
|
|
}
|
|
|
}
|
|
|
|