Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: OpenGIS Simple Features Reference Implementation
4 : * Purpose: Implements OGRMemDataSource class.
5 : * Author: Frank Warmerdam, warmerdam@pobox.com
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2003, Frank Warmerdam <warmerdam@pobox.com>
9 : *
10 : * Permission is hereby granted, free of charge, to any person obtaining a
11 : * copy of this software and associated documentation files (the "Software"),
12 : * to deal in the Software without restriction, including without limitation
13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 : * and/or sell copies of the Software, and to permit persons to whom the
15 : * Software is furnished to do so, subject to the following conditions:
16 : *
17 : * The above copyright notice and this permission notice shall be included
18 : * in all copies or substantial portions of the Software.
19 : *
20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 : * DEALINGS IN THE SOFTWARE.
27 : ****************************************************************************/
28 :
29 : #include "cpl_port.h"
30 : #include "ogr_mem.h"
31 :
32 : #include "cpl_conv.h"
33 : #include "cpl_string.h"
34 : #include "ogr_core.h"
35 : #include "ogr_spatialref.h"
36 : #include "ogrsf_frmts.h"
37 :
38 : /************************************************************************/
39 : /* OGRMemDataSource() */
40 : /************************************************************************/
41 :
42 679 : OGRMemDataSource::OGRMemDataSource(const char *pszFilename,
43 679 : char ** /* papszOptions */)
44 679 : : papoLayers(nullptr), nLayers(0), pszName(CPLStrdup(pszFilename))
45 : {
46 679 : }
47 :
48 : /************************************************************************/
49 : /* ~OGRMemDataSource() */
50 : /************************************************************************/
51 :
52 1356 : OGRMemDataSource::~OGRMemDataSource()
53 :
54 : {
55 678 : CPLFree(pszName);
56 :
57 1463 : for (int i = 0; i < nLayers; i++)
58 785 : delete papoLayers[i];
59 :
60 678 : CPLFree(papoLayers);
61 1356 : }
62 :
63 : /************************************************************************/
64 : /* ICreateLayer() */
65 : /************************************************************************/
66 :
67 : OGRLayer *
68 793 : OGRMemDataSource::ICreateLayer(const char *pszLayerName,
69 : const OGRGeomFieldDefn *poGeomFieldDefn,
70 : CSLConstList papszOptions)
71 : {
72 : // Create the layer object.
73 :
74 793 : const auto eType = poGeomFieldDefn ? poGeomFieldDefn->GetType() : wkbNone;
75 : const auto poSRSIn =
76 793 : poGeomFieldDefn ? poGeomFieldDefn->GetSpatialRef() : nullptr;
77 :
78 793 : OGRSpatialReference *poSRS = nullptr;
79 793 : if (poSRSIn)
80 : {
81 181 : poSRS = poSRSIn->Clone();
82 181 : poSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
83 : }
84 793 : OGRMemLayer *poLayer = new OGRMemLayer(pszLayerName, poSRS, eType);
85 793 : if (poSRS)
86 : {
87 181 : poSRS->Release();
88 : }
89 :
90 793 : if (CPLFetchBool(papszOptions, "ADVERTIZE_UTF8", false))
91 31 : poLayer->SetAdvertizeUTF8(true);
92 :
93 793 : poLayer->SetDataset(this);
94 793 : poLayer->SetFIDColumn(CSLFetchNameValueDef(papszOptions, "FID", ""));
95 :
96 : // Add layer to data source layer list.
97 793 : papoLayers = static_cast<OGRMemLayer **>(
98 793 : CPLRealloc(papoLayers, sizeof(OGRMemLayer *) * (nLayers + 1)));
99 :
100 793 : papoLayers[nLayers++] = poLayer;
101 :
102 793 : return poLayer;
103 : }
104 :
105 : /************************************************************************/
106 : /* DeleteLayer() */
107 : /************************************************************************/
108 :
109 9 : OGRErr OGRMemDataSource::DeleteLayer(int iLayer)
110 :
111 : {
112 9 : if (iLayer >= 0 && iLayer < nLayers)
113 : {
114 7 : delete papoLayers[iLayer];
115 :
116 9 : for (int i = iLayer + 1; i < nLayers; ++i)
117 2 : papoLayers[i - 1] = papoLayers[i];
118 :
119 7 : --nLayers;
120 :
121 7 : return OGRERR_NONE;
122 : }
123 :
124 2 : return OGRERR_FAILURE;
125 : }
126 :
127 : /************************************************************************/
128 : /* TestCapability() */
129 : /************************************************************************/
130 :
131 582 : int OGRMemDataSource::TestCapability(const char *pszCap)
132 :
133 : {
134 582 : if (EQUAL(pszCap, ODsCCreateLayer))
135 228 : return TRUE;
136 354 : else if (EQUAL(pszCap, ODsCDeleteLayer))
137 1 : return TRUE;
138 353 : else if (EQUAL(pszCap, ODsCCreateGeomFieldAfterCreateLayer))
139 137 : return TRUE;
140 216 : else if (EQUAL(pszCap, ODsCCurveGeometries))
141 19 : return TRUE;
142 197 : else if (EQUAL(pszCap, ODsCMeasuredGeometries))
143 0 : return TRUE;
144 197 : else if (EQUAL(pszCap, ODsCZGeometries))
145 0 : return TRUE;
146 197 : else if (EQUAL(pszCap, ODsCRandomLayerWrite))
147 8 : return TRUE;
148 189 : else if (EQUAL(pszCap, ODsCAddFieldDomain))
149 10 : return TRUE;
150 179 : else if (EQUAL(pszCap, ODsCDeleteFieldDomain))
151 0 : return TRUE;
152 179 : else if (EQUAL(pszCap, ODsCUpdateFieldDomain))
153 0 : return TRUE;
154 :
155 179 : return FALSE;
156 : }
157 :
158 : /************************************************************************/
159 : /* GetLayer() */
160 : /************************************************************************/
161 :
162 3859 : OGRLayer *OGRMemDataSource::GetLayer(int iLayer)
163 :
164 : {
165 3859 : if (iLayer < 0 || iLayer >= nLayers)
166 5 : return nullptr;
167 :
168 3854 : return papoLayers[iLayer];
169 : }
170 :
171 : /************************************************************************/
172 : /* AddFieldDomain() */
173 : /************************************************************************/
174 :
175 20 : bool OGRMemDataSource::AddFieldDomain(std::unique_ptr<OGRFieldDomain> &&domain,
176 : std::string &failureReason)
177 : {
178 20 : if (GetFieldDomain(domain->GetName()) != nullptr)
179 : {
180 1 : failureReason = "A domain of identical name already exists";
181 1 : return false;
182 : }
183 19 : const std::string domainName(domain->GetName());
184 19 : m_oMapFieldDomains[domainName] = std::move(domain);
185 19 : return true;
186 : }
187 :
188 : /************************************************************************/
189 : /* DeleteFieldDomain() */
190 : /************************************************************************/
191 :
192 6 : bool OGRMemDataSource::DeleteFieldDomain(const std::string &name,
193 : std::string &failureReason)
194 : {
195 6 : const auto iter = m_oMapFieldDomains.find(name);
196 6 : if (iter == m_oMapFieldDomains.end())
197 : {
198 2 : failureReason = "Domain does not exist";
199 2 : return false;
200 : }
201 :
202 4 : m_oMapFieldDomains.erase(iter);
203 :
204 8 : for (int i = 0; i < nLayers; i++)
205 : {
206 4 : OGRMemLayer *poLayer = papoLayers[i];
207 10 : for (int j = 0; j < poLayer->GetLayerDefn()->GetFieldCount(); ++j)
208 : {
209 : OGRFieldDefn *poFieldDefn =
210 6 : poLayer->GetLayerDefn()->GetFieldDefn(j);
211 6 : if (poFieldDefn->GetDomainName() == name)
212 : {
213 3 : auto oTemporaryUnsealer(poFieldDefn->GetTemporaryUnsealer());
214 3 : poFieldDefn->SetDomainName(std::string());
215 : }
216 : }
217 : }
218 :
219 4 : return true;
220 : }
221 :
222 : /************************************************************************/
223 : /* UpdateFieldDomain() */
224 : /************************************************************************/
225 :
226 3 : bool OGRMemDataSource::UpdateFieldDomain(
227 : std::unique_ptr<OGRFieldDomain> &&domain, std::string &failureReason)
228 : {
229 6 : const std::string domainName(domain->GetName());
230 3 : const auto iter = m_oMapFieldDomains.find(domainName);
231 3 : if (iter == m_oMapFieldDomains.end())
232 : {
233 1 : failureReason = "No matching domain found";
234 1 : return false;
235 : }
236 2 : m_oMapFieldDomains[domainName] = std::move(domain);
237 2 : return true;
238 : }
|