3

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 Answer 1

4

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.

7
  • this is a great resource and I'm leaning towards assuming that schema changes will be part of the updating of feature layers. I'd follow the 2nd sample however the latter sample links you posted specifically state "A connection to ArcGIS Server must be established in the catalog window of ArcMap before running this script" which is what I'm trying to avoid if I have no ArcGis Server set up. Commented Aug 16, 2016 at 20:03
  • @SteeleJohnson, I've updated my answer with additional details, hope this will make it clearer for you. Commented Aug 16, 2016 at 21:09
  • Hi @Alex is there a way to convert from shapefile to SDDraft instead of from MapDocument to SDDraft? I haven't seen anything quite that does this. Commented Aug 17, 2016 at 20:03
  • @SteeleJohnson, unfortunately, the feature service is based on the map document which means if you want to automate the whole workflow, you would need to add a shapefile to a map document with arcpy and save it then before converting further to .sd. Look here for a sample: gis.stackexchange.com/questions/4882/… Commented Aug 18, 2016 at 6:49
  • I see. The only issue is I wouldn't want to have an mxd for each shapefile I want to publish separately. It'd be easier to figure out a way to publish shapefiles from a .gdb or even just stored in a specific folder. Commented Aug 18, 2016 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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