3

I use the gdal executable in QGIS file, with a DOS command.

I have a csv file like this (test.csv) :

  X       Y
-5.48   42.81
-4.78   42.52
-5.06   42.02

This coordinates are GCP. I would like to convert my csv file in a VRT. On the internet, I found example like :

<OGRVRTDataSource>
    <OGRVRTLayer name="test">
        <SrcDataSource>test.csv</SrcDataSource>
        <GeometryType>wkbPolygon</GeometryType>
        <LayerSRS>WGS84</LayerSRS>
        <Field name="id" src="id" />
        <GeometryField encoding="WKT" field="geo" />
    </OGRVRTLayer>
</OGRVRTDataSource>

Simply, I don't know where to put this line. Should I put them in a DOS command in my QGIS file ?

2 Answers 2

5

If you want to work with GDAL on command line, you have to change your input file to comma delimiters:

X,Y
-5.48,42.81
-4.78,42.52
-5.06,42.02

The appropriate vrt file for it is:

<OGRVRTDataSource>
    <OGRVRTLayer name="test">
        <SrcDataSource>test.csv</SrcDataSource>
        <GeometryType>wkbPoint</GeometryType>
        <LayerSRS>WGS84</LayerSRS>
        <GeometryField encoding="PointFromColumns" x="X" y="Y"/>
    </OGRVRTLayer>
</OGRVRTDataSource>

Note that the source file name, geometry type, and geometry column names must match your data file.

You can test it with ogrinfo test.vrt on the DOS command line.

Alternatively, since you have QGIS already installed, you can load the file directly (even with blanks) as Delimited Text (the icon with the comma).

5
  • It works with other separators too: "The driver will also accept a semicolon, a tabulation or a space (GDAL >= 2.0 for the later) character as field separator . This autodetection will work only if there's no other potential separator on the first line of the CSV file. Otherwise it will default to comma as separator." gdal.org/drv_csv.html Jul 17, 2015 at 11:13
  • 1
    True, but it does not work with fixed formatting (i.e. padded with blanks) as given in the question. ogrinfo will not open it (not saying that it does not like the separators). BTW we do not yet work with GDAL 2.0 in QGIS.
    – AndreJ
    Jul 17, 2015 at 11:17
  • @AndreJ Thank you for your help, but do you know where can I build this vrt file ? I don't use QGIS directly because I will insert lately the gdal modul to a software
    – Manimalis
    Jul 17, 2015 at 12:11
  • From the manual page gdal.org/drv_vrt.html: The virtual files are currently normally prepared by hand. It is not very difficult to understand the logic behind it.
    – AndreJ
    Jul 17, 2015 at 12:44
  • With blank padded numbers you would need a <OpenOptions><OOI key="MERGE_SEPARATOR">YES</OOI></OpenOptions> element per gdal.org/drivers/vector/csv.html and gdal.org/drivers/vector/vrt.html In QGIS, I often need a relativeToVRT attribute like: <SrcDataSource relativeToVRT="1">test.csv</SrcDataSource> if the data doesn't happen to be stored in the same directory as the app.
    – Dave X
    Sep 8, 2021 at 3:12
3
<OGRVRTDataSource>
    <OGRVRTLayer name="test">
        <SrcDataSource>test.csv</SrcDataSource>
        <GeometryType>wkbPolygon</GeometryType>
        <LayerSRS>WGS84</LayerSRS>
        <Field name="id" src="id" />
        <GeometryField encoding="WKT" field="geo" />
    </OGRVRTLayer>
</OGRVRTDataSource>

is the content of a VRT file. Just save it as test.csv.vrt for example. Then, if you wrote it correctly, you can use the OGR tools on it, for example ogrinfo test.csv.vrt.

In your case it would probably need to have geometry definitions like

<GeometryType>wkbPoint</GeometryType>
<GeometryField encoding="PointFromColumns" x="X" y="Y" />

You do not have a "id" field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.