Geographic Information Systems Asked on October 31, 2020
For each group
I want to find the maximum point-to-point distance (and which of the id1
features participate in the max).
For example, if I have the following attribute table:
FID Long Lat id1 group
0 120.65627 23.649932 15 1
1 120.65677 23.650132 2 1
2 120.65887 23.652732 111 2
3 120.66057 23.654632 17 3
4 120.66167 23.655832 120 3
5 120.67133 23.65794 55 3
I would like to get an output table that looks like this:
Here the group=2
is omitted because there is only one point in the group, so no pt2pt distance to calculate (or it’s fine if it is there and is zero). This is maximum distance for group=3
point combinations (others, 55.57 meters and 1,011 meters are less).
The Point Distance tool doesn’t have any group-id capability nor any statistics (min, max, etc.) capablities.
I will be doing this in ArcPy, but I am not sure which tools or sequences of tools to use to get started.
I worked out a solution:
arcpy.Project_management()
FREQUENCY
of each id1
: arcpy.Statistics_analysis()
id1
values that are repeated id1
and max_dist
and populate in
a loop through all the duplicate id1
values: arcpy.CopyFeatures_management()
id1
arcpy.PointDistance_analysis()
arcpy.da.SearchCursor()
id1
and max_dist
It doesn't save the id1
values like I initially wanted, but that could be achieved by going back and adding a join operation on the OID (FID) after Step 6 and then populating the table accordingly in Step 9.
import arcpy, pandas as pd
epsg = 26911
arcpy.Project_management(inshp, projshp, arcpy.SpatialReference(epsg))
temptable1 = 'in_memory{}_Table'.format('mytemp1')
arcpy.Statistics_analysis(projshp, temptable1, statistics_fields=[['id1', 'COUNT']],
case_field='id1')
mydf = pd.DataFrame(data=[row for row in arcpy.da.SearchCursor(temptable1,
['id1', 'FREQUENCY'])], columns=['id1', 'FREQUENCY'])
mydf = mydf[mydf.FREQUENCY != 1]
keeps = mydf.id1.values.tolist()
# clean-up
arcpy.Delete_management(tempmerge)
arcpy.Delete_management(temptable1)
del mydf
df = pd.DataFrame(columns=['id1', 'max_dist'])
for n in keeps:
arcpy.CopyFeatures_management(projshp, tempshp)
with arcpy.da.UpdateCursor(tempshp, ['id1']) as rows:
for val, in rows:
if val not in [n]:
rows.deleteRow()
temptable2 = 'in_memory{}_Table'.format('mytemp2')
arcpy.PointDistance_analysis(tempshp, tempshp, temptable2)
maxdist = max([row[0] for row in arcpy.da.SearchCursor(temptable2, ['DISTANCE'])])
df = df.append({'id1': int(n), 'max_dist': maxdist}, ignore_index=True)
# clean-up
arcpy.Delete_management(temptable2)
arcpy.Delete_management(tempshp)
del maxdist
df.to_csv('myfilename.csv', index=False)
Correct answer by AlexS1 on October 31, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP