当前位置:网站首页>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种类型的样式符号
边栏推荐
- Redis client tool redis insight recommendation
- Learn when playing No 5 | human high quality examination, right here →
- Design a stack with getmin function
- Introduction to machine learning (I): understanding maximum likelihood estimation in supervised learning
- When deep learning makes data sets, it specifies how many frames to extract an image from the long video to the specified file path
- while(~scanf(“%d“, &n)) 等价于 while(scanf(“%d“,&n)!=EOF)
- oracle 触发器创建
- [unity entry program] make my first little game
- Surpassing transformer, Tsinghua, byte significantly refresh parallel text generation SOTA performance | ICML 2022
- Learn no when playing 8 | the enterprise saves hundreds of thousands in an instant, just because it uses it
猜你喜欢
Cache design in Web services (error allowed, error not allowed)

enq: HW – contention等待引起的故障分析

Nano data, football data, football match scores, sports data API, Qatar world cup

How to reverse a stack with recursive functions and stack operations only
![[unity entry plan] interface Introduction (1) -scene view](/img/88/dee292cb90cd740640018e7260107f.png)
[unity entry plan] interface Introduction (1) -scene view

如何仅用递归函数和栈操作逆序一个栈

转行学什么成为了一大部分人的难题,那么为什么很多人学习软件测试呢?
![[unity introduction program] basic concepts GameObject & components](/img/fc/7e0a6f057394a6fd4409a7bf518ba5.png)
[unity introduction program] basic concepts GameObject & components

Efcore's solution of multi tenant zero script, table and database read-write separation under SaaS system

Introduction and principle explanation of 30 common sensor modules in IOT embedded devices
随机推荐
Introduction to machine learning (I): understanding maximum likelihood estimation in supervised learning
IoT物联网嵌入式设备中30种常见传感器模块简介及原理讲解
New version 8.6 SEO quick release system (can be built at source level)
475-82(230、43、78、79、213、198、1143)
Can Flink's current capabilities support the synchronization of a table from source (MySQL) to sink (MySQL) to separate databases and tables
uiautomator2 常用命令
Learn when playing No 4 | can you take an online exam in programming? Right here →
【Unity入门计划】界面介绍(1)-Scene视图
RK3399开发板I2C4挂载EEPROM实例
The two Nobel Prize winners became the chief scientist of the sky high price Baijiu of "taishanglaojun holding a dream"
【Unity入门计划】基本概念-GameObject&Components
Is the yield of financial products high or low?
Learn no when playing 8 | the enterprise saves hundreds of thousands in an instant, just because it uses it
MathWorks has been in China for 15 years. What are the secrets of continuous innovation?
batchnorm 和layernorm的区别
pom容易忽略的问题
While (~scanf ("%d", & n)) is equivalent to while (scanf ("%d", & n)! =eof)
In depth analysis of yolov7 network architecture
由两个栈组成的队列
Tunnel broadcasting and wireless trunking communication broadcasting system - the case of Tiantaishan tunnel
https://docs.python.org/zh-cn/3.7/library/sqlite3.html