Select to view content in your preferred language

Unable to highlight intersecting polygons using c#

673
2
08-08-2010 07:33 PM
YashikaSareen
Emerging Contributor
Hi everyone,
I am new to GIS programming.I wanted to write a code in which we will be having two polygon layers and we need to select the second layer polygons that are intersecting with the polygons in first layer.I have been able to cop up with the code upto some extent.but still it is not selecting the polygons.I am getting the resultant polygons through ispatial filter but unable to highlight them.below is the code..plz help its urgent...i have tried several methods such as selectionset.add,selectionset.addlist but its not working.

using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Geodatabase;





namespace try1
{
[Guid("571a1393-83e1-429d-b48b-27de18e8416f")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("try1")]
public class custom : ICommand
{
public ILayer l1;
public ILayer l2;
public IMxApplication mxApplication; //to create application display object
IApplication m_application;
private IntPtr m_hBitmap;
public IApplication m_app;
public IMxDocument m_mxDoc;
public MxDocument m_mxDocument;
public Map m_map;
public PageLayout m_pageLayout;
public IFeatureClass featureClass;

// Needed to clear up the Hbitmap unmanaged resource
[System.Runtime.InteropServices.DllImport("gdi32.dl l")]
static extern bool DeleteObject(IntPtr hObject);

#region "Component Category Registration"
[ComRegisterFunction()]
static void Reg(string regKey)
{
MxCommands.Register(regKey);
}

[ComUnregisterFunction()]
static void Unreg(string regKey)
{
MxCommands.Unregister(regKey);
}
#endregion


#region ICommand Members

public string Message
{
get
{
// Set the message string that appears in the statusbar of the
// application when the mouse passes over the command.
return "first tool";
//return null;
}
}
public int HelpContextID
{
get
{
return 0;
}
}

public string Name
{
get
{

return "sample tool";

}
}


public void OnClick()
{

l1 = m_map.get_Layer(0);
l2 = m_map.get_Layer(1);
IFeatureCursor featureCursor1;
IFeatureLayer pfl = (IFeatureLayer)m_map.get_Layer(0);
IFeatureLayer pfl1 = (IFeatureLayer)m_map.get_Layer(1);
IFeatureClass featureClass = pfl.FeatureClass;
IFeatureClass featureClass1 = pfl1.FeatureClass;
IFeatureCursor featureCursor = featureClass.Search(null, true);
IFeature Feat = featureCursor.NextFeature();
ISpatialFilter spatialFilter = new SpatialFilterClass();
IFeatureSelection sel = Feat as IFeatureSelection;


IActiveView activeView = m_mxDoc.ActiveView;

while (Feat != null)
{
spatialFilter.GeometryField = featureClass1.ShapeFieldName;
spatialFilter.Geometry = Feat.Shape;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
featureCursor1 = featureClass1.Search(spatialFilter, false);

while (Feat1 != null) //to check the result of the ispatialquery
{

MessageBox.Show("FEAT " + Feat1.OID.ToString());
Feat1 = featureCursor1.NextFeature();


}
Feat = featureCursor.NextFeature();
}


sel.SelectFeatures(null, esriSelectionResultEnum.esriSelectionResultNew, false);


activeView.PartialRefresh(esriViewDrawPhase.esriVi ewGeoSelection, null, null);
}


public int Bitmap
{
get
{
return m_hBitmap.ToInt32();

}
}
public void OnCreate(object hook)
{
m_app = hook as IApplication;
m_mxDoc = m_app.Document as IMxDocument;
m_mxDocument = m_app.Document as MxDocument;
m_map = m_mxDoc.FocusMap as Map;

}
public string Caption
{
get
{

return "MyTool in CSharp";
//return null;
}
}

public string Tooltip
{
get
{
// Set the string that appears in the screen tip.
return "MyTool in CSharp";
//return null;
}
}
public bool Checked
{
get
{
return false;
}
}

public bool Enabled
{
get
{
// Add some logic here to specify when the command should
// be enabled. In this example, the command is always enabled.
return true;
}
}
public string HelpFile
{
get
{
return null;
}
}

public string Category
{
get
{
// Set the category of this command. This determines where the
// command appears in the Commands panel of the Customize dialog.
return "test tool";
//return null;
}
}

#endregion







}
}
0 Kudos
2 Replies
JanDuske
Emerging Contributor
In your main loop try this:

m_map.ClearSelection();
while (Feat != null)
{
  spatialFilter.GeometryField = featureClass1.ShapeFieldName;
  spatialFilter.Geometry = Feat.Shape;
  spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
  featureCursor1 = featureClass1.Search(spatialFilter, false);
  Feat1 = featureCursor1.NextFeature();
  while (Feat1 != null) //to check the result of the ispatialquery
  {
    // MessageBox.Show("FEAT " + Feat1.OID.ToString());
    m_map.SelectFeature(l2, Feat1);
    Feat1 = featureCursor1.NextFeature();
  }
  Marshal.FinalReleaseComObject(featureCursor1);
  Feat = featureCursor.NextFeature();
}
Marshal.FinalReleaseComObject(featureCursor);
activeView.Refresh();


And try finding better (more descriptive, consistent) variable names, that helps preventing errors. And you have to manually destroy your Cursor objects after using them (see additions above) or otherwise the cursors will remain opened, keeping a lock to their tables ans causing a DB error once you have gone through too many loops and have exceeded the allowed number of open cursors of the DBMS.
0 Kudos
RameshPodugu
Deactivated User
Hi Sareen,

If your requirement is to highlight intersected polygon feature, Use IMap::SelectFeature method by giving Layer name and polygon Feature.

Hope this will helps


Ramesh Podugu
0 Kudos