forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnmresultlayer.cpp
219 lines (189 loc) · 7.25 KB
/
gnmresultlayer.cpp
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/******************************************************************************
*
* Project: GDAL/OGR Geography Network support (Geographic Network Model)
* Purpose: GNM result layer class.
* Authors: Mikhail Gusev (gusevmihs at gmail dot com)
* Dmitry Baryshnikov, [email protected]
*
******************************************************************************
* Copyright (c) 2014, Mikhail Gusev
* Copyright (c) 2014-2015, NextGIS <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "gnm.h"
#include "gnm_priv.h"
/** Constructor */
OGRGNMWrappedResultLayer::OGRGNMWrappedResultLayer(GDALDataset *poDSIn,
OGRLayer *poLayerIn)
{
this->poDS = poDSIn;
this->poLayer = poLayerIn;
// create standard fields
OGRFieldDefn oFieldGID(GNM_SYSFIELD_GFID, GNMGFIDInt);
poLayer->CreateField(&oFieldGID);
OGRFieldDefn oFieldLayerName(GNM_SYSFIELD_LAYERNAME, OFTString);
oFieldLayerName.SetWidth(254);
poLayer->CreateField(&oFieldLayerName);
OGRFieldDefn oFieldNo(GNM_SYSFIELD_PATHNUM, OFTInteger);
poLayer->CreateField(&oFieldNo);
OGRFieldDefn oFieldType(GNM_SYSFIELD_TYPE, OFTString); // EDGE or VERTEX
poLayer->CreateField(&oFieldType);
}
OGRGNMWrappedResultLayer::~OGRGNMWrappedResultLayer()
{
delete poDS;
}
void OGRGNMWrappedResultLayer::ResetReading()
{
poLayer->ResetReading();
}
OGRFeature *OGRGNMWrappedResultLayer::GetNextFeature()
{
return poLayer->GetNextFeature();
}
OGRErr OGRGNMWrappedResultLayer::SetNextByIndex(GIntBig nIndex)
{
return poLayer->SetNextByIndex(nIndex);
}
OGRFeature *OGRGNMWrappedResultLayer::GetFeature(GIntBig nFID)
{
return poLayer->GetFeature(nFID);
}
OGRFeatureDefn *OGRGNMWrappedResultLayer::GetLayerDefn()
{
return poLayer->GetLayerDefn();
}
GIntBig OGRGNMWrappedResultLayer::GetFeatureCount(int bForce)
{
return poLayer->GetFeatureCount(bForce);
}
int OGRGNMWrappedResultLayer::TestCapability(const char *pszCap)
{
return poLayer->TestCapability(pszCap);
}
OGRErr OGRGNMWrappedResultLayer::CreateField(const OGRFieldDefn *poField,
int bApproxOK)
{
return poLayer->CreateField(poField, bApproxOK);
}
OGRErr
OGRGNMWrappedResultLayer::CreateGeomField(const OGRGeomFieldDefn *poField,
int bApproxOK)
{
return poLayer->CreateGeomField(poField, bApproxOK);
}
const char *OGRGNMWrappedResultLayer::GetFIDColumn()
{
return poLayer->GetFIDColumn();
}
const char *OGRGNMWrappedResultLayer::GetGeometryColumn()
{
return poLayer->GetGeometryColumn();
}
OGRSpatialReference *OGRGNMWrappedResultLayer::GetSpatialRef()
{
return poLayer->GetSpatialRef();
}
/** Undocumented */
OGRErr OGRGNMWrappedResultLayer::InsertFeature(OGRFeature *poFeature,
const CPLString &soLayerName,
int nPathNo, bool bIsEdge)
{
VALIDATE_POINTER1(poFeature, "Input feature is invalid",
OGRERR_INVALID_HANDLE);
// add fields from input feature
OGRFeatureDefn *poSrcDefn = poFeature->GetDefnRef();
OGRFeatureDefn *poDstFDefn = GetLayerDefn();
if (nullptr == poSrcDefn || nullptr == poDstFDefn)
return OGRERR_INVALID_HANDLE;
int nSrcFieldCount = poSrcDefn->GetFieldCount();
int nDstFieldCount = poDstFDefn->GetFieldCount();
int iField, *panMap;
// Initialize the index-to-index map to -1's
panMap = (int *)CPLMalloc(sizeof(int) * nSrcFieldCount);
for (iField = 0; iField < nSrcFieldCount; iField++)
panMap[iField] = -1;
for (iField = 0; iField < nSrcFieldCount; iField++)
{
OGRFieldDefn *poSrcFieldDefn = poSrcDefn->GetFieldDefn(iField);
OGRFieldDefn oFieldDefn(poSrcFieldDefn);
/* The field may have been already created at layer creation */
int iDstField = poDstFDefn->GetFieldIndex(oFieldDefn.GetNameRef());
if (iDstField >= 0)
{
// TODO: by now skip fields with different types. In future shoul
// cast types
OGRFieldDefn *poDstField = poDstFDefn->GetFieldDefn(iDstField);
if (nullptr != poDstField &&
oFieldDefn.GetType() == poDstField->GetType())
panMap[iField] = iDstField;
}
else if (CreateField(&oFieldDefn) == OGRERR_NONE)
{
/* Sanity check : if it fails, the driver is buggy */
if (poDstFDefn->GetFieldCount() != nDstFieldCount + 1)
{
CPLError(CE_Warning, CPLE_AppDefined,
"The output driver has claimed to have added the %s "
"field, but it did not!",
oFieldDefn.GetNameRef());
}
else
{
panMap[iField] = nDstFieldCount;
nDstFieldCount++;
}
}
}
OGRFeature *poInsertFeature = OGRFeature::CreateFeature(GetLayerDefn());
if (poInsertFeature->SetFrom(poFeature, panMap, TRUE) != OGRERR_NONE)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Unable to translate feature " CPL_FRMT_GIB
" from layer %s.\n",
poFeature->GetFID(), soLayerName.c_str());
OGRFeature::DestroyFeature(poInsertFeature);
CPLFree(panMap);
return OGRERR_FAILURE;
}
// poInsertFeature->SetField( GNM_SYSFIELD_GFID,
// (GNMGFID)poFeature->GetFID() );
poInsertFeature->SetField(GNM_SYSFIELD_LAYERNAME, soLayerName);
poInsertFeature->SetField(GNM_SYSFIELD_PATHNUM, nPathNo);
poInsertFeature->SetField(GNM_SYSFIELD_TYPE, bIsEdge ? "EDGE" : "VERTEX");
CPLErrorReset();
if (CreateFeature(poInsertFeature) != OGRERR_NONE)
{
OGRFeature::DestroyFeature(poInsertFeature);
CPLFree(panMap);
return OGRERR_FAILURE;
}
OGRFeature::DestroyFeature(poInsertFeature);
CPLFree(panMap);
return OGRERR_NONE;
}
OGRErr OGRGNMWrappedResultLayer::ISetFeature(OGRFeature *poFeature)
{
return poLayer->SetFeature(poFeature);
}
OGRErr OGRGNMWrappedResultLayer::ICreateFeature(OGRFeature *poFeature)
{
return poLayer->CreateFeature(poFeature);
}