Hi,
Please find below code working fine for me...points count will be added into polygon layer ...updated into given filed.....
using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace CountPoints
{
/// <summary>
/// Summary description for CountAllPoints.
/// </summary>
[Guid("96ac8c8b-1a7c-457d-aa69-dcdcba6034f2")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("CountPoints.CountAllPoints")]
public sealed class CountAllPoints : ESRI.ArcGIS.ADF.BaseClasses.BaseCommand
{
#region COM Registration Function(s)
[ComRegisterFunction()]
[ComVisible(false)]
static void RegisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryRegistration(registerType);
//
// TODO: Add any COM registration code here
//
}
[ComUnregisterFunction()]
[ComVisible(false)]
static void UnregisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryUnregistration(registerType);
//
// TODO: Add any COM unregistration code here
//
}
#region ArcGIS Component Category Registrar generated code
/// <summary>
/// Required method for ArcGIS Component Category registration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryRegistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
ESRI.ArcGIS.ADF.CATIDs.MxCommands.Register(regKey);
}
/// <summary>
/// Required method for ArcGIS Component Category unregistration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryUnregistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
ESRI.ArcGIS.ADF.CATIDs.MxCommands.Unregister(regKey);
}
#endregion
#endregion
private ESRI.ArcGIS.Framework.IApplication m_application;
public CountAllPoints()
{
//
// TODO: Define values for the public properties
//
base.m_category = "Spatial Analsis"; //localizable text
base.m_caption = "Spatial Analsis"; //localizable text
base.m_message = "Counts the point inside the polygon"; //localizable text
base.m_toolTip = "Counts the point inside the polygon"; //localizable text
base.m_name = "Spatial_Points_Inside_Polygon"; //unique id, non-localizable (e.g. "MyCategory_ArcMapCommand")
try
{
//
// TODO: change bitmap name if necessary
//
string bitmapResourceName = GetType().Name + ".gif";
base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
}
}
#region Overriden Class Methods
/// <summary>
/// Occurs when this command is created
/// </summary>
/// <param name="hook">Instance of the application</param>
public override void OnCreate(object hook)
{
if (hook == null)
return;
m_application = hook as ESRI.ArcGIS.Framework.IApplication;
//Disable if it is not ArcMap
if (hook is ESRI.ArcGIS.ArcMapUI.IMxApplication)
base.m_enabled = true;
else
base.m_enabled = false;
// TODO: Add other initialization code
}
/// <summary>
/// Occurs when this command is clicked
/// </summary>
public override void OnClick()
{
// TODO: Add CountAllPoints.OnClick implementation
ESRI.ArcGIS.ArcMapUI.IMxDocument pMxDoc = m_application.Document as ESRI.ArcGIS.ArcMapUI.IMxDocument;
ESRI.ArcGIS.Carto.IFeatureLayer pPolygonLayer = pMxDoc.FocusMap.get_Layer(1) as ESRI.ArcGIS.Carto.IFeatureLayer;
ESRI.ArcGIS.Carto.IFeatureLayer pPointLayer = pMxDoc.ActiveView.FocusMap.get_Layer(0) as ESRI.ArcGIS.Carto.IFeatureLayer;
ESRI.ArcGIS.Editor.IEditor pEditor;
ESRI.ArcGIS.esriSystem.UID pUID = new ESRI.ArcGIS.esriSystem.UIDClass();
pUID.Value = "esricore.Editor";
pEditor = m_application.FindExtensionByCLSID(pUID) as ESRI.ArcGIS.Editor.IEditor;
if(pEditor.EditState == ESRI.ArcGIS.Editor.esriEditState.esriStateEditing )
{
pEditor.StartOperation();
GetAllPoitsInsidePolygon(pPolygonLayer.FeatureClass, pPointLayer.FeatureClass, ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelContains, "TVSID");//TVSID is filed name whihc should be in the poly layer
pEditor.StopOperation("PolygonDensity");
}
else
{
System.Windows.Forms.MessageBox.Show("Not in edit session");
}
}
private void GetAllPoitsInsidePolygon(ESRI.ArcGIS.Geodatabase.IFeatureClass pFeatureClass1,ESRI.ArcGIS.Geodatabase.IFeatureClass pFeatClass2,ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum lSpatialRel,String strFeatFld)
{
ESRI.ArcGIS.Geodatabase.IFeatureCursor pFeatCursor = pFeatureClass1.Search(null,false);
int lFld=0;
lFld = pFeatureClass1.FindField(strFeatFld);
if(lFld == -1)
{
System.Windows.Forms.MessageBox.Show("field not found: " + strFeatFld);
return;
}
ESRI.ArcGIS.Geodatabase.IFeature pFeature = pFeatCursor.NextFeature();
while (pFeature != null)
{
ESRI.ArcGIS.Geodatabase.ISpatialFilter pSpatialFilter = new ESRI.ArcGIS.Geodatabase.SpatialFilterClass();
pSpatialFilter.Geometry = pFeature.ShapeCopy;
pSpatialFilter.SpatialRel = lSpatialRel;
pSpatialFilter.GeometryField = pFeatClass2.ShapeFieldName;
pFeature.set_Value(lFld,pFeatClass2.FeatureCount(pSpatialFilter));
System.Windows.Forms.MessageBox.Show("OID: " + pFeature.OID + ", count: " + pFeature.get_Value(lFld));
pFeature.Store();
pFeature = pFeatCursor.NextFeature();
}
}
#endregion
}
}
I hope this helps you....
Thanks and Regards,
Venkat Tammineni