Jump to content

 
Photo

How did they do that?

- - - - -

  • Please log in to reply
5 replies to this topic

#1
François Goulet

François Goulet

    Ultimate Contributor

  • Validated Member
  • PipPipPipPipPipPip
  • 688 posts
  • Gender:Male
  • Location:Mille-Isles, Qc
  • Interests:Cartography, History, Graphic Design and almost everything else...
  • Canada

I have to develop, using VB and ArcObjects an application and one of the procedures is a NearestNeighbor without using any geoprocessing tools (to be sure it will work no matter what's le license level).

I have 1200 points in my "source" feature class and 550 points in my target one. With Hawths Tools, it takes about 6-7 seconds to create the output csv file contains the closest target point to each source. I had a couple of operations to do to those files so that's why I would like to program something.

I'm a beginner so I don't know all the methods and properties of all object. I try to do two loops, looking, one by one, with target point is nearest to each source points. That's 660,000 operations. My application is doing about 100 checks/seconds so that's roughly 1h50 minutes. It's ridiculous. I want to simplify my job, not giving me the time to go buy a coffee each time.

I didn't find any code explaining how I could do it in less that 10 seconds... :( Any clues?

Thanks!

#2
dsl

dsl

    Master Contributor

  • Validated Member
  • PipPipPipPip
  • 225 posts
  • Gender:Male
  • Location:Denver
  • United States

This is how I would approach it, but I don't know how fast this method will be.

Start looping through the points in the featureclass. I'll call it the pFeature variable.

Inside this loop:

buffer pfeature by some distance where the result is a polygon = pPolygon.

spatialfilter (intersect) pPolygon with the original featureclass to get a new featurecursor.

if the featurecount is 0

inside this if statement

do until featurecount > 0

double the distance and buffer, then run spatialfilter again


if the featurecount is 1 then you found the nearest point.

if the featurecount is > than 1 then you loop through this featurecursor and calculate the distance between each point and the original point (sqrt(deltax^2 + deltay^2)).

Write out the distance and point Id to table

Loop

Here is info on the SpatialFilter and here is info on ITopologicalOperator and Buffering.

Hope that helps,
David

#3
François Goulet

François Goulet

    Ultimate Contributor

  • Validated Member
  • PipPipPipPipPipPip
  • 688 posts
  • Gender:Male
  • Location:Mille-Isles, Qc
  • Interests:Cartography, History, Graphic Design and almost everything else...
  • Canada

That's an interesting approach. I'll give it a try today.

Thanks!

I have found the IIndexQuery.NearestFeature method, but I don't know how to get it in VB... I'll check both and come back witht the one I find best...

Thanks again!

#4
François Goulet

François Goulet

    Ultimate Contributor

  • Validated Member
  • PipPipPipPipPipPip
  • 688 posts
  • Gender:Male
  • Location:Mille-Isles, Qc
  • Interests:Cartography, History, Graphic Design and almost everything else...
  • Canada

I did it!!

It's funny because I ask about the same question to my professor and he too, suggested me to try something very similar with buffers.

It works well, but I still had to create at minimum, 1200 buffers. It reduce my processing times by at least 50 times.

I tried the NearestFeature method that I was talking about (from the IIndexQuery2 Interface).

Basically, it create an index of a defined envelope (here, my target Feature Class) and then, I passed in argument a Geometry defined as my Source Feature Cursor and it returns directly the FID of the nearest point.

I looped through 550 targets points for my 1200 source points in about 40 seconds. Still longer than the 6 of Hawths Tools, but I could definitely live with that.

Thanks a lot David. I've understand a lot of thing with trying different methods and I've thought of a couple of things I could do with the one you suggested (and of which I did not have thought).


If you're interested, here's my code in attachment. I have a form to select my source and target layer so that's passed in argument to the function. The "Offset" field is already present in my target Feature Class and that's the information I have to pass to the source FC. All my layers' names and comments where in French so I tried to translate as much as possible. I hope I didn't create errors.

Thanks again!

Attached Files



#5
dsl

dsl

    Master Contributor

  • Validated Member
  • PipPipPipPip
  • 225 posts
  • Gender:Male
  • Location:Denver
  • United States

Thanks for sharing your code. I'm glad you got a working method.

I wrote my own k-function tool once, which requires calculating the distance between one point and nearly every point in the file. The data I was working with was in the thousands and it took forever to calculate the tool (overnight). Then I decided instaed of using the ArcObjects classes to cycle through each point, that I'd put them in a more efficient data storage class in VB.NET (a dictionary in this case). The calculations happened in minutes instead of hours/days. So I suspect Hawth's tool does something similar, and takes the data and stores it in a more efficient container (think he uses c++). That probably explains why his calculation is so quick, and unfortunately VBA lacks these capabilities. Anyway, it surprised me how drastic the difference was.

Cheers,
David

#6
François Goulet

François Goulet

    Ultimate Contributor

  • Validated Member
  • PipPipPipPipPipPip
  • 688 posts
  • Gender:Male
  • Location:Mille-Isles, Qc
  • Interests:Cartography, History, Graphic Design and almost everything else...
  • Canada

Thanks for sharing your code. I'm glad you got a working method.

I wrote my own k-function tool once, which requires calculating the distance between one point and nearly every point in the file. The data I was working with was in the thousands and it took forever to calculate the tool (overnight). Then I decided instaed of using the ArcObjects classes to cycle through each point, that I'd put them in a more efficient data storage class in VB.NET (a dictionary in this case). The calculations happened in minutes instead of hours/days. So I suspect Hawth's tool does something similar, and takes the data and stores it in a more efficient container (think he uses c++). That probably explains why his calculation is so quick, and unfortunately VBA lacks these capabilities. Anyway, it surprised me how drastic the difference was.

Cheers,
David


I didn't thought that the language could do such a difference. Thanks for the input. I have to do it with ArcOjects (it's a requirement), but I did C++ a long time ago and I think I'll give it a try for geoprocessing when I'll have the time.

Thanks again!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

-->