# layer.SaveStyle()

**URL:** <https://mapwindow.discourse.group/t/layer-savestyle/260>\
**Category:** MapWinGIS\
**Created:** [September 24, 2019, 12:16pm UTC](https://mapwindow.discourse.group/t/layer-savestyle/260 "2019-09-24T12:16:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![cxdeus](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@cxdeus](https://mapwindow.discourse.group/u/cxdeus)\
**Post date:** [September 24, 2019, 12:16pm UTC](https://mapwindow.discourse.group/t/layer-savestyle/260/1 "2019-09-24T12:16:18Z")

</div>

Hi everyone,

In your documentation there is a following code.

if (!layer.SaveStyle(“new\_style”))  
{  
Debug.WriteLine("Failed to save style: " + layer.GdalLastErrorMsg);  
}  
else  
{  
Debug.WriteLine(“The new style has been saved.”);  
}

layer is OrgLayer.  
But inside my C# enviroment there is no such a method layer.SaveStyle() .There is only layer.RemoveStyle() method.Is there any mistake here?

Thanks a lot

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [September 24, 2019, 11:46pm UTC](https://mapwindow.discourse.group/t/layer-savestyle/260/2 "2019-09-24T23:46:21Z")

</div>

It looks to me like an oversight. I checked the source code, and a SaveStyle is indeed defined on the OgrLayer class, but it is not exposed publicly through the IDL file, so it is inaccessible.

Are you, or can you, build from source code? or are you using the installed component? If you build from source code, you can easily make the change. Otherwise you may have to wait until a new dev/master build is released.

Jerry.

---

<div class="post-metadata">

**Author:** ![cxdeus](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@cxdeus](https://mapwindow.discourse.group/u/cxdeus)\
**Post date:** [September 25, 2019, 11:37am UTC](https://mapwindow.discourse.group/t/layer-savestyle/260/3 "2019-09-25T11:37:11Z")

</div>

Hi JerryFaust,

I was unable to compile the code  
I want to use LoadLayerOptions() with OgrLayer.When i checked the source code i found the following lines in Map\_layer.cpp

İf (l-\>IsOgrLayer())  
return LoadOgrStyle()

But when i try LoadLayerOptions in C# it doesnt get any style from the database.How can i use LoadLayerOptions() in database?

Thanks

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [September 25, 2019, 8:38pm UTC](https://mapwindow.discourse.group/t/layer-savestyle/260/4 "2019-09-25T20:38:56Z")

</div>

Hello @cxdeus

Having never worked with Styles, I’m not that familiar with this code. But I can certainly dig a little deeper.

1. Forgive me for asking, but are you sure you’re specifying the correct name for the Style?
2. Are you getting an error back (using the ICallback interface)? or just no Style returned?
3. What database type are you using, is it one that you can send a sample? Then I could step through it.

Thanks.

---

<div class="post-metadata">

**Author:** ![cxdeus](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@cxdeus](https://mapwindow.discourse.group/u/cxdeus)\
**Post date:** [September 26, 2019, 6:43am UTC](https://mapwindow.discourse.group/t/layer-savestyle/260/5 "2019-09-26T06:43:07Z")

</div>

Hello JerryFaust,

I use PostgreSql.I have shape files and imported them to PostgreSql.In my database i have mw\_styles table.I have four columns in my\_styles table.Styleid,layername,stylename and style. I have also one layer table.It’s name is states.  
Here is my sample code

statesHandle = axmap1.AddLayerFromDatabase(connString, “states”, true); (There is no problem here.It adds the layer without any problem)

string desc = “”;  
var statesretVal = axmap1.LoadLayerOptions(statesHandle, “states”, ref desc); --\> There is a problem here.It doesnt load style options from mw\_styles table.

In my mw\_styles table layername column is “states”.  
axmap1.LoadLayerOptions return false when i want to get styles from database.

Thanks

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [September 26, 2019, 5:14pm UTC](https://mapwindow.discourse.group/t/layer-savestyle/260/6 "2019-09-26T17:14:47Z")

</div>

Internally, the `LoadLayerOptions` call ultimately calls the following:

`LoadStyle(_dataset, GetStyleTableName(), GetLayerName(), name)`

which creates a query as follows:

`sql.Format(L"SELECT style FROM %s WHERE layername = '%s' AND stylename = '%s'", styleTableName, layerName, styleName);`

GetStyleTableName will resolve to mw\_styles, GetLayerName will presumably resolve to “states”, and stylename should be the name of the style. For this parameter, you are passing in “states”.

So your query will look like this (and in fact you should be able to execute this query against PostgreSQL).

`SELECT style FROM mw_styles WHERE layername = 'states' AND stylename = 'states'`

**Is “states” the name of both the Table and the Style?** If you open the mw\_styles table, what is the name of the style associated with table ‘states’? That is the parameter you should be passing in. If the stylename is ‘states’, then it would seem your query should be working, and there must be another problem.

---

<div class="post-metadata">

**Author:** ![cxdeus](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@cxdeus](https://mapwindow.discourse.group/u/cxdeus)\
**Post date:** [September 26, 2019, 7:46pm UTC](https://mapwindow.discourse.group/t/layer-savestyle/260/7 "2019-09-26T19:46:33Z")

</div>

Hello JerryFaust,

My style name is ‘states’ and layer name is ‘states’.  
Do you have any example for ‘style’ column? i think it should be in xml style. I created the style file from states.shp through MapWindow and i copied the xml code inside ‘style’ column in mw\_styles table but didn’t load the style.  
Probably there is a problem with this column.I copied wrong xml code inside the ‘style’ column.

Thanks

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [September 26, 2019, 8:58pm UTC](https://mapwindow.discourse.group/t/layer-savestyle/260/8 "2019-09-26T20:58:52Z")

</div>

Ok, Looking through the code to see where it may be failing…

1. There are a few pre-checks before querying for the Style. Let’s assume for the moment that it is passing those tests (mostly verifying it is an OgrLayer, based on a table lookup ‘states’ rather than a query lookup ‘SELECT \* FROM states’). I think these should be ok.

2. It then queries for the XML string. If you execute the SQL below, you should get back the XML string that is saved as the Style. Do you get that? and does it look ok? (personally, I have never worked with styles, so I don’t know what it should look like).

3. It then tries to parse the XML, looking for a node named ‘Layer’. Does a Layer node exist in your XML?

4. Within the Layer node, it looks like you should see values related to Visibility and Scale, and Labels? Whatever it finds, it simply applies to the current layer.

If you can submit the contents of your XML, I may be able to see whether or not it looks appropriate (based on what I can see in the code. You can also check out the parsing code if you have source to MapWinGIS, in OgrLayer.cpp.

Let me know what you see.

Jerry.

---

<div class="post-metadata">

**Author:** ![cxdeus](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@cxdeus](https://mapwindow.discourse.group/u/cxdeus)\
**Post date:** [September 27, 2019, 8:00am UTC](https://mapwindow.discourse.group/t/layer-savestyle/260/9 "2019-09-27T08:00:45Z")

</div>

Hello Jerry,

Finally it worked.The problem was ‘style’ column.Because of xml syntax.This is the example which worked for me.Below you can see the content of style column in PostgreSQL.

MapWinGIS OcxVersion=“5.0.1.0” FileType=“LayerFile” FileVersion=“2” FilenamesEncoding=“utf8”  
Layer LayerType=“OgrLayer” LayerName=“states” LayerVisible=“1” LayerKey="" Filename="" MinVisibleZoom=“8” DynamicVisibility=“1”  
DefaultDrawingOptions FillColor=“12574681” LineColor=“9868950”  
/Layer  
MapWinGIS

Actually LayerType is Shapefile when you save style to disk. I also changed the LayerType to ‘OgrLayer’

I removed the \< \> characters here because i was unable the post code here  
Thanks a lot again for your help!

---

<div class="post-metadata">

**Author:** ![pmeems](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/pmeems/32/9_2.png) [@pmeems](https://mapwindow.discourse.group/u/pmeems)\
**Post date:** [January 5, 2022, 3:08pm UTC](https://mapwindow.discourse.group/t/layer-savestyle/260/10 "2022-01-05T15:08:52Z")

</div>


