ogr2ogr Or arcpy for csv to shapefile?
Asked Answered
M

3

5

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.

Military answered 19/3, 2014 at 6:0 Comment(0)
R
13

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

Robot answered 17/6, 2014 at 14:58 Comment(4)
What are other options for the geometry type? I'm going to give this a shop. the response is much appreciated.Military
There are several other geometry types. Unfortunately I never used it for anything but converting points to shapes. Which kind of geometry do you want to create? You can find the description of gdal's vrt-driver hereRobot
I'll be using it for points however I didn't know what the wkb stands for or if there are different types of points. I just need a standard point shapefile. in my csv I also have other columns with text that need to be added to the shapefile as attributes.Military
Well, for standard point shapefiles, the easiest way to go is the method described above. Adding additional information as attributes, is thereby handled automatically. If you want to have any specific field definitions in your shapefile, you can create a so-called csvt-file, which holds the information. For the example above, it would contain: "Real","Real","Real". Other definitions are available as well, find them hereRobot
K
1

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")
Kahl answered 24/3, 2014 at 20:22 Comment(0)
M
0

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)
        })
Minne answered 10/8, 2020 at 20:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.