Notebook failing randomly... or not?

176
0
03-07-2024 04:37 PM
Labels (1)
RogerAsbury
Occasional Contributor

I have a notebook, the purpose of which is to calculate the intersections of several feature layers with another layer, and then store the results in tables particular to each layer. I've attached the Notebook to this post. What's odd is that for the most part, it works. But what seemed to be randomly (until lately when it fails all the time) it would fail with the error: 

Exception: Item 'ZonePercentages.csv' already exists.
(Error Code: 409)

This is not an error it used to throw. The Notebook is written to delete all the files it creates when it is done, so no files should exist when it starts. 


My assumption ... my assumption when ever anything breaks ... is that I have a typo somewhere. However, two of us here have looked at this and can't figure out why it is doing this. And like I said, it just used to work. 

I've tested clearing the directory before starting the script, and it still throws that error. I was wondering if anyone can spot any glaring errors.  

Notebook starts here (forum software complained when I tried to attach it below as either .py or .txt):

#!/usr/bin/env python
# coding: utf-8

# ## Welcome to your notebook.
#

# #### Run this cell to connect to your GIS and get started:

# In[20]:


from arcgis.gis import GIS
import arcgis
import arcpy
import os
import shutil

gis = GIS("home")


# In[21]:


def delete():
 for f in os.listdir(directory):
  if ".snapshot" not in f and ".tasks" not in f:
    if os.path.isfile(os.path.join(directory, f)):
      os.remove(os.path.join(directory, f))
    elif os.path.isdir(os.path.join(directory, f)):
      shutil.rmtree(os.path.join(directory, f))


# #### Now you are ready to start!

# In[22]:


directory = "/arcgis/home/"
parcels_shp = os.path.join(directory, "parcels")
zones_shp = os.path.join(directory, "zones")
fsa_shp = os.path.join(directory, "fsa")
sra_shp = os.path.join(directory, "sra")
fz_shp = os.path.join(directory, "floodzones")
cp_shp = os.path.join(directory, "compPlan")
pd_shp = os.path.join(directory, "planDist")
ub_shp = os.path.join(directory, "urbanBoundary")
rd_shp = os.path.join(directory, "roadDist")
rsa_shp = os.path.join(directory, "rsa")
zone_csv = os.path.join(directory, "ZonePercentages.csv")
fsa_csv = os.path.join(directory, "fsa.csv")
sra_csv = os.path.join(directory, "sra.csv")
fz_csv = os.path.join(directory, "FloodZonePercentages.csv")
cp_csv = os.path.join(directory, "ComPlanPercentages.csv")
pd_csv = os.path.join(directory, "planningDistrictPercentages.csv")
ub_csv = os.path.join(directory, "urbanBoundaryPercentages.csv")
rd_csv = os.path.join(directory, "roadDistrictPercentages.csv")
rsa_csv = os.path.join(directory, "roadServiceAreaPercentages.csv")
delete()


# In[23]:


parcels = arcgis.features.FeatureLayer("https://gisportal.fnsb.gov/referenced/rest/services/_/Parcels_with_TaxInfo/FeatureServer/1").query().sdf[["PAN", "SHAPE"]]
parcels.drop(parcels[parcels["PAN"] == 9999999].index, inplace=True)
parcels.drop(parcels[parcels["PAN"] == 999999].index, inplace=True)
zones = arcgis.features.FeatureLayer("https://gisportal.fnsb.gov/referenced/rest/services/_/Zoning_Districts/FeatureServer/1").query().sdf[["ZONE", "ZONE_DESC", "SHAPE"]]
fsa = arcgis.features.FeatureLayer("https://gisportal.fnsb.gov/referenced/rest/services/_/Fire_Service_Areas/FeatureServer/1").query().sdf[["NAME", "SHAPE"]]
sra = arcgis.features.FeatureLayer("https://services.arcgis.com/f4rR7WnIfGBdVYFd/ArcGIS/rest/services/Special_Regulatory_Areas/FeatureSe...").query().sdf[["TYPE", "NOTES", "SHAPE"]]
floodZones = arcgis.features.FeatureLayer("https://services.arcgis.com/f4rR7WnIfGBdVYFd/ArcGIS/rest/services/National_Flood_Hazards_Layer_-_Sta...").query(where = "DFIRM_ID='02090C'").sdf[["FLD_ZONE", "ZONE_SUBTY", "SHAPE"]]
compPlans = arcgis.features.FeatureLayer("https://services.arcgis.com/f4rR7WnIfGBdVYFd/ArcGIS/rest/services/Comprehensive_Plan/FeatureServer/1...").query().sdf[["NAME", "SHAPE"]]
planDist = arcgis.features.FeatureLayer("https://services.arcgis.com/f4rR7WnIfGBdVYFd/ArcGIS/rest/services/Planning_Districts/FeatureServer/2").query().sdf[["NAME", "SHAPE"]]
urbanBound = arcgis.features.FeatureLayer("https://services.arcgis.com/f4rR7WnIfGBdVYFd/ArcGIS/rest/services/Census_2020_Urban_Boundary/Feature...").query().sdf[["NAME20", "SHAPE"]]
roadDist = arcgis.features.FeatureLayer("https://services.arcgis.com/f4rR7WnIfGBdVYFd/ArcGIS/rest/services/Road_Districts/FeatureServer/3").query().sdf[["NAME", "DIST_NUM", "SHAPE"]]
rsa = arcgis.features.FeatureLayer("https://gisportal.fnsb.gov/referenced/rest/services/_/Road_Service_Areas/FeatureServer/1").query().sdf[["NAME", "SHAPE"]]


# In[24]:


parcels = parcels.spatial.to_featureclass(parcels_shp)
zones = zones.spatial.to_featureclass(zones_shp)
fsa = fsa.spatial.to_featureclass(fsa_shp)
sra = sra.spatial.to_featureclass(sra_shp)
floodZones = floodZones.spatial.to_featureclass(fz_shp)
compPlans = compPlans.spatial.to_featureclass(cp_shp)
planDist = planDist.spatial.to_featureclass(pd_shp)
urbanBound = urbanBound.spatial.to_featureclass(ub_shp)
roadDist = roadDist.spatial.to_featureclass(rd_shp)
rsa = rsa.spatial.to_featureclass(rsa_shp)


# In[25]:


zone_percentages = arcpy.analysis.TabulateIntersection(parcels, "PAN", zones, zone_csv, ["ZONE", "ZONE_DESC"], out_units="ACRES")
fsa_percentages = arcpy.analysis.TabulateIntersection(parcels, "PAN", fsa, fsa_csv, ["NAME"], out_units="ACRES")
sra_percentages = arcpy.analysis.TabulateIntersection(parcels, "PAN", sra, sra_csv, ["AREATYPE", "NOTES"], out_units="ACRES")
floodZone_percentages = arcpy.analysis.TabulateIntersection(parcels, "PAN", floodZones, fz_csv, ["FLD_ZONE", "ZONE_SUBTY"], out_units="ACRES")
compPlan_percentages = arcpy.analysis.TabulateIntersection(parcels, "PAN", compPlans, cp_csv, ["NAME"], out_units = "ACRES")
planDist_percentages = arcpy.analysis.TabulateIntersection(parcels, "PAN", planDist, pd_csv, ["NAME"], out_units = "ACRES")
urbanBound_percentages = arcpy.analysis.TabulateIntersection(parcels, "PAN", urbanBound, ub_csv, ["NAME20"], out_units = "ACRES")
roadDist_percentages = arcpy.analysis.TabulateIntersection(parcels, "PAN", roadDist, rd_csv, ["NAME", "DIST_NUM"], out_units = "ACRES")
rsa_percentages = arcpy.analysis.TabulateIntersection(parcels, "PAN", rsa, rsa_csv, ["NAME"], out_units="ACRES")


# In[26]:


item_properties = {"title": "temp"}
folder = "Parcel Overlap Calculations"
zone_percentages_item = gis.content.add(item_properties, zone_csv, folder=folder)
fsa_percentages_item = gis.content.add(item_properties, fsa_csv, folder=folder)
sra_percentages_item = gis.content.add(item_properties, sra_csv, folder=folder)
floodZone_percentages_item = gis.content.add(item_properties, fz_csv, folder=folder)
compPlan_percentage_item = gis.content.add(item_properties, cp_csv, folder=folder)
planDist_percentage_item = gis.content.add(item_properties, pd_csv, folder=folder)
urbanBound_percentage_item = gis.content.add(item_properties, ub_csv, folder=folder)
roadDist_percentage_item = gis.content.add(item_properties, rd_csv, folder=folder)
rsa_percentage_item = gis.content.add(item_properties, rsa_csv, folder=folder)


# In[27]:


zones_fl = arcgis.features.FeatureLayer("https://services.arcgis.com/f4rR7WnIfGBdVYFd/arcgis/rest/services/Zone_Percentages/FeatureServer/0")
fsa_fl = arcgis.features.FeatureLayer("https://services.arcgis.com/f4rR7WnIfGBdVYFd/ArcGIS/rest/services/FireServiceAreas/FeatureServer/0")
sra_fl = arcgis.features.FeatureLayer("https://services.arcgis.com/f4rR7WnIfGBdVYFd/arcgis/rest/services/SpecialRegAreas/FeatureServer/0")
fz_fl = arcgis.features.FeatureLayer("https://services.arcgis.com/f4rR7WnIfGBdVYFd/ArcGIS/rest/services/FloodZonePercentages/FeatureServer...")
cp_fl = arcgis.features.FeatureLayer("https://services.arcgis.com/f4rR7WnIfGBdVYFd/ArcGIS/rest/services/ComPlanPercentages/FeatureServer/0")
pd_fl = arcgis.features.FeatureLayer("https://services.arcgis.com/f4rR7WnIfGBdVYFd/arcgis/rest/services/PlanningDistrictPercentages/Featur...")
ub_fl = arcgis.features.FeatureLayer("https://services.arcgis.com/f4rR7WnIfGBdVYFd/arcgis/rest/services/UrbanBoundaryPercentages/FeatureSe...")
rd_fl = arcgis.features.FeatureLayer("https://services.arcgis.com/f4rR7WnIfGBdVYFd/ArcGIS/rest/services/RoadDistricPercentages/FeatureServ...")
rsa_fl = arcgis.features.FeatureLayer("https://services.arcgis.com/f4rR7WnIfGBdVYFd/ArcGIS/rest/services/RoadServiceAreaPercentages/Feature...")
zones_fl.manager.truncate()
fsa_fl.manager.truncate()
sra_fl.manager.truncate()
fz_fl.manager.truncate()
cp_fl.manager.truncate()
pd_fl.manager.truncate()
ub_fl.manager.truncate()
rd_fl.manager.truncate()
rsa_fl.manager.truncate()


# In[36]:


zones_fl.append(item_id = zone_percentages_item.itemid, upload_format = "csv", source_table_name = "ZonePercentages", source_info = gis.content.analyze(item = zone_percentages_item.itemid))


# In[37]:


fsa_fl.append(item_id = fsa_percentages_item.itemid, upload_format="csv", source_table_name="FireServiceAreaPercentages", source_info = gis.content.analyze(item = fsa_percentages_item.itemid))


# In[38]:


sra_fl.append(item_id = sra_percentages_item.itemid, upload_format = "csv", source_table_name = "SpecialRegAreas", source_info = gis.content.analyze(item = sra_percentages_item.itemid))


# In[39]:


fz_fl.append(item_id = floodZone_percentages_item.itemid, upload_format = "csv", source_table_name = "FloodZonePercentages", source_info = gis.content.analyze(item = floodZone_percentages_item.itemid))


# In[40]:


cp_fl.append(item_id = compPlan_percentage_item.itemid, upload_format = "csv", source_table_name = "ComPlanPercentages", source_info = gis.content.analyze(item = compPlan_percentage_item.itemid))


# In[41]:


pd_fl.append(item_id = planDist_percentage_item.itemid, upload_format = "csv", source_table_name = "PlanningDistrictPercentages", source_info = gis.content.analyze(item = planDist_percentage_item.itemid))


# In[42]:


ub_fl.append(item_id = urbanBound_percentage_item.itemid, upload_format = "csv", source_table_name = "UrbanBoundaryPercentages", source_info = gis.content.analyze(item = urbanBound_percentage_item.itemid))


# In[43]:


rd_fl.append(item_id = roadDist_percentage_item.itemid, upload_format = "csv", source_table_name = "RoadDistrictPercentages", source_info = gis.content.analyze(item = roadDist_percentage_item.itemid))


# In[44]:


rsa_fl.append(item_id = rsa_percentage_item.itemid, upload_format = "csv", source_table_name = "RoadServiceAreaPercentages", source_info = gis.content.analyze(item = rsa_percentage_item.itemid))


# In[45]:


csv_items = [
zone_percentages_item
, fsa_percentages_item
, sra_percentages_item
, floodZone_percentages_item
, compPlan_percentage_item
, planDist_percentage_item
, urbanBound_percentage_item
, roadDist_percentage_item
, rsa_percentage_item
]

for i in csv_items:
print(i)
try:
i.delete()
except Exception as e:
print(e)


# In[46]:


delete()


# In[ ]:

 

 

--
Roger Asbury
Analyst/Programmer - Fairbanks North Star Borough
Tags (3)
0 Kudos
0 Replies