当前位置:   article > 正文

java 利用gdal,将shpfile转为gdb的图层_gdal java gdbdriver.createdatasource

gdal java gdbdriver.createdatasource

简要说明

java开发中,利用gdal将shp转为gdb的一个图层

maven依赖

		<!--需要安装完gdal后,本地install gdal包才能使用 -->
		<!--gdal安装可参考 https://blog.csdn.net/qq_41613913/article/details/135743562 -->
		<dependency>
            <groupId>org.gdal</groupId>
            <artifactId>gdal</artifactId>
            <version>3.7.3</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

样例代码

public static void main(String[] args) {
        // 注册所有的驱动
        ogr.RegisterAll();
        // 为了支持中文路径,请添加下面这句代码
        gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","YES");
        // 为了使属性表字段支持中文,请添加下面这句
        gdal.SetConfigOption("SHAPE_ENCODING","CP936");

        String strVectorFile = "D:\\shp\\xxx.shp";
        //打开数据
        DataSource ds = ogr.Open(strVectorFile,0);
        Layer layer = ds.GetLayer(0);
        String authority = ds.GetLayer(0).GetSpatialRef().GetAttrValue("AUTHORITY", 1);
        System.out.println("坐标系: " + authority);
        if (ds == null)
        {
            System.out.println("打开文件失败!" );
            return;
        }
        //
        String layerName = layer.GetName();
        System.out.println("图层名称:"+layerName);

        SpatialReference spatialReference = layer.GetSpatialRef();
        //System.out.println(spatialReference);
        System.out.println("空间参考坐标系:"+spatialReference.GetAttrValue("AUTHORITY",0)+spatialReference.GetAttrValue("AUTHORITY",1));

        FeatureDefn featureDefn = layer.GetLayerDefn();
        int fieldCount = featureDefn.GetFieldCount();

        Driver gdbDriver = ogr.GetDriverByName("OpenFileGDB");
        if (gdbDriver == null)
        {
            System.out.println("打开驱动失败!" );
            return;
        }
        String gdbFileName = "D:\\tmp\\gdb\\xxxx.gdb";
        DataSource dataSourceGDB = gdbDriver.CreateDataSource(gdbFileName);
        int geomtryType = layer.GetGeomType();
        int srid = Integer.valueOf(layer.GetSpatialRef().GetAttrValue("AUTHORITY", 1));
        SpatialReference spatialReference2 = new SpatialReference();
        spatialReference2.ImportFromEPSG(srid);
        Layer gdbLayer = dataSourceGDB.CreateLayer("图层名称", spatialReference2, geomtryType, null);
        for(int i=0; i<fieldCount; i++){
            FieldDefn fieldDefn = featureDefn.GetFieldDefn(i);
            gdbLayer.CreateField(fieldDefn);
        }
        Feature feature = null;
        //针对fid等于0的情况做处理
        while ((feature = layer.GetNextFeature()) != null)
        {
            feature.SetFID(1 + feature.GetFID());
            gdbLayer.CreateFeature(feature);
        }
        
        dataSourceGDB.delete();
        gdbDriver.delete();
    }
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/456481
推荐阅读
相关标签
  

闽ICP备14008679号