Select to view content in your preferred language

I need Help on any Python script that can clip a data frame to a selected feature

9158
27
Jump to solution
08-10-2012 06:02 PM
OLANIYANOLAKUNLE
Frequent Contributor
I am in bear need of any python script that can clip the data frame extent to a selected feature, a script different from the clipping under the geoprocesssing tool?
Tags (2)
0 Kudos
27 Replies
OLANIYANOLAKUNLE
Frequent Contributor
i used the following python script to create a script tool to select an attribute of a particular feature, but it returns all the records of the feature specified in the FeatureClass;

import arcpy
arcpy.env.overwriteOutput = True

try:
# Get the input parameters for the selection tool
FeatureClass = arcpy.GetParameterAsText(0)
SQLStatement = arcpy.GetParameterAsText(1)

# Select by Layer Attribute
arcpy.SelectLayerByAttribute_management(FeatureClass, "NEW_SELECTION", "")


# Report a success message
arcpy.AddMessage("All done!")

except:
# Report an error messages
arcpy.AddError("Could not complete the selection")

# Report any error messages that the selection tool might have generated
arcpy.AddMessage(arcpy.GetMessages())

Please any suggestions. Thanks
0 Kudos
RichardFairhurst
MVP Alum
You did not use the SQLStatement parameter to filter the selection. I would revise the code as follows:
import arcpy
arcpy.env.overwriteOutput = True

try:
  # Get the input parameters for the selection tool
  FeatureClass = arcpy.GetParameterAsText(0)
  SQLStatement = arcpy.GetParameterAsText(1)

  # Select by Layer Attribute
  arcpy.SelectLayerByAttribute_management(FeatureClass, "NEW_SELECTION", SQLStatement)


  # Report a success message 
  arcpy.AddMessage("All done!")

except:
  # Report an error messages
  arcpy.AddError("Could not complete the selection")

  # Report any error messages that the selection tool might have generated 
  arcpy.AddMessage(arcpy.GetMessages())
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
Thanks rfairhur24 for the nugget of information, but when i run the script it does not return any record(s)? Any other suggestion(s)
0 Kudos
RichardFairhurst
MVP Alum
Thanks rfairhur24 for the nugget of information, but when i run the script it does not return any record(s)? Any other suggestion(s)


Is your SQLStatement valid for selecting records?  Paste it into a Select By Attributes query on the feature class you have used in your model in ArcMap and run it to see what it selects.  If it does not select anything build a query that does select something and paste that working query into the tool within your model to see if it gets the same selection results.

The selection results will not appear on your map, they will just appear as an output for use within the Model for another tool to use, so connect another tool that uses a feature selection and see if you get any output.  let me know what tool you used and publish your SQLStatement.

If you are trying to get the selection to show up in the map you have open I believe that takes other Python code.
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
Yes the SQL Statement is valid for selecting records, and i intend to run the script tool in an active view not within a model builder. After the query is completed no record is selected even for the valid SQL Statement.
0 Kudos
BruceBacia
Frequent Contributor
Have  tried using arcpy.AddMessage(SQLStatement) to see what exactly is being passed to select by attribute?  Also, you need to convert the feature class to a feature layer or table view prior to using select by attribute.
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
Thanks brucejr312 for the python script for clipping the layers data frame extent, i've been trying to use your script under a single button so that all the clipping processes would be done by the click of a botton but i can't get to hardcode the exportLayer = arcpy.GetParameterAsText(0) as i.e. (datalayer1, datalayer2, datalayer3 etc) instead of using the script as a script tool and i also want the results of the clipping process to be enabled and added to the dataframe by default and i also want the output to be overwritten each time i run the clip.Please can you profer a solution to this. Thanks once again.
0 Kudos
BruceBacia
Frequent Contributor
The adding the layers to the map and the overwriting would be a pretty simple fix.  As far as the exportLayer, would you just want it to clip every layer in the map?
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
Yes exactly
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
Do you know of any arcpy script that can change an field attribute property named Bearing from double - numeric to double - direction ( radians & quadrant bearing) and field attribute property named Distance to numeric and rounding of 2 decimal places. Thanks
0 Kudos