Count consecutive points in a polygon

I have animal relocation data and want to know how many times that animal walked within a polygon for more than one step (i.e there is more than one point within a polygon). I am more interested in how many times this happens, and not necessarily how many points there were consecutively (I suspect that there will only be 2 relocations in a row in the polygon, or maybe 3).

I don't want to count the number of points in the polygon because it needs to be: In the polygon, in the polygon... and NOT in the polygon, out of the polygon, in the polygon.

Any ideas?

This question still needs to be answered! Help please! I've attached an image that shows when I do want to count the path (red), and when I don't (green).

Note: I want to count how many times this (red in the picture) happens, not how many points there are. • This is a bit confusing. Could you post an image with a sample of your data? Do you have multiple point files? Is a new point created every time an animal enters the 'polygon'? – Radar May 13 '13 at 21:36
• Confusing, are you just saying an animal walking in and out of a polygon is counted as one entry -- no matter how long the walk is? "2 relocations in a row" means there will likely only be 2 - 3 entries? – awesomo May 13 '13 at 22:21
• Alright, I'll be more specific. I've got animal relocations - so every 15 minutes there is a point. I want to see how many times the animal followed a linear feature (a line turned into a polygon by adding a buffer), as apposed to just crossing it or going on it for one location. Ie, I want to know how many times an animal traveled along the linear feature for more than 15 minutes (i.e two or more locations along that line/in the polygon). Does that make any more sense? – Mel May 14 '13 at 15:14
• I cant post an image until I'm a 10 apparently – Mel May 14 '13 at 15:26

Two ideas:

The points represent the animal's walking path. This would assume that the points can be converted to lines. Create lines from the data and run a tool that will extract intersections of the lines and polygons. Then count the resulting features?

Second, try to convert the polygons to points (Feature to Point). Then, select the points where the line intersects them. Simply Lines Blog Post

Of course, you would have to account for the animal walking in and then back out as being one instance, not two.

Curious to know what works for you.

*Edit: Also, if you like the line idea, and if you're using ArcMAP, you might be able to use topology rules to identify points of intersection.

• There's something to be said for that conversion-to-line idea: it is possible that such a path could cross a polygon without any of the original points actually lying within the polygon. Although this would not answer the question as stated, it seems closer to what is really wanted. (+1) – whuber May 14 '13 at 0:02
• Alright, I'll be more specific. I've got animal relocations - so every 15 minutes there is a point. I want to see how many times the animal followed a linear feature (a line turned into a polygon by adding a buffer), as apposed to just crossing it or going on it for one location. Ie, I want to know how many times an animal traveled along the linear feature for more than 15 minutes (i.e two or more locations along that line/in the polygon). Does that make any more sense? – Mel May 14 '13 at 15:11
• This is helpful, but unfortunately I need to count ONLY when the animal follows the polygon (i.e there needs to be at least two consecutive points within the polygon). What you've mentioned will only tell me how many times the animal crossed the polygon or entered it, but I need this ONLY if it stayed there for at least two points (I don't care if it crossed the polygon, or if it was only in the polygon for one point) – Mel May 14 '13 at 16:41

Hopefully I understood your comment correctly. I read it as "I need to count points along line/polygon"

So, add a field to your points data and give each point the value '1'. Then do a spatial join and sum the field. Now you have a count of how many point features are within each polygon.

ArcMAP: ESRI Blog: Count Points within Polygon GRASS: Count Points

Hawth's tools also has a tool for this.

• Hmm this is close, but I need CONSECUTIVE points in the polygon. So what I need to do is count the number of times there are consecutive (a time series) points in a polygon. I don't necessarily care how many points...just how many times the points are consecutively in that given polygon. This would be easier to explain with an image.. – Mel May 14 '13 at 16:36

Here's a thought:

If an animal wanders across the polygon boundary and does not stay the min 15 minutes, or wanders in and out (as in your green drawings), then the number of times the animal path crosses the polygon boundary will be greater than the number of locations inside the polygon. If, on the other hand, the animal goes into the polygon and stays for at least one 15 minute period before leaving, then the number of points inside the polygon will be greater than the number of crosses. So if you can put into a spatial database the GPS points, and the polygon buffer then

1. create the animals path (line feature)
2. create the polygon boundary line (also line)
3. count the number of points inside the polygon
4. count the number of intersections between the path and the polygon boundary
5. compare 3 and 4

that might do it.

• My issue with this approach is that it is still giving me the # of points, and not the # of times that two or more points were within the line. So, for example, if the animal was within the line for four locations in a row (one hour), just counting the number of points (4) would "look" no different than if the animal went in for two points (30 mins), out, and then back in for two points (30 mins). That would throw off my calculation of "how many times the animal stays within the line for more than one point". Does that make sense? – Mel May 18 '13 at 22:50
• The only thing that differentiates the two cases you mention is the number to times that the animal crosses the polygon boundary. The case of "in for 1 hour then out" means only two boundary crossings. Whereas "in for 30 min, then out and in again for another 30 min" would be 4 crossings. But I can't think of any general way to differentiate all possible meanderings of teh animals... – Micha May 19 '13 at 14:09

I've come up with a work around:

1) Turn points into lines 2) Identify which lines are in the polygon - label these "1" in a new column 3) Identify when there are multiple "1"s in a row - label these consecutively (in python)