Select to view content in your preferred language

MMPK Label Definitions

1619
6
06-18-2018 10:05 AM
MarkCederholm
Frequent Contributor

100.2:  I notice that label definitions from layers in an MMPK do not contain text symbol info in the JSON.  Is this a known issue?  At some point in the future, I would like to be able to change the color of labels based on changes to the map background.

6 Replies
dotMorten_esri
Esri Notable Contributor

Text symbols should be converted to vector symbols, because there's no guarantee the required font will be available at the specific device.

Are you not seeing them render correctly?

0 Kudos
MarkCederholm
Frequent Contributor

Sorry if I'm being cryptic. I want to use code to change the label colors for a feature layer.  However, LabelDefinition.ToJSON() returns incomplete results in that the "symbol" entry is an empty object {}.  [If you look in the MMPK's geodatabase, you can see the full info there.]

Right now, if I want to change the label color of a feature layer by updating the label definitions, I have to supply the missing text symbol info, as in the following snippet, instead of merely changing the color of the symbol definition.

If there's a better approach that I'm missing, I'd like to learn of it.

Dictionary<string, object> font = new Dictionary<string, object>();
font["family"] = "Arial";
font["size"] = 8;
font["style"] = "normal";
font["weight"] = "normal";
font["decoration"] = "none";
Dictionary<string, object> sym = new Dictionary<string, object>();
sym["type"] = "esriTS";
byte[] RGBA;
if (bUseRed)
     RGBA = new byte[] { 255, 0, 0, 255 };
else
     RGBA = new byte[] { 0, 0, 0, 255 };
sym["color"] = RGBA;
sym["backgroundColor"] = null;
sym["borderLineColor"] = null;
sym["borderLineSize"] = null;
sym["verticalAlignment"] = "bottom";
sym["horizontalAlignment"] = "left";
sym["rightToLeft"] = false;
sym["angle"] = 0;
sym["xoffset"] = 0;
sym["yoffset"] = 0;
sym["kerning"] = true;
sym["haloColor"] = null;
sym["haloSize"] = null;
sym["font"] = font;

// Update label definitions

IList<LabelDefinition> LabelDefs = lyr.LabelDefinitions;
List<LabelDefinition> NewDefs = new List<LabelDefinition>();
foreach (LabelDefinition ld in LabelDefs)
{
     string sOldJSON = ld.ToJson();
     Dictionary<string, object> dict = (Dictionary<string, object>)js.DeserializeObject(sOldJSON);
     dict["symbol"] = sym;
     string sNewJSON = js.Serialize(dict);
     NewDefs.Add(LabelDefinition.FromJson(sNewJSON));
}
LabelDefs.Clear();
foreach (LabelDefinition ld in NewDefs)
     LabelDefs.Add(ld);
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
JustinB
Emerging Contributor

Did you ever figure out how the LabelDefinition.ToJson to include the Symbol data?

I am trying to get various properties about the text (Color, FontFamily, etc.) and currently have to work around this by extracting the mapx from and then getting the Json from that but I would really rather not do this and just use the API.

(Currently using Runtime 100.5)

0 Kudos
MarkCederholm
Frequent Contributor

No, as you observe, the problem still exists at 100.5.  At this point I'm just waiting to see if 100.6 fixes this problem.

0 Kudos
MarkCederholm
Frequent Contributor

Problem is still present at 100.6!

0 Kudos
MarkCederholm
Frequent Contributor

Looks like 100.7 finally fixed the problem.  It's interesting that ToJSON returns the full CIM definition; nonetheless, I was able to update the colors successfully.  I notice I can still substitute the simpler style JSON used in the Guide as well.

		private static void UpdateTextSymbol(Dictionary<string, object>labelDef, string sLayerName)
		{

			Dictionary<string, object> txtSymbol = labelDef["symbol"] as Dictionary<string, object>;
			Dictionary<string, object> txtSym = txtSymbol["symbol"] as Dictionary<string, object>;
			Dictionary<string, object> polySymbol = txtSym["symbol"] as Dictionary<string, object>;
			object[] symbolLayers = polySymbol["symbolLayers"] as object[];
			Dictionary<string, object> lyr = symbolLayers[0] as Dictionary<string, object>;

			switch (sLayerName)
			{

				// Landbase labels

				case "Street Name":
				case "Street Centerline":
					lyr["color"] = new object[] { 255, 0, 0, 255 };
					break;

				case "Lot Number":
					lyr["color"] = new object[] { 170, 255, 0, 255 };
					break;

				// Gas labels

				case "Meter Setting":
					lyr["color"] = new object[] { 255, 0, 0, 255 };
					break;

				case "Distribution Main":
				case "High Pressure Distribution Main":
				case "Transmission Main":
					lyr["color"] = new object[] { 255, 255, 0, 255 };
					break;

			}

		}
0 Kudos