arcgis python脚本筛选与线共边的面_ArcGIS公共函数Python脚本,Arcgis,常用,功能

Arcgis常用功能 Python脚本

对一些常用功能的python脚本进行总结,方便以后直接调用。

1. 根据属性值删除行

arcpy.MakeFeatureLayer_management(outputpath, "parcels_lyr")#根据输入要素类或图层创建临时图层,如果不将此图层保持到磁盘或地图文档,该图层在会话结束后将不会继续存在。

arcpy.SelectLayerByAttribute_management("parcels_lyr", "NEW_SELECTION", '"field" < 4')#按属性表选择图层

arcpy.DeleteRows_management("parcels_lyr") #删除上一级选定的行

2. 添加字段

try:

arcpy.AddField_management(in_table, "field_name", "field_type")

except:field_name

arcpy.DeleteField_management(in_table, "field_name")

arcpy.AddField_management(in_table, "field_name", "field_type")

官方链接

字段类型

TEXT —任何字符串。

FLOAT — 在 -3.4E38 和 1.2E38 之间的小数。

DOUBLE — 在 -2.2E308 和 1.8E308 之间的小数。

SHORT — 在 -32,768 和 32,767 之间的整数。

LONG — 在 -2,147,483,648 和 2,147,483,647 之间的整数。

DATE —日期和/或时间。

BLOB —长二进制数序列。您需要一个自定义的加载器、查看器或第三方应用程序将这些项加载到 BLOB 字段中或者查看 BLOB 字段的内容。

RASTER —栅格影像。可以存储 ArcGIS 软件支持的所有栅格数据集格式,但强烈建议您仅使用小影像。

GUID —全局唯一标识符。

3 .计算要素面积、长度

try:

arcpy.AddField_management(in_table, "field_name", "field_type")

except:field_name

arcpy.DeleteField_management(in_table, "field_name")

arcpy.AddField_management(in_table, "field_name", "field_type")

express = "!SHAPE.area@squaremeter!" # !shape.length@kilometers!

arcpy.CalculateField_management (in_table, "field_name", express, "PYTHON_9.3") #计算字段

官方链接

面积测量单位关键字

ACRES | ARES | HECTARES | SQUARECENTIMETERS | SQUAREDECIMETERS | SQUAREINCHES | SQUAREFEET | SQUAREKILOMETERS | SQUAREMETERS | SQUAREMILES | SQUAREMILLIMETERS | SQUAREYARDS | SQUAREMAPUNITS

英亩 | 公亩 | 公顷 | 平方厘米 | 平方分米 | 平方英寸 | 平方英尺 | 平方千米 | 平方米 | 平方英里 | 平方毫米 | 平方码 | 平方地图单位

线性测量单位关键字

CENTIMETERS | DECIMALDEGREES | DECIMETERS | FEET | INCHES | KILOMETERS | METERS | MILES | MILLIMETERS | NAUTICALMILES | POINTS | UNKNOWN | YARDS

厘米 | 十进制度 | 分米 | 英尺 | 英寸 | 千米 | 米 | 英里 | 毫米 | 海里 | 磅 | 未知 | 码

4. 将图层文件添加到mxd中

不设置符号系统

tempmxd = r"D:\xxx\xxx\temp.mxd" #空的mxd的路径

mxd = arcpy.mapping.MapDocument(tempmxd)

df = arcpy.mapping.ListDataFrames(mxd)[0]

layer = arcpy.mapping.Layer(inputfiles) #

arcpy.mapping.AddLayer(df, layer) #将图层文件添加到地图文档中

arcpy.RefreshTOC()

arcpy.RefreshActiveView()

mxd.saveACopy(resultMxd)

设置符号系统

tempmxd = r"D:\xxx\xxx\xxx.mxd" #空的mxd的路径

layerFile = r"D:\xxx\xxx\xxx.lyr" #图层文件

mxd = arcpy.mapping.MapDocument(tempmxd)

layer = arcpy.mapping.Layer(inputfiles)

df = arcpy.mapping.ListDataFrames(mxd)[0]

arcpy.mapping.AddLayer(df, layer)

layer1 = arcpy.mapping.ListLayers(mxd, "name", df)[0]

arcpy.ApplySymbologyFromLayer_management(layer1, layerFile)

arcpy.RefreshTOC()

arcpy.RefreshActiveView()

mxd.saveACopy(resultMxd)

5. 遍历读取dbf表

#pip install dbfread

from dbfread import DBF

table = DBF(in_table,load=True)

for record in table:

bj = record["field_name"]

print(bj)

6. txt,csv, xls 转点要素

import arcpy

import sys

import pandas as pd

import os

from arcpy import env

from arcpy.sa import *

IN_DF= "C:\xxx\xxxx.txt"

MIDF="C:\xxx\xxxx.shp"

def xlsx_to_csv_pd(xlsxpath,csvpath):

data_xls = pd.read_excel(xlsxpath, index_col=0)

data_xls.to_csv(csvpath, encoding='utf-8')

if IN_DF.endswith(".txt") or IN_DF.endswith(".csv"):

pass

if IN_DF.endswith(".xls") or IN_DF.endswith(".xlsx"):

xlsx_to_csv_pd(IN_DF,in_df_csv)

IN_DF = in_df_csv

try:

arcpy.Delete_management(MIDF)

createFC = arcpy.CreateFeatureclass_management(env.workspace, MIDF, "POINT", "", "", "")

except:

createFC = arcpy.CreateFeatureclass_management(env.workspace, MIDF, "POINT", "", "", "")

arcpy.AddField_management(env.workspace + "/" + MIDF, "YEAR", "SHORT" )

arcpy.AddField_management(env.workspace + "/" + MIDF, "MONTH", "SHORT" )

arcpy.AddField_management(env.workspace + "/" + MIDF, "DAY", "SHORT" )

arcpy.AddField_management(env.workspace + "/" + MIDF, "DF", "SHORT" )

arcpy.AddField_management(env.workspace + "/" + MIDF, "lon", "FLOAT" )

arcpy.AddField_management(env.workspace + "/" + MIDF, "lat", "FLOAT" )

iflds = ["YEAR","MONTH","DAY","DF", "lon","lat", "SHAPE@XY"]

iCur = arcpy.da.InsertCursor(env.workspace + "/" + MIDF, iflds)

count = 1

for ln in open(IN_DF, 'r').readlines():

lnstrip = ln.strip()

if count > 1:

dataList = ln.split(",")

y=dataList[4]

m=dataList[5]

d=dataList[6]

j=dataList[7]

lat = dataList[2]

lon = dataList[1]

ivals = [y,m,d,j, float(lon), float(lat),(float(lon), float(lat))]

iCur.insertRow(ivals)

count += 1

print('Finish Projection:',MIDF)

7. 汇总统计数据

类似于融合,但是只对表操作,运行速度比融合快

arcpy.Statistics_analysis(out_feature_class, out_feature_rr,[["area", "SUM"]],["CTJCBM"])

8. 字段计算器——随机赋值

import random

def fa():

num = ["好(很湿润)","较好(湿润)","较差(半干旱)","差(干旱)"]

a = random.choice(num)

return a

9. 字段计算器——根据别的字段,赋值

def fun(a):

if a=="01" :

return "一等"

elif a=="02" :

return "二等"

elif a=="03" :

return "三等"

elif a=="04" :

return "四等"

elif a=="05" :

return "五等"

elif a=="06" :

return "六等"

elif a=="07" :

return "七等"

elif a=="08" :

return "八等"

elif a=="09" :

return "九等"

elif a=="10" :

return "十等"

elif a=="11" :

return "十一等"

elif a=="12" :

return "十二等"

elif a=="13" :

return "十三等"

elif a=="14" :

return "十四等"

else :

return "十五等"

10. 返回表的总行数

arcpy.management.GetCount(os.path.join(outputpath,newgdbname+".gdb",'dataset',"topo_error_point"))[0]# == "0"

11. 拓扑检查

arcpy.CreateFileGDB_management(outputpath, newgdbname+".gdb")#创建文件地理数据库

arcpy.CreateFeatureDataset_management( os.path.join(outputpath,newgdbname+".gdb"), 'dataset')#创建要素数据集

arcpy.FeatureClassToGeodatabase_conversion(inputfile, os.path.join(outputpath,newgdbname+".gdb",'dataset'))#要素类至地理数据库

arcpy.CreateTopology_management(os.path.join(outputpath,newgdbname+".gdb",'dataset'), "Topology")#创建拓扑

arcpy.AddFeatureClassToTopology_management(os.path.join(outputpath,newgdbname+".gdb",'dataset',"Topology"),os.path.join(outputpath,newgdbname+".gdb",'dataset',Gdbname), 1, 1) #向拓扑中添加要素类

arcpy.AddRuleToTopology_management(os.path.join(outputpath,newgdbname+".gdb",'dataset',"Topology"), topurule, os.path.join(outputpath,newgdbname+".gdb",'dataset',Gdbname))#添加拓扑规则

arcpy.ValidateTopology_management(os.path.join(outputpath,newgdbname+".gdb",'dataset',"Topology"))#验证拓扑

arcpy.ExportTopologyErrors_management(os.path.join(outputpath,newgdbname+".gdb",'dataset',"Topology"),os.path.join(outputpath,newgdbname+".gdb",'dataset'), "topo_error") #错误导出

12 .不重叠部分导出

arcpy.SymDiff_analysis("climate.shp", "elevlt250.shp", "C:/output/symdiff.shp", "ALL", 0.001)

13. 遍历属性表

cursor = arcpy.da.SearchCursor(in_table, ["field1","field2"])

for row in cursor:

print(row[0],row[1])

del cursor,row

待更新

你可能感兴趣的:(arcgis)