Arcpy Web Service – Load a Layer from Enterprise Geodatabase or Folder in Python Script for Web Tool Publishing

arcpygeoprocessing-servicesdelayerweb service

I have a script that loads a vector layer from an enterprise geodatabase (PostgreSQL database).

arcpy.MakeFeatureLayer_management(r"M:\SDE\[email protected]/ven.x.y","vlyr")

I have also tried

arcpy.MakeFeatureLayer_management(r"M:\SDE\[email protected]/SDE.ven.x.y","vlyr")

as suggested in How does one access a featurelayer in SDE via Python?

It works on Desktop. The published tool returns the error:

File "\…\hosting-root-directories\arcgissystem\arcgisinput\toolName.GPServer\extracted\p20\services_test\skriptName.py", line 5
g_ESRI_variable_1 = os.path.join(arcpy.env.packageWorkspace,'\\…\hosting-root-directories\arcgissystem\arcgisinput\toolName.GPServer\extracted\p20\[email protected]\SDE.ven.x.y"'')
^
SyntaxError: EOL while scanning string literal

I have registered the database in the Manage Registered Datastores via add -> database -> import. The connection link is

SERVER=serverName;INSTANCE=sde:postgresql:serverName;DBCLIENT=postgresql;DB_CONNECTION_PROPERTIES=serverName;DATABASE=DBname;USER=userName;AUTHENTICATION_MODE=DBMS

It is the same for publisher and server. I can uncheck "same as publisher", but then I cannot enter anything, as I did for registering the folders (C:).

How do I need to address the file in an EGDB so that it is recognized as being in the registered database?
Or do I maybe have to run "Enable Enterprise Geodatabase" (right click on database in catalog pane)? It says that the authorization file is on "\Program Files\ESRI\License<release#>\sysgen folder on Windows". I cannot find this path, does it refer to the server. In the server manager in the browser I could also not find it. Where do I access the sript on the server? I read in some posts here that people did that and just fixed the paths there. Do I have to ask an admin to do it or could I do it as publisher?

When I hover over the database in catalog, it shows up "Type: Enterprise Connection".

When I replace the path to the EGDB with a path to a shapefile, (the folder where the shapefile is in being registered as a datastore), the published tool returns the error:

File "\…\hosting-root-directories\arcgissystem\arcgisinput\toolName.GPServer\extracted\p20\folderName\script.py", line 7
g_ESRI_variable_3 = 'C:\shapefileName.shp'00.shp'
^
SyntaxError: invalid syntax

Best Answer

First, and most importantly, do not use Make Feature Layer in a GP Service. There are times when it may make sense, but based on your code snippet, you're just turning a feature class into a layer.

arcpy.MakeFeatureLayer_management(r"M:\SDE\[email protected]/ven.kacheln.dtk100_k40_utm32s","vlyr")

This is an expensive operation. In fact, if it's not the biggest, it's very close to the biggest performance mistake people make in their GP Service. The only time it may make sense to use it would be if you're attaching a where-clause. Even in that situation there would be a better way to achieve your result. See the performance help tip on layers. Since I'm saying don't Make layers, then how do you use a layer? Your workflow starts with a layer. As you're writing a Python script, you just name match the layer in the Contents panel to the variable in your script.

For example, if you have a layer in your Contents panel named vlyr, inside your script you would have:

myLayerVar = "vlyr"
arcpy.Buffer_management(myLayerVar, "10 Kilometers" ....)

There is a screen shot in the Referencing layers as project data section of this help topic that shows this name match.

Obviously the script tool is not portable: it's tightly tied to the Project/Map with that layer inside. (that's fine. You're making a tool designed to be turned into a GP Service) However, when you publish the GP Service, the process will start up the layer and have it ready within the service. No need to Make the layer every time. This blog I wrote a few years is not focused on using layers, but shows it in practice.

If you've made it this far and you've re-designed your script, now to sort out the publishing problem. My two suggestions when publishing:

  1. Make sure the reference data option is selected in the Publishing Tool dialog. (NOT the copy data)
  2. Before publishing / analyzing, clear your data store entry. When you analyze, it should flag and say "this data set will be copied". Use the analyzer to help create your data store entry. Re-analyze and the copy data warning should go away. Publish your tool and no longer worry about .SDE files.
Related Question