ogr2ogr或arcpy for csv to shapefile?

时间:2014-03-19 06:00:49

标签: csv gis shapefile arcpy

ogr2ogr或arcpy可以直接csv进行shapefile转换吗? 我正在尝试用一个小脚本自动化一些进程,并希望我可以使用ogr2​​ogr或arcpy轻松完成,我是新手。

任何输入都将不胜感激。

3 个答案:

答案 0 :(得分:11)

可以轻松地使用ogr2​​ogr完成。

假设您有一个包含坐标的csv文件,例如(必须以逗号分隔):

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

然后你需要在同一目录中创建一个示例文件(根据你的csv命名):

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>

然后运行:

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

这将在您在样本文件中指定的坐标系中为您提供“output.shp”。

此致

muxav

答案 1 :(得分:1)

您需要以下工作流程才能使用Python arcpy site-package将.csv坐标转换为要素类:

  1. Make XY Event Layer (Data Management)将表格数据转换为临时空间图层
  2. Feature Class To Feature Class (Conversion)将图层转换为永久要素类

  3. 这应该让你开始。

    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")
    

答案 2 :(得分:0)

我在这里的任何解决方案都没有成功,但是我能够提出一个使用Python的shape和fiona模块工作的解决方案。它使用制表符描述的.ascii文件(相对于.csv是我的偏爱),但可以轻松地使其适应所提出的问题,以使用.csv。希望这对尝试自动化同一任务的其他人有所帮助。

# ------------------------------------------------------
# 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)
        })