【java 调用gdal读取gdb、shp、mdb数据】

代码

package com.epf.gdbh.exam.testGeo;

import org.gdal.gdal.gdal;
import org.gdal.ogr.*;

import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

public class GdbUtils {
    static {
        gdal.AllRegister();
        //        配置GDAL_DATA路径
        gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
        //        属性表字段支持中文
        gdal.SetConfigOption("SHAPE_ENCODING", "");
    }

    public static LayerDto getLayerDto(String FirePath){
        //Driver driver = ogr.GetDriverByName("OpenFileGDB"); 
        Driver driver = ogr.GetDriverByName("ESRI Shapefile");//文件驱动名称
        if (driver == null) {
            return null;
        }
        List<Map> list = new ArrayList<>();
        List<Map<String,String>> list1 =new ArrayList<>();
        LayerDto layerDto = new LayerDto();
        DataSource dataSource = null;
        try{
            dataSource = driver.Open(FirePath, 0);
            int num = dataSource.GetLayerCount();
            for (int i = 0; i < num; i++) {
                //  获取图层
                Layer layer = dataSource.GetLayer(i);
                String strlayerName = layer.GetName();  //表名称
                // 获取图层要数个数
                long count = layer.GetFeatureCount();//获取总条数
                if (0!=count){
                    do {//获取图层下的要素 一个图层下有多个Feature 
                        Feature feature = layer.GetNextFeature();
                        System.out.println(feature);
                        if (null == feature) {
                            break;
                        }
                        //获取边界坐标
                        Geometry geometry = feature.GetGeometryRef();
                        if (geometry!=null){
                            String geometryJson = geometry.ExportToJson();
                            String str = geometryJson.substring(geometryJson.lastIndexOf(":") + 1, geometryJson.length());
                            if (!" [ [ ] ] }".equals(str)){
                                String s1 = str.substring(7, str.length() - 8);
                                String[] split = s1.replaceAll(" ", "").replaceAll("\\[","").replaceAll("]","").split(",");
                                List<String> xList = new ArrayList();
                                List<String> yList = new ArrayList();
                                Map<String,String> map1 = new HashMap();
                                for (int j = 0; j < split.length; j++) {
                                    if (j!=(split.length-1)&&j%2==0) {
                                        map1.put(split[j], split[j + 1]);
                                    }
                                }
                                list1.add(map1);
                            }
                        }
                        Map map = new HashMap();
                        //获取属性
                        for (int p = 0; p < feature.GetFieldCount(); p++) {
                            map.put(feature.GetFieldDefnRef(p).GetName(),getProperty(feature, p));
                        }
                        list.add(map);
                        feature.delete();
                    } while (true);
                }
                layerDto.setStrlayerName(strlayerName);
                layerDto.setList(list);
                layerDto.setCount(count);
                layerDto.setList1(list1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(dataSource != null){
                dataSource.delete();
            }
        }
        return layerDto;
    }

    private static Object getProperty(Feature feature, int index) {
        int type = feature.GetFieldType(index);
        PropertyGetter propertyGetter;
        if (type < 0 || type >= propertyGetters.length) {
            propertyGetter = stringPropertyGetter;
        } else {
            propertyGetter = propertyGetters[type];
        }
        try {
            return propertyGetter.get(feature, index);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 属性获取器
     */
    @FunctionalInterface
    private interface PropertyGetter {
        Object get(Feature feature, int index);
    }

    private static final PropertyGetter stringPropertyGetter = (feature, index) -> feature.GetFieldAsString(index);

    /**
     * feature.GetFieldType(index)得到一个属性类型的int值,该值对应具体类型
     */
    private static final PropertyGetter[] propertyGetters = new PropertyGetter[]{
            (feature, index) -> feature.GetFieldAsInteger(index),//0	Integer
            (feature, index) -> feature.GetFieldAsIntegerList(index),//1	IntegerList
            (feature, index) -> feature.GetFieldAsDouble(index),//2	Real
            (feature, index) -> feature.GetFieldAsDoubleList(index),//3	RealList
            stringPropertyGetter,//4	String
            (feature, index) -> feature.GetFieldAsStringList(index),//5	StringList
            stringPropertyGetter,//6	(unknown)
            stringPropertyGetter,//7	(unknown)
            (feature, index) -> feature.GetFieldAsBinary(index),//8	Binary
            (feature, index) -> {
                int[] pnYear = new int[1];
                int[] pnMonth = new int[1];
                int[] pnDay = new int[1];
                int[] pnHour = new int[1];
                int[] pnMinute = new int[1];
                float[] pfSecond = new float[1];
                int[] pnTZFlag = new int[1];
                feature.GetFieldAsDateTime(index, pnYear, pnMonth, pnDay, pnHour, pnMinute, pfSecond, pnTZFlag);
                java.sql.Date date = java.sql.Date.valueOf(LocalDate.of(pnYear[0], pnMonth[0], pnDay[0]));
                return date;
            },//9	Date
            (feature, index) -> {
                int[] pnYear = new int[1];
                int[] pnMonth = new int[1];
                int[] pnDay = new int[1];
                int[] pnHour = new int[1];
                int[] pnMinute = new int[1];
                float[] pfSecond = new float[1];
                int[] pnTZFlag = new int[1];
                feature.GetFieldAsDateTime(index, pnYear, pnMonth, pnDay, pnHour, pnMinute, pfSecond, pnTZFlag);
                float fSecond = pfSecond[0];
                int s = (int) fSecond;
                int ns = (int) (1000000000 * fSecond - s);
                Time time = Time.valueOf(LocalTime.of(pnHour[0], pnMinute[0], s, ns));
                return time;
            },// 10	Time
            (feature, index) -> {
                int[] pnYear = new int[1];
                int[] pnMonth = new int[1];
                int[] pnDay = new int[1];
                int[] pnHour = new int[1];
                int[] pnMinute = new int[1];
                float[] pfSecond = new float[1];
                int[] pnTZFlag = new int[1];
                feature.GetFieldAsDateTime(index, pnYear, pnMonth, pnDay, pnHour, pnMinute, pfSecond, pnTZFlag);
                float fSecond = pfSecond[0];
                int s = (int) fSecond;
                int ns = (int) (1000000000 * fSecond - s);
                LocalDateTime localDateTime = LocalDateTime.of(
                        LocalDate.of(pnYear[0], pnMonth[0], pnDay[0]),
                        LocalTime.of(pnHour[0], pnMinute[0], s, ns)
                );
                Timestamp timestamp = Timestamp.valueOf(localDateTime);
                return timestamp;
            },//11	DateTime
            (feature, index) -> feature.GetFieldAsInteger64(index),//12	Integer64
            (feature, index) -> feature.GetFieldAsIntegerList(index),//13 Integer64List
            //>=14	(unknown)
    };
}

通过Feature 获取属性和值

package com.epf.gdbh.tool.geo;


import org.gdal.ogr.*;

import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

public class FeatureUtils {

    /**
     * @Author: huangcheng
     * @Date: 2022/7/4 16:07
     * @Description: 通过单个feature获取字段或值
     * @param feature
     * @return: List<Map>
     */
    public static List<Map> getLayerFeature(Feature feature){
        List<Map> list = new ArrayList<>();
        Map map = new HashMap();
        for (int p = 0; p < feature.GetFieldCount(); p++) {
            map.put(feature.GetFieldDefnRef(p).GetName(),getProperty(feature, p));
        }
        list.add(map);
        return list;
    }

    /**
    * @Author: huangcheng
    * @Date: 2022/7/4 16:31
    * @Description: 通过List<Feature>获取字段或值
    * @param features
    * @return:  List<Map>
    */
    public static List<Map> getLayerFeatures(List<Feature> features) {
        List<Map> list = new ArrayList<>();
        Map map = new HashMap();
        for (Feature feature :features) {
            for (int p = 0; p < feature.GetFieldCount(); p++) {
                map.put(feature.GetFieldDefnRef(p).GetName(),getProperty(feature, p));
            }
            list.add(map);
        }
        return list;
    }

    private static Object getProperty(Feature feature, int index) {
        int type = feature.GetFieldType(index);
        PropertyGetter propertyGetter;
        if (type < 0 || type >= propertyGetters.length) {
            propertyGetter = stringPropertyGetter;
        } else {
            propertyGetter = propertyGetters[type];
        }
        try {
            return propertyGetter.get(feature, index);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 属性获取器
     */
    @FunctionalInterface
    private interface PropertyGetter {
        Object get(Feature feature, int index);
    }

    private static final PropertyGetter stringPropertyGetter = (feature, index) -> feature.GetFieldAsString(index);

    /**
     * feature.GetFieldType(index)得到一个属性类型的int值,该值对应具体类型
     */
    private static final PropertyGetter[] propertyGetters = new PropertyGetter[]{
            (feature, index) -> feature.GetFieldAsInteger(index),//0	Integer
            (feature, index) -> feature.GetFieldAsIntegerList(index),//1	IntegerList
            (feature, index) -> feature.GetFieldAsDouble(index),//2	Real
            (feature, index) -> feature.GetFieldAsDoubleList(index),//3	RealList
            stringPropertyGetter,//4	String
            (feature, index) -> feature.GetFieldAsStringList(index),//5	StringList
            stringPropertyGetter,//6	(unknown)
            stringPropertyGetter,//7	(unknown)
            (feature, index) -> feature.GetFieldAsBinary(index),//8	Binary
            (feature, index) -> {
                int[] pnYear = new int[1];
                int[] pnMonth = new int[1];
                int[] pnDay = new int[1];
                int[] pnHour = new int[1];
                int[] pnMinute = new int[1];
                float[] pfSecond = new float[1];
                int[] pnTZFlag = new int[1];
                feature.GetFieldAsDateTime(index, pnYear, pnMonth, pnDay, pnHour, pnMinute, pfSecond, pnTZFlag);
                java.sql.Date date = java.sql.Date.valueOf(LocalDate.of(pnYear[0], pnMonth[0], pnDay[0]));
                return date;
            },//9	Date
            (feature, index) -> {
                int[] pnYear = new int[1];
                int[] pnMonth = new int[1];
                int[] pnDay = new int[1];
                int[] pnHour = new int[1];
                int[] pnMinute = new int[1];
                float[] pfSecond = new float[1];
                int[] pnTZFlag = new int[1];
                feature.GetFieldAsDateTime(index, pnYear, pnMonth, pnDay, pnHour, pnMinute, pfSecond, pnTZFlag);
                float fSecond = pfSecond[0];
                int s = (int) fSecond;
                int ns = (int) (1000000000 * fSecond - s);
                Time time = Time.valueOf(LocalTime.of(pnHour[0], pnMinute[0], s, ns));
                return time;
            },// 10	Time
            (feature, index) -> {
                int[] pnYear = new int[1];
                int[] pnMonth = new int[1];
                int[] pnDay = new int[1];
                int[] pnHour = new int[1];
                int[] pnMinute = new int[1];
                float[] pfSecond = new float[1];
                int[] pnTZFlag = new int[1];
                feature.GetFieldAsDateTime(index, pnYear, pnMonth, pnDay, pnHour, pnMinute, pfSecond, pnTZFlag);
                float fSecond = pfSecond[0];
                int s = (int) fSecond;
                int ns = (int) (1000000000 * fSecond - s);
                LocalDateTime localDateTime = LocalDateTime.of(
                        LocalDate.of(pnYear[0], pnMonth[0], pnDay[0]),
                        LocalTime.of(pnHour[0], pnMinute[0], s, ns)
                );
                Timestamp timestamp = Timestamp.valueOf(localDateTime);
                return timestamp;
            },//11	DateTime
            (feature, index) -> feature.GetFieldAsInteger64(index),//12	Integer64
            (feature, index) -> feature.GetFieldAsIntegerList(index),//13 Integer64List
            //>=14	(unknown)
    };
}

借鉴与:作者:墨白Aa:java 调用gdal读取gdb数据

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值