博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Multipart to single part feature
阅读量:7240 次
发布时间:2019-06-29

本文共 6154 字,大约阅读时间需要 20 分钟。

Multipart to single part feature

Explode

Link:

 

Created:

10/25/2000

Last Modified:

4/26/2002

Description:

This sample copies all feature in a selected feature class to a new feature class created in the same dataset. Features with multiple parts are broken up so that each part is saved as a new separate feature. 
How to use:

  1. Select a feature layer in the table of contents.
  2. Click the Explode command button.
  3. Enter the name of the new feature class that will be created.
  4. Once completed, add the new layer to ArcMap, notice all previous mutipart features are broken into separate features.

Application: ArcMap

Difficulty: Intermediate

Explode.cs

using System;

using System.Drawing;

using System.Windows.Forms;

using System.Runtime.InteropServices;

// Esri references

using ESRI.ArcObjects.Core;

using ESRI.ArcObjects.Samples.BaseClasses;

using ESRI.ArcObjects.Samples.CatIDs;

 

namespace ArcMapTools

{

/// <summary>

/// Explode breaks multi-part features in single part features.

/// </summary>

 

[ClassInterface(ClassInterfaceType.None)]

[GuidAttribute("689cebc3-b751-4919-a8c6-af59390371de")]

public sealed class ExplodeCS: BaseCommand

{

[ComRegisterFunction()]

static void Reg(String regKey)

{

MxCommand.Register(regKey);

}

 

[ComUnregisterFunction()]

static void Unreg(String regKey)

{

MxCommand.Unregister(regKey);

}

 

private IApplication m_app;

 

public ExplodeCS()

{

try

{

m_bitmap = new Bitmap(GetType().Assembly.GetManifestResourceStream("ArcMapTools.x.bmp"));

}

catch

{

m_bitmap = null;

}

m_category = "Developer Samples";

m_caption = "Explode Command (C#)";

m_message = "Converts parts to features in new feature class.";

m_toolTip = "Converts parts to features.";

m_name = "Explode";

}

 

public override void OnClick()

{

IMxDocument mxDoc = m_app.Document as IMxDocument;

// Make certain the selected item in the toc is a feature layer

if (mxDoc.SelectedItem == null)

{

MessageBox.Show("Select a feature layer in the table of contents " +

"as the input feature class.");

return;

}

 

if (!(mxDoc.SelectedItem is IFeatureLayer))

{

MessageBox.Show("No feature layer selected.");

return;

}

 

IFeatureLayer featureLayer = mxDoc.SelectedItem as IFeatureLayer;

IFeatureClass featureClass = featureLayer.FeatureClass;

 

// Don't process point layers, they have no multi-part features

if (featureClass.ShapeType == esriGeometryType.esriGeometryPoint)

{

MessageBox.Show("Point layers do not have multi-parts.");

return;

}

 

// Prompt for a new feature class name

FeatureClassDialog dlg = new FeatureClassDialog();

dlg.ShowDialog();

string name;

if (dlg.DialogResult == DialogResult.OK)

name = dlg.FileName;

else

return;

 

if (name == "") return;

 

try

{

// Create a new feature class to store the new features

// Create the feature class in the same dataset if one exists - shapefiles don't have one

IFields fields = featureLayer.FeatureClass.Fields;

IDataset dataset;

IFeatureWorkspace featureWorkspace;

IFeatureClass newFeatureClass;

if (featureClass.FeatureDataset == null)

{

dataset = featureClass as IDataset;

featureWorkspace = dataset.Workspace as IFeatureWorkspace;

newFeatureClass = featureWorkspace.CreateFeatureClass(name, fields, null, null,

esriFeatureType.esriFTSimple, featureClass.ShapeFieldName, "");

}

else

{

newFeatureClass = featureClass.FeatureDataset.CreateFeatureClass(name, fields, null, null,

esriFeatureType.esriFTSimple, featureClass.ShapeFieldName, "");

}

 

// Create an insert cursor

IFeatureCursor insertFeatureCursor = newFeatureClass.Insert(true);

IFeatureBuffer featureBuffer = newFeatureClass.CreateFeatureBuffer();

 

// Copy each feature from the original feature class to the new feature class

IFeatureCursor featureCursor = featureClass.Search(null, true);

IFeature feature;

IGeometryCollection geometryColl;

 

while ((feature = featureCursor.NextFeature()) != null)

{

geometryColl = feature.Shape as IGeometryCollection;

if (geometryColl.GeometryCount == 1)

{

InsertFeature(insertFeatureCursor, featureBuffer, feature, feature.Shape);

}

else if (feature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon)

{

IPolygon2 polygon = feature.Shape as IPolygon2;

IPolygon[] polygonArray = new IPolygon[polygon.ExteriorRingCount];

polygon.GetConnectedComponents(polygon.ExteriorRingCount, polygonArray);

for (int i = 0; i <=polygon.ExteriorRingCount -1; i++)

{

InsertFeature(insertFeatureCursor, featureBuffer, feature, polygonArray[i]);

}

}

else

{

for (int i = 0; i <=geometryColl.GeometryCount -1; i++)

{

InsertFeature(insertFeatureCursor, featureBuffer, feature, geometryColl.get_Geometry(i));

}

}

}

}

catch

{

MessageBox.Show("An error occurred. Check that the shapefile specified doesn't already exist.");

}

}

 

public override void OnCreate(object hook)

{

m_app = hook as IApplication;

}

 

private void InsertFeature(IFeatureCursor featureCursor, IFeatureBuffer featureBuffer, IFeature originalFeature, IGeometry newShape)

{

IGeometryCollection newShapeColl = null;

IField field;

 

// Copy the attributes of the orig feature the new feature

IFields fields = originalFeature.Fields;

for (int i = 0; i <= fields.FieldCount - 1; i++)

{

field = fields.get_Field(i);

// skip OID and geometry

if (!(field.Type == esriFieldType.esriFieldTypeGeometry) &&

!(field.Type == esriFieldType.esriFieldTypeOID) && field.Editable)

{

featureBuffer.set_Value(i, originalFeature.get_Value(i));

}

}

 

// Handle cases where parts are passed down:

// InsertGeometries requires an IGeometry[] so we need to set up an array.

IGeometry[] geoArray = new IGeometry[1];

if (newShape.GeometryType == esriGeometryType.esriGeometryPath)

{

newShapeColl = new Polyline() as IGeometryCollection;

geoArray[0] = newShape;

newShapeColl.AddGeometries(1, geoArray);

newShape = newShapeColl as IGeometry;

}

else if (originalFeature.Shape.GeometryType == esriGeometryType.esriGeometryMultipoint)

{

if (newShape is IMultipoint)

{

IPointCollection pointColl = newShape as IPointCollection;

newShape = pointColl.get_Point(0);

}

geoArray[0] = newShape;

newShapeColl = new Multipoint() as IGeometryCollection;

newShapeColl.AddGeometries(1, geoArray);

newShape = newShapeColl as IGeometry;

}

 

featureBuffer.Shape = newShape;

featureCursor.InsertFeature(featureBuffer);

featureCursor.Flush();

}

}

}

转载地址:http://ekfbm.baihongyu.com/

你可能感兴趣的文章
批量删除记录时如何实现全选【总结】
查看>>
Thread’s start method and run method
查看>>
使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【二】——使用Repository模式构建数据库访问层...
查看>>
CDN发展史
查看>>
Atitit.研发团队的管理原则---立长不立贤与按资排辈原则
查看>>
UVa 10763 - Foreign Exchange
查看>>
#lspci | grep Eth
查看>>
日订单峰值破40万!58速运订单调度系统架构大解密
查看>>
Objective-C 资源收藏
查看>>
MFC——从实现角度分析微云界面
查看>>
正则 群组 Group
查看>>
An Introduction To The SQLite C/C++ Interface
查看>>
关闭Pycharm拼写检查
查看>>
一个优秀的程序员是如何炼成的?
查看>>
区块链的12个技术理解误区,你知道哪些?
查看>>
常用面试编程训练5大网站!
查看>>
对比三大旗舰真正全面屏,荣耀Magic2不学OV,绝不妥协
查看>>
SMILEY黄色笑脸攻占华为授权体验店 网友惊呼“太魔性”!
查看>>
陈林接替张一鸣任今日头条CEO 字节跳动学谷歌进行架构升级
查看>>
万盏彩灯迎新春 江苏大丰梅花湾新春灯会小年夜亮灯
查看>>