public virtual bool IsIntersects(ZhFeature uZHFeature) { bool rbc = false; rbc = this.IsIntersects(uZHFeature.pFeature.Shape); return(rbc); }
/// <summary> /// 更新几何对象长度到一个自定义的字段中 /// </summary> /// <param name="CustomLengthField">自定义的字段</param> /// <returns></returns> public bool UpdateShapeLengthToCustomAreaField(IFeatureClass fc, string CustomLengthField) { ZhFeature zhfeat = null; double interLength = 0; IGeometry geo = null; if (fc != null) { IFeatureCursor featCur = fc.Update(null, false); IFeature feat = featCur.NextFeature(); while (feat != null) { interLength = 0; zhfeat = new ZHFeaturePolygon(feat); geo = zhfeat.pFeature.ShapeCopy; if (geo is ICurve) { interLength = (geo as ICurve).Length; } zhfeat.setFieldValue(CustomLengthField, interLength); featCur.UpdateFeature(feat); feat = featCur.NextFeature(); } featCur.Flush(); System.Runtime.InteropServices.Marshal.ReleaseComObject(featCur); } return(true); }
/// <summary> /// 拷贝属性字段 this.Fields->tpObjectZHFeature.Fields /// </summary> /// <param name="tpObjectZHFeature"></param> public virtual void CopyField(ref ZhFeature tpObjectZHFeature) { //属性 this.preGetFeatureFieldNames(); foreach (string fdName in this.FieldNames) { if (fdName != "OBJECTID" && fdName != "FID" && this.getFieldValue(fdName) != null) { tpObjectZHFeature.setFieldValue(fdName, this.getFieldValue(fdName)); } } }
private IGeometry[] MuliCutPolygon(ZhFeature tpFeat, IGeometry[] byCutGeoArray, IGeometry tpUsedGeo) { List <IGeometry> GeoList = new List <IGeometry>(); GeoList.Clear(); IGeometry[] tpcgGeoArray = null; foreach (IGeometry tpGeo in byCutGeoArray) { tpcgGeoArray = tpFeat.Cut(tpGeo, tpUsedGeo); foreach (IGeometry ogeo in tpcgGeoArray) { GeoList.Add(ogeo); } } return(GeoList.ToArray()); }
public virtual void Update(ZhFeature pZHFeat, string fieldNames, object[] objValues) { if (pZHFeat == null) { return; } string[] FNamesArray = null; FNamesArray = fieldNames.Split(new char[] { ',' }); for (int i = 0; i < FNamesArray.Length; i++) { pZHFeat.setFieldValue(FNamesArray[i], objValues[i]); } pZHFeat.SaveFeature(); FNamesArray = null; }
//分割面 public virtual ZhFeature[] SplitPolygon(ZhFeature[] zhFeatures, ZhFeature[] pUsedZHFeatures) { List <ZhFeature> pMuliFeatures = new List <ZhFeature>(); List <IGeometry> GeoList = new List <IGeometry>(); GeoList.Clear(); IGeometry[] byCutGeoArray = null; IGeometry[] cgGeoArray = null; //几何对象分割 foreach (ZhFeature tpByFeat in zhFeatures) { byCutGeoArray = null; cgGeoArray = null; GeoList.Clear(); GeoList.Add(tpByFeat.pFeature.ShapeCopy); byCutGeoArray = GeoList.ToArray(); foreach (ZhFeature tpUsedFeat in pUsedZHFeatures) { cgGeoArray = this.MuliCutPolygon(tpByFeat, byCutGeoArray, tpUsedFeat.pFeature.ShapeCopy); byCutGeoArray = null; byCutGeoArray = cgGeoArray; } //byCutGeoArray //清除原对象 GeoList.Clear(); //加入分割后对象集合 foreach (IGeometry ogeo in byCutGeoArray) { GeoList.Add(ogeo); } //创建分割后对象 ZHFeatureByGeoList ZhFeature tpFeat = null; foreach (IGeometry pGeo in GeoList) { tpFeat = this.CreateFeature(); //属性拷贝(含GHDM赋码) tpByFeat.CopyField(ref tpFeat); tpFeat.pFeature.Shape = pGeo; //保存 tpFeat.pFeature.Store(); pMuliFeatures.Add(tpFeat); } } return(pMuliFeatures.ToArray()); }
//求相交点集合 public IGeometry[] MuliCutPolyline(ZhFeature tpFeat, ZhFeature tpUsedFeat) { List <IGeometry> GeoList = new List <IGeometry>(); GeoList.Clear(); IGeometry tpGeo = null; IPolyline pcutter = null; switch (tpUsedFeat.pFeature.Shape.Dimension) { case esriGeometryDimension.esriGeometry2Dimension: ITopologicalOperator pTop = tpUsedFeat.pFeature.ShapeCopy as ITopologicalOperator; pTop.Simplify(); pcutter = pTop.Boundary as IPolyline; break; case esriGeometryDimension.esriGeometry1Dimension: pcutter = tpUsedFeat.pFeature.ShapeCopy as IPolyline; break; } //线线相交 tpGeo = tpFeat.IntersectsSameDim(pcutter, esriGeometryDimension.esriGeometry0Dimension); if (tpGeo is IMultipoint) { IMultipoint Multigeos = tpGeo as IMultipoint; IGeometryCollection geoColl = (IGeometryCollection)Multigeos; IGeometry geo = null; for (int i = 0; i < geoColl.GeometryCount; i++) { geo = geoColl.get_Geometry(i); if (geo is IPoint) { (geo as ITopologicalOperator).Simplify(); GeoList.Add(geo); } } } return(GeoList.ToArray()); }
public virtual void Update(string WhereClause, string fieldNames, object[] objValues) { IQueryFilter oQf = new QueryFilterClass(); oQf.WhereClause = WhereClause; oQf.SubFields = fieldNames; IFeatureCursor oFCursor = this.featureClass.Update(oQf, false); IFeature pFeat = oFCursor.NextFeature(); ZhFeature pZHFeat = null; while (pFeat != null) { pZHFeat = new ZHFeaturePoint(); pZHFeat.pFeature = pFeat; this.Update(pZHFeat, fieldNames, objValues); pFeat = oFCursor.NextFeature(); } pFeat = null; pZHFeat = null; oFCursor = null; oQf = null; }
//分割线 public virtual ZhFeature[] SplitPolyline(ZhFeature[] zhFeatures, ZhFeature[] pUsedZHFeatures) { List <ZhFeature> pMuliFeatures = new List <ZhFeature>(); List <IGeometry> GeoList = new List <IGeometry>(); GeoList.Clear(); List <IGeometry> CutterGeoList = new List <IGeometry>(); IPointCollection pMultipoint = null; IGeometry[] cgGeoArray = null; //几何对象分割 foreach (ZhFeature tpByFeat in zhFeatures) { CutterGeoList.Clear(); cgGeoArray = null; object obj = Type.Missing; IPolyline cutterPolyline = new PolylineClass(); pMultipoint = (IPointCollection)cutterPolyline; //求相交点的所有集合 foreach (ZhFeature tpUsedFeat in pUsedZHFeatures) { //求相交点集合 cgGeoArray = this.MuliCutPolyline(tpByFeat, tpUsedFeat); foreach (IGeometry tGeo in cgGeoArray) { if (((IRelationalOperator)pMultipoint).Contains(tGeo) == false) { pMultipoint.AddPoint((IPoint)tGeo, ref obj, ref obj); } } } //添加原线端点 //pMultipoint.AddPointCollection((IPointCollection)tpByFeat.pFeature.ShapeCopy); //拓朴化处理 去掉重复点操作 //ITopologicalOperator pTop = pMultipoint as ITopologicalOperator; //pTop.Simplify(); //pMultipoint = pTop as IPointCollection; //清除原对象 GeoList.Clear(); //用相交点集合分割线 if (tpByFeat.pFeature.Shape is IPolycurve2) { //IPoint outVertex = null; //IPoint preVertex = null; //int outPartIndex = 0; //int vertexIndex = 0; IPolycurve2 Curve2 = tpByFeat.pFeature.Shape as IPolycurve2; IEnumVertex SplitPoints = pMultipoint.EnumVertices; Curve2.SplitAtPoints(SplitPoints, true, true, -1); IGeometryCollection pcgGeoColl = Curve2 as IGeometryCollection; for (int i = 0; i < pcgGeoColl.GeometryCount; i++) { IGeometry tpcgGeo = pcgGeoColl.get_Geometry(i); IGeometryCollection oGeoCol = new PolylineClass(); oGeoCol.AddGeometries(1, ref tpcgGeo); if (((ITopologicalOperator)oGeoCol).IsSimple == false) { ((ITopologicalOperator)oGeoCol).Simplify(); } GeoList.Add(oGeoCol as IGeometry); } #region IEnumSplitPoint //IEnumSplitPoint cgEnumPoint=Curve2.SplitAtPoints(SplitPoints,false,false,-1); //cgEnumPoint.Reset(); //cgEnumPoint.Next(out outVertex, out outPartIndex, out vertexIndex); //while (outVertex != null && outVertex.IsEmpty != true && cgEnumPoint.IsLastInPart()!=true) //{ // preVertex=outVertex; // cgEnumPoint.Next(out outVertex, out outPartIndex, out vertexIndex); // if (preVertex != null && outVertex != null && outVertex.IsEmpty!=true) // { // IPolyline pcgPolyline = new PolylineClass(); // (pcgPolyline as IPointCollection).AddPoint(preVertex, ref obj, ref obj); // (pcgPolyline as IPointCollection).AddPoint(outVertex, ref obj, ref obj); // if (pcgPolyline.IsEmpty != true) // { // GeoList.Add(pcgPolyline); // } // } //} #endregion } //创建分割后对象 ZHFeatureByGeoList ZhFeature tpFeat = null; foreach (IGeometry pGeo in GeoList) { if (pGeo.IsEmpty != true) { tpFeat = this.CreateFeature(); //属性拷贝(含GHDM赋码) tpByFeat.CopyField(ref tpFeat); tpFeat.pFeature.Shape = pGeo; //保存 //tpFeat.pFeature.Store(); pMuliFeatures.Add(tpFeat); } } } return(pMuliFeatures.ToArray()); }
// //输出外环面 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); }
//输出单个图层中所有要素集合到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); }
//输出通过一个图层已选中的要素去选择输出其他图层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); }
//输出单个图层中已选中的要素集合到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 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; } } }
private void ShapeFileProjectionConvert(ISpatialReference SourceSpatialReference, ISpatialReference ObjectSpatialReference, IFeatureClass SourceFeatureClass, IFeatureClass ObjectFeatureClass) { int SourceFactoryCode = SourceSpatialReference.FactoryCode; int ObjectFactoryCode = ObjectSpatialReference.FactoryCode; //if (SourceFactoryCode == 0 || ObjectFactoryCode == 0) //{ // MessageBox.Show("源文件是自定义的投影坐标系统,这里暂不进行转换", "投影转换提示"); // return; //} EnumProjectionDatum SourceProjection = EnumProjectionDatum.Bejing54; //默认 EnumStrip SourceStrip = EnumStrip.Strip3; //默认 EnumProjectionDatum ObjectProjection = EnumProjectionDatum.Xian80; //默认 EnumStrip ObjectStrip = EnumStrip.Strip3; //默认 //源中央子午线 double L0 = (SourceSpatialReference as IProjectedCoordinateSystem4GEN).GetCentralLongitude(); bool IsBigNumber_Source = true; //源文件默认是大数 bool IsBigNumber_Object = true; //目标默认是大数 //------------------------------------------------------------------------------------------ //高斯投影大数情况分析 int BJ54D3_25srid = (int)esriSRProjCS4Type.esriSRProjCS_Beijing1954_3_Degree_GK_Zone_25; int BJ54D3_45srid = (int)esriSRProjCS4Type.esriSRProjCS_Beijing1954_3_Degree_GK_Zone_45; int BJ54D6_13srid = (int)esriSRProjCSType.esriSRProjCS_Beijing1954GK_13; int BJ54D6_23srid = (int)esriSRProjCSType.esriSRProjCS_Beijing1954GK_23; int Xian80D3_25srid = (int)esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_Zone_25; int Xian80D3_45srid = (int)esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_Zone_45; int Xian80D6_13srid = (int)esriSRProjCS4Type.esriSRProjCS_Xian1980_GK_Zone_13; int Xian80D6_23srid = (int)esriSRProjCS4Type.esriSRProjCS_Xian1980_GK_Zone_23; //源文件分析 //BeiJing1954 3 degree if (SourceFactoryCode >= BJ54D3_25srid && SourceFactoryCode <= BJ54D3_45srid) { SourceProjection = EnumProjectionDatum.Bejing54; SourceStrip = EnumStrip.Strip3; IsBigNumber_Source = true; } //BeiJing1954 6 degree if (SourceFactoryCode >= BJ54D6_13srid && SourceFactoryCode <= BJ54D6_23srid) { SourceProjection = EnumProjectionDatum.Bejing54; SourceStrip = EnumStrip.Strip3; IsBigNumber_Source = true; } //Xian1980 3 degree if (SourceFactoryCode >= Xian80D3_25srid && SourceFactoryCode <= Xian80D3_45srid) { SourceProjection = EnumProjectionDatum.Xian80; SourceStrip = EnumStrip.Strip3; IsBigNumber_Source = true; } //Xian1980 6 degree if (SourceFactoryCode >= Xian80D6_13srid && SourceFactoryCode <= Xian80D6_23srid) { SourceProjection = EnumProjectionDatum.Xian80; SourceStrip = EnumStrip.Strip3; IsBigNumber_Source = true; } //目标文件分析 //BeiJing1954 3 degree if (ObjectFactoryCode >= BJ54D3_25srid && ObjectFactoryCode <= BJ54D3_45srid) { ObjectProjection = EnumProjectionDatum.Bejing54; ObjectStrip = EnumStrip.Strip3; IsBigNumber_Object = true; } //BeiJing1954 6 degree if (ObjectFactoryCode >= BJ54D6_13srid && ObjectFactoryCode <= BJ54D6_23srid) { ObjectProjection = EnumProjectionDatum.Bejing54; ObjectStrip = EnumStrip.Strip3; IsBigNumber_Object = true; } //Xian1980 3 degree if (ObjectFactoryCode >= Xian80D3_25srid && ObjectFactoryCode <= Xian80D3_45srid) { ObjectProjection = EnumProjectionDatum.Xian80; ObjectStrip = EnumStrip.Strip3; IsBigNumber_Object = true; } //Xian1980 6 degree if (ObjectFactoryCode >= Xian80D6_13srid && ObjectFactoryCode <= Xian80D6_23srid) { ObjectProjection = EnumProjectionDatum.Xian80; ObjectStrip = EnumStrip.Strip3; IsBigNumber_Object = true; } //------------------------------------------------------------------------------------- ////高斯投影小数情况分析 int BJ54D3_25srid_CM = (int)esriSRProjCS4Type.esriSRProjCS_Beijing1954_3_Degree_GK_CM_75E; int BJ54D3_45srid_CM = (int)esriSRProjCS4Type.esriSRProjCS_Beijing1954_3_Degree_GK_CM_135E; int BJ54D6_13srid_CM = (int)esriSRProjCSType.esriSRProjCS_Beijing1954GK_13N; int BJ54D6_23srid_CM = (int)esriSRProjCSType.esriSRProjCS_Beijing1954GK_23N; int Xian80D3_25srid_CM = (int)esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_CM_75E; int Xian80D3_45srid_CM = (int)esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_CM_135E; int Xian80D6_13srid_CM = (int)esriSRProjCS4Type.esriSRProjCS_Xian1980_GK_CM_75E; int Xian80D6_23srid_CM = (int)esriSRProjCS4Type.esriSRProjCS_Xian1980_GK_CM_135E; //源文件分析 //BeiJing1954 3 degree if (SourceFactoryCode >= BJ54D3_25srid_CM && SourceFactoryCode <= BJ54D3_45srid_CM) { SourceProjection = EnumProjectionDatum.Bejing54; SourceStrip = EnumStrip.Strip3; IsBigNumber_Source = false; } //BeiJing1954 6 degree if (SourceFactoryCode >= BJ54D6_13srid_CM && SourceFactoryCode <= BJ54D6_23srid_CM) { SourceProjection = EnumProjectionDatum.Bejing54; SourceStrip = EnumStrip.Strip3; IsBigNumber_Source = false; } //Xian1980 3 degree if (SourceFactoryCode >= Xian80D3_25srid_CM && SourceFactoryCode <= Xian80D3_45srid_CM) { SourceProjection = EnumProjectionDatum.Xian80; SourceStrip = EnumStrip.Strip3; IsBigNumber_Source = false; } //Xian1980 6 degree if (SourceFactoryCode >= Xian80D6_13srid_CM && SourceFactoryCode <= Xian80D6_23srid_CM) { SourceProjection = EnumProjectionDatum.Xian80; SourceStrip = EnumStrip.Strip3; IsBigNumber_Source = false; } //目标文件分析 //BeiJing1954 3 degree if (ObjectFactoryCode >= BJ54D3_25srid_CM && ObjectFactoryCode <= BJ54D3_45srid_CM) { ObjectProjection = EnumProjectionDatum.Bejing54; ObjectStrip = EnumStrip.Strip3; IsBigNumber_Object = false; } //BeiJing1954 6 degree if (ObjectFactoryCode >= BJ54D6_13srid_CM && ObjectFactoryCode <= BJ54D6_23srid_CM) { ObjectProjection = EnumProjectionDatum.Bejing54; ObjectStrip = EnumStrip.Strip3; IsBigNumber_Object = false; } //Xian1980 3 degree if (ObjectFactoryCode >= Xian80D3_25srid_CM && ObjectFactoryCode <= Xian80D3_45srid_CM) { ObjectProjection = EnumProjectionDatum.Xian80; ObjectStrip = EnumStrip.Strip3; IsBigNumber_Object = false; } //Xian1980 6 degree if (ObjectFactoryCode >= Xian80D6_13srid_CM && ObjectFactoryCode <= Xian80D6_23srid_CM) { ObjectProjection = EnumProjectionDatum.Xian80; ObjectStrip = EnumStrip.Strip3; IsBigNumber_Object = false; } //------------------------------------------------------------------------------ //KDFeatureClass sfc = new KDFeatureClass_TYPoint(SourceFeatureClass); //KDFeatureClass ofc = new KDFeatureClass_TYPoint(ObjectFeatureClass); ZhFeatureClass sfc = new ZhPointFeatureClass(SourceFeatureClass); ZhFeatureClass ofc = new ZhPointFeatureClass(ObjectFeatureClass); //KDFeature[] FeatArray = sfc.getSelectedFeaturesByQueryFilter(null); //KDFeature sfeat = null; //KDFeature ofeat = null; ZhFeature[] FeatArray = sfc.getSelectedFeaturesByQueryFilter(null); ZhFeature sfeat = null; ZhFeature ofeat = null; IGeometry geo = null; IPoint p = null; /*frmProgressBar1 pb = new frmProgressBar1(); * pb.Text = "坐标投影转换"; * pb.label1.Text = "总数:" + FeatArray.Length.ToString(); * pb.progressBar1.Maximum = FeatArray.Length + 1; * pb.progressBar1.Value = 0; * pb.Show();*/ for (int i = 0; i < FeatArray.Length; i++) { /*if (pb.progressBar1.Value < pb.progressBar1.Maximum) * { * pb.progressBar1.Value += 1; * pb.label1.Text = "第[" + pb.progressBar1.Value.ToString() + "]个/总数[" + FeatArray.Length.ToString() + "]"; * pb.Refresh(); * }*/ sfeat = FeatArray[i]; //ofeat = ofc.CreateKDFeature(); ofeat = ofc.CreateFeature(); //copy 源字段的值到目标字段中 sfeat.CopyField(ref ofeat); geo = sfeat.pFeature.ShapeCopy; //geo.Project(ObjectSpatialReference); ////坐标投影转换 IGeometry ObjGeometry = null; if (geo is IPoint) { //点 double x = 0; double y = 0; double B = 0; double L = 0; x = (geo as IPoint).X; y = (geo as IPoint).Y; this.GetBLFromXY(y, x, ref B, ref L, 0, SourceProjection, SourceStrip, L0, IsBigNumber_Source); this.GetXYFromBL(B, L, ref x, ref y, 0, ObjectProjection, ObjectStrip, IsBigNumber_Object); IPoint objP = new PointClass(); objP.X = y; objP.Y = x; objP.SpatialReference = ObjectSpatialReference; //投影到目标点对象 ObjGeometry = objP as IGeometry; } else if (geo is IPolyline) { //线 IPolyline objPolyline = new PolylineClass(); IPointCollection objPc = objPolyline as IPointCollection; double x = 0; double y = 0; double B = 0; double L = 0; IPointCollection pcol = geo as IPointCollection; object miss = Type.Missing; for (int j = 0; j < pcol.PointCount; j++) { p = pcol.get_Point(j); x = p.X; y = p.Y; this.GetBLFromXY(y, x, ref B, ref L, 0, SourceProjection, SourceStrip, L0, IsBigNumber_Source); this.GetXYFromBL(B, L, ref x, ref y, 0, ObjectProjection, ObjectStrip, IsBigNumber_Object); IPoint objPoint = new PointClass(); objPoint.X = y; objPoint.Y = x; objPoint.SpatialReference = ObjectSpatialReference; objPc.AddPoint(objPoint, ref miss, ref miss); } (objPc as ITopologicalOperator).Simplify(); ObjGeometry = objPc as IGeometry; } else if (geo is IPolygon) { //面 IPolygon objPolygon = new PolygonClass(); IGeometryCollection objPc = objPolygon as IGeometryCollection; double x = 0; double y = 0; double B = 0; double L = 0; IGeometryCollection GeoCol = geo as IGeometryCollection; object miss = Type.Missing; IGeometry tpGeo = null; IPointCollection pcol = null; IRing newRing = null; IPointCollection newRingPointColl = null; for (int j = 0; j < GeoCol.GeometryCount; j++) { tpGeo = GeoCol.get_Geometry(j); //面内环ring(内/外环) pcol = tpGeo as IPointCollection; newRing = new RingClass(); newRingPointColl = newRing as IPointCollection; for (int k = 0; k < pcol.PointCount; k++) { p = pcol.get_Point(k); x = p.X; y = p.Y; this.GetBLFromXY(y, x, ref B, ref L, 0, SourceProjection, SourceStrip, L0, IsBigNumber_Source); this.GetXYFromBL(B, L, ref x, ref y, 0, ObjectProjection, ObjectStrip, IsBigNumber_Object); IPoint objPoint = new PointClass(); objPoint.X = y; objPoint.Y = x; newRingPointColl.AddPoint(objPoint, ref miss, ref miss); } newRing.SpatialReference = ObjectSpatialReference; objPc.AddGeometry(newRing as IGeometry, ref miss, ref miss); } (objPc as IGeometry).SpatialReference = ObjectSpatialReference; (objPc as ITopologicalOperator).Simplify(); ObjGeometry = objPc as IGeometry; } else { //注记 暂未写 } ofeat.pFeature.Shape = ObjGeometry; ofeat.SaveFeature(); } /*pb.Close(); * pb.Dispose(); * pb = null;zk*/ }