# Data selection with gmt spatial

**URL:** <https://forum.generic-mapping-tools.org/t/data-selection-with-gmt-spatial/5789>\
**Category:** Q&A\
**Created:** [March 14, 2025, 2:43pm UTC](https://forum.generic-mapping-tools.org/t/data-selection-with-gmt-spatial/5789 "2025-03-14T14:43:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [March 14, 2025, 2:43pm UTC](https://forum.generic-mapping-tools.org/t/data-selection-with-gmt-spatial/5789/1 "2025-03-14T14:43:31Z")

</div>

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. ☹

Could it be a bug ?

Best,

```auto
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

```

 ![test](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/f/ffe8cd5a733204482db6015ca086432201138b79.png)

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [March 14, 2025, 6:35pm UTC](https://forum.generic-mapping-tools.org/t/data-selection-with-gmt-spatial/5789/2 "2025-03-14T18:35:32Z")

</div>

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**?

---

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [March 15, 2025, 8:57pm UTC](https://forum.generic-mapping-tools.org/t/data-selection-with-gmt-spatial/5789/3 "2025-03-15T20:57:36Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [March 16, 2025, 1:44pm UTC](https://forum.generic-mapping-tools.org/t/data-selection-with-gmt-spatial/5789/4 "2025-03-16T13:44:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [March 18, 2025, 8:37am UTC](https://forum.generic-mapping-tools.org/t/data-selection-with-gmt-spatial/5789/5 "2025-03-18T08:37:34Z")

</div>

And how would I get it back ?

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [March 18, 2025, 8:19pm UTC](https://forum.generic-mapping-tools.org/t/data-selection-with-gmt-spatial/5789/6 "2025-03-18T20:19:13Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [March 19, 2025, 8:12am UTC](https://forum.generic-mapping-tools.org/t/data-selection-with-gmt-spatial/5789/7 "2025-03-19T08:12:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [March 19, 2025, 8:47pm UTC](https://forum.generic-mapping-tools.org/t/data-selection-with-gmt-spatial/5789/8 "2025-03-19T20:47:46Z")

</div>

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?

```auto
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

```

---

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [March 20, 2025, 3:49pm UTC](https://forum.generic-mapping-tools.org/t/data-selection-with-gmt-spatial/5789/9 "2025-03-20T15:49:09Z")

</div>

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.
