Пример #1
0
        //---------------------------------------------------------------------------------------
        private void initShp(ref Layer oLayer, ref Driver oDriver, ref DataSource oDS, ref Layer newLayer, LAYER_TYPE type)
        {
            //创建数据
            string shpFileName = string.Format("{0}\\{1}.shp", mOutPath, oLayer.GetName());

            oDriver = Ogr.GetDriverByName("ESRI Shapefile");
            if (oDriver == null)
            {
                Console.WriteLine("{0} 驱动不可用!\n", shpFileName);
                return;
            }
            // 创建数据源
            oDS = oDriver.CreateDataSource(shpFileName, null);
            if (oDS == null)
            {
                Console.WriteLine("创建矢量文件【%s】失败!\n", shpFileName);
                return;
            }
            // 创建图层,创建一个点图层,这里没有指定空间参考,如果需要的话,需要在这里进行指定
            switch (type)
            {
            case LAYER_TYPE.LINE:
                newLayer = oDS.CreateLayer(oLayer.GetName(), null, wkbGeometryType.wkbLineString, null);
                break;

            case LAYER_TYPE.POINT:
                newLayer = oDS.CreateLayer(oLayer.GetName(), null, wkbGeometryType.wkbPoint, null);
                break;

            default:
                break;
            }
            if (oLayer == null)
            {
                Console.WriteLine("图层创建失败!\n");
                return;
            }
        }
Пример #2
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("生成完毕");
        }