Hello, I'm getting "ERROR 000354: The name contains invalid characters" at line 59 of the attached python script, at the ConnectPublicTransitDataModelToStreets call:
# Imports
import arcpy
import os
import pandas as pd
from arcgis.features import GeoAccessor, GeoSeriesAccessor
# Paths
proj_dir = r"C:\Users\josephda\GitHub\osm"
streets_dir = proj_dir + r'\data\raw\Street_Centerlines\Street_Centerlines.shp'
gtfs_dir = proj_dir + r'\data\raw\gtfs'
aprx_dir = proj_dir + r'\arcgis\arcgis.aprx'
gdb_dir = proj_dir + r'\arcgis\arcgis.gdb'
nw_dir = proj_dir + r'\arcgis\arcgis.gdb\TransitNetwork'
aprx_dir = proj_dir + r'\arcgis\arcgis.aprx'
# Environment settings
arcpy.env.workspace = gdb_dir
# Set output coordinate system to NAD83 CA Albers
arcpy.env.outputCoordinateSystem = arcpy.SpatialReference(3310)
# Declare the project file
aprx = arcpy.mp.ArcGISProject(aprx_dir)
# List maps in the project
for m in aprx.listMaps():
print(m.name)
# List layers in the map
for lyr in m.listLayers():
print(lyr.name)
# Remove any existing layers except the basemap
if lyr.isBasemapLayer == False:
m.removeLayer(lyr)
aprx.save()
# Load the streets shapefile as a spatially enabled dataframe
streets_sed = pd.DataFrame.spatial.from_featureclass(streets_dir)
streets_fc = streets_sed.spatial.to_featureclass(location=gdb_dir + r'\streets_fc')
# Add restrict pedestrian field
arcpy.AddField_management("streets_fc", "RestrictPedestrians", "TEXT")
arcpy.CalculateField_management("streets_fc", "RestrictPedestrians", "'Y' if !circulatio! == 'Freeway' or !circulatio! == 'State Route' or !road_type! == 'Railway' else 'N'", "PYTHON")
arcpy.AddField_management("streets_fc", "ROAD_CLASS", "SHORT")
# Create the feature dataset
arcpy.CreateFeatureDataset_management(gdb_dir, "TransitNetwork", arcpy.SpatialReference(3310))
# Covert GTFS to transit data model
arcpy.transit.GTFSToPublicTransitDataModel(gtfs_dir, nw_dir, "NO_INTERPOLATE", "NO_APPEND")
# Copy streets to TransitNetwork feature dataset as "Streets"
arcpy.CopyFeatures_management("streets_fc", nw_dir + r'\Streets')
# Delete initial copy of streets
arcpy.Delete_management("streets_fc")
# Connect the transit data model to streets
arcpy.transit.ConnectPublicTransitDataModelToStreets(nw_dir, "Streets", "100 Meters", "RestrictPedestrians <> 'N'")
Here's an image of the error:
I can't figure out what exactly is causing the error. I don't see any issues with the filename "Streets" and the path to the network dataset it resides in works in subsequent lines.
I can run the ConnectPublicTransitDataModelToStreets tool in the ArcGIS Pro GUI and it runs fine. However if I copy the python command as is directly into my script and run it, it fails with the same error message.
Any suggestions for how address this would be appreciated.
Solved! Go to Solution.
One further thought (after looking at the tool code): Try printing the value of arcpy.env.scratchGDB in your code and see what it points to. Delete it, and try running the script again. I've seen cases where the scratch gdb gets corrupted, and then weird things happen.
Possibly it works when you run the tool in the Pro UI because it's pointing to a different scratch gdb.
Hmm, I'm not sure why you're getting that error based on what I see in the code. However, the one thing that jumps out at me is the way you're constructing filepaths in the Python script. It's safer to use os.path.join() to construct paths from multiple components rather than the + operator. It doesn't seem like the way you have them should cause an error, but it's not really the best coding practice.
Try updating like this, and see if that fixes it:
streets_dir = os.path.join(proj_dir, '\data\raw\Street_Centerlines\Street_Centerlines.shp')
Got it, appreciate the suggestion. I'm shifting from primarily using R so am still familiarizing myself with best practices in Python.
I've updated the code using the os.path.join() method, which required me to concatenate each level to build the full path. See in the code below:
# Imports
import arcpy
import os
import pandas as pd
from arcgis.features import GeoAccessor, GeoSeriesAccessor
# Paths
proj_dir = r"C:\Users\josephda\GitHub\osm"
streets_dir = os.path.join(proj_dir, "data", "raw", "Street_Centerlines", "Street_Centerlines.shp")
gtfs_dir = os.path.join(proj_dir, "data", "raw", "gtfs")
aprx_dir = os.path.join(proj_dir, "arcgis", "arcgis.aprx")
gdb_dir = os.path.join(proj_dir, "arcgis", "arcgis.gdb")
nw_dir = os.path.join(proj_dir, "arcgis", "arcgis.gdb", "TransitNetwork")
# Environment settings
arcpy.env.workspace = gdb_dir
# Set output coordinate system to NAD83 CA Albers
arcpy.env.outputCoordinateSystem = arcpy.SpatialReference(3310)
# Declare the project file
aprx = arcpy.mp.ArcGISProject(aprx_dir)
# List maps in the project
for m in aprx.listMaps():
print(m.name)
# List layers in the map
for lyr in m.listLayers():
print(lyr.name)
# Remove any existing layers except the basemap
if lyr.isBasemapLayer == False:
m.removeLayer(lyr)
aprx.save()
# Load the streets shapefile as a spatially enabled dataframe
streets_sed = pd.DataFrame.spatial.from_featureclass(streets_dir)
# Create fc from streets sed
streets_fc_dir = os.path.join(gdb_dir, "streets_fc")
streets_fc = streets_sed.spatial.to_featureclass(streets_fc_dir)
# Add restrict pedestrian field
arcpy.AddField_management("streets_fc", "RestrictPedestrians", "TEXT")
arcpy.CalculateField_management("streets_fc", "RestrictPedestrians", "'Y' if !circulatio! == 'Freeway' or !circulatio! == 'State Route' or !road_type! == 'Railway' else 'N'", "PYTHON")
arcpy.AddField_management("streets_fc", "ROAD_CLASS", "SHORT")
# Create the feature dataset
arcpy.CreateFeatureDataset_management(gdb_dir, "TransitNetwork", arcpy.SpatialReference(3310))
# Covert GTFS to transit data model
arcpy.transit.GTFSToPublicTransitDataModel(gtfs_dir, nw_dir, "NO_INTERPOLATE", "NO_APPEND")
# Copy streets to TransitNetwork feature dataset as "Streets"
nw_streets_dir = os.path.join(nw_dir, "Streets")
arcpy.CopyFeatures_management("streets_fc", nw_streets_dir)
# Delete the other copy of streets
arcpy.Delete_management("streets_fc")
# Connect the transit data model to streets
arcpy.transit.ConnectPublicTransitDataModelToStreets(nw_dir, "Streets", "100 Meters", "RestrictPedestrians <> 'N'")
It throws the same error
Unfortunately, this update still throws the same 000354 error when connecting the transit model to streets (at line 62). If I replace line 62 with the following:
arcpy.transit.ConnectPublicTransitDataModelToStreets(r"C:\Users\josephda\GitHub\osm\arcgis\arcgis.gdb\TransitNetwork", "Streets", "100 Meters", "RestrictPedestrians <> 'N'")
It throws the same error. This makes me think the issue isn't rooted in the path construction, but I could be wrong.
Does the tool still error if you replace "Streets" with the full path to the streets feature class? Maybe the tool isn't correctly paying attention to the workspace environment set in the script.
Yeah, still the same error when replacing "Streets" with the full path to the fc.
Okay. Well I don't see anything obvious from just looking at the script, unfortunately. You may need to call Esri Support so they can dig into your data and script in detail and figure out what's happening.
Ok yeah I'll keep troubleshooting and call support if needed. Thanks for taking a look.
One further thought (after looking at the tool code): Try printing the value of arcpy.env.scratchGDB in your code and see what it points to. Delete it, and try running the script again. I've seen cases where the scratch gdb gets corrupted, and then weird things happen.
Possibly it works when you run the tool in the Pro UI because it's pointing to a different scratch gdb.
That was it! After deleting scratch gdb the script ran successfully. I wish I knew how exactly the scratch gdb was possibly corrupted to avoid or help trouble shoot future issues. But in any case it worked. Thanks very much!
"I wish I knew how exactly the scratch gdb was possibly corrupted"
Me too! I'd love to get that fixed on our end.
Glad that worked!!