/// <summary>
    /// Method to ensure the static constructor is being called.
    /// </summary>
    /// <remarks>Be sure to call this function before using Gdal/Ogr/Osr</remarks>
    public static void ConfigureOgr()
    {
        if (_configuredOgr)
        {
            return;
        }

        // Register drivers
        Ogr.RegisterAll();
        _configuredOgr = true;
    }
Пример #2
0
    /// <summary>
    /// Method to ensure the static constructor is being called.
    /// </summary>
    /// <remarks>Be sure to call this function before using Gdal/Ogr/Osr</remarks>
    public static void ConfigureOgr()
    {
        MessageBox.Show("配置Ogr。。。。。");
        if (_configuredOgr)
        {
            return;
        }

        // Register drivers
        Ogr.RegisterAll();
        _configuredOgr = true;
    }
Пример #3
0
        private void runFunc()
        {
            Ogr.RegisterAll();
            Gdal.SetConfigOption("SHAPE_ENCODING", "CP936");

            string strInfo = string.Empty;
            //打开数据
            DataSource ds = Ogr.Open(mFileName, 0);

            if (ds == null)
            {
                strInfo += string.Format("打开文件【{0}】失败!\n", mFileName);
                return;
            }
            strInfo += string.Format("打开文件【{0}】成功!\n", mFileName);

            CGetShp mdbToShp = new CGetShp(mFileName, mOutPath);
            // 获取该数据源中的图层个数,一般shp数据图层只有一个,如果是mdb、dxf等图层就会有多个
            int iLayerCount = ds.GetLayerCount();

            strInfo += string.Format("个人地理数据库共包含{0}个图层!\n", iLayerCount);
            for (int i = 0; i < iLayerCount; i++)
            {
                // 获取第一个图层
                Layer oLayer = ds.GetLayerByIndex(i);

                if (oLayer == null)
                {
                    strInfo += string.Format("获取第{0}个图层失败!\n", i);
                    return;
                }
                strInfo += string.Format("第{0}个图层名称:{1}\n", i, oLayer.GetName());
                //若当前图层为向量数据
                if (oLayer.GetName().Contains("LINE"))
                {
                    //调用方法获取向量信息,并传入想应的点图层名以方便查询
                    mdbToShp.readLine(ref oLayer, oLayer.GetName().Replace("LINE", "POINT"));
                    //break;
                }
                else
                {
                    mdbToShp.readPoint(ref oLayer);
                    //break;
                    //mdbToShp.test(ref oLayer);
                }

                SetTextMessage(100 * (i + 1) / iLayerCount);
            }
        }
Пример #4
0
        private void WriteVectorFile(string strVectorFile)
        {
            SharpMap.GdalConfiguration.ConfigureGdal();
            SharpMap.GdalConfiguration.ConfigureOgr();
            // 为了支持中文路径,请添加下面这句代码
            OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
            // 为了使属性表字段支持中文,请添加下面这句
            OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "");
            Ogr.RegisterAll();

            string strDriverName = "ESRI Shapefile";
            Driver oDriver       = Ogr.GetDriverByName(strDriverName);

            if (oDriver == null)
            {
                MessageBox.Show("%s 驱动不可用!!\n", strVectorFile);
                return;
            }

            DataSource oDS = oDriver.CreateDataSource(strVectorFile, null);

            if (oDS == null)
            {
                MessageBox.Show("创建矢量文件【%s】失败!\n", strVectorFile);
                return;
            }

            Layer oLayer = oDS.CreateLayer("TestPolygon", null, wkbGeometryType.wkbPolygon, null);

            if (oLayer == null)
            {
                MessageBox.Show("图层创建失败!\n");
                return;
            }
            //FID
            FieldDefn oFieldID = new FieldDefn("FID", FieldType.OFTInteger);

            oLayer.CreateField(oFieldID, 1);

            //批次
            FieldDefn oPCMC = new FieldDefn("PCMC", FieldType.OFTString);

            oPCMC.SetWidth(100);
            oLayer.CreateField(oPCMC, 1);
            //地块号
            FieldDefn oDKH = new FieldDefn("DKH", FieldType.OFTString);

            oDKH.SetWidth(100);
            oLayer.CreateField(oDKH, 1);

            //日期
            FieldDefn oDate = new FieldDefn("Date", FieldType.OFTString);

            oDate.SetWidth(10);
            oLayer.CreateField(oDate, 1);

            //用地类型
            FieldDefn oType = new FieldDefn("Type", FieldType.OFTString);

            oType.SetWidth(10);
            oLayer.CreateField(oType, 1);

            FeatureDefn oDefn = oLayer.GetLayerDefn();
            int         index = 0;

            foreach (Data data in DataList)
            {
                index++;
                Feature oFeature = new Feature(oDefn);
                oFeature.SetField(0, index);
                oFeature.SetField(1, data.PCMC);
                oFeature.SetField(2, data.Dkh);
                oFeature.SetField(3, data.Date);
                oFeature.SetField(4, data.Type);
                Geometry geomTriangle = Geometry.CreateFromWkt(GetGeometry(data));
                oFeature.SetGeometry(geomTriangle);
                oLayer.CreateFeature(oFeature);
            }
            oDS.Dispose();
            MessageBox.Show("生成完毕");
        }