-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathOgrStyle.cpp
More file actions
162 lines (144 loc) · 5.52 KB
/
OgrStyle.cpp
File metadata and controls
162 lines (144 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "stdafx.h"
#include "OgrStyle.h"
#include "OgrHelper.h"
#include "ShapefileHelper.h"
#include "ShapeStyleHelper.h"
char* STYLES_TABLE_NAME = "mw_styles";
// *************************************************************
// LoadStyle()
// *************************************************************
CStringW OgrStyleHelper::LoadStyle(GDALDataset* dataset, CStringW styleTableName, CStringW layerName, CStringW styleName)
{
USES_CONVERSION;
CStringW sql;
sql.Format(L"SELECT style FROM %s WHERE layername = '%s' AND stylename = '%s'", styleTableName, layerName, styleName);
CStringW xml = L"";
bool found = false;
CPLErrorReset();
OGRLayer* layer = dataset->ExecuteSQL(OgrHelper::String2OgrString(sql), NULL, NULL);
if (layer) {
OGRFeature* ft = layer->GetNextFeature();
if (ft) {
const char* s = ft->GetFieldAsString(0);
if (s) {
xml = OgrHelper::OgrString2Unicode(s);
}
OGRFeature::DestroyFeature(ft);
}
dataset->ReleaseResultSet(layer);
}
return xml;
}
// *************************************************************
// HasStyleTable()
// *************************************************************
bool OgrStyleHelper::HasStyleTable(GDALDataset* dataset, CStringW layerName)
{
USES_CONVERSION;
CStringW sql;
sql.Format(L"SELECT COUNT(*) FROM %s", A2W(STYLES_TABLE_NAME));
// we are checking the presence; it's normal to receive an error; no need to report it to user
m_globalSettings.suppressGdalErrors = true;
OGRLayer* lyr = dataset->ExecuteSQL(OgrHelper::String2OgrString(sql), NULL, NULL);
m_globalSettings.suppressGdalErrors = false;
bool hasTable = lyr != NULL;
if (lyr)
dataset->ReleaseResultSet(lyr);
else
CPLErrorReset();
return hasTable;
}
// *************************************************************
// HasStyleTable()
// *************************************************************
bool OgrStyleHelper::SaveStyle(GDALDataset* dataset, IShapefile* shapefile, CStringW layerName, CStringW styleName)
{
CStringW xml = ShapeStyleHelper::GetSymbologyFileAsXml(shapefile);
return SaveStyle(dataset, xml, layerName, styleName);
}
// *************************************************************
// SaveStyle()
// *************************************************************
bool OgrStyleHelper::SaveStyle(GDALDataset* dataset, CStringW xml, CStringW layerName, CStringW styleName)
{
xml.Replace(L"\n", L"");
xml.Replace(L"'", L"''''");
if (xml.GetLength() == 0) return false;
CStringW sql;
sql.Format(L"INSERT INTO %s(layername, stylename, style) VALUES ('%s', '%s', '%s')", GetStyleTableName(layerName),
layerName, styleName, xml);
CPLErrorReset();
OGRLayer* layer = dataset->ExecuteSQL(OgrHelper::String2OgrString(sql), NULL, NULL);
return CPLGetLastErrorNo() == OGRERR_NONE;
}
// *************************************************************
// GerStyleTableName()
// *************************************************************
CStringW OgrStyleHelper::GetStyleTableName(CStringW layerName)
{
CStringW name;
USES_CONVERSION;
if (m_globalSettings.useSchemesForStyles) {
name.Format(L"%s%s", GetDbSchemeName(layerName, true), A2W(STYLES_TABLE_NAME));
}
else {
name = A2W(STYLES_TABLE_NAME);
}
return name;
}
// *************************************************************
// GetDbSchemeName()
// *************************************************************
CStringW OgrStyleHelper::GetDbSchemeName(CStringW layerName, bool withTrailingPoint)
{
int pos = layerName.ReverseFind(L'.');
if (pos != -1)
{
return layerName.Mid(0, pos + withTrailingPoint ? 1 : 0);
}
return L"";
}
// *************************************************************
// CreateStyleTable()
// *************************************************************
int OgrStyleHelper::CreateStyleTable(GDALDataset* dataset, CStringW layerName)
{
if (OgrHelper::IsPostGisDatasource(dataset))
{
CPLErrorReset();
CStringW schemaName = GetDbSchemeName(layerName, true);
CStringW sql;
sql.Format(L"CREATE Table %s (StyleId serial primary key, LayerName varchar(128), StyleName varchar(128), Style text, CONSTRAINT layer_style_unique UNIQUE (LayerName,StyleName));", GetStyleTableName());
OGRLayer* lyr = dataset->ExecuteSQL(OgrHelper::String2OgrString(sql), NULL, NULL);
return CPLGetLastErrorNo() == OGRERR_NONE ? tkNO_ERROR : tkOGR_FAILED_TO_CREATE_STYLE_TABLE;
}
else {
return tkOGR_NO_STYLE_TABLE_CREATION;
}
}
// *************************************************************
// SupportsStyles()
// *************************************************************
bool OgrStyleHelper::SupportsStyles(GDALDataset* dataset, CStringW layerName)
{
if (!OgrStyleHelper::HasStyleTable(dataset, layerName))
{
long errorCode = OgrStyleHelper::CreateStyleTable(dataset, layerName);
if (errorCode != tkNO_ERROR)
return false;
}
return true;
}
// *************************************************************
// RemoveStyle()
// *************************************************************
bool OgrStyleHelper::RemoveStyle(GDALDataset* dataset, CStringW styleTableName, CStringW layerName, CStringW styleName)
{
USES_CONVERSION;
CStringW sql;
sql.Format(L"DELETE FROM %s WHERE layername = '%s' AND stylename = '%s'", styleTableName, layerName, styleName);
CPLErrorReset();
OGRLayer* layer = dataset->ExecuteSQL(OgrHelper::String2OgrString(sql), NULL, NULL);
dataset->ExecuteSQL(OgrHelper::String2OgrString(sql), NULL, NULL);
return CPLGetLastErrorNo() == OGRERR_NONE;
}