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.
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.
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
You need the following workflow to convert a .csv of coordinates to a feature class using the Python arcpy site-package:
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)
})
© 2022 - 2024 — McMap. All rights reserved.