Search code examples
csvgisshapefilearcpy

ogr2ogr Or arcpy for csv to shapefile?


Can ogr2ogr or arcpy do a direct csv to shapefile conversion? I'm trying to automate some processes with a small script and was hoping I can do it easily with ogr2ogr or arcpy which I'm new to.

Any input would be appreciated.


Solution

  • It can be done with ogr2ogr easily.

    Assuming you have a csv-file containing coordinates, like for example (has to be comma seperated):

    coord.csv

    x,y,z
    48.66080825,10.28323850,0
    48.66074700,10.28292000,0
    48.66075045,10.28249425,0
    48.66075395,10.28249175,0
    48.66077113,10.28233356,0
    48.66080136,10.28213118,0
    48.66079620,10.28196900,0
    

    Then you need to create a sample file (name it according to your csv) in the same directory:

    coord.vrt

    <OGRVRTDataSource>
      <OGRVRTLayer name="output">
        <SrcDataSource relativeToVRT="1">.</SrcDataSource>
        <SrcLayer>coord</SrcLayer>
        <GeometryType>wkbPoint</GeometryType>
        <LayerSRS>WGS84</LayerSRS>
        <GeometryField encoding="PointFromColumns" x="x" y="y"/>
      </OGRVRTLayer>
    </OGRVRTDataSource>
    

    Then run:

    ogr2ogr -f "ESRI Shapefile" . coord.csv && ogr2ogr -f "ESRI Shapefile" . coord.vrt
    

    This will give you "output.shp" in the coordinate system, you specified in the sample file.

    Regards,

    muxav