/// <summary> /// DeleteShapeFile /// </summary> /// <returns></returns> public bool DeleteShapeFile() { bool rbc = false; IFeatureClass fc = null; try { fc = this.getIFeatureClass(); if (fc != null) { IDataset fds = fc as IDataset; fds.Delete(); fds = null; TokayWorkspace.ComRelease(fc); fc = null; } this.DeleteTempFile(this.LocalShapePathFileName); rbc = true; } catch (Exception ee) { System.Diagnostics.Debug.WriteLine(ee.ToString()); rbc = false; } finally { if (fc != null) { TokayWorkspace.ComRelease(fc); fc = null; } } return(rbc); }
/// <summary> /// deleteAllFeatures /// </summary> /// <returns></returns> public bool deleteAllFeatures() { bool rbc = false; try { IFeatureClass fc = this.getIFeatureClass(); if (fc != null) { ZhPointFeatureClass zhfc = new ZhPointFeatureClass(fc); zhfc.DeleteAllFeatures(); TokayWorkspace.ComRelease(fc); fc = null; zhfc = null; rbc = true; } } catch (Exception ee) { System.Diagnostics.Debug.WriteLine(ee.ToString()); rbc = false; } return(rbc); }
//---- private void RefreshSpatialReferenceText(string file) { this.textBox3.Text = ""; try { //首先获取文件名,文件路径及文件类型 string[] sArray = file.Split('\\'); string filename = sArray[sArray.Length - 1]; string url = ""; for (int i = 0; i < sArray.Length - 1; i++) { url = url + sArray[i] + "\\"; } string[] sFile = filename.Split('.'); string file_ext = ""; if (sFile.Length > 0) { file_ext = sFile[sFile.Length - 1]; } file_ext = file_ext.ToLower(); //---- ShapefileWorkspaceFactoryClass wsf = new ShapefileWorkspaceFactoryClass(); IWorkspace ws = wsf.OpenFromFile(url, 0); IFeatureWorkspace pFeatureWorkspace; pFeatureWorkspace = (IFeatureWorkspace)ws; IFeatureClass pFeatureClass = null; pFeatureClass = pFeatureWorkspace.OpenFeatureClass(filename); this.RefreshSpatialReferenceText(pFeatureClass); ZhFeatureClass zhfeaclass = new ZhPolygonFeatureClass(pFeatureClass); List <string> FieldNames = zhfeaclass.GetFieldNames(); this.CB_Fields.Items.Clear(); foreach (string fn in FieldNames) { this.CB_Fields.Items.Add(fn); } TokayWorkspace.ComRelease(pFeatureClass); pFeatureClass = null; TokayWorkspace.ComRelease(ws); ws = null; wsf = null; } catch (Exception ee) { this.textBox3.Text = ""; MessageBox.Show(ee.Message, "选择文件提示"); } }
/// <summary> /// 删除要素 by UpdateCursor更新游标方法 大于500,000条记录时用 /// </summary> public virtual bool DeleteFeaturesByUpdateCursor(IQueryFilter qf) { int fCount = 0; IFeatureCursor pFeatureCursor = this.featureClass.Update(qf, false); IFeature pFeature = pFeatureCursor.NextFeature(); while (pFeature != null) { fCount += 1; if (fCount % 1000 == 0) { Application.DoEvents(); } pFeatureCursor.DeleteFeature(); pFeature = pFeatureCursor.NextFeature(); } TokayWorkspace.ComRelease(pFeatureCursor); pFeatureCursor = null; return(true); }
//添加椭球面积字段 private void Btn_addEllipField_Click(object sender, EventArgs e) { try { string tpFileName = this.textBox1.Text; LocalShapeFileOperator shpOp = new LocalShapeFileOperator(); shpOp.LocalShapePathFileName = tpFileName; IFeatureClass fc = shpOp.getIFeatureClass(); shpOp.AddCustomAreaField(fc, "TQMJ", "椭球面积"); shpOp.AddCustomAreaField(fc, "TXMJ", "图形面积"); shpOp.Dispose(); TokayWorkspace.ComRelease(fc); fc = null; MessageBox.Show("添加椭球面积(TQMJ)字段成功!", "提示"); } catch (Exception ee) { MessageBox.Show(ee.Message, "提示"); } }
private void LoopCloseAllLayer(ILayer layer) { if (layer is ICompositeLayer) { ICompositeLayer cplayer = layer as ICompositeLayer; for (int j = cplayer.Count - 1; j >= 0; j--) { LoopCloseAllLayer(cplayer.get_Layer(j)); } } if (layer is IDataLayer2) { IDataLayer2 pDataLayer = (IDataLayer2)layer; pDataLayer.Disconnect(); } if (layer is IFeatureLayer) { IFeatureClass fc = (layer as IFeatureLayer).FeatureClass; TokayWorkspace.ComRelease(fc); fc = null; } this.Map.DeleteLayer(layer); }
//开始计算 private void Btn_Compute_Click(object sender, EventArgs e) { string tpFiledName = this.CB_Fields.Text; object obj = this.comboBox1.SelectedItem; if (obj != null && obj is CommonComboBoxItem) { CommonComboBoxItem item = obj as CommonComboBoxItem; IFeatureClass pFeatureClass = item.Tag as IFeatureClass; decimal ellipseArea = 0; AnyTrapeziaEllipseAreaCompute_JF jf = new AnyTrapeziaEllipseAreaCompute_JF(); //获取坐标系 jf.Datum = EnumProjectionDatum.CGCS2000; if (this.rb2000.Checked == true) { jf.Datum = EnumProjectionDatum.CGCS2000; } if (this.rb_xian80.Checked == true) { jf.Datum = EnumProjectionDatum.Xian80; } if (this.rb_beijing54.Checked == true) { jf.Datum = EnumProjectionDatum.Bejing54; } //获取大数设置 jf.IsBigNumber = false; if (this.CB_IsBigNumber.Checked == true) { jf.IsBigNumber = true; } //获取分度设置 jf.Strip = EnumStrip.Strip6; if (CB_FD.Checked == true) { jf.Strip = EnumStrip.Strip3; } //获取中央子午线 jf.L0 = CommonClass.TDec(this.txt_DD.Text) * (int)jf.Strip; int fIndex = 0; int tmpFCount = pFeatureClass.FeatureCount(null); frmProgressBar1 pb = new frmProgressBar1(); pb.Text = "正在进行椭球面积计算..."; pb.KDCaption1 = "正在进行椭球面积计算..."; pb.KDProgressBar1.Maximum = tmpFCount + 1; pb.KDProgressBar1.Value = 0; pb.Show(); Application.DoEvents(); IFeatureCursor fcur = pFeatureClass.Update(null, false); IFeature feat = fcur.NextFeature(); while (feat != null) { fIndex += 1; if (fIndex % 500 == 0) { pb.KDCaption1 = "已计算完成要素个数:" + fIndex.ToString() + "/总" + tmpFCount.ToString(); pb.KDProgressBar1.Value = fIndex; Application.DoEvents(); fcur.Flush(); } ellipseArea = jf.Compute(feat.ShapeCopy); //save ellipseArea... ZhFeature zhfeat = new ZHFeaturePolygon(feat); zhfeat.setFieldValue(tpFiledName, ellipseArea); fcur.UpdateFeature(feat); feat = fcur.NextFeature(); } fcur.Flush(); TokayWorkspace.ComRelease(fcur); fcur = null; pb.Close(); pb.Dispose(); pb = null; MessageBox.Show(this, "计算椭球面积完毕!", "提示"); } }
//开始平移 事件 private void Btn_MapMove_Click(object sender, EventArgs e) { frmProgressBar2 pb2 = null; try { this.Btn_MapMove.Enabled = false; this.Cursor = Cursors.WaitCursor; string LineText = ""; double dx = 0.0; double dy = 0.0; dx = CommonClass.TNum(this.numericBox1.Text.Trim()); dy = CommonClass.TNum(this.numericBox2.Text.Trim()); if (MessageBox.Show("请确认是否要对选中项进行整体平移操作?", "提示", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.Cancel) { this.Btn_MapMove.Enabled = true; this.Cursor = Cursors.Default; return; } //获取选中的要素类 List <IFeatureClass> fcList = new List <IFeatureClass>(); for (int i = 0; i < this.checkedListBox1.Items.Count; i++) { if (this.checkedListBox1.GetItemChecked(i) == true) { //选中要素类 CommonComboBoxItem cbitem = this.checkedListBox1.Items[i] as CommonComboBoxItem; fcList.Add(cbitem.Tag as IFeatureClass); } } if (fcList != null && fcList.Count > 0) { pb2 = new frmProgressBar2(); pb2.Text = "平移进度..."; pb2.progressBar1.Maximum = fcList.Count + 1; pb2.progressBar1.Value = 0; pb2.Caption1.Text = "正在平移..."; pb2.Show(this); Application.DoEvents(); // ZHFeaturePoint zhfeat = null; int featIndex = 0; int featCount = 0; for (int i = 0; i < fcList.Count; i++) { featIndex = 0; IFeatureClass fc = fcList[i]; pb2.Caption1.Text = "正在平移图层:" + fc.AliasName + "..."; pb2.progressBar1.Value = i; Application.DoEvents(); // featCount = fc.FeatureCount(null); pb2.progressBar2.Value = 0; pb2.progressBar2.Maximum = featCount + 1; pb2.Caption2.Text = "正在平移要素..."; Application.DoEvents(); // IFeatureCursor fcur = fc.Update(null, false); IFeature feat = fcur.NextFeature(); while (feat != null) { featIndex += 1; if (featIndex % 200 == 0) { pb2.Caption2.Text = "正在平移图层:" + fc.AliasName + "(当前:" + featIndex.ToString() + "/" + featCount.ToString() + ")"; pb2.progressBar2.Value = featIndex; Application.DoEvents(); fcur.Flush(); } zhfeat = new ZHFeaturePoint(feat); if (fc.FeatureType == esriFeatureType.esriFTAnnotation) { //注记要素移动 OK IAnnotationFeature anFeat = feat as IAnnotationFeature; IElement TextEl = (IElement)((IClone)anFeat.Annotation).Clone(); ITransform2D trans = TextEl as ITransform2D; trans.Move(dx, dy); anFeat.Annotation = TextEl; fcur.UpdateFeature(feat); } else { //一般要素移动 OK zhfeat.Move(dx, dy); fcur.UpdateFeature(feat); } // feat = fcur.NextFeature(); } fcur.Flush(); if (fcur != null) { TokayWorkspace.ComRelease(fcur); fcur = null; } pb2.Caption2.Text = "正在重新计算Extent范围..."; Application.DoEvents(); //重新计算Extent范围 TokayWorkspace.UpdateExtent(fc); } if (pb2 != null) { pb2.Close(); pb2.Dispose(); pb2 = null; } MessageBox.Show("地图平移完毕!", "提示"); } } catch (Exception ee) { Log.WriteLine(ee); MessageBox.Show(ee.Message, "提示"); } finally { this.Btn_MapMove.Enabled = true; this.Cursor = Cursors.Default; if (pb2 != null) { pb2.Close(); pb2.Dispose(); pb2 = null; } } }
//快速叠加传值功能 private void QuickOverlapTranValue() { IFeatureCursor fcur = null; mdbAccessLayerClass mdbOp = null; frmProgressBar1 pb = null; try { this.button1.Enabled = false; this.Cursor = Cursors.WaitCursor; //获取参数 string updateField = this.CB_Fields1.Text.Trim(); string valFromField = this.CB_Fields2.Text.Trim(); pb = new frmProgressBar1(); pb.Text = "空间叠加传值进度..."; pb.Caption1.Text = "预处理中..."; pb.progressBar1.Maximum = 10; pb.progressBar1.Value = 0; pb.Show(this); Application.DoEvents(); //1 获取和生成临时空间数据库 string tmpdir = Application.StartupPath + "\\TmpDir"; if (System.IO.Directory.Exists(tmpdir) == false) { System.IO.Directory.CreateDirectory(tmpdir); } this.tmpMdbFilePath = tmpdir + "\\GeoTransValue.mdb"; if (System.IO.File.Exists(this.tmpMdbFilePath) == true) { CommonClass.DeleteFile(this.tmpMdbFilePath); } //-- TokayWorkspace.CreateGeodatabase(this.tmpMdbFilePath); //2 开始空间叠加操作 pb.Caption1.Text = "空间叠加中..."; pb.progressBar1.Value += 1; Application.DoEvents(); //两个图层作叠加相交运算输出到一个mdb空间数据库中 string FID_fcName1 = ""; string fcName1 = ""; string fcName2 = ""; string fd_Shape_Area = "shape_area"; IFeatureClass fc1 = null; IFeatureClass fc2 = null; //目标图层 object obj = this.CB_LayerList1.SelectedItem; if (obj != null && obj is CommonComboBoxItem) { CommonComboBoxItem item = obj as CommonComboBoxItem; fc1 = item.Tag as IFeatureClass; fcName1 = (fc1 as IDataset).Name; } //源图层 obj = this.CB_LayerList2.SelectedItem; if (obj != null && obj is CommonComboBoxItem) { CommonComboBoxItem item = obj as CommonComboBoxItem; fc2 = item.Tag as IFeatureClass; fcName2 = (fc2 as IDataset).Name; fd_Shape_Area = fc2.ShapeFieldName + "_area"; } //生成的相交图层名称 tmpLayerName = fcName1 + "_" + fcName2; FID_fcName1 = "FID_" + fcName1; double TopoTolerance = 0.0001; IntersectCalculateMdbEx mdbIsCal = new IntersectCalculateMdbEx(); if (mdbIsCal.Execute(fc1, fc2, this.tmpMdbFilePath + "\\" + tmpLayerName, "INPUT", TopoTolerance) == true) { #region 空间传值中 pb.Caption1.Text = "空间传值中..."; pb.progressBar1.Value += 1; Application.DoEvents(); // ZhFeature zhfeat = null; int index_fc1 = fc1.Fields.FindField(valFromField); if (index_fc1 >= 0) { valFromField = valFromField + "_1"; } mdbOp = new mdbAccessLayerClass(this.tmpMdbFilePath); string x = "select " + FID_fcName1 + "," + valFromField + ",sum(" + fd_Shape_Area + ") as geo_area "; x += " from " + tmpLayerName + " "; x += " group by " + FID_fcName1 + "," + valFromField + ""; DataTable dt = mdbOp.GetMdbDB.ExecuteDataTable(x); if (dt != null && dt.Rows.Count > 0) { #region 值中 string oid = ""; int fc1_count = 0; int index = 0; string objval = ""; double geo_area = 0.0; double geo_area_max = 0.0; //更新值 fc1_count = fc1.FeatureCount(null); pb.progressBar1.Value = 0; pb.progressBar1.Maximum = fc1_count + 1; Application.DoEvents(); // fcur = fc1.Update(null, false); IFeature feat = fcur.NextFeature(); while (feat != null) { index += 1; if (index % 50 == 0) { pb.Caption1.Text = "空间叠加传值中...第[" + index + "]个/共[" + fc1_count + "]个"; pb.progressBar1.Value = index; Application.DoEvents(); fcur.Flush(); } zhfeat = new ZHFeaturePolygon(feat); oid = zhfeat.getObjectID(); // DataRow[] drArray = dt.Select(FID_fcName1 + "=" + oid); if (drArray != null && drArray.Length >= 1) { objval = ""; geo_area = 0.0; geo_area_max = 0.0; //获取面积最大的面积值 foreach (DataRow dr in drArray) { geo_area = CommonClass.TNum(dr["geo_area"].ToString()); if (geo_area >= geo_area_max) { objval = dr[valFromField].ToString(); geo_area_max = geo_area; } } //设置值 zhfeat.setFieldValue(updateField, objval); fcur.UpdateFeature(feat); } feat = fcur.NextFeature(); } fcur.Flush(); if (fcur != null) { TokayWorkspace.ComRelease(fcur); fcur = null; } #endregion } if (mdbOp != null) { mdbOp.GetMdbDB.Dispose(); mdbOp = null; } #endregion if (pb != null) { pb.Close(); pb.Dispose(); pb = null; } MessageBox.Show("传值完毕!", "提示"); } else { MessageBox.Show("空间叠加失败!" + mdbIsCal.Message, "提示"); } } catch (Exception ee) { Log.WriteLine(ee); MessageBox.Show(ee.Message, "提示"); } finally { this.button1.Enabled = true; this.Cursor = Cursors.Default; if (fcur != null) { TokayWorkspace.ComRelease(fcur); fcur = null; } if (mdbOp != null) { mdbOp.GetMdbDB.Dispose(); mdbOp = null; } if (pb != null) { pb.Close(); pb.Dispose(); pb = null; } } }
//输出单个图层中所有要素集合到shp文件中功能 OK public bool OutPut(IFeatureClass featclass, string outShapeFilePath) { bool rbc = false; if (System.IO.File.Exists(outShapeFilePath) == true) { //输出Shape文件 LocalShapeFileOperator delshpOp = new LocalShapeFileOperator(); delshpOp.LocalShapePathFileName = outShapeFilePath; delshpOp.DeleteShapeFile(); delshpOp.Dispose(); } esriGeometryType geoType = featclass.ShapeType; //创建新的Shape文件 LocalShapeFileOperator shpOp = new LocalShapeFileOperator(); shpOp.LocalShapePathFileName = outShapeFilePath; string dir = shpOp.getDir(outShapeFilePath); string name = shpOp.getFileName(outShapeFilePath); ShapefileWorkspaceFactoryClass objwsf = new ShapefileWorkspaceFactoryClass(); IWorkspace objws = objwsf.OpenFromFile(dir, 0); IFeatureWorkspace feaureWS = objws as IFeatureWorkspace; //设置投影 shpOp.ShapeSpatialReference = (featclass as IGeoDataset).SpatialReference; //设置shp文件的oid字段和几何字段 if (IsObjGeo == false) { if (featclass.HasOID == true) { this.OIDFieldName = featclass.OIDFieldName; } this.GeometryFieldName = featclass.ShapeFieldName; } shpOp.OIDFieldName = this.OIDFieldName; shpOp.GeometryFieldName = this.GeometryFieldName; //创建要素类 IFeatureClass fc = shpOp.CreateFeatureClass(feaureWS, name, esriFeatureType.esriFTSimple, geoType); if (fc != null) { TokayWorkspace.ComRelease(fc); fc = null; rbc = true; } TokayWorkspace.ComRelease(objws); feaureWS = null; objws = null; objwsf = null; //拷贝字段结构 IFeatureClass Dfeatclass = shpOp.getIFeatureClass(); ZhFeatureClass Szhfeatclass = new ZhPointFeatureClass(featclass); Szhfeatclass.CopyFieldsToObjectFeatureClass(Dfeatclass); //拷贝几何对象和属性数据 ZhFeature Szhfeat = null; IFeatureBuffer DfeatBuffer = null; ZhFeature Dzhfeat = null; IFeatureCursor featCurInsert = Dfeatclass.Insert(true); //获取总要素个数 int fIndex = 0; int tmpFCount = Szhfeatclass.FeatureClass.FeatureCount(null); frmProgressBar1 pb = new frmProgressBar1(); pb.Text = "正在输出shp文件..."; pb.Caption1.Text = "正在输出shp文件..."; pb.progressBar1.Maximum = tmpFCount + 1; pb.progressBar1.Value = 0; pb.Show(this.ParentForm); Application.DoEvents(); // IGeometry geo = null; // IFeatureCursor featcur = Szhfeatclass.FeatureClass.Search(null, false); IFeature feat = featcur.NextFeature(); while (feat != null) { fIndex += 1; if (fIndex % 500 == 0) { pb.Caption1.Text = "已输出要素个数:" + fIndex.ToString() + "/总" + tmpFCount.ToString(); pb.progressBar1.Value = fIndex; Application.DoEvents(); featCurInsert.Flush(); } DfeatBuffer = Dfeatclass.CreateFeatureBuffer(); //拷贝几何对象 geo = feat.ShapeCopy; //去掉Z值 IZAware pZaware = geo as IZAware; if (pZaware.ZAware == true) { pZaware.DropZs(); pZaware.ZAware = false; } // DfeatBuffer.Shape = geo; // //拷贝属性数据 Szhfeat = new ZHFeaturePoint(feat); Szhfeat.CopyField(ref DfeatBuffer); featCurInsert.InsertFeature(DfeatBuffer); feat = featcur.NextFeature(); } featCurInsert.Flush(); TokayWorkspace.ComRelease(featcur); featcur = null; TokayWorkspace.ComRelease(featCurInsert); featCurInsert = null; rbc = true; pb.Close(); pb.Dispose(); pb = null; return(rbc); }
// //输出外环面 shape public bool OutPutPolygonExteriorRing(IFeatureClass featclass, string outShapeFilePath, bool IsGetExtRingOfHaveInnerRing) { bool rbc = false; if (System.IO.File.Exists(outShapeFilePath) == true) { //输出Shape文件 LocalShapeFileOperator delshpOp = new LocalShapeFileOperator(); delshpOp.LocalShapePathFileName = outShapeFilePath; delshpOp.DeleteShapeFile(); delshpOp.Dispose(); } esriGeometryType geoType = featclass.ShapeType; //创建新的Shape文件 LocalShapeFileOperator shpOp = new LocalShapeFileOperator(); shpOp.LocalShapePathFileName = outShapeFilePath; string dir = shpOp.getDir(outShapeFilePath); string name = shpOp.getFileName(outShapeFilePath); ShapefileWorkspaceFactoryClass objwsf = new ShapefileWorkspaceFactoryClass(); IWorkspace objws = objwsf.OpenFromFile(dir, 0); IFeatureWorkspace feaureWS = objws as IFeatureWorkspace; //设置投影 ISpatialReference sr = (featclass as IGeoDataset).SpatialReference; shpOp.ShapeSpatialReference = sr; //设置shp文件的oid字段和几何字段 if (IsObjGeo == false) { if (featclass.HasOID == true) { this.OIDFieldName = featclass.OIDFieldName; } this.GeometryFieldName = featclass.ShapeFieldName; } shpOp.OIDFieldName = this.OIDFieldName; shpOp.GeometryFieldName = this.GeometryFieldName; //创建要素类 IFeatureClass fc = shpOp.CreateFeatureClass(feaureWS, name, esriFeatureType.esriFTSimple, geoType); if (fc != null) { TokayWorkspace.ComRelease(fc); fc = null; rbc = true; } TokayWorkspace.ComRelease(objws); feaureWS = null; objws = null; objwsf = null; //拷贝字段结构 IFeatureClass Dfeatclass = shpOp.getIFeatureClass(); ZhFeatureClass Szhfeatclass = new ZhPointFeatureClass(featclass); Szhfeatclass.CopyFieldsToObjectFeatureClass(Dfeatclass); //拷贝几何对象和属性数据 ZhFeature Szhfeat = null; IFeatureBuffer DfeatBuffer = null; IFeatureCursor featCurInsert = Dfeatclass.Insert(true); //获取总要素个数 int fIndex = 0; int tmpFCount = Szhfeatclass.FeatureClass.FeatureCount(null); frmProgressBar1 pb = new frmProgressBar1(); pb.Text = "正在输出shp文件..."; pb.Caption1.Text = "正在输出shp文件..."; pb.progressBar1.Maximum = tmpFCount + 1; pb.progressBar1.Value = 0; pb.Show(this.ParentForm); Application.DoEvents(); object refobj = Type.Missing; IPolygon s_p = null; IRing[] ExtRingArray = null; IRing[] InnerRingArray = null; IFeatureCursor featcur = Szhfeatclass.FeatureClass.Search(null, false); IFeature feat = featcur.NextFeature(); while (feat != null) { fIndex += 1; if (fIndex % 200 == 0) { pb.Caption1.Text = "已输出要素个数:" + fIndex.ToString() + "/总" + tmpFCount.ToString(); pb.progressBar1.Value = fIndex; Application.DoEvents(); featCurInsert.Flush(); } s_p = feat.ShapeCopy as IPolygon; //获取外环面几何对象 ExtRingArray = PolygonHelper.GetExteriorRings(s_p); if (ExtRingArray != null && ExtRingArray.Length > 0) { foreach (IRing r in ExtRingArray) { if (IsGetExtRingOfHaveInnerRing == true) { InnerRingArray = PolygonHelper.GetInteriorRingsByExterior(r, s_p); if (InnerRingArray != null && InnerRingArray.Length > 0) { //有内环 //外环构面并输出保存 DfeatBuffer = Dfeatclass.CreateFeatureBuffer(); PolygonClass pclass = new PolygonClass(); pclass.AddGeometry(r, ref refobj, ref refobj); pclass.SimplifyPreserveFromTo(); pclass.SpatialReference = sr; DfeatBuffer.Shape = pclass; //拷贝属性数据 Szhfeat = new ZHFeaturePoint(feat); Szhfeat.CopyField(ref DfeatBuffer); featCurInsert.InsertFeature(DfeatBuffer); // } } else { //外环构面并输出保存 DfeatBuffer = Dfeatclass.CreateFeatureBuffer(); PolygonClass pclass = new PolygonClass(); pclass.AddGeometry(r, ref refobj, ref refobj); pclass.SimplifyPreserveFromTo(); pclass.SpatialReference = sr; DfeatBuffer.Shape = pclass; //拷贝属性数据 Szhfeat = new ZHFeaturePoint(feat); Szhfeat.CopyField(ref DfeatBuffer); featCurInsert.InsertFeature(DfeatBuffer); // } } } feat = featcur.NextFeature(); } featCurInsert.Flush(); TokayWorkspace.ComRelease(featcur); featcur = null; TokayWorkspace.ComRelease(featCurInsert); featCurInsert = null; rbc = true; pb.Close(); pb.Dispose(); pb = null; return(rbc); }
private void BtnUpdate_Click(object sender, EventArgs e) { frmProgressBar2 pb2 = null; try { this.BtnUpdate.Enabled = false; this.Cursor = Cursors.WaitCursor; // string txtPath = this.textBox1.Text.Trim(); if (txtPath == "" || System.IO.File.Exists(txtPath) == false) { MessageBox.Show("请先选择YSDM对照txt文件", "提示"); this.BtnUpdate.Enabled = true; this.Cursor = Cursors.Default; return; } // if (MessageBox.Show("请确认是否要对选中项进行批量更新YSDM字段的值操作?", "提示", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.Cancel) { this.BtnUpdate.Enabled = true; this.Cursor = Cursors.Default; return; } //-------------------------------------- List <ItemClass> ItemList = new List <ItemClass>(); StreamReader sr = new StreamReader(txtPath, Encoding.Default); while (sr.EndOfStream == false) { string line = sr.ReadLine(); string[] lineArray = line.Split(new char[] { ',' }); if (lineArray != null && lineArray.Length >= 3) { ItemClass item = new ItemClass(); item.Value = lineArray[0].Trim(); //YSDM item.Caption = lineArray[1].Trim(); //YSDMName item.Tag = lineArray[2].Trim(); //LayerName // ItemList.Add(item); } } if (sr != null) { sr.Close(); sr.Dispose(); sr = null; } //-------------------------------------- //获取选中的要素类 List <IFeatureClass> fcList = new List <IFeatureClass>(); for (int i = 0; i < this.checkedListBox1.Items.Count; i++) { if (this.checkedListBox1.GetItemChecked(i) == true) { //选中要素类 CommonComboBoxItem cbitem = this.checkedListBox1.Items[i] as CommonComboBoxItem; fcList.Add(cbitem.Tag as IFeatureClass); } } if (fcList != null && fcList.Count > 0) { pb2 = new frmProgressBar2(); pb2.Text = "批量更新YSDM字段进度..."; pb2.progressBar1.Maximum = fcList.Count + 1; pb2.progressBar1.Value = 0; pb2.Caption1.Text = "正在更新..."; pb2.Show(this); Application.DoEvents(); // ZHFeaturePoint zhfeat = null; int featIndex = 0; int featCount = 0; // string YSDM = ""; for (int i = 0; i < fcList.Count; i++) { featIndex = 0; IFeatureClass fc = fcList[i]; pb2.Caption1.Text = "正在更新图层:" + fc.AliasName + "..."; pb2.progressBar1.Value = i; Application.DoEvents(); // featCount = fc.FeatureCount(null); pb2.progressBar2.Value = 0; pb2.progressBar2.Maximum = featCount + 1; pb2.Caption2.Text = "正在更新要素..."; Application.DoEvents(); //获取图层对应的要素代码值 string fcName = fc.AliasName; if (fc is IDataset) { fcName = (fc as IDataset).Name; } foreach (ItemClass item in ItemList) { string txtLayerName = item.Tag.ToString(); //------------------------------- if (cb_IsLayerLastPreStr.Checked == true) { //是后缀字符串 txtLayerName = txtLayerName + this.txt_LayerPreStr.Text.Trim(); } else { //是前缀字符串 txtLayerName = this.txt_LayerPreStr.Text.Trim() + txtLayerName; } //------------------------------- if (fcName.ToUpper() == txtLayerName.ToUpper()) { YSDM = item.Value.ToString(); break; } } // IFeatureCursor fcur = fc.Update(null, false); IFeature feat = fcur.NextFeature(); while (feat != null) { featIndex += 1; if (featIndex % 200 == 0) { pb2.Caption2.Text = "正在更新图层:" + fc.AliasName + "(当前:" + featIndex.ToString() + "/" + featCount.ToString() + ")"; pb2.progressBar2.Value = featIndex; Application.DoEvents(); fcur.Flush(); } zhfeat = new ZHFeaturePoint(feat); zhfeat.setFieldValue("YSDM", YSDM); fcur.UpdateFeature(feat); // feat = fcur.NextFeature(); } fcur.Flush(); if (fcur != null) { TokayWorkspace.ComRelease(fcur); fcur = null; } } if (pb2 != null) { pb2.Close(); pb2.Dispose(); pb2 = null; } MessageBox.Show("更新YSDM完毕!", "提示"); } } catch (Exception ee) { Log.WriteLine(ee); MessageBox.Show(ee.Message, "提示"); } finally { this.BtnUpdate.Enabled = true; this.Cursor = Cursors.Default; if (pb2 != null) { pb2.Close(); pb2.Dispose(); pb2 = null; } } }
//输出通过一个图层已选中的要素去选择输出其他图层shp文件功能 OK public bool OutPutSelectedFeatureByOverlap(IFeatureLayer SelectedFeatLayer, IFeatureClass outFeatClass, string outShapeFilePath) { bool rbc = false; if (System.IO.File.Exists(outShapeFilePath) == true) { //输出Shape文件 LocalShapeFileOperator delshpOp = new LocalShapeFileOperator(); delshpOp.LocalShapePathFileName = outShapeFilePath; delshpOp.DeleteShapeFile(); delshpOp.Dispose(); } esriGeometryType geoType = outFeatClass.ShapeType; //创建新的Shape文件 LocalShapeFileOperator shpOp = new LocalShapeFileOperator(); shpOp.LocalShapePathFileName = outShapeFilePath; shpOp.GeometryType = geoType; // string dir = shpOp.getDir(outShapeFilePath); string name = shpOp.getFileName(outShapeFilePath); ShapefileWorkspaceFactoryClass objwsf = new ShapefileWorkspaceFactoryClass(); IWorkspace objws = objwsf.OpenFromFile(dir, 0); IFeatureWorkspace feaureWS = objws as IFeatureWorkspace; //设置投影 shpOp.ShapeSpatialReference = (outFeatClass as IGeoDataset).SpatialReference; //设置shp文件的oid字段和几何字段 if (IsObjGeo == false) { if (outFeatClass.HasOID == true) { this.OIDFieldName = outFeatClass.OIDFieldName; } this.GeometryFieldName = outFeatClass.ShapeFieldName; } shpOp.OIDFieldName = this.OIDFieldName; shpOp.GeometryFieldName = this.GeometryFieldName; //创建要素类 IFeatureClass fc = shpOp.CreateFeatureClass(feaureWS, name, esriFeatureType.esriFTSimple, geoType); if (fc != null) { TokayWorkspace.ComRelease(fc); fc = null; rbc = true; } TokayWorkspace.ComRelease(objws); feaureWS = null; objws = null; objwsf = null; //拷贝字段结构 IFeatureClass Dfeatclass = shpOp.getIFeatureClass(); ZhFeatureClass Szhfeatclass = new ZhPointFeatureClass(outFeatClass); Szhfeatclass.CopyFieldsToObjectFeatureClass(Dfeatclass); //拷贝几何对象和属性数据 ZhFeature Szhfeat = null; IFeatureBuffer DfeatBuffer = null; ZhFeature Dzhfeat = null; IFeatureCursor featCurInsert = Dfeatclass.Insert(true); //获取总要素个数 int fIndex = 0; int tmpFCount = Szhfeatclass.FeatureClass.FeatureCount(null); frmProgressBar1 pb = new frmProgressBar1(); pb.Text = "正在输出shp文件..."; pb.Caption1.Text = "正在输出shp文件..."; pb.progressBar1.Maximum = tmpFCount + 1; pb.progressBar1.Value = 0; pb.Show(this.ParentForm); Application.DoEvents(); //获取已选择要的要素集合 IFeatureSelection featSel = SelectedFeatLayer as IFeatureSelection; ICursor SelectedCur = null; featSel.SelectionSet.Search(null, false, out SelectedCur); if (SelectedCur != null) { IFeatureCursor Selectedfeatcur = SelectedCur as IFeatureCursor;// Szhfeatclass.FeatureClass.Search(null, false); if (Selectedfeatcur != null) { IFeature Selectedfeat = Selectedfeatcur.NextFeature(); while (Selectedfeat != null) { ISpatialFilter sFilter = new SpatialFilterClass(); sFilter.Geometry = Selectedfeat.ShapeCopy; sFilter.GeometryField = Szhfeatclass.FeatureClass.ShapeFieldName; sFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; //相交 //总个数 pb.progressBar1.Maximum = Szhfeatclass.FeatureClass.FeatureCount(sFilter); pb.progressBar1.Value = 0; Application.DoEvents(); fIndex = 0; // IFeatureCursor featcur = Szhfeatclass.FeatureClass.Search(sFilter, false); IFeature feat = featcur.NextFeature(); while (feat != null) { fIndex += 1; // pb.Caption1.Text = "已输出要素个数... 第" + fIndex.ToString() + "个/总" + pb.progressBar1.Maximum.ToString() + "个"; pb.progressBar1.Value = fIndex; Application.DoEvents(); if (fIndex % 500 == 0) { featCurInsert.Flush(); } // DfeatBuffer = Dfeatclass.CreateFeatureBuffer(); //拷贝几何对象 DfeatBuffer.Shape = feat.ShapeCopy; //拷贝属性数据 Szhfeat = new ZHFeaturePoint(feat); Szhfeat.CopyField(ref DfeatBuffer); // featCurInsert.InsertFeature(DfeatBuffer); //下一个要素 feat = featcur.NextFeature(); } featCurInsert.Flush(); if (featcur != null) { TokayWorkspace.ComRelease(featcur); featcur = null; } //下一个选中的要素 Selectedfeat = Selectedfeatcur.NextFeature(); } if (Selectedfeatcur != null) { TokayWorkspace.ComRelease(Selectedfeatcur); Selectedfeatcur = null; } } if (featCurInsert != null) { TokayWorkspace.ComRelease(featCurInsert); featCurInsert = null; } rbc = true; } pb.Close(); pb.Dispose(); pb = null; // return(rbc); }
private void btnUpdate_Click(object sender, EventArgs e) { ProgressDialog pd = null; try { ItemClass item = this.comboBox1.SelectedItem as ItemClass; if (item == null) { MessageBox.Show("请先选择要更新的图层", "提示"); return; } ItemClass fd_item = this.comboBox2.SelectedItem as ItemClass; if (fd_item == null) { MessageBox.Show("请先选择要更新的字段", "提示"); return; } // ILayer layer = item.Value as ILayer; IFeatureLayer featLayer = layer as IFeatureLayer; IFeatureClass fc = featLayer.FeatureClass; // string fdName = fd_item.Caption; // int fCount = fc.FeatureCount(null); if (fCount > 0) { pd = new ProgressDialog(); pd.Text = "进度"; pd.Message = "计算扣除地类系数中......"; pd.Minimum = 0; pd.Maximum = fCount; pd.Show(this); // Application.DoEvents(); // IFeatureCursor pcursor = fc.Update(null, false); IFeature pf = pcursor.NextFeature(); int index = fc.FindField(fdName); // int n = 0; while (pf != null) { n = n + 1; if (n % 200 == 0) { pd.Value = n; pd.Message = "计算扣除地类系数中......" + pd.Value.ToString() + "/" + pd.Maximum.ToString(); Application.DoEvents(); pcursor.Flush(); } object v = pf.get_Value(index); if (v != null) { double t_kcdlxs = ToDouble(v); if (rb_fd4_100.Checked == true) { //除100后保留4位小数(针对【保护图斑】BHTB省标和国标) t_kcdlxs = t_kcdlxs / 100.0; if (cb_IsAwayFromZero.Checked == true) { t_kcdlxs = Math.Round(t_kcdlxs, 4, MidpointRounding.AwayFromZero); } else { t_kcdlxs = Math.Round(t_kcdlxs, 4); } } if (rb_fd2_100.Checked == true) { //除100后保留2位小数(针对【保护图斑】BHTB省标和国标) t_kcdlxs = t_kcdlxs / 100.0; if (cb_IsAwayFromZero.Checked == true) { t_kcdlxs = Math.Round(t_kcdlxs, 2, MidpointRounding.AwayFromZero); } else { t_kcdlxs = Math.Round(t_kcdlxs, 2); } } if (rb_fd4.Checked == true) { //保留4位小数 if (cb_IsAwayFromZero.Checked == true) { t_kcdlxs = Math.Round(t_kcdlxs, 4, MidpointRounding.AwayFromZero); } else { t_kcdlxs = Math.Round(t_kcdlxs, 4); } } if (rb_fd2.Checked == true) { //保留2位小数 if (cb_IsAwayFromZero.Checked == true) { t_kcdlxs = Math.Round(t_kcdlxs, 2, MidpointRounding.AwayFromZero); } else { t_kcdlxs = Math.Round(t_kcdlxs, 2); } } pf.set_Value(index, t_kcdlxs); pcursor.UpdateFeature(pf); // } pf = pcursor.NextFeature(); } pcursor.Flush(); if (pcursor != null) { TokayWorkspace.ComRelease(pcursor); pcursor = null; } if (pd != null) { pd.Dispose(); pd = null; } MessageBox.Show("更新完毕!", "提示"); } } catch (Exception ee) { MessageBox.Show(ee.StackTrace, "提示"); } finally { if (pd != null) { pd.Dispose(); pd = null; } } }
//自定义叠加传值 private void CustomOverlapTranValue() { frmProgressBar1 pb = null; // IFeatureCursor fCur = null; IFeatureCursor fCur_fc2 = null; // try { this.button1.Enabled = false; this.Cursor = Cursors.WaitCursor; //获取参数 string updateField = this.CB_Fields1.Text.Trim(); string valFromField = this.CB_Fields2.Text.Trim(); pb = new frmProgressBar1(); pb.Text = "空间叠加传值进度"; pb.Caption1.Text = "预处理中......"; pb.progressBar1.Maximum = 10; pb.progressBar1.Value = 0; pb.Show(this); Application.DoEvents(); //2 开始空间叠加操作 pb.Caption1.Text = "空间叠加中..."; pb.progressBar1.Value += 1; Application.DoEvents(); // IFeatureClass fc1 = null; IFeatureClass fc2 = null; // object objval = ""; double geo_area = 0.0; double geo_area_max = 0.0; //目标图层 object obj = this.CB_LayerList1.SelectedItem; if (obj != null && obj is CommonComboBoxItem) { CommonComboBoxItem item = obj as CommonComboBoxItem; fc1 = item.Tag as IFeatureClass; } //源图层 obj = this.CB_LayerList2.SelectedItem; if (obj != null && obj is CommonComboBoxItem) { CommonComboBoxItem item = obj as CommonComboBoxItem; fc2 = item.Tag as IFeatureClass; } // if (fc1 != null && fc2 != null) { pb.progressBar1.Maximum = fc1.FeatureCount(null); Application.DoEvents(); //目标图层 fCur = fc1.Update(null, false); if (fCur != null) { int recIndex = 0; IFeature feat_fc1 = fCur.NextFeature(); while (feat_fc1 != null) { ZhFeature zhfeat_fc1 = new ZHFeaturePolygon(feat_fc1); objval = ""; geo_area = 0.0; geo_area_max = 0.0; //获取面积最大的面积值 #region //与fc2相交运算功能 ISpatialFilter sFilter = new SpatialFilterClass(); sFilter.Geometry = feat_fc1.ShapeCopy; sFilter.GeometryField = fc2.ShapeFieldName; sFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; fCur_fc2 = fc2.Search(sFilter, false); IFeature feat_fc2 = fCur_fc2.NextFeature(); while (feat_fc2 != null) { ZhFeature zhfeat_fc2 = new ZHFeaturePolygon(feat_fc2); IGeometry t_geo = null; #region //获取相交对象 IGeometry topGeo = feat_fc1.ShapeCopy; IGeometry topGeo2 = feat_fc2.ShapeCopy; ITopologicalOperator Topoperator = (ITopologicalOperator)topGeo; Topoperator.Simplify(); ITopologicalOperator TopGeometry = (ITopologicalOperator)topGeo2; TopGeometry.Simplify(); try { t_geo = Topoperator.Intersect(topGeo2, topGeo.Dimension); } catch (Exception ee) { t_geo = null; } #endregion if (t_geo is IArea) { //面积 geo_area = (t_geo as IArea).Area; } else if (t_geo is ICurve) { //长度 geo_area = (t_geo as ICurve).Length; } if (geo_area >= geo_area_max) { objval = zhfeat_fc2.getFieldValue(valFromField); geo_area_max = geo_area; } //下一个要素 feat_fc2 = fCur_fc2.NextFeature(); } if (fCur_fc2 != null) { TokayWorkspace.ComRelease(fCur_fc2); fCur_fc2 = null; } #endregion //设置值 zhfeat_fc1.setFieldValue(updateField, objval); fCur.UpdateFeature(feat_fc1); // recIndex += 1; pb.progressBar1.Value = recIndex; pb.Caption1.Text = "正在叠加传值......... 第" + pb.progressBar1.Value + "个/共" + pb.progressBar1.Maximum + "个"; Application.DoEvents(); // //下一个要素 feat_fc1 = fCur.NextFeature(); } } if (fCur != null) { fCur.Flush(); } if (fCur != null) { TokayWorkspace.ComRelease(fCur); fCur = null; } if (pb != null) { pb.Close(); pb.Dispose(); pb = null; } MessageBox.Show("传值完毕!", "提示"); } } catch (Exception ee) { Log.WriteLine(ee); MessageBox.Show(ee.Message, "提示"); } finally { this.button1.Enabled = true; this.Cursor = Cursors.Default; if (fCur != null) { TokayWorkspace.ComRelease(fCur); fCur = null; } if (fCur_fc2 != null) { TokayWorkspace.ComRelease(fCur_fc2); fCur_fc2 = null; } if (pb != null) { pb.Close(); pb.Dispose(); pb = null; } } }
private void Btn_PreHandle_Click(object sender, EventArgs e) { //完成导出为mdb空间库中 try { //this.Btn_PreHandle.Enabled = false; this.Cursor = Cursors.WaitCursor; #region //两个图层作叠加相交运算输出到一个mdb空间数据库中 string fcName1 = ""; string fcName2 = ""; IFeatureClass fc1 = null; IFeatureClass fc2 = null; object obj = this.CB_LayerList1.SelectedItem; if (obj != null && obj is CommonComboBoxItem) { CommonComboBoxItem item = obj as CommonComboBoxItem; fc1 = item.Tag as IFeatureClass; fcName1 = (fc1 as IDataset).Name; } obj = this.CB_LayerList2.SelectedItem; if (obj != null && obj is CommonComboBoxItem) { CommonComboBoxItem item = obj as CommonComboBoxItem; fc2 = item.Tag as IFeatureClass; fcName2 = (fc2 as IDataset).Name; } tmpLayerName = fcName1 + "_" + fcName2; double TopoTolerance = 0.0001; IntersectCalculateMdbEx mdbIsCal = new IntersectCalculateMdbEx(); if (mdbIsCal.Execute(fc1, fc2, this.tmpMdbFilePath + "\\" + tmpLayerName, "INPUT", TopoTolerance) == true) { gdbUtil sic = new gdbUtil(); sic.enumSDEServer = EnumSDEServer.Access; sic.SDE_DataBase = this.tmpMdbFilePath; sic.OpenSDEConnection(); IFeatureClass mdbFc = sic.getFeatureClass(tmpLayerName); if (mdbFc != null) { this.GetFields(mdbFc); } TokayWorkspace.ComRelease(mdbFc); mdbFc = null; sic.Dispose(); sic = null; } #endregion } catch (Exception ee) { MessageBox.Show(ee.Message, "提示"); } finally { //this.Btn_PreHandle.Enabled = true; this.Cursor = Cursors.Default; } }
//开始经纬度计算椭球面积 private void Btn_JWDComputeElliArea_Click(object sender, EventArgs e) { try { this.Btn_JWDComputeElliArea.Enabled = false; this.Cursor = Cursors.WaitCursor; string tpFileName = this.textBox1.Text; if (tpFileName == "" || System.IO.File.Exists(tpFileName) == false) { MessageBox.Show("需先选择一个shp文件", "提示"); return; } string tpFiledName = this.CB_Fields.Text; string[] sArray = tpFileName.Split('\\'); string filename = sArray[sArray.Length - 1]; string url = ""; for (int i = 0; i < sArray.Length - 1; i++) { url = url + sArray[i] + "\\"; } string[] sFile = filename.Split('.'); string file_ext = ""; if (sFile.Length > 0) { file_ext = sFile[sFile.Length - 1]; } file_ext = file_ext.ToLower(); //---- ShapefileWorkspaceFactoryClass wsf = new ShapefileWorkspaceFactoryClass(); IWorkspace ws = wsf.OpenFromFile(url, 0); IFeatureWorkspace pFeatureWorkspace; pFeatureWorkspace = (IFeatureWorkspace)ws; IFeatureClass pFeatureClass = null; pFeatureClass = pFeatureWorkspace.OpenFeatureClass(filename); decimal ellipseArea = 0; AnyTrapeziaEllipseAreaCompute_JF_WGS1984 jf = new AnyTrapeziaEllipseAreaCompute_JF_WGS1984(); IFeatureCursor fcur = pFeatureClass.Search(null, false); IFeature feat = fcur.NextFeature(); while (feat != null) { ellipseArea = jf.Compute(feat.ShapeCopy); //save ellipseArea... ZhFeature zhfeat = new ZHFeaturePolygon(feat); zhfeat.setFieldValue(tpFiledName, ellipseArea); zhfeat.setFieldValue("TXMJ", zhfeat.GeometryArea); zhfeat.SaveFeature(); feat = fcur.NextFeature(); } TokayWorkspace.ComRelease(fcur); fcur = null; TokayWorkspace.ComRelease(ws); ws = null; MessageBox.Show(this, "计算椭球面积完毕!", "提示"); } catch (Exception ee) { Log.WriteLine(ee); MessageBox.Show(this, ee.Message, "提示"); } finally { this.Btn_JWDComputeElliArea.Enabled = true; this.Cursor = Cursors.Default; } }
//开始计算 private void Btn_Compute_Click(object sender, EventArgs e) { try { this.Btn_Compute.Enabled = false; this.Cursor = Cursors.WaitCursor; string tpFileName = this.textBox1.Text; if (tpFileName == "" || System.IO.File.Exists(tpFileName) == false) { MessageBox.Show("需先选择一个shp文件", "提示"); return; } string tpFiledName = this.CB_Fields.Text; string[] sArray = tpFileName.Split('\\'); string filename = sArray[sArray.Length - 1]; string url = ""; for (int i = 0; i < sArray.Length - 1; i++) { url = url + sArray[i] + "\\"; } string[] sFile = filename.Split('.'); string file_ext = ""; if (sFile.Length > 0) { file_ext = sFile[sFile.Length - 1]; } file_ext = file_ext.ToLower(); //---- ShapefileWorkspaceFactoryClass wsf = new ShapefileWorkspaceFactoryClass(); IWorkspace ws = wsf.OpenFromFile(url, 0); IFeatureWorkspace pFeatureWorkspace; pFeatureWorkspace = (IFeatureWorkspace)ws; IFeatureClass pFeatureClass = null; pFeatureClass = pFeatureWorkspace.OpenFeatureClass(filename); decimal ellipseArea = 0; AnyTrapeziaEllipseAreaCompute_JF jf = new AnyTrapeziaEllipseAreaCompute_JF(); jf.Datum = EnumProjectionDatum.Bejing54; if (CB_Datum.Checked == true) { jf.Datum = EnumProjectionDatum.Xian80; } jf.IsBigNumber = false; if (this.CB_IsBigNumber.Checked == true) { jf.IsBigNumber = true; } jf.Strip = EnumStrip.Strip6; if (CB_FD.Checked == true) { jf.Strip = EnumStrip.Strip3; } jf.L0 = CommonClass.TDec(this.txt_DD.Text) * (int)jf.Strip; int fIndex = 0; int tmpFCount = pFeatureClass.FeatureCount(null); frmProgressBar1 pb = new frmProgressBar1(); pb.Text = "正在进行椭球面积计算..."; pb.Caption1.Text = "正在进行椭球面积计算..."; pb.progressBar1.Maximum = tmpFCount + 1; pb.progressBar1.Value = 0; pb.Show(); Application.DoEvents(); IFeatureCursor fcur = pFeatureClass.Update(null, false); IFeature feat = fcur.NextFeature(); while (feat != null) { fIndex += 1; if (fIndex % 500 == 0) { pb.Caption1.Text = "已计算完成要素个数:" + fIndex.ToString() + "/总" + tmpFCount.ToString(); pb.progressBar1.Value = fIndex; Application.DoEvents(); fcur.Flush(); } ellipseArea = jf.Compute(feat.ShapeCopy); //save ellipseArea... ZhFeature zhfeat = new ZHFeaturePolygon(feat); zhfeat.setFieldValue(tpFiledName, ellipseArea); zhfeat.setFieldValue("TXMJ", zhfeat.GeometryArea); fcur.UpdateFeature(feat); //zhfeat.SaveFeature(); feat = fcur.NextFeature(); } fcur.Flush(); TokayWorkspace.ComRelease(fcur); fcur = null; TokayWorkspace.ComRelease(ws); ws = null; pb.Close(); pb.Dispose(); pb = null; MessageBox.Show(this, "计算椭球面积完毕!", "提示"); } catch (Exception ee) { Log.WriteLine(ee); MessageBox.Show(this, ee.Message, "提示"); } finally { this.Btn_Compute.Enabled = true; this.Cursor = Cursors.Default; } }
//开始批量更新 事件 private void BtnUpdate_Click(object sender, EventArgs e) { frmProgressBar2 pb2 = null; try { this.BtnUpdate.Enabled = false; this.Cursor = Cursors.WaitCursor; string LineText = ""; if (MessageBox.Show("请确认是否要对选中项进行批量更新BSM字段的值操作?", "提示", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.Cancel) { this.BtnUpdate.Enabled = true; this.Cursor = Cursors.Default; return; } //获取选中的要素类 List <IFeatureClass> fcList = new List <IFeatureClass>(); for (int i = 0; i < this.checkedListBox1.Items.Count; i++) { if (this.checkedListBox1.GetItemChecked(i) == true) { //选中要素类 CommonComboBoxItem cbitem = this.checkedListBox1.Items[i] as CommonComboBoxItem; fcList.Add(cbitem.Tag as IFeatureClass); } } if (fcList != null && fcList.Count > 0) { pb2 = new frmProgressBar2(); pb2.Text = "批量更新BSM字段进度..."; pb2.progressBar1.Maximum = fcList.Count + 1; pb2.progressBar1.Value = 0; pb2.Caption1.Text = "正在更新..."; pb2.Show(this); Application.DoEvents(); // ZHFeaturePoint zhfeat = null; int featIndex = 0; int featCount = 0; // int BSMIndex = 0; for (int i = 0; i < fcList.Count; i++) { featIndex = 0; IFeatureClass fc = fcList[i]; pb2.Caption1.Text = "正在更新图层:" + fc.AliasName + "..."; pb2.progressBar1.Value = i; Application.DoEvents(); // featCount = fc.FeatureCount(null); pb2.progressBar2.Value = 0; pb2.progressBar2.Maximum = featCount + 1; pb2.Caption2.Text = "正在更新要素..."; Application.DoEvents(); // IFeatureCursor fcur = fc.Update(null, false); IFeature feat = fcur.NextFeature(); while (feat != null) { featIndex += 1; //--- BSMIndex += 1; //--- if (featIndex % 200 == 0) { pb2.Caption2.Text = "正在更新图层:" + fc.AliasName + "(当前:" + featIndex.ToString() + "/" + featCount.ToString() + ")"; pb2.progressBar2.Value = featIndex; Application.DoEvents(); fcur.Flush(); } zhfeat = new ZHFeaturePoint(feat); zhfeat.setFieldValue("BSM", BSMIndex); fcur.UpdateFeature(feat); // feat = fcur.NextFeature(); } fcur.Flush(); if (fcur != null) { TokayWorkspace.ComRelease(fcur); fcur = null; } } if (pb2 != null) { pb2.Close(); pb2.Dispose(); pb2 = null; } MessageBox.Show("更新BSM完毕!", "提示"); } } catch (Exception ee) { Log.WriteLine(ee); MessageBox.Show(ee.Message, "提示"); } finally { this.BtnUpdate.Enabled = true; this.Cursor = Cursors.Default; if (pb2 != null) { pb2.Close(); pb2.Dispose(); pb2 = null; } } }
private void button1_Click(object sender, EventArgs e) { ProgressDialog pd = null; try { CommonComboBoxItem item = this.comboBox1.SelectedItem as CommonComboBoxItem; if (item == null) { MessageBox.Show("请先选择要计算的图层", "提示"); return; } string fd_item = this.CB_Fields.Text; if (fd_item == "") { MessageBox.Show("请先选择要计算的字段", "提示"); return; } //ILayer layer = item.Tag as ILayer; //IFeatureLayer featLayer = layer as IFeatureLayer; IFeatureClass fc = item.Tag as IFeatureClass; // string fdName = this.CB_Fields.Text; // int fCount = fc.FeatureCount(null); if (fCount > 0) { IFeatureCursor pcursor = fc.Update(null, false); IFeature pf = pcursor.NextFeature(); int index = fc.FindField(fdName); pd = new ProgressDialog(); pd.Text = "进度"; pd.Message = "计算字段图形面积中......"; pd.Minimum = 0; pd.Maximum = fCount; pd.Show(this); // Application.DoEvents(); double t_area = 0.0; int n = 0; while (pf != null) { n = n + 1; if (n % 200 == 0) { pd.Value = n; pd.Message = "计算字段图形面积中......" + pd.Value.ToString() + "/" + pd.Maximum.ToString(); Application.DoEvents(); pcursor.Flush(); } IArea area = (pf.Shape as IArea); if (area != null) { t_area = area.Area; pf.set_Value(index, t_area); // pcursor.UpdateFeature(pf); } // pf = pcursor.NextFeature(); } pcursor.Flush(); if (pcursor != null) { TokayWorkspace.ComRelease(pcursor); pcursor = null; } if (pd != null) { pd.Dispose(); pd = null; } MessageBox.Show("计算完毕!", "提示"); } } catch (Exception ee) { MessageBox.Show(ee.StackTrace, "提示"); } finally { if (pd != null) { pd.Dispose(); pd = null; } } }
//输出单个图层中已选中的要素集合到shp文件中功能 OK public bool OutPutSelectedFeature(IFeatureLayer featLayer, string outShapeFilePath) { IFeatureClass featclass = featLayer.FeatureClass; bool rbc = false; if (System.IO.File.Exists(outShapeFilePath) == true) { //输出Shape文件 LocalShapeFileOperator delshpOp = new LocalShapeFileOperator(); delshpOp.LocalShapePathFileName = outShapeFilePath; delshpOp.DeleteShapeFile(); delshpOp.Dispose(); } esriGeometryType geoType = featclass.ShapeType; //创建新的Shape文件 LocalShapeFileOperator shpOp = new LocalShapeFileOperator(); shpOp.LocalShapePathFileName = outShapeFilePath; string dir = shpOp.getDir(outShapeFilePath); string name = shpOp.getFileName(outShapeFilePath); ShapefileWorkspaceFactoryClass objwsf = new ShapefileWorkspaceFactoryClass(); IWorkspace objws = objwsf.OpenFromFile(dir, 0); IFeatureWorkspace feaureWS = objws as IFeatureWorkspace; //设置投影 shpOp.ShapeSpatialReference = (featclass as IGeoDataset).SpatialReference; //设置shp文件的oid字段和几何字段 if (IsObjGeo == false) { if (featclass.HasOID == true) { this.OIDFieldName = featclass.OIDFieldName; } this.GeometryFieldName = featclass.ShapeFieldName; } shpOp.OIDFieldName = this.OIDFieldName; shpOp.GeometryFieldName = this.GeometryFieldName; //创建要素类 IFeatureClass fc = shpOp.CreateFeatureClass(feaureWS, name, esriFeatureType.esriFTSimple, geoType); if (fc != null) { TokayWorkspace.ComRelease(fc); fc = null; rbc = true; } TokayWorkspace.ComRelease(objws); feaureWS = null; objws = null; objwsf = null; //拷贝字段结构 IFeatureClass Dfeatclass = shpOp.getIFeatureClass(); ZhFeatureClass Szhfeatclass = new ZhPointFeatureClass(featclass); Szhfeatclass.CopyFieldsToObjectFeatureClass(Dfeatclass); //拷贝几何对象和属性数据 ZhFeature Szhfeat = null; IFeatureBuffer DfeatBuffer = null; ZhFeature Dzhfeat = null; IFeatureCursor featCurInsert = Dfeatclass.Insert(true); //获取已选择要的要素集合 IFeatureSelection featSel = featLayer as IFeatureSelection; ICursor cur = null; featSel.SelectionSet.Search(null, false, out cur); if (cur != null) { IFeatureCursor featcur = cur as IFeatureCursor;// Szhfeatclass.FeatureClass.Search(null, false); if (featcur != null) { IFeature feat = featcur.NextFeature(); while (feat != null) { DfeatBuffer = Dfeatclass.CreateFeatureBuffer(); //拷贝几何对象 DfeatBuffer.Shape = feat.ShapeCopy; //拷贝属性数据 Szhfeat = new ZHFeaturePoint(feat); Szhfeat.CopyField(ref DfeatBuffer); featCurInsert.InsertFeature(DfeatBuffer); feat = featcur.NextFeature(); } } TokayWorkspace.ComRelease(featcur); featcur = null; } featCurInsert.Flush(); TokayWorkspace.ComRelease(featCurInsert); featCurInsert = null; rbc = true; return(rbc); }
private void btnUpdate_Click(object sender, EventArgs e) { ProgressDialog pd = null; try { ItemClass item = this.comboBox1.SelectedItem as ItemClass; if (item == null) { MessageBox.Show("请先选择要更新的图层", "提示"); return; } ItemClass fd_item = this.comboBox2.SelectedItem as ItemClass; if (fd_item == null) { MessageBox.Show("请先选择要更新的字段", "提示"); return; } ItemClass fd_from = this.comboBox3.SelectedItem as ItemClass; if (fd_from == null) { MessageBox.Show("请先选择值来源的字段", "提示"); return; } int index_from = CommonClass.TInt(this.fd_indexFrom.Value); int index_to = CommonClass.TInt(this.fd_indexTo.Value); if (index_from < 0) { MessageBox.Show("取子字符串只能从大于等于0开始!", "提示"); return; } // ILayer layer = item.Value as ILayer; IFeatureLayer featLayer = layer as IFeatureLayer; IFeatureClass fc = featLayer.FeatureClass; // string fdName = fd_item.Caption; string fdfromName = fd_from.Caption; // int fCount = fc.FeatureCount(null); if (fCount > 0) { pd = new ProgressDialog(); pd.Text = "进度"; pd.Message = "计算扣除地类系数中......"; pd.Minimum = 0; pd.Maximum = fCount; pd.Show(this); // Application.DoEvents(); // IFeatureCursor pcursor = fc.Update(null, false); IFeature pf = pcursor.NextFeature(); int index = fc.FindField(fdfromName); int indexUpdate = fc.FindField(fdName); // int n = 0; while (pf != null) { n = n + 1; if (n % 200 == 0) { pd.Value = n; pd.Message = "取子字符串中......" + pd.Value.ToString() + "/" + pd.Maximum.ToString(); Application.DoEvents(); pcursor.Flush(); } object v = pf.get_Value(index); if (v != null) { string value_from = v.ToString(); string result_v = value_from; if (index_to < value_from.Length) { result_v = value_from.Substring(index_from, index_to); } else { result_v = value_from.Substring(index_from); } // pf.set_Value(indexUpdate, result_v); pcursor.UpdateFeature(pf); } pf = pcursor.NextFeature(); } pcursor.Flush(); if (pcursor != null) { TokayWorkspace.ComRelease(pcursor); pcursor = null; } if (pd != null) { pd.Dispose(); pd = null; } MessageBox.Show("更新完毕!", "提示"); } } catch (Exception ee) { MessageBox.Show(ee.StackTrace, "提示"); } finally { if (pd != null) { pd.Dispose(); pd = null; } } }