|
POST
|
Maybe it can't describe the name of a folder? Sorry I wasn't clear, it is blowing up on me... here is the error: Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> File "<string>", line 26, in import_to_GDB File "<string>", line 2, in rename File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\__init__.py", line 1200, in Describe return gp.describe(value) File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 374, in describe self._gp.Describe(*gp_fixargs(args, True))) IOError: "Buffer" does not exist
... View more
04-09-2015
11:09 AM
|
0
|
0
|
1493
|
|
POST
|
Hi All, I am trying to create a True Statement to check if a definition passed the "if" On line 19 i am running "DEF RENAME" (Changes the name so the tool will run and I can fix it later) Line 20 I want to know if Rename passed the if statement My code is to change folders and shapefiles into a GDB while maintaining the structure of the folders. Please let me know if you have advice or see a large issue. The way I run this is through the command line. def rename(variable):
desc = arcpy.Describe(variable)
name = desc.name
new_name = "renamed"
print name
if " " or "-" or "0" or "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8" or "9" in name:
print("{0} is being changed to: {1}".format(name, new_name))
arcpy.Rename_management(variable, new_name)
else:
pass
def import_to_GDB(input_workspace, output_workspace):
import os
base = os.path.basename(input_workspace)
arcpy.CreateFileGDB_management(output_workspace, base)
for dirpath, dirnames, filenames in arcpy.da.Walk(input_workspace):
for dirname in dirnames:
try:
test = rename(dirname)
if test == True:
dirname = "renamed"
dir_path = os.path.join(dirpath, dirname)
arcpy.env.workspace = dir_path
fcs = arcpy.ListFeatureClasses()
fds = os.path.join(output_workspace, base + ".gdb\\")
arcpy.CreateFeatureDataset_management(fds, dirname)
suboutput = os.path.join(fds, dirname)
count = 0
else:
dir_path = os.path.join(dirpath, dirname)
arcpy.env.workspace = dir_path
fcs = arcpy.ListFeatureClasses()
fds = os.path.join(output_workspace, base + ".gdb\\")
arcpy.CreateFeatureDataset_management(fds, dirname)
suboutput = os.path.join(fds, dirname)
count = 0
except:
print("{0} had an error, passing to the next object".format(dirname))
for fc in fcs:
count = count + 1
try:
arcpy.FeatureClassToGeodatabase_conversion(fc, suboutput)
print("{0} Complete".format(count))
except:
print("{0} had an error, passing to the next object".format(fc))
... View more
04-09-2015
10:06 AM
|
0
|
6
|
4807
|
|
POST
|
Ahmed, I have a python script that searches through a directory/geodatabase and creates KMZ files. What it does is create an replica of the input workspace(folder/geodatabase) and produce .kmz files. Not sure how familiar you are with python but I will copy my code here: def export_to_kml(input_workspace, output_workspace):
"""
This function extracts the feature classes from a geodatabase
and converts them to .kmz files directory structure is maintained
by folders being created based on the feature dataset.
input_workspace ==> Path to a: Geodatabase, Folder which contains
Feature Classes or Shapefiles. (EX: 'H:\GIS DATA\Counties.gdb')
output_workspace ==> Path to the location you want the .kmz files
to go. (EX: 'H:\GIS DATA')
"""
# Searches through all sub-directories within the input workspace
import os
for dirpath, dirnames, filenames in arcpy.da.Walk(input_workspace):
for dirname in dirnames:
# vars
dir_path = os.path.join(dirpath, dirname)
arcpy.env.workspace = dir_path
fcs = arcpy.ListFeatureClasses()
print("Processing Feature Dataset/Folder:{0}\nFeature Classes/Shapefiles:{1}\n".format(dir_path, fcs))
arcpy.CreateFolder_management(output_workspace, dirname)
suboutput = os.path.join(output_workspace, dirname)
count = 0
for fc in fcs:
print("Converting: {0}".format(fc))
count = count + 1
layer = 'layer' + fc
out_kmz_file = os.path.join(suboutput, fc + '.kmz')
layer_output_scale = 0
is_composite = 'false'
boundary_box_extent = '#'
image_size = 1024
dpi = 300
zvalue = 'CLAMPED_TO_GROUND'
arcpy.MakeFeatureLayer_management(fc, layer) # Creates a layer to convert to the .kmz
arcpy.LayerToKML_conversion(layer, out_kmz_file, layer_output_scale, is_composite,
boundary_box_extent, image_size, dpi, zvalue)
print("{0} complete\n".format(count)) The way that you can run this is by opening the python window and copy and pasting this script(attched a .txt to make it easy) into the command line. from there you can type out: >>>export_to_kml('H:\input\geodatabase.gdb', 'C:\Users\XXXXX\Desktop\kmz')
# first value is your input folder/gdb second value is your output location Let me know if you need any help with it Also this will create the kmz file with the orginal name of your shapefile/feature class
... View more
04-07-2015
10:22 AM
|
1
|
0
|
2515
|
|
POST
|
The split did the trick, i tried that earlier but i believe that i put a ',' not a ';' in. Thanks for the help
... View more
03-04-2015
05:21 AM
|
0
|
0
|
2504
|
|
POST
|
On my Testing script it looks like this fields = arcpy.GetParameterAsText(1) # Multiple Input Script Tool if i do arcpy.AddMessage(fields) the output is: FID;Latitude;Longitude # The Three selected fields if I try to take my "fields" parameter and insert it into my cursor, it pops an error on the for loop line: RuntimeError: A column was specified that does not exist. with arcpy.da.SearchCursor(inFeat, [fields]) as cursor:
for row in cursor:
arcpy.AddMessage("{0} | Phase: {1} | OH/UG: {2}".format(row[0], row[1], row[2])) This is what im working with Hopefully that is better information for you
... View more
03-03-2015
01:30 PM
|
0
|
1
|
2504
|
|
POST
|
I have a script tool where it takes an input table and "MULTIPLE INPUT FIELDS" It then runs a cursor and all i want to do is print out each row with the 3 field values. If i do 3 parameters for the fields, it works. If i do the mutliple it wont. I tried printing out the value and it looked like this: fields = "ID;Lat:Long" I guess it needs to be in list format to run through the cursor as a row? Here is my code inFeat = arcpy.GetParameterAsText(0)
fields = arcpy.GetParameterAsText(1)
with arcpy.da.SearchCursor(inFeat, [fields]) as cursor:
for row in cursor:
arcpy.AddMessage(row[0], row[1], row[2])
... View more
03-03-2015
12:45 PM
|
0
|
5
|
6511
|
|
POST
|
Good information. I still consider myself a Novice at this by I am trying to absorb as much as possible. Thanks Darren Wiens and Tom
... View more
02-26-2015
12:10 PM
|
0
|
0
|
2291
|
|
POST
|
I didn't realize that it was that easy.... haha. Here I was thinking I needed to do a bunch of stuff in the loop to make this work, Thanks!
... View more
02-26-2015
10:33 AM
|
0
|
4
|
2291
|
|
POST
|
As I dive further into Python, I find myself making a "GIS Module" that calls functions that are useful to my work. I created a quick find and replace function but i want to print off the total number items replaced. I also have a show unique value function that lists all the unique items in a field and their values. I want to include just a simple line like: 547 items replaced here are my 2 functions can anyone explain how to include this: def field_counter(table,fieldname):
values = [r[0] for r in arcpy.da.SearchCursor(table, [fieldname])]
unique_values = set(values)
count_dict = {}
for value in unique_values:
count_dict[value] = values.count(value)
print("{0} Unique Value: {1} has {2} attribute(s)".format(fieldname, value, count_dict[value]))
def find_and_replace(table, field, find, replace):
with arcpy.da.UpdateCursor(table, [field]) as cursor:
for row in cursor:
if row[0] == find:
row[0] = replace
cursor.updateRow(row)
print(row[0] + ' was replaced by' + replace)
elif row[0] != find:
print(row[0] + ' was not replaced')
... View more
02-26-2015
10:17 AM
|
0
|
7
|
5770
|
|
POST
|
Maybe I should just remove the print for the elif statement, it just fills my command line up with excess. Thanks for the response.
... View more
02-18-2015
07:03 AM
|
0
|
0
|
483
|
|
POST
|
So I got my script to work but it is still giving me a problem. When i run the script through command line it is printing out the elif clause about 10-15 times each before it starts to delete fields. Anyone have any advice? import arcpy
fieldList = ['ID','LAT','LON','AREA','PARIMETER','Hectares','CODE',
'WEBCODE','DATE_','TIME_','SURVEYOR','TYPE','REV_DATE',
'DATA_SRC','PATTERN','SWATH_DIST','VIEW_DIRC','CONDITIONS',
'CONFIDENCE','RPT_YR','SURVEY_ID1','SURVEY_ID2','SURVEY_ID3',
'DMG_TYPE1','DMG_TYPE2','DMG_TYPE3','SEVERITY1','SEVERITY2',
'SEVERITY3','PATTERN1','PATTERN2','PATTERN3','TPA1', 'TPA2',
'TPA3','NO_TREES1','NO_TREES2','NO_TREES3','NOTES','NOTE',
'DAMAGE94_','DAMAGE94_I','STATUS','MODIFIIER','ACRES',
'PESTNUM','MODNUM','GRP1','GRP2','GRP3','SURVEY_YR','RPT_RGN',
'R295_DMG_I','R295_DMG_','R2_PC3','R2_PC2','R2_PC1','R201_DMG_',
'R201_DMG_I','AREA_METER', 'SOURCETHM','PCT_MORT1','PCT_MORT2',
'PCT_MORT3']
arcpy.env.workspace = r'H:\GIS DATA\Data\Pine Beetle\Beetle\Test'
fcs = arcpy.ListFeatureClasses()
for fc in fcs:
for i in fieldList:
x = arcpy.ListFields(fc)
for a in x:
if i == a.name:
arcpy.DeleteField_management(fc, i)
print i + ' was deleted from ' + fc
elif i != a.name:
print i
... View more
02-18-2015
06:31 AM
|
0
|
2
|
3009
|
|
POST
|
Yes Darren, My orginal script was in a Python Toolbox. This is my plan maybe later down the road but it seems like everytime the new data comes out, there is a change so I have to adjust something. Matt
... View more
02-12-2015
05:15 AM
|
0
|
0
|
1294
|
|
POST
|
Blake T, Thanks for the response, this is the exact information that I am looking for. Right now I am at the point where I can get things to "work". I want to get more in line with the right styles and methods of doing things. The inline comments are mainly for the other people who may not have coding experience to help understand what is going on. My personal version of this contains far less comments. Also great information on the import situation. I always wondered why most people did not use the multiple import method. Thanks!
... View more
02-11-2015
01:07 PM
|
0
|
0
|
1294
|
|
POST
|
Hey everyone, Wanted to ask a generic but help question of the community. Currently I have been learning Python on my own and have just completed what I call my first "real world use" script for my company. It is a pretty basic script but am trying to get better. This script could be used by multiple people who don't know anything about python. I wanted to see for them, and myself if there are improvements that I could make to make this script more robust and "user friendly". I am using all this as a learning process. Any help, ideas, opinions, comments would be fantastic. -Matt
import time, sys, platform, imp, arcpy
start = (time.strftime('%x %X')) # Start time
##### MUST EDIT THESE INPUTS ##### MUST EDIT THESE INPUTS ##### MUST EDIT THESE INPUTS ##### MUST EDIT THESE INPUTS #####
arcpy.env.workspace = 'insert path' # Location of output files
newInput = 'insert path' # Input File (New Year)
clipFeat = 'insert path' # Clip Feature
eraseFeat = 'insert path' # Previous years merge
x = '2014' # Change for current year
year = str(x) # Input file year (Change only the year)
outClip = 'r' + x + 'C' # Clip Output
outLayer = 'Query_Layer' # Layer output (Used to run the query)
outFc = 'Query_' + outClip # Shapefile output from the Layer file
eraseOut = 'r' + x + 'E' # Erase output
arcpy.env.overwriteOutput = 1 # Allows for output file overwriting
messageList = [] # Message list for the log file
log = 'insert path' # Creates a .txt doc with a log of the geoprocessing tools run (Edit path)
##### MUST EDIT THESE INPUTS ##### MUST EDIT THESE INPUTS ##### MUST EDIT THESE INPUTS ##### MUST EDIT THESE INPUTS #####
# Clip file to specified parameter
arcpy.Clip_analysis(newInput, clipFeat, outClip)
messageList.append(arcpy.GetMessages())
# Create a layer to add to Table of Contents
newLyr = arcpy.MakeFeatureLayer_management(outClip, outLayer)[0]
messageList.append(arcpy.GetMessages())
# Add year field and Calculate Field
arcpy.AddField_management(outClip, "Year", "TEXT", 0, "", 10, "","NULLABLE", "NON_REQUIRED","")
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, 'Year', year, "PYTHON_9.3", "")
messageList.append(arcpy.GetMessages())
# Add Fields and run USDA QUERY on the Layer
arcpy.AddField_management(outLayer, "BType", "TEXT", 0, "", 10, "","NULLABLE", "NON_REQUIRED","")
messageList.append(arcpy.GetMessages())
arcpy.AddField_management(outLayer, "MPB_Only", "TEXT", 0, "", 10, "","NULLABLE", "NON_REQUIRED","")
messageList.append(arcpy.GetMessages())
arcpy.AddField_management(outLayer, "Aspen_Dec", "TEXT", 0, "", 10, "","NULLABLE", "NON_REQUIRED","")
messageList.append(arcpy.GetMessages())
# Query the data, based on the provided information from the USDA (Forest Service)
# MPB
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' "DCA1" = 80003 OR "DCA2" = 80003 OR "DCA3" = 80003 OR "DCA1" = 11006 OR "DCA2" = 11006 OR "DCA3" = 11006 ')
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "MPB_Only", 'str("MPB")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")
# LP
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' ("DCA1" = 11006 AND "HOST1" = 108) OR ("DCA2" = 11006 AND "HOST2" = 108) OR ("DCA3" = 11006 AND "HOST3" = 108) ')
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "BType", 'str("LP")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")
# PP
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' ("DCA1" = 11006 AND "HOST1" = 122) OR ("DCA2" = 11006 AND "HOST2" = 122) OR ("DCA3" = 11006 AND "HOST3" = 122) ')
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "BType", 'str("PP")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")
# 5N
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' ("DCA1" = 80003) OR ("DCA2" = 80003) OR ("DCA3" = 80003) ')
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "BType", 'str("SN")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")
# SB
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' ("DCA1" = 11009) OR ("DCA2" = 11009) OR ("DCA3" = 11009) ')
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "BType", 'str("SB")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")
# DFB
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' ("DCA1" = 11007) OR ("DCA2" = 11007) OR ("DCA3" = 11007) ')
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "BType", 'str("DFB")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")
# WBBB
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' ("DCA1" = 80002) OR ("DCA2" = 80002) OR ("DCA3" = 80002) ')
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "BType", 'str("WBBB")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")
# WSB
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' ("DCA1" = 12040) OR ("DCA2" = 12040) OR ("DCA3" = 12040) ')
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "BType", 'str("WSB")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")
# WPB
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' ("DCA1" = 11002) OR ("DCA2" = 11002) OR ("DCA3" = 11002) ')
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "BType", 'str("WPB")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")
# Aspen Decline
arcpy.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", ' ("DCA1" = 80001) OR ("DCA2" = 80001) OR ("DCA3" = 80001) OR ("DCA1" = 24032) OR ("DCA2" = 24032) OR ("DCA3" = 24032) ')
messageList.append(arcpy.GetMessages())
arcpy.CalculateField_management(outLayer, "Aspen_Dec", 'str("AD")', "PYTHON_9.3", "")
arcpy.SelectLayerByAttribute_management(outLayer, "CLEAR_SELECTION")
# Creates a Shape file from the layer
arcpy.CopyFeatures_management(outLayer, outFc)
messageList.append(arcpy.GetMessages())
# Erase
arcpy.Erase_analysis(outFc, eraseFeat, eraseOut)
messageList.append(arcpy.GetMessages())
# Writes information to log file
f = open(log, 'w') # Log file location
f.write('Created by: Matt Russo' + '\n')
f.write('Email: matthewrusso1986@gmail.com' + '\n')
f.write('Architecture : ' + platform.architecture()[0] + '\n')
f.write('Python EXE : ' + sys.executable + '\n')
f.write('Path to arcpy : ' + imp.find_module('arcpy')[1] + '\n' +'\n')
f.write('Start time was: ' + start + '\n')
end = (time.strftime('%x %X')) # End time
f.write('End time was: ' + end + '\n' + '\n')
for message in messageList:
f.write(message)
f.write('\n' + '\n')
f.close()
... View more
02-11-2015
11:59 AM
|
0
|
5
|
5211
|
|
POST
|
EDIT: I have joined it via GP Tool Ok, progress has been made. I ran what you listed above and it looks like my fields are showing up as seperate files still(guessing because of the Join) I tried to make the join permenate by exporting/copying feature but it isn't working since the fields are still linking to the joined file. Notice the print of f.name #Texas FC Texas_COUNTY_CODE Texas_STATE_CODE #Texas_Counties FC Texas_Counties_FID Texas_Counties_STATEFP10 Texas_Counties_COUNTYFP10 Texas_Counties_NAME10 It seems like it is working, just can't find that field because it is not displaying as the "Texas" FC
... View more
02-05-2015
10:53 AM
|
0
|
2
|
695
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 04-07-2015 10:22 AM | |
| 1 | 12-18-2015 09:59 AM | |
| 1 | 01-28-2015 12:58 PM |
| Online Status |
Offline
|
| Date Last Visited |
05-10-2023
03:58 PM
|