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

411
0
07-08-2021 11:25 PM
LindsayRaabe_FPCWA
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 (fs.save (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 fs.save() 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 = "https://services3.arcgis.com/###############/arcgis/rest/services/##################/FeatureServer/1" # 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
   try:
      import arcpy
      from arcpy import env
      from arcgis import gis #Instead of signing in as follows (gis = GIS("https://#####.maps.arcgis.com", "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       # http://docs.python-requests.org/en/latest/user/advanced/
      s = requests.Session()
      url_gis="https://#####.maps.arcgis.com"
      s.user = (secrets["username"])
      s.pw = (secrets["password"])

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

      # CREATING BACKUP OF FEATURE SERVICE      # https://community.esri.com/t5/python-questions/using-arcpy-to-copy-a-portal-feature-service-to-a-fgdb-feature/m-p/4285#M394
      fl = FeatureLayer(url_fl)
      fs = fl.query()
      print ("Exporting backup of feature service")
      fs.save(destGDB, 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