Skip to main content

Today we had a great question on the community about counting the number of line vertices in a given section of line:

 

"Lets say I have locations of point/polygon features along a line and I would like to count the number of points along the line using a moving window. For instance I'd like to slide along the line using a window of 100 feet.

 

As I move along the line, I want a count the number of points within the 100 feet window until I reach the end."

 

At first glance, this looks like it ought to be a problem of spatial analysis: you create a buffer perhaps, 100 feet long, and do an overlay to count the points inside it. Then you move it along by a set offset and repeat.

 

But consider: if you could just take the line feature, create a list of its points, and calculate how far along the line they existed... well then you could analyze the numbers not the geometry. That would be easier and quicker.

 

And the distance along the line that a point exists has a name: a Measure.

 

Measures

A measure - as noted above - is a points distance along a line. So if I had a line 100 units long, with points every 10 units, the measures would be: 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100.

 

The transformer that calculates those in FME is called a MeasureGenerator:

 

qotw-measures-1 

Of course a real line is not equally divided in such a way. It would have measures like 0, 10.343, 17.43755, 24.32445, 39.3523, and so on. But you can see that if I had a window between 10 and 30, I could easily count the number of vertices.

 

So my first section of workspace looks like this:

 

qotw-measures-2 

The length of the line I'll use in a minute. The MeasureGenerator creates measures and the MeasureExtractor puts them into a list, where I can use them more easily.

 

Set Up Calculations

Usually I want a solution to be generic, so that it could be used in different scenarios and with different parameters. Here the user wants a window (I'm calling them sectors) of 100 feet. But do they want the windows to overlap or to match up against each other? They don't say. So I've created an attribute that lets you vary the offset:

 

qotw-measures-3 

So I've set the sector length to 100, and the offset is 1 (so the sectors will overlap by 99 feet). I also calculate the number of sectors to generate which is a function of the line length, sector length, and offset.

 

I now create one feature per sector with the Cloner transformer (creating NumberOfSectors features) and calculate what the start and end point of each sector will be:

 

qotw-measures-4 

Now I have all my sectors and know what the start and end measure is for each. Now I can find out which actual measures fall within these limits...

 

List Operations

I was really worried about how I would do this... but the ListSearcher transformer is way more flexible than I remembered. It allows me to search not only for a particular value, but for a value greater or less than x.

 

Here, for example, I search for the first measure in my list that is greater than (or equal to) my sector start point:

 

qotw-measures-5 

I can repeat that for my end point and now I have the list index numbers for the points that fall inside my sector; for example start index = 10 and end index = 17.

 

The number of points inside that sector is simply end index - start index = 7. Here's what that final bit of workspace looks like:

 

qotw-measures-6 

The ListElementCounter provides the maximum vertex for the last sector, which otherwise wouldn't have a match. Trust me, it works.

 

And the best part - EXACTLY 10 TRANSFORMERS! Perfectly meets the Don limit for transformers in a demo workspace. Although I could add an Excel writer and end up with:

 

qotw-measures-7 

Fanned out by a line ID I might even handle multiple lines in one process.

 

Summary

So there it is. You can download my workspace - it's attached to the foot of the page. It uses 2020.2 and might complain if you use an earlier version. Guess you'll have to upgrade!

 

This is a fine example of "great minds think alike" because while I was writing this I noticed @jdh had also posted an answer that uses measures! Plus she mentions using lists and the ListSearcher transformer. That's fantastic.

 

Actually, that's rather spooky!

Well I suppose it is Halloween tomorrow. Have a great weekend everyone.

 

qotw-measures-8 

 

Thanks for the nice write up @mark2atsafe​ . I am going to try to use it and see the results I get.


Also, is there a way to specify the sector length in units? Maybe feet or meters or inches?


Also, is there a way to specify the sector length in units? Maybe feet or meters or inches?

The sector length in the workspace should be in units already. In my example above, it's set to 100 meters.


Reply