Data selection with gmt spatial

Hi there,

I try to truncate a multi-segment file against a polygon. If a segment is completly outside : it is removed, if a point from a segment is outside, it is projected against the “wall”. See picture for illustration.

I achieved that with gmt spatial data -Twall > data_new BUT, I loose the third column information. :frowning:

Could it be a bug ?

Best,

cat > data.txt << 'EOF'
>
1 1 25
6 1 25
6 6 25
1 6 25
1 1 25
>
3 3 1
4 3 1 
4 4 1 
3 4 1
3 3 1
EOF

cat > area.txt << 'EOF'
2 0
10 0
10 10
2 10
2 0
EOF

gmt begin test png
    gmt spatial data.txt -Tarea.txt > data_selected.txt    
    gmt plot -JX10c -R0/10/0/10 data.txt -Sc0.1c -Gblack -B -l"points originaux"
    gmt plot area.txt -Wfat,red,dashed -l"Zone"
    gmt plot data_selected.txt -Wfaint,red
    gmt plot data_selected.txt -Sc0.2 -Wthin,green -l"nouveaux points"
gmt end show

We cannot know it anymore but I think it’s not a bug. -T is for (2D) polygons so when it truncates the polygon I think its right to project it on the wall. Now, the loosing of 3rd column is annoying and I would almost consider it a bug. But again, what values to assign to the projected points? The originals? But their x,y has changed so why should we keep the z?

Maybe you want to use the -N instead of -T?

I’m not sure to understand what -N does really.
What I’m looking for is what is illustrated above (new point created at the wall).
At first I tried to use a combination of sample1d and select, but it generates too many points…

Sorry, -N is not an alternative (it tells about in which polygon the points/lines fall and other alike information).

But now I don’t understand what you want and not getting. If it is the 3rd column, the lost of all extra columns beyond 2 was intentional in the code.

And how would I get it back ?

Hmm, honestly not sure. And certainly not in bash. But again, with -T you new points. What would you want to put in their’s 3rd column?

In the end, I’m writing a small script with these 4 steps :

  1. gmt spatial a.txt b.txt -Ie to get the crossovers between my wall and the polygons
  2. grep to identify the relevant crossovers
  3. awk to append said crossovers location to original polygons and copy/paste the third column
  4. gmt select to remove outliers

So… instead of projecting against the wall, I look where the polygons meet the wall.
gmt sample1d would benefit this option I believe

Now … The problem is that I append arbitrarily at the end of each segment, which might not respect the points order.
Do you think it is feasible to determine the proper position (based on gmt spatial maybe?

When you use the -T, you get what you seek but loose the 3rd column. On the other hand, with your procedure you save the 3rd column but loose the order, Right?

What about searching, for each polygon, the points from the -T result in those of your procedure and keep them (from -T) but add the 3rds from the other solution?

gmt spatial data.txt -Tarea.txt
> P1
2       1
6       1
6       6
2       6
> P2
3       3
4       3
4       4
3       4
3       3

Then I face the problem you mentioned. If it’s “projected” against the wall, the new location doesn’t have the same value. What I need is somehow flag where the segment if being truncated and replace this portion with the crossover location (and 3rd column value). I think.
Maybe I’m overthinking this.