'''----------------------------------------------------------------------------------
Tool Name: CreateFeaturesFromTextFile
Source Name: CreateFeaturesFromTextFile.py
Version: ArcGIS 9.1
Author: Environmental Systems Research Institute Inc.
Required Argumuments: An Input Text File containing feature coordinates
An Input Character designating the decimal separator used in the text file.
An output feature class
Optional Arguments: A spatial reference can be specified. This will be the
spatial reference of the output fc.
Description: Reads a text file with feature coordinates and creates a feature class
from the coordinates.
----------------------------------------------------------------------------------''' import string, os, sys, locale, arcgisscripting
gp = arcgisscripting.create()
gp.overwriteoutput = 1 msgErrorTooFewParams = "Not enough parameters provided."
msgUnknownDataType = " is not a valid datatype. Datatype must be point, multipoint, polyline or polygon."
msgErrorCreatingPoint = "Error creating point %s on feature %s" # sets all the point properties
def createPoint(point, geometry):
try:
point.id = geometry[0]
point.x = geometry[1]
point.y = geometry[2]
# When empty values are written out from pyWriteGeomToTextFile, they come as 1.#QNAN
# Additionally, the user need not supply these values, so if they aren't in the list don't add them
if len(geometry) > 3:
if geometry[3].lower().find("nan") == -1: point.z = geometry[3]
if len(geometry) > 4:
if geometry[4].lower().find("nan") == -1: point.m = geometry[4]
return point
except:
raise Exception, msgErrorCreatingPoint try:
# get the provided parameters
inputTxtFile = open(gp.getparameterastext(0))
fileSepChar = gp.getparameterastext(1)
outputFC = gp.getparameterastext(2) # spatial reference is optional
outputSR = gp.getparameterastext(3) # make sure the text type specified in the text file is valid.
inDataType = inputTxtFile.readline().strip().lower()
dataTypes = ["point", "multipoint", "polyline", "polygon"]
if inDataType.lower() not in dataTypes:
msgUnknownDataType = "%s%s" % (inDataType, msgUnknownDataType)
raise Exception, msgUnknownDataType # create the new featureclass
gp.toolbox = "management"
gp.CreateFeatureclass(os.path.split(outputFC)[0], os.path.split(outputFC)[1], inDataType, "#", "ENABLED", "ENABLED", outputSR)
# create a new field to assure the id of each feature is preserved.
idfield = "File_ID"
gp.addfield(outputFC, idfield, "LONG")
# get some information about the new featureclass for later use.
outDesc = gp.describe(outputFC)
shapefield = outDesc.ShapeFieldName
# create the cursor and objects necessary for the geometry creation
rows = gp.insertcursor(outputFC)
pnt = gp.createobject("point")
pntarray = gp.createobject("Array")
partarray = gp.createobject("Array") locale.setlocale(locale.LC_ALL, '')
sepchar = locale.localeconv()['decimal_point'] # loop through the text file.
featid = 0
lineno = 1
for line in inputTxtFile.readlines():
lineno += 1
# create an array from each line in the input text file
values = line.replace("\n", "").replace("\r", "").replace(fileSepChar, sepchar).split(" ") # for a point feature class simply populate a point object and insert it.
if inDataType == "point" and values[0].lower() != "end":
row = rows.newrow()
pnt = createPoint(pnt, values)
row.SetValue(shapefield, pnt)
row.SetValue(idfield, int(values[0]))
rows.insertrow(row) # for a multipoint the text file is organized a bit differently. Groups of points must be inserted at the same time.
elif inDataType == "multipoint":
if len(values) > 2:
pnt = createPoint(pnt, values)
pntarray.add(pnt)
elif (len(values) == 2 and lineno != 2) or values[0].lower() == "end":
row = rows.newrow()
row.SetValue(shapefield, pntarray)
# store the feature id just in case there is an error. helps track down the offending line in the input text file.
if values[0].lower() != "end":
row.SetValue(idfield, featid)
featid = int(values[0])
else:
row.SetValue(idfield, featid)
rows.insertrow(row)
pntarray.removeall()
elif (len(values) == 2 and lineno == 2):
featid = int(values[0]) # for polygons and lines. polygons have a bit of logic for interior rings (donuts).
# lines use the same logic as polygons (except for the interior rings)
elif inDataType == "polygon" or inDataType == "polyline":
#takes care of
#adds the point array to the part array and then part array to the feature
if (len(values) == 2 and float(values[1]) == 0 and lineno != 2) or values[0].lower() == "end":
partarray.add(pntarray)
row = rows.newrow()
row.SetValue(shapefield, partarray)
# store the feature id just in case there is an error. helps track down the offending line in the input text file.
if values[0].lower() != "end":
row.SetValue(idfield, featid)
featid = int(values[0])
else:
row.SetValue(idfield, featid)
rows.insertrow(row)
partarray.removeall()
pntarray.removeall()
#adds parts and/or interior rings to the part array
elif (len(values) == 2 and float(values[1]) > 0) or values[0].lower() == "interiorring":
partarray.add(pntarray)
pntarray.removeall()
#add points to the point array
elif len(values) > 2:
pnt = createPoint(pnt, values)
pntarray.add(pnt)
elif (len(values) == 2 and lineno == 2):
featid = int(values[0]) inputTxtFile.close()
del rows
del row except Exception, ErrorDesc:
# handle the errors here. if the point creation fails, want to keep track of which point failed (easier to fix the
# text file if we do)
if ErrorDesc[0] == msgErrorCreatingPoint:
if inDataType.lower() == "point":
msgErrorCreatingPoint = msgErrorCreatingPoint % (values[0], values[0])
else:
msgErrorCreatingPoint = msgErrorCreatingPoint % (values[0], featid)
gp.AddError(msgErrorCreatingPoint)
elif ErrorDesc[0] != "":
gp.AddError(str(ErrorDesc)) gp.AddError(gp.getmessages(2)) # make sure to close up the fileinput no matter what.
if inputTxtFile: inputTxtFile.close()

create feature from text file的更多相关文章

  1. The 12th tip of DB Query Analyzer, powerful in text file process

    MA Gen feng ( Guangdong Unitoll Services incorporated, Guangzhou 510300) Abstract   It's very powerf ...

  2. [转]How to Import a Text File into SQL Server 2012

    Importing a using the OpenRowSet() Function The OPENROWSET bulk row set provider is accessed by call ...

  3. shell脚本执行时报"bad interpreter: Text file busy"的解决方法

    在执行一个shell脚本时,遇到了“-bash: ./killSession.sh: /bin/bash: bad interpreter: Text file busy”错误提示,如下所示: [or ...

  4. eclipse的使用-------Text File Encoding没有GBK选项的设置

    eclipse的使用-------Text File Encoding没有GBK选项的设置 2013-12-25 09:48:06 标签:java myeclipse使用 有一个项目是使用GBK编码的 ...

  5. Writing Text File From A Tabular Block In Oracle Forms

    The example given below for writing text file or CSV using Text_IO package from a tabular block in O ...

  6. 【转】shell脚本执行时报"bad interpreter: Text file busy"的解决方法

    1)问题现象: 在ubuntu下执行以下脚本( while_count),报错: -bash: ./while_count: /bin/bash: bad interpreter: Text file ...

  7. EXCEPTION:FATAL: UNABLE TO CREATE ‘…GIT/INDEX.LOCK’ FILE EXISTS

    FATAL: UNABLE TO CREATE ‘…GIT/INDEX.LOCK’ FILE EXISTS Hi, Today I will share you my other experience ...

  8. New text file line delimiter

    Window -> Preferences -> General -> Workspace : Text file encoding :Default : 选择此项将设定文件为系统默 ...

  9. ArcCatalog连接ArcSDE连接报:unable to create new database connection file,permission is denied

    参考博文:链接 ArcCatalog连接ArcSDE连接报:unable to create new database connection file,permission is denied 最近经 ...

随机推荐

  1. ActiveX控件

    什么是ActiveX控件:一个进程内服务器,支持多种的COM接口.(可以理解为,一个COM接口是一个纯抽象基类,你实现了它,并且它支持自注册,就是一个ActiveX控件了)可以把ActiveX控件看做 ...

  2. 题目连接:http://acm.zznu.edu.cn/problem.php?id=1329

    题目大意: 定理:把一个至少两位的正整数的个位数字去掉,再从余下的数中减去个位数的5倍.当且仅当差是17的倍数时,原数也是17的倍数 . 例如,34是17的倍数,因为3-20=-17是17的倍数:20 ...

  3. class int

    class int(object): """ int(x=0) -> integer int(x, base=10) -> integer Convert a ...

  4. linux 5 配置xmanager

    0 关闭防火墙或者打开177端口 iptables -A INPUT -p udp --dport 177 -j ACCEPT 1.vi /etc/inittab id:5:initdefault:  ...

  5. POJ 3298 Antimonotonicity (思维)

    题目链接:http://poj.org/problem?id=3298 找一个最长不要求连续的子序列,如a1 > a3 < a6 > a7 ... 举个例子模拟一下差不多明白了,a[ ...

  6. .net 学习资源(转)

      名称:快速入门地址:http://chs.gotdotnet.com/quickstart/描述:本站点是微软.NET技术的快速入门网站,我们不必再安装.NET Framework中的快速入门示例 ...

  7. iis配置好后,解决打开服务器要输入用户名和密码的问题

    [转]IIS网站访问需要输入用户名和密码 xp系统下安装IIS5,并设置好网站路径,但是访问网站时需要输入用户名和密码,这个问题极大可能是因为你网站放置在一个文件系统为NTFS的盘符上,而IIS默认的 ...

  8. Struts2 高危漏洞补丁版本为: Struts 2.3.15.1

    Struts2 昨天爆出高危漏洞,黑客利用这个漏洞可以执行任意命令(包括恶意的jsp代码),轻松绕过您的验证系统,登陆您的网站后台,使您的网站后台密码形同虚设!! 目前Struts2官方已经发布了一个 ...

  9. VM VirtualBox 上安装 CentOs6.4(详细)

    在网上下载:CentOS-6.4-i386-bin-DVD1.iso镜像. 这是我在VBox上安装CentOs6.4的过程: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12 ...

  10. 扩展User增加部门字段

    通过继承User<TEntity>类增加一个字段 /// <summary>用户信息</summary> [Serializable] [ModelCheckMod ...