前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >shape文件的生成与打包下载

shape文件的生成与打包下载

作者头像
lzugis
发布2018-10-23 11:19:12
2.7K1
发布2018-10-23 11:19:12
举报

概述

本文讲述如何结合Geotools实现后端shp文件的生成与打包下载。

实现效果

生成文件
生成文件
压缩文件
压缩文件

实现

  1. shp文件生成 如何生成shp文件在前面的相关博文里面已经做过说明,本文不再赘述。
  2. shp文件打包 对与一个shp文件来说,下面四个文件是必须的:.dbf、.prj、.shp、.shx,其中: 1)*.dbf为属性文件; 2)*.prj为投影文件; 3)*.shp为空间信息存储文件; 4)*.shx为图形文件;
  3. 实现代码 1) shp生成与打包
代码语言:javascript
复制
package com.lzugis.helper;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import java.io.*;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ShapeHelper {

    public ShapeHelper(){
        super();

    }

    public void write2ShapeFile(String shpPath, String[] header, List data){
        try{
            //创建shape文件对象
            File file = new File(shpPath);
            Map<String, Serializable> params = new HashMap<String, Serializable>();
            params.put( ShapefileDataStoreFactory.URLP.key, file.toURI().toURL() );
            ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
            //定义图形信息和属性信息
            SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
            tb.setCRS(DefaultGeographicCRS.WGS84);
            tb.setName("shapefile");
            tb.add("the_geom", Point.class);
            for(int i= 0;i<header.length;i++){
                String field = header[i];
                tb.add(field.toUpperCase(), String.class);
            }
            ds.createSchema(tb.buildFeatureType());
            //设置编码
            Charset charset = Charset.forName("GBK");
            ds.setCharset(charset);
            //设置Writer
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
            //写入文件信息
            for(int i=0;i<data.size();i++){
                SimpleFeature feature = writer.next();
                Map<String, Object> row = (Map)data.get(i);
                Geometry geom = new GeometryFactory().createPoint(new Coordinate((Double)row.get("x"), (Double)row.get("y")));
                feature.setAttribute("the_geom", geom);
                for (Map.Entry entry : row.entrySet()) {
                    feature.setAttribute(entry.getKey().toString().toUpperCase()
                            , entry.getValue());
                }
            }
            writer.write();
            writer.close();
            ds.dispose();

            //添加到压缩文件
            zipShapeFile(shpPath);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void zipShapeFile(String shpPath){
        try{
            File shpFile = new File(shpPath);
            String shpRoot = shpFile.getParentFile().getPath(),
                    _shpName = shpFile.getName(),
                    shpName = _shpName.substring(0, _shpName.lastIndexOf("."));

            String zipPath = shpRoot+File.separator+shpName+".zip";
            File zipFile = new File(zipPath);
            InputStream input = null;
            ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
            // zip的名称为
            zipOut.setComment(shpName);
            String[] shpFiles = new String[]{
                    shpRoot+File.separator+shpName+".dbf",
                    shpRoot+File.separator+shpName+".prj",
                    shpRoot+File.separator+shpName+".shp",
                    shpRoot+File.separator+shpName+".shx",
            };
            for(int i=0;i<shpFiles.length;i++){
                File _file = new File(shpFiles[i]);
                input = new FileInputStream(_file);
                zipOut.putNextEntry(new ZipEntry(_file.getName()));
                int temp = 0;
                while ((temp = input.read()) != -1) {
                    zipOut.write(temp);
                }
                input.close();
            }
            zipOut.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

2) shp文件下载

代码语言:javascript
复制
    @RequestMapping(value="geocode/down")
    public void downGeocodePois(String type, HttpServletResponse response){
        try {
            String downFile = geoService.getDownFile(type);
            File file = new File(downFile);
            String filename = file.getName();// 获取日志文件名称
            InputStream fis = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            response.reset();
            // 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.replaceAll(" ", "").getBytes("utf-8"),"iso8859-1"));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream os = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            os.write(buffer);// 输出文件
            os.flush();
            os.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年11月29日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • 实现效果
  • 实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档