Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Using ArcGIS 10.1 the arcpy module has a TableToNumPyArray command, which converts a table into an array.

Unfortunately I don't have access to 10.1 on my work PC. Is there a workaround which will accomplish this using ArcGIS 10.0?

Background

I'm iterating through features in a featureclass, and I need to look-up a price depending on an attribute value.

I need to find a price for multiple fields per feature, so I can't use a table join. I can't create a relationship class as I only have an ArcView license.

A messy workaround could be to load the table into a searchCursor and iterate through each record until I find the desired value. But this would be intensive as I'd need to work through the searchCursor for every feature in the featureclass.

Thanks for any advice, Steve

share|improve this question
    
I would guess you have work restrictions preventing you from upgrading to 10.1? If not, just do the upgrade, it's free and surprisingly painless. –  bananafish Jul 25 '12 at 4:36
    
Would using the Select tool to subset your feature class before opening a SearchCursor on just that help? –  PolyGeo Jul 25 '12 at 4:39
    
@bananafish if only it was that easy :) –  Stephen Lead Jul 25 '12 at 5:41
    
@PolyGeo thanks, I ended up doing something similar but the answer below is much faster –  Stephen Lead Jul 25 '12 at 5:42
    
I used a Python dictionary for the first time a few months ago but am still learning their full power - great answer by @dmahr ! –  PolyGeo Jul 25 '12 at 7:53
add comment

1 Answer

up vote 10 down vote accepted

A workaround for not having the TableToNumPyArray command would be this:

  • First, iterate through all features in your feature class, and store the price data in a Python dictionary.
  • Second, iterate again through all features in your feature class, and just lookup your price data from the dictionary.

Instead of the processing time for n features being n*n, it will just be n*2. Here's some code:

import arcpy

PriceDictionary = {}

MyCursor = arcpy.SearchCursor("MyLayer")
for Feature in MyCursor:
    PriceDictionary[Feature.getValue("ID")] = Feature.getValue("Price")
del Feature
del MyCursor

LookupCursor = arcpy.UpdateCursor("MyLayer")
for Feature in LookupCursor:
    Feature.setValue("NewPrice",PriceDictionary[Feature.getValue("LookupField")])
del Feature
del LookupCursor

If you're not familiar with dictionaries, check out this tutorial on dictionaries. For reference, using Python dictionaries and other data structures in clever ways is often much faster than using their arcpy counterparts. If your script is going slowly, try to remove as many arcpy commands as you can.

share|improve this answer
    
genius, thanks for that –  Stephen Lead Jul 25 '12 at 5:41
    
Dictionaries are often a savior for me as well; it's pretty amazing the things you can do with them. –  Chad Cooper Jul 25 '12 at 13:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.