1
        Gdal.AllRegister();
         //为了支持中文路径,请添加下面这句代码
        OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
         //为了使属性表字段支持中文,请添加下面这句
        OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "");
        Ogr.RegisterAll();
        OSGeo.OGR.Driver outDriver = Ogr.GetDriverByName("GeoJSON");
        OSGeo.OGR.DataSource outDataSource =    outDriver.CreateDataSource("aaa.geojson",null);
        DataSource pnn3 = Ogr.Open("MSSQL:server=192.168.1.9,1433;database=KS_DataBase;UId=sa;PWD=sa", 0);

         OSGeo.OGR.Layer layer = pnn3.ExecuteSQL("select * from KS_HXStation", null, "");
        OSGeo.OGR.Feature f;
        layer.ResetReading();

I don't know how to take the mssql spatial data transfer Geojson is.

2 Answers 2

1
string sqlConn = "MSSQL:server=[IP];database=[DB];uid=[id]; pwd=[pwd];tables=[tablName];";
string filePath = $@"D:\shp\shpFile.shp";

// 註冊所有的驅動 
Ogr.RegisterAll();

// in order to support chinese path
//OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");

// in order to support chinese attribute (CP950 for big5, CP936 for GB)
// reference: http://blog.xuite.net/ebeaoi/beast/9836928-%E7%B7%A8%E7%A2%BC%E5%95%8F%E9%A1%8C--charset%E5%8F%8Acodepage
Gdal.SetConfigOption("SHAPE_ENCODING", "CP950 | CP936");

// auto dispose DataSource
using (DataSource ds = Ogr.Open(sqlConn, 0))
{
    // load first layer
    Layer oLayer = ds.GetLayerByIndex(0);

    // output file type
    var outDriver = Ogr.GetDriverByName("ESRI Shapefile");

    // auto dispose DataSource
    using (var targetDataSet = outDriver.CreateDataSource(filePath, null))
    {
        targetDataSet.CopyLayer(oLayer, oLayer.GetName(), null);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0
var driver = Ogr.GetDriverByName("GeoJSON");
var trgPath = "";  //export geojson destination path
var trgDataset = driver.CreateDataSource(trgPath, null);
trgDataset.CopyLayer(layer, layer.GetName(), null);
trgDataset.Dispose();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.