POST
|
I just finally realized this, after running my tool a few times. Checking the optional box does indeed automatically make the text reflect that of the default gdB workspace. However, now when I don't check it and put in the path to a different gdB, it still goes saves to the default gdB, and in the processing window states 'false' for parameter [0] and 'true' for [1], which I don't understand because [0] is not a boolean, it is a workspace directory. Going to write in some extra code to see if that helps. Thanks again Darren!
... View more
07-15-2015
11:49 AM
|
0
|
0
|
1022
|
POST
|
Great, thank you very much for the walk-through explanation. Thanks to everyone for helping point me to 'Tool Validation', which I wasn't very familiar with.
... View more
07-13-2015
02:38 PM
|
0
|
0
|
1022
|
POST
|
@ Ian - Thanks for your reply. There's a couple of different reasons for this. Firstly, the tool I'm making is not necessarily for people who are GIS savvy, ergo they may not know where/what their default gdB is, or how to find it. Also, being as how selecting a gdB path is subject to the most recently viewed folder (i.e., when selecting the folder button, it will not necessarily show the defaulted path, but rather the last path accessed), I want a quick and easy way to fill the folder with the default path by clicking the check-box. Lastly, I understand what I'm asking isn't exactly practical, I'm more just curious as to how one would go about doing this. Thanks!
... View more
07-13-2015
01:15 PM
|
0
|
0
|
1022
|
POST
|
I'm using ArcGIS for Desktop 10.3.0.4322 I have a script tool which requires a parameter in the GUI (folder path to a gdB, via GetParameterAsText) so that a later process can be saved there. The thing I want to do is make an Optional Boolean parameter, that when checked, automatically fills in the folder path gdB parameter. The boolean text asks the user, "Use default workspace?", to which if they check the box (boolean == True), then the folder path parameter will be automatically defaulted (saving the user a small amount of time). I have already created the boolean parameter in the script tool, I am however unaware of how to "fill in" the required parameter based on the user's boolean. Thanks in advance for any help/tips! Code Snippet: # Ask user to select the Geodatabase workspace to use for data output
userWorkspace = arcpy.GetParameterAsText(0)
# Ask user if they want to use default workspace (boolean)
useDefault = arcpy.GetParameterAsText(1)
# If user wants default workspace, then give it to the required parameter (0)
if useDefault == True:
arcpy.env.workspace = path
else:
# Set workspace environment based upon user's choice
arcpy.env.workspace = userWorkspace
... View more
07-13-2015
12:42 PM
|
0
|
7
|
3981
|
POST
|
Thanks, Dan. My apologies for not being clearer, I did/do understand what you were trying to say about the try/except and that by using "pass", I am simply letting it go without any helpful error messages. The main purpose for this (at the moment) is that I'm still writing the code (there is still more functionality that I want to accomplish after I figure out this mysterious occurrence). I wouldn't have even used the try/except code, because obviously TableToExcel would give its own errors if something went wrong. I merely am using the try/except/pass because of the fact that the excel inputs are optional for the user via the GUI (because the main purpose is to output a feature class, but some do/may want an excel file). In the case that someone doesn't want an excel file, I didn't want the script to freak out because there was no input for it and I don't need any error message to show, it just needs to simply carry along with the rest of the process (pass). Thanks again, and by the way, I've never sent any issues to tech support. Is there a specific way I should go about doing this?
... View more
07-02-2015
11:31 AM
|
0
|
1
|
826
|
POST
|
Thanks again for your help Dan. I haven't edited/updated my code that is listed here, as none of it seems to be the issue/remedy (except obviously for the TableToExcel tool). I took out the try/except statements and just let the excel inputs run (after putting them in on the GUI) and the same situation happens. Very odd!
... View more
07-02-2015
10:02 AM
|
0
|
3
|
826
|
POST
|
@Dan Patterson - Thank you very much for taking a look at my code and trying to help me discern the problem. Unfortunately, that is not the issue and did not solve my strange occurrence. I removed both 'arcpy.env.overwriteOutput' lines and then ran the script, it still had the same issue. So then I went a step further and in the Script Tool GUI I didn't put in any of the optional excel inputs, ran the script, and still the same issue occurred, regardless of the fact that arcpy.TableToExcel_conversion didn't even run, but the code was still there. Lastly, to reconfirm what I already knew, I coded (#) out the excel parts, and lo and behold, the feature class was added to the TOC and worked just fine. Besides there being some very strange bug or interaction happening with the TableToExcel tool, I have no idea what could be making this happen.
... View more
07-02-2015
07:33 AM
|
0
|
5
|
826
|
POST
|
I'm using `ArcGIS for Desktop 10.3.0.4322`. I have a peculiar occurrence that I don't quite fully understand. I have a Python Script Tool that does a number of different things, to include using the `ArcPy Mapping Module` to add newly acquired data to the `CURRENT` .mxd for viewing. This works perfectly fine, except when running a bit of additional code (bottom of post), which optionally uses (based on user input) the `arcpy.TableToExcel_conversion` tool in order to provide the user with an Excel spreadsheet version of the feature class data they queried. When watching the script run, the new feature class, `queryLayer`, shows up in the .mxd's Table of Contents, only to immediately disappear thereafter. While investigating, the feature class still exists and is fine (as would be expected), as the `arcpy.TableToExcel_conversion` tool does not delete the source file, it merely creates a newly converted one. So it is obvious that something is happening while/after the conversion is being made (as without the conversion it works fine), but I don't understand why it would be removing the newly added feature class from the .mxd's data frame/TOC. Any help or thoughts are greatly appreciated, thank you! # import arcpy module
import arcpy
# Ask user to select the Geodatabase workspace to use for data output
userWorkspace = arcpy.GetParameterAsText(0)
# Set workspace environment based upon user's choice
arcpy.env.workspace = userWorkspace
# Ask user to select an Oracle dB Connection
oracleDB = arcpy.GetParameterAsText(1)
# Ask user to name the Query Layer
queryLayer = arcpy.GetParameterAsText(2)
# Ask user if they want to overwrite previously named Query Layer
overwriteQ = arcpy.GetParameterAsText(3)
# Ask user for an SQL Query Expression to be run against the selected Oracle dB
sqlQuery = arcpy.GetParameterAsText(4)
# Ask user to name the output of excel file (this is optional, for those who want to do excel analysis)
excelName = arcpy.GetParameterAsText(5)
# Ask user if they want to overwrite previously named Excel File
overwriteE = arcpy.GetParameterAsText(6)
# Ask user to select the folder output of the excel file (optional as well, depending on if user wants an excel file)
excelFolder = arcpy.GetParameterAsText(7)
# Create spatial reference variable to assign to queryLayer
spatialRef = arcpy.SpatialReference("W:\Coordinate Systems\LRS Lambert.prj")
# Process: 'Make Query Layer' - Creates a Query Layer using the user's Oracle dB and SQL query expression
arcpy.MakeQueryLayer_management(oracleDB, "Temp_Layer", sqlQuery, "UNIQUE_ID", "POINT", "1050010", spatialRef)
# Set overwrite output for Query Layer to user's choice
arcpy.env.overwriteOutput = overwriteQ
# Process: 'Copy Features' - Copies the temporary Query Layer and stores it as a permanent feature class
arcpy.CopyFeatures_management("Temp_Layer", queryLayer)
# Process: 'Delete Features' - Deletes the temporary file Temp_Layer
# This allows for multiple executions of this tool within the same ArcMap session without error
arcpy.Delete_management("Temp_Layer")
# Process: 'Define Projection' - Defines the projection of queryLayer feature class output
arcpy.DefineProjection_management(queryLayer, spatialRef)
# Process: 'Add Field' - Adds new column fields to queryLayer
arcpy.AddField_management(queryLayer, "First_Time", "DATE") # The first LOGDT ping
arcpy.AddField_management(queryLayer, "Last_Time", "DATE") # The last LOGDT ping
arcpy.AddField_management(queryLayer, "Total_Time", "STRING") # Summation of the first to last ping in time
arcpy.AddField_management(queryLayer, "Total_Pings", "INTEGER") # Total number of pings (rows)
arcpy.AddField_management(queryLayer, "Possible_Pings", "INTEGER") # Total number of pings possible in given timeframe
arcpy.AddField_management(queryLayer, "Time_to_Process", "DATE") # How long it took for each ping to process
# Calculates the total number of rows (pings) for the Total_Pings field
numRows = int(arcpy.GetCount_management(queryLayer).getOutput(0))
# UpdateCursor that will write the value of numRows to the Total_Pings field
cursor = arcpy.da.UpdateCursor(queryLayer, "Total_Pings")
for row in cursor:
row[0] = numRows
cursor.updateRow(row)
# SearchCursor that will read the values of LOGDT and return the first value (earliest)
try: # try to read in values based on the user's SQL query
firstLogdt = [row[0] for row in arcpy.da.SearchCursor(queryLayer, "LOGDT")][0]
except: # exception error if the user's SQL query produces no return
err = "No Results Found Via SQL Query\nSCRIPT EXITED" # Error message variable
arcpy.AddError(err) # Print error message
sys.exit() # Exits the script, does not attempt to run any further
# UpdateCursor that will write the first (earliest) LOGDT value to the First_Time field
cursor = arcpy.da.UpdateCursor(queryLayer, "First_Time")
for row in cursor:
row[0] = firstLogdt
cursor.updateRow(row)
# SearchCursor that will read the values of LOGDT and return the last value (latest)
lastLogdt = [row[0] for row in arcpy.da.SearchCursor(queryLayer, "LOGDT")][-1]
# UpdateCursor that will write the last (latest) LOGDT value to the Last_Time field
cursor = arcpy.da.UpdateCursor(queryLayer, "Last_Time")
for row in cursor:
row[0] = lastLogdt
cursor.updateRow(row)
# Calculates the difference between firstLogdt and lastLogdt
timeDiff = lastLogdt - firstLogdt # Produces a timedelta object, not datetime
# Calculates the total number of seconds from timeDiff
timeSecs = timeDiff.total_seconds()
# Creates a function that will convert timeSecs to a readable format (yy:dd:hh:mm:ss)
def readTime(seconds):
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
years, days = divmod(days, 365)
return '%02d:%02d:%02d:%02d:%02d' % (years, days, hours, minutes, seconds)
# UpdateCursor that will write the time difference calculation to the Total_Time field
cursor = arcpy.da.UpdateCursor(queryLayer, "Total_Time")
for row in cursor:
row[0] = readTime(timeSecs)
cursor.updateRow(row)
# Calculates the total number of pings that would occur with optimal reception
possiblePings = timeSecs / 5 # 1 ping every 5 seconds
# UpdateCursor that will write the total number of pings possible to the Possible_Pings field
cursor = arcpy.da.UpdateCursor(queryLayer, "Possible_Pings")
for row in cursor:
row[0] = possiblePings
cursor.updateRow(row)
# Using ArcPy Mapping Module to add queryLayer to current .mxd map display and zoom to it
mxd = arcpy.mapping.MapDocument("CURRENT") # Set variable to currently active .mxd
dataFrame = arcpy.mapping.ListDataFrames(mxd)[0] # Set variable equal to the first data frame within mxd
addLayer = arcpy.mapping.Layer(queryLayer) # Set variable for queryLayer
arcpy.mapping.AddLayer(dataFrame, addLayer) # Adds queryLayer to the map
dataFrame.zoomToSelectedFeatures() # Zooms to queryLayer
arcpy.RefreshActiveView() # Refreshes the active data frame's view
# Set overwrite output for Excel File to user's choice
arcpy.env.overwriteOutput = overwriteE
# Change workspace environment to where the user wants the Excel file to be saved
try: # Tries to change the environment workspace to the user's optional excel output folder
arcpy.env.workspace = excelFolder
except:
pass # If user did not specify excel folder input, then let pass and continue on
# Process: 'Table To Excel' - Converts the final queried data to an excel spreadsheet file (optional)
try: # Tries to run the tool (will only work if user specified optional excel inputs)
arcpy.TableToExcel_conversion(queryLayer, excelName+'.xls')
except:
pass # If user did not specify optional excel inputs, then let pass and continue on
# delete cursor, row variables
del cursor, row
... View more
07-01-2015
01:40 PM
|
0
|
8
|
3404
|
POST
|
I've changed "total_time" to a string and it works perfectly fine, thanks for helping!
... View more
06-22-2015
11:33 AM
|
0
|
1
|
180
|
POST
|
You are correct, for whatever reason I wasn't thinking and was doing something else. Thanks for the help!
... View more
06-22-2015
11:32 AM
|
0
|
0
|
845
|
POST
|
Darren, Thanks for spotting that - I did as well too, but am unable to figure out exactly what I'm supposed to do to get the function value to work with the updateRow(row) ?
... View more
06-22-2015
11:15 AM
|
0
|
3
|
845
|
POST
|
Thanks for the suggestions, I have updated my question with the exact error and my full code. Also, I am aware that the same value is going to be updated for all rows, this is fine and is what is supposed to happen for this particular set of data I have.
... View more
06-22-2015
11:06 AM
|
0
|
0
|
845
|
POST
|
Thanks for the suggestions, I have updated my question with the exact error and my full code.
... View more
06-22-2015
11:06 AM
|
0
|
0
|
845
|
POST
|
I have the following code below and my question is this: How do I take the returning value of a function that I've defined and use it in an UpdateCursor (updateRow)? The issue is that I'm (obviously) getting an error with regards to the syntax type not being compatible. Essentially, what I'm trying to do is take the "raw" number of total_seconds() and update a column in the rows to make them easier to read - i.e. instead of it saying 3454352 seconds, it will be in dd:hh:mm:ss. The function works fine, it is the "cursor.updateRow(row)" that draws the syntax error. I understand that it is because it will not accept a function as the input, so how do I take just the value of the function and use that as the criteria for cursor.updateRow(row) ? Thanks in advance for any help! error: \Toolbox_Python_SQL_Scripts\HISTORY_ARCHIVE_query.py", line 89, in <module> cursor.updateRow(row) RuntimeError: The value type is incompatible with the field type. [Total_Time] Failed to execute (HistoryArchiveScriptTool). # import arcpy module
import arcpy
# Ask user to select the Geodatabase workspace to use for data output
userWorkspace = arcpy.GetParameterAsText(0)
# Set workspace environment based upon user's choice
arcpy.env.workspace = userWorkspace
# Ask user to select an Oracle dB Connection
oracleDB = arcpy.GetParameterAsText(1)
# Ask user to name the Query Layer
queryLayer = arcpy.GetParameterAsText(2)
# Ask user for an SQL Query Expression to be run against the selected Oracle dB
sqlQuery = arcpy.GetParameterAsText(3)
# Create spatial reference variable to assign to queryLayer
spatialRef = arcpy.SpatialReference("Vector\Coordinate Systems\WGS 1984.prj")
# Process: 'Make Query Layer' - Creates a Query Layer using the user's Oracle dB and SQL query expression
arcpy.MakeQueryLayer_management(oracleDB, "Temp_Layer", sqlQuery, "UNIQUE_ID", "POLYLINE", "1050010", spatialRef)
# Process: 'Copy Features' - Copies the temporary Query Layer and stores it as a permanent feature class
arcpy.CopyFeatures_management("Temp_Layer", queryLayer)
# Process: 'Define Projection' - Defines the projection of queryLayer feature class output
arcpy.DefineProjection_management(queryLayer, spatialRef)
# Process: 'Add Field' - Adds new column fields to queryLayer
arcpy.AddField_management(queryLayer, "First_Time", "DATE") # The first LOGDT ping
arcpy.AddField_management(queryLayer, "Last_Time", "DATE") # The last LOGDT ping
arcpy.AddField_management(queryLayer, "Total_Time", "FLOAT") # Summation of the first to last ping in time
arcpy.AddField_management(queryLayer, "Total_Pings", "INTEGER") # Total number of pings (rows)
arcpy.AddField_management(queryLayer, "Possible_Pings", "INTEGER") # Total number of pings possible in given timeframe
arcpy.AddField_management(queryLayer, "Time_to_Process", "DATE") # How long it took for each ping to process
# Calculates the total number of rows for newly added column Total_Pings
numRows = int(arcpy.GetCount_management(queryLayer).getOutput(0))
# UpdateCursor that will write the value of numRows to the Total_Pings column
cursor = arcpy.da.UpdateCursor(queryLayer, "Total_Pings")
for row in cursor:
row[0] = numRows
cursor.updateRow(row)
# SearchCursor that will read the values of LOGDT and return the first value (earliest)
firstLogdt = [row[0] for row in arcpy.da.SearchCursor(queryLayer, "LOGDT")][0]
# UpdateCursor that will write the first (earliest) LOGDT value to the field First_Time
cursor = arcpy.da.UpdateCursor(queryLayer, "First_Time")
for row in cursor:
row[0] = firstLogdt
cursor.updateRow(row)
# SearchCursor that will read the values of LOGDT and return the last value (latest)
lastLogdt = [row[0] for row in arcpy.da.SearchCursor(queryLayer, "LOGDT")][-1]
# UpdateCursor that will write the last (latest) LOGDT value to the field Last_Time
cursor = arcpy.da.UpdateCursor(queryLayer, "Last_Time")
for row in cursor:
row[0] = lastLogdt
cursor.updateRow(row)
# Calculates the difference between firstLogdt and lastLogdt
timeDiff = lastLogdt - firstLogdt # Produces a timedelta object, not datetime
# Calculates the total number of seconds from timeDiff
timeSecs = timeDiff.total_seconds()
# Creates a function that will convert timeDiff.total_seconds() to a readable format (dd:hh:mm:ss)
def readTime(seconds):
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
return '%02d:%02d:%02d:%02d' % (days, hours, minutes, seconds)
# UpdateCursor that will write the time difference calculation to the new Total_Time field
cursor = arcpy.da.UpdateCursor(queryLayer, "Total_Time")
for row in cursor:
row[0] = readTime(timeSecs)
cursor.updateRow(row)
# Calculates the total number of pings that would occur if no pings were dropped
possiblePings = timeSecs / 5 # 1 ping every 5 seconds (with good reception)
# UpdateCursor that will write the total number of pings possible to the new Possible_Pings field
cursor = arcpy.da.UpdateCursor(queryLayer, "Possible_Pings")
for row in cursor:
row[0] = possiblePings
cursor.updateRow(row)
... View more
06-22-2015
10:00 AM
|
0
|
11
|
3877
|
Title | Kudos | Posted |
---|---|---|
1 | 08-13-2014 07:08 AM |
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:24 AM
|