OpenCPN Partial API docs
Loading...
Searching...
No Matches
s57mgr.cpp
1/******************************************************************************
2 *
3 * Project: OpenCP
4 * Purpose: S57 Chart Manager
5 * Author: David Register
6 *
7 ***************************************************************************
8 * Copyright (C) 2010 by David S. Register *
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24 ***************************************************************************
25 *
26 *
27 */
28
29#include "dychart.h"
30
31#include "s57mgr.h"
32
33#include "s57chart.h"
34
35#include "cpl_port.h"
36#include "cpl_csv.h"
37
38#include "gdal.h"
39
40static wxString *pval;
41
42WX_DECLARE_STRING_HASH_MAP(wxString, EnvHash);
43
44static EnvHash env;
45
46//----------------------------------------------------------------------------------
47// mygetenv
48//
49// Replacement for posix getenv() which works for __WXMSW__
50// Todo Make this thing into a couple of string arrays to stop leakage
51//----------------------------------------------------------------------------------
52
53/*
54extern "C" char *mygetenv(char *pvar)
55{
56 wxString key(pvar);
57 wxString test_val = env[key];
58 if(test_val.Len())
59 {
60 pval->Empty();
61 pval->Append(wxString(test_val));
62 return((char *)pval->mb_str());
63
64 }
65 else
66 {
67 wxString val;
68 wxGetEnv(key, &val);
69 env[key] = val;
70 pval->Empty();
71 pval->Append(wxString(val));
72 return((char *)pval->mb_str());
73 }
74
75}
76*/
77
78//----------------------------------------------------------------------------------
79// s57mgr Implementation
80//----------------------------------------------------------------------------------
81
82s57mgr::s57mgr(const wxString &csv_dir) {
83#ifdef __WXMSW__
84 pval = new wxString; // initialize static string
85#endif
86
87 // MS Windows Build Note:
88 // In a .dll GDAL build, the following _putenv() call DOES NOT properly
89 // set the environment accessible to GDAL/OGR. So, S57 Reader options
90 // are not set AT ALL. Defaults will apply.
91 // See the README file
92
93#ifdef __WXMSW__
94 wxString envs1("S57_CSV=");
95 envs1.Append(csv_dir);
96 _putenv(envs1.mb_str());
97#else
98 wxSetEnv("S57_CSV", csv_dir.mb_str());
99#endif
100
101 // Set some S57 OGR Options thru environment variables
102
103 // n.b. THERE IS A BUG in GDAL/OGR 1.2.0 wherein the sense of the flag
104 // UPDATES= is reversed. That is, anything other than UPDATES=APPLY selects
105 // update mode. Conversely, UPDATES=APPLY deselects updates. Fixed in
106 // GDAL 1.3.2, at least, maybe earlier?? Detect by GDALVersion check
107
108 wxString set1, set2;
109
110 set1 = "LNAM_REFS=ON";
111 set1.Append(",SPLIT_MULTIPOINT=OFF");
112 set1.Append(",ADD_SOUNDG_DEPTH=OFF");
113 set1.Append(",PRESERVE_EMPTY_NUMBERS=OFF");
114 set1.Append(",RETURN_PRIMITIVES=OFF");
115 set1.Append(",RETURN_LINKAGES=OFF");
116
117 const char *version_string = GDALVersionInfo("VERSION_NUM");
118 int ver_num = (int)CPLScanLong((char *)version_string, 4);
119
120 if (ver_num < 1320)
121 set2 = ",UPDATES=BUGBUG"; // updates ENABLED
122 else
123 set2 = ",UPDATES=APPLY";
124
125 set1.Append(set2);
126
127#ifdef __WXMSW__
128 wxString envs2("OGR_S57_OPTIONS=");
129 envs2.Append(set1);
130 _putenv(envs2.mb_str());
131
132#else
133 wxSetEnv("OGR_S57_OPTIONS", set1.mb_str());
134#endif
135
136 pcsv_locn = new wxString(csv_dir);
137
138 // CPLSetConfigOption( "CPL_DEBUG", "ON");
139 // CPLSetConfigOption( "CPL_LOG", "c:\\LOG");
140
141 RegisterOGRS57();
142}
143
144s57mgr::~s57mgr() {
145 delete pcsv_locn;
146 delete pval;
147
148 // Close and release any csv file access elements,
149 // Particularly the s57objectclasses.csv used for s57 object query support
150 CSVDeaccess(NULL);
151}
152
153//----------------------------------------------------------------------------------
154// Get First Chart M_COVR Object
155// n.b. Caller owns the data source and the feature on success
156//----------------------------------------------------------------------------------
157bool s57mgr::GetChartFirstM_COVR(char *pFullPath, OGRDataSource **pDS,
158 OGRFeature **pFeature, OGRLayer **pLayer,
159 int &catcov) {
160 OGRDataSource *poDS = OGRSFDriverRegistrar::Open(pFullPath);
161
162 *pDS = poDS; // give to caller
163
164 if (poDS == NULL) {
165 *pFeature = NULL;
166 return false;
167 }
168
169 OGRLayer *pLay = poDS->GetLayerByName("M_COVR");
170 *pLayer = pLay; // Give to caller
171 pLay->ResetReading();
172 OGRFeature *objectDef = pLay->GetNextFeature();
173 *pFeature = objectDef; // Give to caller
174
175 if (objectDef) {
176 // Fetch the CATCOV attribute
177 for (int iField = 0; iField < objectDef->GetFieldCount(); iField++) {
178 if (objectDef->IsFieldSet(iField)) {
179 OGRFieldDefn *poFDefn = objectDef->GetDefnRef()->GetFieldDefn(iField);
180 if (!strcmp(poFDefn->GetNameRef(), "CATCOV"))
181 catcov = objectDef->GetFieldAsInteger(iField);
182 }
183 }
184 return true;
185 }
186
187 else {
188 delete poDS;
189 *pDS = NULL;
190 return false;
191 }
192}
193
194//----------------------------------------------------------------------------------
195// GetNext Chart M_COVR Object
196// n.b. Caller still owns the data source and the feature on
197// success
198//----------------------------------------------------------------------------------
199bool s57mgr::GetChartNextM_COVR(OGRDataSource *pDS, OGRLayer *pLayer,
200 OGRFeature *pLastFeature, OGRFeature **pFeature,
201 int &catcov) {
202 if (pDS == NULL) return false;
203
204 catcov = -1;
205
206 int fid = pLastFeature->GetFID();
207
208 OGRFeature *objectDef = pLayer->GetFeature(fid + 1);
209 *pFeature = objectDef; // Give to caller
210
211 if (objectDef) {
212 for (int iField = 0; iField < objectDef->GetFieldCount(); iField++) {
213 if (objectDef->IsFieldSet(iField)) {
214 OGRFieldDefn *poFDefn = objectDef->GetDefnRef()->GetFieldDefn(iField);
215 if (!strcmp(poFDefn->GetNameRef(), "CATCOV"))
216 catcov = objectDef->GetFieldAsInteger(iField);
217 }
218 }
219 return true;
220 }
221 return false;
222}
223
224//----------------------------------------------------------------------------------
225// Get Chart Extents
226//----------------------------------------------------------------------------------
227bool s57mgr::GetChartExtent(char *pFullPath, Extent *pext) {
228 // Fix this find extents of which?? layer??
229 /*
230 OGRS57DataSource *poDS = new OGRS57DataSource;
231 poDS->Open(pFullPath, TRUE);
232
233 if( poDS == NULL )
234 return false;
235
236 OGREnvelope Env;
237 S57Reader *poReader = poDS->GetModule(0);
238 poReader->GetExtent(&Env, true);
239
240 pext->NLAT = Env.MaxY;
241 pext->ELON = Env.MaxX;
242 pext->SLAT = Env.MinY;
243 pext->WLON = Env.MinX;
244
245 delete poDS;
246 */
247 return true;
248}
249
250//----------------------------------------------------------------------------------
251// Get Chart Scale
252//----------------------------------------------------------------------------------
253int s57mgr::GetChartScale(char *pFullPath) {
254 DDFModule poModule;
255
256 if (!poModule.Open(pFullPath)) {
257 return 0;
258 }
259
260 DDFRecord *poRecord = poModule.ReadRecord();
261 if (poRecord == NULL) {
262 poModule.Close();
263 return 0;
264 }
265
266 int scale = 1;
267 for (; poRecord != NULL; poRecord = poModule.ReadRecord()) {
268 if (poRecord->FindField("DSPM") != NULL) {
269 scale = poRecord->GetIntSubfield("DSPM", 0, "CSCL", 0);
270 break;
271 }
272 }
273
274 poModule.Close();
275
276 return scale;
277}
Definition: Quilt.cpp:864