当前位置:   article > 正文

GDAL数据批量导出gdb、shapefile等,Java使用GDAL实现通用的数据导出工具类,gdb导出,shp导出,GDAL导出,几何数据导出,shapefile导出,java导出_java 导出gdb文件

java 导出gdb文件

目录

一、实现代码

1.字段注解GdalExportField

2.创建数据源

3.wkt转GDAL几何

4.设置要素核心方法

5.实体对象LandDTO

6.调用示例

二、数据导出效果

1.导出gdb效果

 2.导出shapefile效果

三、使用方法


最近的项目遇到一个需要导出点、线、面、表导出gdb的业务需求,经过思考比较选择了使用GDAL,并且为了便于后续使用,基于注解反射编写了通用的数据导出工具类,只需要将自定义的注解添加在实体字段上便可实现对字段的导出数设置,使用非常便捷。对GIS中常用的数据格式gdb和shapefile进行了验证,二者导出的使用方式相同,并且完美实现字段名、字段类型、字段长度、别名、是否允许空值等属性的控制,下面来看导出效果和工具类代码。

工具类源代码下载:https://download.csdn.net/download/tylkhx/85196838

一、实现代码

1.字段注解GdalExportField

通过注解来指定输出图层的字段信息

  1. @Target(ElementType.FIELD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. public @interface GdalExportField {
  4. String name() default "";
  5. int type() default ogr.OFTString;
  6. int length() default 100;
  7. String alternativeName() default "";
  8. boolean nullAble() default true;
  9. String defaultValue() default "";
  10. int subType() default 0;
  11. boolean isGeom() default false;
  12. }

2.创建数据源

创建输出数据的数据源

  1. public static DataSource createDataSource(String savePath, String driverName){
  2. if (StringUtil.isEmpty(savePath) || StringUtil.isEmpty(driverName)){
  3. return null;
  4. }
  5. Driver driverOut = ogr.GetDriverByName(driverName);
  6. return driverOut.CreateDataSource(savePath);
  7. }

3.wkt转GDAL几何

将java内的几何由wkt格式转换成GDAL几何对象

  1. public static Geometry createGeometry(String wkt){
  2. if (StringUtil.isEmpty(wkt)){
  3. return null;
  4. }
  5. return Geometry.CreateFromWkt(wkt);
  6. }

4.设置要素核心方法

将要素添加到输出图层的核心方法

  1. public static void setFeature(Layer layer, List<Object> dataList, Class dataClass){
  2. if (layer == null || dataClass == null){
  3. return;
  4. }
  5. Field[] fields = dataClass.getDeclaredFields();
  6. if (fields == null || fields.length == 0){
  7. return;
  8. }
  9. for (int i=0;i<fields.length;i++){
  10. Field fieldClass = fields[i];
  11. fieldClass.setAccessible(true);
  12. if (fieldClass.isAnnotationPresent(GdalExportField.class)){
  13. GdalExportField gdalExportField = fieldClass.getAnnotation(GdalExportField.class);
  14. if (gdalExportField.isGeom()){
  15. continue;
  16. }
  17. String fieldClassName = fieldClass.getName();
  18. if (StringUtil.notEmpty(gdalExportField.name())){
  19. fieldClassName = gdalExportField.name();
  20. }
  21. FieldDefn fieldDefn = new FieldDefn();
  22. fieldDefn.SetName(fieldClassName);
  23. fieldDefn.SetType(gdalExportField.type());
  24. fieldDefn.SetWidth(gdalExportField.length());
  25. String alternativeName = gdalExportField.alternativeName();
  26. if (StringUtil.isEmpty(alternativeName)){
  27. alternativeName = fieldClassName;
  28. }
  29. fieldDefn.SetAlternativeName(alternativeName);
  30. if (!gdalExportField.nullAble()){
  31. fieldDefn.SetNullable(0);
  32. }
  33. if (StringUtil.notEmpty(gdalExportField.defaultValue())){
  34. fieldDefn.SetDefault(gdalExportField.defaultValue());
  35. }
  36. fieldDefn.SetSubType(gdalExportField.subType());
  37. layer.CreateField(fieldDefn);
  38. }
  39. }
  40. if (dataList == null || dataList.isEmpty()){
  41. return;
  42. }
  43. FeatureDefn featureDefn = layer.GetLayerDefn();
  44. for (Object o:dataList){
  45. Feature feature = new Feature(featureDefn);
  46. for (int k=0;k<fields.length;k++){
  47. Field field = fields[k];
  48. if (field.isAnnotationPresent(GdalExportField.class)){
  49. GdalExportField gdalField = field.getAnnotation(GdalExportField.class);
  50. String fieldName = field.getName();
  51. if (StringUtil.notEmpty(gdalField.name())){
  52. fieldName = gdalField.name();
  53. }
  54. int filedType = gdalField.type();
  55. Object fieldValue = null;
  56. try {
  57. fieldValue = field.get(o);
  58. }catch (Exception e){
  59. e.printStackTrace();
  60. }
  61. if (fieldValue == null){
  62. continue;
  63. }
  64. if (gdalField.isGeom()){
  65. if (layer.GetGeomType() == ogr.wkbNone){
  66. continue;
  67. }
  68. Geometry geometry = null;
  69. if (TYPE_GADL_Geometry.equals(fieldValue.getClass().getName())){
  70. geometry = (Geometry) fieldValue;
  71. }else {
  72. geometry = createGeometry(fieldValue.toString());
  73. }
  74. if (geometry == null){
  75. continue;
  76. }
  77. feature.SetGeometry(geometry);
  78. }else if (filedType == ogr.OFTDate || filedType == ogr.OFTDateTime){
  79. Date date = (Date) fieldValue;
  80. Calendar calendar = Calendar.getInstance();
  81. calendar.setTime(date);
  82. int year = calendar.get(Calendar.YEAR);
  83. int month = calendar.get(Calendar.MONTH)+1;
  84. int day = calendar.get(Calendar.DATE);
  85. int hour = calendar.get(Calendar.HOUR_OF_DAY);
  86. int minute = calendar.get(Calendar.MINUTE);
  87. float second = calendar.get(Calendar.SECOND);
  88. feature.SetField(fieldName,year,month,day,hour,minute,second,0);
  89. }else if (filedType == ogr.OFTInteger){
  90. feature.SetField(fieldName,String.valueOf(fieldValue));
  91. }else if (filedType == ogr.OFTReal){
  92. feature.SetField(fieldName,Double.valueOf(String.valueOf(fieldValue)));
  93. }else if (filedType == ogr.OFTString){
  94. feature.SetField(fieldName,String.valueOf(fieldValue));
  95. }else if (filedType == ogr.OFTBinary){
  96. feature.SetField(fieldName,String.valueOf(fieldValue));
  97. }
  98. }
  99. }
  100. layer.CreateFeature(feature);
  101. }
  102. }

5.实体对象LandDTO

在LandDTO的字段中要添加GdalExportField注解

  1. public class LandDTO {
  2. @GdalExportField(length = 19)
  3. private String ZDDM; // 宗地代码
  4. @GdalExportField(length = 50,name = "ZDMC",alternativeName = "宗地名称",nullAble = false)
  5. private String name; // 宗地名称
  6. @GdalExportField(length = 50,type = 2)
  7. private Double ZDMJ; // 宗地面积
  8. @GdalExportField(isGeom = true)
  9. private Geometry geom;
  10. public String getZDDM() {
  11. return ZDDM;
  12. }
  13. public void setZDDM(String ZDDM) {
  14. this.ZDDM = ZDDM;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. public Double getZDMJ() {
  23. return ZDMJ;
  24. }
  25. public void setZDMJ(Double ZDMJ) {
  26. this.ZDMJ = ZDMJ;
  27. }
  28. public Geometry getGeom() {
  29. return geom;
  30. }
  31. public void setGeom(Geometry geom) {
  32. this.geom = geom;
  33. }
  34. }

6.调用示例

  1. public static void main(String args[]) throws IOException {
  2. List<LandDTO> landDTOList = new ArrayList<>();
  3. LandDTO landDTO = new LandDTO();
  4. landDTO.setZDDM("542145210001");
  5. landDTO.setName("张三");
  6. landDTO.setZDMJ(135.24);
  7. landDTO.setGeom(createGeometry("Polygon((2 2,6 2,8 8,2 6,2 2))"));
  8. landDTOList.add(landDTO);
  9. LandDTO landDTO2 = new LandDTO();
  10. landDTO2.setZDDM("542145210002");
  11. landDTO2.setName("李四");
  12. landDTO2.setZDMJ(232.94);
  13. landDTO2.setGeom(createGeometry("Polygon((12 12,16 12,18 18,12 16,12 12))"));
  14. landDTOList.add(landDTO2);
  15. SpatialReference sr = new SpatialReference();
  16. sr.ImportFromEPSG(4326);
  17. String saveGdbPath = "C:/Users/dl/Desktop/新建文件夹/测试输出gdb.gdb";
  18. DataSource dataSourceGDB = GdalUtil.createDataSource(saveGdbPath, "FileGDB");
  19. Layer gdbLayer = dataSourceGDB.CreateLayer("ZDXX", sr, ogr.wkbPolygon, null);
  20. GdalUtil.setFeature(gdbLayer,(List) landDTOList,LandDTO.class);
  21. Layer gdbTable = dataSourceGDB.CreateLayer("ZDB", sr, ogr.wkbNone, null);
  22. GdalUtil.setFeature(gdbTable,(List) landDTOList,LandDTO.class);
  23. dataSourceGDB.SyncToDisk();
  24. dataSourceGDB.FlushCache();
  25. dataSourceGDB.delete();
  26. String saveShpPath = "C:/Users/dl/Desktop/新建文件夹/测试输出shp.shp";
  27. DataSource dataSourceShp = GdalUtil.createDataSource(saveShpPath, "ESRI Shapefile");
  28. Layer ShpLayer = dataSourceShp.CreateLayer("ZDXX", sr, ogr.wkbPolygon, null);
  29. GdalUtil.setFeature(ShpLayer,(List) landDTOList,LandDTO.class);
  30. dataSourceShp.SyncToDisk();
  31. dataSourceShp.FlushCache();
  32. dataSourceShp.delete();
  33. }

二、数据导出效果

1.导出gdb效果

 2.导出shapefile效果

三、使用方法

1.在实体对象要导出的字段上添加GdalExportField注解,并在注解中指定字段名(如果不知道,则默认使用实体对象的字段名)、字段类型(默认字符串)、字段长度、别名、是否允许空值等信息。关注微行公众号:“GIS工具乐园”,持续更新开发技巧分享哦。

2.调用工具类的createDataSource()方法创建数据源(DataSource),创建数据源时指定要导出的数据格式:“FileGDB”为GDB,“ESRI Shapefile”为shapefile。

3.调用GDAL DataSource的自带方法CreateLayer()创建图层,在创建图层时指定图层名、图层类型等。

4.调用工具类的setFeature()方法将要导出的数据添加的图层中,该方法会创建要素,并反射获取GdalExportField注解的内容,设置到要素字段,并添加要素到图层中。

5.依次调用GDAL DataSource的自带方法SyncToDisk()、FlushCache()、 delete()方法将数据写出到磁盘

6.以上操作在工具类的main方法有示例

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/456488
推荐阅读
相关标签
  

闽ICP备14008679号