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.
ogrinfo d:/Temp/example.vrt -al
? And shouldn't you write out the layer "lyr" and not the data source "in_ds"?