本文讲述如何结合Geotools实现后端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();
}
}