How can I convert an excel file with x, y columns to a point shapefile?
There are some somewhat optional requirements in addition to the correct creation of a shapefile:
- Column types (as per Excel's format specifiers) should be retained (especially date types)
- Column names should be taken from the header
- I would like to do this from the command-line
- If I can include heterogeneous spatial references for the points in a third column I would be very happy 🙂
Best Answer
I'd recommend using OGR/GDAL, which is part of the GDAL library. OGR supports a virtual format which allows specification via an XML file. If you convert your Excel worksheet into a CSV, you can generate a VRT to access the data.
Assuming you have something like this
example.csv
:You can create a VRT
example.vrt
as follows:Starting in GDAL 1.7, you can additionally specify the datatypes of attribute fields using the
<Field>
element inside of<OGRVRTLayer>
, like so:Keep in mind that shapefiles store attributes in the DBASE IV format which has less flexibility in data types than Excel. Once you've got your VRT file specified, you can use the normal OGR toolchain to convert the data into a Shapefile:
Unfortunately, #4 is not possible — the shapefile specification allows a single projection (viewable here in
example.prj
after the last step).