赞
踩
目录
最近的项目遇到一个需要导出点、线、面、表导出gdb的业务需求,经过思考比较选择了使用GDAL,并且为了便于后续使用,基于注解反射编写了通用的数据导出工具类,只需要将自定义的注解添加在实体字段上便可实现对字段的导出数设置,使用非常便捷。对GIS中常用的数据格式gdb和shapefile进行了验证,二者导出的使用方式相同,并且完美实现字段名、字段类型、字段长度、别名、是否允许空值等属性的控制,下面来看导出效果和工具类代码。
工具类源代码下载:https://download.csdn.net/download/tylkhx/85196838
通过注解来指定输出图层的字段信息
-
- @Target(ElementType.FIELD)
- @Retention(RetentionPolicy.RUNTIME)
- public @interface GdalExportField {
- String name() default "";
- int type() default ogr.OFTString;
- int length() default 100;
- String alternativeName() default "";
- boolean nullAble() default true;
- String defaultValue() default "";
- int subType() default 0;
- boolean isGeom() default false;
- }
创建输出数据的数据源
- public static DataSource createDataSource(String savePath, String driverName){
- if (StringUtil.isEmpty(savePath) || StringUtil.isEmpty(driverName)){
- return null;
- }
- Driver driverOut = ogr.GetDriverByName(driverName);
- return driverOut.CreateDataSource(savePath);
- }
将java内的几何由wkt格式转换成GDAL几何对象
- public static Geometry createGeometry(String wkt){
- if (StringUtil.isEmpty(wkt)){
- return null;
- }
- return Geometry.CreateFromWkt(wkt);
- }
将要素添加到输出图层的核心方法
- public static void setFeature(Layer layer, List<Object> dataList, Class dataClass){
- if (layer == null || dataClass == null){
- return;
- }
-
- Field[] fields = dataClass.getDeclaredFields();
- if (fields == null || fields.length == 0){
- return;
- }
- for (int i=0;i<fields.length;i++){
- Field fieldClass = fields[i];
- fieldClass.setAccessible(true);
- if (fieldClass.isAnnotationPresent(GdalExportField.class)){
- GdalExportField gdalExportField = fieldClass.getAnnotation(GdalExportField.class);
- if (gdalExportField.isGeom()){
- continue;
- }
- String fieldClassName = fieldClass.getName();
- if (StringUtil.notEmpty(gdalExportField.name())){
- fieldClassName = gdalExportField.name();
- }
- FieldDefn fieldDefn = new FieldDefn();
- fieldDefn.SetName(fieldClassName);
- fieldDefn.SetType(gdalExportField.type());
- fieldDefn.SetWidth(gdalExportField.length());
- String alternativeName = gdalExportField.alternativeName();
- if (StringUtil.isEmpty(alternativeName)){
- alternativeName = fieldClassName;
- }
- fieldDefn.SetAlternativeName(alternativeName);
- if (!gdalExportField.nullAble()){
- fieldDefn.SetNullable(0);
- }
- if (StringUtil.notEmpty(gdalExportField.defaultValue())){
- fieldDefn.SetDefault(gdalExportField.defaultValue());
- }
- fieldDefn.SetSubType(gdalExportField.subType());
- layer.CreateField(fieldDefn);
- }
- }
-
- if (dataList == null || dataList.isEmpty()){
- return;
- }
- FeatureDefn featureDefn = layer.GetLayerDefn();
- for (Object o:dataList){
- Feature feature = new Feature(featureDefn);
- for (int k=0;k<fields.length;k++){
- Field field = fields[k];
- if (field.isAnnotationPresent(GdalExportField.class)){
- GdalExportField gdalField = field.getAnnotation(GdalExportField.class);
- String fieldName = field.getName();
- if (StringUtil.notEmpty(gdalField.name())){
- fieldName = gdalField.name();
- }
- int filedType = gdalField.type();
- Object fieldValue = null;
- try {
- fieldValue = field.get(o);
- }catch (Exception e){
- e.printStackTrace();
- }
- if (fieldValue == null){
- continue;
- }
- if (gdalField.isGeom()){
- if (layer.GetGeomType() == ogr.wkbNone){
- continue;
- }
- Geometry geometry = null;
- if (TYPE_GADL_Geometry.equals(fieldValue.getClass().getName())){
- geometry = (Geometry) fieldValue;
- }else {
- geometry = createGeometry(fieldValue.toString());
- }
- if (geometry == null){
- continue;
- }
- feature.SetGeometry(geometry);
- }else if (filedType == ogr.OFTDate || filedType == ogr.OFTDateTime){
- Date date = (Date) fieldValue;
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- int year = calendar.get(Calendar.YEAR);
- int month = calendar.get(Calendar.MONTH)+1;
- int day = calendar.get(Calendar.DATE);
- int hour = calendar.get(Calendar.HOUR_OF_DAY);
- int minute = calendar.get(Calendar.MINUTE);
- float second = calendar.get(Calendar.SECOND);
- feature.SetField(fieldName,year,month,day,hour,minute,second,0);
- }else if (filedType == ogr.OFTInteger){
- feature.SetField(fieldName,String.valueOf(fieldValue));
- }else if (filedType == ogr.OFTReal){
- feature.SetField(fieldName,Double.valueOf(String.valueOf(fieldValue)));
- }else if (filedType == ogr.OFTString){
- feature.SetField(fieldName,String.valueOf(fieldValue));
- }else if (filedType == ogr.OFTBinary){
- feature.SetField(fieldName,String.valueOf(fieldValue));
- }
- }
- }
- layer.CreateFeature(feature);
- }
- }
在LandDTO的字段中要添加GdalExportField注解
- public class LandDTO {
- @GdalExportField(length = 19)
- private String ZDDM; // 宗地代码
- @GdalExportField(length = 50,name = "ZDMC",alternativeName = "宗地名称",nullAble = false)
- private String name; // 宗地名称
- @GdalExportField(length = 50,type = 2)
- private Double ZDMJ; // 宗地面积
- @GdalExportField(isGeom = true)
- private Geometry geom;
-
- public String getZDDM() {
- return ZDDM;
- }
-
- public void setZDDM(String ZDDM) {
- this.ZDDM = ZDDM;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public Double getZDMJ() {
- return ZDMJ;
- }
-
- public void setZDMJ(Double ZDMJ) {
- this.ZDMJ = ZDMJ;
- }
-
- public Geometry getGeom() {
- return geom;
- }
-
- public void setGeom(Geometry geom) {
- this.geom = geom;
- }
- }
- public static void main(String args[]) throws IOException {
- List<LandDTO> landDTOList = new ArrayList<>();
- LandDTO landDTO = new LandDTO();
- landDTO.setZDDM("542145210001");
- landDTO.setName("张三");
- landDTO.setZDMJ(135.24);
- landDTO.setGeom(createGeometry("Polygon((2 2,6 2,8 8,2 6,2 2))"));
- landDTOList.add(landDTO);
-
- LandDTO landDTO2 = new LandDTO();
- landDTO2.setZDDM("542145210002");
- landDTO2.setName("李四");
- landDTO2.setZDMJ(232.94);
- landDTO2.setGeom(createGeometry("Polygon((12 12,16 12,18 18,12 16,12 12))"));
- landDTOList.add(landDTO2);
-
- SpatialReference sr = new SpatialReference();
- sr.ImportFromEPSG(4326);
- String saveGdbPath = "C:/Users/dl/Desktop/新建文件夹/测试输出gdb.gdb";
- DataSource dataSourceGDB = GdalUtil.createDataSource(saveGdbPath, "FileGDB");
- Layer gdbLayer = dataSourceGDB.CreateLayer("ZDXX", sr, ogr.wkbPolygon, null);
- GdalUtil.setFeature(gdbLayer,(List) landDTOList,LandDTO.class);
-
- Layer gdbTable = dataSourceGDB.CreateLayer("ZDB", sr, ogr.wkbNone, null);
- GdalUtil.setFeature(gdbTable,(List) landDTOList,LandDTO.class);
- dataSourceGDB.SyncToDisk();
- dataSourceGDB.FlushCache();
- dataSourceGDB.delete();
- String saveShpPath = "C:/Users/dl/Desktop/新建文件夹/测试输出shp.shp";
- DataSource dataSourceShp = GdalUtil.createDataSource(saveShpPath, "ESRI Shapefile");
- Layer ShpLayer = dataSourceShp.CreateLayer("ZDXX", sr, ogr.wkbPolygon, null);
- GdalUtil.setFeature(ShpLayer,(List) landDTOList,LandDTO.class);
- dataSourceShp.SyncToDisk();
- dataSourceShp.FlushCache();
- dataSourceShp.delete();
- }
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方法有示例
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。