Question

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.

Was it helpful?

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

OTHER TIPS

You need the following workflow to convert a .csv of coordinates to a feature class using the Python arcpy site-package:

  1. Make XY Event Layer (Data Management) converts the tabular data to a temporary spatial layer
  2. Feature Class To Feature Class (Conversion) converts the layer to a permanent feature class

This should get you started.

import arcpy
from arcpy import env

# Set environment settings
env.workspace = "C:/data"
ws = env.workspace

# Set the local variables
in_Table = "your_table.csv"
x_coords = "POINT_X"
y_coords = "POINT_Y"
z_coords = "POINT_Z"
out_Layer = "your_layer"

# Set the spatial reference--this is simply a path to a .prj file
spRef = r"Coordinate Systems\Projected Coordinate Systems\Utm\Nad 1983\NAD 1983 UTM Zone 11N.prj"

# Make the XY event layer...
arcpy.MakeXYEventLayer_management(in_Table, x_coords, y_coords, out_Layer, spRef, z_coords)

# Now convert to a feature class
arcpy.FeatureClassToFeatureClass_conversion (out_layer, ws, "out.shp")

I did not have success with any of the solutions here but I was able come up with a solution that worked using Python's shapely and fiona modules. It uses a tab-delineated .ascii file (my preference as opposed to .csv) but can easily be adapted to use a .csv as in the question posed. Hopefully this is helpful someone else trying to automate this same task.

# ------------------------------------------------------
# IMPORTS
# ------------------------------------------------------

import os
import pandas as pd
from shapely.geometry import Point, mapping
from fiona import collection

# ------------------------------------------------------
# INPUTS
# ------------------------------------------------------

# Define path
path = os.path.abspath(os.path.dirname(__file__))

# Set working directory
os.chdir(path)  

# Define file to convert
file = 'points.ascii'

# Define shp file schema
schema = { 'geometry': 'Point', 'properties': { 'LocationID': 'str', 'Latitude': 'float', 'Longitude': 'float' } }

# Read in data
data = pd.read_csv(file, sep='\t') 

# Define shp file to write to
shpOut = 'points.shp'

# Create shp file
with collection(shpOut, "w", "ESRI Shapefile", schema) as output:
    # Loop through dataframe and populate shp file
    for index, row in data.iterrows():
        
        # Define point
        point = Point(row['Longitude'], row['Latitude'])
        # Write output
        output.write({
            'properties': {'LocationID': row['LocationID'], 'Latitude': row['Latitude'], 'Longitude': row['Longitude'] }, 
            'geometry': mapping(point)
        })
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top