Menu
  • HOME
  • TAGS

Trouble with for loop for python process [closed]

python,for-loop,numpy,gis,arcpy

You need to add a colon after the (). def cumulativecalculation(): ...

Writing lat/long geometries to a feature class field

python,gis,arcpy

You were on the right track with the UpdateCursor. If you haven't already added the XY fields, then you can do that with the script and set the field_scale parameter (number of decimal places) to 3 or round the values themselves. Then just make sure to include the new XY...

searching for shapefile within a directory

python,search,gis,arcpy

Give the the provided information, we'll need to make some assumptions: All precip shapefiles are in one directory. precip files have some random element in their filename, thus making it necessary to search inside of refer directly to the shape file by name. precip files have identifiable month and year...

Is there a possibility to define dtype only once for all columns?

python,arrays,numpy,arcpy

This example generates 4 fields by creating the string: '<f8,<f8,<f8,<f8': In [126]: np.zeros((3,),np.dtype(','.join(['<f8']*4))) Out[126]: array([(0.0, 0.0, 0.0, 0.0), (0.0, 0.0, 0.0, 0.0), (0.0, 0.0, 0.0, 0.0)], dtype=[('f0', '<f8'), ('f1', '<f8'), ('f2', '<f8'), ('f3', '<f8')]) There's probably a way of constructing it with lists and tuples, but this string version is...

Exporting attributes of multiple shapefiles to a text file

python,text,arcgis,arcpy

Here's one way: import arcpy import csv f = open(r'C:\\user\\Exports.txt', 'w') w = csv.writer(f, lineterminator='\n') arcpy.env.workspace = "C:\\user\\rainfiles" shapefileList = arcpy.ListFeatureClasses("*.shp") for table in shapefileList: f.write("Shapefile:\n") f.write(table + "\n") fieldList = arcpy.ListFields(table) field_names = [field.name for field in fieldList] w.writerow(field_names) for row in arcpy.SearchCursor(table): field_vals = [] for field in...

Changing a variable during a loop, Python

python,loops,variables,arcpy

I think the comment is on the right track. What you want to do is restructure the following: HarbName1 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName1")[0] HarbName2 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName2")[0] HarbName3 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName3")[0] such as: HarbName = { 1: arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName1")[0], 2: arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName2")[0] 3:...

Create for loop to process multiple rasters in a file using arcpy Clip management

python,arcpy

You can't export a ModelBuilder to Python and use it as it is. The dynamic %Name% notation used in ModelBuilder will not be recognoised in Python. You have to define the workspace of interest prior to running the ListRasters function (check its syntax here). Also, as mentioned by @abarnet, the...

How to use raw_input() with while-loop?

python,python-2.7,arcpy

You would want to first make sure that you have the correct filename and then proceed to the next step. The code should be: (assuming arcpy.Exists(buff_name) does what it is supposed to do, because I think it should be arcpy.Exists(buff_name+str(100)) or something like it.) import arcpy arcpy.env.overwriteOutput = 1 arcpy.env.workspace="C:\\salzburg.gdb"...

How to display messages while creating directories?

python,python-2.7,arcgis,arcpy,os.path

Your else clause looks like it's out of place. In the code you posted, the "Creating" message would've been printed only when an OSError occurred AND dirmk did not exist. In this snippet below, the "Created" message would be printed if no errors were encountered while creating dirmk. td =...

How to construct an interactive dialog using wxPython?

wxpython,arcgis,arcpy

If I were you, I would start out by getting the wxPython demo package. The demo allows you to see examples of 99% of the widgets including the code. It also includes a few folders of decent example applications. The primary widget I would use for table is probably some...

How do I count the number of times a date shows up in a list?

python,csv,counting,arcpy

Look at the Counter class in the collections package. It's a type of dictionary where the keys will be dates (in your case), and the values will be the accumulated counts of how many times that date occurred. So for each employee you create a Counter instead of a dictionary....

arcpy query not working

python,select,arcgis,arcpy

Could it be that you got your apostrophes wrong? myQuery = '"VM_POLY" = \'E-dom\'' or '"VM_POLY" = \'E-subdom\'' # ^ ¨ ¨^ ^ ¨ ¨^ (Apostrophes marked by a ^ delimit a Python string; those marked with ¨ are escaped and therefore part of the string.) I suppose your query...

Using OR operator in NumPy Array to Append multiple items

python-2.7,numpy,arcgis,arcpy

Instead of doing the "OR" inside the append, you'll need to do an if statement: if category == 'bulky item': items.append((Address, x, y, x, y, ReasonCode, SRNumber, SRNumber, FullName, ResolutionCode, HomePhone, CreatedDate, UpdatedDate, BulkyItemInfo, k_bulky_comm, ServiceDate, CYLA_DISTRICT, SCCatDesc, # ServiceNotes, Prior_Resolution_Code, CYLA_DISTRICT, )) elif category == 'e-waste': items.append((Address, x, y,...

How to limit for loop iterations within cursor?

python,cursor,arcpy

Add a counter and a logical statement to limit the number of iterations. For example: import arcpy fc = r'C:\path\to\featureclass' count = 1 # Start a counter with arcpy.da.SearchCursor(fc, ["fieldA", "FieldB", "FieldC"]) as cursor: for row in cursor: # Do something... if count >= 2: print "Processing stopped because iterations...

Program crashing because '#' is being added to argument somewhere

python,python-2.7,arcpy

No pound sign is being added anywhere. That's simply an ESRI message flag. It's telling you that there is no shapefile \storage1\gis\temp\alpha.shp. Note that it's not telling you that beta.shp is missing (though it may be as well). The path is incorrect. You have an extra 'e' after table1 in...

difflib.SequenceMatcher not returning unique ratio

python,gis,arcpy,difflib,sequencematcher

If you know both files always have the exact same number of lines, a simple approach like this would work: ratios = [] with open('fieldName1', 'r') as f1, open('fieldName2', 'r') as f2: for l1, l2 in zip(f1, f2): R = difflib.SequenceMatcher(None,l1,l2).ratio() ratios.append((l1, l2, R)) This will produce a list of...

Python extract data from xml and save it to excel

python,xml,python-2.7,arcpy

The following should work: import xml.etree.ElementTree as ET import arcpy xmlfile = 'D:/Working/Test/Test.xml' element_tree = ET.parse(xmlfile) root = element_tree.getroot() agreement = root.find(".//agreementid").text arcpy.AddMessage(agreement) The root.find() call uses an XPath expression (quick cheatsheet is in the Python docs here) to find the first tag at any level under the current level...

local variable value is not used - python

python,arcpy

By convention if you're looping and don't intend to use the value you store the iterator in a variable named _. This is still a normal variable that gets each value in turn, but is taken to mean "I don't plan to use this value." To use this convention you'd...

How to calculate the proportion of each cell in the matrix to the sum of the column?

python,arrays,numpy,arcpy

Using NumPy's bradcasting rules you can avoid the loops doing something like: s = a.sum(axis=0) x = ((a/s)**2).sum(axis=0) where a is your m X n matrix....

How to use the List field function for a toolbox script?

python,arcgis,arcpy

ListFields returns a list of field objects, and each of these objects has attributes associated with it. So the structure looks like this: field_list = [field object1: field.name, field.type, etc...; field object2: field.name, field.type, etc...; ] You'll probably want to access the .name attribute of each field object to determine...

How do I check if my search cursor is empty in arcpy?

python,arcgis,arcpy

The following should work by counting the number of rows returned by the cursor: i = 0 with arcpy.da.SearchCursor("your_table", ["your_fields"], "your_query") as cursor: for row in cursor: i += 1 if i == 0: print "No results returned." Note the construction of the cursor, where a list of your fields...

Python - How to add column headers from the generated List

python,arrays,numpy,arcpy

I`m not getting what your requirement exactly, may be you want to add the list in 'fList' as an header to 'matrix', if it is so, you can do it simply as - matrix = np.insert(matrix,0,flist,0) Please accept the answer or elaborate your requirement more. ...

import many ascii files in a directory, read as array, print number of rows, columns for each file

numpy,pandas,scipy,arcpy

I think numpy could be used for this. for file in arcpy.ListTables(): outfile = numpy.loadtxt(file, delimiter=" ", skiprows = 6) if outfile.shape != (23,23): print file + " has an incorrect number of rows or columns" ...