Gdal关于CAD转SHP格式

package cn.atide;

import org.gdal.gdal.gdal;
import org.gdal.ogr.DataSource;
import org.gdal.ogr.Driver;
import org.gdal.ogr.Layer;
import org.gdal.ogr.ogr;
import org.junit.Test;
/**
 * 读取cad数据
 * @author Administrator
 *
 */
public class ReadDxf {
	
	@Test
    public void test() {
        // 注册所有的驱动
        ogr.RegisterAll();
        gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","YES");
        gdal.SetConfigOption("SHAPE_ENCODING","");
        String strVectorFile = "E:\\gdalTest\\output\\财信.渝中城项目5#楼竣工.dxf";
        DataSource ds = ogr.Open(strVectorFile,0);
        if (ds == null)
        {
            System.out.println("打开文件失败!" );
            return;
        }
        System.out.println("打开文件成功!" );
        int iLayerCount = ds.GetLayerCount();
        Layer oLayer = ds.GetLayerByIndex(0);
        String name = oLayer.GetName();
        if(oLayer == null){
        	System.out.println("获取失败");
        	return ;
        }
        oLayer.ResetReading();
        long featureCount = oLayer.GetFeatureCount();
        //Driver dv = ogr.GetDriverByName("GeoJSON");
        Driver dv = ogr.GetDriverByName("ESRI Shapefile");
        String extfile = "D:\\rect_field_demo.shp";
        DataSource dataSource = dv.CreateDataSource(extfile);
        dv.CopyDataSource(ds, extfile);
        //dataSource.CreateLayer(name)
        /*if (dv == null)
        {
            System.out.println("打开驱动失败!" );
            return; 
        }
        System.out.println("打开驱动成功!" );
        dv.CopyDataSource(ds, "E:\\gdalTest\\output\\node.json");
        System.out.println("转换成功!" );*/
    }
}


daShapefile = r"C:\Temp\Voting_Centers_and_Ballot_Sites.shp"

driver = ogr.GetDriverByName('ESRI Shapefile')
#需要用到驱动器
dataSource = driver.Open(daShapefile, 0) # 0 表示只读. 1 表示可写.

# 检测文件是否存在
if dataSource is None:
    print 'Could not open %s' % (daShapefile)
else:
    print 'Opened %s' % (daShapefile)
    layer = dataSource.GetLayer()
    featureCount = layer.GetFeatureCount()
    print "Number of features in %s: %d" % (os.path.basename(daShapefile),featureCount)

            
转载自:https://blog.csdn.net/wt346326775/article/details/70841323

You may also like...