int nMax = (int)numFIDTo.Value;
int nMin = (int)numFIDFrom.Value;
IStepProgressor stepProg = ArcMap.Application.StatusBar.ProgressBar;
stepProg.MinRange = 0;
stepProg.MaxRange = nMax;
stepProg.Position = 0;
stepProg.Show();
try
{
IMap map = ArcMap.Document.ActiveViewI don't know why this code gets a memory overflow error for datasets with more than 20k of data.FocusMap;
IFeatureLayer flCrater = ClsShare.GetLayerFromName(mapif anybody knows, comboBox1thank you for your help.Text) as IFeatureLayer;
int nMax = (int)numFIDTo.Value;
bool isPolygon int nMin = check_isPokygon(int)numFIDFrom.Checked;
Value;
FrmAddFieldIStepProgressor frmAddFieldstepProg = new FrmAddField(flCrater);ArcMap.Application.StatusBar.ProgressBar;
int field_RstepProg.MinRange = frmAddField.AddField("local_R",0;
esriFieldType.esriFieldTypeDouble, true);
stepProg.MaxRange =
nMax;
IFeatureClass fcCraterstepProg.Position = flCrater.FeatureClass;0;
if stepProg.Show(fcCrater);
== null) try
{
IMap map = ArcMap.Document.ActiveView.FocusMap;
IFeatureLayer flCrater = ClsShare.GetLayerFromName(map, comboBox1.Text) as IFeatureLayer;
bool isPolygon = check_isPokygon.Checked;
FrmAddField frmAddField = new FrmAddField(flCrater);
int field_R = frmAddField.AddField("local_R", esriFieldType.esriFieldTypeDouble, true);
IFeatureClass fcCrater = flCrater.FeatureClass;
if (fcCrater == null)
{
MessageBox.Show("撞击坑图层要素为空!");
}
IGeoDataset geodataset = fcCrater as IGeoDataset;
ISpatialReference spatialR_Crater = geodataset.SpatialReference;
IGeographicCoordinateSystem geoSys = ClsShare.GetGeographicCoordinateSystem(spatialR_Crater);
IFeatureCursor featureCursor = fcCrater.Update(null, false);
IFeature feature = null;
double dDiam = 0;
double dR = 0;
IClone polyClone;
IPolyline polylineClone;
while ((feature = featureCursor.NextFeature()) != null)
{
if (feature.OID < nMin || feature.OID > nMax)
continue;
if (feature.ShapeCopy.IsEmpty) continue;
IPolygon polygonSrc = feature.ShapeCopy as IPolygon;
IPolyline polylineSrc = new PolylineClass();
polylineSrc.SpatialReference = polygonSrc.SpatialReference;
ISegmentCollection segs = polylineSrc as ISegmentCollection;
segs.AddSegmentCollection(polygonSrc as ISegmentCollection);
IPoint ptCenter = ClsShare.GetCenter(polygonSrc as IGeometry);
// 将中心点首先投影到地里坐标系上
ptCenter.Project(geoSys);
// 创建局部投影坐标系
IProjectedCoordinateSystem prjLocal = ClsShare.CreateLocalProjectedCoordinateSystem(
geoSys, 0, 0, 1.0, 0, ptCenter.X, ptCenter.Y);
// 获取地理坐标系 polyClone = polylineSrc as IClone;
IGeoDataset geodataset polylineClone = fcCraterpolyClone.Clone() as IGeoDataset;IPolyline;
ISpatialReference spatialR_Crater = geodataset polylineClone.SpatialReference;Project(prjLocal);
IGeographicCoordinateSystem geoSys = ClsShare.GetGeographicCoordinateSystem if (spatialR_CraterpolylineClone.IsEmpty); continue;
IFeatureCursor featureCursor = fcCrater.Update(null, false);
IFeature feature = null;
double dDiam = 0;
double dR = 0;
IClone polyClone;
IPolyline polylineClone;
while ((feature = featureCursor.NextFeature()) != null)
{
if (feature.OID < nMin || feature.OID > nMax)
continue;
if (feature.ShapeCopy.IsEmpty) continue;
// 将圆变成圆弧进行投影
IPolygon polygonSrc = feature.ShapeCopy as IPolygon;
IPolyline polylineSrc = new PolylineClass();
polylineSrc.SpatialReference = polygonSrc.SpatialReference;
ISegmentCollection segs = polylineSrc as ISegmentCollection;
segs.AddSegmentCollection(polygonSrc as ISegmentCollection);
IPoint ptCenter = ClsShare.GetCenter(polygonSrc as IGeometry);
// 将中心点首先投影到地里坐标系上
ptCenter.Project(geoSys);
// 创建局部投影坐标系
IProjectedCoordinateSystem prjLocalgeoPoly = ClsShare.CreateLocalProjectedCoordinateSystem(
geoSys, 0, 0, 1.0, 0,polylineClone ptCenter.X,as ptCenter.Y);IGeometry;
polyClone = polylineSrc as IClone;//!<得到圆的外包最长边作为圆直径
polylineClone dDiam = polyClone0.Clone0;
if (isPolygon)
as IPolyline; {
polylineClone //!<如果是提取任意多边形的最长直径,则要取矩形框的对角线长度作为最长值
dDiam = Math.ProjectSqrt(prjLocalgeoPoly.Envelope.Width * geoPoly.Envelope.Width + geoPoly.Envelope.Height * geoPoly.Envelope.Height);
if }
else
{
dDiam = Math.Max(polylineClonegeoPoly.IsEmptyEnvelope.Width, geoPoly.Envelope.Height);
continue; }
IGeometry geoPoly dR = polylineClonedDiam as/ IGeometry;2.0;
feature.set_Value(field_R,dR);
featureCursor.UpdateFeature(feature);
stepProg.Step();
//!<得到圆的外包最长边作为圆直径
dDiam = 0System.0;Runtime.InteropServices.Marshal.ReleaseComObject(feature);
if (isPolygon)
{System.Runtime.InteropServices.Marshal.ReleaseComObject(polygonSrc);
//!<如果是提取任意多边形的最长直径,则要取矩形框的对角线长度作为最长值System.Runtime.InteropServices.Marshal.ReleaseComObject(polylineSrc);
dDiam = Math.Sqrt(geoPoly.Envelope.Width * geoPoly.Envelope.Width + geoPolySystem.EnvelopeRuntime.Height * geoPolyInteropServices.EnvelopeMarshal.HeightReleaseComObject(ptCenter);
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(prjLocal);
else
{System.Runtime.InteropServices.Marshal.ReleaseComObject(segs);
dDiam = Math.Max(geoPolySystem.EnvelopeRuntime.Width, geoPolyInteropServices.EnvelopeMarshal.HeightReleaseComObject(geoPoly);
}
dR = dDiam / 2.0;
feature.set_Value(field_R,dR);
featureCursor.UpdateFeature(feature);
stepProg.Step();
System.Runtime.InteropServices.Marshal.ReleaseComObject(feature);
System.Runtime.InteropServices.Marshal.ReleaseComObject(polygonSrc);
System.Runtime.InteropServices.Marshal.ReleaseComObjectHide(polylineSrc);
System.Runtime.InteropServices.MarshalMessageBox.ReleaseComObjectShow(ptCenter"撞击坑更新完成");
System.Runtime.InteropServices.Marshal.ReleaseComObject(prjLocalfeatureCursor);
System.Runtime.InteropServices.MarshalGC.ReleaseComObjectCollect(segs);
System.Runtime.InteropServices.MarshalGC.ReleaseComObjectWaitForPendingFinalizers(geoPoly);
}
stepProg.Hide();
MessageBox.Show("撞击坑更新完成");
System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor);
catch System.GC.Collect();
Exception System.GC.WaitForPendingFinalizers(ex);
}
catch (Exception ex){
{
MessageBox.Show(ex.Message+"");
}