首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ogr2ogr还是csv到shapefile的弧?

ogr2ogr还是csv到shapefile的弧?
EN

Stack Overflow用户
提问于 2014-03-19 06:00:49
回答 3查看 6.7K关注 0票数 5

ogr2ogr或arcpy可以直接将csv转换为shapefile吗?我正试图用一个小脚本来自动化一些进程,并希望我可以轻松地使用ogr2ogr或arcpy,这是我所熟悉的。

如有任何意见,将不胜感激。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-06-17 14:58:26

它可以很容易地用ogr2ogr完成。

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

coord.csv

代码语言:javascript
复制
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

代码语言:javascript
复制
<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>

然后跑:

代码语言:javascript
复制
ogr2ogr -f "ESRI Shapefile" . coord.csv && ogr2ogr -f "ESRI Shapefile" . coord.vrt

这将在坐标系中给出"output.shp“,这是在示例文件中指定的。

致以敬意,

穆沙夫

票数 13
EN

Stack Overflow用户

发布于 2014-03-24 20:22:47

您需要以下工作流才能使用Python站点包将坐标的.csv转换为特性类:

  1. 使XY事件层(数据管理)将表格数据转换为临时空间层。
  2. 特征类到特征类(转换)将该层转换为永久的特性类。

这应该能让你开始。

代码语言:javascript
复制
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")
票数 1
EN

Stack Overflow用户

发布于 2020-08-10 20:51:50

这里的任何解决方案我都没有成功,但我想出了一个使用Python的shapely和fiona模块的解决方案。它使用一个选项卡描述的.ascii文件(我的首选项,而不是.csv),但是可以很容易地调整为使用.csv,就像提出的问题一样。希望这能帮助其他人自动完成同样的任务。

代码语言:javascript
复制
# ------------------------------------------------------
# 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)
        })
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22497541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档