shape文件的生成与打包下载
来源:互联网 发布:true key 是什么软件 编辑:程序博客网 时间:2024/06/03 20:25
概述
本文讲述如何结合Geotools实现后端shp文件的生成与打包下载。
实现效果
实现
shp文件生成
如何生成shp文件在前面的相关博文里面已经做过说明,本文不再赘述。shp文件打包
对与一个shp文件来说,下面四个文件是必须的:.dbf、.prj、.shp、.shx,其中:
1)*.dbf为属性文件;
2)*.prj为投影文件;
3)*.shp为空间信息存储文件;
4)*.shx为图形文件;实现代码
1) shp生成与打包
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文件下载
@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(); } }
技术博客
CSDN:http://blog.csdn.NET/gisshixisheng
博客园:http://www.cnblogs.com/lzugis/
在线教程
http://edu.csdn.Net/course/detail/799
Github
https://github.com/lzugis/
联系方式
阅读全文
0 0
- shape文件的生成与打包下载
- 文件的打包压缩与下载
- 文件生成与下载
- 文件下载、打包下载的问题汇总
- Drupal 文件生成与下载
- jxl实现Excel文件的生成与下载
- 打包下载Linux上的文件
- SpringMVC下打包文件的下载
- Android生成打包的签名文件命令
- 文件的压缩与打包
- 文件的压缩与打包
- 文件的压缩与打包
- PHP实现txt文件生成与下载
- PDF的生成与下载
- java文件打包下载
- 文件打包下载
- C#文件打包下载
- 批量打包 下载文件
- Tomcat优化
- 魂十司机攻略
- 注解@PostConstruct与@PreDestroy讲解及实例
- DrawerLayout侧滑布局点击不响应事件
- java中文乱码问题
- shape文件的生成与打包下载
- JEECMS——web.xml配置
- 餐巾计划问题
- Linux用户和用户组管理
- Lightoj1147【二进制表状态】
- package.json中的dependencies和devDependencies的区别
- 瑞芯微1——RK3288系统编译及环境搭建
- feature map大小计算方法
- 阿里云MaxCompute携手华大基因打造精准医疗应用云平台