#参考来源:http://www.faqssys.info/creating-shp-from-txt-files-using-arcpy/#comments
import os, sys, arcpy
InFile = sys.argv[1]
OutShp = sys.argv[2]
EPSG = sys.argv[3]
try:
SR = arcpy.SpatialReference(int(EPSG))
# create a spatial reference from EPSG definition
except:
arcpy.AddWarning(arcpy.GetMessages())
arcpy.AddError("Unable to create spatial reference with code %s" % EPSG)
sys.exit(0)
#break up OutShp to folder and name
OutFolder = os.path.dirname(OutShp)
OutName = os.path.basename(OutShp)
# Create the output feature class/创建函数
arcpy.CreateFeatureclass_management(OutFolder,OutName,"POINT",has_z="ENABLED",spatial_reference=SR)
# add fields to store the values
arcpy.AddField_management(OutShp,"PntID","INTEGER")
arcpy.AddField_management(OutShp,"Xcoord","DOUBLE")
arcpy.AddField_management(OutShp,"Ycoord","DOUBLE")
arcpy.AddField_management(OutShp,"Zcoord","DOUBLE")
with open(InFile,'r') as srcFile:
with arcpy.da.InsertCursor(OutShp,["SHAPE@","PntID","Xcoord","Ycoord","Zcoord"]) as InsCur:
for fileLine in srcFile:
# split the line up into a list
# 1 337172.8 4278952.4 85.7334909091 becomes [1,337172.8,4278952.4,85.7334909091]
# try a few common delimiters: tab, space, comma, pipe../确定数据间隔符类型:tab、空格、逗号
lSplit = fileLine.split("t")
if len(lSplit) == 1:
lSplit = fileLine.split(" ")
if len(lSplit) == 1:
lSplit = fileLine.split(",")
if len(lSplit) == 1:
lSplit = fileLine.split("|")
if len(lSplit) > 1:
# more than just one word on the line
pointsOK = True
try:
ID = int(lSplit[0])
Xcoord = float(lSplit[1])
Ycoord = float(lSplit[2])
Zcoord = float(lSplit[3])
except:
arcpy.AddWarning("Unable to translate points")
pointsOK = False
if pointsOK:
# create a point geometry from the 3 coordinates
newGeom = arcpy.PointGeometry(arcpy.Point(Xcoord,Ycoord,Zcoord))
newGeom.spatial_reference = SR # set spatial reference
# you could project here using newGeom.projectAs(SpatialRef)
# if you needed them in a different spatial reference
InsCur.insertRow([newGeom,ID,Xcoord,Ycoord,Zcoord])# insert this point into the feature class