当前位置:   article > 正文

java 调用gdal读取gdb数据_javagdal读gbd

javagdal读gbd

1.下载gdal

链接: link.

2.配置gdal环境

将解压目录下bin目录下的dll文件全部复制到java安装路径的bin目录下:C:\Program Files\Java\jdk1.8.0_171\bin
在这里插入图片描述
在这里插入图片描述
并将bin/gdal/java下的dll调用文件同样复制到jdk环境下C:\Program Files\Java\jdk1.8.0_171\bin
在这里插入图片描述
最后配置环境
在这里插入图片描述

3.导入jar包和pom

添加lib包
在这里插入图片描述
下面展示一些 内联代码片

<dependency>
            <groupId>org.gdal</groupId>
            <artifactId>gdal</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/gdal.jar</systemPath>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4.代码

最后导入代码

下面展示一些 内联代码片

=

package com.epf.zjgg.utils;


import com.epf.zjgg.dto.LayerDto;
import org.apache.commons.lang.StringUtils;
import org.gdal.gdal.gdal;
import org.gdal.ogr.*;

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

/**
 * gdal读取gdb示例
 */
public class GdbUtils {
    static {
        gdal.AllRegister();
        //        配置GDAL_DATA路径
        gdal.SetConfigOption("GDAL_DATA", "F:\\BaiduNetdiskDownload\\release-1900-x64-gdal-2-4-4-mapserver-7-4-3\\bin\\gdal-data");
        gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
        //        属性表字段支持中文
        gdal.SetConfigOption("SHAPE_ENCODING", "");
    }

    public static LayerDto getLayerDto(String FirePath){
        Driver driver = ogr.GetDriverByName("OpenFileGDB");
        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 = layer.GetNextFeature();
                        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)
    };
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/456513
推荐阅读
相关标签
  

闽ICP备14008679号