private static final String GEOMETRY_TYPE_POINT = "Point";
private static final String GEOMETRY_TYPE_MULTIPOINT = "MultiPoint";
private static final String GEOMETRY_TYPE_LINESTRING = "LineString";
private static final String GEOMETRY_TYPE_MULTILINESTRING = "MultiLineString";
private static final String GEOMETRY_TYPE_POLYGON = "Polygon";
private static final String GEOMETRY_TYPE_MULTIPOLYGON = "MultiPolygon";
public static void write2Shape(String shpPath, String encode, String authority,String geoType, List geoms) {
try {
//创建shape文件对象
File file = new File(shpPath);
Map params = new HashMap<>();
ShapefileDataStore ds = new ShapefileDataStore(file.toURI().toURL());
//定义图形信息和属性信息
SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
//坐标框架
CoordinateReferenceSystem sourceCRS = CRS.decode(authority);
tb.setCRS(sourceCRS);
tb.setName("shapefile");
if (GEOMETRY_TYPE_POINT.equals(geoType)) {
tb.add("the_geom", Point.class);
} else if (GEOMETRY_TYPE_MULTIPOINT.equals(geoType)) {
tb.add("the_geom", MultiPoint.class);
} else if (GEOMETRY_TYPE_LINESTRING.equals(geoType)) {
tb.add("the_geom", LineString.class);
} else if (GEOMETRY_TYPE_MULTILINESTRING.equals(geoType)) {
tb.add("the_geom", MultiLineString.class);
}else if (GEOMETRY_TYPE_POLYGON.equals(geoType)) {
tb.add("the_geom", Polygon.class);
} else if (GEOMETRY_TYPE_MULTIPOLYGON.equals(geoType)) {
tb.add("the_geom", MultiPolygon.class);
} else {
throw new Exception("Geometry中没有该类型:" + geoType);
}
ds.createSchema(tb.buildFeatureType());
//设置编码
Charset charset = Charset.forName(encode);
ds.setCharset(charset);
//设置Writer
FeatureWriter writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
for (Geometry geom : geoms) {
//String type = geom.getGeometryType();
SimpleFeature feature = writer.next();
feature.setAttribute("the_geom", geom);
}
writer.write();
writer.close();
ds.dispose();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void write2Shape(String shpPath, String encode, String prjWKT, String geoType, String shpKey, List attrKeys, List