There was an old GIS soft "ArcView" ; scripts in avenu language which worked much faster than my Add-ins now (made in Visual Studio in С#). So I would like understand what did I do wrong? Why is the modern technology less effective than the old one?
The code calculates intersection area of every combination of polygons in two featureclasses. Featureclass fc1 contains 100 features, FC fc2 contains 1000 features, therefore there are 100,000 combinations.
My first try was "FOR" cycle (without using cursors) this way:
IMxDocument mxdoc = ArcMap.Application.Document as IMxDocument;
IActiveView activeView = mxdoc.ActiveView;
IMap map = activeView as IMap;
ILayer layer1 = map.Layer[0];
ILayer layer2 = map.Layer[1];
IFeatureClass fclayer1 = (layer1 as IFeatureLayer2).FeatureClass;
IFeatureClass fclayer2 = (layer2 as IFeatureLayer2).FeatureClass;
IFeature f1;
IFeature f2;
IFeature fINTERS;
IGeometry g1;
IGeometry g2;
IGeometry gINTERS;
IArea arINTERS;
double areaINTERS;
ITopologicalOperator topoINTERS;
for (int i = 0; i < fclayer1.FeatureCount(null); i++)
{
f1 = fclayer1.GetFeature(i);
g1 = f1.Shape as IGeometry;
topoINTERS = g1 as ITopologicalOperator;
for (int j = 0; j < fclayer2.FeatureCount(null); j++)
{
f2 = fclayer2.GetFeature(j);
g2 = f2.Shape as IGeometry;
gINTERS = topoINTERS.Intersect(g2, esriGeometryDimension.esriGeometry2Dimension);
fINTERS = fclayer1.CreateFeature();
fINTERS.Shape = gINTERS;
arINTERS = fINTERS.Shape as IArea;
areaINTERS = arINTERS.Area;
fINTERS.Delete();
}
}
It takes about 90 minutes to do this calculation!
My second try was "WHILE" cycle (using cursors) this way:
IFeatureCursor fcur1 = fclayer1.Search(null,true);
IFeatureCursor fcur2;
IFeature f1 = fcur1.NextFeature();
IFeature f2;
IFeature fPEREC;
IGeometry g1;
IGeometry g2;
IGeometry gINTERS;
IArea arINTERS;
double areaINTERS;
ITopologicalOperator topoINTERS;
while (f1 != null)
{
g1 = f1.Shape as IGeometry;
topoINTERS = g1 as ITopologicalOperator;
//update cursor
fcur2 = fclayer2.Search(null, true);
f2 = fcur2.NextFeature();
while (f2 != null)
{
g2 = f2.Shape as IGeometry;
gINTERS = topoINTERS.Intersect(g2, esriGeometryDimension.esriGeometry2Dimension);
fPEREC = fclayer1.CreateFeature();
fPEREC.Shape = gINTERS;
arINTERS = fPEREC.Shape as IArea;
areaINTERS = arINTERS.Area;
fPEREC.Delete();
f2 = fcur2.NextFeature();
}
f1 = fcur1.NextFeature();
}This code spends about 2 minutes to do this, it's too long for my purpose too.
Script in avenue (ArcView) spends about 1 second on this task (script uses "FOREACH" cycle, but in C# is not allowed to iterate the features within featureclass this way).
Is there any idea how to optimize this code to make it work faster?
Using: C#, ArcGIS 10.2.2
No comments:
Post a Comment