3

I want to convert a CSV file of point coordinates to a ShapeFile using OGR and Python. The example.csv file looks like this:

ID,X,Y
1,240007.000,647756.000
2,240008.000,647756.000
3,240009.000,647756.000
4,240020.000,647756.000
5,240021.000,647756.000
6,240022.000,647756.000
7,240023.000,647756.000
8,240024.000,647756.000
9,240025.000,647756.000
10,240026.000,647756.000

And I have created an OGR virtual dataset definition file with the *.vrt extension. It looks like this:

<OGRVRTDataSource>
    <OGRVRTLayer name="example">
        <SrcDataSource>D:\Temp\GCMRC\003L\example_short.csv</SrcDataSource>
        <SrcLayer>example</SrcLayer>
        <GeometryType>wkbPoint</GeometryType>
        <GeometryField encoding="PointFromColumns" x="X" y="Y"/>
    </OGRVRTLayer>
</OGRVRTDataSource>

I have written a simple Python script that tries to open a dataset from the CSV file, loop over the features (simply to prove they are legit) and then export the points to a ShapeFile. I got this to work on an example that I found, but cannot get it to work on my data posted here.

from osgeo import ogr

in_ds = ogr.Open("D:/Temp/example.vrt")
lyr = in_ds.GetLayer('example')
for feat in lyr:
    geom = feat.GetGeometryRef()
    print geom.ExportToWkt()

ogr.GetDriverByName("ESRI Shapefile").CopyDataSource(in_ds, "D:/Temp/example.shp")

The code crashes on the GetGeometryRef() call. It also produces an empty "line" ShapeFile.

  • 1
    What does the ogrinfo utility show with ogrinfo d:/Temp/example.vrt -al ? And shouldn't you write out the layer "lyr" and not the data source "in_ds"? – user30184 Jul 20 '16 at 21:06
6

Thanks to user30184! You helped me solve it.

The SrcLayer must specify the same name as the CSV file. Here is the correct, working VRT file. The change is on line 4:

<OGRVRTDataSource>
    <OGRVRTLayer name="example">
        <SrcDataSource>D:\Temp\example_short.csv</SrcDataSource>
        <SrcLayer>example_short</SrcLayer>
        <GeometryType>wkbPoint</GeometryType>
        <GeometryField encoding="PointFromColumns" x="X" y="Y"/>
    </OGRVRTLayer>
</OGRVRTDataSource>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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