Hi James,Many thanks for your suggestion. As a result, I modified the code to add a new field, copy the values from the RASTERVALU field (instead of renaming it), and then delete the RASTERVALU field. Unfortunately, the same error still occurred.To try and isolate the cause of the problem, I reduced the code to the following:
FLResponse = cboResponse.Value
RLCovarTif = lboCovariate.List(0)
fileExtract = tmpFolder & "\UseExtraction0.shp"
gp.ExtractValuesToPoints_sa FLResponse, RLCovarTif, fileExtract, "NONE", "VALUE_ONLY"
FLResponse = "UseExtraction0"
gp.AddXY_management FLResponse
It worked, so the resulting question then was how to implement this working code in a loop that would process any number of raster layers. For unknown reasons, I found that I need to run the AddXY tool before I delete the newly created layer from the project. So I am currently adding it after the last extraction with this code:
' Process: Extract Values to Points...
If i = 0 Then
gp.ExtractValuesToPoints_sa FLResponse, RLCovarTif, fileExtract, "NONE", "VALUE_ONLY"
Else
gp.ExtractValuesToPoints_sa pFClass2, RLCovarTif, fileExtract, "NONE", "VALUE_ONLY"
End If
FLResponse = "UseExtraction" & i
'NEW CODE
If (i = (lboCovariate.ListCount - 1)) Then
gp.AddXY_management FLResponse
End If
The full code for that procedure is then:
For i = 0 To lboCovariate.ListCount - 1
fileExtract = tmpFolder & "\UseExtraction" & i & ".shp"
RLCovarTif = lboCovariate.List(i)
' Process: Extract Values to Points...
If i = 0 Then
gp.ExtractValuesToPoints_sa FLResponse, RLCovarTif, fileExtract, "NONE", "VALUE_ONLY"
Else
gp.ExtractValuesToPoints_sa pFClass2, RLCovarTif, fileExtract, "NONE", "VALUE_ONLY"
End If
FLResponse = "UseExtraction" & i
If (i = (lboCovariate.ListCount - 1)) Then
'Add X- and Y- coordinates
gp.AddXY_management FLResponse
End If
'Delete new layer from project
For j = 0 To pMap.LayerCount - 1
If (pMap.Layer(j).Name) = (FLResponse) Then
pMap.DeleteLayer pMap.Layer(j)
pMxDoc.UpdateContents
Exit For
End If
Next j
'Load most recent extraction point layer
Set pFWkSpace2 = pWkFactory2.OpenFromFile(tmpFolder & "\", 0)
Set pFClass2 = pFWkSpace2.OpenFeatureClass(FLResponse)
'Get all fields of extraction point layer
Set pFields = pFClass2.Fields
'This checks if a field of proposed name already exists
intRasField = pFields.FindField("RasVal" & i)
If intRasField = -1 Then 'Proposed field name does NOT exist
pFieldEdit.Name = ("RasVal" & i)
pFClass2.AddField pFieldEdit
'Set index to position of the new field
intRasField = pFields.FindField("RasVal" & i)
End If
'Find position of original raster field
intOrigRasVal = pFields.FindField("RASTERVALU")
'Set values of new field to RASTERVALU
Set pFCursor = pFClass2.Update(Nothing, True)
Set pFeature = pFCursor.NextFeature
Do Until pFeature Is Nothing
'Copy value from RASTERVALU (it will only 1st be in memory)
pFeature.Value(intRasField) = pFeature.Value(intOrigRasVal)
'Write it to the feature class
pFCursor.UpdateFeature pFeature
Set pFeature = pFCursor.NextFeature
Loop
Set pField = pFields.Field(pFields.FindField("RASTERVALU"))
pFClass2.DeleteField pField
'If last covariate layer in list was processed, add response data
If (i = (lboCovariate.ListCount - 1)) Then
'This checks if a field of proposed name already exists
intResField = pFields.FindField("Response")
If intResField = -1 Then 'Proposed field name does NOT exist
pFClass2.AddField pFieldEdit2
'Set index to position of the new field
intResField = pFields.FindField("Response")
End If
'Set values of Response field to use = 1
Set pFCursor = pFClass2.Update(Nothing, True)
Set pFeature = pFCursor.NextFeature
Do Until pFeature Is Nothing
pFeature.Value(intResField) = 1
'Write it to the feature class
pFCursor.UpdateFeature pFeature
Set pFeature = pFCursor.NextFeature
Loop
End If
Next i
Cheers, Gordon