Managing Symbology using Python

22699
25
11-23-2011 05:15 AM
DaveJordan1
New Contributor III
Can someone point me to the correct help source for managing symbology using python?  I want to change symbol colors, line widths... for feature classes as well as graphics

Thanks in advance
Tags (2)
25 Replies
BrandonConner
New Contributor II

I have a situation where I have a unique values source layer file that I use to update polygon feature classes with the appropriate symbology.

arcpy.mapping.UpdateLayer() does a great job, however, it applies all possible values ( 428 possibles ). This makes the legend unusable, and I only need the 10-20 I actually have values for in the feature class. Using the symbology.addAllValues() changes my labels AND reorders the colors assigned to the unique values, frustrating.

arcpy.ApplySymbologyFromLayer_management() only applies the ones I have values for in the feature class, fantastic, except it doesn't respect the headings from the source layer file. What do I do?

Do I edit each layer file I'm creating to add in the headings, etc... that I need? OR

Do I open each layer file and delete all values that have a zero count?

Keeping in mind that there are many of these files generated daily.

Thanks for your time and any offer of help,

Brandon

JasonHarshman
Occasional Contributor

Can you set the legend option to "Only show features class that are visible in the current map extent"? 

0 Kudos
BrandonConner
New Contributor II

It's not that I can't find a way to display them.

These layer files are converted to kml's and ovl's. They get delivered to customers.

Not GIS expert customers either. My main customer is the NAVY.

I've python scripted most of the final product creation that we do here, but this last thing still has to be done by hand.

0 Kudos
JasonHarshman
Occasional Contributor

What if you used Make Feature Layer on the layer file source, but dynamically apply a where clause to only the feature(s) in a given feature class. Then use ApplySymbologyFromLayer_management to apply the symbology to the feature class from the temporary layer created using Make Feature Layer. Something like this:


sourceLyr = "source_layer.lyr"
tmpLyrName = "tmpLyr"
featureClass = "data.gdb/fc"
expression = some SQL 

MakeFeatureLayer_management(sourceLyr,tmpLyr,expression)
ApplySymbologyFromLayer_management(featureClass,tmpLyr)

It seems what you're trying to do can be done without manually editing so I'm sure there are probably other ways to do this.

0 Kudos
BrandonConner
New Contributor II

Sorry, ApplySymbologyFromLayer_management() doesn't respect the headings in the layer file, so it still has to be edited. That's actually worse than deleting all the values with a zero count when using mapping.UpdateLayer().

Thanks for trying though.

MarceloRosensaft
New Contributor III

Hello all,

I'm looking for a solution to my situation although I'm quite pessimistic. I work with Python/arcpy for ArcMap.

I have a similar situation like the one previously described: one huge layer file with the enterprise symbology for a polygons unique value layer and the need to apply it to smaller layers programatically. I use Python and arcpy.mapping since I need MXD files, although a solution for arcpy.mp would do some help.

I tried ApplySymbologyFromLayer_management and also mapping.UpdateLayer. Both work by creating a layer that can be saved to a layer file and also saved in an MXD. The problem is that my value field values are not ordered in the order that I want to see in the TOC - the order that we use in the symbology of the enterprise layer file - and when I use any of the two methods - ApplySymbology... or UpdateLayer - I get the values list in ascending order of the value field and that's not what I need.

I've also tried to play with the symbology.classValues and symbology.classLabels lists, by changing the order of the elements, but nothing helps. The order goes back to the wrong one.

I'm happy with any help. Thank you in advance,

Marcelo

0 Kudos
ChristopherThompson
Occasional Contributor III
I'm the guilty one, that was my presentation and my quote.  Now I want to give it some context and ask a couple of questions....We could easily triple the arcpy.mapping API but just attempting to provide all that capability.  We don't want to do that.  We are trying to keep arcpy.mapping as simple as possible without making it as complex as ArcObjects....Help me understand, in a real mapping scenario, why do you need to dynamically change these symbol properties?  ...without having to recreate ArcObjects capabilities at the arcpy.mapping level...Thanks for your help,
Jeff


Hi Jeff, don't misunderstand me, i totally get the message and understand why there are limitations in the arcpy.mapping module. I'm really on the fence with some of these issues.  As a user in a real-world GIS context the issues surrounding my desire to have more ability within arcpy.mapping are two-fold.

One is simply about having tools that are easily grasped and are accessible.  ArcObjects is great, if you are writing .net code and have access to development tools like VisualStudio - but not all organizations have these or the staff who have the skills to use these.  The amount of learning one has to do to master ArcObjects is also somewhat formidable as well, and given the rate at which technology is advancing, the more things one has to learn to simply 'do the job' is a challenge.  I love Python because it cheap, fast, easy and lightweight (attributes i tend to appreciate about most things in life) - it costs virtually nothing for my organization to use and the ease of which it can be implemented really helps in the fast-paced world in which we tend to work.  Conversely, I also don't want to see arcpy.mapping become so bloated and complex that it defeats the stated purpose.  Part of my 'frustration' (if you can call it that) is that VBA is gone- Python scripting I thought was the answer to the deprecation of that capability, but arcpy and arcpy.mapping does that in a hamstrung way.  At least with VBA you had access to all of arcObjects, even though you can't really distribute those tools very easily.

The other issue simply has to do with real world practicalities of managing things like .lyr files.  So much of what we do are short lived products that managing on-disk libraries of things like .lyr files and such just isn't always practical.  If i can write code that creates these without having to take the time to create and maintain a file structure and things inside it then thats very helpful. For example, recently i wrote a script that makes a selection in featureclass A, and then selects all features from another featureclass B where those features intersect.  The outputs from the script are .lyr files saved to disk with definition queries so these aren't simply 'selection layers'.  How nice would it be to simply create those layers on the fly (there are 96 of them in the end) and add those to maps iteratively and apply symbology.  To accomplish this in arcypy i have to save these to disk, manually create 'template layers' with the proper symbology (simple stuff like line weights and color), then write the code to access those .lyr files and point at the template layers to get symbology, and then add those to a map. Though its easier than doing this manually, its certainly not as elegant as it could be and having to write data to disk seems unnecessary.

Ultimately, things aren't bad, they are in fact really good, its just the more capability I see, the more I want without having to change horses.  I want one set of development tools, and not have to spend time moving down one development path only to realize "oh crap, i really need to be writing .net code against arcObjects to do this".  Contact me via email if you want to discuss more about some of this - i dont' want to take this thread any further off-topic at this point.
BarraLoubna
New Contributor
Hello,

I would like to change the symbology of a single layer at a transition from one scale to another.

You can found my post here:

http://forums.arcgis.com/threads/72035-How-to-change-the-symbology-of-a-single-layer-at-a-transition....

Please i need a help.

Thank you.
0 Kudos
MatthewGerbrandt
New Contributor II
Hello, everyone. I'm a little late to the discussion but was hoping one of you could point me in the right direction. I've got a script that downloads a large number of shapefiles from an FTP site.

Those shapefiles are then imported into a FileGeodatabase FeatureClass and their datestamp is used for graphically differentiating them. These datestamps change over time, so three days from now, thousands of new datestamps will appear in the database. I want all of these data to be represented by a an arrow whose rotation is defined by a field in the featureclass. I've attached a screenshot, showing the conversion which I need to perform in terms of symbology.

Could someone please tell me if this task is possible through Python and if so, how would I do it? Thanks a million!



I'm the guilty one, that was my presentation and my quote.  Now I want to give it some context and ask a couple of questions...

Help me understand, in a real mapping scenario, why do you need to dynamically change these symbol properties?...

I'm not trying to argue here, I'm looking for well documented scenarios to help us continue expanding the capabilities of arcpy.mapping where absolutely needed for map automation tasks.  At 10.1, we added quite a few new capabilities including some symbology classes that are needed to support map automation: GraduatedColorSymbology, GraduatedSymbolsSymbology, etc.

Thanks for your help,
Jeff
0 Kudos
TedChapin
Occasional Contributor III
Jeff,

Hopefully you still get a notification of this reply to a year-old post.  Here's a real-world scenario where sheet-by-sheet programmatic access to detailed symbology properties is needed.  Data driven pages support attribute-driven map rotation.  But polygon line fill symbols do not support attribute-driven angles.  So when the data frame is rotated the 45 degree symbol is not 45 degrees anymore.  This is a real problem if you have more than one angled line symbol (forward leaning angle means one thing, backwards leaning angle means something else).  If the map is rotated just right the symbols are just cartographically wrong.  In a static map you can calculate the fill angle to compensate for the map rotation, but this is not possible in a dynamic data driven page where the rotation is changing.  We can do really cool things with Python and data driven pages, but this is not one of them.  ArcObjects should not be required to solve this problem, which has existed for years.

Ted Chapin
0 Kudos