Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm trying to save the raster layer in the stand-alone python GUI but I keep getting a syntax error.

I'm using the code from Save To Layer File (Data Management). And the error that shows up is: the LA MESA part of below:

in_features = "spain_1901_1.shp"
out_layer0 = "spain_1901_1"
where_clause = '"NAME" = 'LA MESA''
workspace = "C:/output"

now I've replaced their fill in things from the website with what I need but I keep getting an error saying that LA MESA isn't the correct syntax even when I replace it with my own name.

please help!

EDIT: Here's the entire code:

#MAKE RASTER FILE
# Import system modules
import arcpy


# Set local variables
inNetCDFFile = "C:/Users/ESS/Desktop/temp/tmpy1901m01.nc"
variable = "tmp"
XDimension = "lon"
YDimension = "lat"
outRasterLayer = "C:/Users/ESS/Desktop/out/spain_1901_1.shp"
bandDimmension = ""
dimensionValues = ""
valueSelectionMethod = ""

# Execute MakeNetCDFRasterLayer
arcpy.MakeNetCDFRasterLayer_md(inNetCDFFile, variable, XDimension, YDimension, outRasterLayer, bandDimmension, dimensionValues, valueSelectionMethod)

#SAVE IT!!!!!!
# Name: SaveToLayerFile_Example2.py
# Description: Saves an inMemory layer to a file on disk
# Author: ESRI

# Import system modules
import arcpy
from arcpy import env

# Set workspace
env.workspace = "C:/Users/ESS/Desktop/out"

# Set local variables
in_layer = "spain_1901_1"
out_layer = "spain_1901_1.lyr"

#MakeFeatureLayer variables
in_features = "spain_1901_1.shp"
out_layer0 = "spain_1901_1"
where_clause = '"NAME" = \'LA MESA\''
workspace = "C:/Users/ESS/Desktop/out"

try:
    # Execute MakeFeatureLayer
    arcpy.MakeFeatureLayer_management(in_features, out_layer0, where_clause, workspace)

    # Execute SaveToLayerFile
    arcpy.SaveToLayerFile_management(in_layer, out_layer, "ABSOLUTE")
except:
    print arcpy.GetMessages()

##THE CALCULATIONS  
# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *

# Set environment settings
env.workspace = "C:/Users/ESS/Desktop/out"

# Set local variables
inZoneData = "es2001.shp"
zoneField = "FIDS"
inValueRaster = "spain_1901_1"
outTable = "spain_1901_1_data.dbf"


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

# Execute ZonalStatisticsAsTable
outZSaT = ZonalStatisticsAsTable(inZoneData, zoneField, inValueRaster, 
                                 outTable, "NODATA", "ALL")
share|improve this question
    
I answered this too quickly. Since that syntax is used in the sample it would work here as well. We need to have more of your script to see if there is something else going on. It might be related to the fact that you are saving a raster layer. This sample was set up for a feature layer and there may be some differences in the syntax necessary. Please post the rest of your script. –  Get Spatial Oct 10 '12 at 22:50
add comment

4 Answers 4

Escape the LA MESA portion with backslashes. Like so, '"NAME" = \'LA MESA\'' Single quotes are reserved characters in Python and must be escaped so that the interpreter knows that you wish for them to be regarded as a string. Check this link on the Python documentation: http://docs.python.org/reference/lexical_analysis.html#string-literals

share|improve this answer
    
I tried that, didn't work. Thanks though! –  econ Oct 10 '12 at 23:02
    
I'll defer to @Get-Spatial for this then, as he correctly noted the trouble is likely somewhere more severe than the query string. My above should resolve any string escaping issues, however. –  AHigh Oct 10 '12 at 23:14
add comment

It looks like you are having a single vs. double quotes issue. I'm pretty sure you cannot have single quotes within other single quotes.

Try this:

in_features = "spain_1901_1.shp"
out_layer0 = "spain_1901_1"
where_clause = "'NAME' = 'LA MESA'"
workspace = "C:/output"

Edit --------------

The quote issue above is probably a minor problem.
You stated that you are exporting a Raster. This however, doesn't work for the details you have in the script provided. You cannot have a Raster Layer that is saved as a Shapefile. You need to first make sure the type of features you are exporting and what you are exporting to. The other issue is that the Where clause is specifying a field of title Name in the shapefile where the values are equal to La Mesa. Unless you have specifically created an attribute table for your Raster, the only contents you would be able to select for would be a field named Value, and the contents would be Integer values, not text.

As I said in my comment above, please provide more detail and I'll edit this answer as more info is available.

Edit 2 -----------------
Edit 3 -----------------

Simplicity is the answer here. One thing that is often confusing in ArcGIS is when you have to actually save a layer to disk, thus saving the actual Raster Dataset, or when you may use the Raster Layer as the input to a new function. Based on the script you added above, it looks like a number of the functions may actually be left out.

The first step is to Make a NetCDF Raster Layer

The output of this is a Raster Layer, which is essentially the ArcGIS Raster object properties with your NetCDF Layer as the source. This step negates the need for a couple of steps in your original script, which are as follows:

  • Make Feature Layer - This is not necessary for 2 reasons. The first is that it is the wrong function. Since you are dealing with a Raster, you would use the Make Raster Layer. Feature Layers deal with vector data, whereas Raster Layers deal with grid based data, and each has specific unique properties. The 2nd reason it is not necessary is that the Make NetCDF Raster Layer function outputs the same type of layer as this function would.
  • Save Layer File - This simply saves a .lyr file to disk, thus letting you bring up the Raster again with the same properties saved. It does not actually create a new Raster Dataset. That would be accomplished with a different function, which may be an option down below.

As I mentioned above, it is sometimes hard to determine when you can work with a Raster Layer as an input, or when you need to actually reference a Raster Dataset that is saved to disk. For that reason, I will include two different versions of the script. The 2nd version will include the intermediate step of saving the NetCDF Raster to a .tif on disk, and then referencing that in the Zonal Statistics function.

Version #1:

#MAKE RASTER FILE
# Import system modules
import arcpy
from arcpy import env
from arcpy import sa

# Set Local variables
inNetCDFFile = "C:/Users/ESS/Desktop/temp/tmpy1901m01.nc"
variable = "tmp"
XDimension = "lon"
YDimension = "lat"
outRasterLayer = "C:/Users/ESS/Desktop/out/spain_1901_1"
bandDimmension = ""
dimensionValues = ""
valueSelectionMethod = ""

# Execute MakeNetCDFRasterLayer
arcpy.MakeNetCDFRasterLayer_md(inNetCDFFile, variable, XDimension, YDimension, outRasterLayer, bandDimmension, dimensionValues, valueSelectionMethod)

#Perform Spatial Statistics calculations using Spatial Analyst
# Set environment settings
env.workspace = "C:/Users/ESS/Desktop/out"

# Set local variables
inZoneData = "es2001.shp"
zoneField = "FIDS"
inValueRaster = outRasterLayer
outTable = "spain_1901_1_data.dbf"


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

# Execute ZonalStatisticsAsTable
outZSaT = ZonalStatisticsAsTable(inZoneData, zoneField, inValueRaster, 
                                 outTable, "NODATA", "ALL")

A couple of important points to note: I have removed the references to saving a .shp file from your output Raster in the NetCDF Raster function. This is because the Shapefile format is a vector format and would likely generate an error upon running. Also, I have simply passed through the variable for the layer name of the output from the first function, to the variable for the input of the 2nd function.

In case the above doesn't work because the Zonal Statistics script requires an actual Raster dataset as input, I am including another version where the NetCDF Raster Layer output is saved to a Raster .tif file using the Raster Copy function.

Version 2:

#MAKE RASTER FILE
# Import system modules
import arcpy
from arcpy import env
from arcpy import sa

# Set Local variables
inNetCDFFile = "C:/Users/ESS/Desktop/temp/tmpy1901m01.nc"
variable = "tmp"
XDimension = "lon"
YDimension = "lat"
outRasterLayer = "C:/Users/ESS/Desktop/out/spain_1901_1"
bandDimmension = ""
dimensionValues = ""
valueSelectionMethod = ""

# Execute MakeNetCDFRasterLayer
arcpy.MakeNetCDFRasterLayer_md(inNetCDFFile, variable, XDimension, YDimension, outRasterLayer, bandDimmension, dimensionValues, valueSelectionMethod)

#Save Raster Layer to disk as .tif file

inrasterlayer = outRasterLayer
outRastertif = "C:/Users/ESS/Desktop/out/spain_1901_1.tif"

try:
    arcpy.CopyRaster_management(inrasterlayer,outRastertif)
except:
    print "Copy Raster example failed."
    print arcpy.GetMessages()

#Perform Spatial Statistics calculations using Spatial Analyst
# Set environment settings
env.workspace = "C:/Users/ESS/Desktop/out"

# Set local variables
inZoneData = "es2001.shp"
zoneField = "FIDS"
inValueRaster = "spain_1901_1.tif"
outTable = "spain_1901_1_data.dbf"


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

# Execute ZonalStatisticsAsTable
outZSaT = sa.ZonalStatisticsAsTable(inZoneData, zoneField, inValueRaster, 
                                 outTable, "NODATA", "ALL")

Hopefully this will help, let me know if there are still problems.

share|improve this answer
    
I'm really new at ArcGIS, but I have had some experience with python. My goal is to import a shp file, then import weather data in a .nc file. Do some computations with that file. Save it, then convert it to a .csv –  econ Oct 10 '12 at 23:05
    
@user10877 - I edited my answer to incorporate the code you included. Hopefully it will shed some light. –  Get Spatial Oct 11 '12 at 2:17
    
Thanks! I tried it and I got the following error –  econ Oct 11 '12 at 3:21
    
Executing: MakeRasterLayer spain_1901_1 spain_1901_1 # # # Start Time: Wed Oct 10 23:19:18 2012 Failed to execute. Parameters are not valid. ERROR 000865: Input raster: spain_1901_1 does not exist. Failed to execute (MakeRasterLayer). Failed at Wed Oct 10 23:19:19 2012 (Elapsed Time: 1.00 seconds) –  econ Oct 11 '12 at 3:21
    
Here, in_raster and out_raslayer names are same. Those should be different. May be this will be helpful: help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//… –  Emi Oct 11 '12 at 6:57
show 1 more comment

Please try:

where_clause = " NAME = 'LA MESA' "

NAME is a field name, not a string.

The WHERE clause 'NAME' = 'LA MESA' will never be true.

share|improve this answer
add comment

You can try:

where_clause = "\"Name\" = 'LA MESA'"

Or,

where_clause = "Name = 'LA MESA'"

Both should work, if the column "Name" is present in the attribute table.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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