Select to view content in your preferred language

Get Error on MakeFeatureLayer_management Processing

503
3
01-26-2012 11:27 PM
FransiskaWidiastuti
New Contributor
I am beginner about arcpy, i has a *.shp file (point), that i will split to few layers based on date in a date_idx50 field (string type).
I use this coding, to process single layer, and this is working :

  import os
  import arcpy
  from arcpy import env

  mxd = arcpy.mapping.MapDocument("C:/Arc-Train/forma/training/layout/frame_report_latihan.mxd")
  df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
  layer1 = arcpy.mapping.ListLayers(mxd)[3]

  q = "date_idx50 = '2010-12-01'"

  layer2 = arcpy.MakeFeatureLayer_management(layer1,"2010-12-01",q,"#","FID FID VISIBLE NONE;Shape Shape VISIBLE   NONE;id id VISIBLE NONE;lat lat VISIBLE NONE;lon lon VISIBLE NONE;date_idx50 date_idx50 VISIBLE NONE")

  arcpy.SaveToLayerFile_management(layer2,layerpath + "2010-12-01.lyr","#")


I need to looping process, so i need each date data in date_idx50 field, will be created as one layer, i try to change coding "q" like this :
 
  import os
  from datetime import date
  import arcpy
  from arcpy import env

  mxd = arcpy.mapping.MapDocument("C:/Arc-Train/forma/training/layout/frame_report_latihan.mxd")
  df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
  layer1 = arcpy.mapping.ListLayers(mxd)[3]

  tanggal = date(2010,12,1)       
  q = '"' + 'date_idx50' + ' ' + '=' + ' ' + "'" + str(tanggal) + "'" + '"'

  layer2 = arcpy.MakeFeatureLayer_management(layer1,"2010-12-01",q,"#","FID FID VISIBLE NONE;Shape Shape VISIBLE NONE;id id VISIBLE NONE;lat lat VISIBLE NONE;lon lon VISIBLE NONE;date_idx50 date_idx50 VISIBLE NONE")
  arcpy.SaveToLayerFile_management(layer3,layerpath + "2010-12-01.lyr","#")


But i get Error process in MakeFeatureLayer_management, (just for process 1 layer). So i can't continue to create loop coding.

anyone can help me? or maybe has a new idea?

Thanks
Tags (2)
0 Kudos
3 Replies
ChrisSnyder
Honored Contributor
Try using this:

q = "date_idx50 = " + "'" + str(tanggal) + "'"
layer2 = arcpy.MakeFeatureLayer_management(layer1,"2010-12-01",q,"","")
0 Kudos
FransiskaWidiastuti
New Contributor
Thank you..that is working now.  ^_^
0 Kudos
curtvprice
MVP Esteemed Contributor
Python format strings can make query (and calculation) expressions even a little easier to read, write, and debug. For example, a way to create your query expression using format strings could be:

whereExpr = "\"%s\" = \'%s\'" % ("date_idx50",tanggal)


Within the SQL expressions, field names may* be enclosed in double quotes and string literals must be in single quotes. In the above expression, the back slashes are to include quotes literally in the resulting string. The "%s" format string has a nice property of converting the corresponding values in the tuple after the "%" operator to a string format, avoiding clouding your code up with a str() function.

*Double quotes around field names may avoid problems with field names that could be SQL keywords, for example, COUNT.
0 Kudos