import arcpy
import numpy
import math def AddGeometryAttributes(fc, geomProperties, lUnit, aUnit, cs):
"""-------------------------------------------------------------------------
Tool: Add Geometry Attributes (Data Management Tools)
Source Name: AddGeometryAttributes.py
Version: ArcGIS 10.2.1
Author: Esri, Inc.
Usage: arcpy.AddGeometryAttributes_management(
Input_Features,
Geometry_Properties,
{Length_Unit},
{Area_Unit},
{Coordinate_System})
Required Arguments: Input Features
Geometry Properties
Optional Arguments: Length Unit
Area Unit
Coordinate System
Description: Adds attribute fields to the input features containing
measurements and coordinate properties of the feature
geometries (for example, length or area).
Updated: Not yet.
------------------------------------------------------------------------""" desc = arcpy.Describe(fc)
hasZ, hasM = desc.hasZ, desc.hasM
if cs:
sr = arcpy.SpatialReference()
sr.loadFromString(cs)
try:
srMetersPerUnit = sr.metersPerUnit
except:
srMetersPerUnit = 1
else:
try:
srMetersPerUnit = desc.spatialReference.metersPerUnit
except:
srMetersPerUnit = 1
shapeDict = {"POINT":1,
"MULTIPOINT":1.5,
"POLYLINE":2,
"POLYGON":3,}
shapeDim = shapeDict[str(desc.shapeType).upper()]
del desc fields = CreateOutputFields(fc, geomProperties, hasZ, hasM) arcpy.SetProgressor("STEP", arcpy.GetIDMessage(86174), 0,
int(arcpy.GetCount_management(fc).getOutput(0)), 1) hasNulls = False
# Calculate geometry properties into new fields
with arcpy.da.UpdateCursor(fc,fields + ["SHAPE@"],"",cs) as ucur:
ucurFields = ucur.fields
global ucurFields
for row in ucur:
global row
geom = row[ucurFields.index("SHAPE@")]
if geom:
if shapeDim == 1:
if "POINT_X_Y_Z_M" in geomProperties:
Update("POINT_X", geom.firstPoint.X)
Update("POINT_Y", geom.firstPoint.Y)
if hasZ:
Update("POINT_Z", geom.firstPoint.Z)
if hasM:
Update("POINT_M", geom.firstPoint.M)
if shapeDim>1:
if "PART_COUNT" in geomProperties:
Update("PART_COUNT", geom.partCount)
if "CENTROID" in geomProperties:
Update("CENTROID_X", geom.trueCentroid.X)
Update("CENTROID_Y", geom.trueCentroid.Y)
if hasZ:
Update("CENTROID_Z", geom.trueCentroid.Z)
if hasM:
Update("CENTROID_M", geom.trueCentroid.M)
if "EXTENT" in geomProperties:
Update("EXT_MIN_X", geom.extent.XMin)
Update("EXT_MIN_Y", geom.extent.YMin)
Update("EXT_MAX_X", geom.extent.XMax)
Update("EXT_MAX_Y", geom.extent.YMax)
if shapeDim>=2:
if "POINT_COUNT" in geomProperties:
Update("PNT_COUNT", geom.pointCount)
if "LINE_START_MID_END" in geomProperties:
Update("START_X", geom.firstPoint.X)
Update("START_Y", geom.firstPoint.Y)
if shapeDim ==2:
midPoint = geom.positionAlongLine(0.5,
True).firstPoint
else:
line = arcpy.Polyline(geom.getPart(0), "#",
hasZ, hasM)
if line.length > 0:
midPoint = line.positionAlongLine(0.5,
True).firstPoint
else:
hasNulls = True
del line
Update("MID_X", midPoint.X)
Update("MID_Y", midPoint.Y)
Update("END_X", geom.lastPoint.X)
Update("END_Y", geom.lastPoint.Y)
if hasZ:
Update("START_Z", geom.firstPoint.Z)
Update("MID_Z", midPoint.Z)
Update("END_Z", geom.lastPoint.Z)
if hasM:
Update("START_M", geom.firstPoint.M)
Update("MID_M", midPoint.M)
Update("END_M", geom.lastPoint.M)
del midPoint
if "CENTROID_INSIDE" in geomProperties:
Update("INSIDE_X", geom.centroid.X)
Update("INSIDE_Y", geom.centroid.Y)
if hasZ:
Update("INSIDE_Z", geom.centroid.Z)
if hasM:
Update("INSIDE_M", geom.centroid.M)
if shapeDim==2:
if "LINE_BEARING" in geomProperties:
lat1 = geom.firstPoint.Y
lon1 = geom.firstPoint.X
lat2 = geom.lastPoint.Y
lon2 = geom.lastPoint.X
Update("BEARING", (90 - math.degrees(math.atan2(lat2-lat1, lon2-lon1))) % 360)
del lat1, lon1, lat2, lon2
if "LENGTH" in geomProperties:
Update("LENGTH", ConvertFromMeters("LINEAR",
geom.length,
lUnit,
srMetersPerUnit))
if "LENGTH_3D" in geomProperties:
Update("LENGTH_3D", ConvertFromMeters("LINEAR",
geom.length3D,
lUnit,
srMetersPerUnit))
if "LENGTH_GEODESIC" in geomProperties:
Update("LENGTH_GEO", ConvertFromMeters("LINEAR",
geom.getLength("PRESERVE_SHAPE"),
lUnit,
srMetersPerUnit))
if shapeDim==3:
if "PERIMETER_LENGTH" in geomProperties:
Update("PERIMETER", ConvertFromMeters("LINEAR",
geom.length,
lUnit,
srMetersPerUnit))
if "AREA" in geomProperties:
Update("POLY_AREA", ConvertFromMeters("AREA",
geom.area,
aUnit,
srMetersPerUnit))
if "AREA_GEODESIC" in geomProperties:
Update("AREA_GEO", ConvertFromMeters("AREA",
geom.getArea("PRESERVE_SHAPE"),
aUnit,
srMetersPerUnit))
if "PERIMETER_LENGTH_GEODESIC" in geomProperties:
Update("PERIM_GEO", ConvertFromMeters("LINEAR",
geom.getLength("PRESERVE_SHAPE"),
lUnit,
srMetersPerUnit))
ucur.updateRow(row)
else:
hasNulls = True
arcpy.SetProgressorPosition()
if hasNulls:
arcpy.AddIDMessage("WARNING", 957) def CreateOutputFields(fc, geomProperties, hasZ, hasM):
propDict = {"POINT_X_Y_Z_M": ["POINT_X",
"POINT_Y",
"POINT_Z",
"POINT_M"],
"PART_COUNT": ["PART_COUNT"],
"CENTROID": ["CENTROID_X",
"CENTROID_Y",
"CENTROID_Z",
"CENTROID_M"],
"EXTENT": ["EXT_MIN_X",
"EXT_MIN_Y",
"EXT_MAX_X",
"EXT_MAX_Y"],
"POINT_COUNT": ["PNT_COUNT"],
"LINE_START_MID_END": ["START_X",
"START_Y",
"START_Z",
"START_M",
"MID_X",
"MID_Y",
"MID_Z",
"MID_M",
"END_X",
"END_Y",
"END_Z",
"END_M"],
"LINE_BEARING": ["BEARING"],
"CENTROID_INSIDE": ["INSIDE_X",
"INSIDE_Y",
"INSIDE_Z",
"INSIDE_M"],
"LENGTH": ["LENGTH"],
"PERIMETER_LENGTH": ["PERIMETER"],
"AREA": ["POLY_AREA"],
"LENGTH_GEODESIC": ["LENGTH_GEO"],
"AREA_GEODESIC": ["AREA_GEO"],
"LENGTH_3D": ["LENGTH_3D"],
"PERIMETER_LENGTH_GEODESIC":["PERIM_GEO"],
}
if not hasZ:
propDict["POINT_X_Y_Z_M"].remove("POINT_Z")
propDict["CENTROID"].remove("CENTROID_Z")
propDict["CENTROID_INSIDE"].remove("INSIDE_Z")
propDict["LINE_START_MID_END"].remove("START_Z")
propDict["LINE_START_MID_END"].remove("MID_Z")
propDict["LINE_START_MID_END"].remove("END_Z")
if not hasM:
propDict["POINT_X_Y_Z_M"].remove("POINT_M")
propDict["CENTROID"].remove("CENTROID_M")
propDict["CENTROID_INSIDE"].remove("INSIDE_M")
propDict["LINE_START_MID_END"].remove("START_M")
propDict["LINE_START_MID_END"].remove("MID_M")
propDict["LINE_START_MID_END"].remove("END_M") addList = []
geomPropertiesList = []
currentFields = [field.name for field in arcpy.ListFields(fc)]
for prop in geomProperties:
for field in propDict[prop.upper()]:
geomPropertiesList.append(field)
if field not in currentFields:
addList.append(field)
else:
arcpy.AddIDMessage("WARNING", 1097, field) if addList:
narray = numpy.array([], numpy.dtype([("_ID", numpy.int)] +
[(n, numpy.float) for n in addList]))
arcpy.da.ExtendTable(fc, "OID@", narray, "_ID") return geomPropertiesList def ConvertFromMeters(type, value, unit, metersPerUnit):
if not unit:
return value
else:
distanceUnitInfo = {"METERS": 1.0,
"FEET_US": 0.304800609601219,
"NAUTICAL_MILES": 1852.0,
"MILES_US": 1609.34721869444,
"KILOMETERS": 1000.0,
"YARDS": 0.914401828803658,} areaUnitInfo = {"ACRES": 4046.86,
"HECTARES": 10000.0,
"SQUARE_METERS": 1.0,
"SQUARE_FEET_US": 0.09290341161327473,
"SQUARE_NAUTICAL_MILES": 3429904.0,
"SQUARE_MILES_US": 2589998.4703195295,
"SQUARE_KILOMETERS": 1000000.0,
"SQUARE_YARDS": 0.8361307045194741,}
if type == "LINEAR":
return (value*metersPerUnit)/distanceUnitInfo[unit]
if type == "AREA":
return (value*math.pow(metersPerUnit,2))/areaUnitInfo[unit] def Update(field, value):
if value:
if not math.isnan(value):
row[ucurFields.index(field)] = value #run the script
if __name__ == '__main__':
# Get Parameters
fc = arcpy.GetParameterAsText(0)
if arcpy.GetParameterAsText(1).find(";") > -1:
geomProperties = arcpy.GetParameterAsText(1).upper().split(";")
else:
geomProperties = [arcpy.GetParameterAsText(1).upper()]
lUnit = arcpy.GetParameterAsText(2)
aUnit = arcpy.GetParameterAsText(3)
cs = arcpy.GetParameterAsText(4)
if not cs:
cs = arcpy.env.outputCoordinateSystem # Run the main script
AddGeometryAttributes(fc, geomProperties, lUnit, aUnit, cs)

arcgis python添加几何属性的更多相关文章

  1. python 添加类属性

    类属性必须赋值. 创建类属性 类是模板,而实例则是根据类创建的对象. 绑定在一个实例上的属性不会影响其他实例,但是,类本身也是一个对象,如果在类上绑定一个属性,则所有实例都可以访问类的属性,并且,所有 ...

  2. ArcGIS + Python 批量裁剪、添加X/Y坐标脚本

    前言 前一段时间,同事拿来的数据范围太大,用不了那么多(只需要一个乡镇的,结果拿来区县的),太多了加载也是问题.所以就让我给处理下. 由于文件较多,手动裁剪的话,我一个一个用ArcGIS工具箱中的工具 ...

  3. arcgis python arcpy add data script添加数据脚本

    arcgis python arcpy add data script添加数据脚本mxd = arcpy.mapping.MapDocument("CURRENT")... df ...

  4. python装饰器、继承、元类、mixin,四种給类动态添加类属性和方法的方式(一)

    介绍装饰器.继承.元类.mixin,四种給类动态添加类属性和方法的方式 有时候需要給类添加额外的东西,有些东西很频繁,每个类都需要,如果不想反复的复制粘贴到每个类,可以动态添加. # coding=u ...

  5. Python小白学习之如何添加类属性和类方法,修改类私有属性

    如何添加类属性和类方法,修改类私有属性 2018-10-26  11:42:24 类属性.定义类方法.类实例化.属性初始化.self参数.类的私有变量的个人学习笔记 直接上实例: class play ...

  6. Arcgis Engine 添加一个Symbol符号样式步骤

    public static void DrawPictureMarkerSymbol(IGlobe globe, String layerName) { //添加一个图层 ESRI.ArcGIS.Ca ...

  7. ArcGIS Python人门到精通目录基于ArcGIS10.2,100以上案例15章42个视频806分钟,51GIS网站上线

    ArcGIS Python人门到精通目录 闫老师 QQ:276529800 微信13108507190 1.  ArcGIS Python基础 1.1  ArcGIS为什么学习Python 1.2 A ...

  8. python限定类属性的类属性:__slots__

    __slots__ 由于Python是动态语言,任何实例在运行期都可以动态地添加属性. 如果要限制添加的属性,例如,Student类只允许添加 name.gender和score 这3个属性,就可以利 ...

  9. Python中的属性管理

    Python管 理属性的方法一般有三种:操作符重载(即,__getattr__.__setattr__.__delattr__和 __getattribute__,有点类似于C++中的重载操作符).p ...

随机推荐

  1. vue组件中的轮播实现

    一.直接上代码 <template> <el-row class="Slide"> <el-row class="title"&g ...

  2. 面试题:输入两个整数 n 和 m,从数列1,2,3…….n 中 随意取几个数, 使其和等于 m

    问题: 2010年中兴面试题 编程求解: 输入两个整数 n 和 m,从数列1,2,3…….n 中 随意取几个数, 使其和等于 m ,要求将其中所有的可能组合列出来. 思路: 类似这种组合问题一般都是使 ...

  3. MIT6.006Lec01:Python实现

    MIT6.006是Algo Intro这门课,据说语言使用python Lec01是讲peak finding,也就是峰值点 具体为: 一维情况下一个数组中a[i]>a[i-1]且a[i]> ...

  4. ef查询mysql数据库数据支持DbFunctions函数

    1.缘由 快下班的时候,一同事说在写linq查询语句时where条件中写两时间相减大于某具体天数报错:后来仔细一问,经抽象简化,可以总结为下面的公式: a.当前时间 减去 某表时间字段 大于 某具体天 ...

  5. swftools中的pdf2swf转换Error overflow ID 65535 解决办法

    近几日因为项目需要在线转换pdf到swf实现电子期刊阅读,用到了这个工具,版本是:swftools-0.9.2.tar.gz 当然也遇到了很头疼的问题,那就是在转换pdf中色彩图形比较复杂的页时会抛出 ...

  6. ASP.NET Web API 2:创建API帮助页面

         当你新建了一个web API服务之后,再建一个API帮助页面是很有好处的,这样其他开发人员就会很清楚地知道如何调用你的API接口.你可以选择自己手工建立,但是如果能自动生成岂不是更好.为了简 ...

  7. Rookey.Frame之DAL工厂

    昨天给大家介绍了表单验证功能,今天给大家介绍下Rookey.Frame框架的数据层工厂,由于Rookey.Frame框架ORM是基于servicestack.ormlite,很多朋友反映这个网上中文资 ...

  8. ref:如何将自定义异常的信息显示在jsp页面上

    ref:https://blog.csdn.net/tao_ssh/article/details/53486449 在项目中,经常会抛出异常,输出比较友好的信息来提示用户,并指导用户行为.大体思路: ...

  9. 关于mysql中storage_engine中 MYISAM 和 INNODB 的选择

    简单点说 读操作多用myisam 写操作多用innodb 不过现在大家好像基本都用innodb,本人小白一个就直接用InnoDB. MySQL自20多年前成立以来一直支持可插拔存储引擎,但在一段相当长 ...

  10. CAT 3.0 开源发布,支持多语言客户端及多项性能提升

    项目背景 CAT(Central Application Tracking),是美团点评基于 Java 开发的一套开源的分布式实时监控系统.美团点评基础架构部希望在基础存储.高性能通信.大规模在线访问 ...