Question

Question of the Week: Replacing rotated features with a grid

  • 26 March 2021
  • 4 replies
  • 4 views

Userlevel 4
Badge +25

Question of the Week

This week's question-of-the-week was asked on GIS/StackExchange. It's all about how to create a grid from a rectangular feature.

 

qotw-rotatedgrid-1 

"I have a set of polygons, each one comprises 4 sides. Each facing sides are equally divided into segments (right figure). Is there anyhow to automatically create a grid as shown in the figure on on the left ?"

 

So let's have a look at how we can do that.

 

[Ah, I just found it's also been asked on the community here]

 

Creating Grids

It's pretty easy to create a grid like that from scratch if you know the dimensions needed. You just put down a 2DGridCreator:

 

qotw-rotatedgrid-2  

...and that was suggested as a solution by someone. But, if the feature exists already, the better transformer is the 2DGridAccumulator, like so:

 

qotw-rotatedgrid-3  

The trick here is to set a group-by, else you get a single grid that covers all of the features (whereas we want a grid per feature).

 

However, this doesn't take into account that the user's question showed a rotated polygon!

 

Handling the Rotation

Handling rotation is harder than it looks because the 2DGridAccumulator has no concept of rotation. It merely takes the bounding box and creates a grid from that:

 

qotw-rotatedgrid-4  

...which isn't exactly what we need.

 

The solution is to undo the rotation before applying the grid. Then we can rotate the data back. Here's the workspace I used:

 

Setup:

qotw-rotatedgrid-5  

Process:

qotw-rotatedgrid-6  

The setup is only creating some test data, although the one part that is important is fetching the xy coordinate of the lower-left point. That's what is used to rotate the data about, and I'll use the same point to rotate back again.

 

So, the processing part takes the source data and undoes any rotation. Here I know it's a consistent 10 degrees, because that's what I created. But if it wasn't 10 degrees, or it varied per feature, you could easily get the value from the fme_rotation format attribute.

 

Next, I create the required grid. I could either create fixed-sized grid cells (in which case they would be consistent in size but might not exactly fit the source feature) or I could create a fixed number of cells (in which case they wouldn't be consistent in size, but would exactly fit the source feature). Notice that I'm grouping on x/y attributes, because I know they are unique for each feature.

 

Next, I aggregate the created grid cells. Why? Because I need to rotate them all by the same amount and from the same origin point. I couldn't do that if they were all individual features. Again I group by x/y attributes because this time I know they are common to all related grid cells.

 

Finally, I rotate the data back again. Again, it's a fixed 10 degrees, but I could use fme_rotation instead. The result is:

 

Image is not available

  

The one final aspect I didn't include was a Deaggregator, to revert the grid back to individual features; but that's something I didn't know if the user needed.

 

The lesson from this... well, if you don't need grids like this, then maybe the lesson is that when FME is slightly lacking in an option (like the 2DGridAccumulator is) there's usually a way to recreate it with other transformers. I also think this is an interesting study in using group-by parameters, in places and ways that you might not have considered. So I hope it's an interesting workspace.

 

Other Questions

A very quick scan highlighted these other questions of interest:

 

  • How to replace spaces with commas. We all love a bit of regex (but only a bit). Here it didn't seem necessary, but I think the follow-up query suggests regex is required. The problem is that multiple spaces need to be replaced by only 1 comma. Can the StringReplacer do that?
    • If you really like regex, here's a question that maybe needs a bit more complex answer.
  • How to expose attributes from a JSONFlattener. This can be a painful process, but FME2020 or newer helps by letting you use cached data. Here the user has 2019 but can hopefully upgrade. Also, note the upcoming webinar on feature caching 101.
  • How to find an FME consultant. It sounds like this user needs more help than we usually provide in the community, but is worried about the cost of using a partner. Are little, paid jobs like this of interest to our community? Should we set up a tip jar for answerers to receive donations? Or is monetizing the community not a good idea? I'd love to hear what you think. Post a comment below.
  • How to prompt a user to select the source format. When the source data is raster, but could be either tif or jpg or something else, then the Generic Reader is the solution.

 


4 replies

Badge +3

@mark2atsafe​ images are missing :(

Badge +20

This is awkward

awkward

Userlevel 4
Badge +25

This is awkward

awkward

Gah! There's always something, eh? It should be fixed now. Thanks for letting me know.

Userlevel 4
Badge +25

@mark2atsafe​ images are missing :(

OK, I suppose it would be better with the images showing. Should be fixed now. Thanks for letting me know.

Reply