Maintain ArcOnline Feature Service Geometry Type when 0 rows exported via python

07-08-2021 11:25 PM
Occasional Contributor III

Hi Brains Trust. I've got a "problem" that I haven't been able to figure out through the googs, but it's also not business critical and I can ignore it, but the desire to know more is spurring me on. 

The below script is part of a larger code which downloads a feature service from ArcOnline (Point, Polyline or Polygon) and saves a copy as a feature class in a GDB on our server. 

The "issue" I've found is that where it has ( (destGDB, destFC...) on Line 40, if the feature service has 0 rows, it changes the feature class to a table with 0 rows. It would be nice if it preserved the Geometry type (i.e. still shows as a point/line/polygon) despite having no data. Any ideas how I could make this happen?

I did try swapping the for arcpy.conversion.FeatureClassToFeatureClass() but that returned an Object error (I assume because the input wasn't a feature class but was a feature layer [fs]). I did manage to figure out how to determine the Geometry type of the feature service using desc = arcpy.Describe(fs) and print (desc.shapeType) but not pass it to the export step. 


name = "Dataset Name" #Name of dataset for use in error email notification
url_fl = "" # Service URL for feature layer to download as feature class
destGDB = r"C:\temp\download.gdb" #The GDB where the backup feature class will be created
destFC = "DailyBackup" #The backup feature class name (no spaces - user _ or CamelCase)

while True:  #If something fails in the main script under "try", the "except" section emails a notification to the GIS Inbox
      import arcpy
      from arcpy import env
      from arcgis import gis #Instead of signing in as follows (gis = GIS("", "Username", "Password") this breaks it up so that you are able to call the sign in details independently
      from arcgis.gis import GIS
      from arcgis.features import FeatureLayer
      import getpass
      import json
      import requests
      from time import strftime
      def getSecrets():
          # Secrets from file stored outside of revison control
          with open(r".\secrets.json") as f:
              secrets = json.load(f)
          return secrets
      secrets = getSecrets()

      # Get login credentials       #
      s = requests.Session()
      s.user = (secrets["username"]) = (secrets["password"])

      print ("Signing into ArcGIS Online")
      source = gis.GIS(url_gis, s.user, #signing in
      print ("Signed into ArcGIS Online")

      fl = FeatureLayer(url_fl)
      fs = fl.query()
      print ("Exporting backup of feature service"), destFC + "_" + strftime("%Y%m%d_%H%M%S"))
      time.sleep(10) #add 10 seconds delay to allow export to complete
      print (name + " feature service exported to backup GDB: " + destGDB + "\\" + destFC + strftime("_%Y%m%d_%H%M%S"))
      break # Stops script here
   except Exception as e:
      #code here sends email if script fails
      break # Stops script here



Lindsay Raabe
GIS Officer
Forest Products Commission WA
0 Kudos
0 Replies