Hello,How to write a code VB �?? Arcobject that counts the number of points inside each polygon?Please, see the attached image that shows my questionI�??ve two layers, point layer and polygon layer, I need to write a code that calculate the number of points inside each polygonThank youkalid
Dear Venkat,Thank you so much for the answer. It�??s really helpfulSure. The code you have developed is working perfectly. 1. how to develop the code so that we don�??t need:�?� To work in edit session.�?� To have the field �??name�?� in the district layer.2. How to develop the code considering the following approach:�?� to intersect the two layers (districts and hospitals: both are polygons)�?� And then to make summery in the table of the resultant layer (from the intersection) so that we count all the hospitals in each district.I�??m supposed to print the result in the form and then to be able to export it to a table to excelPlease, see the attached fileAppreciatedKalid
Hi, please check below code...Sub test() Dim pMxDoc As IMxDocument Set pMxDoc = ThisDocument Dim pPolyFLayer As IFeatureLayer Set pPolyFLayer = pMxDoc.FocusMap.Layer(1) Dim pPointFlayer As IFeatureLayer Set pPointFlayer = pMxDoc.FocusMap.Layer(0) Dim pEditor As IEditor, pUID As New UID pUID.Value = "esricore.Editor" Set pEditor = Application.FindExtensionByCLSID(pUID) If pEditor.EditState = esriStateEditing Then pEditor.StartOperation CountFeatures pPolyFLayer.FeatureClass, pPointFlayer.FeatureClass, _ esriSpatialRelContains, "NAME" pEditor.StopOperation "PolygonDensity" Else MsgBox "no edit session" End IfEnd SubSub CountFeatures(pFC1 As IFeatureClass, _ pFC2 As IFeatureClass, _ lSpatialRel As esriSpatialRelEnum, _ strCountFld As String) Dim pFCur As IFeatureCursor Set pFCur = pFC1.Search(Nothing, False) Dim lFld As Long lFld = pFC1.FindField(strCountFld) If lFld = -1 Then MsgBox "field not found: " & strCountFld Exit Sub End If Dim pFeat As IFeature Set pFeat = pFCur.NextFeature Do While Not pFeat Is Nothing Dim pSF As ISpatialFilter Set pSF = New SpatialFilter Set pSF.Geometry = pFeat.ShapeCopy pSF.SpatialRel = lSpatialRel pSF.GeometryField = pFC2.ShapeFieldName pFeat.Value(lFld) = pFC2.FeatureCount(pSF) MsgBox "OID: " & pFeat.OID & ", count: " & pFeat.Value(lFld) pFeat.Store Set pFeat = pFCur.NextFeature LoopEnd Subis that your looking for ?...Thanks and Regards,Venkat
thank you for so much,actually i need it to be written in VBA-Arcobjects not in any other language.have you any idea how to do it in VBA-Arcobjects ?where can i find examples of similar kind of work?thank youregardsJamal
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
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Jste tu noví? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.