|
POST
|
Hi Jason, Python looks in the directories that are part of the module search path. These directories are listed in the sys.path variable from the sys module. To list where the Python looks for modules, print out the value of the sys.path variable. For example, type the following in pythonwin's interactive window: import sys
print sys.path By default, arcpy's default location is: C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy or C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy If the path isn't there, try adding the path using the sys.path.append method. Ex: sys.path.append(r"C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy")
... View more
09-27-2012
04:41 AM
|
0
|
0
|
684
|
|
POST
|
Here is an example of some code that I got to work after running the Fishnet tool. I created a two new fields called 'ROW' and 'COLUMN_' and then used the following functions to update them: fc = "fishnet" def updateROW(x, y, z, n): while x <= 25: rows = arcpy.UpdateCursor(fc) for row in rows: if row.OID == x and x > y and x <= z: row.ROW = n x += 1 rows.updateRow(row) else: y += 5 z += 5 n += 1 row.ROW = n x += 1 rows.updateRow(row) def updateCOLUMN(): x = 1 n = 1 while x <= 25: rows = arcpy.UpdateCursor(fc) y = x for row in rows: if row.OID == y and y <= 25 and n < 6: row.COLUMN_ = n y += 5 rows.updateRow(row) x += 1 n += 1 updateROW(1, 0, 5, 1) updateCOLUMN() You will need to update some of the variables depending on the size of your Fishnet. The above example is executed on a 5x5 fishnet. For the updateROW function, the 'while' loop is the total number of polygons, and the 'y' and 'z' increment values are based on the number of rows. For the updateCOLUMN function, the 'while' loop is the total number of polygons, the 'y' variable in the 'if' statement is the total number of polygons, 'n < 6' represents one less the number of columns, and the 'y' variable is incremented by the number of columns. The only problem with the above code is that it starts at the bottom left corner of the fishnet feature class rather than the top left. You would need to populate a field with values of the OID field in descending value, then use that field in the function. I have not thoroughly tested this, and there is a probably a more elegant way to do this, but this may help you get started.
... View more
09-25-2012
11:22 AM
|
0
|
0
|
3152
|
|
POST
|
Hi John, According to the code you posted, you are specifying 'sde.default' (2nd to last parameter). Try changing this to dbo.default.
... View more
09-25-2012
04:16 AM
|
0
|
0
|
1441
|
|
POST
|
Hi John, Are you specifying the 'version' parameter? Ex: arcpy.CreateArcSDEConnectionFile_management("C:/Projects","SQLEXPRESS", "SDE_SERVER","sde:sqlserver:SDE_SERVER/sqlexpress","Data","OPERATING_SYSTEM_AUTH","#","#","SAVE_USERNAME","dbo.DEFAULT","SAVE_VERSION")
... View more
09-25-2012
03:45 AM
|
0
|
0
|
1441
|
|
POST
|
Hi James, How was the polygon GRID created? Did you use the 'Create Fishnet' tool in ArcToolbox? If not, is there an ObjectID that increments from left to right? And, is there an equal amount of rows and columns?
... View more
09-24-2012
12:09 PM
|
0
|
0
|
3152
|
|
POST
|
Here's an example on how to write the current windows user and current date and time to a text file: import getpass, datetime f = open(r"C:\temp\python\logfile.txt", "a") user = getpass.getuser() date = str(datetime.datetime.now()) f.write("Username:\t" + user + "\n") f.write("Date:\t" + date + "\n") f.close()
... View more
09-24-2012
11:58 AM
|
0
|
0
|
1359
|
|
POST
|
Converting to string then back to integers can be tricky. I ran into to some unsuspected errors when testing this, but I was able to get the following to work: def Reclass(contour):
newContour = str(contour) #string will have decimal
newContour = newContour.split(".")[0]
try:
newContour[-2:]
if newContour[-2:] == '10':
return 10
except IndexError:
pass
if newContour[-1] == '5':
return 5
else:
return 1 Reclass(!Contour!)
... View more
09-24-2012
08:12 AM
|
0
|
0
|
2395
|
|
POST
|
You can easily write the output to a text file. Take a look at the examples here. Also, here is an updated example of the script posted earlier: logpath = r"C:\temp\python\file.txt"
if os.path.exists(logpath):
os.remove(logpath)
f = open(logpath, "w")
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for layer in arcpy.mapping.ListLayers(mxd, '', df):
if layer.name.lower() == "cities":
arcpy.MakeFeatureLayer_management(layer, "cities_lyr")
arcpy.SelectLayerByLocation_management("cities_lyr", "INTERSECT", "point", "30 FEET", "NEW_SELECTION")
rows = arcpy.SearchCursor("cities_lyr")
for row in rows:
f.write(row.NAME)
del row, rows
f.close() Note: Be sure to import the 'os' module if you are going to check if the file already exists.
... View more
09-24-2012
03:43 AM
|
0
|
0
|
1788
|
|
POST
|
Hi Alison, You could create a python script to update the tables. After the editors are finished updating the Roads feature class, they could run a script to update the corresponding table. Here's an example: import arcpy
from arcpy import env
env.workspace = r"Database Connections\ORCL.sde"
fc = "Roads"
table = "Addresses"
fcList = []
rows = arcpy.SearchCursor(fc)
for row in rows:
fcList.append(row.getValue("CODE"))
del row, rows
tableList = []
rows = arcpy.SearchCursor(table)
for row in rows:
tableList.append(row.getValue("CODE"))
del row, rows
for n in fcList:
if n not in tableList:
rows = arcpy.InsertCursor(table)
row = rows.newRow()
row.CODE = n
rows.insertRow(row)
try:
del row, rows
except NameError:
pass The script above compares the Road features within the Primary Key (CODE) to the values within the Foreign Key (CODE) for the Addresses table. If the value is not present in the Addresses table it will insert a record with this value. The script can be added to a toolbox, and when the editor is finished editing, the user can execute the script by double-clicking it. Or, if you are running ArcGIS 10.1 you could create a python add-in and have the user click a button from a toolbar once their edit session is complete.
... View more
09-20-2012
11:14 AM
|
0
|
0
|
839
|
|
POST
|
Hi Diana, Here is a script that I was able to get to work: import arcpy
from arcpy import env
env.overwriteOutput = 1
env.workspace = r"C:\temp\python\test.gdb"
lstFCs = arcpy.ListFeatureClasses("*")
for fc in lstFCs:
lstFields = arcpy.ListFields(fc)
for field in lstFields:
if field.domain:
print fc + ": " + field.name + ": " + field.domain
arcpy.DomainToTable_management(env.workspace, field.domain, "tb_" + field.domain, "code", "description")
rows = arcpy.SearchCursor(fc, "", "", field.name)
listValues = []
for row in rows:
fieldValues = row.getValue(field.name)
listValues.append(fieldValues)
del row, rows
rows = arcpy.SearchCursor("tb_" + field.domain, "", "", "code")
listCodes = []
for row in rows:
fieldCodes = row.getValue("code")
listCodes.append(fieldCodes)
del row, rows
for n in listValues:
if n not in listCodes:
print fc + " contains field " + field.name + " with domain " + field.domain + " containing value " + n + " that is not a coded value"
lstTables = arcpy.ListTables("tb_*")
for table in lstTables:
arcpy.Delete_management(table) What the script does is create a table for any domain found being used by a feature class within the geodatabase. It then appends the field values to a list and compares these values to the values within the table previously created. If the field contains a value that is not a coded domain value, it will report the feature class, field name, domain, and the corresponding value.
... View more
09-20-2012
03:39 AM
|
0
|
0
|
3409
|
|
POST
|
Hi Anton, Is this for a label expression? If so, you can use: "CTE: " + [CTE] + "\n" + "Omschr: " + [OMSCHR] Be sure that the Parser is set to 'Python'.
... View more
09-20-2012
02:40 AM
|
0
|
0
|
541
|
|
POST
|
In the above example, the script creates a shapefile and then removes it at the end using the 'arcpy.Delete_management' function. However, you can create an IN_MEMORY feature class. This is a temporary feature class. You would need to update the following lines: arcpy.CopyFeatures_management(ptGeometry, r"C:\temp\python\POINT.shp")
arcpy.MakeFeatureLayer_management(r"C:\temp\python\POINT.shp", "point") to: arcpy.CopyFeatures_management(ptGeometry, "IN_MEMORY/POINT")
arcpy.MakeFeatureLayer_management("IN_MEMORY/POINT", "point") The reason I used a shapefile was the problem with the bug mentioned before. At 10.0, using an IN_MEMORY feature class should work. More information about IN_MEMORY feature classes can be found here.
... View more
09-19-2012
12:00 PM
|
0
|
0
|
1788
|
|
POST
|
Hi Nancy, Try the following in the field calculator: d = {}
def findDuplicates(inVal):
try:
d[inVal] += 1
return d[inVal]
except KeyError:
d[inVal] = 1
return 1 findDuplicates(!FieldA!) Be sure 'Python' is checked within the Field Calculator.
... View more
09-19-2012
11:15 AM
|
0
|
0
|
1600
|
|
POST
|
Take a look at my previous example. Instead of writing the output to a shapefile you can easily write it to a feature class instead.
... View more
09-19-2012
11:09 AM
|
0
|
0
|
1788
|
|
POST
|
Glad you got it to work! I originally tested this with 10.1, but could not get it working. I tried 10.0 and it worked. This may be a bug with 10.1.
... View more
09-19-2012
10:29 AM
|
0
|
0
|
1788
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 3 weeks ago | |
| 4 | 05-07-2020 05:14 PM | |
| 1 | 03-25-2026 04:16 AM | |
| 1 | 03-16-2026 01:00 PM | |
| 1 | 12-22-2025 10:39 AM |
| Online Status |
Offline
|
| Date Last Visited |
yesterday
|