|
POST
|
Hmmm, wonder if you open it with the new version, and select "Save a Copy" if it will keep broken paths in there and let you replace? R_
... View more
08-01-2013
11:26 AM
|
0
|
0
|
937
|
|
POST
|
import arcpy import os p = arcpy.GetParameterAsText(0) arcpy.env.workspace = p newtable = r"H:/TableList.gdb/GISLibrary" list = [] for dirpath, dirnames, filenames in arcpy.da.Walk(p): for filename in filenames: if arcpy.Exists(os.path.join(dirpath, filename)): arcpy.AddMessage("adding file to list") descr = arcpy.Describe(os.path.join(dirpath, filename)) list.append([descr.catalogPath, descr.name, descr.dataType]) else: arcpy.AddMessage('File ' + filename + 'does not exist') continue with arcpy.da.InsertCursor(newtable, ['Path', 'FileName', 'Type']) as insert: for f in list: insert.insertRow(f) EDIT: this code only works when i chose a geodatabase as input p. if I used a folder (that contained geodatabases) as input all of my feature classes were tagged as "do not exist) Suspect the p variable is not getting passed correctly as the da.walk works fine for FGDB or the folder. Especially since the way you have it, it will only evaluate to False if you end up with an empty filenames list, which means that either p is the wrong path, or there is NODATA in there at all. Basically, da.walk makes a list, then you iterate through it and say "This file exists, so I put it in my list, if this file doesn't exist, then: so, the only way this would not be true is if the list is empty, so deleting that line here would have the same effect (minus the error of course on empty list). Also, the continue statement you have is not needed. Basically, it is the last line in the for loop. So, you are telling it, that when it is finished will all the code for the first item in the list, continue to the next. this is the default behaviour. Don't think it would make a difference, but possibly telling it to skip one? perhaps you need to set p = to: p = str(arcpy.GetParameterAsText(0)) or something. Like i said, don't tool script tools, so not sure how a single parameter gets passed. can print p and type(p) to make sure it is a string and the value is that actual path/name to the folder you want to walk through. The code above actually did not work the next day....but this should be correct (although this code is now saying all tools don't exist, might be I need to use something different than arcpy.Exists(filename) or how I defined the environment/workspace or setting my parameter as a workspace but still working that out) for arcpy.Exists: Tests for the existence of feature classes, tables, datasets, shapefiles, workspaces, layers, and files in the current workspace Don't think it was intended to look for "Tools", of course, a toolbox is a .tbx "file" so it should find them. However, I'm a little confused here. I don't do script tools, but I know that you have to tell the script what tool to use, where the toolbox is, etc. and I see no reference what so ever in your script to any tools or toolboxes. where are you defining them, and where are you calling the con tool that is giving you the error? The spatial analyst toolbox should be here: c:\program files\arcgis\desktop10.1\ArcToolbox\Toolboxes\Spatial Analyst Tools.tbx R_ Shoot, just saw something else going on here. Try the change in red above. you are checking to see if a file exists, da.walk reports filenames and directories sepratly, so you need to pass the path and name to locate the file. Just like in your describe statement.
... View more
08-01-2013
11:03 AM
|
0
|
0
|
6858
|
|
POST
|
I do everything in the stand alone IDLE that comes with the default install.
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> doc = r"D:\test.mxd"
>>> import arcpy
>>> mxd = arcpy.mapping.MapDocument(doc)
>>> DFList = arcpy.mapping.ListDataFrames(mxd) ### first with just empty mxd
>>> del mxd
>>> mxd = arcpy.mapping.MapDocument(doc)
>>> DFList = arcpy.mapping.ListDataFrames(mxd)
>>> arcpy.mapping.ListBrokenDataSources(mxd) ### then added windrosepoints, deleted the dataset, ensured it was broken, this is result.
[<map layer u'windrosepoints'>]
R_ Only tested with empty and with ONE broken dataset. This is not the first time I have encountered an issue where something fails in an external python window but works inside of arc. I have found just the opposite of this. That is why I run everything externally.....
... View more
08-01-2013
10:35 AM
|
0
|
0
|
2784
|
|
POST
|
Can you tell me why you switched from "PYTHON" to "PYTHON_9.3" in the Calc Field expression? Is that so you can use "exp" without having to escape the quotation marks? From reading other posts I was under the impression that because exp and exp2 were strings they couldn't be used as variables without formatting them as '"' + exp + '"'. Because it seems that is the one I can ususally get to work as expected. Guess I'm getting used to haveing objects returned rather than strings. â?¢To calculate strings to text or character fields, in the dialog box the string must be double-quoted ("string"), or in scripting, the double-quoted string must also be encapsulated in single quotes ('"string"'). Otherwise, just encapulated in a single set of double quotes passes the variable. Not sure where the + exp + comes from. Basically, the expression needs the quote around it when it gets passed to calcfield. If you didn't put the double quotes around exp2 in the calcfiled line, you would have to include them in the exp2 string, and since that is a calculation, would have to concatenate them on there. Easier to just pass them around the exp variable.
exp2 = '"' + str(os.path.join(dirpath, filename)) + '"'
If you used that for your exp2 variable, it would include the double quotes, so in the calcfield you would just put exp2 without the double quotes. Hope this makes sense. Even using model builder I still can't get the expression right to calc the path to a field. I tried the parse path but either I'm not using it right or that is not it's intention. so, what are you getting for the print os.path.join(dirpath, filename), or what is getting written to the text file for this value? The way I have it, it will return my path u'd:\\working.gdb\dataset' which I wrap in a str() for my exp2 variable. Oh heck, now I see an error in my code that could be causing this (breaking it for sure).
Change this:
exp2 = str(os.path.join(dirpath, filename)
to this:
exp2 = str(os.path.join(dirpath, filename))
After that, see if it works, if not, let me know what the print statements say. Actually, the one that matters is, what does print exp2 report? Also, noticed you have set the field lenth to 255, are you sure your path(s) are not exceeding that length? In ArcMap, it will warn you and truncate, not sure how it handles that in code. Back in the python script I have a feeling the script is not running in the order I specified. I keep getting an error message about the schemas not matching to do the append. When I try printing the fields after the delete statement I see all the original fields - meaning no new fields were added and calc'd, and the old fields were not deleted. How do I ensure that the processes run in the order I've put them in the script? Unless you are redirecting it somewhere (sending it to a def()), it will run line by line, so the order is the same. You are not redirecting, so it will do line by line, except, of course where you loop through using for statements. I suspect that you are running into this issue for append: Specifies if the schema (field definitions) of the input datasets must match the schema of the target dataset in order for data to be appended. â?¢TEST â??Input dataset schema (field definitions) must match the schema of the target dataset. An error will be returned if the schemas do not match. â?¢NO_TEST â??Input dataset schema (field definitions) do not have to match that of the target dataset. Any fields from the input datasets that do not match the fields of the target dataset will not be mapped to the target dataset unless the mapping is explicitly set in the Field Map control. First, have you ensured that there is an existing point FC named C:\TEMP\PDX_SAB\PDX_SAB.gdb\PDX_SAB_Centerpoint that has two text fields (and is in writeable workspace, and not being viewed in ArcMap or Catalog by anyone (otherwise, lock file..)). One named ProjectNum with size 20 and one named OriginalPath with size 255 And then try this for you append line
arcpy.Append_management(outFC, appendFC, "NO_TEST", "", "") see if that gets past the schema issues. With the NO_TEST, it should copy the attributes that match exactly. R_
... View more
08-01-2013
10:32 AM
|
0
|
0
|
2307
|
|
POST
|
Have you applied service pack 1? I don't have a copy without SP1 to test, but, with 10.1, service pack 1 on an empty mxd OR an mxd with all the data sources broken, I can run both these commands without error. Tried it on two different computers, works just fine in both. R_
... View more
08-01-2013
09:33 AM
|
0
|
0
|
2784
|
|
POST
|
Ben, I don't use sde or subtypes, so can't really test it. I'd look here: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/A_quick_tour_of_using_SQL_with_ArcSDE_geodatabases/006z00000008000000/ and the sub folders under that link. Might get you what you need. R_
... View more
08-01-2013
09:04 AM
|
0
|
0
|
3946
|
|
POST
|
the error is related to your expression. Often I find it is easier to create the expression string as a variable and pass this way. this is working for me: import arcpy, os #Set the workspace in_workspace = r"D:\Projects" appendFC = r"C:\TEMP\PDX_SAB\PDX_SAB.gdb\PDX_SAB_Centerpoint" # make an in memory dataset its faster. no need to write to disk if you are just deleting it. outFC = "in_memory\\centerPoints" with open(r'D:/Projects/temp.txt','w',0) as f: # create list of files from directories and subdirectories for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="Polygon"): # disregard any folder named "ARCHIVE" in creating list if "ARCHIVE" in dirnames: dirnames.remove('ARCHIVE') for filename in filenames: if filename.startswith("APE") or filename.startswith("SAB"): print os.path.join(dirpath, filename) f.write(os.path.join(dirpath, filename)) f.write("\n") if arcpy.Exists(outFC): arcpy.Delete_management(outFC) # convert to points arcpy.FeatureToPoint_management(os.path.join(dirpath, filename), outFC, "INSIDE") # add field to capture project number arcpy.AddField_management(outFC, "ProjectNum", "TEXT", "", "", 20, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(outFC, "OriginalPath", "TEXT", "", "", 255, "", "NULLABLE", "REQUIRED") # extract ProjectNum from path and calc fields exp = str(os.path.join(dirpath, filename)[9:13]) exp2 = str(os.path.join(dirpath, filename) arcpy.CalculateField_management(outFC, "ProjectNum","exp", "PYTHON_9.3") arcpy.CalculateField_management(outFC, "OriginalPath","exp2" ,"PYTHON_9.3") # delete fields; use ListFields to get a list of field objects fieldObjList = arcpy.ListFields(outFC) # Create an empty list that will be populated with field names fieldNameList = [] # For each field in the object list, add the field name to the # name list. If the field is required, exclude it, to prevent errors for field in fieldObjList: if not field.required: fieldNameList.append(field.name) # delete field arcpy.DeleteField_management(outFC, fieldNameList) # append to final FC arcpy.Append_management(outFC, appendFC) f.write("done with script \n") f.close I took the liberty to add the file writes to your script. Basically, after the with open statement, the file is open for writing as "f". so, anytime between the with open, and the f.close if you do a f.write() statement, it will write it to the text file. f.close has to be at same indentation as the with open. You now have example of how to write a variable, a new row, and text (with new row). R_ Also, do you use modelbuider? For this kind of script, my initial approach would be to create a model in arcmap to perform this on just one FC and not worry about iterating. So, in the model, I would pull in the FeatureToPoint tool and convert to in_memory FC then I'd pull in two AddField tools and configure to create both the fields then pull in two calculateField tools and configure both then would pull in the Append tool and configure to append to the appendFC. now, run the model and make sure it works. If you get errors, fix them until the model runs correctly. Now, File menu, export, export to python script. Now you have a starting point, with the proper syntax for most the tools and all you have to do is add the da.walk iterator stuff to make it work on multiples.
... View more
07-31-2013
04:24 PM
|
0
|
0
|
2307
|
|
POST
|
Also, if you have added a service using the MapServices widget, it will "confuse" things as well. Well, just tested this a little more. have a fv34 app with nav, overview, coordinate, headercontroller and layerlist widgets, and no others. I still get this weird behaviour. If I click, move down, it works fine, if I tell the same layer to move down again, or for that matter, tell it to move up, it will jump down 5 positions. Doesn't matter which way I tell it to move, after the second click, it will jump down like 5 positions. Once it moves all the way to the bottom, the move up will work again twice, then will ignore all the move ups you send it until you move down again.. R_ seems to do it in fv33 as well. However, my FV31 project works fine until I add a layer using MapServices widget, and load at bottom, then it behaves the similar
... View more
07-31-2013
03:45 PM
|
0
|
0
|
1454
|
|
POST
|
Like I said, I don't make script tools, but I do use con now and then, I just use arcpy for it. http://resources.arcgis.com/en/help/main/10.1/index.html#//009z00000005000000 R_
... View more
07-31-2013
03:16 PM
|
0
|
0
|
2953
|
|
POST
|
import arcpy
from arcpy import env
from arcpy import mapping
workspace = arcpy.env.workspace = "C:\GIS\MAPBOOK\Proposed Zoning Book"
arcpy.env.overwriteOutput = True
mxdList = arcpy.ListFiles("*.mxd")
for mxd in mxdList:
mxd = workspace + "\\" + mxd
print mxd + " is being processed"
mxd = arcpy.mapping.MapDocument(mxd)
mxd.findAndReplaceWorkspacePaths(r"C:\Documents and Settings\***\Application Data\ESRI\ArcCatalog\***.sde",r"C:\Users\***\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DBO.***.VECTOR.sde")
print "Successfully updated data sources"
mxd.save(r'new_mxd_filename.mxd'))
Not sure, looks like it should be saving it. Wonder if the findAndReplaceWorkspacePaths is confusing it in a loop since it is a global command. Try the above code, see what happens. If that doesn't work, I'd try mxd.saveACopy() and see if the new file is written, if so, if the paths are updated appropriatly. R_ Your listing of the mxds is much cleaner now..
... View more
07-31-2013
02:51 PM
|
0
|
0
|
3522
|
|
POST
|
Could put something like this in there:
if not arcpy.Exists(filename):
continue
this way, if the filename doesn't exist, it will move on to the next iteration. also, keep in mind that when using da.walk, the actuall path to and including the filename is os.path.join(dirpath,filename) not just "filename", especially important if the file is not in your set env.workspace. there are some posts here that also show how to code for ( if not in something, then do something) http://forums.arcgis.com/threads/89638-List-features-in-multiple-workspaces-then-convert-to-point-BUT-ignore-some-paths that might come in handy. as far as the script tool issues, can't help you there, I do all my stuff stand alone. R_
... View more
07-31-2013
02:43 PM
|
0
|
0
|
2953
|
|
POST
|
First, are you running the toolbox tool, or running code? If coding, there is normally a way to tell it to move on if it can't find the file. However, since you have to tell it what file(s) to use, normally, you just don't pass a non-existent file to it. also, not sure why you are getting that error as the only valid inputs for the con tool are rasters, so it should never be looking for a table, yet alone in an mdb. R_
... View more
07-31-2013
01:39 PM
|
0
|
0
|
2953
|
|
POST
|
Thanks! I had just discovered the dirnames.remove feature. And yes, ARCHIVE is a subdirectory. I am getting an error though: "ExecuteError: ERROR 000210: Cannot create output \\in_memory\centerPoints Failed to execute (FeatureToPoint)." Also, how do I get it to print (maybe to a text file?) the paths? I want to use that to make sure I'm only appending the ones I want, in case I need to add additional exceptions (like the ARCHIVE folder). Do I just put 'print filename' after the if filename startswith line? My bad, try this:
outFC = "in_memory\\centerPoints"
yes, print filename after the startswith line at the same indentation level as the if ArcpyExists to print to the console. could put print os.path.join(dirpath, filename) if you want the entire path/filename. To print to a text file, you need to open it in write mode and write to it. Here is an example of some of my code that writes results to a text file.
with open('D:/update/temp.txt','a',0) as f: ### the "a" tells it to append to existing file. "w" for write would create a new file, even if it exists.
print dt
f.write(dt)
f.write("\n\nthis is first line of image_tables.py \n\n")
# Process: Delete
if arcpy.Exists(Images_full):
print "deleting ",Images_full
f.write("\ndeleting ")
f.write(Images_full)
arcpy.Delete_management(Images_full, "Table")
if arcpy.Exists(WasteSitePoly_centroids):
print "deleting ",WasteSitePoly_centroids
f.write("\ndeleting ")
f.write(WasteSitePoly_centroids)
arcpy.Delete_management(WasteSitePoly_centroids)
f.close # at the same indentation level as the with open line
Since I opened it as "f" (with open as f), then f.write are the statements that write to the file, the print just echos to the screen, so, what is printed to the screen is also written to the text file. a print statement automatically indexes to a new line when you hit enter, the f.write statements, the \n adds a new line, otherwise, it just concatenates one long line. R_
... View more
07-31-2013
12:49 PM
|
0
|
0
|
2307
|
|
POST
|
No, you would just get a not licesed error if that were the case. If you have too much security, you will get unable to run errors as they rely on ActiveX being enabled now. If it is not finding it, even after a re-boot of the computer, you may need a re-install. Does it not find them in ArcMap or ArcCatalog? That is a weird one, as if it is showing you the list under Conditional, then it has to be seeing the Spatial Analyst Tools.tbx... R_
... View more
07-31-2013
12:34 PM
|
0
|
0
|
3905
|
|
POST
|
Just at a glance, I would think that your mxd is not getting set correctly. maybe the fullpath variable isn't set correctly? what does a print fullpath give you? what does >>>MXD.filePath give you? It looks like you are wanting to replace the data source on all layers within the mxd(s). No need to itereate if that is the case, the findAndReplaceWorkspacePaths function does that automatically, and is simpler to code (don't need to specify type) and will replace in all layers in the mxd. Also, you might find it easier to get the mxd's using os.walk rather than listDirs, the entities are already parsed, and you only need to join the path/filename. Would suggest the arcpy.da.walk, but there is a bug in that where it won't report map documents (though the docs say it will). R_
... View more
07-31-2013
12:16 PM
|
0
|
0
|
3522
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 05-14-2026 04:00 PM | |
| 1 | 09-14-2022 07:53 AM | |
| 1 | 09-14-2022 08:23 AM | |
| 1 | 05-21-2026 08:53 AM | |
| 1 | 05-14-2026 04:28 PM |
| Online Status |
Offline
|
| Date Last Visited |
Wednesday
|