| package com.supermap.jczcpt.pipe.manage.planninganalysis.util; |
| |
| 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.locationtech.jts.geom.*; |
| 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 ShapeUtil { |
| |
| |
| * 生成 shape 文件 |
| * |
| * @param shpPath 生成 shape 文件路径(包含文件名称) |
| * @param encode 编码 |
| * @param geoType 图幅类型,Point 和 Rolygon |
| * @param geoms 图幅集合 |
| */ |
| public static void write2Shape(String shpPath, String encode, String geoType, List<Geometry> geoms) { |
| try { |
| |
| File file = new File(shpPath); |
| Map<String, Serializable> params = new HashMap<>(); |
| 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"); |
| |
| if ("Polygon".equals(geoType)) { |
| tb.add("the_geom", Polygon.class); |
| } else if ("MultiPolygon".equals(geoType)) { |
| tb.add("the_geom", MultiPolygon.class); |
| } else if ("Point".equals(geoType)) { |
| tb.add("the_geom", Point.class); |
| } else if ("MultiPoint".equals(geoType)) { |
| tb.add("the_geom", MultiPoint.class); |
| } else if ("LineString".equals(geoType)) { |
| tb.add("the_geom", LineString.class); |
| } else if ("MultiLineString".equals(geoType)) { |
| tb.add("the_geom", MultiLineString.class); |
| } else { |
| throw new Exception("Geometry中没有该类型:" + geoType); |
| } |
| |
| ds.createSchema(tb.buildFeatureType()); |
| |
| Charset charset = Charset.forName(encode); |
| ds.setCharset(charset); |
| |
| FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT); |
| for (Geometry geom : geoms) { |
| |
| |
| |
| SimpleFeature feature = writer.next(); |
| |
| feature.setAttribute("the_geom", geom); |
| } |
| writer.write(); |
| writer.close(); |
| ds.dispose(); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| |
| * 生成 shape 文件 |
| * |
| * @param shpPath 生成 shape 文件路径(包含文件名称) |
| * @param encode 编码 |
| * @param geoType 图幅类型,Point 和 Rolygon |
| * @param shpKey data 中图幅的 key |
| * @param attrKeys 属性 key 集合 |
| * @param data 图幅和属性集合 |
| */ |
| public static void write2Shape(String shpPath, String encode, String geoType, String shpKey, List<String> attrKeys, List<Map<String, Object>> data) { |
| try { |
| if (data == null || data.size() == 0) { |
| return; |
| } |
| |
| File file = new File(shpPath); |
| Map<String, Serializable> params = new HashMap<>(); |
| 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"); |
| |
| if ("Polygon".equals(geoType)) { |
| tb.add("the_geom", Polygon.class); |
| } else if ("MultiPolygon".equals(geoType)) { |
| tb.add("the_geom", MultiPolygon.class); |
| } else if ("Point".equals(geoType)) { |
| tb.add("the_geom", Point.class); |
| } else if ("MultiPoint".equals(geoType)) { |
| tb.add("the_geom", MultiPoint.class); |
| } else if ("LineString".equals(geoType)) { |
| tb.add("the_geom", LineString.class); |
| } else if ("MultiLineString".equals(geoType)) { |
| tb.add("the_geom", MultiLineString.class); |
| } else { |
| throw new Exception("Geometry中没有该类型:" + geoType); |
| } |
| |
| for (String field : attrKeys) { |
| tb.add(field.toUpperCase(), String.class); |
| } |
| |
| ds.createSchema(tb.buildFeatureType()); |
| |
| Charset charset = Charset.forName(encode); |
| ds.setCharset(charset); |
| |
| 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 = data.get(i); |
| Geometry geom = (Geometry) row.get(shpKey); |
| feature.setAttribute("the_geom", geom); |
| for (String key : row.keySet()) { |
| if (!key.equals(shpKey)) { |
| if (row.get(key) != null) { |
| feature.setAttribute(key.toUpperCase(), row.get(key).toString()); |
| } else { |
| feature.setAttribute(key.toUpperCase(), ""); |
| } |
| } |
| } |
| } |
| writer.write(); |
| writer.close(); |
| ds.dispose(); |
| |
| |
| |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| |
| |
| * 压缩 shape 文件 |
| * |
| * @param shpPath shape 文件路径(包含 shape 文件名称) |
| */ |
| public static void zipShapeFile(String shpPath) { |
| try { |
| File shpFile = new File(shpPath); |
| String shpRoot = shpFile.getParentFile().getPath(); |
| String shpName = shpFile.getName().substring(0, shpFile.getName().lastIndexOf(".")); |
| |
| String zipPath = shpRoot + File.separator + shpName + ".zip"; |
| File zipFile = new File(zipPath); |
| InputStream input = null; |
| ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); |
| |
| 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", |
| shpRoot + File.separator + shpName + ".fix" |
| }; |
| |
| 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(); |
| } |
| } |
| } |