I have drawn a polygon. Using SpatialFilter I found and processed all the features in it. But I wanted to get the feature count before working with the features.
Is there any method that I can use to determine that the count of features in a drawn Polygon Geometry beforehand?
I have drawn a polygon. The intersecting features inside the Polygon. Also, I wanted myself to count of all the features inside that drawn boundary. For example, if 1 transmedia and 1 structure are there in the drawn boundary. I am supposed to have the feature count 2. I am anyhow getting the count at the time of processing. But I have to find the count before hand. Why I am doing it beforehand since I want to find what is the percentage of processing done at a certain time and display it in progress bar.
Here is a snippet.
while ((esriLayer = esriEnumLayer.Next()) != null)
{
if (esriLayer.Valid) // Checking if the layer is a valid layer
{
esriFeatureLayer = esriLayer as IFeatureLayer;
IFeatureCursor esriFeatureCursor = esriFeatureLayer.FeatureClass.Search(spatialFilter, true);
if (esriFeatureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
{
continue;
}
while ((esriFeature = esriFeatureCursor.NextFeature()) != null)
{
int count = 0;
totalNumberOfFeatures++;
_progressStatus = 5;
string message = "Validating " + esriFeature.Class.AliasName.ToString() + ":" + esriFeature.OID.ToString();
ShowProgressBar(_progressStatus, message);
lstFeature.Add(esriFeature.Class.AliasName.ToString());
esriInvalidFeature = ReadConfigAndValidateFeature(esriFeature, message);
_progressStatus = _progressStatus + 60;
ShowProgressBar(_progressStatus, message);
if ( esriInvalidFeature != null )
{
string Key = esriInvalidFeature.Class.AliasName.ToString() + ":" + esriInvalidFeature.OID.ToString();
if (!diction.ContainsKey(Key))
{
diction.Add(Key, esriInvalidFeature);
}
else
{
continue;
}
}
}
}
}
I am processing the features using the method # ReadConfigAndValidateFeature I am hardcoding the stuff for now. But I want to showcase the percentage.
Update : After seeing the useful hint I have written a full method to count the features. Here is the code #
private int FeatureCount(IMap mapInstance, ISpatialFilter spatialFilter)
{
int featureCount = 0;
IEnumLayer esriEnumLayer = mapInstance.get_Layers(null, true);
ILayer esriLayer = null;
IFeatureLayer esriFeatureLayer = null;
while ((esriLayer = esriEnumLayer.Next()) != null)
{
esriFeatureLayer = (IFeatureLayer) esriLayer;
if (esriFeatureLayer.FeatureClass.ShapeType ==
esriGeometryType.esriGeometryPolygon)
{
continue;
}
if (esriLayer.Valid) // Checking if the layer is a valid layer
{
if (
esriFeatureLayer.FeatureClass.FeatureCount(spatialFilter) > 0)
{
featureCount +=
esriFeatureLayer.FeatureClass.FeatureCount(spatialFilter);
}
}
}
return featureCount;
}
-
Do you want to count feature of another shapefile inside your drwan polygon? This part is not cleare.Shiuli Pervin– Shiuli Pervin2017年09月11日 07:47:20 +00:00Commented Sep 11, 2017 at 7:47
-
Update: I am trying to clear things up. I have drawn a polygon. The intersecting features inside the Polygon I need. Also, I need the count of all the features inside that drawn boundary. For example, if 1 transmedia and 1 structure are there in the drawn boundary. I need to have the feature count 2. I am anyhow getting the count at the time of processing. But I need the count before hand. Why I need it beforehand since I need to find what is the percentage of processing done at a certain time and display it in progress bar.royan– royan2017年09月11日 07:52:07 +00:00Commented Sep 11, 2017 at 7:52
-
I have updated the code even. I think now I can make it clear.royan– royan2017年09月11日 08:00:24 +00:00Commented Sep 11, 2017 at 8:00
-
Please Edit the question in response to requests for clarification. It's not fair to those who would answer to need to mine the comments for critical information.Vince– Vince2017年09月11日 10:10:38 +00:00Commented Sep 11, 2017 at 10:10
-
Hello Vince, I am sorry for writing the word "need" . I tried to remove that. You are correct.royan– royan2017年09月11日 10:34:04 +00:00Commented Sep 11, 2017 at 10:34
1 Answer 1
Use the IFeatureClass.FeatureCount()
method. It's right here in the documentation.
-
Thanks Berend for Adding your valuable comments and showed me the path for solution. But for this I am not getting the feature count directly. I was thinking that there might be some ESRI spatial function to count the features as we do count(*) in a Oracle. But it is working with little time. I am updating with the full code after seeing your comment.royan– royan2017年09月11日 20:04:44 +00:00Commented Sep 11, 2017 at 20:04