Select to view content in your preferred language

How to get nearest feature from Buffer

1321
8
05-19-2013 05:41 AM
MohammadSharique
Emerging Contributor
Hi All

i found all features within buffer area  using geometry service and identify task , in my case i get all the cities within buffer area from  selected point , but i dont want all , i just need to get the ID of nearest city(point)

if i get sample then will be more appreciated.

Plz help me .
Thank You
0 Kudos
8 Replies
wangzhifang
Frequent Contributor
FYI,
foreach(var city in citiesWithinBuffer)
{
    city.Attributes["DistToPoint"]=Math.Square(Math.Pow((city.X+selectedPoint.X),2)+Math.Pow((city.Y+selectedPoint.Y),2));
}
//using System.Linq;
Graphic nearestCity=citiesWithinBuffer.Min(c=>double.Parse(c.Attributes["DistToPoint"].ToString()));
0 Kudos
MohammadSharique
Emerging Contributor
Thanks for replying diligentpig

My code to get feature within buffer is below. i tried your code but could not find co-ordinate X and Y and function Square and also u used " city.Attributes["DistToPoint"] " is that field is of type distance , i replcaed it with CITY as in my case,

Plz explain  Thank you.

private void idtask_ExecuteCompleted(object sender, IdentifyEventArgs args)
        {          
            string Place=string.Empty,wellno=string.Empty,SiteName;
            int wellId=0;
            foreach (IdentifyResult selectedGraphic in args.IdentifyResults)
            {
               
                switch (selectedGraphic.LayerId)
                {
                    case 0:
                        if (selectedGraphic.Feature.Attributes["City"] != null)
                        {
                            string City= selectedGraphic.Feature.Attributes["City"].ToString();
                        }
                        break;
                    case 1:
                        if (selectedGraphic.Feature.Attributes["CODE"] != null)
                        {
                            Code.Add(Convert.ToInt16(selectedGraphic.Feature.Attributes["CODE"]));
                        }
                        break;
}
0 Kudos
wangzhifang
Frequent Contributor
FYI,
foreach(var city in citiesWithinBuffer)
{
    city.Attributes["DistToPoint"]=Math.Square(Math.Pow((city.X+selectedPoint.X),2)+Math.Pow((city.Y+selectedPoint.Y),2));
}
//using System.Linq;
Graphic nearestCity=citiesWithinBuffer.Min(c=>double.Parse(c.Attributes["DistToPoint"].ToString()));


Assume you have a collection of graphics called citiesWithinBuffer which stores the results of your spatial query.  "DistToPoint" attribute is used to save the calculate result of the distance from each graphic to your query point, by using Pythagorean theorem. Sorry for mistake, Math.Square should be Math.Sqrt. Now you have every distance from candidate cities to your destination point stored as city.Attributes["DistToPoint"].
.Min is a LINQ function which could retrieve  the minimum value from a collection.
0 Kudos
MohammadSharique
Emerging Contributor
Hi Wang

this line throwing casting error "Can not convert double to graphics"
//using System.Linq;
Graphic nearestCity=citiesWithinBuffer.Min(c=>double.Parse(c.Attributes["DistToPoint"].ToString()));

is below line will give a distance between selectedpoint and found feature(point)

selectedGraphic.Feature.Attributes["DistToPoint"] = Math.Sqrt(Math.Pow((selectedGraphic.Feature.Geometry.Extent.XMin + selectedpoint.Extent.XMin), 2) + Math.Pow((selectedGraphic.Feature.Geometry.Extent.YMin + selectedpoint.Extent.YMin), 2));

I added ur code like this in my application

foreach (IdentifyResult selectedGraphic in args.IdentifyResults)
            {

                selectedGraphic.Feature.Attributes["DistToPoint"] = Math.Sqrt(Math.Pow((selectedGraphic.Feature.Geometry.Extent.XMin + geometryForBuffer.Extent.XMin), 2) + Math.Pow((selectedGraphic.Feature.Geometry.Extent.YMin + geometryForBuffer.Extent.YMin), 2));
               
                switch (selectedGraphic.LayerId)
                {
                    case 1:
                        if (selectedGraphic.Feature.Attributes["Well No"] != null)
                        {
                            wellno = selectedGraphic.Feature.Attributes["Well No"].ToString();  // i need nearest well number
                        }
                        if (selectedGraphic.Feature.Attributes["ID"] != null)
                        {
                            wellId = Convert.ToInt16(selectedGraphic.Feature.Attributes["ID"]); // i need nearest well id
                        }
                        break;
                    case 3:
                        if (selectedGraphic.Feature.Attributes["NAME"] != null)
                        {
                            Roads = selectedGraphic.Feature.Attributes["NAME"].ToString(); // i need nearest road name
                        }
                        break;
case 4:
                        if (selectedGraphic.Feature.Attributes["NAME"] != null)
                        {
                            Place= selectedGraphic.Feature.Attributes["NAME"].ToString();  // i need nearest place
                        }
                        break;
}


this code is written in identifytask_ExecuteCompleted event. can u plz explain where ur code will fit in this app.

Thanks a lot
0 Kudos
wangzhifang
Frequent Contributor
Sorry for inconvenience, but I just try to provide you a thought rather than exact code of your problem...
btw, the last line should be:
Graphic nearestCity= citiesWithinBuffer.First(c=>double.Parse(c.Attributes["DistToPoint"].ToString())== citiesWithinBuffer.Min(c=>double.Parse(c.Attributes["DistToPoint"].ToString()));

You really need to find out what the LINQ function means before you copy it to your code.
0 Kudos
ChrisBradberry
Deactivated User
Another way to do this is to make a Thiessen polygon from the city points using the Create Thiessen Polygons (Analysis) command. Then just do a identify on the Thiessen polygons with your input point.  Include the coordinates of the cities in the polygon, and you can find the distance and direction too.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
To get the closest point from a point, we can also do it at client side by calculating the distance between 2 points and selecting the minimum value 🙂
0 Kudos
MohammadSharique
Emerging Contributor
Hi Dominique Broux

Can u please refer any link or sample for this , as i don't have any field for distance in my service attribute ,
or how can i measure distance between center of buffer to the point .

thank you in advance ,
0 Kudos