0

I am trying to calculate:

  1. total numbers of wells that fall into each county in a shapefile, and update the county table with the total count.
  2. calculate the well density (how many wells there are per area unit, and update the table with the values obtained)

This is what I have so far but I have to set up the where_clause (the where clause should be set up to select the county that the cursor is on).

How should I sep up the where_clause to select the county and similarly select the wells that intersect on that county where the cursor is on?

import arcpy


WellsPath = "C:/Lesson5_Data/Wells.shp"
COpath = "C:/Lesson5_Data/COUNTIES.shp"

# Create a feature class for the wells

arcpy.MakeFeatureLayer_management (wellspath, "wells_lyr")
arcpy.MakeFeatureLayer_management (COpath, "county_lyr")

# Set up the field list to be used by the update cursor
fieldlist = "COUNTY"

countyname = "'ADAMS'"
where_clause = "COUNTY = " +  countyname
print where_clause

with arcpy.da.UpdateCursor(COpath, fieldlist) as countycursor:
    for row in countycursor:
        print row[0]
        countyCount =   int(arcpy.GetCount_management("county_lyr").getOutput(0))
        print "Counties selected: " + str(countyCount)
        arcpy.SelectLayerByLocation_management("wells_lyr", "INTERSECT", "county_lyr", "", "NEW_SELECTION")

I'm new to Python and I have no clue how to do this.

3
  • PolyGeo, I think I have an issue with the where_clause, when I run my scrip the result is empty but when I exclude the where_clause from the cursor it print the name of the 64 counties selected. What is wrong with my scrip?
    – goyo
    Commented Jul 14, 2016 at 18:32
  • 1
    Are the values in the COUNTY field uppercase? e.g. are the values in that field ADAMS rather than Adams or adams?
    – Midavalo
    Commented Jul 14, 2016 at 19:37
  • 1
    Base on your comments on my answer I'm not sure I fully understand what you're needing. Could you edit your question and include a couple of screenshots? One of the attribute table of your data, and one of how you'd like the output to look (perhaps put some example data into an Excel table and include a screenshot of that). Your Update Cursor includes a Select By Location but no actual update of the data, so I'm unsure what you're trying to actually achieve at the end of this.
    – Midavalo
    Commented Jul 15, 2016 at 22:36

1 Answer 1

1

The Where Clause in the cursor will be case sensitive (see Building a query expression) so you'll need to allow for that.

Try this:

countyname = "ADAMS"
where_clause = """UPPER(COUNTY) = '{}'""".format(countyname)
print where_clause

with arcpy.da.UpdateCursor(COpath, fieldlist, where_clause) as countycursor:
    for row in countycursor:
        print row[0]
        countyCount =   int(arcpy.GetCount_management("county_lyr").getOutput(0))
        print "Counties selected: " + str(countyCount)
        arcpy.SelectLayerByLocation_management("wells_lyr", "INTERSECT", "county_lyr", "", "NEW_SELECTION")

I've removed the single quotes ' from countyname = "ADAMS" and added into the where clause. Also added an UPPER() into the where clause to force it to treat all values as upper case to match your countyname value. Values in a where clause SQL statement are case sensitive for everything but personal geodatabases, so best to force them all to upper or lower here.

1
  • Midavalo, the name of the counties are in paper case; "ADAMS" is just one of the 64 counties; how can I write the where_clause to select each of the counties in the row. The field list to be used by the update cursor is "COUNTY". In this list are all the 64 counties names. thanks for helping me out with this.
    – goyo
    Commented Jul 14, 2016 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.