|
POST
|
I have a table that I've modified coming out of the Network Analyst OD Cost Matrix (If you've read my other post on getting a result object to a GDB Table, I am currently getting this out of ArcMap as a workaround). The table looks something like this like this, where the latter three fields are type 'Double' OriginID DestID Total_Length MarketShare Sum_MarketShare 112358924132 61124 1.24 24 55 112358924133 61124 1.36 26 44 112358924134 61124 1.54 32 64 112358924135 61124 1.72 54 109 112358924132 53856 1.36 21 55 112358924133 53856 .24 18 44 112358924134 53856 1.53 32 64 112358924135 53856 1.27 55 109 I need to calculate the 'Sum_MarketShare' field to determine the Sum off all market share for each unique Block Group set.
... View more
06-11-2013
01:03 PM
|
0
|
4
|
1217
|
|
POST
|
I think I've angered the GIS gods...
arcpy.da.FeatureClassToNumPyArray(ODCM_LYR, "*")
Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> RuntimeError: cannot open 'C:\ArcGIS\SYSTEM\COF\Data\BG2Branches.lyr'
... View more
06-11-2013
11:33 AM
|
0
|
0
|
1919
|
|
POST
|
I'm not sure what your source table is (.dbf?), Hey James, any insight is useful. The ODCM_Table should just be holding the result object from the OD Cost Matrix in Network Analyst. The OD Cost Matrix analysis layer 'outNALayer' is composed of six network analyst classes, one of which is 'Lines' which stores the path information, like Total Length from Origin to Destination. So really the question here is how can I convert the an NA Class to a Table, or Feature Layer outside of an ArcMap Session. It doesn't seem possible.
... View more
06-11-2013
10:56 AM
|
0
|
0
|
2780
|
|
POST
|
Here's the full code, though it's evolved a bit since the original post. For the sake of brevity, I'm not posting the entire code here. Just the section causing issues but I do know for a fact that the issue is within the section. I'm also just using the 'cbsaFD' variable statically for testing purposes however, in the actual code 'cbsaFD' represents an list item to iterate through using a for-loop (ie: for cbsaFD in cbsaFD_set). In a prior step, the 'bgCentroids', 'proForma' and 'AOIs' were clipped to the extent of the the CBSA boundary with which they intersect using SelectLayerByLocation.
arcpy.env.workspace = r"C:\ArcGIS\SYSTEM\COF\Data\CapitalOne.gdb\CBSA_12345"
arcpy.env.overwriteOutput = True
GDB = r"C:\ArcGIS\SYSTEM\COF\Data\CapitalOne.gdb"
cbsaFD = "CBSA_12345"
bgCentroids = r"C:\ArcGIS\SYSTEM\COF\Data\CapitalOne.gdb\CBSA_12345\CBSA_12345_bgCentroids"
proForma = r"C:\ArcGIS\SYSTEM\COF\Data\CapitalOne.gdb\CBSA_12345\CBSA_12345_proForma"
AOIs = r"C:\ArcGIS\SYSTEM\COF\Data\CapitalOne.gdb\CBSA_12345\CBSA_12345_RN"
inOrigins = bgCentroids
inDestinations = proForma
inNetworkDataset = r"C:\ArcGIS\Business Analyst\US\Data\StreetMapData\streets"
outNALayerName = "BG2Branches"
impedenceAttribute = "Length"
outLayerFile = outNALayerName + ".lyr"
outNALayer = arcpy.MakeODCostMatrixLayer_na(inNetworkDataset, outNALayerName, "Length", "", "", "", "", "", "USE_HIERARCHY", "", "NO_LINES")
outNALayer = outNALayer.getOutput(0)
subLayerNames = arcpy.na.GetNAClassNames(outNALayer)
originsLayerName = subLayerNames["Origins"]
originsFieldMap = arcpy.na.NAClassFieldMappings(outNALayer, originsLayerName)
originsFieldMap["Name"].mappedFieldName = "BG_ID"
arcpy.na.AddLocations(outNALayer, originsLayerName, inOrigins, originsFieldMap)
destinationsLayerName = subLayerNames["Destinations"]
destinationsFieldMap = arcpy.na.NAClassFieldMappings(outNALayer, destinationsLayerName)
destinationsFieldMap["Name"].mappedFieldName = "DID"
arcpy.na.AddLocations(outNALayer, destinationsLayerName, inDestinations, destinationsFieldMap)
arcpy.na.Solve(outNALayer)
So in an ArcMap Session, I can grab the result object exactly as you said by referencing the Feature Layer directly out of the TOC and export it to a table:
arcpy.TableToTable_conversion(r"BG2Branches\Lines", cbsaFD + "_ODCM")
The problem arises when I do this through Catalog. Since there is no way to reference the feature directly because ArcMap did not automatically convert the result object to a Feature Layer, I have to do this myself. No sweat, right?
arcpy.MakeFeatureLayer_management(outLayerName, cbsaFD + "ODCM", "", GDB)
Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\management.py", line 5748, in MakeFeatureLayer raise e ExecuteError: ERROR 000732: Input Features: Dataset BG2Branches.lyr does not exist or is not supported
So...I tried doing an arcpy.Describe and get an Object Does Not Exist error. So then I tried instantiating the result object under a variable:
Branch2BG_ODCM_LYR = arcpy.Solve(outNALayer)
Branch2BG_ODCM_LYR = Branch2BG_ODCM_LYR.getOutput(0)
print Branch2BG_ODCM_LYR
GPL0
Whoo hoo, have the object reference under a variable, yay!
arcpy.Describe(Branch2BG_ODCM_LYR)
Runtime error
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\__init__.py", line 1200, in Describe
return gp.describe(value)
File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 374, in describe
self._gp.Describe(*gp_fixargs(args, True)))
IOError: "GPL0" does not exist
What in holy heck is going on here?!
... View more
06-11-2013
08:21 AM
|
0
|
0
|
2780
|
|
POST
|
If you are trying to get the lines object from an od cost matrix, you will need to use the "Select" tool. It would look something like this (sorry it's a dirty example): ND = "your network dataset"
OD_Lines = "output location for table
Lines = r"OD_Matrix\Lines"
OD_Matrix = "OD_Matrix"
arcpy.MakeODCostMatrixLayer_na(ND,OD_Matrix,"Distance","","1")
arcpy.AddLocations_na(OD_Matrix, "Origins", "your input")
arcpy.AddLocations_na(OD_Matrix, "Destinations", "your input")
arcpy.Solve_na(OD_Matrix, "SKIP","CONTINUE")
arcpy.Select_analysis(Lines, OD_Lines) It seems this only works if you're doing it from the Python Window in ArcMap. Otherwise, I can't seem to get an instantiation of the Lines object. I've tried directly referencing the result object through a variable but then when I try to do something with it it says it doesnt exist. When I run the exact same code in Catalog...no workie.
... View more
06-10-2013
04:54 PM
|
0
|
0
|
2780
|
|
POST
|
I have a table being returned from the Network Analyst OD Cost Matrix... I want to convert the result object to a GDB Table and I tried using this..
ODCM_Table = arcpy.na.Solve(outNALayer) #Solves the Origin-Destination Cost Matrix and produces the ODCM_Table
arcpy.TableToTable_conversion(ODCM_Table, workspace + "\Default.gdb", "ODCM") # Thought this should convert result to GDB Table
But it throws an error: Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> File "c:\program files\arcgis\desktop10.1\arcpy\arcpy\conversion.py", line 1904, in TableToTable raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Input Rows: Dataset GPL0 does not exist or is not supported Failed to execute (TableToTable). I thought maybe the cause of the error was the formatting of the result object being held on the 'ODCM_Table' variable, so I printed it to see what it returns...
print ODCM_Table
GPL0
No problem I can see there. So how can I convert this result object to an actual table?
... View more
06-05-2013
10:39 AM
|
0
|
20
|
5918
|
|
POST
|
I have a table being returned from the Network Analyst OD Cost Matrix... [ATTACH=CONFIG]25021[/ATTACH] I want to convert the result object to a GDB Table and I tried using this..
ODCM_Table = arcpy.na.Solve(outNALayer) #Solves the Origin-Destination Cost Matrix and produces the ODCM_Table
arcpy.TableToTable_conversion(ODCM_Table, workspace + "\Default.gdb", "ODCM") # Thought this should convert result to GDB Table
But it throws an error: Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> File "c:\program files\arcgis\desktop10.1\arcpy\arcpy\conversion.py", line 1904, in TableToTable raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Input Rows: Dataset GPL0 does not exist or is not supported Failed to execute (TableToTable). I thought maybe the cause of the error was the formatting of the result object being held on the 'ODCM_Table' variable, so I printed it to see what it returns...
print ODCM_Table
GPL0
No problem I can see there. So how can I convert this result object to an actual table?
... View more
06-05-2013
10:38 AM
|
0
|
0
|
793
|
|
POST
|
Thanks Caleb! Just before you replied, I actually figured out that it was easier to just update the workspace directly with the 'uniqueVal' variable than to use the ListFeatureDatasets function. I agree with your thoughts on the ListFeatureDatasets function returning the value in brackets. os.path.join is probably a better way to go. Once I got the original code running, the first loop to 2 minutes to run and the second loop took 24 minutes to run for a total runtime of 26 minutes. With that, I combined the functions into a single loop, hoping that would result in some level of improvement since it wouldn't need to loop through the dataset twice.
for uniqueVal in uniqueSet:
print "Creating Feature Dataset for CBSA " + uniqueVal + "..."
arcpy.CreateFeatureDataset_management(workspace, "CBSA_" + str(uniqueVal))
print "Successfully created CBSA " + str(uniqueVal) + " Feature Dataset."
print "Resetting workspace to CBSA " + str(uniqueVal) + "'s Feature Dataset..."
arcpy.env.workspace = r"C:\Users\jdk588\Documents\New File Geodatabase.gdb\CBSA_" + str(uniqueVal)
print "Successfully reset Workspace."
print "Splitting CBSA " + str(uniqueVal) + " into Feature Dataset..."
arcpy.Select_analysis(fc, "CBSA_" + str(uniqueVal) + "_bdy", '"ID" = ' + "'" + str(uniqueVal) + "'")
print "Successfully split CBSA " + str(uniqueVal) + " into Feature Dataset."
Even with the functions combined into a single loop, it still took 26 minutes to run the entire process from FD creation to splitting the uniqueVals into their respective FD. A little longer than I think it should take. I wonder if the os.path.join method you mention would result in any performance improvements. My instinct tells me it could, but I suspect it would be negligible since resetting the workspace only takes a second, if that. Can you or anyone else spot any areas I could revise to boost performance?
... View more
05-28-2013
08:19 AM
|
0
|
0
|
1854
|
|
POST
|
So, now I'm trying to split records into multiple feature datasets, based on the record's unique value. The end goal is to have a seperate feature dataset for each unique record. I'm getting some errors when trying to dynamically create the feature dataset though. I figured the easiest way to accomplish this would be through two seperate loops, one to create the Feature Datasets based on the uniqueVal and another to actually perform the split.
workspace = arcpy.env.workspace = r"C:\Users\jdk588\Documents\New File Geodatabase.gdb"
fc = r"C:\Users\jdk588\Documents\New File Geodatabase.gdb\Selected_CBSAs"
uniqueSet = set([r[0] for r in arcpy.da.SearchCursor (fc, ["ID"])])
for uniqueVal in uniqueSet:
print "Creating Feature Dataset for CBSA " + uniqueVal + "..."
arcpy.CreateFeatureDataset_management(workspace, "CBSA_" + str(uniqueVal))
print "Successfully created CBSA " + str(uniqueVal) + " Feature Dataset."
for uniqueVal in uniqueSet:
featureDataset = arcpy.ListDatasets("*" + str(uniqueVal), "Feature")
workspace = arcpy.env.workspace = featureDataset
print "Splitting CBSA " + str(uniqueVal) + "..."
arcpy.Select_analysis(fc, "CBSA_" + str(uniqueVal) + "_bdy", "ID = " + str(uniqueVal))
print "Success."
Creation of the featuredatasets works fine and it creates them all. However in the second loop, I'm trying to load each feature into its own feature dataset using the Select_analysis tool, which means I need to set the workspace to the appropriate feature dataset with each iteration of the loop. I'm not understanding why it can't access the workspace. Runtime error Traceback (most recent call last): File "<string>", line 11, in <module> File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 529, in set_ self[env] = val File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 581, in __setitem__ ret_ = setattr(self._gp, item, value) RuntimeError: Object: Error in accessing environment <workspace>
... View more
05-28-2013
05:49 AM
|
0
|
4
|
2030
|
|
POST
|
The name is invalid because names in the gdb cannot contain ".". The ".0" is appearing I would guess because if the input field is float or double, the string representation in Python includes the ".0". Spot on. Converted the field to string and after fiddling with my expression, it worked perfectly. Thanks curt! Guess I'll go back and apply a string filter to the field.
... View more
05-24-2013
10:21 AM
|
0
|
0
|
1553
|
|
POST
|
Thanks Curt! That was it, except now it's saying my expression is invalid. ExecuteError: ERROR 000358: Invalid expression DID = 27242 Failed to execute (Select). Can you spot anything wrong with it? Nevermind, got it. strings require quotes...
arcpy.Select_analysis(fc, str(prefix), "'" + field + "' = " + "'" + str(uniqueVal) + "'")
Thanks again curt!
... View more
05-24-2013
10:19 AM
|
0
|
0
|
1553
|
|
POST
|
Thanks Curt! That was it, except now it's saying my expression is invalid. ExecuteError: ERROR 000358: Invalid expression DID = 27242 Failed to execute (Select). Can you spot anything wrong with it?
... View more
05-24-2013
10:14 AM
|
0
|
0
|
1553
|
|
POST
|
Any ideas why I'm getting this error: Traceback (most recent call last): File "<string>", line 90, in execute File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\analysis.py", line 98, in Select raise e ExecuteError: ERROR 000210: Cannot create output C:\Users\jdk588\Documents\New File Geodatabase.gdb\Cafe_27242.0 ERROR 000361: The name starts with an invalid character Failed to execute (Select). Failed to execute (Split). From this tool:
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "COF"
self.alias = "cof"
# List of tool classes associated with this toolbox
self.tools = [Split]
class Split(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Split by Unique Value"
self.description = "Splits an input featureclass into multiple feature classes by a unique value."
self.canRunInBackground = True
def getParameterInfo(self):
"""Define workspace parameter"""
workspace = arcpy.Parameter(
displayName = "Workspace",
name = "in_workspace",
datatype = "DEWorkspace",
parameterType = "Required",
direction = "Input",)
"""Apply workspace parameter filter"""
workspace.filter.list= ["Local Database"]
"""Define in_featureclass parameter"""
in_featureclass = arcpy.Parameter(
displayName = "Feature Class to Split",
name = "in_fc",
datatype = "Feature Layer",
parameterType = "Required",
direction = "Input")
"""Apply a Geometry Type Filter to in_featureclass"""
in_featureclass.filter.list = ["Point", "Polygon", "Polyline", "Multipoint"]
"""Define unique field parameter"""
unique_field = arcpy.Parameter(
displayName = "Field containing Unique Values",
name = "unique_values",
datatype = "Field",
parameterType = "Required",
direction = "Input")
"""Apply a dependency to unique field parameter"""
unique_field.parameterDependencies = [in_featureclass.name]
"""Define outfc naming prefix parameter"""
pfx = arcpy.Parameter(
displayName = "Output Prefix",
name = "out_pfx",
datatype = "GPString",
parameterType = "Required",
direction = "Input")
params = [workspace, in_featureclass, unique_field, pfx]
return params
def isLicensed(self):
return True
def updateParameters(self, parameters):
return
def updateMessages(self, parameters):
return
def execute(self, parameters, messages):
"""The source code of the tool."""
arcpy.env.workspace = parameters[0].valueAsText
fc = parameters[1].valueAsText
field = parameters[2].valueAsText
name = parameters[3].valueAsText
uniqueSet = set([r[0] for r in arcpy.da.SearchCursor (fc, [field])])
for uniqueVal in uniqueSet:
prefix = name + "_" + str(uniqueVal)
arcpy.AddMessage("Splitting " + str(uniqueVal) + "...")
arcpy.Select_analysis(fc, str(prefix), field + " = " + str(uniqueVal))
arcpy.AddMessage("Success.")
arcpy.AddMessage("All Unique Values successfully exported.")
return
It's obvious I'm missing something, but I don't know what. The value being entered in the pfx is just "Cafe" and it is taken as a GPString under the parameter and then converted to string again at 'name'. So...what gives? Why is the name invalid? And one more thing...why is it dropping that '.0" at the end of the name? The value its pulling from the field is '27240', where is the '.0' coming from??
... View more
05-24-2013
08:27 AM
|
0
|
4
|
1946
|
|
POST
|
Hmm... I was previously establising the scratchWorkspace under the Execute defintion and I swear it was throwing syntax errors. Ok, chalk that one off to user error, I guess.
def execute(self, parameters, messages):
"""The tool logic to perform once the tool is executed."""
arcpy.env.workspace = "C:\Temp"
Works fine, except I decided look at the Scratch Workspace in the tool GUI and the value is blank. Shouldn't the value "C:\Temp" be populated in the GUI? In any case, I cant get scratchGDB to take without an error.
class Tool(object):
def __init__(self):
"""Define the tool's attributes."""
self.label = "MyTool"
self.description = "Tool1"
self.canRunInBackground = False
arcpy.env.scratchGDB = r"C:\Temp\MyGDB.gdb"
When I do that, 'MyTool' throws an error: Traceback (most recent call last): File "<string>", line 31, in __init__ File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 529, in set_ self[env] = val File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 581, in __setitem__ ret_ = setattr(self._gp, item, value)AttributeError: Object: Environment <scratchGDB> cannot be set It doesn't matter if I use scratchGDB at _init_, getParameterInfo, or execute, result is always the same.
... View more
05-23-2013
01:01 PM
|
0
|
0
|
943
|
|
POST
|
I know that since Python Toolboxes are run in an application, they probably honor the executing application's environment settings. However, I also know if you explicitly establish an environment setting in a Python Script, such as env.scratchWorkspace, then the script is supposed to override the application's environment setting. I need to set a scratchWorkspace and scratchGDB in a Python toolbox tool to ensure that intermediate data locations are consistent no matter which system is executing the tool, but it keeps throwing a syntax error, even though the syntax is correct. If I type the the statement into the Python Window:
import arcpy
from arcpy import env
env.
Then intellisense brings up the scratchWorkspace and scratchGDB functions just fine. But if I do the same in a Python Toolbox, I get a syntax error. Are workspaces not supported programmatically in a python toolbox? Do I really have to have my users go and set it through the GUI? You know, I dont know much in this small, cruel world, but ESRI, that ain't right. That. Just. Ain't. Right. Disclaimer: It is highly possible that workspaces are programmatically supported in python toolboxes and I am just a bit of a moron.
... View more
05-23-2013
10:51 AM
|
0
|
2
|
1712
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 06-11-2015 12:02 PM | |
| 2 | 02-04-2016 02:35 PM | |
| 1 | 04-11-2017 12:51 PM | |
| 1 | 08-07-2015 11:00 AM | |
| 4 | 06-19-2015 01:44 PM |
| Online Status |
Offline
|
| Date Last Visited |
11-11-2020
02:23 AM
|