New answers tagged

0

In the field calculator there is a help link (info) that leads you to the help-page where exactly that examples is shown. You have to put into the expression field something like(activate code-block needs to be checked): def classification(class_field): if class_field == "Forested Wetland": return 6 elif class_field == "Water": return 7 and ...


0

I might expect the results for area calculated in the projected space to be different from those using geographic coordinates. It just depends what areas the tool claim to compute. Consider the "square" whose corners are at the UTM coordinates 18n 528007 4467447 18n 528008 4467447 18n 528008 4467448 18n 528007 4467448 It might be plausible to ...


2

Since your working in arcpy and doing a simple data transfer, use a search cursor to load your Spatial Join count results as the value in a dictionary associated with the FID key and then use and update cursor to read the FID from the current feature being updated and lookup the count from the dictionary to post it. It takes a few more lines of code to set ...


4

You can use the \W (upper case W) to match any non-alphanumeric characters. If using regexp_replace() it seems you need to escape the backslash, so it becomes \\W (thanks, Joseph!) For example regexp_replace( '=+&hello%%£world','\\W+','_') returns _hello_world If using the python equivalent function re.sub(), you probably won't need to escape ...


1

Try putting your number "01" into a second set of quotes "'01'" (double-quote then single-quote or the other way around) arcpy.CalculateField_management("Table1","Field1","'01'","PYTHON","#")


0

Try using the .zfill() function somewhere in your code. Input the length you are needing in the parentheses, 2 in your case. This will pad the text field with 0s


0

I think that what you may be looking for is the Esri Technical Article on HowTo: Display coded value domains and subtype codes instead of descriptions: By default, ArcMap displays the coded value domain and subtype descriptions in the attribute table. The instructions provided ... describe how to display the coded value domain and subtype codes ...


0

I had a similar need to extract a variable length string from within a longer string, but where the left and right lengths of characters to be removed from the source string were fixed (but not necessarily the same). I used this expression in the field calculator for the new string: Left(RIGHT([field],Len([field] )-N1),Len(RIGHT([field],Len([field] ...


1

I was able to reproduce this effect, although I had to end up using the code: !shape.area@squaremeters! I got the following results in my table, with both fields being identical like you stated: Reading into the help guide, it looks like it could be an issue with the fact that the "Calculate Geometry" tool lets you utilize the coordinate system that ...


2

Your single line !file!.replace(" ","") can be added to by including more .replace() !file!.replace(" ","").replace("?","").replace(",","").replace(chr(33),"") and so on. You may need to escape some of the characters, or use their chr() code if the field calculator doesn't like them. chr(33) in my example replaces the !. Chr codes can be retrieved ...


1

It's not pretty, but ''.join([x for x in list(!file!) if x not in ["!","@","$"]]) should remove any single character you specify in the list, and return the resulting characters as a string (the "etc" in your example wouldn't work, however).


5

Even though this question can be considered off topic I though I would make a suggestion. Try using the Python re module in the Field Calculator. Here is a simple example used in the ArcMap field calculator. Pre-Logic Script Code import re def repChar(strg) regexChars = '[!@$*#]' line = re.sub(regexChars,'',x) return line Field ...


4

This is a two step process, and as a result, the Field Calculator is not well suited to it. It is better to run this in a standalone script. However, it can be done in the field calculator, provided you use a trick. You do need to use a cursor to load all of the values to a global dictionary from a sorted list, but only during the calculation of the first ...


5

"Solution" with 2 sorted fields (ascending): mxd = arcpy.mapping.MapDocument("CURRENT") lr=arcpy.mapping.ListLayers(mxd)[0] tbl=arcpy.da.TableToNumPyArray(lr,("oid","A","B")) bs=sorted(tbl, key=lambda x: (x[1], x[2])) def sortSeq(fid,a,b): for i,ent in enumerate(bs): if ent[0]==fid: return i -------------------------------------- sortSeq( !OID!, !A!, ...


2

According to http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/expressions.html the member function QgsExpression.prepare() is not mandatory but makes the execution faster. I would recommend to comment out the line e.prepare(layer.pendingFields()) and see what happens. And as far as I see, an exp.prepare(...) is missing directly behind the ...


2

There were some records that were blank which caused an error. This code works: def getSufDir( prop_location ): if prop_location is None: return None else: return prop_location.strip().split(" ")[-1]


6

Try passing your variable to your string: var_a = 6.2 arcpy.CalculateField_management (in_table, in_field,"[FIELD_A]*100/{}".format(var_a))


6

You can use the following code which connects the attributeValueChanged event to a function we can define which inserts the results of the $now expression. Highlight your layer and copy/paste the following into the Python Console: layer = qgis.utils.iface.activeLayer() def update(): field = layer.fieldNameIndex('mod') e = QgsExpression( " $now " ) ...


3

This will convert ALL last 2 characters to uppercase, even if these characters are not directions (Nw, Sw,...) : concat( substr( title( "fieldname"), 1, length( "fieldname")-2), upper(substr("fieldname", length( "fieldname")-1, 2)) )


5

So, I've coded a plugin (Sort and Number) to solve your issue. It allows you to select up to 3 fields and order your attribute table according to these fields. Then, it numbers the attribute table in a new field (named "order" by default), starting from 1.


6

I'm not sure you can with field calculator, but you can use a virtual layer with a query like : SELECT * , (SELECT count(*) FROM your_layer AS b WHERE a.length >= b.length) AS length_position FROM your_layer AS a ORDER BY a.length



Top 50 recent answers are included