POST
|
I fixed several errors in my script that were preventing it from functioning, and I've included it below for anyone who is interested. I changed the purpose of my code slightly (searched for 50 closest observations, kept rank rather than distance, etc.) but the general idea is the same. fc = "pts"
with arcpy.da.SearchCursor(fc,['Shape@','Name']) as cursor:
for row in cursor:
name=str(row[1])
query= "\"Name\" = \'"+name+"\'"
arcpy.SelectLayerByAttribute_management (fc, "NEW_SELECTION", query)
arcpy.GenerateNearTable_analysis(fc, "observations", "near_"+name, \
closest='ALL', closest_count=50)
arcpy.AddField_management("near_"+name, "Name_"+name, "TEXT")
arcpy.CalculateField_management("near_"+name, "Name_"+name, \
repr(row[1]), "PYTHON")
arcpy.AddField_management("near_"+name, "Rank_"+name, "SHORT")
arcpy.CalculateField_management("near_"+name, "Rank_"+name, \
"!Near_Rank!", "PYTHON")
arcpy.JoinField_management("observations", "FID", "near_"+name, "Near_FID",\
"Rank_"+name)
... View more
12-01-2016
05:40 PM
|
1
|
0
|
541
|
POST
|
Yes, I actually use Generate Near Table in my current approach (see above). I seems to be running into challenges using this tool in a loop, however.
... View more
12-01-2016
10:08 AM
|
0
|
0
|
541
|
POST
|
I have a set of several points, "pts", and a large dataset, "observations", of thousands of points. I would like to calculate the distance from each point in the small dataset to all points in the large dataset (within 100km). My current strategy is to loop over the smaller dataset, select all relevant points in the larger datset (within 100km) and create a near table. I then try joining the near table for each of the smaller points to the large dataset. This would ideally mean that "observations" would have extra columns entitled, "Dist_pt1", "Dist_pt2", etc. equal to the distance between that observation and the given point (or blank if the observation and point are far away). I'm running into two errors. First, the near tables appear to have zero or just one observation (corresponding to the closest point). Also, the JoinField command (which I've been commenting out for now) causes the code (and ArcGIS) to crash. I'd appreciate any help! fc = "pts"
with arcpy.da.SearchCursor(fc,['Shape@','Name']) as cursor:
for row in cursor:
name=str(row[1])
arcpy.AddField_management("observations", name, "FLOAT")
query= "\"Name\" = \'"+name+"\'"
arcpy.SelectLayerByAttribute_management (fc, "NEW_SELECTION", query)
arcpy.GenerateNearTable_analysis(fc, "observations", "near_"+name, \
"100 Kilometers")
arcpy.AddField_management("near_"+name, "Name_"+name, "TEXT")
arcpy.CalculateField_management("near_"+name, "Name_"+name, repr(row[1]), "PYTHON")
arcpy.AddField_management("near_"+name, "Dist_"+name, "TEXT")
arcpy.CalculateField_management("near_"+name, "Dist_"+name, "!Near_Dist!", "PYTHON")
arcpy.JoinField_management(fc, "Name", "near_"+name, "Name_"+name, "Dist_"+name)
... View more
11-30-2016
02:28 PM
|
0
|
4
|
1248
|
POST
|
That worked beautifully--Thanks! Does the indexing you do on row (row[0], row[1]) refer to the field names you asked for when creating the searchcursor? As in, does row[0] refer to the geometry of the given row and row[1] refer to the name?
... View more
11-28-2016
05:20 PM
|
0
|
1
|
913
|
POST
|
I have a polygon feature class and a point feature class. For each polygon, I would like to: (1) Select the points contained in that polygon and (2) Calculate the centroid of those selected points Ideally, I would end up with a new point feature class called "centroids", for example, that would have a centroid for each polygon (they are named with "Name"). I'm a beginner in ArcPy, especially with search cursors. So far, I've tried writing something to iterate over each polygon (farm), select the points(turbines) within the farm, calculate the centroid of those points, and create a layer named "turbine_center"+the name of the farm. I'm running into an error in the SelectLayerByLocation tool: working="S:/Wind/"
turbines= working + "Turbines_eastern.shp"
farms_poly= working + "Farms_polygons.shp"
arcpy.MakeFeatureLayer_management(farms_poly, "farms_poly_lyr")
arcpy.MakeFeatureLayer_management(turbines, "turbines_lyr")
fc="farms_poly_lyr"
rows=arcpy.SearchCursor(fc)
for farm in rows:
farmName=farm.getValue("Name")
print farmName
arcpy.SelectLayerByLocation_management("turbines_lyr", "intersect", farm)
arcpy.MeanCenter_stats("turbines_lyr", "turbine_center_"+farmName) Any help would be greatly appreciated!
... View more
11-28-2016
04:56 PM
|
0
|
4
|
1511
|
Title | Kudos | Posted |
---|---|---|
1 | 12-01-2016 05:40 PM |
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:24 AM
|