# Loop all shapes in query (speed issue)

**URL:** <https://mapwindow.discourse.group/t/loop-all-shapes-in-query-speed-issue/253>\
**Category:** MapWinGIS\
**Created:** [September 16, 2019, 12:21pm UTC](https://mapwindow.discourse.group/t/loop-all-shapes-in-query-speed-issue/253 "2019-09-16T12:21:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bouvin87](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@Bouvin87](https://mapwindow.discourse.group/u/Bouvin87)\
**Post date:** [September 16, 2019, 12:21pm UTC](https://mapwindow.discourse.group/t/loop-all-shapes-in-query-speed-issue/253/1 "2019-09-16T12:21:37Z")

</div>

Hello, i need to speed up a loop trough alot of shapes. Im looping trough them to put all points into a table. This is for calculating the actual position on a road (in meters from the begining of the road).  
There can be more than one road on top of each other so i do the loop in a prarallel.foreach.  
On one road there is about 5000-10000 loops. This takes about total time of 40sec when there is 3 roads.

```
             foreach (DataRow row in query)
        {
            Shape = int.Parse(row[14].ToString());
            if (riktning == "Mot")
            {
                if (row.ItemArray[5].ToString() != "Syskonbak")
                {
                    if (row.ItemArray[4].ToString() == "Mot")
                    {
                        for (var i_shape = 0; i_shape <= shapeFile.Shape[Shape].numPoints - 2; i_shape++)
                        {
                            count += 1;
                            MapWinGIS.Point a = new MapWinGIS.Point();
                            MapWinGIS.Point b = new MapWinGIS.Point();
                            a = shapeFile.QuickPoint(Shape, i_shape);
                            b = shapeFile.QuickPoint(Shape, i_shape + 1);
                            shapeLength = Math.Sqrt((Math.Pow(Math.Abs(b.x - a.x), 2)) + (Math.Pow(Math.Abs(b.y - a.y), 2)));
                            start_a = stop_a;
                            stop_a = start_a + shapeLength;
                            dt.Rows.Add(Shape, a.x, a.y, b.x, b.y, shapeLength, start_a, stop_a);
                        }
                    }
                    else
                    {
                        for (var i_shape = shapeFile.Shape[Shape].numPoints - 1; i_shape >= 1; i_shape += -1)
                        {
                            count += 1;
                            MapWinGIS.Point a = new MapWinGIS.Point();
                            MapWinGIS.Point b = new MapWinGIS.Point();
                            a = shapeFile.QuickPoint(Shape, i_shape);
                            b = shapeFile.QuickPoint(Shape, i_shape - 1);
                            shapeLength = Math.Sqrt((Math.Pow(Math.Abs(b.x - a.x), 2)) + (Math.Pow(Math.Abs(b.y - a.y), 2)));
                            start_a = stop_a;
                            stop_a = start_a + shapeLength;
                            dt.Rows.Add(Shape, a.x, a.y, b.x, b.y, shapeLength, start_a, stop_a);
                        }
                    }
                }
                else
                {
                    for (var i_shape = 0; i_shape <= shapeFile.Shape[Shape].numPoints - 2; i_shape++)
                    {
                        count += 1;
                        MapWinGIS.Point a = new MapWinGIS.Point();
                        MapWinGIS.Point b = new MapWinGIS.Point();
                        a = shapeFile.QuickPoint(Shape, i_shape);
                        b = shapeFile.QuickPoint(Shape, i_shape + 1);
                        shapeLength = Math.Sqrt((Math.Pow(Math.Abs(b.x - a.x), 2)) + (Math.Pow(Math.Abs(b.y - a.y), 2)));
                        start_a = stop_a;
                        stop_a = start_a + shapeLength;
                        dt.Rows.Add(Shape, a.x, a.y, b.x, b.y, shapeLength, start_a, stop_a);
                    }
                }
            }
            else
            {
                if (row.ItemArray[4].ToString() == "Mot")
                {
                    for (var i_shape = shapeFile.Shape[Shape].numPoints - 1; i_shape >= 1; i_shape += -1)
                    {
                        count += 1;
                        MapWinGIS.Point a = new MapWinGIS.Point();
                        MapWinGIS.Point b = new MapWinGIS.Point();
                        a = shapeFile.QuickPoint(Shape, i_shape);
                        b = shapeFile.QuickPoint(Shape, i_shape - 1);
                        shapeLength = Math.Sqrt((Math.Pow(Math.Abs(b.x - a.x), 2)) + (Math.Pow(Math.Abs(b.y - a.y), 2)));
                        start_a = stop_a;
                        stop_a = start_a + shapeLength;
                        dt.Rows.Add(Shape, a.x, a.y, b.x, b.y, shapeLength, start_a, stop_a);
                    }
                }
                else
                {
                    for (var i_shape = 0; i_shape <= shapeFile.Shape[Shape].numPoints - 2; i_shape++)
                    {
                        count += 1;
                        MapWinGIS.Point a = new MapWinGIS.Point();
                        MapWinGIS.Point b = new MapWinGIS.Point();
                        a = shapeFile.QuickPoint(Shape, i_shape);
                        b = shapeFile.QuickPoint(Shape, i_shape + 1);
                        shapeLength = Math.Sqrt((Math.Pow(Math.Abs(b.x - a.x), 2)) + (Math.Pow(Math.Abs(b.y - a.y), 2)));
                        start_a = stop_a;
                        stop_a = start_a + shapeLength;
                        dt.Rows.Add(Shape, a.x, a.y, b.x, b.y, shapeLength, start_a, stop_a);
                    }
                }
            }
        }
```

---

<div class="post-metadata">

**Author:** ![pmeems](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/pmeems/32/9_2.png) [@pmeems](https://mapwindow.discourse.group/u/pmeems)\
**Post date:** [September 17, 2019, 2:15pm UTC](https://mapwindow.discourse.group/t/loop-all-shapes-in-query-speed-issue/253/2 "2019-09-17T14:15:56Z")

</div>

Caching the shape you are working on, will help.

So instead of doing:

```
Shape = int.Parse(row[14].ToString());
for (var i_shape = 0; i_shape <= shapeFile.Shape[Shape].numPoints - 2; i_shape++)

```

you could do

```
shapeId = int.Parse(row[14].ToString());
shp = shapeFile.Shape[shapeId]; 
numPoints = shp.numPoints;
for (var i_shape = 0; i_shape <= numPoints - 2; i_shape++)

```

You need to avoid to go too many time across the COM-bridge, because that is the most timeconsuming part.

You could also consider getting all points at once using [shapeFile.QuickPoints()](https://www.mapwindow.org/documentation/mapwingis4.9/group __shapefile__ optimizations.html#ga8027806af318a85b4c18b881f8893cd5) and then loop through the resulting array.

---

<div class="post-metadata">

**Author:** ![Bouvin87](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@Bouvin87](https://mapwindow.discourse.group/u/Bouvin87)\
**Post date:** [September 25, 2019, 6:15am UTC](https://mapwindow.discourse.group/t/loop-all-shapes-in-query-speed-issue/253/3 "2019-09-25T06:15:28Z")

</div>

Thanks for the tips Paul. I’ve managed to speed the loop up so now the time is about 5 000ms instead of 30 000ms.

---

<div class="post-metadata">

**Author:** ![pmeems](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/pmeems/32/9_2.png) [@pmeems](https://mapwindow.discourse.group/u/pmeems)\
**Post date:** [September 27, 2019, 6:15am UTC](https://mapwindow.discourse.group/t/loop-all-shapes-in-query-speed-issue/253/4 "2019-09-27T06:15:30Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
