码上敲享录 > openlayers地图开发常见问题 > java导出地图矢量Shp文件,也包含shx,qix,prj,dbf,fix文件

java导出地图矢量Shp文件,也包含shx,qix,prj,dbf,fix文件

上一章章节目录下一章 2020-06-12已有3512人阅读 评论(0)

java导出地图矢量Shp文件,也包含shx,qix,prj,dbf,fix


解决方法:

1.下面方法list就是从数据库查出来的数据,geometry字段要加ST_ASTEXT,我这里只导出两个属性,我把所有地图信息文件都打在一个zip包中到浏览器下载

SELECT e.estate_num, ST_ASTEXT(e.geom) geom  from tb_real e



2.核心方法

/**

* 导出Shp

*/

public static boolean exportShp(HttpServletResponse response, List<Map<String, Object>> list) {

boolean flag = false;

FileOutputStream fileOutputStream = null;

String id = StringUtil.getid();

String tempPath = System.getProperty("catalina.home") + "/temp/"+ id + "/" ;

String shpFilePath = tempPath + id + ".shp";//临时文件

String zippath = System.getProperty("catalina.home") + "/temp/" + id + ".zip";//压缩包,把xls文件压缩到zip中,在浏览器下载

File dir = new File(tempPath);

if (!dir.exists()) {//每目录就新建

dir.mkdirs();

}

//把生成的文件压缩为.zip的

File zipfile = new File(zippath);

File shpFile = new File(shpFilePath);

try {


//定义属性

final SimpleFeatureType TYPE = DataUtilities.createType("Location",

"the_geom:MultiPolygon," + // <- the geometry attribute: Point type

"BH:String"

);

SimpleFeatureCollection collection = FeatureCollections.newCollection();

SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);


for (Map<String, Object> row : list) {

String wkt = row.get("geom").toString();

//WKT转Geometry

WKTReader wktReader = new WKTReader();

Geometry geometry = null;

try {

geometry = wktReader.read(wkt);

//geometry.setSRID(4326);

} catch (com.vividsolutions.jts.io.ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

;


Object[] obj = {geometry, row.get("estate_num")};

SimpleFeature feature = featureBuilder.buildFeature(null, obj);

collection.add(feature);

/*feature = featureBuilder.buildFeature(null, obj);

collection.add(feature);*/

}

ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();

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

params.put("url", shpFile.toURI().toURL());

params.put("create spatial index", Boolean.TRUE);

ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);

newDataStore.createSchema(TYPE);

newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);


Transaction transaction = new DefaultTransaction("create");

String typeName = newDataStore.getTypeNames()[0];

SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);


if (featureSource instanceof SimpleFeatureStore) {

SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;

featureStore.setTransaction(transaction);

try {

featureStore.addFeatures(collection);

transaction.commit();

} catch (Exception problem) {

problem.printStackTrace();

transaction.rollback();

} finally {

transaction.close();

}

}



FileOutputStream fos1 = new FileOutputStream(zipfile);

ZipUtils.toZip(tempPath, fos1, true);

// 读到流中

InputStream inStream = new FileInputStream(zippath);//文件的存放路径

// 设置输出的格式

response.reset();

response.setContentType("bin");

response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipfile.getName(), "UTF-8"));

ServletOutputStream outputStream = response.getOutputStream();

// 循环取出流中的数据

byte[] b = new byte[1024];

int len;

while ((len = inStream.read(b)) > 0) {

outputStream.write(b, 0, len);

}

inStream.close();

outputStream.flush();

outputStream.close();

flag = true;

} catch (Exception e) {

log.error("【shp导出失败】" + e.getMessage());

} finally {

if (fileOutputStream != null) {

try {

fileOutputStream.close();

} catch (Exception e) {

log.error("【shp导出流关闭失败】" + e.getMessage());

}

}

File filedir = new File(tempPath);

if (filedir.exists() && filedir.isDirectory()) {

File[] files = filedir.listFiles();

for (File f : files) {

if (f.exists()) {

f.delete();

}


}

filedir.delete();


}

              if (zipfile.exists()) {

zipfile.delete();

}


}


return flag;

}



3.ZipUtils压缩工具类:http://yayihouse.com/yayishuwu/chapter/2609


4.如果您找不到依赖包可以参考:http://yayihouse.com/yayishuwu/chapter/2607


向大家推荐《Activiti工作流实战教程》:https://xiaozhuanlan.com/activiti
0

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交