# Plot shapefiles in X-Y coordinates

**URL:** <https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851>\
**Category:** Q&A\
**Created:** [April 26, 2022, 3:15pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851 "2022-04-26T15:15:41Z")\
**Posts on this page:** 20\
**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:** [April 26, 2022, 3:15pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/1 "2022-04-26T15:15:42Z")

</div>

Hi there,

I’m not used to work with shapefile format and I have a bunch I’d like to process for a geologic map.

There are actually 8 file formats associated to the data :

- .cpg
- .dbf
- .lyr
- .prj
- .sbn
- .sbx
- .shp
- .shx

I think only **shp** and **shx** are useful for GMT, the others being for ArcGIS, I suspect. Again, I’m not sure… I don’t even know how to extract information properly from those.

```auto
gmt info file_2154.shp
    file_2154.shp: N = 24303	<84725.096698/1201589.26104>	<6032821.09236/7106074.34002>

```

As you can see, the units are in meter (UTM). From the documentation I know that the projection is Lambert II extended (`-Jl`) centred at {3ºE ; 46º30’N} (_or 700000/6600000_) , using Clarke-1880-IGN ellipsoïd.

In order to have a better grip on GMT, I’d like to convert the file to full WGS84 lon/lat system. When I explore the `mapproject` module, I got a bunch of errors, either because of the units (`-R[...]+u1`) or because of the projection

```auto
area="-R-22841.3905/1712212.6192/1087335.9605/2703971.1254+u1"
projection="-Jl700000/6600000/44/49/1:50000"
gmt mapproject $area $projection $data -C -E -I -F --PROJ_ELLIPSOID=Clarke-1880-IGN > test.txt
    Option -R: The +u<unit> modifier only applies to projected coordinates. Your unit 1 is ignored

```

Ultimately, I’d like to plot all the files, associated colours to strata and everything. But for that, I need to understand how to deal with them.

Any help ?

I can provide some files if needed (shp ~20Mb , shx ~20Kb)

Guillaume

---

<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:** [April 26, 2022, 3:30pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/2 "2022-04-26T15:30:22Z")

</div>

Did you try +ue for meter? +u1 is not a recognized thing.

---

<div class="post-metadata">

**Author:** ![Rom1](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/rom1/32/4955_2.png) [@Rom1](https://forum.generic-mapping-tools.org/u/Rom1)\
**Post date:** [April 26, 2022, 4:30pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/3 "2022-04-26T16:30:09Z")

</div>

When I need to plot shapefile I use ogr2ogr to convert the shape to a GMT multiple segment file

for example

```auto
ogr2ogr -f "GMT" $gmt_format_file $shape_file
gmt mapproject $gmt_format_file ...
gmt psxy $gmt_format_file ...

```

ogr2ogr can be found with gdal library

Hope it can help you

---

<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:** [April 26, 2022, 4:36pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/4 "2022-04-26T16:36:29Z")

</div>

> [@pwessel](#):
>
> Did you try +ue for meter? +u1 is not a recognized thing.

I tried, it doesn’t solve the issue. My text file has a first column filed with 80-ish values and a second column of zeros.

> [@Rom1](#):
>
> When I need to plot shapefile I use ogr2ogr to convert the shape to a GMT multiple segment file

I think GMT does that under the hood.  
I’m currently able to plot the shape file “as is”, but I don’t know how to handle the informations / values and so on, so I can’t associate a colour to specific entries

---

<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:** [April 26, 2022, 5:24pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/5 "2022-04-26T17:24:02Z")

</div>

- All of those .xxx are part of the _shapefile_ (no ArcGis involved) though the `.cpg` and `.lyr` are new to me.

- `mapproject` requires **-R** in geogs, even when doing inverse projections. That’s a quite annoying thing, I agree.

- doing it with Julia should be pretty straightforward.

---

<div class="post-metadata">

**Author:** ![EJFielding](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/ejfielding/32/82_2.png) [@EJFielding](https://forum.generic-mapping-tools.org/u/EJFielding)\
**Post date:** [April 26, 2022, 5:45pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/6 "2022-04-26T17:45:26Z")

</div>

You can do the projection change with GDAL also. It will get the input projection information from the shapefile data automatically. Look at the options for “ogr2ogr” that can both project and convert to GMT vectors.

---

<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:** [April 26, 2022, 5:57pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/7 "2022-04-26T17:57:23Z")

</div>

Check .shp metadata using ogrinfo

`ogrinfo -so file_2154.shp`

it’ll show layer names. then run the ogrinfo with the layer name appended to get that layer metadata.  
`-so` means “summary only”, without it ogrinfo will dump all the file/layer content on your terminal.

---

<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:** [April 26, 2022, 7:20pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/8 "2022-04-26T19:20:18Z")

</div>

> [@Joaquim](#):
>
> - doing it with Julia should be pretty straightforward.

Hehehe. Always.

> [@Joaquim](#):
>
> `mapproject` requires **-R** in geogs, even when doing inverse projections. That’s a quite annoying thing, I agree.

```auto
area="-R10/20/33/54+ue"
projection="-Jl3/46.5/44/49/1:50000"
gmt mapproject $area $projection $data -C -E -I -F > test.txt

```

`test.txt` stays with 1 column of 80-ish values and 1 column of zeros.

> [@EJFielding](#):
>
> Look at the options for “ogr2ogr” that can both project and convert to GMT vectors.

Running this command (debug) :

```auto
gmt plot -W -Bafg file_2154.shp -png test -Vd

```

I got this :

```auto
gmtinfo [DEBUG]: Running ogr2ogr -mapFieldType Integer64=Integer -skipfailures -f "OGR_GMT" "gmt_ogr_5387.gmt" "file_2154.shp"

Revised options: -W -Bafg file_2154.shp -Vd -R40000/1280000/6000000/7160000 -JX15c/15c

Plotting segment 0
...
Plotting segment 80072

```

(if I plot all the files, the figure is quite nice even without colouring. This is “just” one file :

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

> [@mkononets](#):
>
> Check .shp metadata using ogrinfo

All it gives me is :

```auto
INFO: Open of `file_2154.shp'
      using driver `ESRI Shapefile' successful.
1: file_2154 (Line String)

```

Nothing else

---

<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:** [April 26, 2022, 7:54pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/9 "2022-04-26T19:54:32Z")

</div>

> [@PlanetGus](#):
>
> `1: file_2154 (Line String)`

then run the ogrinfo with the layer name appended to get that layer metadata:  
`ogrinfo -so file_2154.shp file_2154`

“Line string” means contour lines if i remember correctly

---

<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:** [April 26, 2022, 8:05pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/10 "2022-04-26T20:05:51Z")

</div>

Ok, that gave the information I can read from the `.prj` file (projection, ellipsoid …)

If I remove the “summary”, I got the polygon coordinates for each element (\>80k). Now I need to find what these elements are supposed to be.

---

<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:** [April 26, 2022, 8:17pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/11 "2022-04-26T20:17:23Z")

</div>

There should be something else right under the layer projection info, a list of contour feature parameters (set at some values for every contour).

Can be easier to run ogr2ogr exactly as gmt does under the hood and study the header of the output file and few first records.  
`ogr2ogr -mapFieldType Integer64=Integer -skipfailures -f "OGR_GMT" "gmt_ogr_5387.gmt" "file_2154.shp"`

---

<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:** [April 26, 2022, 8:26pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/12 "2022-04-26T20:26:07Z")

</div>

> [@mkononets](#):
>
> ogr2ogr -mapFieldType Integer64=Integer -skipfailures -f “OGR\_GMT” “gmt\_ogr\_5387.gmt” “file\_2154.shp”

Yep, I got that :

```auto
# @NCODE|DESCR|TERRE_MER|CODE_LEG|NOM_SYMB|WT_SYMB|C_SYMB|M_SYMB|J_SYMB|N_SYMB
# @Tinteger|string|string|integer|string|double|double|double|double|double

```

Where CODE\_LEG | NOM\_SYMB | … refer to the spatial database somehow

---

<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:** [April 26, 2022, 8:35pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/13 "2022-04-26T20:35:14Z")

</div>

Now you actually need to check documentation hopefully accompanying your shapefile data. Otherwise it is going to be very hard to guess the meaning of all these parameters:  
`CODE|DESCR|TERRE_MER|CODE_LEG|NOM_SYMB|WT_SYMB|C_SYMB|M_SYMB|J_SYMB|N_SYMB`

Well, DESCR must be some sort of description string. Others are not clear. Every contour header should have all these parameters set to some values (according to the value types). Once you know what exactly you want, you can extract it with something like  
`ogr2ogr -where CODE_LEG=some_value -f GMT "gmt_ogr_5387.gmt" "file_2154.shp"`

---

<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:** [April 26, 2022, 8:45pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/14 "2022-04-26T20:45:23Z")

</div>

Thanks @mkononets 🙂  
The doc talk about the structure of the database, but not so much about the parameters themselves. Hopefully the “description” will help !

---

<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:** [April 26, 2022, 8:53pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/15 "2022-04-26T20:53:14Z")

</div>

Ok ! So another file contains actual polygons (surfaces) with explicit names. There’s a code associated starting with `@` (e.g. `@Dq3`).  
Is it possible to list all those then use `[..] -where CODE=@Dq3 [...]` to split the file into small bricks ?

---

<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:** [April 26, 2022, 9:03pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/16 "2022-04-26T21:03:36Z")

</div>

> [@PlanetGus](#):
>
> Is it possible to list all those then use `[..] -where CODE=@Dq3 [...]` to split the file into small bricks ?

I think you need to exclude `@D`, which indicates the beginning of the parameter list  
try `-where CODE=123` if CODE is a number (type real or integer) or `-where CODE='123'` if CODE is a string type

---

<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:** [April 26, 2022, 9:28pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/17 "2022-04-26T21:28:03Z")

</div>

Hm, I tried several ways … it oddly doesn’t work.

I switched to an easier test. Using `ogrinfo -so` I’ve isolated this one :

```auto
TERRE_MER: String (1.0)

```

Which has “T” or “M” as value

```auto
ogr2ogr -where TERRE_MER='T' -f GMT "file_2154.gmt" "file_2154.shp"
    ERROR 1: "T" not recognised as an available field.
    ERROR 1: SetAttributeFilter(TERRE_MER=T) on layer 'file_2154' failed.

```

`'T'`, `T` or `"T"` doesn’t change anything

---

<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:** [April 26, 2022, 9:44pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/18 "2022-04-26T21:44:25Z")

</div>

try putting the expression after `-where` in double quotes:  
`ogr2ogr -where "TERRE_MER='T'" -f GMT "file_2154.gmt" "file_2154.shp"`

---

<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:** [April 26, 2022, 9:46pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/19 "2022-04-26T21:46:44Z")

</div>

Alright ! That throw a bunch of warning due to Integer conversion. But it works.

Thanks again @mkononets. Now I have to define what I want and how to convert it 🙂

---

<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:** [April 26, 2022, 10:01pm UTC](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851/20 "2022-04-26T22:01:19Z")

</div>

gmt uses `-mapFieldType Integer64=Integer` to avoid Integer64 to Real conversion warnings  
there’s also `-skipfailures` but this might not let you see if something went radically wrong.

BTW you do not necessarily have to convert to GMT format. If you leave it out and give some other .shp output filename, the result will also be in .shp format (with no potentially incompatible type conversions). However .gmt files are human-readable, which is helpful sometimes.

[Next page](https://forum.generic-mapping-tools.org/t/plot-shapefiles-in-x-y-coordinates/2851.md?page=2)
