Multipart to single part feature

Explode

Link: http://edndoc.esri.com/arcobjects/8.3/?URL=/arcobjectsonline/samples/arcmap/explode/explode.htm

 

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();

}

}

}

Multipart to single part feature的更多相关文章

  1. SSD: Single Shot MultiBox Detector论文阅读摘要

    论文链接: https://arxiv.org/pdf/1512.02325.pdf 代码下载: https://github.com/weiliu89/caffe/tree/ssd Abstract ...

  2. JavaScript Module Pattern: In-Depth

    2010-03-12 JavaScript Module Pattern: In-Depth The module pattern is a common JavaScript coding patt ...

  3. javascript 模块化编程

    The module pattern is a common JavaScript coding pattern. It’s generally well understood, but there ...

  4. Fiddler源代码分享

    frmViewer.cs: namespace Fiddler{    using Microsoft.Win32;    using System;    using System.Collecti ...

  5. Microsoft FIM: Working with Domino Connector v8

    Microsoft FIM: Working with Domino Connector v8 Posted on July 22, 2013 by Michael Pearn - 4 Comment ...

  6. 在vs环境中跑动sift特征提取(代码部分)

    因为在前两天的学习中发现.在opencv环境中跑动sift特征点提取还是比较困难的. 所以在此,进行记述. 遇到的问题分别有,csdn不愿意花费积分.配置gtk困难.教程海量然而能跑者鲜.描述不详尽等 ...

  7. FeatureClass Copy

    http://edndoc.esri.com/arcobjects/9.2/NET/c45379b5-fbf2-405c-9a36-ea6690f295b2.htm Method What is tr ...

  8. JTAG 引脚自动识别 JTAG Finder, JTAG Pinout Tool, JTAG Pin Finder, JTAG pinout detector, JTAGULATOR, Easy-JTAG, JTAG Enumeration

    JTAG Finder Figuring out the JTAG Pinouts on a Device is usually the most time-consuming and frustra ...

  9. Intel daal数据预处理

    https://software.intel.com/en-us/daal-programming-guide-datasource-featureextraction-py # file: data ...

随机推荐

  1. Codeforces Round #378 (Div. 2) A B C D 施工中

    A. Grasshopper And the String time limit per test 1 second memory limit per test 256 megabytes input ...

  2. hdu2874 LCA

    题意:现在有 n 个点与 m 条边的无向无环图,但是图不一定完全连通,边有各自的边权,给出多组询问,查询两点之间的路径权值和,或者输出两点不连通. 一开始有最短路的想法,但是由于询问有 1e6 组,做 ...

  3. Qt 手动添加ui文件到工程(转)

    制作ui文件 先应该用Qt Designer绘制一个自己的界面,并存为myform.ui(这里的myform可以用自己喜欢的名字代替).在制作自己的界面文件时要注意以下几个要点: 1.要记住ui文件的 ...

  4. IOS5中的Safari不兼容Javascript中的Date问题

    在IOS5以上版本(不包含IOS5)中的Safari浏览器能正确解释出Javascript中的 new Date('2016-06-07') 的日期对象. 但是在IOS5版本里面的Safari解释ne ...

  5. byte与char的区别

     byte 是字节数据类型 ,是有符号型的,占1 个字节:大小范围为-128—127 .char 是字符数据类型 ,是无符号型的,占2字节(Unicode码 ):大小范围 是0—65535 :char ...

  6. knockout 学习实例2 text

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 调试技巧--Windows端口号是否被占用

    调试技巧--Windows端口号是否被占用 一.端口概念 10.0.0.0~10.255.255.255,172.16.0.0~172.16.255.255, 192.168.0.0~192.168. ...

  8. 学习SQL的点点滴滴(一)-常用函数

    该文章转载自http://www.cnblogs.com/jiajiayuan/archive/2011/06/16/2082488.html 别人的总结,很详细. 以下所有例子均Studnet表为例 ...

  9. NeHe OpenGL教程 第四十五课:顶点缓存

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  10. 移动端line-height失效

    移动端高度过小,使用rem布局时div里面的文字不能用line-height垂直居中: 解决方案,先高度,字体大小扩大n倍,然后利用transform:scale(0.n)缩小即可.