viernes, 14 de diciembre de 2012

Simple vector operations

I was asked yesterday about how to solve a simple analysis problem: given a lines layer with a channel network and a polygons layer representing forest areas, extract those parts of the first one that intersect the second one. The main goal was to easily calculate parameters such as the total length of rivers across forest areas, but apparently there was interest in also keeping the parts outside of the forest areas to do other kinds of analysis. Solving the first question can be easily done with a little bit of SQL, but since the person who asked me was not familiar with of SQL, and she planned to do some other things with the data, I decided to create a small SEXTANTE model instead, which basically separates line parts within the polygons from those outside, while keeping both in the same layer.

I followed these steps:
  • Clipping the lines layer with the polygons layer. 
  • Calculating the difference of the polygons layer and the lines one.
  • Adding a new field called "IN" to both layers.
  • Filling the field with a value of "1" for the layer obtained from the clipping, since it contains the line parts inside the polygons.
  • Filling the field with a value of "0" for the layer obtained from the difference, since it contains the line parts outside the polygons.
  • Merging these last two layers
The model that automates these tasks looks like this.



Considering that there are other similar layers to process, this will probably save same time for future analysis, and using it is really simple, even for inexperienced users.

---

A (slightly narcissistic) note: I later realized that the model can be created without the two Field calculator algorithms, since the SAGA Merge Shapes Layers algorithm adds a field with the name of the input layer from which each feature comes from in the output layer, and also a numerical field with an index representing that input layer. I went to SAGA to check it and know more about the algorithm (that feature seemed to me an interesting addition to a plain merge...). and I was surprised (and happy) when I  found out that I was the author of that SAGA algorithm... According to the algorithm description, I wrote it back in 2004, which I guess explains my lack of memory :-)