<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How can I select the minimum value for each group within a point file? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-can-i-select-the-minimum-value-for-each-group/m-p/1177626#M64634</link>
    <description>&lt;P&gt;Hi there. I've been constructing a new python tool in ArcGIS Pro which generates a point file around an existing shapefile of polygons:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="DPG_0-1653585108040.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/42213iE2F9862E213B44CC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="DPG_0-1653585108040.png" alt="DPG_0-1653585108040.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I now want to calculate which one of these points for each polygon is closest to the road (the polyline that runs down the centre). However, although I can calculate this value in my script, I am having difficulties selecting the closest point and writing it as a new shapefile.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I initially used Arcpy Near (Analysis) to return the distance of every single point to the road (please refer to my code at the bottom of the post).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I then tried an arcpy Select By Attribute command to select the point with the lowest distance for every group of points (points that sit on the edge of the same shape share a common original FID, field as seen in the attribute table):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="DPG_1-1653587551622.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/42216i6DDD45F38C8E389C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="DPG_1-1653587551622.png" alt="DPG_1-1653587551622.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The SQL expression looked like this, using the select by attribute tool.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NEAR_DIST = (SELECT MIN(NEAR_DIST) FROM asset_points GROUP BY ORIG_FID)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, that didn't return the minimum value for each of the groups; only the first group in the point file attribute table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anybody have any ideas about how I can achieve this, and then slot it into my python script? Here is what my desired output might look like:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="DPG_2-1653587792950.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/42217iA0F5CE56DC23242E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="DPG_2-1653587792950.png" alt="DPG_2-1653587792950.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be much appreciated. For reference, here is my current script:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# -*- coding: utf-8 -*-

import arcpy
import numpy

class Toolbox(object):
    def __init__(self):

        self.label = "FindMinimumPoint"
        self.alias = "toolbox"

        
        self.tools = [Tool]


class Tool(object):
    def __init__(self):
        
        self.label = "FindMinimumPoint"
        self.description = "FindMinimumPoint"
        self.canRunInBackground = False

    def getParameterInfo(self):
        
        # Param 0 = Road
        param0 = arcpy.Parameter(
        displayName="Road",
        name="Parameter0",
        datatype="GPFeatureLayer",
        parameterType="Required",
        direction="Input")

        # Param 1 = Shapes
        param1 = arcpy.Parameter(
        displayName="Shapes",
        name="Parameter1",
        datatype="GPFeatureLayer",
        parameterType="Required",
        direction="Input")

        params = [param0,param1]
        
        return params

    def isLicensed(self):
        
        return True

    def updateParameters(self, parameters):
        
        return

    def updateMessages(self, parameters):
        
        return

    def execute(self, parameters, messages):

        # Overwrite existing file of the same name
        
        arcpy.env.overwriteOutput = True

        # Reference Parameters
        
        road = parameters[0].valueAsText
        shapes = parameters[1].valueAsText

        # make feature layer

        arcpy.management.MakeFeatureLayer(shapes, "shapes_lyr")

        # Generate points along shape edges

        arcpy.management.GeneratePointsAlongLines("shapes_lyr", "shapes_points", 'PERCENTAGE', Percentage=7)

        # Find distance of each vertex to road
        
        arcpy.analysis.Near("shapes_points", road)



        return&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 26 May 2022 18:19:03 GMT</pubDate>
    <dc:creator>DPG</dc:creator>
    <dc:date>2022-05-26T18:19:03Z</dc:date>
    <item>
      <title>How can I select the minimum value for each group within a point file?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-select-the-minimum-value-for-each-group/m-p/1177626#M64634</link>
      <description>&lt;P&gt;Hi there. I've been constructing a new python tool in ArcGIS Pro which generates a point file around an existing shapefile of polygons:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="DPG_0-1653585108040.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/42213iE2F9862E213B44CC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="DPG_0-1653585108040.png" alt="DPG_0-1653585108040.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I now want to calculate which one of these points for each polygon is closest to the road (the polyline that runs down the centre). However, although I can calculate this value in my script, I am having difficulties selecting the closest point and writing it as a new shapefile.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I initially used Arcpy Near (Analysis) to return the distance of every single point to the road (please refer to my code at the bottom of the post).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I then tried an arcpy Select By Attribute command to select the point with the lowest distance for every group of points (points that sit on the edge of the same shape share a common original FID, field as seen in the attribute table):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="DPG_1-1653587551622.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/42216i6DDD45F38C8E389C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="DPG_1-1653587551622.png" alt="DPG_1-1653587551622.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The SQL expression looked like this, using the select by attribute tool.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NEAR_DIST = (SELECT MIN(NEAR_DIST) FROM asset_points GROUP BY ORIG_FID)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, that didn't return the minimum value for each of the groups; only the first group in the point file attribute table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anybody have any ideas about how I can achieve this, and then slot it into my python script? Here is what my desired output might look like:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="DPG_2-1653587792950.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/42217iA0F5CE56DC23242E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="DPG_2-1653587792950.png" alt="DPG_2-1653587792950.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be much appreciated. For reference, here is my current script:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# -*- coding: utf-8 -*-

import arcpy
import numpy

class Toolbox(object):
    def __init__(self):

        self.label = "FindMinimumPoint"
        self.alias = "toolbox"

        
        self.tools = [Tool]


class Tool(object):
    def __init__(self):
        
        self.label = "FindMinimumPoint"
        self.description = "FindMinimumPoint"
        self.canRunInBackground = False

    def getParameterInfo(self):
        
        # Param 0 = Road
        param0 = arcpy.Parameter(
        displayName="Road",
        name="Parameter0",
        datatype="GPFeatureLayer",
        parameterType="Required",
        direction="Input")

        # Param 1 = Shapes
        param1 = arcpy.Parameter(
        displayName="Shapes",
        name="Parameter1",
        datatype="GPFeatureLayer",
        parameterType="Required",
        direction="Input")

        params = [param0,param1]
        
        return params

    def isLicensed(self):
        
        return True

    def updateParameters(self, parameters):
        
        return

    def updateMessages(self, parameters):
        
        return

    def execute(self, parameters, messages):

        # Overwrite existing file of the same name
        
        arcpy.env.overwriteOutput = True

        # Reference Parameters
        
        road = parameters[0].valueAsText
        shapes = parameters[1].valueAsText

        # make feature layer

        arcpy.management.MakeFeatureLayer(shapes, "shapes_lyr")

        # Generate points along shape edges

        arcpy.management.GeneratePointsAlongLines("shapes_lyr", "shapes_points", 'PERCENTAGE', Percentage=7)

        # Find distance of each vertex to road
        
        arcpy.analysis.Near("shapes_points", road)



        return&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2022 18:19:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-select-the-minimum-value-for-each-group/m-p/1177626#M64634</guid>
      <dc:creator>DPG</dc:creator>
      <dc:date>2022-05-26T18:19:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to find and return the minimum value from groups within a point file?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-select-the-minimum-value-for-each-group/m-p/1177636#M64635</link>
      <description>&lt;P&gt;Why don't you incorporate&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/analysis/near.htm" target="_blank"&gt;Near (Analysis)—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;or&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/analysis/generate-near-table.htm" target="_blank"&gt;Generate Near Table (Analysis)—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;into your workflow&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2022 17:39:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-select-the-minimum-value-for-each-group/m-p/1177636#M64635</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2022-05-26T17:39:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to find and return the minimum value from groups within a point file?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-select-the-minimum-value-for-each-group/m-p/1177648#M64636</link>
      <description>&lt;P&gt;Thanks for the suggestion. I used arcpy near initially to get the distance of every point to the line (I've updated my post to make it a little clearer). However, my problem comes from trying to select the lowest distanced point from each set of points on a polygon.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2022 17:58:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-select-the-minimum-value-for-each-group/m-p/1177648#M64636</guid>
      <dc:creator>DPG</dc:creator>
      <dc:date>2022-05-26T17:58:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to find and return the minimum value from groups within a point file?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-select-the-minimum-value-for-each-group/m-p/1177683#M64639</link>
      <description>&lt;P&gt;It sounds like your analysis is working. Why don't you iterate over each polygon individually so that your "nearest" point returns one at a time. This might take a little longer but it seems straightforward?&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2022 19:11:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-select-the-minimum-value-for-each-group/m-p/1177683#M64639</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-05-26T19:11:22Z</dc:date>
    </item>
    <item>
      <title>Re: How can I select the minimum value for each group within a point file?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-select-the-minimum-value-for-each-group/m-p/1177781#M64644</link>
      <description>&lt;P&gt;@Anonymous User&amp;nbsp;is correct. You need a way to group the points so you can run Near on each group separately. I would suggest a spatial join between the points and polygons so you can get the ObjectID (or some other unique identifier) of the polygon each point belongs to. Then run Near for each group or do Generate Near Table on everything and find the closest in each polygon group.&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2022 22:29:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-select-the-minimum-value-for-each-group/m-p/1177781#M64644</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2022-05-26T22:29:47Z</dc:date>
    </item>
    <item>
      <title>Re: How can I select the minimum value for each group within a point file?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-select-the-minimum-value-for-each-group/m-p/1178279#M64662</link>
      <description>&lt;P&gt;You can also calculate a new field that tells you if a point is the closest point:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;shapes_points = arcpy.management.GeneratePointsAlongLines("shapes_lyr", "memory/shapes_points", 'PERCENTAGE', Percentage=7)
arcpy.analysis.Near(shapes_points, road)

arcade_expression = """
var fs_points = FeatureSetByName($datastore, "shapes_points", ["ORIG_FID", "NEAR_DIST"], false)
var fid = $feature.ORIG_FID
fs_points = Filter(fs_points, "ORIG_FID = @fid")
return $feature.NEAR_DIST == Min(fs_points, "NEAR_DIST")
"""
arcpy.management.CalculateField(shapes_points, "IsNearest", arcade_expression, "ARCADE", field_type="Short")

with arcpy.da.UpdateCursor("shapes_points", ["IsNearest"], "IsNearest = 0") as cursor:
    for row in cursor:
        cursor.deleteRow()
arcpy.management.DeleteField(shapes_points, "IsNearest")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 May 2022 09:45:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-select-the-minimum-value-for-each-group/m-p/1178279#M64662</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-05-30T09:45:27Z</dc:date>
    </item>
    <item>
      <title>Re: How can I select the minimum value for each group within a point file?</title>
      <link>https://community.esri.com/t5/python-questions/how-can-i-select-the-minimum-value-for-each-group/m-p/1259904#M66904</link>
      <description>&lt;P&gt;I have a similar issue but all I am trying to do is return the row with the minimum value for each group.&amp;nbsp; I am getting the issue where only the value for the first group is returned. My query is:&lt;/P&gt;&lt;P&gt;Select * From Vehicle_subset WHERE:&lt;/P&gt;&lt;P&gt;time_Converted=(SELECT max( time_Converted) FROM Vehicle_subset GROUP BY step_id)&lt;/P&gt;&lt;P&gt;I am expecting a single row for every group (identified by step_id) but I only get the row from the&lt;STRONG&gt; first group.&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Feb 2023 18:56:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-can-i-select-the-minimum-value-for-each-group/m-p/1259904#M66904</guid>
      <dc:creator>GarretDuffy1</dc:creator>
      <dc:date>2023-02-20T18:56:39Z</dc:date>
    </item>
  </channel>
</rss>

