nearest feature to a given point

4813
7
Jump to solution
03-02-2017 04:10 AM
Faris_TawfiqAbdelFattah
New Contributor

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

0 Kudos
1 Solution

Accepted Solutions
JimCousins
MVP Regular Contributor

ArcInfo level license: use the Near tool.

Basic level license: perhaps start with a spatial join and go from there?

Best Regards,

Jim

View solution in original post

7 Replies
JimCousins
MVP Regular Contributor

ArcInfo level license: use the Near tool.

Basic level license: perhaps start with a spatial join and go from there?

Best Regards,

Jim

Faris_TawfiqAbdelFattah
New Contributor

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.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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.

Faris_TawfiqAbdelFattah
New Contributor

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 

0 Kudos
DanPatterson_Retired
MVP Emeritus

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.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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.

Faris_TawfiqAbdelFattah
New Contributor

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

0 Kudos