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?
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.
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?
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.
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.
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.
Ok, Looking through the code to see where it may be failing…
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.
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'
It then tries to parse the XML, looking for a node named ‘Layer’. Does a Layer node exist in your XML?
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.
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.