The workspace Work.gdb does not exist. Failed to execute (JSONToFeatures).

444
7
09-09-2021 11:57 AM
ErikGarcia1
New Contributor II

I am going over this ESRI training: https://learn.arcgis.com/en/projects/update-real-time-data-with-python/ , I get stuck on step 17 in the "Create stand-alone script" section. There is a similar post available online (https://community.esri.com/t5/python-questions/the-workspace-work-gdb-does-not-exist-failed-to/m-p/1... ), but I couldn't figure out an answer since I am fairly new to python scripting.

Any help would be appreciated, thank you.

Here is my code:

import sys, arcpy, os, tempfile, json
from urllib import request

def feedRoutine (url, workGDB):
    # workGDB and default workspace
    print("Creating workGDB...")
    arcpy.env.workspace =  workGDB
    arcpy.management.CreateFileGDB(os.path.dirname(workGDB) , os.path.basename(workGDB))
    
# Download and split json file
    print("Downloading data...")
    temp_dir = tempfile.mkdtemp()
    filename = os.path.join(temp_dir, 'latest_data.json')
    response = request.urlretrieve(url, filename)
    with open(filename) as json_file:
        data_raw = json.load(json_file)
        data_stations = dict(type=data_raw['type'], features=[])
        data_areas = dict(type=data_raw['type'], features=[])
    for feat in data_raw['features']:
        if feat['geometry']['type'] == 'Point':
            data_stations['features'].append(feat)
        else:
            data_areas['features'].append(feat)
    # Filenames of temp json files
    stations_json_path = os.path.join(temp_dir, 'points.json')
    areas_json_path = os.path.join(temp_dir, 'polygons.json')
    # Save dictionaries into json files
    with open(stations_json_path, 'w') as point_json_file:
        json.dump(data_stations, point_json_file, indent=4)
    with open(areas_json_path, 'w') as poly_json_file:
        json.dump(data_areas, poly_json_file, indent=4)
    # Convert json files to features
    print("Creating feature classes...")
    arcpy.conversion.JSONToFeatures(stations_json_path, 'alert_stations') 
    arcpy.conversion.JSONToFeatures(areas_json_path, 'alert_areas')
    # Add 'alert_level ' field
    arcpy.management.AddField('alert_stations', 'alert_level', 'SHORT', field_alias='Alert Level')
    arcpy.management.AddField('alert_areas', 'alert_level', 'SHORT', field_alias='Alert Level')
    # Calculate 'alert_level ' field
    arcpy.management.CalculateField('alert_stations', 'alert_level', "int(!alert!)")
    arcpy.management.CalculateField('alert_areas', 'alert_level', "int(!alert!)")

    # Deployment Logic
    print("Deploying...")
    deployLogic()

    # Return
    print("Done!")
    return True

def deployLogic():
    pass
    
if __name__ == "__main__":
        [url, workGDB] = sys.argv[1:]
        feedRoutine (url, workGDB)

 This is the error code:

(arcgispro-py3-user) C:\training>python coral_reef_exercise.py https://coralreefwatch.noaa.gov/product/vs/vs_polygons.json C:\Temp\Work.gdb
Creating workGDB...
Downloading data...
Creating feature classes...
Traceback (most recent call last):
  File "coral_reef_exercise.py", line 56, in <module>
    feedRoutine (url, workGDB)
  File "coral_reef_exercise.py", line 34, in feedRoutine
    arcpy.conversion.JSONToFeatures(stations_json_path, 'alert_stations')
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\conversion.py", line 576, in JSONToFeatures
    raise e
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\conversion.py", line 573, in JSONToFeatures
    retval = convertArcObjectToPythonObject(gp.JSONToFeatures_conversion(*gp_fixargs((in_json_file, out_features, geometry_type), True)))
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 999999: Something unexpected caused the tool to fail. Contact Esri Technical Support (http://esriurl.com/support) to Report a Bug, and refer to the error help for potential solutions or workarounds.
CreateFeatureClassName: The workspace  does not exist.
Failed to execute (JSONToFeatures).
GeoMaster in training
0 Kudos
7 Replies
DanPatterson
MVP Esteemed Contributor

If you just ran that script, it will compile then run lines 55 and 56, hence, it needs values for this

sys.argv[1:] is the url and the workGDB since sys.argv[0] is the actual script that is running.

 

[url, workGDB] = sys.argv[1:]
feedRoutine (url, workGDB)

 

Then it runs the feedRoutine function with the values provided.

So in short you need a url and the full path to a workspace geodatabase pasted before the second line (comment out the line 55) to  replacing those values before the feedRoutine is called


... sort of retired...
0 Kudos
ErikGarcia1
New Contributor II

Dan thanks for replying,

I am using this python command on the Python Command Prompt to include that information:

python coral_reef_exercise.py https://coralreefwatch.noaa.gov/product/vs/vs_polygons.json C:\Temp\Work.gdb

After using this my script runs fine but I run into that error.

GeoMaster in training
0 Kudos
DanPatterson
MVP Esteemed Contributor

I don't use that approach, preferring to just put the parameters on your line 55

if __name__ == "__main__":
        url, workGDB = "http:// ....", "c:/temp/your.gdb"
        feedRoutine(url, workGDB)

... sort of retired...
0 Kudos
ErikGarcia1
New Contributor II

Thanks again for helping out Dan! So I updated that part of my script and still got that Failed to execute (JSONToFeatures) error. Here is the updated script with those edits:

import sys, arcpy, os, tempfile, json
from urllib import request

def feedRoutine (url, workGDB):
    # workGDB and default workspace
    print("Creating workGDB...")
    arcpy.env.workspace =  workGDB
    arcpy.management.CreateFileGDB(os.path.dirname(workGDB) , os.path.basename(workGDB))
    
# Download and split json file
    print("Downloading data...")
    temp_dir = tempfile.mkdtemp()
    filename = os.path.join(temp_dir, 'latest_data.json')
    response = request.urlretrieve(url, filename)
    with open(filename) as json_file:
        data_raw = json.load(json_file)
        data_stations = dict(type=data_raw['type'], features=[])
        data_areas = dict(type=data_raw['type'], features=[])
    for feat in data_raw['features']:
        if feat['geometry']['type'] == 'Point':
            data_stations['features'].append(feat)
        else:
            data_areas['features'].append(feat)
    # Filenames of temp json files
    stations_json_path = os.path.join(temp_dir, 'points.json')
    areas_json_path = os.path.join(temp_dir, 'polygons.json')
    # Save dictionaries into json files
    with open(stations_json_path, 'w') as point_json_file:
        json.dump(data_stations, point_json_file, indent=4)
    with open(areas_json_path, 'w') as poly_json_file:
        json.dump(data_areas, poly_json_file, indent=4)
    # Convert json files to features
    print("Creating feature classes...")
    arcpy.conversion.JSONToFeatures(stations_json_path, 'alert_stations') 
    arcpy.conversion.JSONToFeatures(areas_json_path, 'alert_areas')
    # Add 'alert_level ' field
    arcpy.management.AddField('alert_stations', 'alert_level', 'SHORT', field_alias='Alert Level')
    arcpy.management.AddField('alert_areas', 'alert_level', 'SHORT', field_alias='Alert Level')
    # Calculate 'alert_level ' field
    arcpy.management.CalculateField('alert_stations', 'alert_level', "int(!alert!)")
    arcpy.management.CalculateField('alert_areas', 'alert_level', "int(!alert!)")

    # Deployment Logic
    print("Deploying...")
    deployLogic()

    # Return
    print("Done!")
    return True

def deployLogic():
    pass
    
if __name__ == "__main__":
        [url, workGDB] = "https://coralreefwatch.noaa.gov/product/vs/vs_polygons.json", "C:\Temp\work.gdb"
        feedRoutine (url, workGDB)

Here is the Error:

(arcgispro-py3-user) C:\training>python coral_reef_exercise.py
Creating workGDB...
Downloading data...
Creating feature classes...
Traceback (most recent call last):
  File "coral_reef_exercise.py", line 56, in <module>
    feedRoutine (url, workGDB)
  File "coral_reef_exercise.py", line 34, in feedRoutine
    arcpy.conversion.JSONToFeatures(stations_json_path, 'alert_stations')
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\conversion.py", line 576, in JSONToFeatures
    raise e
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\conversion.py", line 573, in JSONToFeatures
    retval = convertArcObjectToPythonObject(gp.JSONToFeatures_conversion(*gp_fixargs((in_json_file, out_features, geometry_type), True)))
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 999999: Something unexpected caused the tool to fail. Contact Esri Technical Support (http://esriurl.com/support) to Report a Bug, and refer to the error help for potential solutions or workarounds.
CreateFeatureClassName: The workspace  does not exist.
Failed to execute (JSONToFeatures).

 

GeoMaster in training
0 Kudos
DanPatterson
MVP Esteemed Contributor

"C:\Temp\work.gdb"

If that exists, then I don't know.  nothing pops out immediately but I avoid the web stuff.  Someone else should have leapt in by now, but if not, you might want to contact tech support


... sort of retired...
IhabHassan
Esri Contributor

Hi @ErikGarcia1 

I had to do one line modification to feedRoutine function:

def feedRoutine (urlworkGDB) :
    # workGDB and default workspace
    print("Creating workGDB...")
    arcpy.env.workspace =  workGDB
    arcpy.env.overwriteOutput = True #incase the gdb file is already created
    arcpy.management.CreateFileGDB(os.path.dirname(workGDB) , os.path.basename(workGDB))

that's the run log

Creating workGDB...
Downloading data...
Creating feature classes...
Deploying...
Done!

Hope that helps

Ihab 

Regards
Ihab
0 Kudos
ErikGarcia1
New Contributor II

Hello Ihab,

I tried your line modification, but I am still getting the same result. I am currently working with support for an answer and I will try to post it here if successful.

Thank you for the help,

Erik Garcia

Erik 

GeoMaster in training
0 Kudos