Exemple #1
0
        private void button4_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter     = "文本文件(*.txt)|*.shp|所有文件|*.*"; //设置文件类型
            sfd.FileName   = "po";                         //设置默认文件名
            sfd.DefaultExt = "shp";                        //设置默认格式(可以不设)

            sfd.AddExtension = true;                       //设置自动在文件名中添加扩展名
            string localFilePat = "";

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                //获得文件路径
                string localFilePath = sfd.FileName.ToString();
                localFilePat = localFilePath;
            }
            strVectorFile = localFilePat;
            MessageBox.Show(localFilePat);
            dabb.Add(localFilePat);

            pc.path.Add(localFilePat);
            pc.sdsad(localFilePat);
            //bb.pat = localFilePat;
            a = localFilePat;
            string strDriverName = "ESRI Shapefile";
            int    count         = Ogr.GetDriverCount();
            Driver oDriver       = Ogr.GetDriverByName(strDriverName);

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

            // 创建数据源
            DataSource oDS = oDriver.CreateDataSource(strVectorFile, null);

            if (oDS == null)
            {
                Console.WriteLine("创建矢量文件【%s】失败!\n", strVectorFile);
                return;
            }
            oLayer = oDS.CreateLayer(textBox3.Text, null, wkbGeometryType.wkbPoint, null);
            if (oLayer == null)
            {
                Console.WriteLine("图层创建失败!\n");
                return;
            }
            // 下面创建属性表
            // 先创建一个叫FieldID的整型属性
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                FieldDefn oFieldI = new FieldDefn(listView1.Items[i].SubItems[0].Text, FieldType.OFTInteger);

                oFieldI.SetWidth(Convert.ToInt32(listView1.Items[i].SubItems[1].Text));
                oLayer.CreateField(oFieldI, 1);
            }
            //Feature poFeature = new Feature(oLayer.GetLayerDefn());
            //Geometry pt = new Geometry(OSGeo.OGR.wkbGeometryType.wkbPoint);
            //for (int i = 100; i < 110; i++)
            //{
            //    //属性一"名称"赋值
            //    poFeature.SetField(0, i);
            //    //属性二"高度"赋值
            //    poFeature.SetField(1, i);
            //    //添加坐标点
            //    pt.AddPoint(i, i, i);
            //    poFeature.SetGeometry(pt);
            //    //将带有坐标及属性的Feature要素点写入Layer中
            //    oLayer.CreateFeature(poFeature);

            //}

            //关闭文件读写
            //poFeature.Dispose();
            oDS.Dispose();
            oDriver.Dispose();

            //string information = OGRReadFile.GetVectorInfo(strVectorFile);
            MessageBox.Show("创建成功!");
        }