POST
|
Hello Curtis, I think I was too vague: the cursor example I provided works fine. However, NOTHING I have tried in the Field Calculator box worked. Even if I replace references of '\r\n' to (chr13) + chr(10), it still doesn't work. One of the responses on GIS StackExchange describing the exact same problem recommends the same thing as you, and those who tried it seemed to have just as little luck as I did. I set up a quick test just to make sure that I was isolating the issue: In a new shapefile, I add 2 text fields (TEXTFIELD, NEWTEXTF) I create a single feature I type the following text in Notepad: "This is a[ENTER]test" (where [ENTER] represents pressing the Enter button) I copy-paste this text (which is on two lines) into the feature's TEXTFIELD value I then run the following in FieldCalculator: NEWTEXTF = !TEXTFIELD!.upper() This generates the following error message: Executing: CalculateField test NEWTEXTF !TEXTFIELD!.upper() PYTHON_9.3 # Start Time: Thu Jul 18 12:35:16 2013 ERROR 000539: Error running expression: "This is a test".upper() <type 'exceptions.SyntaxError'>: EOL while scanning string literal (<string>, line 1) Failed to execute (CalculateField). Failed at Thu Jul 18 12:35:16 2013 (Elapsed Time: 0,00 seconds) Any attempts to replace the newlines, using either either escape sequences or chr() calls, result in the same error. It looks as if the CalculateField is passing along the newlines unescaped, which breaks the interpreter. So, a couple of questions come to mind: Do you get the same behaviour as me for the basic case of !TEXTFIELD!.upper()? If yes, does this mean that ALL CalculateField calls that use the Python interpreter need to have their input sanitized to remove newlines? Or that we should just switch to Cursors in all cases to avoid any errors or difficulties? Could you paste the actual working code you used to get the example working properly? If you'd prefer, we can correspond directly by e-mail too, so I can send you the samples I have. Thanks so much!
... View more
07-18-2013
09:05 AM
|
0
|
0
|
920
|
POST
|
Thank you very much for your answers! Vince: I meant that the source is sometimes shapefiles, which I export into a GDB, and not using the shapefile as some kind of weird interchange format. But it's understood that there's no need to start back from shapefile. I'm also not very aware of the proper processes for spatial defragmentation. If I am using ArcGIS for Desktop, would this involve recreating the Spatial Index of individual FC's one by one? Is there a means of managing this by way of scripting / automation? Thanks in advance! Marc
... View more
12-17-2012
06:49 AM
|
0
|
0
|
210
|
POST
|
Hey everyone, What is the best way of upgrading 9.2/9.3 geodatabases to 10.x? In my mind, I can either: Use the built-in "Upgrade Geodatabase" tool Create a new 10.x geodatabase and use FC to GDB to copy from one GDB to the other. Take the original shapefiles (where available) and use FC to GDB to convert everything to FC's My gut would be to simply use the Upgrade Geodatabase tool, but I was wondering if this would always be the case. Thanks a bunch!
... View more
12-14-2012
09:35 AM
|
0
|
3
|
1168
|
POST
|
I do not believe that this problem has been sufficiently adressed. Using the Field Calculator box, with text containing a CR-LF (Characters 10-13), I have tested all of the following: Using a replace ('\r\n', '') Using strip Using filter Using a comprehension that decompiles and checks individual characters Even using a Codeblock, these tactics did not seem to work. However, if I go from the console and set up something like: rows = arcpy.UpdateCursor(fc)
for row in rows:
if '\r\n' in row.TextString:
row.setValue('TextString', row.TextString.replace('\r\n', ' '))
rows.updateRow(row)
del row
del rows It works exactly as one would expect. But I would love to know more about why this doesn't seem to work from the Field Calculator window.
... View more
12-06-2012
07:28 AM
|
0
|
0
|
920
|
POST
|
Hi Jeff, Much obliged! Just sent you the script and the Map Package.
... View more
09-05-2012
10:32 AM
|
0
|
0
|
384
|
POST
|
Additional Info: Trying to Set Data Sources in ArcCatalog causes it to crash, with a "ArcGIS Desktop has encountered a serious application error" message. I can send the MXD in question to anyone interested.
... View more
09-04-2012
03:12 PM
|
0
|
0
|
384
|
POST
|
Hello! I've created a script to crawl through all the MXD's on our server and log in a CSV the workspaces being used by each MXD. The script seems to work fine; the thing is, certain MXD's seem to cause Python itself to crash. No error messages, or codes, just "Python.exe has stopped working." After tinkering a bit, I found that it seems to be my call to ListLayers that kills everything. The relevant bit of code is here: mxd = arcpy.mapping.MapDocument(mxd_file)
wss = [lyr.workspacePath for lyr in arcpy.mapping.ListLayers(mxd)
if lyr.supports('workspacePath')]
Bizarrely, I can't seem to figure out what's causing Python to crash. The script only seems to fail when it encounters some (but not all) Index Layers for Data Driven Pages... Even more bizarrely, when the implicated MXD is opened in ArcMap and the script is run in parallel, everything works swimmingly. Anyone have any clues on what might be going on? I'm using Python 2.6 with an ArcView 10.0 License, SP4. Windows 7, SP 1 64bit. Thanks a bunch!
... View more
09-04-2012
02:22 PM
|
0
|
5
|
3984
|
POST
|
Thanks for the correction. Been copy-pasting code back and forth between development workstations, which probably caused the typo. Sorry about that! Cheers,
... View more
11-22-2011
08:50 AM
|
0
|
0
|
1778
|
POST
|
Hey guys, I was thinking about this very problem today. I tried to create a simpler, cleaner interface for straightforward field mapping situations. Using Mathew's code and example as a reference, I came up with the following: def GetFieldMappings(fc_in, fc_out, dico):
field_mappings = arcpy.FieldMappings()
field_mappings.addTable(fc_in)
for input, output in dico.iteritems():
field_map = arcpy.FieldMap()
field_map.addInputField(fc_in, input)
field = fieldmap.outputField
field.name = output
field_map.outputField = field
field_mappings.addFieldMap(field_map)
del field, field_map
return field_mappings With this in place, you can simply make a call as follows: fc_in = r'C:/input.shp'
fc_out = r'C:/output_blocks.shp'
dico = {'OBJECTID': 'O_OID',
'BLOCKSTAGE': 'BLOCKSTAGE',
'AREAGIS': 'AREAGIS',
'OPERATIONSTYPE': 'OPTYPE',
'BLOCKTYPE': 'BLOCKTYPE',
'HARVESTSEASON': 'HARVSEASON',
'ROADPERCENTAGE': 'ROADPERCNT'}
Mapper = GetFieldMappings(fc_in, fc_out, dico) and your GeFieldMappings subroutine will return the proper FieldMappings object, properly populate. The relationships are stored within a simple Python dictionary, which seemed to me the most obvious way of structuring the information. Clearly, this doesn't support more complicated relationships for merge rules, but you can either handle those manually, or create a nested dictionary or list that specifies the rule to use. Hope this helps!
... View more
11-22-2011
06:42 AM
|
4
|
0
|
1778
|
POST
|
Hello Christina, What you need is the Make Feature Layer method. It works almost identically to Select By Attribute in ArcMap or ArcCatalog: it will use the filter and generate a layer of features that satisfied the filter condition. filter = "\"FACILITY_I\" = ' '"
shp = r"C:\Users\gisadmin\Desktop\TEST\FH_Test.shp"
lyr = arcpy.MakeFeatureLayer(shp, filter)
result = int(arcpy.GetCount_management(lyr).getOutput(0))
print result And your output can go wherever you want, depending on what format you're looking at. To write it to a table, you could use a Cursor object. If you're writing it to a table in a textfile or CSV, you could write it using Python's builtin open function. With a little more information, this shouldn't be hard to do. I hope this helps!
... View more
10-14-2011
06:58 AM
|
0
|
0
|
367
|
POST
|
Hello Yousaf, My apologies: I had misread the thread! ET Geowizards tools does in fact operate on all items of the shape or feature class. If you want to do it point by point, you could do one of two things: - As described above, do an AddField followed by a CalculateField, but only on the active layer. - Create your fields, select the points you're interested in and do the following: Attribute Table - Right Click on X or Y Field - Calculate Geometry... - X / Y Coordinate of Point. The overhead of manually clicking on each point, then activating the tool you had in mind seems comparable in my books to using this built-in function of ArcMap. I hope this helps! Write back if anything's unclear.
... View more
10-14-2011
06:53 AM
|
0
|
0
|
964
|
POST
|
Hey Jake, Right on! Spatial Join would've been what I'd recommend, though the Python code to do the same work isn't too long and exposes people to a lot of the ideas behind cursors and layers. As a point of information, you could probably speed up this code by using a couple of nifty tricks: In general, list comprehensions are much faster than iterated item appends. There's always the risk of things getting messy, but I think your code is a great example of a case where it would be easy to slide it in. You seem to take the max OID and then just iterate for all numbers between it. You could make that code faster by only iterating through the items of your first list (for valid_OID in list:) or just iterating through the rows of the full feature class directly. import arcpy
arcpy.env.workspace = r"C:\temp\python\test.gdb"
arcpy.env.overwriteOutput = True
cities = "Cities"
counties = "Counties"
city_layer = "cities_feat"
county_layer = "counties_feat"
arcpy.MakeFeatureLayer_management(cities, city_layer)
for row in arcpy.SearchCursor(counties):
county_OID = row.OBJECTID
arcpy.MakeFeatureLayer_management(counties, county_layer, 'OBJECTID = ' + str(county_OID))
arcpy.SelectLayerByLocation_management(city_layer, 'WITHIN', county_layer)
list2 = [r.POPULATION for r in arcpy.SearchCursor(city_layer)]
sumlist = sum(list2)
county_rows = arcpy.UpdateCursor(county_layer )
for layer_row in county_rows:
layer_row.URBPOP = sumlist
county_rows.updateRow(row2)
del county_rows, row, layer_row I hope this is of use!
... View more
10-14-2011
06:30 AM
|
0
|
0
|
528
|
POST
|
import arcpy arcpy.env.workspace = r"C:\temp\python\test.gdb" arcpy.env.overwriteOutput = True cities = "Cities" counties = "Counties" city_layer = "cities_feat" county_layer = "counties_feat" arcpy.MakeFeatureLayer_management(cities, city_layer) for row in arcpy.SearchCursor(counties): county_OID = row.OBJECTID arcpy.MakeFeatureLayer_management(counties, county_layer, 'OBJECTID = ' + str(county_OID)) arcpy.SelectLayerByLocation_management(city_layer, 'WITHIN', county_layer) list2 = [r.POPULATION for r in arcpy.SearchCursor(city_layer)] sumlist = sum(list2) county_rows = arcpy.UpdateCursor("counties_feat") for layer_row in county_rows: layer_row.setValue('URBPOP', sumlist) county_rows.updateRow(row2) del county_rows, row, layer_row
... View more
10-14-2011
06:30 AM
|
0
|
0
|
528
|
POST
|
ETGeowizard's unregistered version can do this as well, through arcpy, ArcCatalog or ArcMap, and it's free. The function is called Polygon To Point, i think. Hope this helps!
... View more
10-13-2011
07:08 PM
|
0
|
0
|
964
|
POST
|
In principle, you can use arcpy.importtoolbox to import whatever toolbox you need. You then need to analyze the items of arcpy (using something like the built-in dir function) to see what these items are called. You could try comparing the items before and after to see what's changed (and thus what was contributed by your 3rd party tool.) Alternatively, I've had success in the past by following these (kludgy) steps if you're in ArcGIS 10: - Import the toolbox into your ArcToolBox window - Run the tool - Check under Results - Right-Click on the tool's results, and select Copy As Python Snippet Voila: you have the executable code and tool reference! You then need to import the Toolbox in arcpy using arcpy.importtoolbox in your Python script for automation. I've had success with ET Geowizards and XTools this way. Hope this helps!
... View more
10-13-2011
08:03 AM
|
0
|
0
|
169
|
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
|