# Ogr/gmt - using aspatial field for label

**URL:** <https://forum.generic-mapping-tools.org/t/ogr-gmt-using-aspatial-field-for-label/3019>\
**Category:** Q&A\
**Created:** [June 13, 2022, 9:15am UTC](https://forum.generic-mapping-tools.org/t/ogr-gmt-using-aspatial-field-for-label/3019 "2022-06-13T09:15:52Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forum.generic-mapping-tools.org/u/Andreas)\
**Post date:** [June 13, 2022, 9:15am UTC](https://forum.generic-mapping-tools.org/t/ogr-gmt-using-aspatial-field-for-label/3019/1 "2022-06-13T09:15:52Z")

</div>

Feels trivial, but still…

My .gmt file consists of a series ff polygons, e.g.

```auto
# @D17|1|17|00256|"text"
# @P
2.881271 56.381291
2.695486 56.528523
2.611864 56.594331
2.528616 56.767993
2.444595 56.941595
2.41605332101142 57.0
3 57
3.0 56.286289123311
2.881271 56.381291

```

I would like to use one of the aspatial fields (the field containing _00256_ in this case) as text for plotting a label. But how to do this?  
Getting the text out and then figuring out the centroid as plotting position sounds like a plan, but seems intricate for such an easy task (…)

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [June 14, 2022, 4:29pm UTC](https://forum.generic-mapping-tools.org/t/ogr-gmt-using-aspatial-field-for-label/3019/2 "2022-06-14T16:29:46Z")

</div>

Easy task for the user if that was all implemented. text does no take data, computing a centroid, then gets a string and plots it. gmt spatial can compute centroids, but not sure how then to get the text item out for a polygon.

---

<div class="post-metadata">

**Author:** ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forum.generic-mapping-tools.org/u/Andreas)\
**Post date:** [June 14, 2022, 8:41pm UTC](https://forum.generic-mapping-tools.org/t/ogr-gmt-using-aspatial-field-for-label/3019/3 "2022-06-14T20:41:25Z")

</div>

Thanks Paul. That was my thought as well; `spatial` to create centroid then extract the needed text. Just had to ask if there was an easy way.

---

<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:** [June 14, 2022, 9:03pm UTC](https://forum.generic-mapping-tools.org/t/ogr-gmt-using-aspatial-field-for-label/3019/4 "2022-06-14T21:03:26Z")

</div>

And I guess you don’t want me (yet) to investigate for a one (two) liners solution, right? 🙂

---

<div class="post-metadata">

**Author:** ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forum.generic-mapping-tools.org/u/Andreas)\
**Post date:** [June 15, 2022, 8:56am UTC](https://forum.generic-mapping-tools.org/t/ogr-gmt-using-aspatial-field-for-label/3019/5 "2022-06-15T08:56:06Z")

</div>

One day, @Joaquim. One day 🙂

For now, I used QGIS with it’s spatialite(?) support and ran sql to produce a simple output of `x, y, text` (x and y of the centroid of the polygon), which I then exported as a gmt chewable csv file;

```
SELECT st_x(Centroid(geom)), st_y(Centroid(geom)), text FROM table
```

---

<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:** [June 15, 2022, 11:30am UTC](https://forum.generic-mapping-tools.org/t/ogr-gmt-using-aspatial-field-for-label/3019/6 "2022-06-15T11:30:07Z")

</div>

> [@Andreas](#):
>
> One day

Petty, because `ogr2ogr` is wrapped so it should really be a one-liner. Except that I’m still to learn how to use it and hidden bugs are always around the corner.

---

<div class="post-metadata">

**Author:** ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forum.generic-mapping-tools.org/u/Andreas)\
**Post date:** [June 15, 2022, 11:57am UTC](https://forum.generic-mapping-tools.org/t/ogr-gmt-using-aspatial-field-for-label/3019/7 "2022-06-15T11:57:17Z")

</div>

I’m fairly sure I could’ve done this with a one liner in `ogr2ogr` as well. But, it’s so nice to get a visual confirmation that what you’re doing (at least) looks right.

---

<div class="post-metadata">

**Author:** ![mkononets](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/mkononets/32/4029_2.png) [@mkononets](https://forum.generic-mapping-tools.org/u/mkononets)\
**Post date:** [June 27, 2022, 7:40pm UTC](https://forum.generic-mapping-tools.org/t/ogr-gmt-using-aspatial-field-for-label/3019/8 "2022-06-27T19:40:56Z")

</div>

> [@Andreas](#):
>
> `SELECT st_x(Centroid(geom)), st_y(Centroid(geom)), text FROM table`

You can run your spatialite code directly in `ogr2ogr`, as it is usually built with spatialite support:

`ogr2ogr -f CSV filename.csv -dialect SQLite -sql "SELECT ST_X(Centroid(GEOMETRY)), ST_Y(Centroid(GEOMETRY)), text FROM filename" filename.gmt`

you can even add a “layer creation option” ` -lco SEPARATOR=TAB` or `SPACE` to skip handling commas.

---

<div class="post-metadata">

**Author:** ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forum.generic-mapping-tools.org/u/Andreas)\
**Post date:** [June 28, 2022, 7:02am UTC](https://forum.generic-mapping-tools.org/t/ogr-gmt-using-aspatial-field-for-label/3019/9 "2022-06-28T07:02:57Z")

</div>

Thanks @mkononets!

---

<div class="post-metadata">

**Author:** ![chhei-s](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/chhei-s/32/1028_2.png) [@chhei-s](https://forum.generic-mapping-tools.org/u/chhei-s)\
**Post date:** [June 29, 2022, 7:33am UTC](https://forum.generic-mapping-tools.org/t/ogr-gmt-using-aspatial-field-for-label/3019/10 "2022-06-29T07:33:51Z")

</div>

One click solution in QGIS is `Vector` -\> `Geometry tools` -\> `Centroids`. Good thing here is that the resulting file also contains all your metadata so that you can pass `x, y, MyLabelAttribute` to `gmt text`

---

<div class="post-metadata">

**Author:** ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forum.generic-mapping-tools.org/u/Andreas)\
**Post date:** [June 29, 2022, 7:49am UTC](https://forum.generic-mapping-tools.org/t/ogr-gmt-using-aspatial-field-for-label/3019/11 "2022-06-29T07:49:33Z")

</div>

Thanks for the tips @chhei-s.

---

<div class="post-metadata">

**Author:** ![mkononets](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/mkononets/32/4029_2.png) [@mkononets](https://forum.generic-mapping-tools.org/u/mkononets)\
**Post date:** [June 29, 2022, 2:07pm UTC](https://forum.generic-mapping-tools.org/t/ogr-gmt-using-aspatial-field-for-label/3019/12 "2022-06-29T14:07:48Z")

</div>

> [@chhei-s](#):
>
> Good thing here is that the resulting file also contains **all your metadata**

very good remark. It is possible to do the same on command-line using `*` (wildcard) in SELECT:

```auto
ogr2ogr -f CSV filename.csv \
        -dialect SQLite \
        -sql "SELECT ST_X(Centroid(GEOMETRY)), ST_Y(Centroid(GEOMETRY)), * FROM filename" \
        filename.gmt

```
