Select to view content in your preferred language

Overwriting Hosted Feature Service from Postgres Feature Class

617
3
Jump to solution
04-21-2023 06:44 AM
Chris_Kell
New Contributor III

Hi,

Using the ArcGIS API for Python I'm trying to create a script that overwrites, or re-publishes, the contents of a hosted feature service.

Using ArcGIS Pro 3.1.1, Enterprise 11.0, and PostgreSQL 13.3

Here's my current script:

from arcgis.gis import GIS
from arcgis.features import FeatureLayer
from arcgis.features import FeatureLayerCollectiongis = GIS("https://ourservername.abc.def/portal", username="username", password="password")
postgres_fc = r"path to postgresql.sde\featureClass" 

hosted_fs = gis.content.get('feature service id')
hosted_flc = FeatureLayerCollection.fromitem(hosted_fs)
hosted_flc.manager.overwrite(postgres_fc)

 

For the postgres_fc variable I've tried both a simple, and a fully qualified name (dbname.sde.featureName).

When executed, the script returns "success,", and the timestamp on the hosted feature service is updated,. However the data is unaffected. 

Am I missing a step?  Is the script completely off the mark?

Any help will be greatly apreciated!

 

Thanks

-Chris

 

 

 

 

 

0 Kudos
2 Solutions

Accepted Solutions
DavidColey
Frequent Contributor

I would say that if you look at this:

https://developers.arcgis.com/python/api-reference/arcgis.features.managers.html#arcgis.features.man...

In part, it reads "The data file used to overwrite should be of the same format and filename as the original that was used to publish the layer". 

So basically you need to create service definition file from your sde layer first. The feature layer is then created from that sd file, and when the sd file is overwritten, both the sd file and feature layer are updated.  

I prefer to use the sharing module in Pro to create the staging definition (sddraft) and service definition files (sd). Then, instead of jumping over to the api to do the overwrite, just continue to use sharing module in pro.

However, it might look something like this

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

gis = GIS("https://yourdomain/yourportal", "username_portal", "user_pw")

try:
portItems = gis.content.search(query="owner:username_portal", item_type="Feature Layer", sort_field="title", sort_order="asc", max_items=5000) #Service Definition Feature Layer
for portItem in portItems:
if (portItem.title == "YourFeatureLayer"):
portFL = FeatureLayerCollection.fromitem(portItem)
print (portItem.title)
portFL.manager.overwrite('C:/ArcProProjects3x/YourSDFile.sd')
#portFL.manager.refresh()
print(portFL.properties)
message = message + "\n" + (portItem.title)

except Exception:
# If an error occurred, print line number and error message
import traceback, sys
tb = sys.exc_info()[2]
e = sys.exc_info()

View solution in original post

DavidColey
Frequent Contributor

Well, for me all I do is make sure my directory holding my .py files is shared and then I just have task scheduler run python.exe as the Action:

C:\Users\yourname\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\python.exe

and for the argument supply the python file using the UNC path

\\MyComputerName\BatchDBTasksAE\SqlUnScripts\BatchSqlUN_SewerStructJunction.py.

The python snippet above is configured for the IDLE, not Notebook, so you can just change the GIS param to match your portal and change the sd file location to your directory location and it will run no problem in the IDLE.

When the above works for you, please mark it as correct thanks!

View solution in original post

3 Replies
DavidColey
Frequent Contributor

I would say that if you look at this:

https://developers.arcgis.com/python/api-reference/arcgis.features.managers.html#arcgis.features.man...

In part, it reads "The data file used to overwrite should be of the same format and filename as the original that was used to publish the layer". 

So basically you need to create service definition file from your sde layer first. The feature layer is then created from that sd file, and when the sd file is overwritten, both the sd file and feature layer are updated.  

I prefer to use the sharing module in Pro to create the staging definition (sddraft) and service definition files (sd). Then, instead of jumping over to the api to do the overwrite, just continue to use sharing module in pro.

However, it might look something like this

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

gis = GIS("https://yourdomain/yourportal", "username_portal", "user_pw")

try:
portItems = gis.content.search(query="owner:username_portal", item_type="Feature Layer", sort_field="title", sort_order="asc", max_items=5000) #Service Definition Feature Layer
for portItem in portItems:
if (portItem.title == "YourFeatureLayer"):
portFL = FeatureLayerCollection.fromitem(portItem)
print (portItem.title)
portFL.manager.overwrite('C:/ArcProProjects3x/YourSDFile.sd')
#portFL.manager.refresh()
print(portFL.properties)
message = message + "\n" + (portItem.title)

except Exception:
# If an error occurred, print line number and error message
import traceback, sys
tb = sys.exc_info()[2]
e = sys.exc_info()

Chris_Kell
New Contributor III

Hi David,

 

Thanks for the super fast reply!

 

I apologize I left out a key requirement.  I'm trying to create this as a Windows scheduled task.

 

-Chris

0 Kudos
DavidColey
Frequent Contributor

Well, for me all I do is make sure my directory holding my .py files is shared and then I just have task scheduler run python.exe as the Action:

C:\Users\yourname\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\python.exe

and for the argument supply the python file using the UNC path

\\MyComputerName\BatchDBTasksAE\SqlUnScripts\BatchSqlUN_SewerStructJunction.py.

The python snippet above is configured for the IDLE, not Notebook, so you can just change the GIS param to match your portal and change the sd file location to your directory location and it will run no problem in the IDLE.

When the above works for you, please mark it as correct thanks!