java导出postgis空间数据几何对象shapefile文件

发布于:2025-04-10 ⋅ 阅读:(37) ⋅ 点赞:(0)

项目开发中,需要java后端实现导出postgis空间数据几何对象shapefile文件,以便能直观查看数据详情。注意事项Shapefile 默认的几何字段名为 the_geom,若导出时未显式指定或字段名被修改,部分软件(如 ArcGIS、QGI)可能无法识别几何数据。

1.自定义几何对象构造器

package com.otitan.gz.bozhouly.forestry.industry.geotools;

import org.geotools.geojson.geom.GeometryJSON;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;

/**
 * <p>自定义几何对象构造器</p>
 * @author carter
 * @blob https://blog.csdn.net/qq_21480329
 * @date
 */
public class GeometryCreator {


    private static GeometryCreator geometryCreator = null;
    private static GeometryFactory geometryFactory = new GeometryFactory();

    /**
     * 设置保留6位小数,否则GeometryJSON默认保留4位小数
     */
    private static GeometryJSON geometryJson = new GeometryJSON(6);


    private GeometryCreator() {
    }

    /**
     * 返回本类的唯一实例
     * @return
     */
    public static GeometryCreator getInstance() {
        if (geometryCreator == null) {
            return new GeometryCreator();
        }
        return geometryCreator;
    }


    /**
     * 1.1根据X,Y坐标构建一个几何对象: 点 【Point】
     * @param x
     * @param y
     * @return
     */
    public  Point createPoint(double x,double y){
        Coordinate coord = new Coordinate(x, y);
        return  geometryFactory.createPoint(coord);
    }

    /**
     * 1.2根据几何对象的WKT描述【String】创建几何对象: 点 【Point】
     * @return
     * @throws ParseException
     */
    public Point createPointByWKT(String PointWKT) throws ParseException {
        WKTReader reader = new WKTReader(geometryFactory);
        return (Point) reader.read(PointWKT);
    }

    /**
     * 1.3根据几何对象的WKT描述【String】创建几何对象:多点 【MultiPoint】
     * @return
     * @throws ParseException
     */
    public MultiPoint createMulPointByWKT(String MPointWKT)throws ParseException{
        WKTReader reader = new WKTReader( geometryFactory );
        return (MultiPoint) reader.read(MPointWKT);
    }

    /**
     * 2.1根据两点 创建几何对象:线 【LineString】
     * @param ax 第一个点的x坐标
     * @param ay 第一个点的y坐标
     * @param bx 第二个点的x坐标
     * @param by 第二个点的y坐标
     * @return
     */
    public LineString createLine(double ax,double ay,double bx,double by){
        Coordinate[] coords  = new Coordinate[] {new Coordinate(ax, ay), new Coordinate(bx, by)};
        return  geometryFactory.createLineString(coords);
    }

    /**
     * 2.2根据线的WKT描述创建几何对象:线 【LineString】
     * @param LineStringWKT
     * @return
     * @throws ParseException
     */
    public LineString createLineByWKT(String LineStringWKT) throws ParseException{
        WKTReader reader = new WKTReader( geometryFactory );
        return (LineString) reader.read(LineStringWKT);
    }

    /**
     * 2.3根据点组合的线数组,创建几何对象:多线 【MultiLineString】
     * @param list
     * @return
     */
    public MultiLineString createMLine(List<Coordinate[]> list){

        if(list == null){
            return null;
        }

        LineString[] lineStrings = new LineString[list.size()];
        int i = 0;
        for (Coordinate[] coordinates : list) {
            lineStrings[i] = geometryFactory.createLineString(coordinates);
        }

        return geometryFactory.createMultiLineString(lineStrings);
    }


    /**
     * 2.4根据几何对象的WKT描述【String】创建几何对象 : 多线【MultiLineString】
     * @param MLineStringWKT
     * @return
     * @throws ParseException
     */
    public MultiLineString createMLineByWKT(String MLineStringWKT)throws ParseException{
        WKTReader reader = new WKTReader( geometryFactory );
        return (MultiLineString) reader.read(MLineStringWKT);
    }


    /**
     * 3.1 根据几何对象的WKT描述【String】创建几何对象:多边形 【Polygon】
     * @param PolygonWKT
     * @return
     * @throws ParseException
     */
    public Polygon createPolygonByWKT(String PolygonWKT) throws ParseException{
        WKTReader reader = new WKTReader( geometryFactory );
        return (Polygon) reader.read(PolygonWKT);
    }

    /**
     * 3.2 根据几何对象的WKT描述【String】创建几何对象: 多多边形 【MultiPolygon】
     * @param MPolygonWKT
     * @return
     * @throws ParseException
     */
    public MultiPolygon createMulPolygonByWKT(String MPolygonWKT) throws ParseException{
        WKTReader reader = new WKTReader( geometryFactory );
        return  (MultiPolygon) reader.read(MPolygonWKT);
    }

    /**
     * 根据多边形数组 进行多多边形的创建
     * @param polygons
     * @return
     * @throws ParseException
     */
    public MultiPolygon createMulPolygonByPolygon(Polygon[] polygons) throws ParseException{
        return geometryFactory.createMultiPolygon(polygons);
    }

    /**
     * 4.1 根据几何对象数组,创建几何对象集合:【GeometryCollection】
     * @return
     * @throws ParseException
     */
    public GeometryCollection createGeoCollect(Geometry[] geoArray) throws ParseException{
        return geometryFactory.createGeometryCollection(geoArray);
    }

    /**
     * 5.1 根据圆点以及半径创建几何对象:特殊的多边形--圆 【Polygon】
     * @param x 圆点x坐标
     * @param y 圆点y坐标
     * @param radius 半径
     * @return
     */
    public Polygon createCircle(double x, double y, final double radius){

        //圆上面的点个数
        final int sides = 32;
        Coordinate[]  coords = new Coordinate[sides+1];
        for( int i = 0; i < sides; i++){
            double angle = ((double) i / (double) sides) * Math.PI * 2.0;
            double dx = Math.cos( angle ) * radius;
            double dy = Math.sin( angle ) * radius;
            coords[i] = new Coordinate( (double) x + dx, (double) y + dy );
        }
        coords[sides] = coords[0];
        //线性环
        LinearRing ring = geometryFactory.createLinearRing(coords);
        return geometryFactory.createPolygon(ring, null);
    }


    /**
     * 6.1 根据WKT创建环
     * @param ringWKT
     * @return
     * @throws ParseException
     */
    public LinearRing createLinearRingByWKT(String ringWKT) throws ParseException{
        WKTReader reader = new WKTReader( geometryFactory );
        return (LinearRing) reader.read(ringWKT);
    }

    /**
     * 几何对象转GeoJson对象
     * @param geometry
     * @return
     * @throws Exception
     */
    public static String geometryToGeoJson(Geometry geometry) throws Exception {
        if (geometry == null) {
            return null;
        }
        StringWriter writer = new StringWriter();
        geometryJson.write(geometry, writer);
        String geojson = writer.toString();
        writer.close();
        return geojson;
    }

    /**
     * GeoJson转几何对象
     * @param geojson
     * @return
     * @throws Exception
     */
    public static Geometry geoJsonToGeometry(String geojson) throws Exception {
        return geometryJson.read(new StringReader(geojson));
    }

}

2.ShapeFile文件读写工具类

package com.otitan.gz.bozhouly.forestry.industry.geotools;

import com.otitan.gz.bozhouly.forestry.industry.IO.StringTokenReader;
import com.otitan.gz.bozhouly.forestry.industry.pojos.ShpDatas;
import com.otitan.gz.bozhouly.forestry.industry.pojos.ShpInfo;
import com.otitan.gz.bozhouly.forestry.industry.result.ResponseMessage;
import com.otitan.gz.bozhouly.forestry.industry.result.ResponseResult;
import org.geotools.data.*;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.renderer.lite.StreamingRenderer;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.locationtech.jts.geom.*;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * <p>ShapeFile文件读写工具类</p>
 * @author carter
 * @blob https://blog.csdn.net/qq_21480329
 * @date
 */
public class ShpTools {

    /**几何对象构造器【自定义的】*/
    private  static GeometryCreator gCreator = GeometryCreator.getInstance();

    /**边界*/
    private  static ReferencedEnvelope bounds;

    /**画布的宽度*/
    private static final int IMAGE_WIDTH = 1280;

    /**画布的高度*/
    private static final int IMAGE_HEIGHT = 1200;

    /**
     * 通过shp文件路径,读取shp内容
     * @param filePath 文件路径
     */
    public static ShpDatas readShpByPath(String filePath,Integer limit) throws Exception {

        // 一个数据存储实现,允许从Shapefiles读取和写入
        ShapefileDataStore shpDataStore =  new ShapefileDataStore(new File(filePath).toURI().toURL());
        // 设置编码【防止中文乱码】
        shpDataStore.setCharset(StandardCharsets.UTF_8);

        // getTypeNames:获取所有地理图层,这里我只取第一个【如果是数据表,取出的就是表名】
        String typeName = shpDataStore.getTypeNames()[0];
        System.out.println("shp【图层】名称:"+typeName);
        FeatureCollection<SimpleFeatureType, SimpleFeature> result = getFeatures(shpDataStore, typeName);

        // 迭代特征集合
        FeatureIterator<SimpleFeature> iterator = result.features();

        ShpDatas shpDatas = new ShpDatas();
        shpDatas.setName(typeName);
        shpDatas.setShpPath(filePath);
        buildShpDatas(limit, iterator, shpDatas);
        iterator.close();
        return  shpDatas;
    }


    /**
     * 根据数据源及图层名称拿到特征集合
     * @param shpDataStore shp数据存储对象
     * @param typeName 图层名称
     * @return FeatureCollection
     */
    private static FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatures(ShapefileDataStore shpDataStore, String typeName) throws IOException {

        // 通过此接口可以引用单个shapefile、数据库表等。与数据存储进行比较和约束
        FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = shpDataStore.getFeatureSource(typeName);
        // 一个用于处理FeatureCollection的实用工具类。提供一个获取FeatureCollection实例的机制
        FeatureCollection<SimpleFeatureType, SimpleFeature> result = featureSource.getFeatures();
        System.out.println("地理要素【记录】:"+result.size()+"个");
        System.out.println("==================================");
        return result;
    }

    /**
     * 构建shpDatas对象
     * @param limit 要素查询限制数
     * @param iterator 迭代器
     * @param shpDatas shp封装的数据集
     */
    private static void buildShpDatas(Integer limit, FeatureIterator<SimpleFeature> iterator, ShpDatas shpDatas) {
        // 这里我们只迭代前limit个
        int stop = 0;
        while (iterator.hasNext()) {
            if (stop > limit) {
                break;
            }
            // 拿到一个特征
            SimpleFeature feature = iterator.next();
            // 取出特征里面的属性集合
            Collection<Property> p = feature.getProperties();

            // 遍历属性集合
            Map<String,Object> prop = new HashMap<>();
            for (Property pro : p) {
                String key = pro.getName().toString();
                String val;
                if ("java.util.Date".equals(pro.getType().getBinding().getName())){
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
                    val  = pro.getValue() ==null ? "" : dateFormat.format(pro.getValue());
                }else{
                    val = pro.getValue()==null ?"":pro.getValue().toString();
                }
                prop.put(key, val);
                System.out.println("key【字段】:"+key+"\t||value【值】:"+val);
            }
            System.out.println("\n============================ 序号:"+stop+"\n");
            shpDatas.addProp(prop);
            stop++;
        } // end 最外层 while
    }

    /**
     * 将一个几何对象写进shapefile
     * @param filePath 文件路径
     * @param geometry 几何对象
     */
    public  static  void writeShpByGeom(String filePath, Geometry geometry) throws Exception{

        ShapefileDataStore ds = getshpDS(filePath, geometry);

        FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0],
                Transaction.AUTO_COMMIT);

        // Interface SimpleFeature:一个由固定列表值以已知顺序组成的SimpleFeatureType实例。
        SimpleFeature feature = writer.next();
        feature.setAttribute("name", "XXXX名称");
        feature.setAttribute("path", "./test");
        feature.setAttribute("the_geom", geometry);
        feature.setAttribute("id", 1010L);
        feature.setAttribute("des", "XXXX描述");

        System.out.println("========= 写入【"+geometry.getGeometryType()+"】成功 !=========");

        // 写入
        writer.write();
        // 关闭
        writer.close();
        // 释放资源
        ds.dispose();
    }

    /**
     * 将一个几何对象写进shapefile
     * @param file 文件
     * @param map 数据
     */
    public static void writeShpByGeom(File file, Map<String, Object> map, Geometry geometry) throws Exception{
        ShapefileDataStore ds = getshpDS(file, map, geometry);

        FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0],
                Transaction.AUTO_COMMIT);

        // Interface SimpleFeature:一个由固定列表值以已知顺序组成的SimpleFeatureType实例。
        SimpleFeature feature = writer.next();

//        for (AttributeDescriptor attr : feature.getFeatureType().getAttributeDescriptors()) {
//            System.out.println("Schema 字段: " + attr.getLocalName());
//        }

        Map<String, Object> sortedMap = new TreeMap<>(map);
        sortedMap.forEach((k, v) -> {
//            System.out.println(k + ":" + v);
            feature.setAttribute(k, v);
        });

//        feature.setAttribute("the_geom", geometry);


//        feature.setAttribute("name", "XXXX名称");
//        feature.setAttribute("path", "./test");
//        feature.setAttribute("the_geom", geometry);
//        feature.setAttribute("id", 1010L);
//        feature.setAttribute("des", "XXXX描述");
//
//        System.out.println("========= 写入【"+geometry.getGeometryType()+"】成功 !=========");

        // 写入
        writer.write();
        // 关闭
        writer.close();
        // 释放资源
        ds.dispose();
    }


    /**
     * 将一个几何对象写进shapefile
     * @param shpInfo shp信息
     */
    public  static ResponseResult writeShpByGeom(ShpInfo shpInfo) throws Exception{

        // 特殊字符串解析器
        StringTokenReader reader = new StringTokenReader();
        // 根据几何对象的wkt字符串,反解【解析】成Geometry对象
        Geometry geometry = reader.read(shpInfo.getGeom());
        // 拿到shp对象所在的目录【文件夹】
        String path = shpInfo.getPath();
        File file = new File(path);
        if(!file.exists()){
            file.mkdir();
        }

        if(!file.isDirectory()){
            return  new ResponseResult(ResponseMessage.BAD_REQUEST,"path不是有效的文件夹" );
        }

        String filePath = shpInfo.getPath()+"/"+shpInfo.getName()+".shp";
        ShapefileDataStore ds = getshpDS(filePath, geometry);
        String typeName = ds.getTypeNames()[0];
        FeatureWriter<SimpleFeatureType, SimpleFeature> writer ;
        if(shpInfo.isAppendWrite()){
            // 追加写几何对象
            writer = ds.getFeatureWriterAppend(typeName, Transaction.AUTO_COMMIT);
        }else{
            // 覆盖写几何对象
            writer = ds.getFeatureWriter(typeName, Transaction.AUTO_COMMIT);
        }

        // Interface SimpleFeature:一个由固定列表值以已知顺序组成的SimpleFeatureType实例。
        SimpleFeature feature = writer.next();
        feature.setAttribute("name", shpInfo.getName());
        feature.setAttribute("path", shpInfo.getPath());
        feature.setAttribute("the_geom", geometry);
        feature.setAttribute("id", shpInfo.getId());
        feature.setAttribute("des", shpInfo.getDes());

        System.out.println("========= 写入【"+geometry.getGeometryType()+"】成功 !=========");

        // 写入
        writer.write();
        // 关闭
        writer.close();
        // 释放资源
        ds.dispose();
        // 返回创建成功后的shp文件路径
        return  new ResponseResult(ResponseMessage.OK,filePath);

    }

    /**
     * 拿到配置好的DataStore
     * @param file 文件
     * @param geometry 几何对象
     * @return ShapefileDataStore
     */
    private static ShapefileDataStore getshpDS(File file, Map<String, Object> map, Geometry geometry) throws IOException {
        Map<String, Serializable> params = new HashMap<>();
        // 1. 获取父目录
        File parentDir = file.getParentFile();

        // 2. 检查父目录是否存在,若不存在则创建
        if (parentDir != null && !parentDir.exists()) {
            parentDir.mkdirs();
        }

        // 2、用于捕获参数需求的数据类 URLP:url to the .shp file.
        params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());

        // 3、创建一个新的数据存储【如果存在,则不创建】
        ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);

        // 4、定义图形信息和属性信息 -- SimpleFeatureTypeBuilder 构造简单特性类型的构造器
        SimpleFeatureTypeBuilder tBuilder = new SimpleFeatureTypeBuilder();

        // 5、设置 -- WGS84:一个二维地理坐标参考系统,使用WGS84数据
        tBuilder.setCRS(DefaultGeographicCRS.WGS84);
        tBuilder.setName("shapefile");

        Map<String, Object> sortedMap = new TreeMap<>(map);
        sortedMap.forEach((k, v) -> {
            tBuilder.add(k, v.getClass());
//            System.out.println(k + ":" + v.getClass());
        });

//        tBuilder.add("the_geom", geometry.getClass());

//        map.forEach((k, v) -> {
//            tBuilder.add(k, v.getClass());
//        });

        // 添加名称
//        tBuilder.add("name", String.class);
//        // 添加shp所在目录名称
//        tBuilder.add("path", String.class);
//        // 添加 一个几何对象
//        tBuilder.add("the_geom", geometry.getClass());
//        // 添加一个id
//        tBuilder.add("id", Long.class);
//        // 添加描述
//        tBuilder.add("des", String.class);

        // 设置此数据存储的特征类型
        ds.createSchema(tBuilder.buildFeatureType());
        // 设置编码
        ds.setCharset(StandardCharsets.UTF_8);
        return ds;
    }

    /**
     * 拿到配置好的DataStore
     * @param filePath 文件路径
     * @param geometry 几何对象
     * @return ShapefileDataStore
     */
    private static ShapefileDataStore getshpDS(String filePath, Geometry geometry) throws IOException {
        // 1.创建shape文件对象
        File file = new File(filePath);

        Map<String, Serializable> params = new HashMap<>();

        // 2、用于捕获参数需求的数据类 URLP:url to the .shp file.
        params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());

        // 3、创建一个新的数据存储【如果存在,则不创建】
        ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);

        // 4、定义图形信息和属性信息 -- SimpleFeatureTypeBuilder 构造简单特性类型的构造器
        SimpleFeatureTypeBuilder tBuilder = new SimpleFeatureTypeBuilder();

        // 5、设置 -- WGS84:一个二维地理坐标参考系统,使用WGS84数据
        tBuilder.setCRS(DefaultGeographicCRS.WGS84);
        tBuilder.setName("shapefile");

        // 添加名称
        tBuilder.add("name", String.class);
        // 添加shp所在目录名称
        tBuilder.add("path", String.class);
        // 添加 一个几何对象
        tBuilder.add("the_geom", geometry.getClass());
        // 添加一个id
        tBuilder.add("id", Long.class);
        // 添加描述
        tBuilder.add("des", String.class);

        // 设置此数据存储的特征类型
        ds.createSchema(tBuilder.buildFeatureType());
        // 设置编码
        ds.setCharset(StandardCharsets.UTF_8);
        return ds;
    }

    /**
     * 打开shp文件,获取地图内容
     * @param filePath  文件路径
     * @param isOpenByChoose 是否自定义打开shp文件
     * @throws Exception
     */
    public static  MapContent getMapContentByPath(String filePath,boolean isOpenByChoose,String color) throws  Exception{

        File file;
        if(isOpenByChoose){
            // 1.1、 数据源选择 shp扩展类型的
            file = JFileDataStoreChooser.showOpenFile("shp", null);
        }else{
            // 1.2、根据路径拿到文件对象
            file = new File(filePath);
        }

        if(file==null){
            return null;
        }
        // 2、得到打开的文件的数据源
        FileDataStore store = FileDataStoreFinder.getDataStore(file);

        // 3、设置数据源的编码,防止中文乱码
        ((ShapefileDataStore)store).setCharset(Charset.forName("UTF-8"));

        /**
         * 使用FeatureSource管理要素数据
         * 使用Style(SLD)管理样式
         * 使用Layer管理显示
         * 使用MapContent管理所有地图相关信息
         */

        // 4、以java对象的方式访问地理信息 --    简单地理要素
        SimpleFeatureSource featureSource = store.getFeatureSource();

        bounds = featureSource.getBounds();

        // 5、创建映射内容,并将我们的shapfile添加进去
        MapContent mapContent = new MapContent();

        // 6、设置容器的标题
        mapContent.setTitle("Appleyk's GeoTools");
        Color color1;
        if(color == null || "".equals(color.toLowerCase())){
            color1 = Color.BLACK;
        }else if("red".equals(color.toLowerCase())){
            color1 = Color.RED;
        }else if("green".equals(color.toLowerCase())){
            color1 = Color.GREEN;
        }else if("blue".equals(color.toLowerCase())){
            color1 = Color.BLUE;
        }else{
            color1 = Color.getColor(color);
        }

        // 7、创建简单样式 【颜色填充】
        Style style = SLD.createSimpleStyle(featureSource.getSchema(),color1);

        // 8、显示【shapfile地理信息+样式】
        Layer layer = new FeatureLayer(featureSource, style);

        // 9、将显示添加进map容器
        mapContent.addLayer(layer);

        return  mapContent;
    }

    public  static  void showMap(MapContent mapContent){
        JMapFrame.showMap(mapContent);
    }

    /**
     * shp文件转Image【格式定png】
     * @param shpFilePath shp目标文件
     * @param destImagePath 转成图片的文件 == 如果没有,转成的图片写进response输出流里
     * @param response 响应流
     * @throws Exception
     */
    public static void shp2Image(String shpFilePath,String destImagePath,String color, HttpServletResponse response) throws  Exception{

        // 流渲染器
        StreamingRenderer renderer = new StreamingRenderer();
        MapContent mapContent = getMapContentByPath(shpFilePath,false,color );
        renderer.setMapContent(mapContent);
        Rectangle imageBounds = new Rectangle(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
        BufferedImage dumpImage = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = dumpImage.createGraphics();
        g2d.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        renderer.paint(g2d, imageBounds, bounds);
        g2d.dispose();
        if(destImagePath == null || "".equals(destImagePath)){
            ImageIO.write(dumpImage, "png", response.getOutputStream());
        }else{
            ImageIO.write(dumpImage, "png", new File(destImagePath));
        }
    }

    public static void main(String[] args) throws  Exception{

//        File file = ResourceUtils.getFile("classpath:static/shpTest[Point]/dp_tl.shp");
//        // 从shp文件里面读取属性信息
//        readShpByPath(file.getAbsolutePath(),10);
        System.out.println("=================下面开始往shp文件里面写几何对象===================");

        // 先创建文件夹test
        String filePath = "./test.shp";

//        String pointWkt="POINT (120.76164848270959 31.22001141278534)";
//        Point point = gCreator.createPointByWKT(pointWkt);
//
//        // Polygon【面】
//        String polygonWkt="POLYGON ((103.859188 34.695908, 103.85661 34.693788, 103.862027 34.69259, 103.863709 34.695078, 103.859188 34.695908))";
//        Polygon polygon = gCreator.createPolygonByWKT(polygonWkt);
//
//        // LineString【线】
//        String linestringWkt="LINESTRING(113.511315990174 41.7274734296674,113.51492087909 41.7284983348307,113.516079593384 41.727649586406,113.515907932007 41.7262243043929,113.514019656861 41.7247989907606,113.512131381714 41.7250872589898,113.51138036319 41.7256637915682,113.511315990174 41.7274734296674)";
//        LineString lineString = gCreator.createLineByWKT(linestringWkt);

        // MultiPolygon【多面】
        String multiPolyWkt = "MULTIPOLYGON(((101.870371 25.19228,101.873633 25.188183,101.880564 25.184416,101.886808 25.186028,101.892043 25.189969,101.896592 25.190163,101.903716 25.190785,101.905454 25.193464,101.899897 25.196202,101.894146 25.197911,101.891657 25.19826,101.886078 25.197658,101.884211145538 25.2007060137013,101.88172564506 25.1949712942389,101.87874 25.199619,101.874641 25.200998,101.868547 25.202415,101.863741 25.202415,101.85887 25.202842,101.854557 25.202182,101.852604 25.199736,101.852282 25.19628,101.854492 25.194183,101.855608 25.192668,101.863698 25.192105,101.870371 25.19228)))";
        MultiPolygon multiPolygon = gCreator.createMulPolygonByWKT(multiPolyWkt);

        // 几何对象的范围【矩形边界】
        Envelope envelope = multiPolygon.getEnvelopeInternal();
        System.out.println(envelope);

        // 往shp文件里面写几何对象
        writeShpByGeom(filePath,multiPolygon);
    }

}

3.导出实现

    @GetMapping("/export/{gid}")
    public void exportShp(@PathVariable("gid") Integer gid, HttpServletResponse response) throws Exception {
        MatterRecordEntity matterRecord = matterRecordService.findMatterRecordByGid(gid);
        // 将PGobject转换为Geometry对象
        Geometry geometry = GeometryConverter.convertPGObjectToGeometry(matterRecord.getGeom());
        String businessCode = matterRecord.getBusinessCode() + "_" + new DateTime().getMillis();
        String filePath = fileShp + businessCode + "/";
        String fileName = matterRecord.getBusinessCode() + ".shp";

        File file = new File(filePath + fileName);

        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(matterRecord));
        // Shapefile 默认的几何字段名为 the_geom,若导出时未显式指定或字段名被修改,部分软件(如 ArcGIS)可能无法识别几何数据
        jsonObject.put("the_geom", geometry);

        Map<String, Object> map = new HashMap<>();
        jsonObject.forEach((k, v) -> {
            if (k.length() > 10) {
                return;
            }
            map.put(k, v);
        });

        // 往shp文件里面写几何对象
        writeShpByGeom(file, map, geometry);

        File sourceDir = new File(filePath);

//        String fileNameZip = URLEncoder.encode(matterRecord.getBusinessCode() + ".zip", StandardCharsets.UTF_8.name());
//        response.setContentType("application/zip");
//        response.setHeader(HttpHeaders.CONTENT_DISPOSITION,
//                "attachment; filename=" + fileNameZip);

        // 获取Servlet的输出流,用于写入ZIP文件内容
        ServletOutputStream outputStream = response.getOutputStream();
        ZipOutputStream zos = new ZipOutputStream(outputStream);
//        addDirectoryToZip(sourceDir, zos, sourceDir.getName());
        compressDirFlat(sourceDir, zos);

        String fileNameZip = URLEncoder.encode(businessCode + ".zip", StandardCharsets.UTF_8.name());
        response.setContentType("application/zip");
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION,
                "attachment; filename=" + fileNameZip);

        zos.close(); // 关闭ZipOutputStream,这将触发所有缓冲的数据被写入输出流
        outputStream.flush(); // 刷新输出流,确保所有数据都被发送
    }

    private void compressDirFlat(File dir, ZipOutputStream zos) throws IOException {
        for (File file : dir.listFiles()) {
            if (file.isHidden()) continue;

            if (file.isDirectory()) {
                compressDirFlat(file, zos); // 递归处理子目录
            } else {
                addFileEntry(file, zos);
            }
        }
    }

    private void addFileEntry(File file, ZipOutputStream zos) throws IOException {
        try (FileInputStream fis = new FileInputStream(file)) {
            // 仅保留文件名,忽略路径
            ZipEntry entry = new ZipEntry(file.getName());
            zos.putNextEntry(entry);

            // 带缓冲的流复制
            byte[] buffer = new byte[4096];
            int len;
            while ((len = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }
            zos.closeEntry();
        }
    }

注意事项Shapefile 默认的几何字段名为 the_geom,若导出时未显式指定或字段名被修改,部分软件(如 ArcGIS、QGI)可能无法识别几何数据。
在这里插入图片描述
4.导出效果截图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
5.相关大数据学习demo地址:
https://github.com/carteryh/big-data