POST
|
Thanks for the response - obviously that was not what I wanted to here.
... View more
08-08-2013
09:15 AM
|
0
|
0
|
406
|
POST
|
Perhaps I did not express myself clearly - I do not have SDE. I am dealing with a file gdb. When I export the table to Access then I can easily extract what I want but within Arc and python I am totally lost.
... View more
08-08-2013
09:04 AM
|
0
|
0
|
422
|
POST
|
Python Beginner - Using version 10.1 and not an SDE install I have an atribute table with firehydrant inspection data. I am trying to create a layer that will only show the records grouped by fire hydrant number and then only display the most recent inspection date. When I execute the following code it runs fine and does in fact create the layer but it doesn't select the Max Date. If I have a record for Hydrant 100 with two dates I only want the most recent date to be created within the new layer. Appreciate any assistance on this. import arcpy
from arcpy import env
env.workspace=r"Z:\GIS_Data\gdb\GIS_Test.gdb"
fc="COA_FH_Inspections"
cursor = arcpy.UpdateCursor(fc)
idlist = []
datelist = []
for row in cursor:
idlist.append(row.getValue('HydrID'))
datelist.append(row.getValue('TestDate'))
cursor.updateRow(row)
print idlist
print datelist
idunique = set(idlist)
#Make a layer from the feature class
arcpy.MakeFeatureLayer_management(fc, "lyr")
for i in idunique:
if i == idlist:
arcpy.SelectLayerByAttribute_management("lyr", "New_Selection", '"HydrID" = i')
arcpy.SelectLayerByAttribute_management("lyr", "subset_Selection",'"TestDate" = (Select Max("TestDate") from "lyr"') Again thank you - Terry
... View more
08-08-2013
07:42 AM
|
0
|
1
|
3389
|
POST
|
New at Python - appreciate assistance pointing me in the right direction. Using version 10.1 - not SDE environment I need to group an attribute table of inspection data and only select the most recent date. The python script below does not provide the data I need. It executes and creates a layer but it has the same records as if I didn't run the script. Example if I have 5 records for ID 100 I only want to return the record with the most recent date. I have roughly 500 firehyrdrants and want to only select the distinct hydrant ID and then the most recent inspection date. Thanks in advance import arcpy
from arcpy import env
env.workspace=r"Z:\GIS_Data\gdb\GIS_Test.gdb"
fc="COA_FH_Inspections"
cursor = arcpy.UpdateCursor(fc)
idlist = []
datelist = []
for row in cursor:
idlist.append(row.getValue('HydrID'))
datelist.append(row.getValue('TestDate'))
cursor.updateRow(row)
print idlist
print datelist
idunique = set(idlist)
#Make a layer from the feature class
arcpy.MakeFeatureLayer_management(fc, "lyr")
for i in idunique:
if i == idlist:
arcpy.SelectLayerByAttribute_management("lyr", "New_Selection", '"HydrID" = i')
arcpy.SelectLayerByAttribute_management("lyr", "subset_Selection",'"TestDate" = (Select Max("TestDate") from "lyr"')
... View more
08-08-2013
06:29 AM
|
0
|
2
|
906
|
POST
|
I rarely use tools/models from within ArcMap other than to write my initial python code for me, so probably not the best to answer this question. However, the few that I do run through ArcMap/Toolbox, I right-click on the model script itself, select properties, and parameters. this is where you set the parameters that get passed to arcpy when run. Can hard code, browse button, etc. Another way to pass parameters is as arguments. C:\pythonpath\python.exe scriptfile.py arg1 arg2 arg3 (which would be parameter(0), (1), etc). However, I normally just hard code it in the python script, as it is either static, OR, python tools can iterate/parse/filter/etc. to get the paramaters I need. R_ This is how I intially was passing the parameter, however I hard coded them as you suggested then ran the process. I did get the values I expected to print with the print statement but I still did not have the changes saved. The interesting thing - after I ran it the first time and then checked the mxd to see if the source was changed, and it wasn't I ran the process again. This time however the original datasource created a message stating it was not found. I am so baffled by this and not sure where to go next? If the issue was a problem with how I wrote the save then running the script again should have still found the original datasource, but it didn't. Any additional thoughts on this? In the meantime I am gong to create a new test set and try the hard coded concept.
... View more
06-26-2013
07:03 AM
|
0
|
0
|
261
|
POST
|
It is not getting your parameters, are you passing them along with the command line. If you are not running from a GP tool, you need to either pass the parameters as arguments (Python D:\scriptfile.py d:\myfolder myData OutPut NewSource) or code the paths (folderPath=r"C:\arcgisserver\local_data\"). I normally just put the path to my data in the script and run it. R_ Thanks I am passing the parms through a GP tool. I will try hard coding to see if I can get this to work. Any suggestion on where to search to learn more about debugging why a parameter being passed via a GP tool isn't working correctly?
... View more
06-24-2013
06:29 AM
|
0
|
0
|
261
|
POST
|
import arcpy, datetime, os
try:
#Read input parameters from GP dialog
folderPath = arcpy.GetParameterAsText(0)
dataSource = arcpy.GetParameterAsText(1)
output = arcpy.GetParameterAsText(3)
newSource = arcpy.GetParameterAsText(2)
print folderPath
print dataSource
print output
print newSource
#Loop through ech MXD file
mCnt = 0
for filename in os.listdir(folderPath):
fullpath = os.path.join(folderPath, filename)
if os.path.isfile(fullpath):
if filename.lower().endswith(".mxd"):
#Reference MXD
mxd = arcpy.mapping.MapDocument(fullpath)
#Determine if the data source exists within the data frames/map document
#If exist then update the datasource - TH Testing 06/05/2013
sCnt = 0
for df in arcpy.mapping.ListDataFrames(mxd):
layerList = []
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
if lyr.supports("dataSource"):
if lyr.dataSource == dataSource:
lyr.findAndReplaceWorkspacePath("dateSource", "newSource") #TH Testing
mxd.save() #TH Testing
mCnt = 1
sCnt = sCnt + 1
))
Are you sure your variables are getting set correctly? What happens if you add the changes above in blue? R_ I entered in the lines you recommended however none of them printed... not sure what I am doing incorrect.
... View more
06-20-2013
02:12 PM
|
0
|
0
|
629
|
POST
|
Thanks I didn't catch that typo. Those seem to get me! Will give it a try. Thanks again. Ok I corrected the syntax in regards to the typo. The script ran and correctly produced the report identifying the mxd within the folder with the datasource specified - however it did not update the data source. Any additional ideas on what I might be missing here?
... View more
06-20-2013
07:12 AM
|
0
|
0
|
629
|
POST
|
Perhaps its due to trying to set it to a variable that hasn't been defined? Not sure the exact syntax for that command, but dateSource <> dataSource R_ Thanks I didn't catch that typo. Those seem to get me! Will give it a try. Thanks again.
... View more
06-20-2013
06:24 AM
|
0
|
0
|
629
|
POST
|
Jeff thank you for the response - however I thought this line that I inserted is handling the save? lyr.findAndReplaceWorkspacePath("dateSource", "newSource") #TH Testing mxd.save() #TH Testing
... View more
06-07-2013
10:00 AM
|
0
|
0
|
629
|
POST
|
Ok - I'm a beginner trying to figure out how to modify one of the scripts I located. The original script is designed to search a folder with MXDs and print out report off all the MXDs that contain a particular data source. I want to take this one step further - so if the data source is located then I would like to replace it. I tried adding this to the sample code - lyr.findAndReplaceWorkspacePath("dateSource", "newSource") #TH Testing mxd.save() #TH Testing The script runs but the change doesn't take place or if it does it isn't saved. Any help would be great. # Author: ESRI
# Date: July 5, 2010
# Version: ArcGIS 10.0 Final
# Purpose: This script will iterate through each MXD in a folder and report the
# name of each map document that contains a specific data source. The
# script is intended to run from a script tool that requires three
# input parameters:
# 1) A folder that contains the set of MXDs that will be searched,
# 2) A data source that you want to search for,
# 3) An output text file.
# The resulting text file will automatically open.
import arcpy, datetime, os
try:
#Read input parameters from GP dialog
folderPath = arcpy.GetParameterAsText(0)
dataSource = arcpy.GetParameterAsText(1)
output = arcpy.GetParameterAsText(3)
newSource = arcpy.GetParameterAsText(2)
#Create an output file
outFile = open(output, "w")
#Report header
outFile.write("Data Source Report: \n")
outFile.write("\n")
outFile.write("This report summarizes the names of all map documents within a folder that\n")
outFile.write("contain a specific data source. \n")
outFile.write("\n")
outFile.write("Folder location: " + folderPath + "\n")
outFile.write("\n")
outFile.write("Date: " + str(datetime.datetime.today().strftime("%B %d, %Y")) + "\n")
#Loop through ech MXD file
mCnt = 0
for filename in os.listdir(folderPath):
fullpath = os.path.join(folderPath, filename)
if os.path.isfile(fullpath):
if filename.lower().endswith(".mxd"):
#Reference MXD
mxd = arcpy.mapping.MapDocument(fullpath)
#Determine if the data source exists within the data frames/map document
#If exist then update the datasource - TH Testing 06/05/2013
sCnt = 0
for df in arcpy.mapping.ListDataFrames(mxd):
layerList = []
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
if lyr.supports("dataSource"):
if lyr.dataSource == dataSource:
lyr.findAndReplaceWorkspacePath("dateSource", "newSource") #TH Testing
mxd.save() #TH Testing
mCnt = 1
sCnt = sCnt + 1
layerList.append(lyr.name)
if sCnt == 1: #Write the MXD header once
outFile.write("\n")
outFile.write("\n")
outFile.write("---------------------------------------------------------------------------------- \n")
outFile.write(" MAPDOCUMENT: " + os.path.basename(mxd.filePath) + "\n")
outFile.write("---------------------------------------------------------------------------------- \n")
sCnt = sCnt + 1
if len(layerList) > 0: #Write the data frame name once
outFile.write("\n")
outFile.write("\t Data Frame: " + df.name + "\n")
for lyr in layerList: #Write each layer name
outFile.write("\n")
outFile.write("\t\t Layer: " + lyr + "\n")
del mxd
if mCnt == 0:
outFile.write("\n")
outFile.write("\n")
outFile.write("---------------------------------------------------------------------------------- \n")
outFile.write(" NO DATA SOURCES FOUND \n")
outFile.write("---------------------------------------------------------------------------------- \n")
outFile.close()
#Open the resulting text file
os.startfile(output)
#Delete variable references
del folderPath, dataSource, output, outFile, fullpath
except Exception, e:
import traceback
map(arcpy.AddError, traceback.format_exc().split("\n"))
arcpy.AddError(str(e))
... View more
06-05-2013
01:28 PM
|
0
|
13
|
3565
|
POST
|
Thanks that was exactly the part I was missing. I got it working now and learned a lot along the way.
... View more
06-05-2013
12:32 PM
|
0
|
0
|
183
|
POST
|
Ok I am at a loss on how to use the UpdateMXDTags script. Based on the comment section it states the script is intended to run from a script tool and requires two input parameters. I created a toolbox and placed the script inside. When I open up ArcCatalog and double click on the script it pops up with "This Tool has no parameters". I expected the input window to appear to enter in the parameters. Obvious I am not understanding how to use this. I am an absolute beginner trying to take some sample scripts and apply them to my work as a learning process. Note it says this was written for 10.0 - I'm running 10.1. It was downloaded from the SampleArcPyMappingScriptTools Thanks Terry
... View more
06-05-2013
07:26 AM
|
0
|
2
|
1899
|
POST
|
Thanks I got it to work. I would like to enhance this script to do some additional things but not sure how to approach. Any chance you could assist on this and point me in the right direction? 1.) Locate a particular FC within the MXD and it's data source Example - we receive an updated shapefile from the county and want to verify all MXD data sources are pointing to the correct file. I am trying to get all MXDs to use the same geodatabase. 2.) Only report on MXDs that have broken data sources
... View more
05-31-2013
07:51 AM
|
0
|
0
|
220
|
POST
|
First let me state that I am not very experienced with Python. I had captured a script from ESRI that I think I might have tweaked. However the script worked fine to produce output in a text file. Now when I run it whether intereactive or from a script all I get is a listing of the MXDs within the interactive window and in the text file I basically get the header information and nothing more. The last time I ran this was in August last year. Can someone assist me in identifing what I am doing wrong or what might have changed? Note I am running 10.1 on Win7 Thanks in advance Terry
... View more
05-29-2013
11:50 AM
|
0
|
3
|
587
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:24 AM
|