6

I made a custom add-in for ArcMap (10.2 in my case) and was reviewing my code with a colleague. We're not sure if the setting the workspace is required in the code since the button's script is only going to be run in ArcMap where all the connections and features exist. It certainly works with the workspace set, but is it required to include it? What would be the best practice?

Code sample:

   ...
   def onClick(self):
    # set workspace to SDE connection
    workspace = r"Database Connections\facilities@5160_93.sde"

    # set the fields to the ones being flipped
    fields = (["FROMLEFTP", "TORIGHTP", "TOLEFTP", "FROMRIGHTP",
               "FROMLEFTA", "TORIGHTA", "TOLEFTA", "FROMRIGHTA",])
   ...
3

Is it necessary? No. However, if you're asking about best practice, I would venture to say you should set your workspace within the code of all your add-ins. If anything, this will prompt you and others who may use your add-in to set the workspace as opposed to allowing previous settings (possibly not default) to take over.

Note:

When a script is run inside a tool from an ArcGIS application or from another geoprocessing script, the environment settings used by the calling application or script are passed to it. These settings become the default settings used by the tool's script when it is executed. The called script may alter the settings passed to it, but those changes are only used within that script or by any other tool it may call. Changes are not passed back to the calling script or application. The environment model can best be described as cascading, where values flow down to any process that uses the geoprocessing environment.

More info on workspace environments can be found here.

  • 2
    Another important thing to note that the script will execute correctly only if there is a connection file with this name under Database Connections of the ArcMap that the user runs. If your add-in might be used by other users and you don't want to force them to create a database connection, use the file path instead to a shared place (\\server\sdecons\file.sde). I never use "Database Connections" in my script tools or add-ins. – Alex Tereshenkov Dec 18 '13 at 17:16
  • Thanks @Radar! I suppose I should reword the question a bit to clarify the difference between best practice and required. – Elliott Dec 18 '13 at 19:11
  • @AlexTereshenkov great point! I will implement that. So a shared drive works well in your environment? – Elliott Dec 19 '13 at 4:03
  • For script tools - absolutely. Just make sure they are there and safe (i.e., users with no proper permissions cannot delete it or modify). I do publish occasionally GP services based on Python scripts, and then the .sde connection files are copied to the server folder to make sure they are accessible, but that's something else. – Alex Tereshenkov Dec 19 '13 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.