public static void main(String[] args) throws SQLException, IOException {
String tableName="routinemonitor.t_bas_grid_layer";
List<String> grid_fields = Arrays.asList("the_geom", "gd_code", "region");
List<Integer> grid_fields_location = Arrays.asList(0, 1, 2);
String shpFilePath="G:\\桌面2020-06-23\\北戴河\\2020-10-27\\北戴河项目更换数据\\北戴河grid.shp";
String sqlFileName="网格图层数据";
ShpToSql.shpConvertSql(tableName, grid_fields, grid_fields_location, shpFilePath, sqlFileName);
}
private static String CHAR_ENCODEING="utf-8";
/**
* 将shp文件生成sql语句
* @param tableName 需要入库的表格名称
* @param tableField 需要入库的表格属性名称集合
* @param fieldLocation 需要入库shp文件:入库表格数据对应shp文件的位置
* @param shpFilePath 需要入库shp文件的绝对路径
* @param sqlFileName 生成sql文件名称
* @throws IOException
*/
private static void shpConvertSql(String tableName, List<String> tableField, List<Integer> fieldLocation, String shpFilePath, String sqlFileName) throws IOException {
StringBuffer field = new StringBuffer();
tableField.stream().forEach(s -> {
field.append(s+",");
});
ShapefileDataStore sds = (ShapefileDataStore)new ShapefileDataStoreFactory().createDataStore(new File(shpFilePath).toURI().toURL());
sds.setCharset(Charset.forName(CHAR_ENCODEING));
SimpleFeatureIterator itertor = sds.getFeatureSource().getFeatures().features();
StringBuffer sql = new StringBuffer();
while(itertor.hasNext()){
SimpleFeature feature = itertor.next();
sql.append("insert into "+tableName+"("+field.substring(0, field.length()-1)+")values(");
for (Integer integer : fieldLocation) {
sql.append("'"+ feature.getAttributes().get(integer).toString()+"',");
}
sql=new StringBuffer(sql.substring(0, sql.length()-1));
sql.append(");\n");
System.out.println(sql.toString());
}
itertor.close();
UserLogUtils.writeToLog(sqlFileName, sql.toString());
}
shp文件生成入库sql语句
于 2020-10-28 16:44:36 首次发布