当前位置:网站首页>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种类型的样式符号
边栏推荐
- P1048 [NOIP2005 普及组 T3] 采药
- IoT物联网嵌入式设备中30种常见传感器模块简介及原理讲解
- App power consumption test
- 整数a按位取反(~)后的值为-(a+1)
- 【Unity入门计划】基本概念-2D刚体Rigidbody 2D
- Didi eta (estimate the travel time)
- People who lose weight should cry: it's no good not eating food, because your brain will be inflamed
- 用一个栈实现另一个栈的排序
- Acnet: asymmetric convolution for image hypersegmentation (with implementation code)
- 2022-07-19 Daily: too many icml2022 papers to read? "One sentence comments on 1234 paper highlights" helps you quickly lock
猜你喜欢

RK3399开发板I2C4挂载EEPROM实例
![[unity introduction program] basic concepts GameObject & components](/img/fc/7e0a6f057394a6fd4409a7bf518ba5.png)
[unity introduction program] basic concepts GameObject & components

The 17th revitalization cup computer programmer (cloud computing platform operation and development) finals

VS2019 C# MFC安装

Supplementary notes on Relevant Issues of complete model group

Today in history: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
![[unity entry program] make my first little game](/img/e7/5dcb113c7fabd73ed632fb29619369.png)
[unity entry program] make my first little game
![[paper notes] effective CNN architecture design guided by visualization](/img/aa/aeeac3f970eac7f110987c523602c8.png)
[paper notes] effective CNN architecture design guided by visualization

How does MTK change the boot logo?

oracle 触发器创建
随机推荐
[unity introduction program] basic concept - preform prefab
Introduction and principle explanation of 30 common sensor modules in IOT embedded devices
Learn when playing No 4 | can you take an online exam in programming? Right here →
Introduction and installation of mongodb
Check the computer restart times and reasons
Enq: HW - fault analysis caused by content waiting
P1086 [noip2004 popularization group question 2] peanut picking
【Unity入门计划】基本概念-2D刚体Rigidbody 2D
Native form submission data
Competition path design of beacon group
Is the yield of financial products high or low?
P1046 [NOIP2005 普及组 T1] 陶陶摘苹果
[unity introduction program] basic concepts - 2D collider collider 2D
牛客动态规划训练
Use of toolbar
Cache design in Web services (error allowed, error not allowed)
滴滴 - eta(Estimate the Travel Time)
My creation anniversary
【Unity入门计划】界面介绍(1)-Scene视图
If Debian infringes the rust trademark, will it be exempted by compromising and renaming?
https://docs.python.org/zh-cn/3.7/library/sqlite3.html