Select to view content in your preferred language

Bulk Import Images Into a Style?

907
2
02-21-2022 05:04 PM
Labels (1)
jdruding
Occasional Contributor

I feel like I must be missing something easy here.  I have been on a hunt for the National Park Service symbols or something similar for use (and more importantly, reuse).  Posted looking for it here.   I never got a response so I thought, ok, I'll just go get the SVGs from the the NPS' source, I'll just make my own SLYX file and upload them all.  Now I'll have a style that I can reuse in other projects.  It appears that that there is is no way to bulk upload images to a Style?  It actually appears I can't even add them one at a time, it looks I have to first use them in a layer and then add them to the style?   I'm sure I must be missing something......  

0 Kudos
2 Replies
DiegoMeira
Occasional Contributor

Is there any solution for bulk import PNGs into the ArcGIS Pro styles (.stylx)?

I have over 300 icons and I’m trying to avoid adding them one by one and manually setting each to a Picture Marker.

I’m using ArcGIS Pro 3.6.0.

DiegoMeira
Occasional Contributor

I found this other community post that was very helpful. With the code provided I managed to use ArcPy to load the PNG files into a .stylx file. 

How to programatically add symbols to a .stylx (St... - Esri Community

Here is my code:

import os
import base64
import sqlite3
import json
import re
from pathlib import Path

# Paths
symbol_directory = r"<png_symbols_folder_full_path>"
stylx_file ="template_style.stylx"
stylx_path = Path(__file__).with_name(stylx_file).resolve()

# Create stylx file if it doesn't exist
if not os.path.exists(stylx_path):
    open(stylx_path, "w").close()

print("Connecting Style DB...")
conn = sqlite3.connect(stylx_path, timeout=60)
cursor = conn.cursor()
print("Connected.")

# Ensure ITEMS table exists
cursor.execute("""
CREATE TABLE IF NOT EXISTS ITEMS (
    ID INTEGER PRIMARY KEY AUTOINCREMENT,
    CLASS INTEGER,
    CATEGORY TEXT,
    NAME TEXT,
    TAGS TEXT,
    CONTENT TEXT,
    KEY TEXT
)
""")

print("Table ready.")
print("Inserting symbols...")

for file_name in os.listdir(symbol_directory):
    if file_name.lower().endswith(".png"):
        symbol_name = os.path.splitext(file_name)[0]
        image_path = os.path.join(symbol_directory, file_name)

        # Read and encode PNG
        with open(image_path, "rb") as img_file:
            encoded_image = base64.b64encode(img_file.read()).decode("utf-8")

        # Proper CIM Picture Marker symbol
        new_symbol_json = {
            "type": "CIMPointSymbol",
            "symbolLayers": [
                {
                    "type": "CIMPictureMarker",
                    "enable": True,
                    "size": 30,
                    "url": f"data&colon;image/png;base64,{encoded_image}",
                    "anchorPoint": {"x": 0, "y": 0},
                    "anchorPointUnits": "Relative",
                    "scaleX": 1,
                    "tintColor": {"values": [255, 255, 255, 255]},
                    "colorLocked": True,
                    "textureFilter": "Best"
                }
            ]
        }

        new_row = (
            3,  # CLASS = Point Symbol
            "",
            symbol_name,
            "",
            json.dumps(new_symbol_json),
            symbol_name.replace(".", "_"),
        )

        cursor.execute(
            "INSERT INTO ITEMS(CLASS, CATEGORY, NAME, TAGS, CONTENT, KEY) VALUES(?,?,?,?,?,?)",
            new_row,
        )

        print(f"Inserted: {symbol_name}")

conn.commit()
conn.close()
print("All symbols added to the .stylx file.")

 

 

 

0 Kudos