public static void Main(string[] args) { if (args.Length != 2) usage(); // Using early initialization of System.Console Console.WriteLine(""); /* -------------------------------------------------------------------- */ /* Register format(s). */ /* -------------------------------------------------------------------- */ Ogr.RegisterAll(); /* -------------------------------------------------------------------- */ /* Get driver */ /* -------------------------------------------------------------------- */ Driver drv = Ogr.GetDriverByName("ESRI Shapefile"); if (drv == null) { Console.WriteLine("Can't get driver."); System.Environment.Exit(-1); } // TODO: drv.name is still unsafe with lazy initialization (Bug 1339) //string DriverName = drv.name; //Console.WriteLine("Using driver " + DriverName); /* -------------------------------------------------------------------- */ /* Creating the datasource */ /* -------------------------------------------------------------------- */ DataSource ds = drv.CreateDataSource( args[0], new string[] {} ); if (drv == null) { Console.WriteLine("Can't create the datasource."); System.Environment.Exit(-1); } /* -------------------------------------------------------------------- */ /* Creating the layer */ /* -------------------------------------------------------------------- */ Layer layer; int i; for(i=0;i<ds.GetLayerCount();i++) { layer = ds.GetLayerByIndex( i ); if( layer != null && layer.GetLayerDefn().GetName() == args[1]) { Console.WriteLine("Layer already existed. Recreating it.\n"); ds.DeleteLayer(i); break; } } layer = ds.CreateLayer( args[1], null, wkbGeometryType.wkbPoint, new string[] {} ); if( layer == null ) { Console.WriteLine("Layer creation failed."); System.Environment.Exit(-1); } /* -------------------------------------------------------------------- */ /* Adding attribute fields */ /* -------------------------------------------------------------------- */ FieldDefn fdefn = new FieldDefn( "Name", FieldType.OFTString ); fdefn.SetWidth(32); if( layer.CreateField( fdefn, 1 ) != 0 ) { Console.WriteLine("Creating Name field failed."); System.Environment.Exit(-1); } fdefn = new FieldDefn( "IntField", FieldType.OFTInteger ); if( layer.CreateField( fdefn, 1 ) != 0 ) { Console.WriteLine("Creating IntField field failed."); System.Environment.Exit(-1); } fdefn = new FieldDefn( "DbleField", FieldType.OFTReal ); if( layer.CreateField( fdefn, 1 ) != 0 ) { Console.WriteLine("Creating DbleField field failed."); System.Environment.Exit(-1); } fdefn = new FieldDefn( "DateField", FieldType.OFTDate ); if( layer.CreateField( fdefn, 1 ) != 0 ) { Console.WriteLine("Creating DateField field failed."); System.Environment.Exit(-1); } /* -------------------------------------------------------------------- */ /* Adding features */ /* -------------------------------------------------------------------- */ Feature feature = new Feature( layer.GetLayerDefn() ); feature.SetField( "Name", "value" ); feature.SetField( "IntField", (int)123 ); feature.SetField( "DbleField", (double)12.345 ); feature.SetField( "DateField", 2007, 3, 15, 18, 24, 30, 0 ); Geometry geom = Geometry.CreateFromWkt("POINT(47.0 19.2)"); if( feature.SetGeometry( geom ) != 0 ) { Console.WriteLine( "Failed add geometry to the feature" ); System.Environment.Exit(-1); } if( layer.CreateFeature( feature ) != 0 ) { Console.WriteLine( "Failed to create feature in shapefile" ); System.Environment.Exit(-1); } ReportLayer(layer); }
//生成点shp的函数 public void pint() { string shpname = System.IO.Path.GetFileNameWithoutExtension(tra_input_tb.Text);//生产的shp文件名为输入文件的文件名; //注册Ogr库 string pszDriverName = "ESRI Shapefile"; OSGeo.OGR.Ogr.RegisterAll(); //调用对Shape文件读写的Driver接口 OSGeo.OGR.Driver poDriver = OSGeo.OGR.Ogr.GetDriverByName(pszDriverName); if (poDriver == null) { MessageBox.Show("Driver Error"); } OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); //用此Driver创建Shape文件//数据源 OSGeo.OGR.DataSource poDS; poDS = poDriver.CreateDataSource("D:\\SHP_TRY", null); if (poDS == null) { MessageBox.Show("DataSource Creation Error"); } //创建层Layer OSGeo.OGR.Layer poLayer; poLayer = poDS.CreateLayer(shpname, null, OSGeo.OGR.wkbGeometryType.wkbPoint, null);//shpname为提取输入TXT的文件名为shp文件文件名; if (poLayer == null) { MessageBox.Show("Layer Creation Failed"); } //创建属性表!!!!!这里是根据基站的TXT的做的实验以后可以改数据 OSGeo.OGR.FieldDefn oField0 = new OSGeo.OGR.FieldDefn("uid", OSGeo.OGR.FieldType.OFTString); oField0.SetWidth(16); OSGeo.OGR.FieldDefn oField1 = new OSGeo.OGR.FieldDefn("time", OSGeo.OGR.FieldType.OFTString); OSGeo.OGR.FieldDefn oField2 = new OSGeo.OGR.FieldDefn("locationid", OSGeo.OGR.FieldType.OFTString); OSGeo.OGR.FieldDefn oField3 = new OSGeo.OGR.FieldDefn("lat", OSGeo.OGR.FieldType.OFTString); OSGeo.OGR.FieldDefn oField4 = new OSGeo.OGR.FieldDefn("lon", OSGeo.OGR.FieldType.OFTInteger); poLayer.CreateField(oField0, 1); poLayer.CreateField(oField2, 2); poLayer.CreateField(oField0, 3); poLayer.CreateField(oField0, 4); poLayer.CreateField(oField0, 5); //创建一个Feature,一个Point OSGeo.OGR.Feature poFeature = new OSGeo.OGR.Feature(poLayer.GetLayerDefn()); OSGeo.OGR.Geometry pt = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbPoint); string path = tra_input_tb.Text;//TXT输入路径; string l; StreamReader sr = new StreamReader(path, Encoding.Default); while ((l = sr.ReadLine()) != null) { string[] strArr = sr.ReadLine().Split('\t'); //属性表赋值 poFeature.SetField(0, strArr[0]); poFeature.SetField(1, strArr[4]); poFeature.SetField(2, strArr[3]); poFeature.SetField(3, strArr[1]); poFeature.SetField(4, strArr[2]); //添加坐标点 pt.AddPoint(Convert.ToDouble(strArr[2]), Convert.ToDouble(strArr[1]), 0); poFeature.SetGeometry(pt); //将带有坐标及属性的Feature要素点写入Layer中 poLayer.CreateFeature(poFeature); } //关闭文件读写 poFeature.Dispose(); poDS.Dispose(); MessageBox.Show("生成文件在:D:\\SHP_TRY!"); }
/// <summary> /// 写shp文件 /// </summary> /// <param name="lstAttribute"></param> /// <param name="geometryType"></param> /// <param name="lstWkt"></param> /// <param name="lstAttributeObj"></param> /// <param name="srsWkt"></param> /// <returns></returns> public bool DoExport(List <AttributeModel> lstAttribute, OSGeo.OGR.wkbGeometryType geometryType, List <string> lstWkt, List <AttributeObj> lstAttributeObj, string srsWkt) { _lstAttribute = lstAttribute; _geoType = geometryType; _lstWkt = lstWkt; _lstAttributeObj = lstAttributeObj; try { //注册 string pszDriverName = "ESRI Shapefile"; //调用对Shape文件读写的Driver接口 OSGeo.OGR.Driver poDriver = OSGeo.OGR.Ogr.GetDriverByName(pszDriverName); if (poDriver == null) { throw new Exception("Driver Error"); } //用此Driver创建Shape文件 OSGeo.OGR.DataSource poDS; poDS = poDriver.CreateDataSource(_shpFileName, null); if (poDS == null) { throw new Exception("DataSource Creation Error"); } //定义坐标系 OSGeo.OSR.SpatialReference srs = new OSGeo.OSR.SpatialReference(srsWkt); //创建层Layer OSGeo.OGR.Layer poLayer = null; string layerName = Path.GetFileNameWithoutExtension(_shpFileName); poLayer = poDS.CreateLayer(layerName, srs, _geoType, null); if (poLayer == null) { throw new Exception("Layer Creation Failed"); } //创建属性列 foreach (AttributeModel att in _lstAttribute) { OSGeo.OGR.FieldDefn oField = new OSGeo.OGR.FieldDefn(att.AttributeName, att.AttributeType); //if (att.AttributeWidth > 0) //{ oField.SetWidth(att.AttributeWidth); //} oField.SetPrecision(att.AttributePrecision); poLayer.CreateField(oField, att.AttributeApproxOK); } //创建一个Feature,一个Geometry OSGeo.OGR.Feature poFeature = new OSGeo.OGR.Feature(poLayer.GetLayerDefn()); OSGeo.OGR.wkbGeometryType wkbGeotype = OSGeo.OGR.wkbGeometryType.wkbGeometryCollection; OSGeo.OGR.Geometry geo = new OSGeo.OGR.Geometry(wkbGeotype); for (int i = 0; i < _lstWkt.Count; i++) { foreach (KeyValuePair <string, string> item in _lstAttributeObj[i].AttributeValue) { poFeature.SetField(item.Key, item.Value); } geo = OSGeo.OGR.Geometry.CreateFromWkt(_lstWkt[i]); poFeature.SetGeometry(geo); poLayer.CreateFeature(poFeature); } //关闭文件读写 poFeature.Dispose(); poDS.Dispose(); return(true); } catch (System.Exception e) { return(false); //throw new Exception(e.Message); } }