0

I am working on spatial interpolation. To extract values for my required points, I am using "Extract values to points" tool from Spatial interpolation toolbox. The tool is working good in ArcMap 10.5. But when I try to use the tool from ArcPy. Same value is generated for each point in test set. Here is my code:

#point extraction
# Set local variables
inPointFeatures = "test.lyr"
inRaster = "out1"
outPointFeatures = "extractvaluespts.dbf"

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# Execute ExtractValuesToPoints
ExtractValuesToPoints(inPointFeatures, inRaster, outPointFeatures,
                  "INTERPOLATE", "VALUE_ONLY")

My code for test point layer is as follow:

   ##XY layer for test
# Set the local variables
in_Table = "test.csv"
x_coords = "LONGITUDE"
y_coords = "LATITUDE"
z_coords = ""
out_Layer = "test"
saved_Layer = r"C:\Users\myc\Desktop\thesis combine\weather parameters\max temp\with elevation\chk1\test.lyr"

# Set the spatial reference
spRef = r"Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj"

# Make the XY event layer...
arcpy.MakeXYEventLayer_management(in_Table, x_coords, y_coords, out_Layer, spRef, z_coords)

# Print the total rows
print(arcpy.GetCount_management(out_Layer))

# Save to a layer file
arcpy.SaveToLayerFile_management(out_Layer, saved_Layer)

I am unable to understand the reason behind this error?

P.S. When I work with geostatistical analyst tool set and GA to points, it works perfect, but I require to use raster calculator that's why I cannot use Geostatistical analyst. How can I fix this?

  • Please Edit the question to contain the full error message as text. – Vince Oct 6 at 17:58
  • It doesn't look like you ever save the layer file after doing MakeXYEventLayer. Try adding arcpy.SaveToLayerFile_management(out_Layer, saved_Layer). Then manually inspect that layer in ArcMap to make sure it looks correct, and ensure that arcpy.env.workspace is set properly (as you do not specify the full path for inPointFeatures – Nick Oct 6 at 18:13
  • No error message, wrong results observed in extracted point values @Vince – Maria Sheikh Oct 6 at 18:55
  • Sorry, missed line in question, otherwise its included in code. Edited it here @Nick – Maria Sheikh Oct 6 at 18:59
  • I suggest you to try running tool from the toolbox and copy python snippet from the Results window. Then compare the tool parameters with your script, ExtractValuesToPoints(inPointFeatures, .... This might be related to the tool environment settings. When you said wrong results observed, is there a systematic shift in your results or is the cell size similar or same for both results? – fatih_dur Oct 6 at 23:27
1

I believe the issue is to do with your input, it's a CSV file, which is simply a text file not a spatial dataset such as geodatabase featureclass or shapefile.

Firstly as as @Nick hints your code is missing the workspace environment setting so it does not know where in_Table is. Either make it a full path string or set the workspace.

Secondly you construct a layer from your CSV file and then save it as a layer file. A layer file IS NOT DATA, it's the symbology, labelling and definition of your source data.

What you want to do is having made your XY event layer, is to export it to a proper spatial dataset format, I would recommend a file geodatabase featureclass, you can use the Copy Features tool to achieve that, then reference that in your code which extracts values to points. As you now have a proper spatial dataset the tool can insert fields and populate.

CSV files are simply text files that just happen to have values separated by commas and have no mechanisms for inserting new fields and updating rows like database tables.

| improve this answer | |
  • I utilized copy feature tool from data management toolset to convert the XY layer to shape file format but still same problem, results are depicting same interpolated value for my test points. – Maria Sheikh Oct 7 at 6:56
  • Are you trying to extract values from a DEM that is in a different coordinate system to your WGS84 points? Project your points into the coordinate system of the DEM. – Hornbydd Oct 7 at 9:54
  • I had data points WGS 1984 with elevation, I applied idw interpolation using spatial analyst toolbox and then raster calculation on resultant raster and then extracted values for test points. Test points are already converted in same coordinate systems @Hornbydd – Maria Sheikh Oct 7 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.