hi all,
Am looking for a python code to find the nearest feature (street layer) for any given point (x,y) on the map.
i need to return the feature id only.
#python #arcpy
ArcInfo level license: use the Near tool.
Basic level license: perhaps start with a spatial join and go from there?
Best Regards,
Jim
Thank you Joshua , Dan and JIM
Finally i created the tool which without your suggestions i couldn't make it.
def Closest_RD_XY( X, Y, Near_FC , Meters 😞 try: SR = arcpy.SpatialReference(4326)# WGS84 GCS pntFC = arcpy.CreateFeatureclass_management("in_memory", "Acc_FC", "POINT", spatial_reference = SR)
# Just to check if layer created or not if pntFC is None: print "FeaClass 'Acc_FC' is not created" #------------------------------------------- In_curs = arcpy.da.InsertCursor( pntFC , ["SHAPE@XY"]) INValue = In_curs.insertRow([(X, Y)]) if INValue < 1 : print "point is not inserted" del In_curs
# use the near tool in arcpy arcpy.Near_analysis(pntFC, Near_FC , str(Meters)+ " Meters", "LOCATION","ANGLE","GEODESIC") S_curs = arcpy.da.SearchCursor(pntFC, "*") RD_ID = S_curs.next() # return all information of the nearest feature SN_curs =arcpy.da.SearchCursor(Near_FC, "*", "OBJECTID = " + str(RD_ID[2])) NFeature = SN_curs.next()
#delete in memory point layer arcpy.Delete_management (pntFC) return NFeature
except Exception as e: return "Error No Road found within 100 Meters 'Error ': " + e.message
As is the case with most geoprocessing tools, the in_features can be ArcPy Geometry objects in addition to feature layers. In your case, create a PointGeometry object for the coordinates you want, and then pass that point geometry as the in_features to the Generate Near Table tool.
You can use a point they created. It sounds like you need to code the ability for a person to enter a point first then run one of those two analysis options.
Thanks Joshua ,
i tried the tool, it does not take a point object (it needs a feature layer as an input). i need to let the user input the point coordinates in order to search the nearest feature and return its information.
unfortunately the two methods (near_analysis, generate near table) are not helping.
thanks
Generate Near Table. Put the table in-memory and then use a cursor to loop through it looking for FIDs. The original table stays intact.
Thanks a lot Jim.
the near tool will add fields to the layer used, am looking for another way which will return only the FIDs so i can loop through them and read information.
something like featurecursor interface in arcobjects.
thanks again for your replay.
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.