# Shapefile to gmt (python)

**URL:** <https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834>\
**Category:** PyGMT Q&A\
**Created:** [September 2, 2020, 10:51am UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834 "2020-09-02T10:51:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saren](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@Saren](https://forum.generic-mapping-tools.org/u/Saren)\
**Post date:** [September 2, 2020, 10:51am UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/1 "2020-09-02T10:51:35Z")

</div>

Hello,  
I’m trying to import a fault map in shapefile format that I created with QGIS in gmt through python.  
I found a solution in Julia but not pyhton in a related topic and I don’t know what synthax I should use to import my shapefile…  
I’d like to mention that my QGIS version doesn’t have the GMT option to export my shapefile under a GMT format.  
Thanks in advance!

---

<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:** [September 2, 2020, 11:15am UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/2 "2020-09-02T11:15:04Z")

</div>

I know little about pygmt but GMT can plot data from shapefiles, so if you don’t want to import the data into python (like the Julia wrapper does), I guess you can make pygmt plot shpafile data too.

Another alternative is to use the Julia wrapper from python using `pyjulia`, then you’ll have the full power of the GMT.jl wrapper from python.

---

<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:** [September 2, 2020, 11:18am UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/3 "2020-09-02T11:18:21Z")

</div>

> [@Saren](#):
>
> I’d like to mention that my QGIS version doesn’t have the GMT option to export my shapefile under a GMT format.

That is done by the GDAL `ogr2ogr` program. GMT does that under the hood when called with a `.shp`

---

<div class="post-metadata">

**Author:** ![Saren](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@Saren](https://forum.generic-mapping-tools.org/u/Saren)\
**Post date:** [September 2, 2020, 12:11pm UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/4 "2020-09-02T12:11:50Z")

</div>

I have no idea how to install pyjulia… I started using python last semester and I’m only using very basic functions…  
I tried to import my shapefile via pandas, but I don’t know what kind of separators I should use and when I try pygmt.fig.plot(‘fault.shp’) I get an error : ‘must specify x and y’ but I don’t know where x and y are stored in the shapefile file…

---

<div class="post-metadata">

**Author:** ![Saren](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@Saren](https://forum.generic-mapping-tools.org/u/Saren)\
**Post date:** [September 2, 2020, 12:13pm UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/5 "2020-09-02T12:13:58Z")

</div>

In the GDAL modules I can’t find GMT format, there is GXT, GML but not GMT (I’m looking for an older version of QGIS right now, maybe I’ll find one that can do the work)

---

<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:** [September 2, 2020, 1:13pm UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/6 "2020-09-02T13:13:10Z")

</div>

> [@Saren](#):
>
> I have no idea how to install pyjulia…

googling … [Welcome to PyJulia’s documentation! — PyJulia 0.6.1 documentation](https://pyjulia.readthedocs.io/en/latest/)

---

<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:** [September 2, 2020, 1:14pm UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/7 "2020-09-02T13:14:48Z")

</div>

> [@Saren](#):
>
> In the GDAL modules I can’t find GMT format

The gmt format has been around for many hears. Your QGis certainly have it too.

```auto
ogr2ogr -f gmt file.shp file.gmt

```

---

<div class="post-metadata">

**Author:** ![Saren](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@Saren](https://forum.generic-mapping-tools.org/u/Saren)\
**Post date:** [September 2, 2020, 2:13pm UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/8 "2020-09-02T14:13:00Z")

</div>

Thanks for your quick answers,  
I’m sorry I have dumb questions, but how does julia works once it’s imported in python? Do I use the pygmt environment? Maybe the easier way is to use the line code you gave, where do I have to enter it? And when the file is in in the gmt format, how do I import it in pygmt?

---

<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:** [September 2, 2020, 2:37pm UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/9 "2020-09-02T14:37:36Z")

</div>

You do like pythons do (but not much tested, I confess. As I said, I’m not a Py-person)

```auto
from julia import GMT

```

See also  
[https://flint.soest.hawaii.edu/t/pygmt-plotting-multiple-lines/102/5](https://flint.soest.hawaii.edu/t/pygmt-plotting-multiple-lines/102/5)

The `ogr2ogr` is a GDAL program so you run it ih the command line (the shell).

---

<div class="post-metadata">

**Author:** ![seisman](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/seisman/32/4_2.png) [@seisman](https://forum.generic-mapping-tools.org/u/seisman)\
**Post date:** [September 2, 2020, 3:28pm UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/10 "2020-09-02T15:28:52Z")

</div>

> [@Saren](#):
>
> I tried to import my shapefile via pandas, but I don’t know what kind of separators I should use and when I try pygmt.fig.plot(‘fault.shp’) I get an error : ‘must specify x and y’ but I don’t know where x and y are stored in the shapefile file…

In PyGMT, you can try `fig.plot(data="fault.shp")`

---

<div class="post-metadata">

**Author:** ![Saren](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@Saren](https://forum.generic-mapping-tools.org/u/Saren)\
**Post date:** [September 2, 2020, 3:49pm UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/11 "2020-09-02T15:49:43Z")

</div>

Ok will try to do this with pyjulia.  
Just another dumb question: can I “mix” pygmt plots and GMT.jl plots in the same program? Maybe I should have started by explaining my objective: I want to built a map with earthquakes from 2018 (which I already did, thanks to the very nice tutos and help from the team 😉) and add a map of the faults I created under QGIS (my shapefile). This map is part of a powerpoint that I’m going to present (soon I hope) during my dissertation viva and pyGMT makes it easy to have a precise and esthetic map. I imported the earthquake map on QGIS and added the fault layer but the result is “ugly”, mainly because I used the gmt map as a georeferenced raster (I didn’t know how to do otherwise)… By looking further in the QGIS options I saw that it possible to export my shapefile as kml, (but not GMT), can I load the kml file in pygmt?  
thanks in advance

---

<div class="post-metadata">

**Author:** ![Saren](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@Saren](https://forum.generic-mapping-tools.org/u/Saren)\
**Post date:** [September 2, 2020, 3:53pm UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/12 "2020-09-02T15:53:36Z")

</div>

Apperently shp format isn’t supported :  
ERROR 4: Unable to open Failles.shx or Failles.SHX. Set SHAPE\_RESTORE\_SHX config option to YES to restore or create it.  
FAILURE:  
Unable to open datasource `Failles.shp' with the following drivers. -> `PCIDSK’  
-\> `netCDF' -> `PDS4’  
-\> `JP2OpenJPEG' -> `PDF’  
-\> `MBTiles' -> `EEDA’  
-\> `DB2ODBC' -> `ESRI Shapefile’  
-\> `MapInfo File' -> `UK .NTF’  
-\> `OGR_SDTS' -> `S57’  
-\> `DGN' -> `OGR\_VRT’  
-\> `REC' -> `Memory’  
-\> `BNA' -> `CSV’  
-\> `NAS' -> `GML’  
-\> `GPX' -> `KML’  
-\> `GeoJSON' -> `GeoJSONSeq’  
-\> `ESRIJSON' -> `TopoJSON’  
-\> `OGR_GMT' -> `GPKG’  
-\> `SQLite' -> `ODBC’  
-\> `WAsP' -> `PGeo’  
-\> `MSSQLSpatial' -> `PostgreSQL’  
-\> `OpenFileGDB' -> `XPlane’  
-\> `DXF' -> `CAD’  
-\> `Geoconcept' -> `GeoRSS’  
-\> `GPSTrackMaker' -> `VFK’  
-\> `PGDUMP' -> `OSM’  
-\> `GPSBabel' -> `SUA’  
-\> `OpenAir' -> `OGR\_PDS’  
-\> `WFS' -> `WFS3’  
-\> `HTF' -> `AeronavFAA’  
-\> `Geomedia' -> `EDIGEO’  
-\> `GFT' -> `SVG’  
-\> `CouchDB' -> `Cloudant’  
-\> `Idrisi' -> `ARCGEN’  
-\> `SEGUKOOA' -> `SEGY’  
-\> `XLS' -> `ODS’  
-\> `XLSX' -> `ElasticSearch’  
-\> `Walk' -> `Carto’  
-\> `AmigoCloud' -> `SXF’  
-\> `Selafin' -> `JML’  
-\> `PLSCENES' -> `CSW’  
-\> `VDV' -> `GMLAS’  
-\> `MVT' -> `TIGER’  
-\> `AVCBin' -> `AVCE00’  
-\> `NGW' -> `HTTP’

And when I tried with the .shx file:  
plot [WARNING]: Mismatch between actual (1) and expected (2) fields near line 3 in file  
plot [ERROR]: Mismatch between actual (1) and expected (2) fields near line 2 in file Failles.shx

---

<div class="post-metadata">

**Author:** ![seisman](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/seisman/32/4_2.png) [@seisman](https://forum.generic-mapping-tools.org/u/seisman)\
**Post date:** [September 2, 2020, 3:55pm UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/13 "2020-09-02T15:55:40Z")

</div>

I know very little about shapefiles, but from my experience, when you use a file like `fault.shp`, you may need the `fault.shx` in the same directory.

---

<div class="post-metadata">

**Author:** ![Saren](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@Saren](https://forum.generic-mapping-tools.org/u/Saren)\
**Post date:** [September 2, 2020, 4:01pm UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/14 "2020-09-02T16:01:19Z")

</div>

I put all QGIS output files in my directory (.shp, .shx, .cpg, .dbf, .prj, .qpj), I don’t know much about shapefile either, maybe it’s because I created my shapefile layer with conditions (in order to have different symbols for different types of faults)? Do you know if I can use a kml file instead?

---

<div class="post-metadata">

**Author:** ![seisman](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/seisman/32/4_2.png) [@seisman](https://forum.generic-mapping-tools.org/u/seisman)\
**Post date:** [September 2, 2020, 4:07pm UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/15 "2020-09-02T16:07:09Z")

</div>

[https://docs.generic-mapping-tools.org/6.1/kml2gmt.html](https://docs.generic-mapping-tools.org/6.1/kml2gmt.html)

GMT has the `kml2gmt` module which can convert your KML files to the format that GMT can read.

---

<div class="post-metadata">

**Author:** ![Saren](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@Saren](https://forum.generic-mapping-tools.org/u/Saren)\
**Post date:** [September 2, 2020, 4:43pm UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/16 "2020-09-02T16:43:51Z")

</div>

I tried again and I get a message :  
translation of layer f-test1 (use -skipfailures to skip errors)  
How can I use this option?

---

<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:** [September 2, 2020, 4:53pm UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/17 "2020-09-02T16:53:38Z")

</div>

> [@Joaquim](#):
>
> ogr2ogr -f gmt file.shp file.gmt

I still think the above is the simplest. Probably this also works

```auto
gmt convert file.shp > file.dat

```

ad the use that `file.dat`.

No, you cant mix GMT.jl and pygmt commands. They work very differently.

Shapefile is a format that allows storing data qualified in types. E.g. lines, multilines, points, multipoints, polygons, etc but ONLY ONE type can be stored in the shp file. It also allows setting data attributes, which makes it a database format. Using it only to store xy data is a dumb thing that only makes life more miserable but unfortunately a very widespread habit.

Converting your data to kml should not give any gain.

---

<div class="post-metadata">

**Author:** ![Saren](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@Saren](https://forum.generic-mapping-tools.org/u/Saren)\
**Post date:** [September 2, 2020, 6:01pm UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/18 "2020-09-02T18:01:56Z")

</div>

ok thanks for your explainations (I guess I need to learn on the different format, how they work…)

I’m having a problem using your solution…

FAILURE:  
Unable to open datasource `Failles.gmt' with the following drivers. -> `JP2ECW’  
-\> `OCI' -> `SOSI’  
-\> `PCIDSK' -> `netCDF’  
-\> `PDS4' -> `JP2OpenJPEG’  
-\> `PDF' -> `MBTiles’  
-\> `EEDA' -> `DB2ODBC’  
-\> `ESRI Shapefile' -> `MapInfo File’  
-\> `UK .NTF' -> `OGR\_SDTS’  
-\> `S57' -> `DGN’  
-\> `OGR_VRT' -> `REC’  
-\> `Memory' -> `BNA’  
-\> `CSV' -> `NAS’  
-\> `GML' -> `GPX’  
-\> `LIBKML' -> `KML’  
-\> `GeoJSON' -> `GeoJSONSeq’  
-\> `ESRIJSON' -> `TopoJSON’  
-\> `Interlis 1' -> `Interlis 2’  
-\> `OGR_GMT' -> `GPKG’  
-\> `SQLite' -> `ODBC’  
-\> `WAsP' -> `PGeo’  
-\> `MSSQLSpatial' -> `OGR\_OGDI’  
-\> `PostgreSQL' -> `MySQL’  
-\> `OpenFileGDB' -> `XPlane’  
-\> `DXF' -> `CAD’  
-\> `Geoconcept' -> `GeoRSS’  
-\> `GPSTrackMaker' -> `VFK’  
-\> `PGDUMP' -> `OSM’  
-\> `GPSBabel' -> `SUA’  
-\> `OpenAir' -> `OGR\_PDS’  
-\> `WFS' -> `WFS3’  
-\> `HTF' -> `AeronavFAA’  
-\> `Geomedia' -> `EDIGEO’  
-\> `GFT' -> `SVG’  
-\> `CouchDB' -> `Cloudant’  
-\> `Idrisi' -> `ARCGEN’  
-\> `SEGUKOOA' -> `SEGY’  
-\> `XLS' -> `ODS’  
-\> `XLSX' -> `ElasticSearch’  
-\> `Walk' -> `Carto’  
-\> `AmigoCloud' -> `SXF’  
-\> `Selafin' -> `JML’  
-\> `PLSCENES' -> `CSW’  
-\> `VDV' -> `GMLAS’  
-\> `MVT' -> `TIGER’  
-\> `AVCBin' -> `AVCE00’  
-\> `NGW' -> `HTTP’

Do you think there is a problem with my shapefile?

---

<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:** [September 2, 2020, 6:09pm UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/19 "2020-09-02T18:09:16Z")

</div>

> [@Saren](#):
>
> `OGR_GMT`

I don’t know what commands you are running but apparently the format is called `OGR_GMT`. This ring some bell but I don’t remember more.

---

<div class="post-metadata">

**Author:** ![Saren](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@Saren](https://forum.generic-mapping-tools.org/u/Saren)\
**Post date:** [September 2, 2020, 7:15pm UTC](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/20 "2020-09-02T19:15:17Z")

</div>

I don’t have the gmt shell, I’m working on a windows computer, so I run the command you provided in OSGeoShell (the shell that QGIS provides) because according to a forum solution this is supposed to work…

[Next page](https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834.md?page=2)
