Arcgis100之后版本坐标值转换成shp图层教程.docx
《Arcgis100之后版本坐标值转换成shp图层教程.docx》由会员分享,可在线阅读,更多相关《Arcgis100之后版本坐标值转换成shp图层教程.docx(11页珍藏版)》请在冰点文库上搜索。
Arcgis100之后版本坐标值转换成shp图层教程
通过坐标数值生成点线面的shp图层
Arcgis10.0之后版本虽然交9.3增加了很多功能,但是却不知何故少了一些常用的功能。
本问主要就比较常用通过坐标数值生成点线面的shp图层做简单介绍。
一、前期准备
1.将附件CreateFeaturesFromTextFile文件复制到任何一个固定的位置。
2.打开Arcgis,新建toolbox
3.按图新建脚本工具
C:
\ProgramFiles\ArcGIS\Desktop10.1\ArcToolbox\Stylesheets\geoprocessing_help.xsl
4.点击下一步,ScriptFile选择刚才那个脚本文件“CreateFeaturesFromTextFile”
5.设置参数
其中,“坐标值规定“中的Default的值为12345678.12345
“输出shp文件”中的Direction改为output
“坐标系统“的type改为optional
6.点击finished
二、制作带有坐标的文本文件。
1.这个文件第一行以d,x,m开头,分别表示点线面。
2.接下几行是坐标值,以“编号(编号从0开始)X坐标Y坐标”
例如:
040545654.256532145697.325
3.另起行以end结束。
保存成txt格式的文件。
三、生产shp图层
1.打开刚才那个制作好的脚本工具CreateFeaturesFromTextFile
2.按照提示操作,坐标系统可以不填
3.点击Ok。
完成
注意事项:
编写的txt文件的坐标必须按照顺序编写,否则图形会出现紊乱(点文件除外)。
将一下文本粘贴到txt中,并保存成.py的文件。
'''----------------------------------------------------------------------------------
ToolName:
CreateFeaturesFromTextFile
SourceName:
CreateFeaturesFromTextFile.py
Version:
ArcGIS9.1
Author:
EnvironmentalSystemsResearchInstituteInc.
RequiredArgumuments:
AnInputTextFilecontainingfeaturecoordinates
AnInputCharacterdesignatingthedecimalseparatorusedinthetextfile.
Anoutputfeatureclass
OptionalArguments:
Aspatialreferencecanbespecified.Thiswillbethe
spatialreferenceoftheoutputfc.
Description:
Readsatextfilewithfeaturecoordinatesandcreatesafeatureclass
fromthecoordinates.
----------------------------------------------------------------------------------'''
importstring,os,sys,locale,arcgisscripting
gp=arcgisscripting.create()
gp.overwriteoutput=1
msgErrorTooFewParams="Notenoughparametersprovided."
msgUnknownDataType="isnotavaliddatatype.Datatypemustbepoint,multipoint,polylineorpolygon."
msgErrorCreatingPoint="Errorcreatingpoint%sonfeature%s"
#setsallthepointproperties
defcreatePoint(point,geometry):
try:
point.id=geometry[0]
point.x=geometry[1]
point.y=geometry[2]
#WhenemptyvaluesarewrittenoutfrompyWriteGeomToTextFile,theycomeas1.#QNAN
#Additionally,theuserneednotsupplythesevalues,soiftheyaren'tinthelistdon'taddthem
iflen(geometry)>3:
ifgeometry[3].lower().find("nan")==-1:
point.z=geometry[3]
iflen(geometry)>4:
ifgeometry[4].lower().find("nan")==-1:
point.m=geometry[4]
returnpoint
except:
raiseException,msgErrorCreatingPoint
try:
#gettheprovidedparameters
inputTxtFile=open(gp.getparameterastext(0))
fileSepChar=gp.getparameterastext
(1)
outputFC=gp.getparameterastext
(2)
#spatialreferenceisoptional
outputSR=gp.getparameterastext(3)
#makesurethetexttypespecifiedinthetextfileisvalid.
inDataT=inputTxtFile.readline().strip().lower()
d={'d':
'point','ml':
'multipoint','x':
'polyline','m':
'polygon'}
inDataType=d[inDataT]
dataTypes=["point","multipoint","polyline","polygon"]
ifinDataType.lower()notindataTypes:
msgUnknownDataType="%s%s"%(inDataType,msgUnknownDataType)
raiseException,msgUnknownDataType
#createthenewfeatureclass
gp.toolbox="management"
gp.CreateFeatureclass(os.path.split(outputFC)[0],os.path.split(outputFC)[1],inDataType,"#","ENABLED","ENABLED",outputSR)
#createanewfieldtoassuretheidofeachfeatureispreserved.
idfield="File_ID"
gp.addfield(outputFC,idfield,"LONG")
#getsomeinformationaboutthenewfeatureclassforlateruse.
outDesc=gp.describe(outputFC)
shapefield=outDesc.ShapeFieldName
#createthecursorandobjectsnecessaryforthegeometrycreation
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']
#loopthroughthetextfile.
featid=0
lineno=1
forlineininputTxtFile.readlines():
lineno+=1
#createanarrayfromeachlineintheinputtextfile
values=line.replace("\n","").replace("\r","").replace(fileSepChar,sepchar).split("")
#forapointfeatureclasssimplypopulateapointobjectandinsertit.
ifinDataType=="point"andvalues[0].lower()!
="end":
row=rows.newrow()
pnt=createPoint(pnt,values)
row.SetValue(shapefield,pnt)
row.SetValue(idfield,int(values[0]))
rows.insertrow(row)
#foramultipointthetextfileisorganizedabitdifferently.Groupsofpointsmustbeinsertedatthesametime.
elifinDataType=="multipoint":
iflen(values)>2:
pnt=createPoint(pnt,values)
pntarray.add(pnt)
elif(len(values)==2andlineno!
=2)orvalues[0].lower()=="end":
row=rows.newrow()
row.SetValue(shapefield,pntarray)
#storethefeatureidjustincasethereisanerror.helpstrackdowntheoffendinglineintheinputtextfile.
ifvalues[0].lower()!
="end":
row.SetValue(idfield,featid)
featid=int(values[0])
else:
row.SetValue(idfield,featid)
rows.insertrow(row)
pntarray.removeall()
elif(len(values)==2andlineno==2):
featid=int(values[0])
#forpolygonsandlines.polygonshaveabitoflogicforinteriorrings(donuts).
#linesusethesamelogicaspolygons(exceptfortheinteriorrings)
elifinDataType=="polygon"orinDataType=="polyline":
#takescareof
#addsthepointarraytothepartarrayandthenpartarraytothefeature
if(len(values)==2andfloat(values[1])==0andlineno!
=2)orvalues[0].lower()=="end":
partarray.add(pntarray)
row=rows.newrow()
row.SetValue(shapefield,partarray)
#storethefeatureidjustincasethereisanerror.helpstrackdowntheoffendinglineintheinputtextfile.
ifvalues[0].lower()!
="end":
row.SetValue(idfield,featid)
featid=int(values[0])
else:
row.SetValue(idfield,featid)
rows.insertrow(row)
partarray.removeall()
pntarray.removeall()
#addspartsand/orinteriorringstothepartarray
elif(len(values)==2andfloat(values[1])>0)orvalues[0].lower()=="interiorring":
partarray.add(pntarray)
pntarray.removeall()
#addpointstothepointarray
eliflen(values)>2:
pnt=createPoint(pnt,values)
pntarray.add(pnt)
elif(len(values)==2andlineno==2):
featid=int(values[0])
inputTxtFile.close()
delrows
delrow
exceptException,ErrorDesc:
#handletheerrorshere.ifthepointcreationfails,wanttokeeptrackofwhichpointfailed(easiertofixthe
#textfileifwedo)
ifErrorDesc[0]==msgErrorCreatingPoint:
ifinDataType.lower()=="point":
msgErrorCreatingPoint=msgErrorCreatingPoint%(values[0],values[0])
else:
msgErrorCreatingPoint=msgErrorCreatingPoint%(values[0],featid)
gp.AddError(msgErrorCreatingPoint)
elifErrorDesc[0]!
="":
gp.AddError(str(ErrorDesc))
gp.AddError(gp.getmessages
(2))
#makesuretocloseupthefileinputnomatterwhat.
ifinputTxtFile:
inputTxtFile.close()