My small non-profit currently uploads feature services manually by zipping shapefiles and "overwriting" them. I'm trying to automate this process in Python and have only come across ago tools and ArcREST. ArcREST seems to be the more robust module but its usability seems predicated on access to an ArcGIS Server and we don't have that capacity at the moment. I see that there might be a way to create .sd files and upload them using a Python script but I am confused on how to do this. Can anyone point me in the right direction with a sample script and/or some instructions on how to do this? I have ArcMap 10.2
- 1,079
- 1
- 8
- 24
- 63
- 1
- 8
1 Answers
You don't need an ArcGIS Server license in order to work with ArcGIS Online hosted services. You can also create an .sd file having ArcGIS Desktop license only.
If your hosted feature service schema is not changing, you probably just want to truncate the feature layer and populate it with the features from your shapefile.
securityinfo = {}
securityinfo['security_type'] = 'Portal'
securityinfo['username'] = agol_admin_username
securityinfo['password'] = agol_admin_password
securityinfo['org_url'] = agol_org_url
securityinfo['proxy_url'] = None
securityinfo['proxy_port'] = None
securityinfo['referer_url'] = None
securityinfo['token_url'] = None
securityinfo['certificatefile'] = None
securityinfo['keyfile'] = None
securityinfo['client_id'] = None
securityinfo['secret_id'] = None
from arcresthelper import featureservicetools
fc = r"C:\GIS\Temp\Wells.shp"
#webmapid = 'long string'
fst = featureservicetools.featureservicetools(securityinfo)
fs = fst.GetFeatureService(itemId=webmapid,returnURLOnly=False)
fl = fst.GetLayerFromFeatureService(fs=fs,layerName='Wells',returnURLOnly=False)
print fl.query(returnCountOnly=True)
#delete all features (truncate)
fst.DeleteFeaturesFromFeatureLayer(fl.url, '1=1')
fl.addFeatures(fc=fc,attachmentTable=None)
print fl.query(returnCountOnly=True)
fl.deleteFeatures(objectIds='310078')
print fl.query(returnCountOnly=True)
If your shapefile might be different from the published hosted feature service in terms of fields definition, you might either edit the hosted feature service to reflect the changes (with this sample) or by publishing .sd file which you can create in ArcMap from a shapefile (using arcpy and this sample and then using this ArcREST sample).
Using arcpy.mapping.CreateMapSDDraft, it is possible to create a .sddraft without having a connection to ArcGIS Server. Specify the server_type parameter to be MY_HOSTED_SERVICES. Then use arcpy.StageService_server which will convert the draft to the .sd file.
You can then use arcpy.UploadServiceDefinition_server to publish this .sd file on your ArcGIS Online organization.
- 29,912
- 4
- 54
- 119
arcpyand save it then before converting further to.sd. Look here for a sample: http://gis.stackexchange.com/questions/4882/adding-shapefile-or-feature-class-as-layer-in-arcgis-desktop-using-python-arcpy – Alex Tereshenkov Aug 18 '16 at 06:49