Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. import arcpy
  2. from arcpy.analysis import *
  3.  
  4. #Define input/output
  5. input = arcpy.GetParameterAsText(0)
  6. fieldName = arcpy.GetParameterAsText(1)
  7. values = arcpy.GetParameterAsText(2)
  8. output = arcpy.GetParameterAsText(3)
  9.  
  10. #Build the where clause
  11. queryJoin = "' OR " + fieldName + " = '"
  12. whereClause = fieldName + " = '" + queryJoin.join(values) + "'"
  13.  
  14. Select(input, output, whereClause)
  15.  
  16. # Import system modules
  17. import arcpy
  18. from arcpy import env
  19.  
  20. # Set the workspace
  21. env.workspace = arcpy.GetParameterAsText(0)
  22. Dir = env.workspace
  23.  
  24. # Local variables
  25. input = arcpy.GetParameterAsText(1)
  26. polygon = arcpy.GetParameterAsText(2)
  27. expression = arcpy.GetParameterAsText(3)
  28. name = arcpy.GetParameterAsText(4)
  29.  
  30. # Make a layer from the input feature class
  31. arcpy.MakeFeatureLayer_management(input, "lyr")
  32.  
  33. # Select all points or polygons which overlap the polygon/s of interest
  34. arcpy.SelectLayerByLocation_management("lyr", "intersect", polygon, 0, "new_selection")
  35.  
  36. # Within selected features, further select based on a SQL query within the script tool
  37. arcpy.SelectLayerByAttribute_management("lyr", "SUBSET_SELECTION", expression)
  38.  
  39. # Write the selected features to a new featureclass
  40. arcpy.CopyFeatures_management("lyr", Dir + "\" + str(name))
  41.  
  42. def whereClause(table, field, values):
  43. """Takes a semicolon-delimited list of values and constructs a SQL WHERE
  44. clause to select those values within a given field and table."""
  45.  
  46. # Add field delimiters
  47. fieldDelimited = arcpy.AddFieldDelimiters(arcpy.Describe(table).path, field)
  48.  
  49. # Split multivalue at semicolons and strip quotes
  50. valueList = [value[1:-1]
  51. if (value.startswith("'") and value.endswith("'"))
  52. else value for value in values.split(';')]
  53.  
  54. # Determine field type
  55. fieldType = arcpy.ListFields(table, field)[0].type
  56.  
  57. # Add single-quotes for string field values
  58. if str(fieldType) == 'String':
  59. valueList = ["'%s'" % value for value in valueList]
  60.  
  61. # Format WHERE clause in the form of an IN statement
  62. whereClause = "%s IN(%s)" % (fieldDelimited, ', '.join(valueList))a
  63. return whereClause
  64.  
  65. # Add field delimiters
  66. fieldDelimited = arcpy.AddFieldDelimiters(arcpy.Describe(table).path, field)
  67.  
  68. # Split multivalue at semicolons and strip quotes
  69. valueList = [value[1:-1]
  70. if (value.startswith("'") and value.endswith("'"))
  71. else value for value in values.split(';')]
  72.  
  73. # Determine field type
  74. fieldType = arcpy.ListFields(table, field)[0].type
  75.  
  76. # Add single-quotes for string field values
  77. if str(fieldType) == 'String':
  78. valueList = ["'%s'" % value for value in valueList]
  79.  
  80. # Format WHERE clause in the form of an IN statement
  81. whereClause = "%s IN(%s)" % (fieldDelimited, ', '.join(valueList))a
  82. return whereClause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement