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?
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.
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?
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.
Thatâs correct. Here is the code:
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
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.
Jerry,
I removed that line, but no shape showed on the map.
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.
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 in the shapefile? Something like
?sfnew.numShapes
and see what it returns?
It returns only 1⌠but there are actually 100+ shapes in shapefile7 (shapefile Iâm supposed to be creating a new shape in).
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.
Hmm ok I have done that but will look at it again. Thank you, Jerry.
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.
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?
When you say âcounting the number of shapesâ, do you mean you are counting them? or are you calling NumShapes 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 following the call, and if non-zero, fetch the error message.