df.zoomToSelectedFeatures not working

839
8
Jump to solution
02-18-2013 05:36 AM
IlanGoichman
New Contributor III
Hello,

I've writen a Python script which worked perfectly on v 10. now, when i have v 10.1, the script works good except for 1 part: df.zoomToSelectedFeatures. The script selects a parcel, copies it, adds it to an mxd, zooms to it (it's suppose to do it), changes its symbology, sets the df.scale and exports to JPEG file.

Here is the script:

 #Import the arcgisscripting module.
import arcpy

# Setting the Variabls.

# Input gush and Helka.
Gush = 11516
Helka = 23

# Output Location (the folder where the resualt will be saved).
Workspace = r"E:\Local Disk (E)\users\Ilan\Projects\Helkot_Maps\11516_23"
arcpy.env.workspace = Workspace

# Exporting the wanted Helka:
hel_openland_shp = r"W:\Av_data\CADASTER_2012-11-18\PARCEL_ALL.shp"
Output_Layer = "hel_openland_Layer"

arcpy.MakeFeatureLayer_management(hel_openland_shp, Output_Layer)

arcpy.SelectLayerByAttribute_management(Output_Layer, "NEW_SELECTION", " \"GUSH_NUM\" = " + str(Gush))


arcpy.SelectLayerByAttribute_management(Output_Layer, "SUBSET_SELECTION", "\"PARCEL\" = " + str(Helka))

result = arcpy.CopyFeatures_management(Output_Layer, "Gush_" + str(Gush) + "_Helka_" + str(Helka))
helkafc = result.getOutput(0)

# Setting the Map file and the Data Frames:
mxd = arcpy.mapping.MapDocument("E:\\Local Disk (E)\\users\\Ilan\\Projects\\Python\\Helkot_Maps_tool\\MXDproject.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Parcel_Map") [0]

# Adding the wanted Helka to the first Data Frame:
helka_layer = "Helka" + " " + str(Helka)

arcpy.MakeFeatureLayer_management(helkafc, helka_layer)

helkalyr = arcpy.mapping.Layer(helka_layer)

arcpy.mapping.AddLayer(df, helkalyr, "TOP")

# Setting the Scale for the Data Frame and applying symbology to the Helka:

arcpy.SelectLayerByAttribute_management(str(helkalyr), "NEW_SELECTION", "\"PARCEL\" = 23")

df.zoomToSelectedFeatures()

arcpy.SelectLayerByAttribute_management(str(helkalyr), "CLEAR_SELECTION", "")

inputLayer = arcpy.mapping.ListLayers(mxd, helkalyr.name, df)[0]
symbologyLayer = arcpy.mapping.Layer("E:\\Local Disk (E)\\users\\Ilan\\Projects\\Python\\Helkot_Maps_tool\\wanted_layer.lyr")

arcpy.mapping.UpdateLayer(df, inputLayer, symbologyLayer, True)

df.scale = 8000

# Changing text elements in the map to match the Gush and Helka:
for textElement in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
  if textElement.text == "map title":
    textElement.text = "Gush" + " " + str(Gush) + " " + "Helka" + " " + str(Helka)

arcpy.RefreshActiveView()

# Exporting the Map file to a JPEG file:
arcpy.mapping.ExportToJPEG(mxd, Workspace, df_export_width=640, df_export_height=480, resolution=150,
                     world_file=False, color_mode=1, jpeg_quality=100, progressive=False)


This is only a trial example. the final one will have 'arcpy.GetParameterAsText(0)' for the Gush, Helka and workspace parameters.
I simply dont understand why the df.zoomToSelectedFeatures function doesnt work. i get a jpeg file with all the layers and the symbology, and the scale and the text changes, but without the zoom in to the selected features.

Will appriciate any help 🙂
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
IlanGoichman
New Contributor III
I've solved it!
The answer was adding another arcpy.MakeFeatureLayer_management () command after adding the layer to the mxd. Apparantly 'helkaelyr' was no longer a feature layer after adding it.
Plus some new paramaters and checks to make sure there was actually something being selected by the arcpy.SelectLayerByAttribute_management () command.

Here is the new code, it works perfectly now:

# Import the arcgisscripting module. import arcpy  # Setting the Variabls.  # Input gush and Helka. Gush = arcpy.GetParameterAsText(0) Helka = arcpy.GetParameterAsText(1)  # Output Location (the folder where the resualt will be saved). Workspace = arcpy.GetParameterAsText(2) arcpy.env.workspace = Workspace  # Exporting the wanted Helka: hel_openland_shp = r"W:\Av_data\CADASTER_2012-11-18\PARCEL_ALL.shp" Output_Layer = "hel_openland_Layer"  arcpy.MakeFeatureLayer_management(hel_openland_shp, Output_Layer)  arcpy.SelectLayerByAttribute_management(Output_Layer, "NEW_SELECTION", "\"GUSH_NUM\" = " + str(Gush))  arcpy.SelectLayerByAttribute_management(Output_Layer, "SUBSET_SELECTION", "\"PARCEL\" = " + str(Helka))  result = arcpy.CopyFeatures_management(Output_Layer, "Gush_" + str(Gush) + "_Helka_" + str(Helka)) helkafc = result.getOutput(0)  arcpy.SelectLayerByAttribute_management(Output_Layer, "CLEAR_SELECTION", "")  # Setting the Map file and the Data Frames: mxd = arcpy.mapping.MapDocument(r"E:\Local Disk (E)\users\Ilan\Projects\Python\Helkot_Maps_tool\MXDproject.mxd") df = arcpy.mapping.ListDataFrames(mxd, "Parcel_Map") [0]  # Adding the wanted Helka to the first Data Frame: helka_layer = "Helka" + "_" + Helka  arcpy.MakeFeatureLayer_management(helkafc, helka_layer)  helkalyr = arcpy.mapping.Layer(helka_layer)  arcpy.mapping.AddLayer(df, helkalyr, "TOP")  # Setting the Scale for the Data Frame and applying symbology to the Helka:  lyr = arcpy.mapping.ListLayers(mxd, helkalyr.name, df)[0]  print lyr  arcpy.MakeFeatureLayer_management(helkalyr, lyr)  result1 = arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", "")  if int(arcpy.GetCount_management(lyr).getOutput(0)) > 0:     print result1.getOutput(0)  df.zoomToSelectedFeatures()  df.scale = 8000  arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION", "")  inputLayer = arcpy.mapping.ListLayers(mxd, helkalyr.name, df)[0] symbologyLayer = arcpy.mapping.Layer("E:\\Local Disk (E)\\users\\Ilan\\Projects\\Python\\Helkot_Maps_tool\\wanted_layer.lyr")  arcpy.mapping.UpdateLayer(df, inputLayer, symbologyLayer, True)  # Changing text elements in the map to match the Gush and Helka: for textElement in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):   if textElement.text == "map title":     textElement.text = "Gush" + " " + Gush + " " + "Helka" + " " + Helka  arcpy.RefreshActiveView()  # Exporting the Map file to a JPEG file: arcpy.mapping.ExportToJPEG(mxd, Workspace, df_export_width=640, df_export_height=480, resolution=150,                      world_file=False, color_mode=1, jpeg_quality=100, progressive=False)


Thank you very much for all the help! 🙂

View solution in original post

0 Kudos
8 Replies
MathewCoyle
Frequent Contributor
You have this set to a static scale. Is this not the result you are getting?

df.scale = 8000


You could also try isolating the layer to make sure there aren't any other selections.
df.extent = lyr.getSelectedExtent()
0 Kudos
IlanGoichman
New Contributor III
You have this set to a static scale. Is this not the result you are getting? 

df.scale = 8000


You could also try isolating the layer to make sure there aren't any other selections. 
df.extent = lyr.getSelectedExtent()



The JPEg which the script generates is at 1:8000 scale, as specified in the script. But, the map isnt zoomd to the selected feature. It's fixed on another part, and i just dont understand why.

If i use 'df.extent = lyr.getSelectedExtent()' then the script crashes when it gets to that part and gives me an error:
exceprions:EOEError:[Errorno 10054] An existing connection was forcibly closed by remote host.
I realy have no idea why it's not working.
0 Kudos
MathewCoyle
Frequent Contributor
That error seems to indicate you are not selecting anything or your selection is not valid. Have you checked to see if you actually select anything using .getOuput() or .FIDSet? Have you tried doing this process manually in ArcMap?
0 Kudos
IlanGoichman
New Contributor III
That error seems to indicate you are not selecting anything or your selection is not valid. Have you checked to see if you actually select anything using .getOuput() or .FIDSet? Have you tried doing this process manually in ArcMap?


Thank you for your help. i have added a line to the script to check if there is actually something being selected usint getOutput (), and then printing the result. I do get the name of the parcel, which means that it is being seleted. But still, df.zoomToSelectedFeatures is not working. The jpeg is still fixed on another part of the map and not on the selected parcel.

can the be some other way of writing this is V 10.1? since its working in v 10? I cant think on anything else, but  im not that expirienced in Python.
0 Kudos
IlanGoichman
New Contributor III
Thank you for your help. i have added a line to the script to check if there is actually something being selected usint getOutput (), and then printing the result. I do get the name of the parcel, which means that it is being seleted. But still, df.zoomToSelectedFeatures is not working. The jpeg is still fixed on another part of the map and not on the selected parcel.

can the be some other way of writing this is V 10.1? since its working in v 10? I cant think on anything else, but  im not that expirienced in Python.


About trying it manualy in Arcmap - the select by attribute tool works good, then the map zooms to the selcted feature when i pushe the 'zoom to selected featuresw button.

If i try the script in Python window, df.zoomToSelectedFeatures  still doesnt work.

thank you.
0 Kudos
IlanGoichman
New Contributor III
I've solved it!

I deleted the df.zoomToSelectedFeatures line from the script. Apperantely, in V 10.1 there is an automatic zoom to the selected features, so no command is actually needed, only setting the scale after selection, and deselecting the feature. Other then that i have no other explanation for this. 

Here is my script, which is working perfectly now:

# Import the arcgisscripting module.
import arcpy

# Setting the Variabls.

# Input gush and Helka.
Gush = arcpy.GetParameterAsText(0)
Helka = arcpy.GetParameterAsText(1)

# Output Location (the folder where the resualt will be saved).
Workspace = arcpy.GetParameterAsText(2)
arcpy.env.workspace = Workspace

# Exporting the wanted Helka:
hel_openland_shp = r"W:\Av_data\CADASTER_2012-11-18\PARCEL_ALL.shp"
Output_Layer = "hel_openland_Layer"

arcpy.MakeFeatureLayer_management(hel_openland_shp, Output_Layer)

arcpy.SelectLayerByAttribute_management(Output_Layer, "NEW_SELECTION", "\"GUSH_NUM\" = " + Gush)

arcpy.SelectLayerByAttribute_management(Output_Layer, "SUBSET_SELECTION", "\"PARCEL\" = " + Helka)

result = arcpy.CopyFeatures_management(Output_Layer, "Gush_" + Gush + "_Helka_" + Helka)
helkafc = result.getOutput(0)

# Setting the Map file and the Data Frames:
mxd = arcpy.mapping.MapDocument(r"E:\Local Disk (E)\users\Ilan\Projects\Python\Helkot_Maps_tool\MXDproject.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Parcel_Map") [0]

# Adding the wanted Helka to the first Data Frame:
helka_layer = "Helka" + " " + Helka

arcpy.MakeFeatureLayer_management(helkafc, helka_layer)

helkalyr = arcpy.mapping.Layer(helka_layer)

arcpy.mapping.AddLayer(df, helkalyr, "TOP")

# Setting the Scale for the first Data Frame and applying symbology to the Helka:
result = arcpy.SelectLayerByAttribute_management(helkalyr, "NEW_SELECTION", "\"PARCEL\" = 23")

if int(arcpy.GetCount_management(helkalyr).getOutput(0)) > 0:
    print result.getOutput(0)

df.scale = 8000

arcpy.SelectLayerByAttribute_management(helkalyr, "CLEAR_SELECTION", "")

inputLayer = arcpy.mapping.ListLayers(mxd, helkalyr.name, df)[0]
symbologyLayer = arcpy.mapping.Layer("E:\\Local Disk (E)\\users\\Ilan\\Projects\\Python\\Helkot_Maps_tool\\wanted_layer.lyr")

arcpy.mapping.UpdateLayer(df, inputLayer, symbologyLayer, True)

# Changing text elements in the map to match the Gush and Helka:
for textElement in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
  if textElement.text == "map title":
    textElement.text = "Gush" + " " + Gush + " " + "Helka" + " " + Helka

arcpy.RefreshActiveView()

# Exporting the Map file to a JPEG file:
arcpy.mapping.ExportToJPEG(mxd, Workspace, df_export_width=640, df_export_height=480, resolution=150,
                     world_file=False, color_mode=1, jpeg_quality=100, progressive=False)
0 Kudos
MathewCoyle
Frequent Contributor
Did you try this?
df.extent = helkalyr.getSelectedExtent()
0 Kudos
IlanGoichman
New Contributor III
I've solved it!
The answer was adding another arcpy.MakeFeatureLayer_management () command after adding the layer to the mxd. Apparantly 'helkaelyr' was no longer a feature layer after adding it.
Plus some new paramaters and checks to make sure there was actually something being selected by the arcpy.SelectLayerByAttribute_management () command.

Here is the new code, it works perfectly now:

# Import the arcgisscripting module. import arcpy  # Setting the Variabls.  # Input gush and Helka. Gush = arcpy.GetParameterAsText(0) Helka = arcpy.GetParameterAsText(1)  # Output Location (the folder where the resualt will be saved). Workspace = arcpy.GetParameterAsText(2) arcpy.env.workspace = Workspace  # Exporting the wanted Helka: hel_openland_shp = r"W:\Av_data\CADASTER_2012-11-18\PARCEL_ALL.shp" Output_Layer = "hel_openland_Layer"  arcpy.MakeFeatureLayer_management(hel_openland_shp, Output_Layer)  arcpy.SelectLayerByAttribute_management(Output_Layer, "NEW_SELECTION", "\"GUSH_NUM\" = " + str(Gush))  arcpy.SelectLayerByAttribute_management(Output_Layer, "SUBSET_SELECTION", "\"PARCEL\" = " + str(Helka))  result = arcpy.CopyFeatures_management(Output_Layer, "Gush_" + str(Gush) + "_Helka_" + str(Helka)) helkafc = result.getOutput(0)  arcpy.SelectLayerByAttribute_management(Output_Layer, "CLEAR_SELECTION", "")  # Setting the Map file and the Data Frames: mxd = arcpy.mapping.MapDocument(r"E:\Local Disk (E)\users\Ilan\Projects\Python\Helkot_Maps_tool\MXDproject.mxd") df = arcpy.mapping.ListDataFrames(mxd, "Parcel_Map") [0]  # Adding the wanted Helka to the first Data Frame: helka_layer = "Helka" + "_" + Helka  arcpy.MakeFeatureLayer_management(helkafc, helka_layer)  helkalyr = arcpy.mapping.Layer(helka_layer)  arcpy.mapping.AddLayer(df, helkalyr, "TOP")  # Setting the Scale for the Data Frame and applying symbology to the Helka:  lyr = arcpy.mapping.ListLayers(mxd, helkalyr.name, df)[0]  print lyr  arcpy.MakeFeatureLayer_management(helkalyr, lyr)  result1 = arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", "")  if int(arcpy.GetCount_management(lyr).getOutput(0)) > 0:     print result1.getOutput(0)  df.zoomToSelectedFeatures()  df.scale = 8000  arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION", "")  inputLayer = arcpy.mapping.ListLayers(mxd, helkalyr.name, df)[0] symbologyLayer = arcpy.mapping.Layer("E:\\Local Disk (E)\\users\\Ilan\\Projects\\Python\\Helkot_Maps_tool\\wanted_layer.lyr")  arcpy.mapping.UpdateLayer(df, inputLayer, symbologyLayer, True)  # Changing text elements in the map to match the Gush and Helka: for textElement in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):   if textElement.text == "map title":     textElement.text = "Gush" + " " + Gush + " " + "Helka" + " " + Helka  arcpy.RefreshActiveView()  # Exporting the Map file to a JPEG file: arcpy.mapping.ExportToJPEG(mxd, Workspace, df_export_width=640, df_export_height=480, resolution=150,                      world_file=False, color_mode=1, jpeg_quality=100, progressive=False)


Thank you very much for all the help! 🙂
0 Kudos