当前位置:网站首页>ArcGIS Pro脚本工具(10)——从图层生成.stylx样式符号
ArcGIS Pro脚本工具(10)——从图层生成.stylx样式符号
2022-07-25 08:01:00 【学学GIS】
做这个工具的原因是,想做一个湘源控规用地颜色的.stylx文件。
但是湘源所有用地类型太多了,如下图所示,足足110类。这么多颜色的面符号,一个个做到.stylx文件里不得累死?

好在Pro对CAD文件的支持比较好,我们直接加载CAD的Polygon到Pro中,Polygon已经按图层分好了,并且颜色也与CAD中一致(仔细对一下CAD与Pro中相同图层的RGB颜色,还是能发现RGB值有一些出入,但是颜色大致是对的,不清楚有没有办法能保持完全一致。上面的示意图中Pro界面内用地颜色较浅,是因为Pro自动为所有CAD面图层加了50%的透明度,这也是之后的步骤中需要处理的)。
一、Pro中取消CAD图层的透明度
要在Pro中手动取消这110个CAD图层的透明度也是一件美事。。。麻烦事,好在之前已经捣鼓过怎么借助代码修改了。

二、Pro中新建.stylx样式文件
可以新建.stylx样式文件,也可以使用已存在的.stylx样式文件(如果使用已有样式文件,请先判断CAD图层符号加入后是否会出现名称、键值的冲突)。
为了将CAD面图层符号写入.stylx样式文件,我们需要对.stylx有基本的认识。.stylx实际上是一个SQLite数据库,我们可以使用Navicat来连接、查看。

数据库的ITEMS表纪录了每个符号的ID、CLASS(点、线、面等)、CATEGORY(分级)、NAME、TAGS(标签)、CONTENT(json字符串)、KEY(键值)。除ID外,符号的其他属性都可在Pro中查看和修改。
学习一下Python中读写SQLite数据库的语法,我们就能向.stylx样式文件写入符号了。
三、编写Python、制作脚本工具
脚本的主要处理流程为:先读取每个CAD图层的名称、符号,再将这些信息写入到.stylx样式文件(SQLite数据库)中。CAD图层名称使用ArcPy便能获取,而获取图层符号则需要使用Python CIM,因为这样能直接获取到可写入CONTENT属性的json字符串。
Python脚本
import arcpy
import sqlite3
aprx = arcpy.mp.ArcGISProject(r"CURRENT")
m = aprx.activeMap
groupLayer = arcpy.GetParameterAsText(0)
lyrs = m.listLayers()
stylx_path = arcpy.GetParameterAsText(1)
stylxDB = sqlite3.connect(stylx_path)
cursor = stylxDB.cursor()
category_switch = {4: "一级类",
5: "二级类",
6: "三级类", }
class_switch = {"Point": 3,
"Polyline": 4,
"Polygon": 5}
for lyr in lyrs:
longName = lyr.longName
if longName.startswith(groupLayer) and lyr.isGroupLayer == False:
type = arcpy.Describe(lyr).featureClass.shapeType
classValue = class_switch.get(type)
sym = lyr.symbology
if sym.renderer.type == 'SimpleRenderer':
try:
cim = lyr.getDefinition("V2").renderer.symbol.symbol
content = arcpy.cim.GetJSONForCIMObject(cim, "V2")
name = lyr.name
key = lyr.name
category = category_switch.get(len(name))
new_row = (classValue, category, name, content, key)
cursor.execute(
'INSERT INTO ITEMS(CLASS, CATEGORY, NAME, CONTENT, KEY) VALUES(?,?,?,?)', new_row)
stylxDB.commit()
arcpy.AddMessage("√ "+lyr.name)
except:
arcpy.AddMessage("× "+lyr.name)
stylxDB.close()
参数设置

效果演示

工具说明
- 只适合于ArcGIS Pro
- 运行工具前,请切换到地图视图
- 该工具可批量生成点、线、面3种类型的样式符号
边栏推荐
- If there is complex select nesting in the SQL of the flick CDC, when it encounters the binlog of delete, it will
- P1048 [noip2005 popularization group t3] drug collection
- 文献学习(part101)--CONVEX BICLUSTERING
- CSDN custom T-shirts are waiting for you to get, and the benefits of new programmer are coming!
- Design a stack with getmin function
- In depth analysis of yolov7 network architecture
- Learn when playing No 5 | human high quality examination, right here →
- [unity introduction plan] interface Introduction (2) -games view & hierarchy & Project & Inspector
- Uiautomator2 common commands
- Weblux default IO threads
猜你喜欢
![[unity introduction program] basic concepts - 2D collider collider 2D](/img/cf/a546238a5eaf4707006ecf1b7f19c6.png)
[unity introduction program] basic concepts - 2D collider collider 2D

People who lose weight should cry: it's no good not eating food, because your brain will be inflamed

Science: listening to music can really relieve pain. Chinese scientists reveal the neural mechanism behind it
![[unity entry program] basic concept trigger](/img/16/cd0f8ae579627fc095935195136729.png)
[unity entry program] basic concept trigger

Pricing is arbitrary, products are difficult to distinguish between true and false, and platforms are running away. Will the Tibetan market continue to be popular?

转行学什么成为了一大部分人的难题,那么为什么很多人学习软件测试呢?

交叉熵计算公式

Eval and assert one sentence Trojan horse analysis

If Debian infringes the rust trademark, will it be exempted by compromising and renaming?

【Unity入门计划】界面介绍(2)-Games视图&Hierarchy&Project&Inspector
随机推荐
Tunnel broadcasting and wireless trunking communication broadcasting system - the case of Tiantaishan tunnel
How does MTK change the boot logo?
P1086 [NOIP2004 普及组第二题] 花生采摘
Google Earth engine - Landsat 1985-2020 ecological remote sensing index resi calculation
Use of toolbar
查看电脑重启次数、原因
File details
Learn no when playing 8 | the enterprise saves hundreds of thousands in an instant, just because it uses it
【Unity入门计划】基本概念-2D刚体Rigidbody 2D
batchnorm 和layernorm的区别
CSDN custom T-shirts are waiting for you to get, and the benefits of new programmer are coming!
Cache design in Web services (error allowed, error not allowed)
IoT物联网嵌入式设备中30种常见传感器模块简介及原理讲解
【ES6】函数的参数、Symbol数据类型、迭代器与生成器
大佬秋招面经
文件详细操作
If Debian infringes the rust trademark, will it be exempted by compromising and renaming?
Network file storage system (III) practice of fastdfs distributed file system
Sqlserver has opened the CDC table. Why can we only pull the full amount of data and can't recognize the incremental data
[unity entry program] basic concept trigger
https://docs.python.org/zh-cn/3.7/library/sqlite3.html