layer.SaveStyle()

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

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.

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

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.

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

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.

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

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).

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

  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.

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!