public void test(ref Layer oLayer) { // 对图层进行初始化,如果对图层进行了过滤操作,执行这句后,之前的过滤全部清空 oLayer.ResetReading(); // 获取图层中的属性表表头并输出 string strInfo = "属性表结构信息:\n"; FeatureDefn oDefn = oLayer.GetLayerDefn(); int iFieldCount = oDefn.GetFieldCount(); for (int iAttr = 0; iAttr < iFieldCount; iAttr++) { FieldDefn oField = oDefn.GetFieldDefn(iAttr); strInfo += string.Format("{0}:{1} ({2}.{3})\n", oField.GetNameRef(), oField.GetFieldTypeName(oField.GetFieldType()), oField.GetWidth(), oField.GetPrecision()); } // 输出图层中的要素个数 strInfo += string.Format("要素个数 = {0}\n", oLayer.GetFeatureCount(0)); Feature oFeature = null; // 下面开始遍历图层中的要素 while ((oFeature = oLayer.GetNextFeature()) != null) { strInfo += string.Format("\n当前处理第{0}个: \n属性值:", oFeature.GetFID()); // 获取要素中的属性表内容 for (int iField = 0; iField < iFieldCount; iField++) { FieldDefn oFieldDefn = oDefn.GetFieldDefn(iField); FieldType type = oFieldDefn.GetFieldType(); switch (type) { case FieldType.OFTString: IntPtr pchar = OGR_F_GetFieldAsString(Feature.getCPtr(oFeature), iField); string val = Marshal.PtrToStringAnsi(pchar); strInfo += string.Format("{0}\t", val); break; case FieldType.OFTReal: strInfo += string.Format("{0}\t", oFeature.GetFieldAsDouble(iField)); break; case FieldType.OFTInteger: strInfo += string.Format("{0}\t", oFeature.GetFieldAsInteger(iField)); break; default: strInfo += string.Format("{0}\t", oFeature.GetFieldAsString(iField)); break; } } // 获取要素中的几何体 Geometry oGeometry = oFeature.GetGeometryRef(); // 为了演示,只输出一个要素信息 break; } strInfo += "\n数据集关闭!"; }
public void opiszWarstwe() { FeatureDefn def = this.layer.GetLayerDefn(); Console.WriteLine("Nazwa warstwy: " + def.GetName()); Console.WriteLine("Liczba obiektów: " + this.layer.GetFeatureCount(1)); Envelope ext = new Envelope(); layer.GetExtent(ext, 1); Console.WriteLine("Zasiêg: " + ext.MinX + "," + ext.MaxX + "," + ext.MinY + "," + ext.MaxY); SpatialReference sr = this.layer.GetSpatialRef(); string srs_wkt = "(unknown)"; if (sr != null) { sr.ExportToPrettyWkt(out srs_wkt, 1); } Console.WriteLine("Uk³ad przestrzenny warstwy: " + srs_wkt); Console.WriteLine("Definicje atrybutów:"); for (int iAttr = 0; iAttr < def.GetFieldCount(); iAttr++) { FieldDefn fdef = def.GetFieldDefn(iAttr); Console.WriteLine(fdef.GetNameRef() + ": " + fdef.GetFieldTypeName(fdef.GetFieldType()) + " (" + fdef.GetWidth() + "." + fdef.GetPrecision() + ")"); } }
public bool getFields() { if (oLayer == null) { return(false); } mFiledList.Clear(); //获取图层的属性表结构 FeatureDefn oDefn = oLayer.GetLayerDefn(); int filedCount = oDefn.GetFieldCount(); for (int i = 0; i < filedCount; i++) { //获取指定序号的属性列 FieldDefn oField = oDefn.GetFieldDefn(i); if (oField != null) { //获取属性列名字 mFiledList.Add(oField.GetNameRef()); } } return(true); }
public static string ReportLayer(Layer layer) { string strInfomation = ""; FeatureDefn def = layer.GetLayerDefn(); strInfomation += ("Layer name: " + def.GetName()); strInfomation += ("Feature Count: " + layer.GetFeatureCount(1).ToString()); Envelope ext = new Envelope(); layer.GetExtent(ext, 1); strInfomation += ("Extent: " + ext.MinX.ToString() + "," + ext.MaxX.ToString() + "," + ext.MinY.ToString() + "," + ext.MaxY.ToString()); /* -------------------------------------------------------------------- */ /* Reading the spatial reference */ /* -------------------------------------------------------------------- */ OSGeo.OSR.SpatialReference sr = layer.GetSpatialRef(); string srs_wkt; if (sr != null) { sr.ExportToPrettyWkt(out srs_wkt, 1); } else { srs_wkt = "(unknown)"; } strInfomation += ("Layer SRS WKT: " + srs_wkt); /* -------------------------------------------------------------------- */ /* Reading the fields */ /* -------------------------------------------------------------------- */ strInfomation += ("Field definition:"); for (int iAttr = 0; iAttr < def.GetFieldCount(); iAttr++) { FieldDefn fdef = def.GetFieldDefn(iAttr); strInfomation += (fdef.GetNameRef() + ": " + fdef.GetFieldTypeName(fdef.GetFieldType()) + " (" + fdef.GetWidth().ToString() + "." + fdef.GetPrecision().ToString() + ")"); } /* -------------------------------------------------------------------- */ /* Reading the shapes */ /* -------------------------------------------------------------------- */ strInfomation += (""); Feature feat; while ((feat = layer.GetNextFeature()) != null) { strInfomation += ReportFeature(feat, def); feat.Dispose(); } return(strInfomation); }
public static MG_Field AsField(FieldDefn f) { MG_Field mgField = new MG_Field(); mgField.Name = f.GetNameRef(); //collect data mgField.Type = AsFieldDBType(f.GetFieldType()); //collect data mgField.Width = f.GetWidth(); //collect data mgField.Precision = f.GetPrecision(); //collect data return(mgField); }
/// <summary> /// 获取所有列名 /// </summary> /// <param name="strVectorFile"></param> /// <returns></returns> public static List <string> getShapeFieldList(string strVectorFile) { List <string> list = new List <string>(); #region ReadShape { Gdal.AllRegister(); // 为了支持中文路径,请添加下面这句代码 OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO"); // 为了使属性表字段支持中文,请添加下面这句 OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", ""); // 注册所有的驱动 Ogr.RegisterAll(); //打开数据 DataSource ds = Ogr.Open(strVectorFile, 0); if (ds == null) { MessageBox.Show("打开文件【{0}】失败!,文件名中不能包含中文!", strVectorFile); return(list); } //MessageBox.Show("打开文件【{0}】成功!", strVectorFile); // 获取该数据源中的图层个数,一般shp数据图层只有一个,如果是mdb、dxf等图层就会有多个 int iLayerCount = ds.GetLayerCount(); // 获取第一个图层 Layer oLayer = ds.GetLayerByIndex(0); if (oLayer == null) { MessageBox.Show("获取第{0}个图层失败!\n", "0"); return(list); } // 对图层进行初始化,如果对图层进行了过滤操作,执行这句后,之前的过滤全部清空 oLayer.ResetReading(); FeatureDefn oDefn = oLayer.GetLayerDefn(); // 输出图层中的要素个数 int iFieldCount = oDefn.GetFieldCount(); for (int iAttr = 0; iAttr < iFieldCount; iAttr++) { FieldDefn oField = oDefn.GetFieldDefn(iAttr); list.Add(oField.GetNameRef()); } } #endregion list.Sort(); return(list); }
/// <summary> /// 获取某条数据的字段内容 /// </summary> /// <param name="iIndex"></param> /// <param name="FeildStringList"></param> /// <returns></returns> public bool GetFeildContent(int iIndex, out List <string> FeildStringList) { FeildStringList = new List <string>(); Feature oFeature = null; if ((oFeature = oLayer.GetFeature(iIndex)) != null) { FeatureDefn oDefn = oLayer.GetLayerDefn(); int iFieldCount = oDefn.GetFieldCount(); // 查找字段属性 for (int iAttr = 0; iAttr < iFieldCount; iAttr++) { FieldDefn oField = oDefn.GetFieldDefn(iAttr); string sFeildName = oField.GetNameRef(); #region 获取属性字段 FieldType Ftype = oFeature.GetFieldType(sFeildName); switch (Ftype) { case FieldType.OFTString: string sFValue = oFeature.GetFieldAsString(sFeildName); string sTempType = "string"; FeildStringList.Add(sFValue); break; case FieldType.OFTReal: double dFValue = oFeature.GetFieldAsDouble(sFeildName); sTempType = "float"; FeildStringList.Add(dFValue.ToString()); break; case FieldType.OFTInteger: int iFValue = oFeature.GetFieldAsInteger(sFeildName); sTempType = "int"; FeildStringList.Add(iFValue.ToString()); break; default: //sFValue = oFeature.GetFieldAsString(ChosenFeildIndex[iFeildIndex]); sTempType = "string"; break; } #endregion } } return(true); }
/// <summary>获取某条数据的字段内容 /// </summary> /// <param name="nIndex"></param> /// <param name="lstFeildString"></param> /// <param name="fileAttrEncoding">文件属性字段编码类型</param> /// <returns></returns> public virtual bool GetFeildContent(int nIndex, out Dictionary <string, string> dictFieldInfo, Encoding fileAttrEncoding) { _FileAttrEncoding = fileAttrEncoding; dictFieldInfo = new Dictionary <string, string>(); Feature oFeature = null; if ((oFeature = _Layer.GetFeature(nIndex)) != null) { FeatureDefn oDefn = _Layer.GetLayerDefn(); int iFieldCount = oDefn.GetFieldCount(); // 查找字段属性 for (int iAttr = 0; iAttr < iFieldCount; iAttr++) { FieldDefn oField = oDefn.GetFieldDefn(iAttr); string strFeildName = oField.GetNameRef(); string strValue = ""; #region 获取属性字段 FieldType Ftype = oFeature.GetFieldType(strFeildName); switch (Ftype) { case FieldType.OFTString: strValue = GetFieldAsStringNu(oFeature, strFeildName, _FileAttrEncoding); break; case FieldType.OFTReal: double dFValue = oFeature.GetFieldAsDouble(strFeildName); strValue = dFValue.ToString(); break; case FieldType.OFTInteger: int iFValue = oFeature.GetFieldAsInteger(strFeildName); strValue = iFValue.ToString(); break; default: break; } dictFieldInfo.Add(strFeildName, strValue); #endregion } } return(true); }
public static void ReportFeature(Feature feat, FeatureDefn def) { Console.WriteLine("Feature(" + def.GetName() + "): " + feat.GetFID()); for (int iField = 0; iField < feat.GetFieldCount(); iField++) { FieldDefn fdef = def.GetFieldDefn(iField); Console.Write(fdef.GetNameRef() + " (" + fdef.GetFieldTypeName(fdef.GetFieldType()) + ") = "); if (feat.IsFieldSet(iField)) { Console.WriteLine(feat.GetFieldAsString(iField)); } else { Console.WriteLine("(null)"); } } if (feat.GetStyleString() != null) { Console.WriteLine(" Style = " + feat.GetStyleString()); } Geometry geom = feat.GetGeometryRef(); if (geom != null) { Console.WriteLine(" " + geom.GetGeometryName() + "(" + geom.GetGeometryType() + ")"); } Envelope env = new Envelope(); geom.GetEnvelope(env); Console.WriteLine(" ENVELOPE: " + env.MinX + "," + env.MaxX + "," + env.MinY + "," + env.MaxY); string geom_wkt; geom.ExportToWkt(out geom_wkt); Console.WriteLine(" " + geom_wkt); Console.WriteLine(""); }
/// <summary> /// 获取某条数据的字段内容 /// </summary> /// <param name="iIndex"></param> /// <param name="FeildStringList"></param> /// <returns></returns> public string GetFeildContent(int iAttr, Feature oFeature) { //string tempType; string attribute = ""; if (oFeature != null) { FeatureDefn oDefn = oLayer.GetLayerDefn(); FieldDefn oField = oDefn.GetFieldDefn(iAttr); string sFeildName = oField.GetNameRef(); FieldType Ftype = oFeature.GetFieldType(sFeildName); switch (Ftype) { case FieldType.OFTString: string sFValue = oFeature.GetFieldAsString(sFeildName); //tempType = "string"; attribute = sFValue.ToString(); break; case FieldType.OFTReal: double dFValue = oFeature.GetFieldAsDouble(sFeildName); //tempType = "float"; attribute = dFValue.ToString(); break; case FieldType.OFTInteger: int iFValue = oFeature.GetFieldAsInteger(sFeildName); //tempType = "int"; attribute = iFValue.ToString(); break; default: //sFValue = oFeature.GetFieldAsString(ChosenFeildIndex[iFeildIndex]); //tempType = "string"; break; } } return(attribute); }
/// <summary>获取所有的属性字段 /// </summary> /// <returns></returns> public virtual bool GetFeilds() { if (null == _Layer) { return(false); } _Feilds.Clear(); wkbGeometryType oTempGeometryType = _Layer.GetGeomType(); FeatureDefn oDefn = _Layer.GetLayerDefn(); int iFieldCount = oDefn.GetFieldCount(); for (int iAttr = 0; iAttr < iFieldCount; iAttr++) { FieldDefn oField = oDefn.GetFieldDefn(iAttr); if (null != oField) { _Feilds.Add(oField.GetNameRef(), oField.GetFieldType()); } } return(true); }
/// <summary> /// 获取所有的属性字段 /// </summary> /// <returns></returns> public void GetFeilds() { if (null == oLayer) { } m_FeildList.Clear(); wkbGeometryType oTempGeometryType = oLayer.GetGeomType(); List <string> TempstringList = new List <string>(); // FeatureDefn oDefn = oLayer.GetLayerDefn(); int iFieldCount = oDefn.GetFieldCount(); for (int iAttr = 0; iAttr < iFieldCount; iAttr++) { FieldDefn oField = oDefn.GetFieldDefn(iAttr); if (null != oField) { m_FeildList.Add(oField.GetNameRef()); } } string[] a = m_FeildList.ToArray(); }
//----------------------------------------------------------------------------------- public void readPoint(ref Layer oLayer) { // 对图层进行初始化,如果对图层进行了过滤操作,执行这句后,之前的过滤全部清空 oLayer.ResetReading(); Driver oDriver = null; DataSource oDS = null; Layer newLayer = null; initShp(ref oLayer, ref oDriver, ref oDS, ref newLayer, LAYER_TYPE.POINT); // 获取图层中的属性表表头并输出 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(); string fieldName = oField.GetName(); //为新图层创建属性 if (!fieldName.Equals("X坐标") && !fieldName.Equals("Y坐标")) { FieldDefn newField = new FieldDefn(fieldName, 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; // 下面开始遍历图层中的要素 while ((oFeature = oLayer.GetNextFeature()) != null) { strInfo += string.Format("\n当前处理第{0}个: \n属性值:", oFeature.GetFID()); //为新图层创建要素 Feature oFeaturePoint = new Feature(newDefn); double pointX = 0.0; double pointY = 0.0; // 获取要素中的属性表内容 for (int iField = 0; iField < iFieldCount; iField++) { FieldDefn oFieldDefn = oDefn.GetFieldDefn(iField); FieldType type = oFieldDefn.GetFieldType(); string name = oFieldDefn.GetNameRef(); switch (type) { case FieldType.OFTString: IntPtr pchar = OGR_F_GetFieldAsString(Feature.getCPtr(oFeature), iField); string val = Marshal.PtrToStringAnsi(pchar); oFeaturePoint.SetField(name, val); break; case FieldType.OFTReal: if (!name.Equals("X坐标") && !name.Equals("Y坐标")) { oFeaturePoint.SetField(name, oFeature.GetFieldAsDouble(iField)); } switch (name) { case "X": pointX = oFeature.GetFieldAsDouble(iField); break; case "Y": pointY = oFeature.GetFieldAsDouble(iField); break; default: break; } break; case FieldType.OFTInteger: oFeaturePoint.SetField(iField, oFeature.GetFieldAsInteger(iField)); break; default: oFeaturePoint.SetField(iField, oFeature.GetFieldAsString(iField)); break; } } //填充要素几何信息 Geometry oGeometry = Geometry.CreateFromWkt(string.Format("POINT({0} {1})", pointX, pointY)); oFeaturePoint.SetGeometry(oGeometry); newLayer.CreateFeature(oFeaturePoint); } strInfo += "\n数据集关闭!"; oDS.Dispose(); }
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(); }
private void button2_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Title = "打开ShapeFile数据"; dlg.Filter = "ShapeFile数据(*.shp)|*.shp"; if (dlg.ShowDialog() == DialogResult.OK) { Ogr.RegisterAll(); string strVectorFile = dlg.FileName; textBox1.Text = strVectorFile; //打开数据 DataSource ds = Ogr.Open(strVectorFile, 0); if (ds == null) { listBox1.Items.Add(string.Format("打开文件【{0}】失败!", strVectorFile)); return; } listBox1.Items.Add(string.Format("打开文件【{0}】成功!", strVectorFile)); // 获取该数据源中的图层个数,一般shp数据图层只有一个,如果是mdb、dxf等图层就会有多个 int iLayerCount = ds.GetLayerCount(); // 获取第一个图层 Layer oLayer = ds.GetLayerByIndex(0); if (oLayer == null) { listBox1.Items.Add(string.Format("获取第{0}个图层失败!\n", 0)); return; } // 对图层进行初始化,如果对图层进行了过滤操作,执行这句后,之前的过滤全部清空 oLayer.ResetReading(); // 通过属性表的SQL语句对图层中的要素进行筛选,这部分详细参考SQL查询章节内容 //oLayer.SetAttributeFilter("\"NAME99\"LIKE \"北京市市辖区\""); // 通过指定的几何对象对图层中的要素进行筛选 //oLayer.SetSpatialFilter(); // 通过指定的四至范围对图层中的要素进行筛选 //oLayer.SetSpatialFilterRect(); // 获取图层中的属性表表头并输出 listBox1.Items.Add("属性表结构信息:"); FeatureDefn oDefn = oLayer.GetLayerDefn(); int iFieldCount = oDefn.GetFieldCount(); for (int iAttr = 0; iAttr < iFieldCount; iAttr++) { FieldDefn oField = oDefn.GetFieldDefn(iAttr); listBox1.Items.Add(string.Format("{0}:{1} ({2}.{3})", oField.GetNameRef(), oField.GetFieldTypeName(oField.GetFieldType()), oField.GetWidth(), oField.GetPrecision())); } // 输出图层中的要素个数 listBox1.Items.Add(string.Format("要素个数 = {0}", oLayer.GetFeatureCount(0))); Feature oFeature = null; // 下面开始遍历图层中的要素 while ((oFeature = oLayer.GetNextFeature()) != null) { Geometry geo = oFeature.GetGeometryRef(); wkbGeometryType wkb = geo.GetGeometryType(); listBox1.Items.Add(string.Format("当前处理第要素值:{0}", wkb.ToString())); string strGml = geo.ExportToGML(); listBox1.Items.Add(strGml); listBox1.Items.Add(string.Format("当前处理第{0}个: \n属性值:", oFeature.GetFID())); // 获取要素中的属性表内容 for (int iField = 0; iField < iFieldCount; iField++) { FieldDefn oFieldDefn = oDefn.GetFieldDefn(iField); FieldType type = oFieldDefn.GetFieldType(); switch (type) { case FieldType.OFTString: listBox1.Items.Add(string.Format("{0}\t", oFeature.GetFieldAsString(iField))); break; case FieldType.OFTReal: listBox1.Items.Add(string.Format("{0}\t", oFeature.GetFieldAsDouble(iField))); break; case FieldType.OFTInteger: listBox1.Items.Add(string.Format("{0}\t", oFeature.GetFieldAsInteger(iField))); break; default: listBox1.Items.Add(string.Format("{0}\t", oFeature.GetFieldAsString(iField))); break; } } // 获取要素中的几何体 Geometry oGeometry = oFeature.GetGeometryRef(); // 为了演示,只输出一个要素信息 break; } listBox1.Items.Add("数据集关闭!"); } }
public static void ReportFeature(Feature feat, FeatureDefn def) { Console.WriteLine("Feature(" + def.GetName() + "): " + feat.GetFID()); for (int iField = 0; iField < feat.GetFieldCount(); iField++) { FieldDefn fdef = def.GetFieldDefn(iField); Console.Write(fdef.GetNameRef() + " (" + fdef.GetFieldTypeName(fdef.GetFieldType()) + ") = "); if (feat.IsFieldSet(iField)) { if (fdef.GetFieldType() == FieldType.OFTStringList) { string[] sList = feat.GetFieldAsStringList(iField); foreach (string s in sList) { Console.Write("\"" + s + "\" "); } Console.WriteLine(); } else if (fdef.GetFieldType() == FieldType.OFTIntegerList) { int count; int[] iList = feat.GetFieldAsIntegerList(iField, out count); for (int i = 0; i < count; i++) { Console.Write(iList[i] + " "); } Console.WriteLine(); } else if (fdef.GetFieldType() == FieldType.OFTRealList) { int count; double[] iList = feat.GetFieldAsDoubleList(iField, out count); for (int i = 0; i < count; i++) { Console.Write(iList[i].ToString() + " "); } Console.WriteLine(); } else { Console.WriteLine(feat.GetFieldAsString(iField)); } } else { Console.WriteLine("(null)"); } } if (feat.GetStyleString() != null) { Console.WriteLine(" Style = " + feat.GetStyleString()); } Geometry geom = feat.GetGeometryRef(); if (geom != null) { Console.WriteLine(" " + geom.GetGeometryName() + "(" + geom.GetGeometryType() + ")"); Geometry sub_geom; for (int i = 0; i < geom.GetGeometryCount(); i++) { sub_geom = geom.GetGeometryRef(i); if (sub_geom != null) { Console.WriteLine(" subgeom" + i + ": " + sub_geom.GetGeometryName() + "(" + sub_geom.GetGeometryType() + ")"); } } Envelope env = new Envelope(); geom.GetEnvelope(env); Console.WriteLine(" ENVELOPE: " + env.MinX + "," + env.MaxX + "," + env.MinY + "," + env.MaxY); string geom_wkt; geom.ExportToWkt(out geom_wkt); Console.WriteLine(" " + geom_wkt); } Console.WriteLine(""); }
public static List <string> getShapeFieldDataList(string strVectorFile) { List <string> list = new List <string>(); #region ReadShape Gdal.AllRegister(); // 为了支持中文路径,请添加下面这句代码 OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO"); // 为了使属性表字段支持中文,请添加下面这句 OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", ""); // 注册所有的驱动 Ogr.RegisterAll(); //打开数据 DataSource ds = Ogr.Open(strVectorFile, 0); if (ds == null) { MessageBox.Show("打开文件【{0}】失败!", strVectorFile); return(list); } //MessageBox.Show("打开文件【{0}】成功!", strVectorFile); // 获取该数据源中的图层个数,一般shp数据图层只有一个,如果是mdb、dxf等图层就会有多个 int iLayerCount = ds.GetLayerCount(); // 获取第一个图层 Layer oLayer = ds.GetLayerByIndex(0); if (oLayer == null) { MessageBox.Show("获取第{0}个图层失败!\n", "0"); return(list); } // 对图层进行初始化,如果对图层进行了过滤操作,执行这句后,之前的过滤全部清空 oLayer.ResetReading(); FeatureDefn oDefn = oLayer.GetLayerDefn(); // 输出图层中的要素个数 int iFieldCount = oDefn.GetFieldCount(); Feature oFeature = null; // 下面开始遍历图层中的要素 while ((oFeature = oLayer.GetNextFeature()) != null) { // 获取要素中的属性表内容 for (int iField = 0; iField < iFieldCount; iField++) { FieldDefn oFieldDefn = oDefn.GetFieldDefn(iField); FieldType type = oFieldDefn.GetFieldType(); string sFieldName = oFieldDefn.GetNameRef(); switch (type) { case FieldType.OFTString: //MessageBox.Show(iField+"=="+oFeature.GetFieldAsString(iField)); break; case FieldType.OFTReal: //MessageBox.Show(iField + "==" + oFeature.GetFieldAsDouble(iField)); break; case FieldType.OFTInteger: //MessageBox.Show(iField + "==" + oFeature.GetFieldAsInteger(iField)); break; default: //MessageBox.Show(iField + "==" + oFeature.GetFieldAsString(iField)); //list.Add(oFeature.GetFieldAsString(iField)); break; } if (sFieldName.Equals("ADM0_NAME")) { list.Add(oFeature.GetFieldAsString(iField)); } } } #endregion list.Sort(); return(list); }
private void WczytajOgrLayer(Layer layer) { List <GeometryFeature> poligony = new List <GeometryFeature>(); List <GeometryFeature> teksty = new List <GeometryFeature>(); FeatureDefn layerDef = layer.GetLayerDefn(); Feature feat = null; while ((feat = layer.GetNextFeature()) != null) { Geometry featGeom = feat.GetGeometryRef(); FeatureDefn featDef = feat.GetDefnRef(); string name = null; string value = null; //pierwsze pole tekstowe for (int i = 0; i < feat.GetFieldCount(); i++) { FieldDefn fdef = featDef.GetFieldDefn(i); if (feat.IsFieldSet(i)) { if (fdef.GetFieldType() == FieldType.OFTString) { name = fdef.GetNameRef(); value = feat.GetFieldAsString(i); break; } } } switch (featGeom.GetGeometryType()) { case wkbGeometryType.wkbPolygon: case wkbGeometryType.wkbPolygon25D: { GeometryFeature geomFeature = new GeometryFeature { Geometry = featGeom.Clone(), Name = name, Value = value }; poligony.Add(geomFeature); } break; case wkbGeometryType.wkbPoint: case wkbGeometryType.wkbPoint25D: { GeometryFeature geomFeature = new GeometryFeature { Geometry = featGeom.Clone(), Name = name, Value = value }; teksty.Add(geomFeature); } break; } feat.Dispose(); } //przypisanie tekstów do poligonów foreach (GeometryFeature gf in poligony) { //przypisz pierwszy tekst zawarty w poligonie foreach (GeometryFeature tf in teksty) { if (gf.Geometry.Contains(tf.Geometry)) { gf.Name = tf.Name; gf.Value = tf.Value; break; } } _geometryLayer.AddFeature(gf); } }
private static void ReportFeature(Feature f, FeatureDefn def) { //string layerName = def.GetName();//Links int fid = f.GetFID(); //0 1 2 3...15 int nFieldCount = f.GetFieldCount(); //3 for (int iField = 0; iField < nFieldCount; iField++) { FieldDefn fdef = def.GetFieldDefn(iField); string fieldName = fdef.GetName(); //Id Name URL string fieldNameRef = fdef.GetNameRef(); //Id Name URL string fieldTypeName = fdef.GetFieldTypeName(fdef.GetFieldType()); //Integer String String if (f.IsFieldSet(iField)) { if (fdef.GetFieldType() == FieldType.OFTStringList) { string[] sList = f.GetFieldAsStringList(iField); foreach (string s in sList) { } } else if (fdef.GetFieldType() == FieldType.OFTIntegerList) { int count; int[] iList = f.GetFieldAsIntegerList(iField, out count); for (int i = 0; i < count; i++) { } } else if (fdef.GetFieldType() == FieldType.OFTRealList) { int count; double[] iList = f.GetFieldAsDoubleList(iField, out count); for (int i = 0; i < count; i++) { } } else if (fdef.GetFieldType() == FieldType.OFTString) { string strField = f.GetFieldAsString(iField); } else if (fdef.GetFieldType() == FieldType.OFTInteger) { int nField = f.GetFieldAsInteger(iField); } else if (fdef.GetFieldType() == FieldType.OFTReal) { double fField = f.GetFieldAsDouble(iField); } else { // other types } } } if (f.GetStyleString() != null) { string style = f.GetStyleString(); } // feature's geometry info Geometry geom = f.GetGeometryRef(); if (geom != null) { string geomName = geom.GetGeometryName(); //POINT string geomType = geom.GetGeometryType().ToString(); //wkbPoint int geometryCount = geom.GetGeometryCount(); //0 for (int i = 0; i < geometryCount; i++) { Geometry sub_geom = geom.GetGeometryRef(i); if (sub_geom != null) { string sub_geomName = sub_geom.GetGeometryName(); string sub_geomType = sub_geom.GetGeometryType().ToString(); } } Envelope env = new Envelope(); geom.GetEnvelope(env); string geom_wkt; geom.ExportToWkt(out geom_wkt);//POINT(-63.4,46.6) } }
private static void ReportLayer(Layer layer) { //layer info string layerName = layer.GetName(); //Links string layerName2 = layer.GetLayerDefn().GetName(); //Links int fc = layer.GetFeatureCount(1); //16 Envelope ext = new Envelope(); layer.GetExtent(ext, 1); /* -------------------------------------------------------------------- */ /* Reading the spatial reference */ /* -------------------------------------------------------------------- */ OSGeo.OSR.SpatialReference sr = layer.GetSpatialRef(); string srs_wkt; if (sr != null) { sr.ExportToPrettyWkt(out srs_wkt, 1); } else { srs_wkt = "(unknown)"; } // feature definition FeatureDefn def = layer.GetLayerDefn(); //string layerName2 = def.GetName();//Links /* -------------------------------------------------------------------- */ /* Reading the fields */ /* -------------------------------------------------------------------- */ int nFieldCount = def.GetFieldCount();//3 for (int iField = 0; iField < nFieldCount; iField++) { // field definition FieldDefn fdef = def.GetFieldDefn(iField); // field info string fieldName = fdef.GetName(); //Id Name URL string fieldNameRef = fdef.GetNameRef(); // Id Name URL FieldType fieldType = fdef.GetFieldType(); //OFTInteger OFTString OFTString string fieldTypeName = fdef.GetFieldTypeName(fdef.GetFieldType()); //Integer String String int width = fdef.GetWidth(); //6 50 254 int precision = fdef.GetPrecision(); //0 0 0 } /* -------------------------------------------------------------------- */ /* Reading the shapes */ /* -------------------------------------------------------------------- */ for (int fid = 0; fid < layer.GetFeatureCount(1); fid++) { Feature f = layer.GetFeature(fid); ReportFeature(f, def); f.Dispose(); } //Feature f; //while ((f = layer.GetNextFeature()) != null) //{ // ReportFeature(f, def); // f.Dispose(); //} }
public void readLayerTest(string strVectorFile) { Gdal.AllRegister(); //为了支持中文路径,请添加下面这句代码 Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); //为了使属性表字段支持中文,请添加下面这句 Gdal.SetConfigOption("SHAPE_ENCODING", ""); // 注册所有的驱动 Ogr.RegisterAll(); Gdal.SetConfigOption("GDAL_DATA", "E://lib//gdal//gdal1.9//data"); //打开数据 DataSource ds = Ogr.Open(strVectorFile, 0); if (ds == null) { Console.WriteLine("打开文件【{0}】失败!", strVectorFile); return; } Console.WriteLine("打开文件【{0}】成功!", strVectorFile); // 获取该数据源中的图层个数,一般shp数据图层只有一个,如果是mdb、dxf等图层就会有多个 int iLayerCount = ds.GetLayerCount(); // 获取第一个图层 Layer oLayer = ds.GetLayerByIndex(0); String c = ""; oLayer.GetSpatialRef().AutoIdentifyEPSG(); Console.WriteLine(oLayer.GetSpatialRef().AutoIdentifyEPSG()); OSGeo.OSR.SpatialReference spa = oLayer.GetSpatialRef(); // if (spa==null) // { spa = new OSGeo.OSR.SpatialReference(null); spa.ImportFromEPSG(3395); spa.ExportToWkt(out c); Console.WriteLine(c); return; // } // String a = ""; // spa.EPSGTreatsAsLatLong(); // Console.WriteLine(spa.ExportToWkt(out a)); // oLayer.GetSpatialRef().ExportToWkt(out a); // Console.WriteLine(spa.GetProjParm(out a)); // Console.WriteLine(oLayer.GetSpatialRef().GetLinearUnitsName()); if (oLayer == null) { Console.WriteLine("获取第{0}个图层失败!\n", 0); return; } // 对图层进行初始化,如果对图层进行了过滤操作,执行这句后,之前的过滤全部清空 oLayer.ResetReading(); // 通过属性表的SQL语句对图层中的要素进行筛选,这部分详细参考SQL查询章节内容 // oLayer.SetAttributeFilter("\"NAME99\"LIKE \"北京市市辖区\""); // 通过指定的几何对象对图层中的要素进行筛选 //oLayer.SetSpatialFilter(); // 通过指定的四至范围对图层中的要素进行筛选 //oLayer.SetSpatialFilterRect(); // 获取图层中的属性表表头并输出 // Console.WriteLine("属性表结构信息:"); FeatureDefn oDefn = oLayer.GetLayerDefn(); int iFieldCount = oDefn.GetFieldCount(); for (int iAttr = 0; iAttr < iFieldCount; iAttr++) { FieldDefn oField = oDefn.GetFieldDefn(iAttr); Console.WriteLine("{0}:{1} ({2}.{3})", oField.GetNameRef(), oField.GetFieldTypeName(oField.GetFieldType()), oField.GetWidth(), oField.GetPrecision()); } // 输出图层中的要素个数 Console.WriteLine("要素个数 = {0}", oLayer.GetFeatureCount(0)); Feature oFeature = null; // 下面开始遍历图层中的要素 while ((oFeature = oLayer.GetNextFeature()) != null) { Console.WriteLine("当前处理第{0}个: \n属性值:", oFeature.GetFID()); // 获取要素中的属性表内容 for (int iField = 0; iField < iFieldCount; iField++) { FieldDefn oFieldDefn = oDefn.GetFieldDefn(iField); FieldType type = oFieldDefn.GetFieldType(); switch (type) { case FieldType.OFTString: Console.WriteLine("{0}\t", oFeature.GetFieldAsString(iField)); break; case FieldType.OFTReal: Console.WriteLine("{0}\t", oFeature.GetFieldAsDouble(iField)); break; case FieldType.OFTInteger: Console.WriteLine("{0}\t", oFeature.GetFieldAsInteger(iField)); break; default: Console.WriteLine("{0}\t", oFeature.GetFieldAsString(iField)); break; } } // 获取要素中的几何体 Geometry oGeometry = oFeature.GetGeometryRef(); // String a=oGeometry.GetGeometryName(); // String b = ""; // oGeometry.ExportToWkt(out b); Console.WriteLine(oGeometry.GetGeometryName()); // 为了演示,只输出一个要素信息 break; } Console.WriteLine("数据集关闭!"); Console.ReadLine(); }
public bool GetFieldContent(int index, List <MField> fieldList) { Feature oFeature = null; if ((oFeature = oLayer.GetFeature(index)) != null) { FeatureDefn oDefn = oLayer.GetLayerDefn(); int iFieldCount = oDefn.GetFieldCount(); // 查找字段属性 for (int i = 0; i < iFieldCount; i++) { FieldDefn oField = oDefn.GetFieldDefn(i); string sFeildName = oField.GetNameRef(); #region 获取属性字段 FieldType Ftype = oFeature.GetFieldType(sFeildName); string sTempType = ""; MField mField = new MField(); switch (Ftype) { case FieldType.OFTString: string sFValue = oFeature.GetFieldAsString(sFeildName); sTempType = "string"; mField.fieldValue = sFValue; mField.fieldName = mFiledList[i]; fieldList.Add(mField); break; case FieldType.OFTReal: double dFValue = oFeature.GetFieldAsDouble(sFeildName); sTempType = "float"; mField.fieldValue = dFValue.ToString(); mField.fieldName = mFiledList[i]; fieldList.Add(mField); break; case FieldType.OFTInteger: int iFValue = oFeature.GetFieldAsInteger(sFeildName); sTempType = "int"; mField.fieldValue = iFValue.ToString(); mField.fieldName = mFiledList[i]; fieldList.Add(mField); break; case FieldType.OFTInteger64: long lFValue = oFeature.GetFieldAsInteger64(sFeildName); sTempType = "long"; mField.fieldValue = lFValue.ToString(); mField.fieldName = mFiledList[i]; fieldList.Add(mField); break; case FieldType.OFTDate: int year = 0, month = 0, day = 0, hour = 0, minute = 0, flag = 0; float second = 0; oFeature.GetFieldAsDateTime(sFeildName, out year, out month, out day, out hour, out minute, out second, out flag); DateTime dt = new DateTime(year, month, day, hour, minute, (int)second); sTempType = "datetime"; mField.fieldValue = dt.ToString(); mField.fieldName = mFiledList[i]; fieldList.Add(mField); break; default: //System.Console.WriteLine("error!"); break; } #endregion } } return(true); }