how to select points in a in a polygon that have a certain difference and append them to another shapefile

578
4
05-11-2018 05:01 AM
SimbarasheKapfumo
New Contributor

i have two different point shapefiles maximum.shp and minimum.shp. i also have a fishnet polygon shapefile that has 867 features. within each polygon there are maximum points and minimum points. now i want to select points in the maximum and in the minimum in each polygon if their grid_codes have a difference of 20m and append maximum points to another new shapefile  (selected_maximum_points) and the minimum points also to another new shapefile (selected_minimum_points). below is the code i have written so far. it only retains one point in each feature class. 

arcpy.MakeFeatureLayer_management(Grid,'index_lyr')
max_points = arcpy.SpatialJoin_analysis (MaximumPoints,Grid,"Surge_Tank")
min_points = arcpy.SpatialJoin_analysis (MinimumPoints,Grid,"Power_House")

MaximumCentralPoints = arcpy.CreateFeatureclass_management(workspace,"Maximum_CentralPoints_test",'POINT',max_points,has_m,has_z,sr)
MinimumCentralPoints = arcpy.CreateFeatureclass_management(workspace,"Minimum_CentralPoints_test",'POINT',min_points,has_m,has_z,sr)

arcpy.MakeFeatureLayer_management("Surge_Tank", 'point_lyr_max')
arcpy.MakeFeatureLayer_management("Power_House", 'point_lyr_min')
# page number represents the grid id
with arcpy.da.SearchCursor("Surge_Tank",["PageNumber","grid_code","OBJECTID"],'#',sr) as cursor1:
    for row in cursor1:
        with arcpy.da.SearchCursor("Power_House",["PageNumber","grid_code","OBJECTID"],'#',sr) as cursor2:
            for row2 in cursor2:
                if (row[0] == row2[0]):
                    if ((row[2]-row2[2])>=20):
                        arcpy.SelectLayerByAttribute_management('point_lyr_min','NEW_SELECTION',"OBJECTID = {}".format(row2[2]))
                        arcpy.FeatureClassToFeatureClass_conversion('point_lyr_min',workspace,"possible_powerhouse")
                        arcpy.Append_management("possible_powerhouse",MinimumCentralPoints,"TEST")
                        arcpy.SelectLayerByAttribute_management('point_lyr_max',"NEW_SELECTION","OBJECTID = {}".format(row[2]))
                        arcpy.FeatureClassToFeatureClass_conversion("point_lyr_max",workspace,"possible_surge_tank")
                        arcpy.Append_management("possible_surge_tank",MaximumCentralPoints,"TEST")
Tags (2)
0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

an image showing the expected results would help a bit.

There are ways to produce origin-destination links with a basic license by emulating 'Near' connections which would simplify the process

0 Kudos
SimbarasheKapfumo
New Contributor

if figured it out but its not that efficient. 

with arcpy.da.SearchCursor("Surge_Tank",["PageNumber","grid_code","OBJECTID"]) as max_cursor:
    for max1 in max_cursor:
        with arcpy.da.SearchCursor("Power_House",["PageNumber","grid_code","OBJECTID"]) as min_cursor:
            for m in min_cursor:
                if(max1[0]==m[0]):
                    if (max1[1]-m[1])>=20:
                        arcpy.SelectLayerByAttribute_management('point_lyr_max', 'NEW_SELECTION', 'OBJECTID = {}'.format(max1[2]))
                        arcpy.Append_management(arcpy.FeatureClassToFeatureClass_conversion('point_lyr_max', workspace1, 'selected_max_points{}'.format(max1[2])),MaximumSelectedPoints,'TEST')
                        arcpy.AddMessage('now at maximum point {}'.format(max1[2]))

                        arcpy.SelectLayerByAttribute_management('point_lyr_min', 'NEW_SELECTION','OBJECTID = {}'.format(m[2]))
                        arcpy.Append_management(arcpy.FeatureClassToFeatureClass_conversion('point_lyr_min', workspace1,'selected_min_points{}'.format(m[2])),MinimumSelectedPoints, 'TEST')
0 Kudos
DanPatterson_Retired
MVP Emeritus

probably the nested cursors

0 Kudos
curtvprice
MVP Esteemed Contributor

Perhaps it would be more efficient to try a spatial join to tag every point with the fishnet polygon its in, either running the summary stats (min,max) using a merged join, or after the spatial join, run summary statistics (min, max, range) and then use the resulting tables, rather than doing all this cursor processing on each cell.

0 Kudos