|
POST
|
The problem here is that !Shape.firstpoint.X! should be !Shape!.firstpoint.X (notice the '!' placement) The exclamation marks delineate a field, which is SHAPE here. Once you're looking at the right field, you can access its members' attributes or methods, using the dot notation. Worked like a charm on my PC. Best of luck!
... View more
10-13-2011
07:52 AM
|
0
|
0
|
1344
|
|
POST
|
As an additional note, Python has a built-in function for adding loop counters to iterations, enumerate() So, you could even write your example as: import arcpy
from arcpy import env
import string
env.workspace = "C:\\Users\\gisadmin\\Desktop\\TEST"
filter = ''
cur = arcpy.UpdateCursor("C:\\Users\\gisadmin\\Desktop\\TEST\\FH_Test.shp", filter)
for i, row in enumerate(cur):
row.FACILITY_I = str(i) #This is the label for the column you want to update, eg. OID
cur.updateRow(row) Cheers,
... View more
10-13-2011
07:37 AM
|
0
|
0
|
739
|
|
POST
|
Hello Kate, I'm no expert on arcpy's mapping module, but I couldn't help myself! Your first and second error messages seem consistent to me: your code dies the first time on the arcpy.mapping.ListLayers call, which returns a list of layers that suit the filter, or an empty list '[]' if no results are found. I think your filter is returning an empty list (no values found), and so the function is trying to return the first element of your empty list, which kills it with an IndexError. [][0] = DEAD Then, by removing the [0], the function successfully passes on the empty list to the arcpy.mapping.UpdateLayer, which dies because it's being handed an empty list to work on. arcpy.mapping.UpdateLayer(df, [], sourceLayer, True) = DEAD I would recommend calling arcpy.mapping.ListLayers with the mxd and no filter, to see what name is being assigned to your layer. The List methods in arcpy are relatively fickle and need an exact match of the value being searched for, or at lest wildcards around them. For example: shapethingy.shp will not appear under ListFeatureClasses(filter) if filter = 'shapethingy', but but will if filter = 'shapethingy*' or 'shapethingy.shp' In fact, if this is your only layer, you don't even need to add in a filter: just call the layer directly by name in your other functions (since the only thing being passed around by ListLayers is a string, not an object) arcpy.mapping.UpdateLayer(df, "LAYERS", sourceLayer, True) I hope this helps!
... View more
10-06-2011
09:40 AM
|
0
|
0
|
1335
|
|
POST
|
Hi David, Would it be possible to give, if not a full traceback, then at least the code snippet that was causing the error your student was describing? I've come across this problem in a couple of different contexts, from what I recall, so a bit of context could go a long way 🙂 Cheers,
... View more
10-06-2011
09:31 AM
|
0
|
0
|
1761
|
|
POST
|
Here here! I'd vote for that if you were to propose it to ArcGIS Ideas!
... View more
09-30-2011
04:45 PM
|
0
|
0
|
600
|
|
POST
|
Hello Dustin, I'm thinking that it's because of the XY Tolerance described on this ArcGIS page that is imposed on the CopyFeatures_management tool, as the data is originally unprojected. Quoting: The XY Tolerance default for an unknown coordinate system is 0.001 units I've tried playing around with arcpy.env.XYolerance, but I don't think it's applied when creating a shapefile from nothing. You might have more luck using arcpy.CreateFeatureClass_management with the proper projection, modifying arcpy.env.XYTolerance, and then using an insert cursor or append or somesuch to generate your polygon. Cheers,
... View more
09-30-2011
06:08 AM
|
0
|
0
|
422
|
|
POST
|
Hello, If you're creating the textfield filename, and trying to write text to it, I think I see the problem. For string literals, your expression value needs to be encapsulated by quotes when it's passed to the Field Calculator. Your code should therefore be: arcpy.CalculateField_management(fc, "filename", '"' + tmp2 + '"') What you were passing to the expression field before was this: myfilename whereas what you wanted to send, and what you'd see in the Field Calculator, is this: "myfilename" Hope this helps!
... View more
09-30-2011
05:07 AM
|
0
|
0
|
658
|
|
POST
|
Hello Linda, To answer your main question, the following line for codes in Adj_Neighbors is bugging because Adj_Neighbors contains nothing more than "shp_lyr", i.e. the name of your layer. To actually cycle through the values, you'd need to run a Search Cursor through it as you did with your main shape, using either: for row2 in arcpy.SearchCursor(Adj_Neighbors):
GCA_List.append(row2.getValue("Grid_Code")) or Python's list comprehensions, GCA_List = [r.getValue('ELEVATION') for r in arcpy.SearchCursor('shp_lyr')] I also don't know how successful you'll be with this approach, if I'm reading your code right. You're creating a layer for each PIP (and so I presume 1 feature per layer), and then you're running that Layer through a Select by Location to find nearby features. For one, the Select by Location can only return a subset of the layer, so either 1 or 0 features. For two, you're comparing a layer to itself: the output will be the layer, as it will always match itself (which is why you can't do a Select By Location of a shape to itself in ArcMap, if I recall.) What you would need to do is to create two separate shapes (as arcpy doesn't allow multiple independent layers on the same shape, I believe), turn one into a layer based on your PIP, and run a select by location on the other seeing how many of its features are a certain distance from your first layer's 1 feature. As a function of your needs, Near (for ArcInfo), Closest Feature Difference (for ET_Geowizards) or some kind of Buffer Analysis might be mroe straightforward. Also worth mentioning - Cursors in 9.x need to be manually iterated through, so you would need to have another row = cur.next() inside your loop to cycle through each row - You seem to only be looking for values (as opposed to modifiying them), so a Search Cursor would be more appropriate. Also, as you seem to be using arcpy, I can add that arcpy module's cursors can be used as iterators, so you could replace: cur = arcpy.UpdateCursor(shp)
row = cur.next()
while row:
...
row = cur.next() with one line: for row in arcpy.SearchCursor(shp):
... - MakeFeatureLayer_management actually allows for a where clause, so you could integrate your SelectByAttribute into it, like so: arcpy.MakeFeatureLayer_management(shp, "shp_lyr", sql) A bit wordy, but I hope this helps! Cheers,
... View more
09-30-2011
04:58 AM
|
0
|
0
|
623
|
|
POST
|
hm... it seems like the 9.3 geoprocessing object doesn't store any of the environment variables in a way that can be called up through dir()... even inspect seems to fail. well, could still do something like: funcs = ['autoCommit','cartographicCoordinateSystem','cellSize','coincidentPoints',
'compression','configKeyword','derivedPrecision','extent',
'geographicTransformations','mask','MDomain','MResolution','MTolerance',
'newPrecision','outputCoordinateSystem','outputMFlag','outputZFlag','outputZValue',
'overwriteoutput','projectCompare','pyramid','qualifiedFieldNames','randomGenerator',
'rasterStatistics','referenceScale','scratchWorkspace','snapRaster','spatialGrid1',
'spatialGrid2','spatialGrid3','terrainMemoryUsage','tileSize','workspace','XYDomain',
'XYResolution','XYTolerance','ZDomain','ZResolution','ZTolerance']
dict_temp = {}
for variable in funcs:
dict_temp[variable] = eval('gp.' + variable) for the first part....
... View more
09-29-2011
12:43 PM
|
0
|
0
|
461
|
|
POST
|
Full, 100% agreement with Logan. Python has a built-in module for logging information, and it has been tried, tested and fine-tuned to respond to most people's needs. If you are looking for a means of repeating everything to the screen and to a file, an intermediary function (like the relatively functional showGpMessage in Chris' attachment) would probably be your easiest means of doing so, along with the logger module. If, for whatever reason, you find that this is not suitable to your needs, you can have a look at overwriting sys.stdout with an object of your choice, which has a method defined for write(). For example: class GetStream(object):
def __init__(self, pLog):
self.terminal = sys.stdout
self.log = pLog
def write(self, message):
self.terminal.write(message)
try:
self.log.write(message)
except UnicodeEncodeError:
self.log.write(message.encode('LATIN-1'))
arcpy.AddMessage(message) Cheers, Marc
... View more
09-29-2011
11:42 AM
|
0
|
0
|
1239
|
|
POST
|
Hello Richard, I've definitely been able to reproduce your issue and don't know what's going on. However, the task you have in mind could be done trivially using Select by Attribute, followed by a standard Field Calculator expression. In a Python script or in the Python window, it would be as simple as: expr = '"REFERENCE_FIELD" IS NULL'
arcpy.MakeFeatureLayer_management('FEATURE_CLASS_NAME', 'temp', expr)
arcpy.CalculateField_management('temp', 'CALCULATED_FIELD', VALUE) I don't have the exact figures, but this approach was significantly faster than comparing values one at a time through an unfiltered Update Cursor (and, by extension the way I understand it, any call to an CalculateField Python script.) Doesn't change the fact that you're definitely pointing out some very weird behaviour that should be fixed! Problem NIM070782 listed for Service Pack 3 might be this, but I'm not quite sure. If you don't get a response from ESRI or someone knowledgeable in the next while, I'd recommend posting it on ArcGIS Ideas. As an aside, I believe that the proper format should always be: foo is None and not foo == None because of: back-end optimization (which is also sadly why while 1 is preferred over while True); and the ability to overload an operator in classes and change the default behaviour of an == comparison. It's no biggie, but worth pointing out. 🙂 Cheers, Marc
... View more
09-29-2011
11:15 AM
|
0
|
0
|
5456
|
|
POST
|
Hi Nick, I think the problem here is that your values might be stored as numbers. !SLOPE!.replace('999', '0') would work perfectly well with strings. The thing is, replace is a string method and so needs an input that is such. Your easiest (though sloppiest IMO) option would be to do something like: int(str(!TNODE_!).replace('10','00')) which converts your number to a string, executes the replace method, and then converts the result back into an integer (or float, or whatever you need.) Something more structured that would allow more flexibility in your logic would involve using the Codeblock in the Field Calculator, where the Code Block would be: def quick_check(val):
if val == 999:
return 0
return val and your main expression would be SLOPE = quick_check(!SLOPE!) In this way, you're passing the value in SLOPE to the function above, which returns 0 if it's 999, or the regular value if it's not. Hope this helps! Marc
... View more
09-26-2011
08:07 AM
|
0
|
0
|
592
|
|
POST
|
Hello there, There might be some contextual information that would help clear up your question: would it be possible to provide the script that is not running properly? Thanks, Marc
... View more
09-23-2011
11:57 AM
|
0
|
0
|
353
|
|
POST
|
grrr! I forgot to mention PEP8, which is absolutely excellent advice! Thanks for the reminder!
... View more
09-22-2011
08:12 AM
|
1
|
0
|
1438
|
|
POST
|
You can also make a call, for Tables, Shapes and Feature Classes, to the ListFields method directly. FieldNames = [f.Name for f in arcpy.ListFields(tbl)] # field name strings in a list
Rows = arcpy.UpdateCursor(tbl)
for Row in Rows:
for f in FieldNames:
print f, str(Row.GetValue(f))
Interestingly, ESRI doesn't seem to list any differences between these two approaches. From the page: Alternatively, the ListFields and ListIndexes functions can be used to create the same lists.
... View more
09-22-2011
08:03 AM
|
0
|
0
|
645
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 09-22-2011 08:12 AM | |
| 1 | 09-15-2011 12:28 PM | |
| 4 | 11-22-2011 06:42 AM |
| Online Status |
Offline
|
| Date Last Visited |
11-11-2020
02:24 AM
|