Multipart to single part feature
2015-09-22 18:15
357 查看
Multipart to single part feature
Link: http://edndoc.esri.com/arcobjects/8.3/?URL=/arcobjectsonline/samples/arcmap/explode/explode.htm
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:
Select a feature layer in the table of contents.
Click the Explode command button.
Enter the name of the new feature class that will be created.
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();
}
}
}
Explode | ![]() |
Created: | 10/25/2000 |
Last Modified: | 4/26/2002 |
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:
Select a feature layer in the table of contents.
Click the Explode command button.
Enter the name of the new feature class that will be created.
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();
}
}
}
相关文章推荐
- 基础 HTML之目录问题(相对路径和绝对路径区别)
- 下载文件
- css设置div等标签背景半透明
- 你所不了解的css选择器补充
- angular @ = &
- css中div层置顶解决办法
- CSS3绘制环形进度条
- Android samples API Demos之UI篇1(ActionBarCompat-ShareActionProvider)
- nio buffer的capacity,position和limit
- JSONP实现跨域
- 【JavaScript】【CSS】前端规则摘抄
- css中使用rgba和opacity设置透明度的区别
- Node.js中的DNS模块
- JS function 传递时间参数无法读取的问题
- uploadify与jquery1.10 在ie浏览器冲突报错处理(e.getAttribute("classid")缺少对象
- nancy 中的json问题
- CSS布局 ——从display,position, float属性谈起
- AngularJS体验式编程系列文章
- 实时视频h5
- 从零开始nodejs系列文章