字段计算器通过python脚本完成要素节点坐标信息提取到属性字段中
在一些业务场景中需要获取空间要素的节点坐标信息存储在属性字段中,如果你使用的是ArcGIS10.3这个版本那么你可以直接使用 !SHAPE.JSON!:表示几何的 esri JSON 字符串;!SHAPE.WKB!:OGC 几何的二进制表达 (WKB);!SHAPE.WKT!:OGC 几何的文本表示,例如这样.
在arcpy Cursor方式中也可以使用类似 :forrowinarcpy.da.SearchCursor(infc,[“SHAPE@WKT”]):SHAPE@JSON、SHAPE@WKB、SHAPE@WKT的方式获取,在这里需要注意的是新建的用于存储节点信息的字符串字段长度问题,如果长度不够可能被截取一部分。对于File
geodatabase Text field width: 2,147,483,647;shapefile Text field width: 254 characte;personalGDB:Text field width: 255 characte ;ArcSDE:Up to DBMS limits。有人可能对File geodatabase的一些其他限制也感兴趣在此一并贴出:
File geodatabase size and name limits are as follows:
File geodatabase size: No limit
Table or feature class size: 1 TB (default), 4 GB or 256 TB with keyword
Number of feature classes and tables: 2,147,483,647
Number of fields in a feature class or table: 65,534
Number of rows in a feature class or table: 2,147,483,647
Geodatabase name length: Number of characters the operating system allows in a folder name
Feature class or table name length: 160 characters
Field name length: 64 characters
Text field width: 2,147,483,647
那么问题到这里实际上已经得到了解答,但是你可能有这样的需求,你的数据是经纬度坐标,你需要度分秒(DDMMSS)”形式的节点坐标信息,一种办法是在上面已经获取到的格式化字符串中提取坐标信息,并对这些信息进行换算即可,但是如果你的需求不仅限与此,需要对每个节点的X.Y坐标完成更多的操作,那么就需要遍历这些节点逐个操作,这就需要自己写脚本来完成,下面的代码完成了这部分工作。可以在需要修改的部分进行修改即可,需要注意的是该段代码做了十进制度转度分秒的工作,如果你的数据是平面坐标注释掉相应的代码即可。经过测试可以支持一下要素类型。
- def GetFeatureVerticesStr(feat):
- if feat.type==“point”:
- pnt=feat.getPart()
- px=‘%f’ %pnt.X
- py=‘%f’ %pnt.Y
- #TranCoorform degree minute second
- px,py=TranCoorform(px,py)
- return “[“+px+“,”+py+“]”
- if feat.type==“multipoint”:
- multstr=”
- part=feat.getPart()
- pnt=part.next()
- while pnt:
- px=‘%f’ %pnt.X
- py=‘%f’ %pnt.Y
- #TranCoorform degree minute second
- px,py=TranCoorform(px,py)
- multstr=multstr+“[“+px+“,”+py+“],”
- pnt=part.next()
- return multstr[:-1]
- elif feat.type in[“polygon”,“polyline”]:
- partnum = 0
- partcount = feat.partCount
- pntcount = 0
- ringcount=0
- str=”
- while partnum < partcount:
- part = feat.getPart(partnum)
- partstr=getpartstr(part)
- partnum += 1
- if partnum!=1:
- partstr=“,”+partstr
- str=str+partstr
- return str
- def getpartstr(part):
- strpart=”
- flag=False
- pnt = part.next()
- strpart=strpart+“[“
- while pnt:
- px=‘%f’ %pnt.x
- py=‘%f’ %pnt.y
- #TranCoorform degree minute second
- px,py=TranCoorform(px,py)
- strpart=strpart+px+“,”+py +“;”
- pnt = part.next()
- # interior ring
- if not pnt:
- pnt = part.next()
- if pnt:
- if flag:
- strpart=strpart[:-1]
- strpart=strpart+“};”
- flag=False
- strpart=strpart+“{“
- flag=True
- else:
- if flag:
- strpart=strpart+“}”
- flag=False
- if strpart[-1]!=“}”:
- strpart=strpart[:-1]
- else:
- strlen=len(strpart)-2
- strpart = strpart[:strlen] + strpart[strlen+1:]
- strpart=strpart+“]”
- return strpart
- def TranCoorform(x,y):
- dfmx=str(x).split(‘.’)
- dx=dfmx[0]
- ftempx=float(“0.”+dfmx[1])*60
- fx=str(int(ftempx))
- mx=str(float(“0.”+str(ftempx).split(‘.’)[1])*60)
- px=dx+“°”+fx+“′”+mx+“″”
- dfmy=str(y).split(‘.’)
- dy=dfmy[0]
- ftempy=float(“0.”+dfmy[1])*60
- fy=str(int(ftempy))
- my=str(float(“0.”+str(ftempy).split(‘.’)[1])*60)
- py=dy+“°”+fy+“′”+my+“″”
- return px,py
def GetFeatureVerticesStr(feat):
if feat.type=="point":
pnt=feat.getPart()
px='%f' %pnt.X
py='%f' %pnt.Y
#TranCoorform degree minute second
px,py=TranCoorform(px,py)
return "["+px+","+py+"]"
if feat.type=="multipoint":
multstr=''
part=feat.getPart()
pnt=part.next()
while pnt:
px='%f' %pnt.X
py='%f' %pnt.Y
#TranCoorform degree minute second
px,py=TranCoorform(px,py)
multstr=multstr+"["+px+","+py+"],"
pnt=part.next()
return multstr[:-1]
elif feat.type in["polygon","polyline"]:
partnum = 0
partcount = feat.partCount
pntcount = 0
ringcount=0
str=''
while partnum < partcount:
part = feat.getPart(partnum)
partstr=getpartstr(part)
partnum += 1
if partnum!=1:
partstr=","+partstr
str=str+partstr
return str
def getpartstr(part):
strpart=''
flag=False
pnt = part.next()
strpart=strpart+"["
while pnt:
px='%f' %pnt.x
py='%f' %pnt.y
#TranCoorform degree minute second
px,py=TranCoorform(px,py)
strpart=strpart+px+","+py +";"
pnt = part.next()
# interior ring
if not pnt:
pnt = part.next()
if pnt:
if flag:
strpart=strpart[:-1]
strpart=strpart+"};"
flag=False
strpart=strpart+"{"
flag=True
else:
if flag:
strpart=strpart+"}"
flag=False
if strpart[-1]!="}":
strpart=strpart[:-1]
else:
strlen=len(strpart)-2
strpart = strpart[:strlen] + strpart[strlen+1:]
strpart=strpart+"]"
return strpart
def TranCoorform(x,y):
dfmx=str(x).split('.')
dx=dfmx[0]
ftempx=float("0."+dfmx[1])*60
fx=str(int(ftempx))
mx=str(float("0."+str(ftempx).split('.')[1])*60)
px=dx+"°"+fx+"′"+mx+"″"
dfmy=str(y).split('.')
dy=dfmy[0]
ftempy=float("0."+dfmy[1])*60
fy=str(int(ftempy))
my=str(float("0."+str(ftempy).split('.')[1])*60)
py=dy+"°"+fy+"′"+my+"″"
return px,py
转载自:https://blog.csdn.net/kone0611/article/details/62076771