Select to view content in your preferred language

looping select by attribute with a variable

1996
2
Jump to solution
07-06-2017 10:16 AM
LauraConner
Emerging Contributor

my script is

[noparse]

print "running..."
import arcpy
import arcpy, os
from arcpy import env
import os
import os.path
env.workspace =r"P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\route_polygons.mxd"


global shp
shp = ".shp"

x=1

in_features= r'P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\meters.shp'
out_layer = 'meters_temp'
arcpy.MakeFeatureLayer_management (in_features, out_layer,)

in_features2= r'P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\parcels.shp'
parcles_temp = 'parcles_temp'
arcpy.MakeFeatureLayer_management (in_features2, parcles_temp,)

while x <= 2:
    import arcpy
    import arcpy, os
    from arcpy import env
    import os
    import os.path
   
    # slection fuction: select meters with x for cycle/ route
    layer = out_layer
    criteria = 'route_sequ = x'
    arcpy.SelectLayerByAttribute_management (layer,'NEW_SELECTION',criteria)
    
    #slect by location: select parcles by selected meters
    in_layer = parcles_temp
    selector = out_layer
    arcpy.SelectLayerByLocation_management (in_layer,select_features = selector )

    #copy features to shape file
    in_features = parcles_temp
    out_feature_class = r"P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\" + str(x) + shp
    arcpy.CopyFeatures_management(in_features, out_feature_class)

    #merge polpgons in to 1
    in_features = r"P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\" + str(x) + shp
    out_feature_class = r"P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\" + str(x)+'merge' + shp
    arcpy.Dissolve_management (in_features, out_feature_class, multi_part='MULTI_PART')

    # add feild
    in_table = out_feature_class
    field_name = "Route"
    field_type = 'TEXT'
    arcpy.AddField_management (in_table, field_name, field_type, field_length=10)

    #populate feild
    in_table =out_feature_class
    field= "Route"
    expression = x
    arcpy.CalculateField_management (in_table, field, expression)

    #add cycle feild
    in_table = out_feature_class
    field_name = "Cycle"
    field_type = 'TEXT'
    arcpy.AddField_management (in_table, field_name, field_type, field_length=10)

    #apend in to database
    inputs = out_feature_class
    target = r"P:\\GIS Public\\Projects\\Meter Zones\\route_polygons\\route_polygons.shp"
    arcpy.Append_management (inputs, target, schema_type='NO_TEST')

    print x
    x=x+1
print "done"

[\noparse]

SelectLayerByLocation_management, CopyFeatures_managemen, Dissolve_management,AddField_management, Append_management work.

the problem is with SelectLayerByAttribute_management. on the after the 1st  iteration of the loop the where_clause/criteria dose not works as intend.  it still interprets 'route_sequ = x' as route_sequ = 1 thus the same meters and ultimately the same parcels are selected. it needs to

on the 2nd pass

  interpret 'route_sequ = x' as route_sequ = 2 , thus select thous meters and ultimately those parcels

on the 3rd pass

interpret 'route_sequ = x' as route_sequ = 3, thus select thous meters and ultimately those parcels

 

on the 4th pass

interpret 'route_sequ = x' as route_sequ = 4, thus select thous meters and ultimately those parcels

ect...

How Do I achieve this? any help would  be appreciated.

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Instead of

criteria = 'route_sequ = x'

try

criteria = 'route_sequ = ' + str(x)

View solution in original post

2 Replies
JoshuaBixby
MVP Esteemed Contributor

Instead of

criteria = 'route_sequ = x'

try

criteria = 'route_sequ = ' + str(x)

LauraConner
Emerging Contributor

thank you, that worked.

0 Kudos