其他分享
首页 > 其他分享> > Gdal 之 dxf转geojson (附加坐标转换)

Gdal 之 dxf转geojson (附加坐标转换)

作者:互联网

问题产生


思路


code

这里只记录一下demo,没有对坐标参数,dto等进行封装

GdalBase.ConfigureAll();
Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");//支持中文路径
Gdal.SetConfigOption("DXF_ENCODING", "UTF-8");

//创建北京54坐标系
var bj54 = new SpatialReference("");
bj54.ImportFromEPSG(4214);//4214
var clong = 121 + 35 / 60.0;
bj54.SetTM(0, clong, 2.0, 50825, -342129);
bj54.SetTOWGS84(-143.309, -238.731, -83.562, -3.49779, 4.016604, -3.591758, -4.44782521);

var wgs84 = new SpatialReference("");
wgs84.ImportFromEPSG(4326);

var wgs84Geo = wgs84.CloneGeogCS();

//创建转换
var coordinateTransformation = new CoordinateTransformation(bj54, wgs84Geo);

//读取文件转化为DataSource
var ds = Ogr.Open(filePath, 0);
if (ds == null)
{
    Console.WriteLine("打开文件失败");
    return;
}

var extfile = "test.geojson";

if (File.Exists(extfile))
    File.Delete(extfile);

//获取geojson驱动
var geojsonDriver = Ogr.GetDriverByName("GeoJSON");
var geojsonDataSource = geojsonDriver.CreateDataSource(extfile, null);
var geojsonLayer = geojsonDataSource.CreateLayer("JZD", wgs84Geo, wkbGeometryType.wkbUnknown, null);

//读取第一个图层,这个图层不是cad中的layer
var oLayer = ds.GetLayerByIndex(0);
if (oLayer == null)
{
    Console.WriteLine("layer 获取失败");
    return;
}

//设置cad layer 筛选
//oLayer.SetAttributeFilter("Layer = \'JZD\'");

Feature feature;
while ((feature = oLayer.GetNextFeature()) != null)
{
    //获取空间属性
    var geometry = feature.GetGeometryRef();

    //转换
    geometry.Transform(coordinateTransformation);

    //注意,转换为后时 纬度 经度的顺序 ,如果需要经度在前,需要交换xy
    geometry.SwapXY();
    geometry.CloseRings();
    var ret = geojsonLayer.CreateFeature(feature);
}

标签:bj54,extfile,feature,geojson,var,dxf,null,Gdal,cad
来源: https://www.cnblogs.com/chilli-with-fish/p/15151854.html