# Change Shape Index

**URL:** <https://mapwindow.discourse.group/t/change-shape-index/1148>\
**Category:** Uncategorized\
**Created:** [November 24, 2021, 7:25pm UTC](https://mapwindow.discourse.group/t/change-shape-index/1148 "2021-11-24T19:25:08Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikeradtke](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/mikeradtke/32/558_2.png) [@mikeradtke](https://mapwindow.discourse.group/u/mikeradtke)\
**Post date:** [November 24, 2021, 7:25pm UTC](https://mapwindow.discourse.group/t/change-shape-index/1148/1 "2021-11-24T19:25:08Z")

</div>

Hi. I want to simply change the index of a shape. For example, change shape 0 in shapefile 7, to shape 25 in shapefile 7.  
When using EditInsertShape, the program is always starting with shape index of 0 no matter if I specify otherwise, or if there is already a shape 0 for that layer/shapefile. Anyone know how to do this?

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [November 30, 2021, 8:11am UTC](https://mapwindow.discourse.group/t/change-shape-index/1148/2 "2021-11-30T08:11:07Z")

</div>

Hello @mikeradtke

I’m not exactly sure what you’re trying to do. Note that internally, the shape collection is an STL vector class. You cannot rely on a fixed shape index as shapes are inserted or removed.

If there are not already at least 25 shapes in the shapefile, then you cannot ‘insert’ a shape at that position; the index can never be greater than the number of shapes already in the file.

And if you then remove the shape at index zero, then all higher shapes will shift down within the vector, reducing their indices.

Does that answer your question?

Regards,  
Jerry.

---

<div class="post-metadata">

**Author:** ![mikeradtke](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/mikeradtke/32/558_2.png) [@mikeradtke](https://mapwindow.discourse.group/u/mikeradtke)\
**Post date:** [November 30, 2021, 1:18pm UTC](https://mapwindow.discourse.group/t/change-shape-index/1148/3 "2021-11-30T13:18:17Z")

</div>

Thank you for your reply! I have a better understanding of how it works now, but I guess it doesn’t solve the problem. Do you know any reason why editinsertshape is always assigning the shape index to zero?

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [November 30, 2021, 5:26pm UTC](https://mapwindow.discourse.group/t/change-shape-index/1148/4 "2021-11-30T17:26:07Z")

</div>

Hello again.

So you’re saying that if you repeatedly call `EditInsertShape`, no matter index you request, the shape is always inserted at position zero?

Do you mind posting the code that is resulting in this behavior?

Thank you.  
Jerry.

---

<div class="post-metadata">

**Author:** ![mikeradtke](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/mikeradtke/32/558_2.png) [@mikeradtke](https://mapwindow.discourse.group/u/mikeradtke)\
**Post date:** [November 30, 2021, 6:14pm UTC](https://mapwindow.discourse.group/t/change-shape-index/1148/5 "2021-11-30T18:14:07Z")

</div>

That’s correct. Here is the code:

```auto
oldsubmx = DMax("[submax]", "mapstate")
newsubmx = DMax("[ID]", "Master")
'go load shape files then return
GoSub ldshpfiles
If oldsubmx <> newsubmx Then GoTo ldnewsbs

Dim xnw As Integer
Dim nlyrno As Integer
Dim nwshpno As Long
Dim sfnew As Shapefile
'load new subs to map and save shapefile
newmn = oldsubmx + 1
For zz = newmn To newsubmx
    lkd = DLookup("[CSI Division]", "Master", "[ID] = " & zz)
    nlyrno = DLookup("[assignedlayer]", "CSI Divisions", "[Description] = '" & lkd & "'")
    DoCmd.SetWarnings (False)
    DoCmd.RunSQL "UPDATE Master Set [Layer] = " & nlyrno & " WHERE [ID] = " & zz
    DoCmd.SetWarnings (True)
    xnw = zz
    nwshpno = DMax("[shapeno]", "Master", "[Layer] = " & nlyrno) + 1

    DoCmd.SetWarnings (False)
    DoCmd.RunSQL "UPDATE Master Set [shapeno] = " & nwshpno & " WHERE [ID] = " & zz
    DoCmd.SetWarnings (True)
    latnw = DLookup("[lat]", "Master", "[ID] = " & zz)
    lngnw = DLookup("[lng]", "Master", "[ID] = " & zz)
        If IsNull(latnw) Or IsNull(lngnw) Then GoTo skpnewsb
    ReDim Preserve shp(xnw)
    Set sfnew = Forms!Map.Map0.Shapefile(nlyrno)
    sfnew.DefaultDrawingOptions.Visible = True
    sfnew.Identifiable = True
    sfnew.Selectable = True
        With Me.Map0
            shape = sfnew.CreateNew("", ShpfileType.SHP_POINT)
            tf.SetWellKnownGeogCS (tkCoordinateSystem.csWGS_84)
            tf.StartTransform (Me.Map0.GeoProjection)
            tx = tf.Transform(lngnw, latnw)
            tf.StopTransform
            shp(xnw).Create (ShpfileType.SHP_POINT)
            index = shp(xnw).AddPoint(lngnw, latnw)
            index = sfnew.EditInsertShape(shp(xnw), nwshpno)
            sfnew.Selectable = True
            sfnew.SelectionColor = vbGreen
        End With
'save shapefile
pt = Application.CurrentProject.Path & "\ShapeFiles\shapefile" & nlyrno & ".shp"
sve = sfnew.SaveAs(pt, Nothing)
skpnewsb:
Next

```

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [November 30, 2021, 6:40pm UTC](https://mapwindow.discourse.group/t/change-shape-index/1148/6 "2021-11-30T18:40:22Z")

</div>

> [@mikeradtke](#):
>
> ```auto
> Set sfnew = Forms!Map.Map0.Shapefile(nlyrno)
> sfnew.DefaultDrawingOptions.Visible = True
> sfnew.Identifiable = True
> sfnew.Selectable = True
> With Me.Map0
> shape = sfnew.CreateNew("", ShpfileType.SHP_POINT)
> tf.SetWellKnownGeogCS (tkCoordinateSystem.csWGS_84)
> tf.StartTransform (Me.Map0.GeoProjection)
> tx = tf.Transform(lngnw, latnw)
> tf.StopTransform
> shp(xnw).Create (ShpfileType.SHP_POINT)
> index = shp(xnw).AddPoint(lngnw, latnw)
> index = sfnew.EditInsertShape(shp(xnw), nwshpno)
> sfnew.Selectable = True
> sfnew.SelectionColor = vbGreen
> End With
> 
> ```

On initial review, I think you need to remove the following line:

```
    shape = sfnew.CreateNew("", ShpfileType.SHP_POINT)

```

since this initialzes sfnew as a new Shapefile on every iteration. This explains why every insert goes at position zero.

If indeed

```
Set sfnew = Forms!Map.Map0.Shapefile(nlyrno)

```

is already returning a valid Shapefile, then your `EditInsertShape` will likely now go into a non-zero position.

Regards,  
Jerry.

---

<div class="post-metadata">

**Author:** ![mikeradtke](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/mikeradtke/32/558_2.png) [@mikeradtke](https://mapwindow.discourse.group/u/mikeradtke)\
**Post date:** [November 30, 2021, 7:12pm UTC](https://mapwindow.discourse.group/t/change-shape-index/1148/7 "2021-11-30T19:12:54Z")

</div>

Jerry,  
I removed that line, but no shape showed on the map.

```auto
Set sfnew = Forms!Map.Map0.Shapefile(nlyrno)

```

This line does return a valid shapefile, or at least I think it does. When clicking on it with the identify tool, I get a valid layer handle returned.

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [November 30, 2021, 7:32pm UTC](https://mapwindow.discourse.group/t/change-shape-index/1148/8 "2021-11-30T19:32:18Z")

</div>

Hello Mike.

Understanding that there is a lot of code I’m not seeing here, I have to assume that somewhere prior to the snippet you submitted, you have preloaded the map with a number of layers (Shapefiles)?

When you debug, and you execute the line above, does sfnew have a value? (not Nothing)

It looks like you’re using VBA or some sort. Can you check the Immediate window, and query the [number of shapes](https://www.mapwindow.org/documentation/mapwingis4.9/class_shapefile.html#a8c16abfdf418de3ae93475953958e907) in the shapefile? Something like

```
?sfnew.numShapes

```

and see what it returns?

---

<div class="post-metadata">

**Author:** ![mikeradtke](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/mikeradtke/32/558_2.png) [@mikeradtke](https://mapwindow.discourse.group/u/mikeradtke)\
**Post date:** [November 30, 2021, 7:40pm UTC](https://mapwindow.discourse.group/t/change-shape-index/1148/9 "2021-11-30T19:40:47Z")

</div>

It returns only 1… but there are actually 100+ shapes in shapefile7 (shapefile I’m supposed to be creating a new shape in).

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [November 30, 2021, 7:54pm UTC](https://mapwindow.discourse.group/t/change-shape-index/1148/10 "2021-11-30T19:54:14Z")

</div>

I think then that you will have to backtrack to where the layers are first added, and verify that they are intact at that point, having the number of shapes you expect.

---

<div class="post-metadata">

**Author:** ![mikeradtke](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/mikeradtke/32/558_2.png) [@mikeradtke](https://mapwindow.discourse.group/u/mikeradtke)\
**Post date:** [November 30, 2021, 8:30pm UTC](https://mapwindow.discourse.group/t/change-shape-index/1148/11 "2021-11-30T20:30:49Z")

</div>

Hmm ok I have done that but will look at it again. Thank you, Jerry.

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [December 1, 2021, 5:12pm UTC](https://mapwindow.discourse.group/t/change-shape-index/1148/12 "2021-12-01T17:12:44Z")

</div>

I’m not questioning what you’re telling me, Mike. I only suggest backtracking because if the map first tells you that there are 100+ shapes in the file, and at a later point it tells you that there is only 1 shape, then it’s just a matter of “divide and conquer” to figure out what happened in between.

Kind Regards,  
Jerry.

---

<div class="post-metadata">

**Author:** ![mikeradtke](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/mikeradtke/32/558_2.png) [@mikeradtke](https://mapwindow.discourse.group/u/mikeradtke)\
**Post date:** [December 1, 2021, 5:31pm UTC](https://mapwindow.discourse.group/t/change-shape-index/1148/13 "2021-12-01T17:31:06Z")

</div>

No no I didn’t take it that way at all! You are going out of your way to help me, which I appreciate immensely.  
Something curious - counting the number of shapes in this shapefile immediately before I try to insert a shape shows 100+. Immediately after insert it shows 1. Not sure if that means anything?

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [December 1, 2021, 6:03pm UTC](https://mapwindow.discourse.group/t/change-shape-index/1148/14 "2021-12-01T18:03:42Z")

</div>

When you say ‘counting the number of shapes’, do you mean you are counting them? or are you calling [NumShapes](https://www.mapwindow.org/documentation/mapwingis4.9/class_shapefile.html#a8c16abfdf418de3ae93475953958e907) before and after, to get the shape count?

If the count is immediately being reduced to 1, it would seem that the shapefile has been reinitialized somehow. You could try calling [LastErrorCode](https://www.mapwindow.org/documentation/mapwingis4.9/class_shapefile.html#aba1199cd5b54004fde4c40fa3a5bd75c) following the call, and if non-zero, fetch the error message.

---

<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:** [January 5, 2022, 11:38am UTC](https://mapwindow.discourse.group/t/change-shape-index/1148/15 "2022-01-05T11:38:22Z")

</div>

@mikeradtke Is this still an issue or did you meanwhile solve this?

---

<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:** [February 2, 2022, 9:22am UTC](https://mapwindow.discourse.group/t/change-shape-index/1148/16 "2022-02-02T09:22:26Z")

</div>


