POST
|
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 more
02-25-2013
08:41 PM
|
0
|
0
|
620
|
POST
|
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)
... View more
02-24-2013
10:45 PM
|
0
|
0
|
620
|
POST
|
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.
... View more
02-24-2013
08:05 PM
|
0
|
0
|
620
|
POST
|
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.
... View more
02-24-2013
07:47 PM
|
0
|
0
|
620
|
POST
|
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.
... View more
02-19-2013
02:56 AM
|
0
|
0
|
620
|
POST
|
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 🙂
... View more
02-18-2013
05:36 AM
|
0
|
8
|
840
|
POST
|
Ok, i feel a little foolish. I looked in another post: http://forums.arcgis.com/threads/65128-ArcPy-script-in-toolbox-what-do-I-do-about-optional-parameters and found the answer and it works! 🙂 the correct code is: # 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
hel_openland_shp = "W:\\Av_data\\cover_new\\mamag_mapi\\hel_openland.shp"
Output_Layer = "hel_openland_Layer"
arcpy.MakeFeatureLayer_management(hel_openland_shp, Output_Layer)
arcpy.SelectLayerByAttribute_management(Output_Layer, "NEW_SELECTION", "\"GUSH_NUM\" = "+ Gush)
... View more
08-21-2012
02:44 AM
|
0
|
0
|
222
|
POST
|
Hello, Im trying to write a code that will be used as a tool on arcgis. The user will have to insert the number of a Block and the Number of a Parcel in 1 shapefile and the tool will create a JPEG map of the Block and Parcel. The 1st thing i need to do is to select the correct Parcel and Block (Gush = Block, Helka = Parcel). The field for Gush in the shapefile is "GUSH_NUM", and the field for Helka is "Parcel" So both Gush and Helka will have to be arcpy.GetParameterAsText, and using a variable in the where clause of the select by attribute tool. But how do i write it in the where clause of the select by attribute tool? I tried using this post for help: http://forums.arcgis.com/threads/20459-Select-By-Attributes-using-a-variable-in-the-Where-clause?highlight=Select+attributes So i tried writing the where clause as the post suggests but it still doesnt work. I get an error - invalid expression. Here is my code so far, i tried only on the Gush for now. Will apriciate any help. 🙂 # 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 hel_openland_shp = "W:\\Av_data\\cover_new\\mamag_mapi\\hel_openland.shp" Output_Layer = "hel_openland_Layer" arcpy.MakeFeatureLayer_management(hel_openland_shp, Output_Layer) arcpy.SelectLayerByAttribute_management(Output_Layer, "NEW_SELECTION", "\"GUSH_NUM\" = " + "'" + Gush + "'")
... View more
08-21-2012
02:30 AM
|
0
|
1
|
1890
|
POST
|
You're right - previos code was not working. It seem that GetCount output has to be casted to integer before comparing. This code should work - added int() around GetCount: # Exporting the selected features to new Shapefiles if there're selected features
if int(arcpy.GetCount_management(Output_Layer).getOutput(0)) > 0:
arcpy.CopyFeatures_management(Output_Layer, "topo_lines")
if int(arcpy.GetCount_management(Output_Layer__6_).getOutput(0)) > 0:
arcpy.CopyFeatures_management(Output_Layer__6_, "infrastructure") Yes! int () solved the problam. Now i recieve only 1 shapefile, topo_line, which intersects with the buffer. Thank you very much 🙂
... View more
07-10-2012
04:55 AM
|
0
|
0
|
428
|
POST
|
Try using Get Count to test if there're selected features before copying features to new feature class: # Exporting the selected features to new Shapefiles if there're selected features
if arcpy.GetCount_management(Output_Layer).getOutput(0) > 0:
arcpy.CopyFeatures_management(Output_Layer, "topo_lines")
if arcpy.GetCount_management(Output_Layer__6_).getOutput(0) > 0:
arcpy.CopyFeatures_management(Output_Layer__6_, "infrastructure") Thank you for the help, but still no good. i get 2 new shapfiles: 1) topo_lines, which is ok. 2) infrastructure, which is empty. maybe there is a way of deleting the empty shapefiles after they are made, in the script?
... View more
07-10-2012
04:01 AM
|
0
|
0
|
428
|
POST
|
Hello, I have a script that: 1) makes a buffer around the input. 2) intersects the buffer with other shapefiles. 3) selects the intersecting features in every shapefile. then i want to export the selected featurs (only selected featurs) from each shapefile into new shapefiles. Ive tried using arcpy.CopyFeatures_managment () and also arcpy.Select_analysis (). Both work good and give me the new shapefiles. But here is the problam: not all the shapefiles intersects with the buffer, so some of them have no selected features at all. But both arcpy.CopyFeatures_managment () and arcpy.Select_analysis () make new shapefiles for all of them, even if the original shapefile had no selected features. This means some of the new shapefiles i get are empty and have no rows in them (becouse there were no selected features to export). so why does the script export those empty shapefiles? how can i export only the selected features in the shapefiles that do intersect with the buffer? Here is an example of the code: # Import the arcgisscripting module. import arcpy import os import sys import string # Setting the Variabls. # Input Shapefile or GDB Feature class (must be Polygon). Input_Kav_Kachol = arcpy.GetParameterAsText(0) # Output Location (the folder where the resualt will be saved, can be GDB or Dataset inside GDB). Workspace = arcpy.GetParameterAsText(1) # Setting the Buffer distance around the Input Shapefile or Feature class. Distance = arcpy.GetParameterAsText(2) arcpy.env.workspace = Workspace Output_Feature_Class = "Buffer" # Creating the Buffer. result = arcpy.Buffer_analysis(Input_Kav_Kachol, Output_Feature_Class, Distance, "", "", "all") buffc = result.getOutput(0) # Setting Local variables for the Make Feature Layer tool: topo_line_shp = "W:\\Av_data\\cover_new\\mamag_mapi\\topo_line.shp" infra_line_shp = "W:\\Av_data\\cover_new\\mamag_mapi\\infra_line.shp" Output_Layer = "topo_line_Layer" Output_Layer__6_ = "infra_line_Layer" # Making temporary Feature Layers from the mamag_mapi shapefils. arcpy.MakeFeatureLayer_management(topo_line_shp, Output_Layer) arcpy.MakeFeatureLayer_management(infra_line_shp, Output_Layer__6_) # Selecting all the Lines and Polygons from mamag_mapi Shapefiles # that intersects with the buffer. arcpy.SelectLayerByLocation_management(Output_Layer, "INTERSECT", buffc, "", "NEW_SELECTION") arcpy.SelectLayerByLocation_management(Output_Layer__6_, "INTERSECT", buffc, "", "NEW_SELECTION") # Exporting the selected features to new Shapefiles. arcpy.CopyFeatures_management(Output_Layer, "topo_lines") arcpy.CopyFeatures_management(Output_Layer__6_, "infrastructure") So Output_Layer (topo_lines) does intersect with the buffer and has selected features in it, so the new Shapefile has rows in it. But Output_Layer__6_ (infrastructure) does not intersect with the buffer. Regardles i get a new shapefile which is empty - and i dont want to get this empty shapefile at all. Will apriciate any tip 🙂
... View more
07-10-2012
02:17 AM
|
0
|
4
|
3487
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|