/// <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; }
private static void PrintDriversOgr() { #if DEBUG var num = Ogr.GetDriverCount(); for (var i = 0; i < num; i++) { var driver = Ogr.GetDriver(i); Console.WriteLine(string.Format("OGR {0}: {1}", i, driver.name)); } #endif }
/// <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; }
public string GetFieldAsString(string field_name) { /* %typemap(csout) (const char *utf8_path) */ IntPtr cPtr = OgrPINVOKE.Feature_GetFieldAsString__SWIG_1(swigCPtr, field_name); string ret = Ogr.Utf8BytesToString(cPtr); if (OgrPINVOKE.SWIGPendingException.Pending) { throw OgrPINVOKE.SWIGPendingException.Retrieve(); } return(ret); }
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); } }
//--------------------------------------------------------------------------------------- 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; } }
public DataSource CopyDataSource(DataSource copy_ds, string utf8_path, string[] options) { IntPtr cPtr = OgrPINVOKE.Driver_CopyDataSource(swigCPtr, DataSource.getCPtr(copy_ds), Ogr.StringToUtf8Bytes(utf8_path), (options != null)? new OgrPINVOKE.StringListMarshal(options)._ar : null); DataSource ret = (cPtr == IntPtr.Zero) ? null : new DataSource(cPtr, true, ThisOwn_true()); if (OgrPINVOKE.SWIGPendingException.Pending) { throw OgrPINVOKE.SWIGPendingException.Retrieve(); } return(ret); }
public static extern int Layer_Update(HandleRef jarg1, HandleRef jarg2, HandleRef jarg3, IntPtr[] jarg4, Ogr.GDALProgressFuncDelegate jarg5, string jarg6);
public int Clip(Layer method_layer, Layer result_layer, string[] options, Ogr.GDALProgressFuncDelegate callback, string callback_data) { int ret = OgrPINVOKE.Layer_Clip(swigCPtr, Layer.getCPtr(method_layer), Layer.getCPtr(result_layer), (options != null)? new OgrPINVOKE.StringListMarshal(options)._ar : null, callback, callback_data); if (OgrPINVOKE.SWIGPendingException.Pending) throw OgrPINVOKE.SWIGPendingException.Retrieve(); return ret; }
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("生成完毕"); }
public void readLine(ref Layer oLayer, string pointTableName) { // 对图层进行初始化,如果对图层进行了过滤操作,执行这句后,之前的过滤全部清空 oLayer.ResetReading(); //打开数据 DataSource newDs = Ogr.Open(mSrcFile, 0); if (newDs == null) { Console.WriteLine(string.Format("打开文件【{0}】失败!\n", mSrcFile)); return; } //根据图层名称获取相应的图层 Layer matchLayer = newDs.GetLayerByName(pointTableName); Driver oDriver = null; DataSource oDS = null; Layer newLayer = null; initShp(ref oLayer, ref oDriver, ref oDS, ref newLayer, LAYER_TYPE.LINE); // 获取图层中的属性表表头并输出 string strInfo = "属性表结构信息:\n"; FeatureDefn oDefn = oLayer.GetLayerDefn(); int iFieldCount = oDefn.GetFieldCount(); for (int iAttr = 0; iAttr < iFieldCount; iAttr++) { FieldDefn oField = oDefn.GetFieldDefn(iAttr); FieldType type = oField.GetFieldType(); //为新图层创建属性 FieldDefn newField = new FieldDefn(oField.GetNameRef(), type); if (type == FieldType.OFTString) { newField.SetWidth(oField.GetWidth()); } newLayer.CreateField(newField, 1); strInfo += string.Format("{0}:{1} ({2}.{3})\n", oField.GetNameRef(), oField.GetFieldTypeName(oField.GetFieldType()), oField.GetWidth(), oField.GetPrecision()); } FeatureDefn newDefn = newLayer.GetLayerDefn(); // 输出图层中的要素个数 strInfo += string.Format("要素个数 = {0}\n", oLayer.GetFeatureCount(0)); Feature oFeature = null; // 下面开始遍历图层中的要素 double S_X = 0.0; double S_Y = 0.0; double E_X = 0.0; double E_Y = 0.0; while ((oFeature = oLayer.GetNextFeature()) != null) { strInfo += string.Format("\n当前处理第{0}个: \n属性值:", oFeature.GetFID()); //为新图层创建要素 Feature oFeatureLineString = new Feature(newDefn); string sql = string.Empty; // 获取要素中的属性表内容 for (int iField = 0; iField < iFieldCount; iField++) { FieldDefn oFieldDefn = oDefn.GetFieldDefn(iField); string name = oFieldDefn.GetNameRef(); FieldType type = oFieldDefn.GetFieldType(); switch (type) { case FieldType.OFTString: IntPtr pchar = OGR_F_GetFieldAsString(Feature.getCPtr(oFeature), iField); string val = Marshal.PtrToStringAnsi(pchar); oFeatureLineString.SetField(iField, val); switch (name) { case "S_Point": sql = oFeature.GetFieldAsString(iField); getCoordinate(ref matchLayer, ref sql, ref S_X, ref S_Y); break; case "E_Point": sql = oFeature.GetFieldAsString(iField); getCoordinate(ref matchLayer, ref sql, ref E_X, ref E_Y); break; default: break; } break; case FieldType.OFTReal: oFeatureLineString.SetField(name, oFeature.GetFieldAsDouble(iField)); break; case FieldType.OFTInteger: oFeatureLineString.SetField(iField, oFeature.GetFieldAsInteger(iField)); break; default: oFeatureLineString.SetField(iField, oFeature.GetFieldAsString(iField)); break; } } Geometry oGeometry = Geometry.CreateFromWkt(string.Format("LINESTRING({0} {1},{2} {3})", S_X, S_Y, E_X, E_Y)); oFeatureLineString.SetGeometryDirectly(oGeometry); newLayer.CreateFeature(oFeatureLineString); oGeometry.Dispose(); } strInfo += "\n数据集关闭!"; oDS.Dispose(); }