'''----------------------------------------------------------------------------------
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. Linux 修改hostname 文件

    linux 的机器修改hostname: 修改 /etc/hosts 修改 /etc/sysconfig/network 重启机器reboot

  2. iOS KVC/KVO/KVB

    看了那么多博客.描述那么复杂,其实KVC很简单,没描述的那么复杂,所以写一篇简单的易于理解的博文,切入正文: 1.KVC底层是通过runtime对method和value操作  比如说如下的一行KVC ...

  3. vtk读取文件中点坐标[转]

    vtk基础编程(2)-读取数据文件中的坐标点 1. 案例说明 在实际计算中,常常需要大量的数据, 这个时候数据文件就必不可少, 例如 数据文件points.dat, 中存放了三个点的坐标, 0.0 0 ...

  4. js和jquery中的事件委托

    [转+自己的修改] 概念: 什么是事件委托:通俗的讲,事件就是onclick,onmouseover,onmouseout,等就是事件,委托呢,就是让别人来做,这个事件本来是加在某些元素上的,然而你却 ...

  5. swift 与 指针初级使用

    swift 里面对应C 的基础类型前面加C,CInt.CBool和CChar UnsafePointer<CChar> 对应C的 const char *;常量指针不可变 UnsafeMu ...

  6. hdu1498 50 years, 50 colors --- 最小点覆盖

    给一个矩阵,里面有一些不同颜色的气球.每次能够消灭一行或一列中某一种颜色的气球,问你在k次及以内,有哪些颜色的气球是不管怎样也消不完的. 那么思路就是,对每一种颜色的气球求最小点覆盖.>k 则为 ...

  7. Android Studio中导入第三方库

    之前开发Android都是使用的eclipse,近期因为和外国朋友Timothy一起开发一款应用,他是从WP平台刚切换使用Android的,使用的开发环境时Android Studio,为了便于项目的 ...

  8. SQL Server如何截断(Truncate)和收缩(Shrink)事务日志

    当SQL Server截断事务日志时,它仅仅是在虚拟日志文件中做个标记,以便不再使用它,然后准备以重用形式来做备份(假如运载在完整或是批量日志恢复模型).也就是说,在使用简单恢复模型时,事务日志包括如 ...

  9. Cocos2d-x内存管理研究<二>

    http://hi.baidu.com/tzkt623/item/46a26805adf7e938a3332a04   上一篇我们讲了内核是如何将指针加入管理类进行管理.这次我将分析一下内核是如何自动 ...

  10. MySQL 5.7.12新增MySQL Shell命令行功能

      在最新发布的MySQL 5.7.12中有许多令人兴奋的新功能,对于MySQL开发者来说,最令人兴奋的莫不是新增的MySQL Shell了,其下载地址: http://dev.mysql.com/d ...