Shapefile to gmt (python)

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!

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.

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

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…

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)

googling … Welcome to PyJulia’s documentation! — PyJulia 0.6.1 documentation

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

ogr2ogr -f gmt file.shp file.gmt
1 Like

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?

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

from julia import GMT

See also
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).

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

1 Like

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 :wink:) 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

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

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.

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?

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.

1 Like

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

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

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.

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?

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.

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…