/src/gdal/ogr/ogrsf_frmts/shape/ogrshapelayer.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: OpenGIS Simple Features Reference Implementation |
4 | | * Purpose: Implements OGRShapeLayer class. |
5 | | * Author: Frank Warmerdam, warmerdam@pobox.com |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 1999, Les Technologies SoftMap Inc. |
9 | | * Copyright (c) 2007-2013, Even Rouault <even dot rouault at spatialys.com> |
10 | | * |
11 | | * SPDX-License-Identifier: MIT |
12 | | ****************************************************************************/ |
13 | | |
14 | | #include "ogrshape.h" |
15 | | |
16 | | #include <cerrno> |
17 | | #include <limits> |
18 | | #include <cmath> |
19 | | #include <cstddef> |
20 | | #include <cstdio> |
21 | | #include <cstdlib> |
22 | | #include <cstring> |
23 | | #include <ctime> |
24 | | #include <algorithm> |
25 | | #include <string> |
26 | | |
27 | | #include "cpl_conv.h" |
28 | | #include "cpl_error.h" |
29 | | #include "cpl_multiproc.h" |
30 | | #include "cpl_port.h" |
31 | | #include "cpl_string.h" |
32 | | #include "cpl_time.h" |
33 | | #include "cpl_vsi.h" |
34 | | #include "ogr_core.h" |
35 | | #include "ogr_feature.h" |
36 | | #include "ogr_geometry.h" |
37 | | #include "ogr_p.h" |
38 | | #include "ogr_spatialref.h" |
39 | | #include "ogr_srs_api.h" |
40 | | #include "ogrlayerpool.h" |
41 | | #include "ograrrowarrayhelper.h" |
42 | | #include "ogrsf_frmts.h" |
43 | | #include "shapefil.h" |
44 | | #include "shp_vsi.h" |
45 | | |
46 | | /************************************************************************/ |
47 | | /* OGRShapeLayer() */ |
48 | | /************************************************************************/ |
49 | | |
50 | | OGRShapeLayer::OGRShapeLayer(OGRShapeDataSource *poDSIn, |
51 | | const char *pszFullNameIn, SHPHandle hSHPIn, |
52 | | DBFHandle hDBFIn, |
53 | | const OGRSpatialReference *poSRSIn, bool bSRSSetIn, |
54 | | const std::string &osPrjFilename, bool bUpdate, |
55 | | OGRwkbGeometryType eReqType, |
56 | | CSLConstList papszCreateOptions) |
57 | 0 | : OGRAbstractProxiedLayer(poDSIn->GetPool()), m_poDS(poDSIn), |
58 | 0 | m_osFullName(pszFullNameIn), m_hSHP(hSHPIn), m_hDBF(hDBFIn), |
59 | 0 | m_bUpdateAccess(bUpdate), m_eRequestedGeomType(eReqType), |
60 | 0 | m_bHSHPWasNonNULL(hSHPIn != nullptr), m_bHDBFWasNonNULL(hDBFIn != nullptr) |
61 | 0 | { |
62 | 0 | if (m_hSHP != nullptr) |
63 | 0 | { |
64 | 0 | m_nTotalShapeCount = m_hSHP->nRecords; |
65 | 0 | if (m_hDBF != nullptr && m_hDBF->nRecords != m_nTotalShapeCount) |
66 | 0 | { |
67 | 0 | CPLDebug("Shape", |
68 | 0 | "Inconsistent record number in .shp (%d) and in .dbf (%d)", |
69 | 0 | m_hSHP->nRecords, m_hDBF->nRecords); |
70 | 0 | } |
71 | 0 | } |
72 | 0 | else if (m_hDBF != nullptr) |
73 | 0 | { |
74 | 0 | m_nTotalShapeCount = m_hDBF->nRecords; |
75 | 0 | } |
76 | 0 | #ifdef DEBUG |
77 | 0 | else |
78 | 0 | { |
79 | 0 | CPLError(CE_Fatal, CPLE_AssertionFailed, |
80 | 0 | "Should not happen: Both m_hSHP and m_hDBF are nullptrs"); |
81 | 0 | } |
82 | 0 | #endif |
83 | |
|
84 | 0 | if (!TouchLayer()) |
85 | 0 | { |
86 | 0 | CPLDebug("Shape", "TouchLayer in shape ctor failed. "); |
87 | 0 | } |
88 | |
|
89 | 0 | if (m_hDBF != nullptr && m_hDBF->pszCodePage != nullptr) |
90 | 0 | { |
91 | 0 | CPLDebug("Shape", "DBF Codepage = %s for %s", m_hDBF->pszCodePage, |
92 | 0 | m_osFullName.c_str()); |
93 | | |
94 | | // Not too sure about this, but it seems like better than nothing. |
95 | 0 | m_osEncoding = ConvertCodePage(m_hDBF->pszCodePage); |
96 | 0 | } |
97 | |
|
98 | 0 | if (m_hDBF != nullptr) |
99 | 0 | { |
100 | 0 | if (!(m_hDBF->nUpdateYearSince1900 == 95 && m_hDBF->nUpdateMonth == 7 && |
101 | 0 | m_hDBF->nUpdateDay == 26)) |
102 | 0 | { |
103 | 0 | SetMetadataItem("DBF_DATE_LAST_UPDATE", |
104 | 0 | CPLSPrintf("%04d-%02d-%02d", |
105 | 0 | m_hDBF->nUpdateYearSince1900 + 1900, |
106 | 0 | m_hDBF->nUpdateMonth, |
107 | 0 | m_hDBF->nUpdateDay)); |
108 | 0 | } |
109 | 0 | struct tm tm; |
110 | 0 | CPLUnixTimeToYMDHMS(time(nullptr), &tm); |
111 | 0 | DBFSetLastModifiedDate(m_hDBF, tm.tm_year, tm.tm_mon + 1, tm.tm_mday); |
112 | 0 | } |
113 | |
|
114 | 0 | const char *pszShapeEncoding = |
115 | 0 | CSLFetchNameValue(m_poDS->GetOpenOptions(), "ENCODING"); |
116 | 0 | if (pszShapeEncoding == nullptr && m_osEncoding == "") |
117 | 0 | pszShapeEncoding = CSLFetchNameValue(papszCreateOptions, "ENCODING"); |
118 | 0 | if (pszShapeEncoding == nullptr) |
119 | 0 | pszShapeEncoding = CPLGetConfigOption("SHAPE_ENCODING", nullptr); |
120 | 0 | if (pszShapeEncoding != nullptr) |
121 | 0 | m_osEncoding = pszShapeEncoding; |
122 | |
|
123 | 0 | if (m_osEncoding != "") |
124 | 0 | { |
125 | 0 | CPLDebug("Shape", "Treating as encoding '%s'.", m_osEncoding.c_str()); |
126 | |
|
127 | 0 | if (!OGRShapeLayer::TestCapability(OLCStringsAsUTF8)) |
128 | 0 | { |
129 | 0 | CPLDebug("Shape", "Cannot recode from '%s'. Disabling recoding", |
130 | 0 | m_osEncoding.c_str()); |
131 | 0 | m_osEncoding = ""; |
132 | 0 | } |
133 | 0 | } |
134 | 0 | SetMetadataItem("SOURCE_ENCODING", m_osEncoding, "SHAPEFILE"); |
135 | |
|
136 | 0 | m_poFeatureDefn = SHPReadOGRFeatureDefn( |
137 | 0 | CPLGetBasenameSafe(m_osFullName.c_str()).c_str(), m_hSHP, m_hDBF, |
138 | 0 | m_osEncoding, |
139 | 0 | CPLFetchBool(m_poDS->GetOpenOptions(), "ADJUST_TYPE", false)); |
140 | | |
141 | | // To make sure that |
142 | | // GetLayerDefn()->GetGeomFieldDefn(0)->GetSpatialRef() == GetSpatialRef() |
143 | 0 | OGRwkbGeometryType eGeomType = m_poFeatureDefn->GetGeomType(); |
144 | 0 | if (eGeomType != wkbNone) |
145 | 0 | { |
146 | 0 | OGRwkbGeometryType eType = wkbUnknown; |
147 | |
|
148 | 0 | if (m_eRequestedGeomType == wkbNone) |
149 | 0 | { |
150 | 0 | eType = eGeomType; |
151 | |
|
152 | 0 | const char *pszAdjustGeomType = CSLFetchNameValueDef( |
153 | 0 | m_poDS->GetOpenOptions(), "ADJUST_GEOM_TYPE", "FIRST_SHAPE"); |
154 | 0 | const bool bFirstShape = EQUAL(pszAdjustGeomType, "FIRST_SHAPE"); |
155 | 0 | const bool bAllShapes = EQUAL(pszAdjustGeomType, "ALL_SHAPES"); |
156 | 0 | if ((m_hSHP != nullptr) && (m_hSHP->nRecords > 0) && |
157 | 0 | wkbHasM(eType) && (bFirstShape || bAllShapes)) |
158 | 0 | { |
159 | 0 | bool bMIsUsed = false; |
160 | 0 | for (int iShape = 0; iShape < m_hSHP->nRecords; iShape++) |
161 | 0 | { |
162 | 0 | SHPObject *psShape = SHPReadObject(m_hSHP, iShape); |
163 | 0 | if (psShape) |
164 | 0 | { |
165 | 0 | if (psShape->bMeasureIsUsed && psShape->nVertices > 0 && |
166 | 0 | psShape->padfM != nullptr) |
167 | 0 | { |
168 | 0 | for (int i = 0; i < psShape->nVertices; i++) |
169 | 0 | { |
170 | | // Per the spec, if the M value is smaller than |
171 | | // -1e38, it is a nodata value. |
172 | 0 | if (psShape->padfM[i] > -1e38) |
173 | 0 | { |
174 | 0 | bMIsUsed = true; |
175 | 0 | break; |
176 | 0 | } |
177 | 0 | } |
178 | 0 | } |
179 | |
|
180 | 0 | SHPDestroyObject(psShape); |
181 | 0 | } |
182 | 0 | if (bFirstShape || bMIsUsed) |
183 | 0 | break; |
184 | 0 | } |
185 | 0 | if (!bMIsUsed) |
186 | 0 | eType = OGR_GT_SetModifier(eType, wkbHasZ(eType), FALSE); |
187 | 0 | } |
188 | 0 | } |
189 | 0 | else |
190 | 0 | { |
191 | 0 | eType = m_eRequestedGeomType; |
192 | 0 | } |
193 | |
|
194 | 0 | OGRSpatialReference *poSRSClone = poSRSIn ? poSRSIn->Clone() : nullptr; |
195 | 0 | if (poSRSClone) |
196 | 0 | { |
197 | 0 | poSRSClone->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER); |
198 | 0 | } |
199 | 0 | auto poGeomFieldDefn = std::make_unique<OGRShapeGeomFieldDefn>( |
200 | 0 | m_osFullName.c_str(), eType, bSRSSetIn, poSRSClone); |
201 | 0 | if (!osPrjFilename.empty()) |
202 | 0 | poGeomFieldDefn->SetPrjFilename(osPrjFilename); |
203 | 0 | if (poSRSClone) |
204 | 0 | poSRSClone->Release(); |
205 | 0 | m_poFeatureDefn->SetGeomType(wkbNone); |
206 | 0 | m_poFeatureDefn->AddGeomFieldDefn(std::move(poGeomFieldDefn)); |
207 | 0 | } |
208 | |
|
209 | 0 | SetDescription(m_poFeatureDefn->GetName()); |
210 | 0 | m_bRewindOnWrite = CPLTestBool(CPLGetConfigOption( |
211 | 0 | "SHAPE_REWIND_ON_WRITE", |
212 | 0 | m_hSHP != nullptr && m_hSHP->nShapeType != SHPT_MULTIPATCH ? "NO" |
213 | 0 | : "YES")); |
214 | |
|
215 | 0 | m_poFeatureDefn->Seal(/* bSealFields = */ true); |
216 | 0 | } |
217 | | |
218 | | /************************************************************************/ |
219 | | /* ~OGRShapeLayer() */ |
220 | | /************************************************************************/ |
221 | | |
222 | | OGRShapeLayer::~OGRShapeLayer() |
223 | | |
224 | 0 | { |
225 | 0 | if (m_eNeedRepack == YES && m_bAutoRepack) |
226 | 0 | Repack(); |
227 | |
|
228 | 0 | if (m_bResizeAtClose && m_hDBF != nullptr) |
229 | 0 | { |
230 | 0 | ResizeDBF(); |
231 | 0 | } |
232 | 0 | if (m_bCreateSpatialIndexAtClose && m_hSHP != nullptr) |
233 | 0 | { |
234 | 0 | CreateSpatialIndex(0); |
235 | 0 | } |
236 | |
|
237 | 0 | if (m_nFeaturesRead > 0 && m_poFeatureDefn != nullptr) |
238 | 0 | { |
239 | 0 | CPLDebug("Shape", "%d features read on layer '%s'.", |
240 | 0 | static_cast<int>(m_nFeaturesRead), m_poFeatureDefn->GetName()); |
241 | 0 | } |
242 | |
|
243 | 0 | ClearMatchingFIDs(); |
244 | 0 | ClearSpatialFIDs(); |
245 | |
|
246 | 0 | if (m_poFeatureDefn != nullptr) |
247 | 0 | m_poFeatureDefn->Release(); |
248 | |
|
249 | 0 | if (m_hDBF != nullptr) |
250 | 0 | DBFClose(m_hDBF); |
251 | |
|
252 | 0 | if (m_hSHP != nullptr) |
253 | 0 | SHPClose(m_hSHP); |
254 | |
|
255 | 0 | if (m_hQIX != nullptr) |
256 | 0 | SHPCloseDiskTree(m_hQIX); |
257 | |
|
258 | 0 | if (m_hSBN != nullptr) |
259 | 0 | SBNCloseDiskTree(m_hSBN); |
260 | 0 | } |
261 | | |
262 | | /************************************************************************/ |
263 | | /* SetModificationDate() */ |
264 | | /************************************************************************/ |
265 | | |
266 | | void OGRShapeLayer::SetModificationDate(const char *pszStr) |
267 | 0 | { |
268 | 0 | if (m_hDBF && pszStr) |
269 | 0 | { |
270 | 0 | int year = 0; |
271 | 0 | int month = 0; |
272 | 0 | int day = 0; |
273 | 0 | if ((sscanf(pszStr, "%04d-%02d-%02d", &year, &month, &day) == 3 || |
274 | 0 | sscanf(pszStr, "%04d/%02d/%02d", &year, &month, &day) == 3) && |
275 | 0 | (year >= 1900 && year <= 1900 + 255 && month >= 1 && month <= 12 && |
276 | 0 | day >= 1 && day <= 31)) |
277 | 0 | { |
278 | 0 | DBFSetLastModifiedDate(m_hDBF, year - 1900, month, day); |
279 | 0 | } |
280 | 0 | } |
281 | 0 | } |
282 | | |
283 | | /************************************************************************/ |
284 | | /* SetWriteDBFEOFChar() */ |
285 | | /************************************************************************/ |
286 | | |
287 | | void OGRShapeLayer::SetWriteDBFEOFChar(bool b) |
288 | 0 | { |
289 | 0 | if (m_hDBF) |
290 | 0 | { |
291 | 0 | DBFSetWriteEndOfFileChar(m_hDBF, b); |
292 | 0 | } |
293 | 0 | } |
294 | | |
295 | | /************************************************************************/ |
296 | | /* ConvertCodePage() */ |
297 | | /************************************************************************/ |
298 | | |
299 | | static CPLString GetEncodingFromLDIDNumber(int nLDID) |
300 | 0 | { |
301 | 0 | int nCP = -1; // Windows code page. |
302 | | |
303 | | // http://www.autopark.ru/ASBProgrammerGuide/DBFSTRUC.HTM |
304 | 0 | switch (nLDID) |
305 | 0 | { |
306 | 0 | case 1: |
307 | 0 | nCP = 437; |
308 | 0 | break; |
309 | 0 | case 2: |
310 | 0 | nCP = 850; |
311 | 0 | break; |
312 | 0 | case 3: |
313 | 0 | nCP = 1252; |
314 | 0 | break; |
315 | 0 | case 4: |
316 | 0 | nCP = 10000; |
317 | 0 | break; |
318 | 0 | case 8: |
319 | 0 | nCP = 865; |
320 | 0 | break; |
321 | 0 | case 10: |
322 | 0 | nCP = 850; |
323 | 0 | break; |
324 | 0 | case 11: |
325 | 0 | nCP = 437; |
326 | 0 | break; |
327 | 0 | case 13: |
328 | 0 | nCP = 437; |
329 | 0 | break; |
330 | 0 | case 14: |
331 | 0 | nCP = 850; |
332 | 0 | break; |
333 | 0 | case 15: |
334 | 0 | nCP = 437; |
335 | 0 | break; |
336 | 0 | case 16: |
337 | 0 | nCP = 850; |
338 | 0 | break; |
339 | 0 | case 17: |
340 | 0 | nCP = 437; |
341 | 0 | break; |
342 | 0 | case 18: |
343 | 0 | nCP = 850; |
344 | 0 | break; |
345 | 0 | case 19: |
346 | 0 | nCP = 932; |
347 | 0 | break; |
348 | 0 | case 20: |
349 | 0 | nCP = 850; |
350 | 0 | break; |
351 | 0 | case 21: |
352 | 0 | nCP = 437; |
353 | 0 | break; |
354 | 0 | case 22: |
355 | 0 | nCP = 850; |
356 | 0 | break; |
357 | 0 | case 23: |
358 | 0 | nCP = 865; |
359 | 0 | break; |
360 | 0 | case 24: |
361 | 0 | nCP = 437; |
362 | 0 | break; |
363 | 0 | case 25: |
364 | 0 | nCP = 437; |
365 | 0 | break; |
366 | 0 | case 26: |
367 | 0 | nCP = 850; |
368 | 0 | break; |
369 | 0 | case 27: |
370 | 0 | nCP = 437; |
371 | 0 | break; |
372 | 0 | case 28: |
373 | 0 | nCP = 863; |
374 | 0 | break; |
375 | 0 | case 29: |
376 | 0 | nCP = 850; |
377 | 0 | break; |
378 | 0 | case 31: |
379 | 0 | nCP = 852; |
380 | 0 | break; |
381 | 0 | case 34: |
382 | 0 | nCP = 852; |
383 | 0 | break; |
384 | 0 | case 35: |
385 | 0 | nCP = 852; |
386 | 0 | break; |
387 | 0 | case 36: |
388 | 0 | nCP = 860; |
389 | 0 | break; |
390 | 0 | case 37: |
391 | 0 | nCP = 850; |
392 | 0 | break; |
393 | 0 | case 38: |
394 | 0 | nCP = 866; |
395 | 0 | break; |
396 | 0 | case 55: |
397 | 0 | nCP = 850; |
398 | 0 | break; |
399 | 0 | case 64: |
400 | 0 | nCP = 852; |
401 | 0 | break; |
402 | 0 | case 77: |
403 | 0 | nCP = 936; |
404 | 0 | break; |
405 | 0 | case 78: |
406 | 0 | nCP = 949; |
407 | 0 | break; |
408 | 0 | case 79: |
409 | 0 | nCP = 950; |
410 | 0 | break; |
411 | 0 | case 80: |
412 | 0 | nCP = 874; |
413 | 0 | break; |
414 | 0 | case 87: |
415 | 0 | return CPL_ENC_ISO8859_1; |
416 | 0 | case 88: |
417 | 0 | nCP = 1252; |
418 | 0 | break; |
419 | 0 | case 89: |
420 | 0 | nCP = 1252; |
421 | 0 | break; |
422 | 0 | case 100: |
423 | 0 | nCP = 852; |
424 | 0 | break; |
425 | 0 | case 101: |
426 | 0 | nCP = 866; |
427 | 0 | break; |
428 | 0 | case 102: |
429 | 0 | nCP = 865; |
430 | 0 | break; |
431 | 0 | case 103: |
432 | 0 | nCP = 861; |
433 | 0 | break; |
434 | 0 | case 104: |
435 | 0 | nCP = 895; |
436 | 0 | break; |
437 | 0 | case 105: |
438 | 0 | nCP = 620; |
439 | 0 | break; |
440 | 0 | case 106: |
441 | 0 | nCP = 737; |
442 | 0 | break; |
443 | 0 | case 107: |
444 | 0 | nCP = 857; |
445 | 0 | break; |
446 | 0 | case 108: |
447 | 0 | nCP = 863; |
448 | 0 | break; |
449 | 0 | case 120: |
450 | 0 | nCP = 950; |
451 | 0 | break; |
452 | 0 | case 121: |
453 | 0 | nCP = 949; |
454 | 0 | break; |
455 | 0 | case 122: |
456 | 0 | nCP = 936; |
457 | 0 | break; |
458 | 0 | case 123: |
459 | 0 | nCP = 932; |
460 | 0 | break; |
461 | 0 | case 124: |
462 | 0 | nCP = 874; |
463 | 0 | break; |
464 | 0 | case 134: |
465 | 0 | nCP = 737; |
466 | 0 | break; |
467 | 0 | case 135: |
468 | 0 | nCP = 852; |
469 | 0 | break; |
470 | 0 | case 136: |
471 | 0 | nCP = 857; |
472 | 0 | break; |
473 | 0 | case 150: |
474 | 0 | nCP = 10007; |
475 | 0 | break; |
476 | 0 | case 151: |
477 | 0 | nCP = 10029; |
478 | 0 | break; |
479 | 0 | case 200: |
480 | 0 | nCP = 1250; |
481 | 0 | break; |
482 | 0 | case 201: |
483 | 0 | nCP = 1251; |
484 | 0 | break; |
485 | 0 | case 202: |
486 | 0 | nCP = 1254; |
487 | 0 | break; |
488 | 0 | case 203: |
489 | 0 | nCP = 1253; |
490 | 0 | break; |
491 | 0 | case 204: |
492 | 0 | nCP = 1257; |
493 | 0 | break; |
494 | 0 | default: |
495 | 0 | break; |
496 | 0 | } |
497 | | |
498 | 0 | if (nCP < 0) |
499 | 0 | return CPLString(); |
500 | 0 | return CPLString().Printf("CP%d", nCP); |
501 | 0 | } |
502 | | |
503 | | static CPLString GetEncodingFromCPG(const char *pszCPG) |
504 | 0 | { |
505 | | // see https://support.esri.com/en/technical-article/000013192 |
506 | 0 | CPLString m_osEncodingFromCPG; |
507 | 0 | const int nCPG = atoi(pszCPG); |
508 | 0 | if ((nCPG >= 437 && nCPG <= 950) || (nCPG >= 1250 && nCPG <= 1258)) |
509 | 0 | { |
510 | 0 | m_osEncodingFromCPG.Printf("CP%d", nCPG); |
511 | 0 | } |
512 | 0 | else if (STARTS_WITH_CI(pszCPG, "8859")) |
513 | 0 | { |
514 | 0 | if (pszCPG[4] == '-') |
515 | 0 | m_osEncodingFromCPG.Printf("ISO-8859-%s", pszCPG + 5); |
516 | 0 | else |
517 | 0 | m_osEncodingFromCPG.Printf("ISO-8859-%s", pszCPG + 4); |
518 | 0 | } |
519 | 0 | else if (STARTS_WITH_CI(pszCPG, "UTF-8") || STARTS_WITH_CI(pszCPG, "UTF8")) |
520 | 0 | m_osEncodingFromCPG = CPL_ENC_UTF8; |
521 | 0 | else if (STARTS_WITH_CI(pszCPG, "ANSI 1251")) |
522 | 0 | m_osEncodingFromCPG = "CP1251"; |
523 | 0 | else |
524 | 0 | { |
525 | | // Try just using the CPG value directly. Works for stuff like Big5. |
526 | 0 | m_osEncodingFromCPG = pszCPG; |
527 | 0 | } |
528 | 0 | return m_osEncodingFromCPG; |
529 | 0 | } |
530 | | |
531 | | CPLString OGRShapeLayer::ConvertCodePage(const char *pszCodePage) |
532 | | |
533 | 0 | { |
534 | 0 | CPLString l_m_osEncoding; |
535 | |
|
536 | 0 | if (pszCodePage == nullptr) |
537 | 0 | return l_m_osEncoding; |
538 | | |
539 | 0 | std::string m_osEncodingFromLDID; |
540 | 0 | if (m_hDBF->iLanguageDriver != 0) |
541 | 0 | { |
542 | 0 | SetMetadataItem("LDID_VALUE", CPLSPrintf("%d", m_hDBF->iLanguageDriver), |
543 | 0 | "SHAPEFILE"); |
544 | |
|
545 | 0 | m_osEncodingFromLDID = |
546 | 0 | GetEncodingFromLDIDNumber(m_hDBF->iLanguageDriver); |
547 | 0 | } |
548 | 0 | if (!m_osEncodingFromLDID.empty()) |
549 | 0 | { |
550 | 0 | SetMetadataItem("ENCODING_FROM_LDID", m_osEncodingFromLDID.c_str(), |
551 | 0 | "SHAPEFILE"); |
552 | 0 | } |
553 | |
|
554 | 0 | std::string m_osEncodingFromCPG; |
555 | 0 | if (!STARTS_WITH_CI(pszCodePage, "LDID/")) |
556 | 0 | { |
557 | 0 | SetMetadataItem("CPG_VALUE", pszCodePage, "SHAPEFILE"); |
558 | |
|
559 | 0 | m_osEncodingFromCPG = GetEncodingFromCPG(pszCodePage); |
560 | |
|
561 | 0 | if (!m_osEncodingFromCPG.empty()) |
562 | 0 | SetMetadataItem("ENCODING_FROM_CPG", m_osEncodingFromCPG.c_str(), |
563 | 0 | "SHAPEFILE"); |
564 | |
|
565 | 0 | l_m_osEncoding = std::move(m_osEncodingFromCPG); |
566 | 0 | } |
567 | 0 | else if (!m_osEncodingFromLDID.empty()) |
568 | 0 | { |
569 | 0 | l_m_osEncoding = std::move(m_osEncodingFromLDID); |
570 | 0 | } |
571 | |
|
572 | 0 | return l_m_osEncoding; |
573 | 0 | } |
574 | | |
575 | | /************************************************************************/ |
576 | | /* CheckForQIX() */ |
577 | | /************************************************************************/ |
578 | | |
579 | | bool OGRShapeLayer::CheckForQIX() |
580 | | |
581 | 0 | { |
582 | 0 | if (m_bCheckedForQIX) |
583 | 0 | return m_hQIX != nullptr; |
584 | | |
585 | 0 | const std::string osQIXFilename = |
586 | 0 | CPLResetExtensionSafe(m_osFullName.c_str(), "qix"); |
587 | |
|
588 | 0 | m_hQIX = SHPOpenDiskTree(osQIXFilename.c_str(), nullptr); |
589 | |
|
590 | 0 | m_bCheckedForQIX = true; |
591 | |
|
592 | 0 | return m_hQIX != nullptr; |
593 | 0 | } |
594 | | |
595 | | /************************************************************************/ |
596 | | /* CheckForSBN() */ |
597 | | /************************************************************************/ |
598 | | |
599 | | bool OGRShapeLayer::CheckForSBN() |
600 | | |
601 | 0 | { |
602 | 0 | if (m_bCheckedForSBN) |
603 | 0 | return m_hSBN != nullptr; |
604 | | |
605 | 0 | const std::string osSBNFilename = |
606 | 0 | CPLResetExtensionSafe(m_osFullName.c_str(), "sbn"); |
607 | |
|
608 | 0 | m_hSBN = SBNOpenDiskTree(osSBNFilename.c_str(), nullptr); |
609 | |
|
610 | 0 | m_bCheckedForSBN = true; |
611 | |
|
612 | 0 | return m_hSBN != nullptr; |
613 | 0 | } |
614 | | |
615 | | /************************************************************************/ |
616 | | /* ScanIndices() */ |
617 | | /* */ |
618 | | /* Utilize optional spatial and attribute indices if they are */ |
619 | | /* available. */ |
620 | | /************************************************************************/ |
621 | | |
622 | | bool OGRShapeLayer::ScanIndices() |
623 | | |
624 | 0 | { |
625 | 0 | m_iMatchingFID = 0; |
626 | | |
627 | | /* -------------------------------------------------------------------- */ |
628 | | /* Utilize attribute index if appropriate. */ |
629 | | /* -------------------------------------------------------------------- */ |
630 | 0 | if (m_poAttrQuery != nullptr) |
631 | 0 | { |
632 | 0 | CPLAssert(m_panMatchingFIDs == nullptr); |
633 | | |
634 | 0 | InitializeIndexSupport(m_osFullName.c_str()); |
635 | |
|
636 | 0 | m_panMatchingFIDs = |
637 | 0 | m_poAttrQuery->EvaluateAgainstIndices(this, nullptr); |
638 | 0 | } |
639 | | |
640 | | /* -------------------------------------------------------------------- */ |
641 | | /* Check for spatial index if we have a spatial query. */ |
642 | | /* -------------------------------------------------------------------- */ |
643 | | |
644 | 0 | if (m_poFilterGeom == nullptr || m_hSHP == nullptr) |
645 | 0 | return true; |
646 | | |
647 | 0 | OGREnvelope oSpatialFilterEnvelope; |
648 | 0 | bool bTryQIXorSBN = true; |
649 | |
|
650 | 0 | m_poFilterGeom->getEnvelope(&oSpatialFilterEnvelope); |
651 | |
|
652 | 0 | OGREnvelope oLayerExtent; |
653 | 0 | if (GetExtent(&oLayerExtent, TRUE) == OGRERR_NONE) |
654 | 0 | { |
655 | 0 | if (oSpatialFilterEnvelope.Contains(oLayerExtent)) |
656 | 0 | { |
657 | | // The spatial filter is larger than the layer extent. No use of |
658 | | // .qix file for now. |
659 | 0 | return true; |
660 | 0 | } |
661 | 0 | else if (!oSpatialFilterEnvelope.Intersects(oLayerExtent)) |
662 | 0 | { |
663 | | // No intersection : no need to check for .qix or .sbn. |
664 | 0 | bTryQIXorSBN = false; |
665 | | |
666 | | // Set an empty result for spatial FIDs. |
667 | 0 | free(m_panSpatialFIDs); |
668 | 0 | m_panSpatialFIDs = static_cast<int *>(calloc(1, sizeof(int))); |
669 | 0 | m_nSpatialFIDCount = 0; |
670 | |
|
671 | 0 | delete m_poFilterGeomLastValid; |
672 | 0 | m_poFilterGeomLastValid = m_poFilterGeom->clone(); |
673 | 0 | } |
674 | 0 | } |
675 | | |
676 | 0 | if (bTryQIXorSBN) |
677 | 0 | { |
678 | 0 | if (!m_bCheckedForQIX) |
679 | 0 | CPL_IGNORE_RET_VAL(CheckForQIX()); |
680 | 0 | if (m_hQIX == nullptr && !m_bCheckedForSBN) |
681 | 0 | CPL_IGNORE_RET_VAL(CheckForSBN()); |
682 | 0 | } |
683 | | |
684 | | /* -------------------------------------------------------------------- */ |
685 | | /* Compute spatial index if appropriate. */ |
686 | | /* -------------------------------------------------------------------- */ |
687 | 0 | if (bTryQIXorSBN && (m_hQIX != nullptr || m_hSBN != nullptr) && |
688 | 0 | m_panSpatialFIDs == nullptr) |
689 | 0 | { |
690 | 0 | double adfBoundsMin[4] = {oSpatialFilterEnvelope.MinX, |
691 | 0 | oSpatialFilterEnvelope.MinY, 0.0, 0.0}; |
692 | 0 | double adfBoundsMax[4] = {oSpatialFilterEnvelope.MaxX, |
693 | 0 | oSpatialFilterEnvelope.MaxY, 0.0, 0.0}; |
694 | |
|
695 | 0 | if (m_hQIX != nullptr) |
696 | 0 | m_panSpatialFIDs = SHPSearchDiskTreeEx( |
697 | 0 | m_hQIX, adfBoundsMin, adfBoundsMax, &m_nSpatialFIDCount); |
698 | 0 | else |
699 | 0 | m_panSpatialFIDs = SBNSearchDiskTree( |
700 | 0 | m_hSBN, adfBoundsMin, adfBoundsMax, &m_nSpatialFIDCount); |
701 | |
|
702 | 0 | CPLDebug("SHAPE", "Used spatial index, got %d matches.", |
703 | 0 | m_nSpatialFIDCount); |
704 | |
|
705 | 0 | delete m_poFilterGeomLastValid; |
706 | 0 | m_poFilterGeomLastValid = m_poFilterGeom->clone(); |
707 | 0 | } |
708 | | |
709 | | /* -------------------------------------------------------------------- */ |
710 | | /* Use spatial index if appropriate. */ |
711 | | /* -------------------------------------------------------------------- */ |
712 | 0 | if (m_panSpatialFIDs != nullptr) |
713 | 0 | { |
714 | | // Use resulting list as matching FID list (but reallocate and |
715 | | // terminate with OGRNullFID). |
716 | 0 | if (m_panMatchingFIDs == nullptr) |
717 | 0 | { |
718 | 0 | m_panMatchingFIDs = static_cast<GIntBig *>( |
719 | 0 | CPLMalloc(sizeof(GIntBig) * (m_nSpatialFIDCount + 1))); |
720 | 0 | for (int i = 0; i < m_nSpatialFIDCount; i++) |
721 | 0 | m_panMatchingFIDs[i] = |
722 | 0 | static_cast<GIntBig>(m_panSpatialFIDs[i]); |
723 | 0 | m_panMatchingFIDs[m_nSpatialFIDCount] = OGRNullFID; |
724 | 0 | } |
725 | | // Cull attribute index matches based on those in the spatial index |
726 | | // result set. We assume that the attribute results are in sorted |
727 | | // order. |
728 | 0 | else |
729 | 0 | { |
730 | 0 | int iWrite = 0; |
731 | 0 | int iSpatial = 0; |
732 | |
|
733 | 0 | for (int iRead = 0; m_panMatchingFIDs[iRead] != OGRNullFID; iRead++) |
734 | 0 | { |
735 | 0 | while (iSpatial < m_nSpatialFIDCount && |
736 | 0 | m_panSpatialFIDs[iSpatial] < m_panMatchingFIDs[iRead]) |
737 | 0 | iSpatial++; |
738 | |
|
739 | 0 | if (iSpatial == m_nSpatialFIDCount) |
740 | 0 | continue; |
741 | | |
742 | 0 | if (m_panSpatialFIDs[iSpatial] == m_panMatchingFIDs[iRead]) |
743 | 0 | m_panMatchingFIDs[iWrite++] = m_panMatchingFIDs[iRead]; |
744 | 0 | } |
745 | 0 | m_panMatchingFIDs[iWrite] = OGRNullFID; |
746 | 0 | } |
747 | |
|
748 | 0 | if (m_nSpatialFIDCount > 100000) |
749 | 0 | { |
750 | 0 | ClearSpatialFIDs(); |
751 | 0 | } |
752 | 0 | } |
753 | |
|
754 | 0 | return true; |
755 | 0 | } |
756 | | |
757 | | /************************************************************************/ |
758 | | /* ResetReading() */ |
759 | | /************************************************************************/ |
760 | | |
761 | | void OGRShapeLayer::ResetReading() |
762 | | |
763 | 0 | { |
764 | 0 | if (!TouchLayer()) |
765 | 0 | return; |
766 | | |
767 | 0 | m_iMatchingFID = 0; |
768 | |
|
769 | 0 | m_iNextShapeId = 0; |
770 | |
|
771 | 0 | if (m_bHeaderDirty && m_bUpdateAccess) |
772 | 0 | SyncToDisk(); |
773 | |
|
774 | 0 | if (m_hDBF) |
775 | 0 | VSIFClearErrL(VSI_SHP_GetVSIL(m_hDBF->fp)); |
776 | 0 | } |
777 | | |
778 | | /************************************************************************/ |
779 | | /* ClearMatchingFIDs() */ |
780 | | /************************************************************************/ |
781 | | |
782 | | void OGRShapeLayer::ClearMatchingFIDs() |
783 | 0 | { |
784 | | /* -------------------------------------------------------------------- */ |
785 | | /* Clear previous index search result, if any. */ |
786 | | /* -------------------------------------------------------------------- */ |
787 | 0 | CPLFree(m_panMatchingFIDs); |
788 | 0 | m_panMatchingFIDs = nullptr; |
789 | 0 | } |
790 | | |
791 | | /************************************************************************/ |
792 | | /* ClearSpatialFIDs() */ |
793 | | /************************************************************************/ |
794 | | |
795 | | void OGRShapeLayer::ClearSpatialFIDs() |
796 | 0 | { |
797 | 0 | if (m_panSpatialFIDs != nullptr) |
798 | 0 | { |
799 | 0 | CPLDebug("SHAPE", "Clear m_panSpatialFIDs"); |
800 | 0 | free(m_panSpatialFIDs); |
801 | 0 | } |
802 | 0 | m_panSpatialFIDs = nullptr; |
803 | 0 | m_nSpatialFIDCount = 0; |
804 | |
|
805 | 0 | delete m_poFilterGeomLastValid; |
806 | 0 | m_poFilterGeomLastValid = nullptr; |
807 | 0 | } |
808 | | |
809 | | /************************************************************************/ |
810 | | /* ISetSpatialFilter() */ |
811 | | /************************************************************************/ |
812 | | |
813 | | OGRErr OGRShapeLayer::ISetSpatialFilter(int iGeomField, |
814 | | const OGRGeometry *poGeomIn) |
815 | 0 | { |
816 | 0 | ClearMatchingFIDs(); |
817 | |
|
818 | 0 | if (poGeomIn == nullptr) |
819 | 0 | { |
820 | | // Do nothing. |
821 | 0 | } |
822 | 0 | else if (m_poFilterGeomLastValid != nullptr && |
823 | 0 | m_poFilterGeomLastValid->Equals(poGeomIn)) |
824 | 0 | { |
825 | | // Do nothing. |
826 | 0 | } |
827 | 0 | else if (m_panSpatialFIDs != nullptr) |
828 | 0 | { |
829 | | // We clear the spatialFIDs only if we have a new non-NULL spatial |
830 | | // filter, otherwise we keep the previous result cached. This can be |
831 | | // useful when several SQL layers rely on the same table layer, and use |
832 | | // the same spatial filters. But as there is in the destructor of |
833 | | // OGRGenSQLResultsLayer a clearing of the spatial filter of the table |
834 | | // layer, we need this trick. |
835 | 0 | ClearSpatialFIDs(); |
836 | 0 | } |
837 | |
|
838 | 0 | return OGRLayer::ISetSpatialFilter(iGeomField, poGeomIn); |
839 | 0 | } |
840 | | |
841 | | /************************************************************************/ |
842 | | /* SetAttributeFilter() */ |
843 | | /************************************************************************/ |
844 | | |
845 | | OGRErr OGRShapeLayer::SetAttributeFilter(const char *pszAttributeFilter) |
846 | 0 | { |
847 | 0 | ClearMatchingFIDs(); |
848 | |
|
849 | 0 | return OGRLayer::SetAttributeFilter(pszAttributeFilter); |
850 | 0 | } |
851 | | |
852 | | /************************************************************************/ |
853 | | /* SetNextByIndex() */ |
854 | | /* */ |
855 | | /* If we already have an FID list, we can easily reposition */ |
856 | | /* ourselves in it. */ |
857 | | /************************************************************************/ |
858 | | |
859 | | OGRErr OGRShapeLayer::SetNextByIndex(GIntBig nIndex) |
860 | | |
861 | 0 | { |
862 | 0 | if (!TouchLayer()) |
863 | 0 | return OGRERR_FAILURE; |
864 | | |
865 | 0 | if (nIndex < 0 || nIndex > INT_MAX) |
866 | 0 | return OGRERR_FAILURE; |
867 | | |
868 | | // Eventually we should try to use m_panMatchingFIDs list |
869 | | // if available and appropriate. |
870 | 0 | if (m_poFilterGeom != nullptr || m_poAttrQuery != nullptr) |
871 | 0 | return OGRLayer::SetNextByIndex(nIndex); |
872 | | |
873 | 0 | m_iNextShapeId = static_cast<int>(nIndex); |
874 | |
|
875 | 0 | return OGRERR_NONE; |
876 | 0 | } |
877 | | |
878 | | /************************************************************************/ |
879 | | /* FetchShape() */ |
880 | | /* */ |
881 | | /* Take a shape id, a geometry, and a feature, and set the feature */ |
882 | | /* if the shapeid bbox intersects the geometry. */ |
883 | | /************************************************************************/ |
884 | | |
885 | | OGRFeature *OGRShapeLayer::FetchShape(int iShapeId) |
886 | | |
887 | 0 | { |
888 | 0 | OGRFeature *poFeature = nullptr; |
889 | |
|
890 | 0 | if (m_poFilterGeom != nullptr && m_hSHP != nullptr) |
891 | 0 | { |
892 | 0 | SHPObject *psShape = SHPReadObject(m_hSHP, iShapeId); |
893 | | |
894 | | // do not trust degenerate bounds on non-point geometries |
895 | | // or bounds on null shapes. |
896 | 0 | if (psShape == nullptr || |
897 | 0 | (psShape->nSHPType != SHPT_POINT && |
898 | 0 | psShape->nSHPType != SHPT_POINTZ && |
899 | 0 | psShape->nSHPType != SHPT_POINTM && |
900 | 0 | (psShape->dfXMin == psShape->dfXMax || |
901 | 0 | psShape->dfYMin == psShape->dfYMax)) || |
902 | 0 | psShape->nSHPType == SHPT_NULL) |
903 | 0 | { |
904 | 0 | poFeature = SHPReadOGRFeature(m_hSHP, m_hDBF, m_poFeatureDefn, |
905 | 0 | iShapeId, psShape, m_osEncoding, |
906 | 0 | m_bHasWarnedWrongWindingOrder); |
907 | 0 | } |
908 | 0 | else if (m_sFilterEnvelope.MaxX < psShape->dfXMin || |
909 | 0 | m_sFilterEnvelope.MaxY < psShape->dfYMin || |
910 | 0 | psShape->dfXMax < m_sFilterEnvelope.MinX || |
911 | 0 | psShape->dfYMax < m_sFilterEnvelope.MinY) |
912 | 0 | { |
913 | 0 | SHPDestroyObject(psShape); |
914 | 0 | poFeature = nullptr; |
915 | 0 | } |
916 | 0 | else |
917 | 0 | { |
918 | 0 | poFeature = SHPReadOGRFeature(m_hSHP, m_hDBF, m_poFeatureDefn, |
919 | 0 | iShapeId, psShape, m_osEncoding, |
920 | 0 | m_bHasWarnedWrongWindingOrder); |
921 | 0 | } |
922 | 0 | } |
923 | 0 | else |
924 | 0 | { |
925 | 0 | poFeature = SHPReadOGRFeature(m_hSHP, m_hDBF, m_poFeatureDefn, iShapeId, |
926 | 0 | nullptr, m_osEncoding, |
927 | 0 | m_bHasWarnedWrongWindingOrder); |
928 | 0 | } |
929 | |
|
930 | 0 | return poFeature; |
931 | 0 | } |
932 | | |
933 | | /************************************************************************/ |
934 | | /* GetNextFeature() */ |
935 | | /************************************************************************/ |
936 | | |
937 | | OGRFeature *OGRShapeLayer::GetNextFeature() |
938 | | |
939 | 0 | { |
940 | 0 | if (!TouchLayer()) |
941 | 0 | return nullptr; |
942 | | |
943 | | /* -------------------------------------------------------------------- */ |
944 | | /* Collect a matching list if we have attribute or spatial */ |
945 | | /* indices. Only do this on the first request for a given pass */ |
946 | | /* of course. */ |
947 | | /* -------------------------------------------------------------------- */ |
948 | 0 | if ((m_poAttrQuery != nullptr || m_poFilterGeom != nullptr) && |
949 | 0 | m_iNextShapeId == 0 && m_panMatchingFIDs == nullptr) |
950 | 0 | { |
951 | 0 | ScanIndices(); |
952 | 0 | } |
953 | | |
954 | | /* -------------------------------------------------------------------- */ |
955 | | /* Loop till we find a feature matching our criteria. */ |
956 | | /* -------------------------------------------------------------------- */ |
957 | 0 | OGRFeature *poFeature = nullptr; |
958 | |
|
959 | 0 | while (true) |
960 | 0 | { |
961 | 0 | if (m_panMatchingFIDs != nullptr) |
962 | 0 | { |
963 | 0 | if (m_panMatchingFIDs[m_iMatchingFID] == OGRNullFID) |
964 | 0 | { |
965 | 0 | return nullptr; |
966 | 0 | } |
967 | | |
968 | | // Check the shape object's geometry, and if it matches |
969 | | // any spatial filter, return it. |
970 | 0 | poFeature = |
971 | 0 | FetchShape(static_cast<int>(m_panMatchingFIDs[m_iMatchingFID])); |
972 | |
|
973 | 0 | m_iMatchingFID++; |
974 | 0 | } |
975 | 0 | else |
976 | 0 | { |
977 | 0 | if (m_iNextShapeId >= m_nTotalShapeCount) |
978 | 0 | { |
979 | 0 | return nullptr; |
980 | 0 | } |
981 | | |
982 | 0 | if (m_hDBF) |
983 | 0 | { |
984 | 0 | if (DBFIsRecordDeleted(m_hDBF, m_iNextShapeId)) |
985 | 0 | poFeature = nullptr; |
986 | 0 | else if (VSIFEofL(VSI_SHP_GetVSIL(m_hDBF->fp)) || |
987 | 0 | VSIFErrorL(VSI_SHP_GetVSIL(m_hDBF->fp))) |
988 | 0 | return nullptr; //* I/O error. |
989 | 0 | else |
990 | 0 | poFeature = FetchShape(m_iNextShapeId); |
991 | 0 | } |
992 | 0 | else |
993 | 0 | poFeature = FetchShape(m_iNextShapeId); |
994 | | |
995 | 0 | m_iNextShapeId++; |
996 | 0 | } |
997 | | |
998 | 0 | if (poFeature != nullptr) |
999 | 0 | { |
1000 | 0 | OGRGeometry *poGeom = poFeature->GetGeometryRef(); |
1001 | 0 | if (poGeom != nullptr) |
1002 | 0 | { |
1003 | 0 | poGeom->assignSpatialReference(GetSpatialRef()); |
1004 | 0 | } |
1005 | |
|
1006 | 0 | m_nFeaturesRead++; |
1007 | |
|
1008 | 0 | if ((m_poFilterGeom == nullptr || FilterGeometry(poGeom)) && |
1009 | 0 | (m_poAttrQuery == nullptr || |
1010 | 0 | m_poAttrQuery->Evaluate(poFeature))) |
1011 | 0 | { |
1012 | 0 | return poFeature; |
1013 | 0 | } |
1014 | | |
1015 | 0 | delete poFeature; |
1016 | 0 | } |
1017 | 0 | } |
1018 | 0 | } |
1019 | | |
1020 | | /************************************************************************/ |
1021 | | /* GetFeature() */ |
1022 | | /************************************************************************/ |
1023 | | |
1024 | | OGRFeature *OGRShapeLayer::GetFeature(GIntBig nFeatureId) |
1025 | | |
1026 | 0 | { |
1027 | 0 | if (!TouchLayer() || nFeatureId > INT_MAX) |
1028 | 0 | return nullptr; |
1029 | | |
1030 | 0 | OGRFeature *poFeature = SHPReadOGRFeature( |
1031 | 0 | m_hSHP, m_hDBF, m_poFeatureDefn, static_cast<int>(nFeatureId), nullptr, |
1032 | 0 | m_osEncoding, m_bHasWarnedWrongWindingOrder); |
1033 | |
|
1034 | 0 | if (poFeature == nullptr) |
1035 | 0 | { |
1036 | | // Reading shape feature failed. |
1037 | 0 | return nullptr; |
1038 | 0 | } |
1039 | | |
1040 | 0 | if (poFeature->GetGeometryRef() != nullptr) |
1041 | 0 | { |
1042 | 0 | poFeature->GetGeometryRef()->assignSpatialReference(GetSpatialRef()); |
1043 | 0 | } |
1044 | |
|
1045 | 0 | m_nFeaturesRead++; |
1046 | |
|
1047 | 0 | return poFeature; |
1048 | 0 | } |
1049 | | |
1050 | | /************************************************************************/ |
1051 | | /* StartUpdate() */ |
1052 | | /************************************************************************/ |
1053 | | |
1054 | | bool OGRShapeLayer::StartUpdate(const char *pszOperation) |
1055 | 0 | { |
1056 | 0 | if (!m_poDS->UncompressIfNeeded()) |
1057 | 0 | return false; |
1058 | | |
1059 | 0 | if (!TouchLayer()) |
1060 | 0 | return false; |
1061 | | |
1062 | 0 | if (!m_bUpdateAccess) |
1063 | 0 | { |
1064 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
1065 | 0 | "%s : unsupported operation on a read-only datasource.", |
1066 | 0 | pszOperation); |
1067 | 0 | return false; |
1068 | 0 | } |
1069 | | |
1070 | 0 | return true; |
1071 | 0 | } |
1072 | | |
1073 | | /************************************************************************/ |
1074 | | /* ISetFeature() */ |
1075 | | /************************************************************************/ |
1076 | | |
1077 | | OGRErr OGRShapeLayer::ISetFeature(OGRFeature *poFeature) |
1078 | | |
1079 | 0 | { |
1080 | 0 | if (!StartUpdate("SetFeature")) |
1081 | 0 | return OGRERR_FAILURE; |
1082 | | |
1083 | 0 | GIntBig nFID = poFeature->GetFID(); |
1084 | 0 | if (nFID < 0 || (m_hSHP != nullptr && nFID >= m_hSHP->nRecords) || |
1085 | 0 | (m_hDBF != nullptr && nFID >= m_hDBF->nRecords)) |
1086 | 0 | { |
1087 | 0 | return OGRERR_NON_EXISTING_FEATURE; |
1088 | 0 | } |
1089 | | |
1090 | 0 | m_bHeaderDirty = true; |
1091 | 0 | if (CheckForQIX() || CheckForSBN()) |
1092 | 0 | DropSpatialIndex(); |
1093 | |
|
1094 | 0 | unsigned int nOffset = 0; |
1095 | 0 | unsigned int nSize = 0; |
1096 | 0 | bool bIsLastRecord = false; |
1097 | 0 | if (m_hSHP != nullptr) |
1098 | 0 | { |
1099 | 0 | nOffset = m_hSHP->panRecOffset[nFID]; |
1100 | 0 | nSize = m_hSHP->panRecSize[nFID]; |
1101 | 0 | bIsLastRecord = (nOffset + nSize + 8 == m_hSHP->nFileSize); |
1102 | 0 | } |
1103 | |
|
1104 | 0 | OGRErr eErr = SHPWriteOGRFeature(m_hSHP, m_hDBF, m_poFeatureDefn, poFeature, |
1105 | 0 | m_osEncoding, &m_bTruncationWarningEmitted, |
1106 | 0 | m_bRewindOnWrite); |
1107 | |
|
1108 | 0 | if (m_hSHP != nullptr) |
1109 | 0 | { |
1110 | 0 | if (bIsLastRecord) |
1111 | 0 | { |
1112 | | // Optimization: we don't need repacking if this is the last |
1113 | | // record of the file. Just potential truncation |
1114 | 0 | CPLAssert(nOffset == m_hSHP->panRecOffset[nFID]); |
1115 | 0 | CPLAssert(m_hSHP->panRecOffset[nFID] + m_hSHP->panRecSize[nFID] + |
1116 | 0 | 8 == |
1117 | 0 | m_hSHP->nFileSize); |
1118 | 0 | if (m_hSHP->panRecSize[nFID] < nSize) |
1119 | 0 | { |
1120 | 0 | VSIFTruncateL(VSI_SHP_GetVSIL(m_hSHP->fpSHP), |
1121 | 0 | m_hSHP->nFileSize); |
1122 | 0 | } |
1123 | 0 | } |
1124 | 0 | else if (nOffset != m_hSHP->panRecOffset[nFID] || |
1125 | 0 | nSize != m_hSHP->panRecSize[nFID]) |
1126 | 0 | { |
1127 | 0 | m_bSHPNeedsRepack = true; |
1128 | 0 | m_eNeedRepack = YES; |
1129 | 0 | } |
1130 | 0 | } |
1131 | | |
1132 | 0 | return eErr; |
1133 | 0 | } |
1134 | | |
1135 | | /************************************************************************/ |
1136 | | /* DeleteFeature() */ |
1137 | | /************************************************************************/ |
1138 | | |
1139 | | OGRErr OGRShapeLayer::DeleteFeature(GIntBig nFID) |
1140 | | |
1141 | 0 | { |
1142 | 0 | if (!StartUpdate("DeleteFeature")) |
1143 | 0 | return OGRERR_FAILURE; |
1144 | | |
1145 | 0 | if (nFID < 0 || (m_hSHP != nullptr && nFID >= m_hSHP->nRecords) || |
1146 | 0 | (m_hDBF != nullptr && nFID >= m_hDBF->nRecords)) |
1147 | 0 | { |
1148 | 0 | return OGRERR_NON_EXISTING_FEATURE; |
1149 | 0 | } |
1150 | | |
1151 | 0 | if (!m_hDBF) |
1152 | 0 | { |
1153 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1154 | 0 | "Attempt to delete shape in shapefile with no .dbf file. " |
1155 | 0 | "Deletion is done by marking record deleted in dbf " |
1156 | 0 | "and is not supported without a .dbf file."); |
1157 | 0 | return OGRERR_FAILURE; |
1158 | 0 | } |
1159 | | |
1160 | 0 | if (DBFIsRecordDeleted(m_hDBF, static_cast<int>(nFID))) |
1161 | 0 | { |
1162 | 0 | return OGRERR_NON_EXISTING_FEATURE; |
1163 | 0 | } |
1164 | | |
1165 | 0 | if (!DBFMarkRecordDeleted(m_hDBF, static_cast<int>(nFID), TRUE)) |
1166 | 0 | return OGRERR_FAILURE; |
1167 | | |
1168 | 0 | m_bHeaderDirty = true; |
1169 | 0 | if (CheckForQIX() || CheckForSBN()) |
1170 | 0 | DropSpatialIndex(); |
1171 | 0 | m_eNeedRepack = YES; |
1172 | |
|
1173 | 0 | return OGRERR_NONE; |
1174 | 0 | } |
1175 | | |
1176 | | /************************************************************************/ |
1177 | | /* ICreateFeature() */ |
1178 | | /************************************************************************/ |
1179 | | |
1180 | | OGRErr OGRShapeLayer::ICreateFeature(OGRFeature *poFeature) |
1181 | | |
1182 | 0 | { |
1183 | 0 | if (!StartUpdate("CreateFeature")) |
1184 | 0 | return OGRERR_FAILURE; |
1185 | | |
1186 | 0 | if (m_hDBF != nullptr && |
1187 | 0 | !VSI_SHP_WriteMoreDataOK(m_hDBF->fp, m_hDBF->nRecordLength)) |
1188 | 0 | { |
1189 | 0 | return OGRERR_FAILURE; |
1190 | 0 | } |
1191 | | |
1192 | 0 | m_bHeaderDirty = true; |
1193 | 0 | if (CheckForQIX() || CheckForSBN()) |
1194 | 0 | DropSpatialIndex(); |
1195 | |
|
1196 | 0 | poFeature->SetFID(OGRNullFID); |
1197 | |
|
1198 | 0 | if (m_nTotalShapeCount == 0 && |
1199 | 0 | wkbFlatten(m_eRequestedGeomType) == wkbUnknown && m_hSHP != nullptr && |
1200 | 0 | m_hSHP->nShapeType != SHPT_MULTIPATCH && |
1201 | 0 | poFeature->GetGeometryRef() != nullptr) |
1202 | 0 | { |
1203 | 0 | OGRGeometry *poGeom = poFeature->GetGeometryRef(); |
1204 | 0 | int nShapeType = -1; |
1205 | |
|
1206 | 0 | switch (poGeom->getGeometryType()) |
1207 | 0 | { |
1208 | 0 | case wkbPoint: |
1209 | 0 | nShapeType = SHPT_POINT; |
1210 | 0 | m_eRequestedGeomType = wkbPoint; |
1211 | 0 | break; |
1212 | | |
1213 | 0 | case wkbPoint25D: |
1214 | 0 | nShapeType = SHPT_POINTZ; |
1215 | 0 | m_eRequestedGeomType = wkbPoint25D; |
1216 | 0 | break; |
1217 | | |
1218 | 0 | case wkbPointM: |
1219 | 0 | nShapeType = SHPT_POINTM; |
1220 | 0 | m_eRequestedGeomType = wkbPointM; |
1221 | 0 | break; |
1222 | | |
1223 | 0 | case wkbPointZM: |
1224 | 0 | nShapeType = SHPT_POINTZ; |
1225 | 0 | m_eRequestedGeomType = wkbPointZM; |
1226 | 0 | break; |
1227 | | |
1228 | 0 | case wkbMultiPoint: |
1229 | 0 | nShapeType = SHPT_MULTIPOINT; |
1230 | 0 | m_eRequestedGeomType = wkbMultiPoint; |
1231 | 0 | break; |
1232 | | |
1233 | 0 | case wkbMultiPoint25D: |
1234 | 0 | nShapeType = SHPT_MULTIPOINTZ; |
1235 | 0 | m_eRequestedGeomType = wkbMultiPoint25D; |
1236 | 0 | break; |
1237 | | |
1238 | 0 | case wkbMultiPointM: |
1239 | 0 | nShapeType = SHPT_MULTIPOINTM; |
1240 | 0 | m_eRequestedGeomType = wkbMultiPointM; |
1241 | 0 | break; |
1242 | | |
1243 | 0 | case wkbMultiPointZM: |
1244 | 0 | nShapeType = SHPT_MULTIPOINTZ; |
1245 | 0 | m_eRequestedGeomType = wkbMultiPointM; |
1246 | 0 | break; |
1247 | | |
1248 | 0 | case wkbLineString: |
1249 | 0 | case wkbMultiLineString: |
1250 | 0 | nShapeType = SHPT_ARC; |
1251 | 0 | m_eRequestedGeomType = wkbLineString; |
1252 | 0 | break; |
1253 | | |
1254 | 0 | case wkbLineString25D: |
1255 | 0 | case wkbMultiLineString25D: |
1256 | 0 | nShapeType = SHPT_ARCZ; |
1257 | 0 | m_eRequestedGeomType = wkbLineString25D; |
1258 | 0 | break; |
1259 | | |
1260 | 0 | case wkbLineStringM: |
1261 | 0 | case wkbMultiLineStringM: |
1262 | 0 | nShapeType = SHPT_ARCM; |
1263 | 0 | m_eRequestedGeomType = wkbLineStringM; |
1264 | 0 | break; |
1265 | | |
1266 | 0 | case wkbLineStringZM: |
1267 | 0 | case wkbMultiLineStringZM: |
1268 | 0 | nShapeType = SHPT_ARCZ; |
1269 | 0 | m_eRequestedGeomType = wkbLineStringZM; |
1270 | 0 | break; |
1271 | | |
1272 | 0 | case wkbPolygon: |
1273 | 0 | case wkbMultiPolygon: |
1274 | 0 | case wkbTriangle: |
1275 | 0 | nShapeType = SHPT_POLYGON; |
1276 | 0 | m_eRequestedGeomType = wkbPolygon; |
1277 | 0 | break; |
1278 | | |
1279 | 0 | case wkbPolygon25D: |
1280 | 0 | case wkbMultiPolygon25D: |
1281 | 0 | case wkbTriangleZ: |
1282 | 0 | nShapeType = SHPT_POLYGONZ; |
1283 | 0 | m_eRequestedGeomType = wkbPolygon25D; |
1284 | 0 | break; |
1285 | | |
1286 | 0 | case wkbPolygonM: |
1287 | 0 | case wkbMultiPolygonM: |
1288 | 0 | case wkbTriangleM: |
1289 | 0 | nShapeType = SHPT_POLYGONM; |
1290 | 0 | m_eRequestedGeomType = wkbPolygonM; |
1291 | 0 | break; |
1292 | | |
1293 | 0 | case wkbPolygonZM: |
1294 | 0 | case wkbMultiPolygonZM: |
1295 | 0 | case wkbTriangleZM: |
1296 | 0 | nShapeType = SHPT_POLYGONZ; |
1297 | 0 | m_eRequestedGeomType = wkbPolygonZM; |
1298 | 0 | break; |
1299 | | |
1300 | 0 | default: |
1301 | 0 | nShapeType = -1; |
1302 | 0 | break; |
1303 | 0 | } |
1304 | | |
1305 | 0 | if (wkbFlatten(poGeom->getGeometryType()) == wkbTIN || |
1306 | 0 | wkbFlatten(poGeom->getGeometryType()) == wkbPolyhedralSurface) |
1307 | 0 | { |
1308 | 0 | nShapeType = SHPT_MULTIPATCH; |
1309 | 0 | m_eRequestedGeomType = wkbUnknown; |
1310 | 0 | } |
1311 | |
|
1312 | 0 | if (wkbFlatten(poGeom->getGeometryType()) == wkbGeometryCollection) |
1313 | 0 | { |
1314 | 0 | const OGRGeometryCollection *poGC = poGeom->toGeometryCollection(); |
1315 | 0 | bool bIsMultiPatchCompatible = false; |
1316 | 0 | for (int iGeom = 0; iGeom < poGC->getNumGeometries(); iGeom++) |
1317 | 0 | { |
1318 | 0 | OGRwkbGeometryType eSubGeomType = |
1319 | 0 | wkbFlatten(poGC->getGeometryRef(iGeom)->getGeometryType()); |
1320 | 0 | if (eSubGeomType == wkbTIN || |
1321 | 0 | eSubGeomType == wkbPolyhedralSurface) |
1322 | 0 | { |
1323 | 0 | bIsMultiPatchCompatible = true; |
1324 | 0 | } |
1325 | 0 | else if (eSubGeomType != wkbMultiPolygon) |
1326 | 0 | { |
1327 | 0 | bIsMultiPatchCompatible = false; |
1328 | 0 | break; |
1329 | 0 | } |
1330 | 0 | } |
1331 | 0 | if (bIsMultiPatchCompatible) |
1332 | 0 | { |
1333 | 0 | nShapeType = SHPT_MULTIPATCH; |
1334 | 0 | m_eRequestedGeomType = wkbUnknown; |
1335 | 0 | } |
1336 | 0 | } |
1337 | |
|
1338 | 0 | if (nShapeType != -1) |
1339 | 0 | { |
1340 | 0 | whileUnsealing(m_poFeatureDefn)->SetGeomType(m_eRequestedGeomType); |
1341 | 0 | ResetGeomType(nShapeType); |
1342 | 0 | } |
1343 | 0 | } |
1344 | | |
1345 | 0 | const OGRErr eErr = SHPWriteOGRFeature( |
1346 | 0 | m_hSHP, m_hDBF, m_poFeatureDefn, poFeature, m_osEncoding, |
1347 | 0 | &m_bTruncationWarningEmitted, m_bRewindOnWrite); |
1348 | |
|
1349 | 0 | if (m_hSHP != nullptr) |
1350 | 0 | m_nTotalShapeCount = m_hSHP->nRecords; |
1351 | 0 | else if (m_hDBF != nullptr) |
1352 | 0 | m_nTotalShapeCount = m_hDBF->nRecords; |
1353 | 0 | #ifdef DEBUG |
1354 | 0 | else // Silence coverity. |
1355 | 0 | CPLError(CE_Fatal, CPLE_AssertionFailed, |
1356 | 0 | "Should not happen: Both m_hSHP and m_hDBF are nullptrs"); |
1357 | 0 | #endif |
1358 | |
|
1359 | 0 | return eErr; |
1360 | 0 | } |
1361 | | |
1362 | | /************************************************************************/ |
1363 | | /* GetFeatureCountWithSpatialFilterOnly() */ |
1364 | | /* */ |
1365 | | /* Specialized implementation of GetFeatureCount() when there is *only* */ |
1366 | | /* a spatial filter and no attribute filter. */ |
1367 | | /************************************************************************/ |
1368 | | |
1369 | | int OGRShapeLayer::GetFeatureCountWithSpatialFilterOnly() |
1370 | | |
1371 | 0 | { |
1372 | | /* -------------------------------------------------------------------- */ |
1373 | | /* Collect a matching list if we have attribute or spatial */ |
1374 | | /* indices. Only do this on the first request for a given pass */ |
1375 | | /* of course. */ |
1376 | | /* -------------------------------------------------------------------- */ |
1377 | 0 | if (m_panMatchingFIDs == nullptr) |
1378 | 0 | { |
1379 | 0 | ScanIndices(); |
1380 | 0 | } |
1381 | |
|
1382 | 0 | int nFeatureCount = 0; |
1383 | 0 | int iLocalMatchingFID = 0; |
1384 | 0 | int iLocalNextShapeId = 0; |
1385 | 0 | bool bExpectPoints = false; |
1386 | |
|
1387 | 0 | if (wkbFlatten(m_poFeatureDefn->GetGeomType()) == wkbPoint) |
1388 | 0 | bExpectPoints = true; |
1389 | | |
1390 | | /* -------------------------------------------------------------------- */ |
1391 | | /* Loop till we find a feature matching our criteria. */ |
1392 | | /* -------------------------------------------------------------------- */ |
1393 | |
|
1394 | 0 | SHPObject sShape; |
1395 | 0 | memset(&sShape, 0, sizeof(sShape)); |
1396 | |
|
1397 | 0 | while (true) |
1398 | 0 | { |
1399 | 0 | int iShape = -1; |
1400 | |
|
1401 | 0 | if (m_panMatchingFIDs != nullptr) |
1402 | 0 | { |
1403 | 0 | iShape = static_cast<int>(m_panMatchingFIDs[iLocalMatchingFID]); |
1404 | 0 | if (iShape == OGRNullFID) |
1405 | 0 | break; |
1406 | 0 | iLocalMatchingFID++; |
1407 | 0 | } |
1408 | 0 | else |
1409 | 0 | { |
1410 | 0 | if (iLocalNextShapeId >= m_nTotalShapeCount) |
1411 | 0 | break; |
1412 | 0 | iShape = iLocalNextShapeId++; |
1413 | |
|
1414 | 0 | if (m_hDBF) |
1415 | 0 | { |
1416 | 0 | if (DBFIsRecordDeleted(m_hDBF, iShape)) |
1417 | 0 | continue; |
1418 | | |
1419 | 0 | if (VSIFEofL(VSI_SHP_GetVSIL(m_hDBF->fp)) || |
1420 | 0 | VSIFErrorL(VSI_SHP_GetVSIL(m_hDBF->fp))) |
1421 | 0 | break; |
1422 | 0 | } |
1423 | 0 | } |
1424 | | |
1425 | | // Read full shape for point layers. |
1426 | 0 | SHPObject *psShape = nullptr; |
1427 | 0 | if (bExpectPoints || |
1428 | 0 | m_hSHP->panRecOffset[iShape] == 0 /* lazy shx loading case */) |
1429 | 0 | psShape = SHPReadObject(m_hSHP, iShape); |
1430 | | |
1431 | | /* -------------------------------------------------------------------- |
1432 | | */ |
1433 | | /* Only read feature type and bounding box for now. In case of */ |
1434 | | /* inconclusive tests on bounding box only, we will read the full |
1435 | | */ |
1436 | | /* shape later. */ |
1437 | | /* -------------------------------------------------------------------- |
1438 | | */ |
1439 | 0 | else if (iShape >= 0 && iShape < m_hSHP->nRecords && |
1440 | 0 | m_hSHP->panRecSize[iShape] > 4 + 8 * 4) |
1441 | 0 | { |
1442 | 0 | GByte abyBuf[4 + 8 * 4] = {}; |
1443 | 0 | if (m_hSHP->sHooks.FSeek( |
1444 | 0 | m_hSHP->fpSHP, m_hSHP->panRecOffset[iShape] + 8, 0) == 0 && |
1445 | 0 | m_hSHP->sHooks.FRead(abyBuf, sizeof(abyBuf), 1, |
1446 | 0 | m_hSHP->fpSHP) == 1) |
1447 | 0 | { |
1448 | 0 | memcpy(&(sShape.nSHPType), abyBuf, 4); |
1449 | 0 | CPL_LSBPTR32(&(sShape.nSHPType)); |
1450 | 0 | if (sShape.nSHPType != SHPT_NULL && |
1451 | 0 | sShape.nSHPType != SHPT_POINT && |
1452 | 0 | sShape.nSHPType != SHPT_POINTM && |
1453 | 0 | sShape.nSHPType != SHPT_POINTZ) |
1454 | 0 | { |
1455 | 0 | psShape = &sShape; |
1456 | 0 | memcpy(&(sShape.dfXMin), abyBuf + 4, 8); |
1457 | 0 | memcpy(&(sShape.dfYMin), abyBuf + 12, 8); |
1458 | 0 | memcpy(&(sShape.dfXMax), abyBuf + 20, 8); |
1459 | 0 | memcpy(&(sShape.dfYMax), abyBuf + 28, 8); |
1460 | 0 | CPL_LSBPTR64(&(sShape.dfXMin)); |
1461 | 0 | CPL_LSBPTR64(&(sShape.dfYMin)); |
1462 | 0 | CPL_LSBPTR64(&(sShape.dfXMax)); |
1463 | 0 | CPL_LSBPTR64(&(sShape.dfYMax)); |
1464 | 0 | } |
1465 | 0 | } |
1466 | 0 | else |
1467 | 0 | { |
1468 | 0 | break; |
1469 | 0 | } |
1470 | 0 | } |
1471 | | |
1472 | 0 | if (psShape != nullptr && psShape->nSHPType != SHPT_NULL) |
1473 | 0 | { |
1474 | 0 | OGRGeometry *poGeometry = nullptr; |
1475 | 0 | OGREnvelope sGeomEnv; |
1476 | | // Test if we have a degenerated bounding box. |
1477 | 0 | if (psShape->nSHPType != SHPT_POINT && |
1478 | 0 | psShape->nSHPType != SHPT_POINTZ && |
1479 | 0 | psShape->nSHPType != SHPT_POINTM && |
1480 | 0 | (psShape->dfXMin == psShape->dfXMax || |
1481 | 0 | psShape->dfYMin == psShape->dfYMax)) |
1482 | 0 | { |
1483 | | // Need to read the full geometry to compute the envelope. |
1484 | 0 | if (psShape == &sShape) |
1485 | 0 | psShape = SHPReadObject(m_hSHP, iShape); |
1486 | |
|
1487 | 0 | if (psShape) |
1488 | 0 | { |
1489 | 0 | poGeometry = SHPReadOGRObject( |
1490 | 0 | m_hSHP, iShape, psShape, m_bHasWarnedWrongWindingOrder); |
1491 | 0 | if (poGeometry) |
1492 | 0 | poGeometry->getEnvelope(&sGeomEnv); |
1493 | 0 | psShape = nullptr; |
1494 | 0 | } |
1495 | 0 | } |
1496 | 0 | else |
1497 | 0 | { |
1498 | | // Trust the shape bounding box as the shape envelope. |
1499 | 0 | sGeomEnv.MinX = psShape->dfXMin; |
1500 | 0 | sGeomEnv.MinY = psShape->dfYMin; |
1501 | 0 | sGeomEnv.MaxX = psShape->dfXMax; |
1502 | 0 | sGeomEnv.MaxY = psShape->dfYMax; |
1503 | 0 | } |
1504 | | |
1505 | | /* -------------------------------------------------------------------- |
1506 | | */ |
1507 | | /* If there is no */ |
1508 | | /* intersection between the envelopes we are sure not to have |
1509 | | */ |
1510 | | /* any intersection. */ |
1511 | | /* -------------------------------------------------------------------- |
1512 | | */ |
1513 | 0 | if (sGeomEnv.MaxX < m_sFilterEnvelope.MinX || |
1514 | 0 | sGeomEnv.MaxY < m_sFilterEnvelope.MinY || |
1515 | 0 | m_sFilterEnvelope.MaxX < sGeomEnv.MinX || |
1516 | 0 | m_sFilterEnvelope.MaxY < sGeomEnv.MinY) |
1517 | 0 | { |
1518 | 0 | } |
1519 | | /* -------------------------------------------------------------------- |
1520 | | */ |
1521 | | /* If the filter geometry is its own envelope and if the */ |
1522 | | /* envelope of the geometry is inside the filter geometry, */ |
1523 | | /* the geometry itself is inside the filter geometry */ |
1524 | | /* -------------------------------------------------------------------- |
1525 | | */ |
1526 | 0 | else if (m_bFilterIsEnvelope && |
1527 | 0 | sGeomEnv.MinX >= m_sFilterEnvelope.MinX && |
1528 | 0 | sGeomEnv.MinY >= m_sFilterEnvelope.MinY && |
1529 | 0 | sGeomEnv.MaxX <= m_sFilterEnvelope.MaxX && |
1530 | 0 | sGeomEnv.MaxY <= m_sFilterEnvelope.MaxY) |
1531 | 0 | { |
1532 | 0 | nFeatureCount++; |
1533 | 0 | } |
1534 | 0 | else |
1535 | 0 | { |
1536 | | /* -------------------------------------------------------------------- |
1537 | | */ |
1538 | | /* Fallback to full intersect test (using GEOS) if we still |
1539 | | */ |
1540 | | /* don't know for sure. */ |
1541 | | /* -------------------------------------------------------------------- |
1542 | | */ |
1543 | 0 | if (OGRGeometryFactory::haveGEOS()) |
1544 | 0 | { |
1545 | | // Read the full geometry. |
1546 | 0 | if (poGeometry == nullptr) |
1547 | 0 | { |
1548 | 0 | if (psShape == &sShape) |
1549 | 0 | psShape = SHPReadObject(m_hSHP, iShape); |
1550 | 0 | if (psShape) |
1551 | 0 | { |
1552 | 0 | poGeometry = |
1553 | 0 | SHPReadOGRObject(m_hSHP, iShape, psShape, |
1554 | 0 | m_bHasWarnedWrongWindingOrder); |
1555 | 0 | psShape = nullptr; |
1556 | 0 | } |
1557 | 0 | } |
1558 | 0 | if (poGeometry == nullptr) |
1559 | 0 | { |
1560 | 0 | nFeatureCount++; |
1561 | 0 | } |
1562 | 0 | else if (m_pPreparedFilterGeom != nullptr) |
1563 | 0 | { |
1564 | 0 | if (OGRPreparedGeometryIntersects( |
1565 | 0 | m_pPreparedFilterGeom, |
1566 | 0 | OGRGeometry::ToHandle(poGeometry))) |
1567 | 0 | { |
1568 | 0 | nFeatureCount++; |
1569 | 0 | } |
1570 | 0 | } |
1571 | 0 | else if (m_poFilterGeom->Intersects(poGeometry)) |
1572 | 0 | nFeatureCount++; |
1573 | 0 | } |
1574 | 0 | else |
1575 | 0 | { |
1576 | 0 | nFeatureCount++; |
1577 | 0 | } |
1578 | 0 | } |
1579 | |
|
1580 | 0 | delete poGeometry; |
1581 | 0 | } |
1582 | 0 | else |
1583 | 0 | { |
1584 | 0 | nFeatureCount++; |
1585 | 0 | } |
1586 | |
|
1587 | 0 | if (psShape && psShape != &sShape) |
1588 | 0 | SHPDestroyObject(psShape); |
1589 | 0 | } |
1590 | |
|
1591 | 0 | return nFeatureCount; |
1592 | 0 | } |
1593 | | |
1594 | | /************************************************************************/ |
1595 | | /* GetFeatureCount() */ |
1596 | | /************************************************************************/ |
1597 | | |
1598 | | GIntBig OGRShapeLayer::GetFeatureCount(int bForce) |
1599 | | |
1600 | 0 | { |
1601 | | // Check if the spatial filter is non-trivial. |
1602 | 0 | bool bHasTrivialSpatialFilter = false; |
1603 | 0 | if (m_poFilterGeom != nullptr) |
1604 | 0 | { |
1605 | 0 | OGREnvelope oSpatialFilterEnvelope; |
1606 | 0 | m_poFilterGeom->getEnvelope(&oSpatialFilterEnvelope); |
1607 | |
|
1608 | 0 | OGREnvelope oLayerExtent; |
1609 | 0 | if (GetExtent(&oLayerExtent, TRUE) == OGRERR_NONE) |
1610 | 0 | { |
1611 | 0 | if (oSpatialFilterEnvelope.Contains(oLayerExtent)) |
1612 | 0 | { |
1613 | 0 | bHasTrivialSpatialFilter = true; |
1614 | 0 | } |
1615 | 0 | else |
1616 | 0 | { |
1617 | 0 | bHasTrivialSpatialFilter = false; |
1618 | 0 | } |
1619 | 0 | } |
1620 | 0 | else |
1621 | 0 | { |
1622 | 0 | bHasTrivialSpatialFilter = false; |
1623 | 0 | } |
1624 | 0 | } |
1625 | 0 | else |
1626 | 0 | { |
1627 | 0 | bHasTrivialSpatialFilter = true; |
1628 | 0 | } |
1629 | |
|
1630 | 0 | if (bHasTrivialSpatialFilter && m_poAttrQuery == nullptr) |
1631 | 0 | return m_nTotalShapeCount; |
1632 | | |
1633 | 0 | if (!TouchLayer()) |
1634 | 0 | return 0; |
1635 | | |
1636 | | // Spatial filter only. |
1637 | 0 | if (m_poAttrQuery == nullptr && m_hSHP != nullptr) |
1638 | 0 | { |
1639 | 0 | return GetFeatureCountWithSpatialFilterOnly(); |
1640 | 0 | } |
1641 | | |
1642 | | // Attribute filter only. |
1643 | 0 | if (m_poAttrQuery != nullptr && m_poFilterGeom == nullptr) |
1644 | 0 | { |
1645 | | // See if we can ignore reading geometries. |
1646 | 0 | const bool bSaveGeometryIgnored = |
1647 | 0 | CPL_TO_BOOL(m_poFeatureDefn->IsGeometryIgnored()); |
1648 | 0 | if (!AttributeFilterEvaluationNeedsGeometry()) |
1649 | 0 | m_poFeatureDefn->SetGeometryIgnored(TRUE); |
1650 | |
|
1651 | 0 | GIntBig nRet = OGRLayer::GetFeatureCount(bForce); |
1652 | |
|
1653 | 0 | m_poFeatureDefn->SetGeometryIgnored(bSaveGeometryIgnored); |
1654 | 0 | return nRet; |
1655 | 0 | } |
1656 | | |
1657 | 0 | return OGRLayer::GetFeatureCount(bForce); |
1658 | 0 | } |
1659 | | |
1660 | | /************************************************************************/ |
1661 | | /* IGetExtent() */ |
1662 | | /* */ |
1663 | | /* Fetch extent of the data currently stored in the dataset. */ |
1664 | | /* The bForce flag has no effect on SHP files since that value */ |
1665 | | /* is always in the header. */ |
1666 | | /* */ |
1667 | | /* Returns OGRERR_NONE/OGRRERR_FAILURE. */ |
1668 | | /************************************************************************/ |
1669 | | |
1670 | | OGRErr OGRShapeLayer::IGetExtent(int iGeomField, OGREnvelope *psExtent, |
1671 | | bool bForce) |
1672 | | |
1673 | 0 | { |
1674 | 0 | if (!TouchLayer()) |
1675 | 0 | return OGRERR_FAILURE; |
1676 | | |
1677 | 0 | if (m_hSHP == nullptr) |
1678 | 0 | return OGRERR_FAILURE; |
1679 | | |
1680 | 0 | double adMin[4] = {0.0, 0.0, 0.0, 0.0}; |
1681 | 0 | double adMax[4] = {0.0, 0.0, 0.0, 0.0}; |
1682 | |
|
1683 | 0 | SHPGetInfo(m_hSHP, nullptr, nullptr, adMin, adMax); |
1684 | |
|
1685 | 0 | psExtent->MinX = adMin[0]; |
1686 | 0 | psExtent->MinY = adMin[1]; |
1687 | 0 | psExtent->MaxX = adMax[0]; |
1688 | 0 | psExtent->MaxY = adMax[1]; |
1689 | |
|
1690 | 0 | if (std::isnan(adMin[0]) || std::isnan(adMin[1]) || std::isnan(adMax[0]) || |
1691 | 0 | std::isnan(adMax[1])) |
1692 | 0 | { |
1693 | 0 | CPLDebug("SHAPE", "Invalid extent in shape header"); |
1694 | | |
1695 | | // Disable filters to avoid infinite recursion in GetNextFeature() |
1696 | | // that calls ScanIndices() that call GetExtent. |
1697 | 0 | OGRFeatureQuery *poAttrQuery = m_poAttrQuery; |
1698 | 0 | m_poAttrQuery = nullptr; |
1699 | 0 | OGRGeometry *poFilterGeom = m_poFilterGeom; |
1700 | 0 | m_poFilterGeom = nullptr; |
1701 | |
|
1702 | 0 | psExtent->MinX = 0; |
1703 | 0 | psExtent->MinY = 0; |
1704 | 0 | psExtent->MaxX = 0; |
1705 | 0 | psExtent->MaxY = 0; |
1706 | |
|
1707 | 0 | const OGRErr eErr = OGRLayer::IGetExtent(iGeomField, psExtent, bForce); |
1708 | |
|
1709 | 0 | m_poAttrQuery = poAttrQuery; |
1710 | 0 | m_poFilterGeom = poFilterGeom; |
1711 | 0 | return eErr; |
1712 | 0 | } |
1713 | | |
1714 | 0 | return OGRERR_NONE; |
1715 | 0 | } |
1716 | | |
1717 | | OGRErr OGRShapeLayer::IGetExtent3D(int iGeomField, OGREnvelope3D *psExtent3D, |
1718 | | bool bForce) |
1719 | 0 | { |
1720 | 0 | if (m_poFilterGeom || m_poAttrQuery) |
1721 | 0 | return OGRLayer::IGetExtent3D(iGeomField, psExtent3D, bForce); |
1722 | | |
1723 | 0 | if (!TouchLayer()) |
1724 | 0 | return OGRERR_FAILURE; |
1725 | | |
1726 | 0 | if (m_hSHP == nullptr) |
1727 | 0 | return OGRERR_FAILURE; |
1728 | | |
1729 | 0 | double adMin[4] = {0.0, 0.0, 0.0, 0.0}; |
1730 | 0 | double adMax[4] = {0.0, 0.0, 0.0, 0.0}; |
1731 | |
|
1732 | 0 | SHPGetInfo(m_hSHP, nullptr, nullptr, adMin, adMax); |
1733 | |
|
1734 | 0 | psExtent3D->MinX = adMin[0]; |
1735 | 0 | psExtent3D->MinY = adMin[1]; |
1736 | 0 | psExtent3D->MaxX = adMax[0]; |
1737 | 0 | psExtent3D->MaxY = adMax[1]; |
1738 | |
|
1739 | 0 | if (OGR_GT_HasZ(m_poFeatureDefn->GetGeomType())) |
1740 | 0 | { |
1741 | 0 | psExtent3D->MinZ = adMin[2]; |
1742 | 0 | psExtent3D->MaxZ = adMax[2]; |
1743 | 0 | } |
1744 | 0 | else |
1745 | 0 | { |
1746 | 0 | psExtent3D->MinZ = std::numeric_limits<double>::infinity(); |
1747 | 0 | psExtent3D->MaxZ = -std::numeric_limits<double>::infinity(); |
1748 | 0 | } |
1749 | |
|
1750 | 0 | if (std::isnan(adMin[0]) || std::isnan(adMin[1]) || std::isnan(adMax[0]) || |
1751 | 0 | std::isnan(adMax[1])) |
1752 | 0 | { |
1753 | 0 | CPLDebug("SHAPE", "Invalid extent in shape header"); |
1754 | | |
1755 | | // Disable filters to avoid infinite recursion in GetNextFeature() |
1756 | | // that calls ScanIndices() that call GetExtent. |
1757 | 0 | OGRFeatureQuery *poAttrQuery = m_poAttrQuery; |
1758 | 0 | m_poAttrQuery = nullptr; |
1759 | 0 | OGRGeometry *poFilterGeom = m_poFilterGeom; |
1760 | 0 | m_poFilterGeom = nullptr; |
1761 | |
|
1762 | 0 | const OGRErr eErr = |
1763 | 0 | OGRLayer::IGetExtent3D(iGeomField, psExtent3D, bForce); |
1764 | |
|
1765 | 0 | m_poAttrQuery = poAttrQuery; |
1766 | 0 | m_poFilterGeom = poFilterGeom; |
1767 | 0 | return eErr; |
1768 | 0 | } |
1769 | | |
1770 | 0 | return OGRERR_NONE; |
1771 | 0 | } |
1772 | | |
1773 | | /************************************************************************/ |
1774 | | /* TestCapability() */ |
1775 | | /************************************************************************/ |
1776 | | |
1777 | | int OGRShapeLayer::TestCapability(const char *pszCap) |
1778 | | |
1779 | 0 | { |
1780 | 0 | if (!TouchLayer()) |
1781 | 0 | return FALSE; |
1782 | | |
1783 | 0 | if (EQUAL(pszCap, OLCRandomRead)) |
1784 | 0 | return TRUE; |
1785 | | |
1786 | 0 | if (EQUAL(pszCap, OLCSequentialWrite) || EQUAL(pszCap, OLCRandomWrite)) |
1787 | 0 | return m_bUpdateAccess; |
1788 | | |
1789 | 0 | if (EQUAL(pszCap, OLCFastFeatureCount)) |
1790 | 0 | { |
1791 | 0 | if (!(m_poFilterGeom == nullptr || CheckForQIX() || CheckForSBN())) |
1792 | 0 | return FALSE; |
1793 | | |
1794 | 0 | if (m_poAttrQuery != nullptr) |
1795 | 0 | { |
1796 | 0 | InitializeIndexSupport(m_osFullName.c_str()); |
1797 | 0 | return m_poAttrQuery->CanUseIndex(this); |
1798 | 0 | } |
1799 | 0 | return TRUE; |
1800 | 0 | } |
1801 | | |
1802 | 0 | if (EQUAL(pszCap, OLCDeleteFeature)) |
1803 | 0 | return m_bUpdateAccess; |
1804 | | |
1805 | 0 | if (EQUAL(pszCap, OLCFastSpatialFilter)) |
1806 | 0 | return CheckForQIX() || CheckForSBN(); |
1807 | | |
1808 | 0 | if (EQUAL(pszCap, OLCFastGetExtent)) |
1809 | 0 | return TRUE; |
1810 | | |
1811 | 0 | if (EQUAL(pszCap, OLCFastGetExtent3D)) |
1812 | 0 | return m_poFilterGeom == nullptr && m_poAttrQuery == nullptr; |
1813 | | |
1814 | 0 | if (EQUAL(pszCap, OLCFastSetNextByIndex)) |
1815 | 0 | return m_poFilterGeom == nullptr && m_poAttrQuery == nullptr; |
1816 | | |
1817 | 0 | if (EQUAL(pszCap, OLCCreateField)) |
1818 | 0 | return m_bUpdateAccess; |
1819 | | |
1820 | 0 | if (EQUAL(pszCap, OLCDeleteField)) |
1821 | 0 | return m_bUpdateAccess; |
1822 | | |
1823 | 0 | if (EQUAL(pszCap, OLCReorderFields)) |
1824 | 0 | return m_bUpdateAccess; |
1825 | | |
1826 | 0 | if (EQUAL(pszCap, OLCAlterFieldDefn) || |
1827 | 0 | EQUAL(pszCap, OLCAlterGeomFieldDefn)) |
1828 | 0 | return m_bUpdateAccess; |
1829 | | |
1830 | 0 | if (EQUAL(pszCap, OLCRename)) |
1831 | 0 | return m_bUpdateAccess; |
1832 | | |
1833 | 0 | if (EQUAL(pszCap, OLCIgnoreFields)) |
1834 | 0 | return TRUE; |
1835 | | |
1836 | 0 | if (EQUAL(pszCap, OLCStringsAsUTF8)) |
1837 | 0 | { |
1838 | | // No encoding defined: we don't know. |
1839 | 0 | if (m_osEncoding.empty()) |
1840 | 0 | return FALSE; |
1841 | | |
1842 | 0 | if (m_hDBF == nullptr || DBFGetFieldCount(m_hDBF) == 0) |
1843 | 0 | return TRUE; |
1844 | | |
1845 | | // Otherwise test that we can re-encode field names to UTF-8. |
1846 | 0 | const int nFieldCount = DBFGetFieldCount(m_hDBF); |
1847 | 0 | for (int i = 0; i < nFieldCount; i++) |
1848 | 0 | { |
1849 | 0 | char szFieldName[XBASE_FLDNAME_LEN_READ + 1] = {}; |
1850 | 0 | int nWidth = 0; |
1851 | 0 | int nPrecision = 0; |
1852 | |
|
1853 | 0 | DBFGetFieldInfo(m_hDBF, i, szFieldName, &nWidth, &nPrecision); |
1854 | |
|
1855 | 0 | if (!CPLCanRecode(szFieldName, m_osEncoding, CPL_ENC_UTF8)) |
1856 | 0 | { |
1857 | 0 | return FALSE; |
1858 | 0 | } |
1859 | 0 | } |
1860 | | |
1861 | 0 | return TRUE; |
1862 | 0 | } |
1863 | | |
1864 | 0 | if (EQUAL(pszCap, OLCMeasuredGeometries)) |
1865 | 0 | return TRUE; |
1866 | | |
1867 | 0 | if (EQUAL(pszCap, OLCZGeometries)) |
1868 | 0 | return TRUE; |
1869 | | |
1870 | 0 | return FALSE; |
1871 | 0 | } |
1872 | | |
1873 | | /************************************************************************/ |
1874 | | /* CreateField() */ |
1875 | | /************************************************************************/ |
1876 | | |
1877 | | OGRErr OGRShapeLayer::CreateField(const OGRFieldDefn *poFieldDefn, |
1878 | | int bApproxOK) |
1879 | | |
1880 | 0 | { |
1881 | 0 | if (!StartUpdate("CreateField")) |
1882 | 0 | return OGRERR_FAILURE; |
1883 | | |
1884 | 0 | CPLAssert(nullptr != poFieldDefn); |
1885 | | |
1886 | 0 | bool bDBFJustCreated = false; |
1887 | 0 | if (m_hDBF == nullptr) |
1888 | 0 | { |
1889 | 0 | const CPLString osFilename = |
1890 | 0 | CPLResetExtensionSafe(m_osFullName.c_str(), "dbf"); |
1891 | 0 | m_hDBF = DBFCreate(osFilename); |
1892 | |
|
1893 | 0 | if (m_hDBF == nullptr) |
1894 | 0 | { |
1895 | 0 | CPLError(CE_Failure, CPLE_OpenFailed, |
1896 | 0 | "Failed to create DBF file `%s'.", osFilename.c_str()); |
1897 | 0 | return OGRERR_FAILURE; |
1898 | 0 | } |
1899 | | |
1900 | 0 | bDBFJustCreated = true; |
1901 | 0 | } |
1902 | | |
1903 | 0 | if (m_hDBF->nHeaderLength + XBASE_FLDHDR_SZ > 65535) |
1904 | 0 | { |
1905 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
1906 | 0 | "Cannot add field %s. Header length limit reached " |
1907 | 0 | "(max 65535 bytes, 2046 fields).", |
1908 | 0 | poFieldDefn->GetNameRef()); |
1909 | 0 | return OGRERR_FAILURE; |
1910 | 0 | } |
1911 | | |
1912 | 0 | CPLErrorReset(); |
1913 | |
|
1914 | 0 | if (m_poFeatureDefn->GetFieldCount() == 255) |
1915 | 0 | { |
1916 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
1917 | 0 | "Creating a 256th field, " |
1918 | 0 | "but some DBF readers might only support 255 fields"); |
1919 | 0 | } |
1920 | | |
1921 | | /* -------------------------------------------------------------------- */ |
1922 | | /* Normalize field name */ |
1923 | | /* -------------------------------------------------------------------- */ |
1924 | 0 | CPLString osFieldName; |
1925 | 0 | if (!m_osEncoding.empty()) |
1926 | 0 | { |
1927 | 0 | CPLClearRecodeWarningFlags(); |
1928 | 0 | CPLPushErrorHandler(CPLQuietErrorHandler); |
1929 | 0 | CPLErr eLastErr = CPLGetLastErrorType(); |
1930 | 0 | char *const pszRecoded = |
1931 | 0 | CPLRecode(poFieldDefn->GetNameRef(), CPL_ENC_UTF8, m_osEncoding); |
1932 | 0 | CPLPopErrorHandler(); |
1933 | 0 | osFieldName = pszRecoded; |
1934 | 0 | CPLFree(pszRecoded); |
1935 | 0 | if (CPLGetLastErrorType() != eLastErr) |
1936 | 0 | { |
1937 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1938 | 0 | "Failed to create field name '%s': cannot convert to %s", |
1939 | 0 | poFieldDefn->GetNameRef(), m_osEncoding.c_str()); |
1940 | 0 | return OGRERR_FAILURE; |
1941 | 0 | } |
1942 | 0 | } |
1943 | 0 | else |
1944 | 0 | { |
1945 | 0 | osFieldName = poFieldDefn->GetNameRef(); |
1946 | 0 | } |
1947 | | |
1948 | 0 | const int nNameSize = static_cast<int>(osFieldName.size()); |
1949 | 0 | char szNewFieldName[XBASE_FLDNAME_LEN_WRITE + 1]; |
1950 | 0 | CPLString osRadixFieldName; |
1951 | 0 | CPLString osRadixFieldNameUC; |
1952 | 0 | { |
1953 | 0 | char *pszTmp = CPLScanString( |
1954 | 0 | osFieldName, std::min(nNameSize, XBASE_FLDNAME_LEN_WRITE), TRUE, |
1955 | 0 | TRUE); |
1956 | 0 | strncpy(szNewFieldName, pszTmp, sizeof(szNewFieldName) - 1); |
1957 | 0 | szNewFieldName[sizeof(szNewFieldName) - 1] = '\0'; |
1958 | 0 | osRadixFieldName = pszTmp; |
1959 | 0 | osRadixFieldNameUC = CPLString(osRadixFieldName).toupper(); |
1960 | 0 | CPLFree(pszTmp); |
1961 | 0 | } |
1962 | |
|
1963 | 0 | CPLString osNewFieldNameUC(szNewFieldName); |
1964 | 0 | osNewFieldNameUC.toupper(); |
1965 | |
|
1966 | 0 | if (m_oSetUCFieldName.empty()) |
1967 | 0 | { |
1968 | 0 | for (int i = 0; i < m_poFeatureDefn->GetFieldCount(); i++) |
1969 | 0 | { |
1970 | 0 | m_oSetUCFieldName.insert( |
1971 | 0 | CPLString(m_poFeatureDefn->GetFieldDefn(i)->GetNameRef()) |
1972 | 0 | .toupper()); |
1973 | 0 | } |
1974 | 0 | } |
1975 | |
|
1976 | 0 | bool bFoundFieldName = |
1977 | 0 | m_oSetUCFieldName.find(osNewFieldNameUC) != m_oSetUCFieldName.end(); |
1978 | |
|
1979 | 0 | if (!bApproxOK && (bFoundFieldName || !EQUAL(osFieldName, szNewFieldName))) |
1980 | 0 | { |
1981 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
1982 | 0 | "Failed to add field named '%s'", poFieldDefn->GetNameRef()); |
1983 | |
|
1984 | 0 | return OGRERR_FAILURE; |
1985 | 0 | } |
1986 | | |
1987 | 0 | if (bFoundFieldName) |
1988 | 0 | { |
1989 | 0 | int nRenameNum = 1; |
1990 | 0 | while (bFoundFieldName && nRenameNum < 10) |
1991 | 0 | { |
1992 | 0 | CPLsnprintf(szNewFieldName, sizeof(szNewFieldName), "%.8s_%.1d", |
1993 | 0 | osRadixFieldName.c_str(), nRenameNum); |
1994 | 0 | osNewFieldNameUC.Printf("%.8s_%.1d", osRadixFieldNameUC.c_str(), |
1995 | 0 | nRenameNum); |
1996 | 0 | bFoundFieldName = m_oSetUCFieldName.find(osNewFieldNameUC) != |
1997 | 0 | m_oSetUCFieldName.end(); |
1998 | 0 | nRenameNum++; |
1999 | 0 | } |
2000 | |
|
2001 | 0 | while (bFoundFieldName && nRenameNum < 100) |
2002 | 0 | { |
2003 | 0 | CPLsnprintf(szNewFieldName, sizeof(szNewFieldName), "%.8s%.2d", |
2004 | 0 | osRadixFieldName.c_str(), nRenameNum); |
2005 | 0 | osNewFieldNameUC.Printf("%.8s%.2d", osRadixFieldNameUC.c_str(), |
2006 | 0 | nRenameNum); |
2007 | 0 | bFoundFieldName = m_oSetUCFieldName.find(osNewFieldNameUC) != |
2008 | 0 | m_oSetUCFieldName.end(); |
2009 | 0 | nRenameNum++; |
2010 | 0 | } |
2011 | |
|
2012 | 0 | if (bFoundFieldName) |
2013 | 0 | { |
2014 | | // One hundred similar field names!!? |
2015 | 0 | CPLError( |
2016 | 0 | CE_Failure, CPLE_NotSupported, |
2017 | 0 | "Too many field names like '%s' when truncated to %d letters " |
2018 | 0 | "for Shapefile format.", |
2019 | 0 | poFieldDefn->GetNameRef(), XBASE_FLDNAME_LEN_WRITE); |
2020 | 0 | return OGRERR_FAILURE; |
2021 | 0 | } |
2022 | 0 | } |
2023 | | |
2024 | 0 | OGRFieldDefn oModFieldDefn(poFieldDefn); |
2025 | |
|
2026 | 0 | if (!EQUAL(osFieldName, szNewFieldName)) |
2027 | 0 | { |
2028 | 0 | CPLError(CE_Warning, CPLE_NotSupported, |
2029 | 0 | "Normalized/laundered field name: '%s' to '%s'", |
2030 | 0 | poFieldDefn->GetNameRef(), szNewFieldName); |
2031 | | |
2032 | | // Set field name with normalized value. |
2033 | 0 | oModFieldDefn.SetName(szNewFieldName); |
2034 | 0 | } |
2035 | | |
2036 | | /* -------------------------------------------------------------------- */ |
2037 | | /* Add field to layer */ |
2038 | | /* -------------------------------------------------------------------- */ |
2039 | 0 | char chType = 'C'; |
2040 | 0 | int nWidth = 0; |
2041 | 0 | int nDecimals = 0; |
2042 | |
|
2043 | 0 | switch (oModFieldDefn.GetType()) |
2044 | 0 | { |
2045 | 0 | case OFTInteger: |
2046 | 0 | if (oModFieldDefn.GetSubType() == OFSTBoolean) |
2047 | 0 | { |
2048 | 0 | chType = 'L'; |
2049 | 0 | nWidth = 1; |
2050 | 0 | } |
2051 | 0 | else |
2052 | 0 | { |
2053 | 0 | chType = 'N'; |
2054 | 0 | nWidth = oModFieldDefn.GetWidth(); |
2055 | 0 | if (nWidth == 0) |
2056 | 0 | nWidth = 9; |
2057 | 0 | } |
2058 | 0 | break; |
2059 | | |
2060 | 0 | case OFTInteger64: |
2061 | 0 | chType = 'N'; |
2062 | 0 | nWidth = oModFieldDefn.GetWidth(); |
2063 | 0 | if (nWidth == 0) |
2064 | 0 | nWidth = 18; |
2065 | 0 | break; |
2066 | | |
2067 | 0 | case OFTReal: |
2068 | 0 | chType = 'N'; |
2069 | 0 | nWidth = oModFieldDefn.GetWidth(); |
2070 | 0 | nDecimals = oModFieldDefn.GetPrecision(); |
2071 | 0 | if (nWidth == 0) |
2072 | 0 | { |
2073 | 0 | nWidth = 24; |
2074 | 0 | nDecimals = 15; |
2075 | 0 | } |
2076 | 0 | break; |
2077 | | |
2078 | 0 | case OFTString: |
2079 | 0 | chType = 'C'; |
2080 | 0 | nWidth = oModFieldDefn.GetWidth(); |
2081 | 0 | if (nWidth == 0) |
2082 | 0 | nWidth = 80; |
2083 | 0 | else if (nWidth > OGR_DBF_MAX_FIELD_WIDTH) |
2084 | 0 | { |
2085 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
2086 | 0 | "Field %s of width %d truncated to %d.", |
2087 | 0 | szNewFieldName, nWidth, OGR_DBF_MAX_FIELD_WIDTH); |
2088 | 0 | nWidth = OGR_DBF_MAX_FIELD_WIDTH; |
2089 | 0 | } |
2090 | 0 | break; |
2091 | | |
2092 | 0 | case OFTDate: |
2093 | 0 | chType = 'D'; |
2094 | 0 | nWidth = 8; |
2095 | 0 | break; |
2096 | | |
2097 | 0 | case OFTDateTime: |
2098 | 0 | CPLError( |
2099 | 0 | CE_Warning, CPLE_NotSupported, |
2100 | 0 | "Field %s created as String field, though DateTime requested.", |
2101 | 0 | szNewFieldName); |
2102 | 0 | chType = 'C'; |
2103 | 0 | nWidth = static_cast<int>(strlen("YYYY-MM-DDTHH:MM:SS.sss+HH:MM")); |
2104 | 0 | oModFieldDefn.SetType(OFTString); |
2105 | 0 | break; |
2106 | | |
2107 | 0 | default: |
2108 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
2109 | 0 | "Can't create fields of type %s on shapefile layers.", |
2110 | 0 | OGRFieldDefn::GetFieldTypeName(oModFieldDefn.GetType())); |
2111 | |
|
2112 | 0 | return OGRERR_FAILURE; |
2113 | 0 | break; |
2114 | 0 | } |
2115 | | |
2116 | 0 | oModFieldDefn.SetWidth(nWidth); |
2117 | 0 | oModFieldDefn.SetPrecision(nDecimals); |
2118 | | |
2119 | | // Suppress the dummy FID field if we have created it just before. |
2120 | 0 | if (DBFGetFieldCount(m_hDBF) == 1 && m_poFeatureDefn->GetFieldCount() == 0) |
2121 | 0 | { |
2122 | 0 | DBFDeleteField(m_hDBF, 0); |
2123 | 0 | } |
2124 | |
|
2125 | 0 | const int iNewField = DBFAddNativeFieldType(m_hDBF, szNewFieldName, chType, |
2126 | 0 | nWidth, nDecimals); |
2127 | |
|
2128 | 0 | if (iNewField != -1) |
2129 | 0 | { |
2130 | 0 | m_oSetUCFieldName.insert(std::move(osNewFieldNameUC)); |
2131 | |
|
2132 | 0 | whileUnsealing(m_poFeatureDefn)->AddFieldDefn(&oModFieldDefn); |
2133 | |
|
2134 | 0 | if (bDBFJustCreated) |
2135 | 0 | { |
2136 | 0 | for (int i = 0; i < m_nTotalShapeCount; i++) |
2137 | 0 | { |
2138 | 0 | DBFWriteNULLAttribute(m_hDBF, i, 0); |
2139 | 0 | } |
2140 | 0 | } |
2141 | |
|
2142 | 0 | return OGRERR_NONE; |
2143 | 0 | } |
2144 | | |
2145 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
2146 | 0 | "Can't create field %s in Shape DBF file, reason unknown.", |
2147 | 0 | szNewFieldName); |
2148 | |
|
2149 | 0 | return OGRERR_FAILURE; |
2150 | 0 | } |
2151 | | |
2152 | | /************************************************************************/ |
2153 | | /* DeleteField() */ |
2154 | | /************************************************************************/ |
2155 | | |
2156 | | OGRErr OGRShapeLayer::DeleteField(int iField) |
2157 | 0 | { |
2158 | 0 | if (!StartUpdate("DeleteField")) |
2159 | 0 | return OGRERR_FAILURE; |
2160 | | |
2161 | 0 | if (iField < 0 || iField >= m_poFeatureDefn->GetFieldCount()) |
2162 | 0 | { |
2163 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "Invalid field index"); |
2164 | 0 | return OGRERR_FAILURE; |
2165 | 0 | } |
2166 | | |
2167 | 0 | m_oSetUCFieldName.clear(); |
2168 | |
|
2169 | 0 | if (DBFDeleteField(m_hDBF, iField)) |
2170 | 0 | { |
2171 | 0 | TruncateDBF(); |
2172 | |
|
2173 | 0 | return whileUnsealing(m_poFeatureDefn)->DeleteFieldDefn(iField); |
2174 | 0 | } |
2175 | | |
2176 | 0 | return OGRERR_FAILURE; |
2177 | 0 | } |
2178 | | |
2179 | | /************************************************************************/ |
2180 | | /* ReorderFields() */ |
2181 | | /************************************************************************/ |
2182 | | |
2183 | | OGRErr OGRShapeLayer::ReorderFields(int *panMap) |
2184 | 0 | { |
2185 | 0 | if (!StartUpdate("ReorderFields")) |
2186 | 0 | return OGRERR_FAILURE; |
2187 | | |
2188 | 0 | if (m_poFeatureDefn->GetFieldCount() == 0) |
2189 | 0 | return OGRERR_NONE; |
2190 | | |
2191 | 0 | OGRErr eErr = OGRCheckPermutation(panMap, m_poFeatureDefn->GetFieldCount()); |
2192 | 0 | if (eErr != OGRERR_NONE) |
2193 | 0 | return eErr; |
2194 | | |
2195 | 0 | if (DBFReorderFields(m_hDBF, panMap)) |
2196 | 0 | { |
2197 | 0 | return whileUnsealing(m_poFeatureDefn)->ReorderFieldDefns(panMap); |
2198 | 0 | } |
2199 | | |
2200 | 0 | return OGRERR_FAILURE; |
2201 | 0 | } |
2202 | | |
2203 | | /************************************************************************/ |
2204 | | /* AlterFieldDefn() */ |
2205 | | /************************************************************************/ |
2206 | | |
2207 | | OGRErr OGRShapeLayer::AlterFieldDefn(int iField, OGRFieldDefn *poNewFieldDefn, |
2208 | | int nFlagsIn) |
2209 | 0 | { |
2210 | 0 | if (!StartUpdate("AlterFieldDefn")) |
2211 | 0 | return OGRERR_FAILURE; |
2212 | | |
2213 | 0 | if (iField < 0 || iField >= m_poFeatureDefn->GetFieldCount()) |
2214 | 0 | { |
2215 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "Invalid field index"); |
2216 | 0 | return OGRERR_FAILURE; |
2217 | 0 | } |
2218 | | |
2219 | 0 | m_oSetUCFieldName.clear(); |
2220 | |
|
2221 | 0 | OGRFieldDefn *poFieldDefn = m_poFeatureDefn->GetFieldDefn(iField); |
2222 | 0 | OGRFieldType eType = poFieldDefn->GetType(); |
2223 | |
|
2224 | 0 | auto oTemporaryUnsealer(poFieldDefn->GetTemporaryUnsealer()); |
2225 | | |
2226 | | // On reading we support up to 11 characters |
2227 | 0 | char szFieldName[XBASE_FLDNAME_LEN_READ + 1] = {}; |
2228 | 0 | int nWidth = 0; |
2229 | 0 | int nPrecision = 0; |
2230 | 0 | DBFGetFieldInfo(m_hDBF, iField, szFieldName, &nWidth, &nPrecision); |
2231 | 0 | char chNativeType = DBFGetNativeFieldType(m_hDBF, iField); |
2232 | |
|
2233 | 0 | if ((nFlagsIn & ALTER_TYPE_FLAG) && |
2234 | 0 | poNewFieldDefn->GetType() != poFieldDefn->GetType()) |
2235 | 0 | { |
2236 | 0 | if (poNewFieldDefn->GetType() == OFTInteger64 && |
2237 | 0 | poFieldDefn->GetType() == OFTInteger) |
2238 | 0 | { |
2239 | 0 | eType = poNewFieldDefn->GetType(); |
2240 | 0 | } |
2241 | 0 | else if (poNewFieldDefn->GetType() != OFTString) |
2242 | 0 | { |
2243 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
2244 | 0 | "Can only convert to OFTString"); |
2245 | 0 | return OGRERR_FAILURE; |
2246 | 0 | } |
2247 | 0 | else |
2248 | 0 | { |
2249 | 0 | chNativeType = 'C'; |
2250 | 0 | eType = poNewFieldDefn->GetType(); |
2251 | 0 | } |
2252 | 0 | } |
2253 | | |
2254 | 0 | if (nFlagsIn & ALTER_NAME_FLAG) |
2255 | 0 | { |
2256 | 0 | CPLString osFieldName; |
2257 | 0 | if (!m_osEncoding.empty()) |
2258 | 0 | { |
2259 | 0 | CPLClearRecodeWarningFlags(); |
2260 | 0 | CPLErrorReset(); |
2261 | 0 | CPLPushErrorHandler(CPLQuietErrorHandler); |
2262 | 0 | char *pszRecoded = CPLRecode(poNewFieldDefn->GetNameRef(), |
2263 | 0 | CPL_ENC_UTF8, m_osEncoding); |
2264 | 0 | CPLPopErrorHandler(); |
2265 | 0 | osFieldName = pszRecoded; |
2266 | 0 | CPLFree(pszRecoded); |
2267 | 0 | if (CPLGetLastErrorType() != 0) |
2268 | 0 | { |
2269 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
2270 | 0 | "Failed to rename field name to '%s': " |
2271 | 0 | "cannot convert to %s", |
2272 | 0 | poNewFieldDefn->GetNameRef(), m_osEncoding.c_str()); |
2273 | 0 | return OGRERR_FAILURE; |
2274 | 0 | } |
2275 | 0 | } |
2276 | 0 | else |
2277 | 0 | { |
2278 | 0 | osFieldName = poNewFieldDefn->GetNameRef(); |
2279 | 0 | } |
2280 | | |
2281 | 0 | strncpy(szFieldName, osFieldName, sizeof(szFieldName) - 1); |
2282 | 0 | szFieldName[sizeof(szFieldName) - 1] = '\0'; |
2283 | 0 | } |
2284 | 0 | if (nFlagsIn & ALTER_WIDTH_PRECISION_FLAG) |
2285 | 0 | { |
2286 | 0 | nWidth = poNewFieldDefn->GetWidth(); |
2287 | 0 | nPrecision = poNewFieldDefn->GetPrecision(); |
2288 | 0 | } |
2289 | |
|
2290 | 0 | if (DBFAlterFieldDefn(m_hDBF, iField, szFieldName, chNativeType, nWidth, |
2291 | 0 | nPrecision)) |
2292 | 0 | { |
2293 | 0 | if (nFlagsIn & ALTER_TYPE_FLAG) |
2294 | 0 | poFieldDefn->SetType(eType); |
2295 | 0 | if (nFlagsIn & ALTER_NAME_FLAG) |
2296 | 0 | poFieldDefn->SetName(poNewFieldDefn->GetNameRef()); |
2297 | 0 | if (nFlagsIn & ALTER_WIDTH_PRECISION_FLAG) |
2298 | 0 | { |
2299 | 0 | poFieldDefn->SetWidth(nWidth); |
2300 | 0 | poFieldDefn->SetPrecision(nPrecision); |
2301 | |
|
2302 | 0 | TruncateDBF(); |
2303 | 0 | } |
2304 | 0 | return OGRERR_NONE; |
2305 | 0 | } |
2306 | | |
2307 | 0 | return OGRERR_FAILURE; |
2308 | 0 | } |
2309 | | |
2310 | | /************************************************************************/ |
2311 | | /* AlterGeomFieldDefn() */ |
2312 | | /************************************************************************/ |
2313 | | |
2314 | | OGRErr OGRShapeLayer::AlterGeomFieldDefn( |
2315 | | int iGeomField, const OGRGeomFieldDefn *poNewGeomFieldDefn, int nFlagsIn) |
2316 | 0 | { |
2317 | 0 | if (!StartUpdate("AlterGeomFieldDefn")) |
2318 | 0 | return OGRERR_FAILURE; |
2319 | | |
2320 | 0 | if (iGeomField < 0 || iGeomField >= m_poFeatureDefn->GetGeomFieldCount()) |
2321 | 0 | { |
2322 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "Invalid field index"); |
2323 | 0 | return OGRERR_FAILURE; |
2324 | 0 | } |
2325 | | |
2326 | 0 | auto poFieldDefn = cpl::down_cast<OGRShapeGeomFieldDefn *>( |
2327 | 0 | m_poFeatureDefn->GetGeomFieldDefn(iGeomField)); |
2328 | 0 | auto oTemporaryUnsealer(poFieldDefn->GetTemporaryUnsealer()); |
2329 | |
|
2330 | 0 | if (nFlagsIn & ALTER_GEOM_FIELD_DEFN_NAME_FLAG) |
2331 | 0 | { |
2332 | 0 | if (strcmp(poNewGeomFieldDefn->GetNameRef(), |
2333 | 0 | poFieldDefn->GetNameRef()) != 0) |
2334 | 0 | { |
2335 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
2336 | 0 | "Altering the geometry field name is not supported for " |
2337 | 0 | "shapefiles"); |
2338 | 0 | return OGRERR_FAILURE; |
2339 | 0 | } |
2340 | 0 | } |
2341 | | |
2342 | 0 | if (nFlagsIn & ALTER_GEOM_FIELD_DEFN_TYPE_FLAG) |
2343 | 0 | { |
2344 | 0 | if (poFieldDefn->GetType() != poNewGeomFieldDefn->GetType()) |
2345 | 0 | { |
2346 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
2347 | 0 | "Altering the geometry field type is not supported for " |
2348 | 0 | "shapefiles"); |
2349 | 0 | return OGRERR_FAILURE; |
2350 | 0 | } |
2351 | 0 | } |
2352 | | |
2353 | 0 | if (nFlagsIn & ALTER_GEOM_FIELD_DEFN_SRS_COORD_EPOCH_FLAG) |
2354 | 0 | { |
2355 | 0 | const auto poNewSRSRef = poNewGeomFieldDefn->GetSpatialRef(); |
2356 | 0 | if (poNewSRSRef && poNewSRSRef->GetCoordinateEpoch() > 0) |
2357 | 0 | { |
2358 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
2359 | 0 | "Setting a coordinate epoch is not supported for " |
2360 | 0 | "shapefiles"); |
2361 | 0 | return OGRERR_FAILURE; |
2362 | 0 | } |
2363 | 0 | } |
2364 | | |
2365 | 0 | if (nFlagsIn & ALTER_GEOM_FIELD_DEFN_SRS_FLAG) |
2366 | 0 | { |
2367 | 0 | if (poFieldDefn->GetPrjFilename().empty()) |
2368 | 0 | { |
2369 | 0 | poFieldDefn->SetPrjFilename( |
2370 | 0 | CPLResetExtensionSafe(m_osFullName.c_str(), "prj").c_str()); |
2371 | 0 | } |
2372 | |
|
2373 | 0 | const auto poNewSRSRef = poNewGeomFieldDefn->GetSpatialRef(); |
2374 | 0 | if (poNewSRSRef) |
2375 | 0 | { |
2376 | 0 | char *pszWKT = nullptr; |
2377 | 0 | VSILFILE *fp = nullptr; |
2378 | 0 | const char *const apszOptions[] = {"FORMAT=WKT1_ESRI", nullptr}; |
2379 | 0 | if (poNewSRSRef->exportToWkt(&pszWKT, apszOptions) == OGRERR_NONE && |
2380 | 0 | (fp = VSIFOpenL(poFieldDefn->GetPrjFilename().c_str(), "wt")) != |
2381 | 0 | nullptr) |
2382 | 0 | { |
2383 | 0 | VSIFWriteL(pszWKT, strlen(pszWKT), 1, fp); |
2384 | 0 | VSIFCloseL(fp); |
2385 | 0 | } |
2386 | 0 | else |
2387 | 0 | { |
2388 | 0 | CPLError(CE_Failure, CPLE_FileIO, "Cannot write %s", |
2389 | 0 | poFieldDefn->GetPrjFilename().c_str()); |
2390 | 0 | CPLFree(pszWKT); |
2391 | 0 | return OGRERR_FAILURE; |
2392 | 0 | } |
2393 | | |
2394 | 0 | CPLFree(pszWKT); |
2395 | |
|
2396 | 0 | auto poNewSRS = poNewSRSRef->Clone(); |
2397 | 0 | poFieldDefn->SetSpatialRef(poNewSRS); |
2398 | 0 | poNewSRS->Release(); |
2399 | 0 | } |
2400 | 0 | else |
2401 | 0 | { |
2402 | 0 | poFieldDefn->SetSpatialRef(nullptr); |
2403 | 0 | VSIStatBufL sStat; |
2404 | 0 | if (VSIStatL(poFieldDefn->GetPrjFilename().c_str(), &sStat) == 0 && |
2405 | 0 | VSIUnlink(poFieldDefn->GetPrjFilename().c_str()) != 0) |
2406 | 0 | { |
2407 | 0 | CPLError(CE_Failure, CPLE_FileIO, "Cannot delete %s", |
2408 | 0 | poFieldDefn->GetPrjFilename().c_str()); |
2409 | 0 | return OGRERR_FAILURE; |
2410 | 0 | } |
2411 | 0 | } |
2412 | 0 | poFieldDefn->SetSRSSet(); |
2413 | 0 | } |
2414 | | |
2415 | 0 | if (nFlagsIn & ALTER_GEOM_FIELD_DEFN_NAME_FLAG) |
2416 | 0 | poFieldDefn->SetName(poNewGeomFieldDefn->GetNameRef()); |
2417 | 0 | if (nFlagsIn & ALTER_GEOM_FIELD_DEFN_NULLABLE_FLAG) |
2418 | 0 | poFieldDefn->SetNullable(poNewGeomFieldDefn->IsNullable()); |
2419 | |
|
2420 | 0 | return OGRERR_NONE; |
2421 | 0 | } |
2422 | | |
2423 | | /************************************************************************/ |
2424 | | /* GetSpatialRef() */ |
2425 | | /************************************************************************/ |
2426 | | |
2427 | | const OGRSpatialReference *OGRShapeGeomFieldDefn::GetSpatialRef() const |
2428 | | |
2429 | 0 | { |
2430 | 0 | if (m_bSRSSet) |
2431 | 0 | return poSRS; |
2432 | | |
2433 | 0 | m_bSRSSet = true; |
2434 | | |
2435 | | /* -------------------------------------------------------------------- */ |
2436 | | /* Is there an associated .prj file we can read? */ |
2437 | | /* -------------------------------------------------------------------- */ |
2438 | 0 | std::string l_osPrjFile = |
2439 | 0 | CPLResetExtensionSafe(m_osFullName.c_str(), "prj"); |
2440 | |
|
2441 | 0 | char *apszOptions[] = { |
2442 | 0 | const_cast<char *>("EMIT_ERROR_IF_CANNOT_OPEN_FILE=FALSE"), nullptr}; |
2443 | 0 | char **papszLines = CSLLoad2(l_osPrjFile.c_str(), -1, -1, apszOptions); |
2444 | 0 | if (papszLines == nullptr) |
2445 | 0 | { |
2446 | 0 | l_osPrjFile = CPLResetExtensionSafe(m_osFullName.c_str(), "PRJ"); |
2447 | 0 | papszLines = CSLLoad2(l_osPrjFile.c_str(), -1, -1, apszOptions); |
2448 | 0 | } |
2449 | |
|
2450 | 0 | if (papszLines != nullptr) |
2451 | 0 | { |
2452 | 0 | m_osPrjFile = std::move(l_osPrjFile); |
2453 | |
|
2454 | 0 | auto poSRSNonConst = new OGRSpatialReference(); |
2455 | 0 | poSRSNonConst->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER); |
2456 | | // Remove UTF-8 BOM if found |
2457 | | // http://lists.osgeo.org/pipermail/gdal-dev/2014-July/039527.html |
2458 | 0 | if (static_cast<unsigned char>(papszLines[0][0]) == 0xEF && |
2459 | 0 | static_cast<unsigned char>(papszLines[0][1]) == 0xBB && |
2460 | 0 | static_cast<unsigned char>(papszLines[0][2]) == 0xBF) |
2461 | 0 | { |
2462 | 0 | memmove(papszLines[0], papszLines[0] + 3, |
2463 | 0 | strlen(papszLines[0] + 3) + 1); |
2464 | 0 | } |
2465 | 0 | if (STARTS_WITH_CI(papszLines[0], "GEOGCS[")) |
2466 | 0 | { |
2467 | | // Strip AXIS[] in GEOGCS to address use case of |
2468 | | // https://github.com/OSGeo/gdal/issues/8452 |
2469 | 0 | std::string osVal; |
2470 | 0 | for (CSLConstList papszIter = papszLines; *papszIter; ++papszIter) |
2471 | 0 | osVal += *papszIter; |
2472 | 0 | OGR_SRSNode oSRSNode; |
2473 | 0 | const char *pszVal = osVal.c_str(); |
2474 | 0 | if (oSRSNode.importFromWkt(&pszVal) == OGRERR_NONE) |
2475 | 0 | { |
2476 | 0 | oSRSNode.StripNodes("AXIS"); |
2477 | 0 | char *pszWKT = nullptr; |
2478 | 0 | oSRSNode.exportToWkt(&pszWKT); |
2479 | 0 | if (pszWKT) |
2480 | 0 | { |
2481 | 0 | CSLDestroy(papszLines); |
2482 | 0 | papszLines = |
2483 | 0 | static_cast<char **>(CPLCalloc(2, sizeof(char *))); |
2484 | 0 | papszLines[0] = pszWKT; |
2485 | 0 | } |
2486 | 0 | } |
2487 | 0 | } |
2488 | 0 | if (poSRSNonConst->importFromESRI(papszLines) != OGRERR_NONE) |
2489 | 0 | { |
2490 | 0 | delete poSRSNonConst; |
2491 | 0 | poSRSNonConst = nullptr; |
2492 | 0 | } |
2493 | 0 | CSLDestroy(papszLines); |
2494 | |
|
2495 | 0 | if (poSRSNonConst) |
2496 | 0 | { |
2497 | 0 | if (CPLTestBool(CPLGetConfigOption("USE_OSR_FIND_MATCHES", "YES"))) |
2498 | 0 | { |
2499 | 0 | auto poSRSMatch = poSRSNonConst->FindBestMatch(); |
2500 | 0 | if (poSRSMatch) |
2501 | 0 | { |
2502 | 0 | poSRSNonConst->Release(); |
2503 | 0 | poSRSNonConst = poSRSMatch; |
2504 | 0 | poSRSNonConst->SetAxisMappingStrategy( |
2505 | 0 | OAMS_TRADITIONAL_GIS_ORDER); |
2506 | 0 | } |
2507 | 0 | } |
2508 | 0 | else |
2509 | 0 | { |
2510 | 0 | poSRSNonConst->AutoIdentifyEPSG(); |
2511 | 0 | } |
2512 | 0 | poSRS = poSRSNonConst; |
2513 | 0 | } |
2514 | 0 | } |
2515 | |
|
2516 | 0 | return poSRS; |
2517 | 0 | } |
2518 | | |
2519 | | /************************************************************************/ |
2520 | | /* ResetGeomType() */ |
2521 | | /* */ |
2522 | | /* Modify the geometry type for this file. Used to convert to */ |
2523 | | /* a different geometry type when a layer was created with a */ |
2524 | | /* type of unknown, and we get to the first feature to */ |
2525 | | /* establish the type. */ |
2526 | | /************************************************************************/ |
2527 | | |
2528 | | int OGRShapeLayer::ResetGeomType(int nNewGeomType) |
2529 | | |
2530 | 0 | { |
2531 | 0 | if (m_nTotalShapeCount > 0) |
2532 | 0 | return FALSE; |
2533 | | |
2534 | 0 | if (m_hSHP->fpSHX == nullptr) |
2535 | 0 | { |
2536 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
2537 | 0 | "OGRShapeLayer::ResetGeomType failed: SHX file is closed"); |
2538 | 0 | return FALSE; |
2539 | 0 | } |
2540 | | |
2541 | | /* -------------------------------------------------------------------- */ |
2542 | | /* Update .shp header. */ |
2543 | | /* -------------------------------------------------------------------- */ |
2544 | 0 | int nStartPos = static_cast<int>(m_hSHP->sHooks.FTell(m_hSHP->fpSHP)); |
2545 | |
|
2546 | 0 | char abyHeader[100] = {}; |
2547 | 0 | if (m_hSHP->sHooks.FSeek(m_hSHP->fpSHP, 0, SEEK_SET) != 0 || |
2548 | 0 | m_hSHP->sHooks.FRead(abyHeader, 100, 1, m_hSHP->fpSHP) != 1) |
2549 | 0 | return FALSE; |
2550 | | |
2551 | 0 | *(reinterpret_cast<GInt32 *>(abyHeader + 32)) = CPL_LSBWORD32(nNewGeomType); |
2552 | |
|
2553 | 0 | if (m_hSHP->sHooks.FSeek(m_hSHP->fpSHP, 0, SEEK_SET) != 0 || |
2554 | 0 | m_hSHP->sHooks.FWrite(abyHeader, 100, 1, m_hSHP->fpSHP) != 1) |
2555 | 0 | return FALSE; |
2556 | | |
2557 | 0 | if (m_hSHP->sHooks.FSeek(m_hSHP->fpSHP, nStartPos, SEEK_SET) != 0) |
2558 | 0 | return FALSE; |
2559 | | |
2560 | | /* -------------------------------------------------------------------- */ |
2561 | | /* Update .shx header. */ |
2562 | | /* -------------------------------------------------------------------- */ |
2563 | 0 | nStartPos = static_cast<int>(m_hSHP->sHooks.FTell(m_hSHP->fpSHX)); |
2564 | |
|
2565 | 0 | if (m_hSHP->sHooks.FSeek(m_hSHP->fpSHX, 0, SEEK_SET) != 0 || |
2566 | 0 | m_hSHP->sHooks.FRead(abyHeader, 100, 1, m_hSHP->fpSHX) != 1) |
2567 | 0 | return FALSE; |
2568 | | |
2569 | 0 | *(reinterpret_cast<GInt32 *>(abyHeader + 32)) = CPL_LSBWORD32(nNewGeomType); |
2570 | |
|
2571 | 0 | if (m_hSHP->sHooks.FSeek(m_hSHP->fpSHX, 0, SEEK_SET) != 0 || |
2572 | 0 | m_hSHP->sHooks.FWrite(abyHeader, 100, 1, m_hSHP->fpSHX) != 1) |
2573 | 0 | return FALSE; |
2574 | | |
2575 | 0 | if (m_hSHP->sHooks.FSeek(m_hSHP->fpSHX, nStartPos, SEEK_SET) != 0) |
2576 | 0 | return FALSE; |
2577 | | |
2578 | | /* -------------------------------------------------------------------- */ |
2579 | | /* Update other information. */ |
2580 | | /* -------------------------------------------------------------------- */ |
2581 | 0 | m_hSHP->nShapeType = nNewGeomType; |
2582 | |
|
2583 | 0 | return TRUE; |
2584 | 0 | } |
2585 | | |
2586 | | /************************************************************************/ |
2587 | | /* SyncToDisk() */ |
2588 | | /************************************************************************/ |
2589 | | |
2590 | | OGRErr OGRShapeLayer::SyncToDisk() |
2591 | | |
2592 | 0 | { |
2593 | 0 | if (!TouchLayer()) |
2594 | 0 | return OGRERR_FAILURE; |
2595 | | |
2596 | 0 | if (m_bHeaderDirty) |
2597 | 0 | { |
2598 | 0 | if (m_hSHP != nullptr) |
2599 | 0 | SHPWriteHeader(m_hSHP); |
2600 | |
|
2601 | 0 | if (m_hDBF != nullptr) |
2602 | 0 | DBFUpdateHeader(m_hDBF); |
2603 | |
|
2604 | 0 | m_bHeaderDirty = false; |
2605 | 0 | } |
2606 | |
|
2607 | 0 | if (m_hSHP != nullptr) |
2608 | 0 | { |
2609 | 0 | m_hSHP->sHooks.FFlush(m_hSHP->fpSHP); |
2610 | 0 | if (m_hSHP->fpSHX != nullptr) |
2611 | 0 | m_hSHP->sHooks.FFlush(m_hSHP->fpSHX); |
2612 | 0 | } |
2613 | |
|
2614 | 0 | if (m_hDBF != nullptr) |
2615 | 0 | { |
2616 | 0 | m_hDBF->sHooks.FFlush(m_hDBF->fp); |
2617 | 0 | } |
2618 | |
|
2619 | 0 | if (m_eNeedRepack == YES && m_bAutoRepack) |
2620 | 0 | Repack(); |
2621 | |
|
2622 | 0 | return OGRERR_NONE; |
2623 | 0 | } |
2624 | | |
2625 | | /************************************************************************/ |
2626 | | /* DropSpatialIndex() */ |
2627 | | /************************************************************************/ |
2628 | | |
2629 | | OGRErr OGRShapeLayer::DropSpatialIndex() |
2630 | | |
2631 | 0 | { |
2632 | 0 | if (!StartUpdate("DropSpatialIndex")) |
2633 | 0 | return OGRERR_FAILURE; |
2634 | | |
2635 | 0 | if (!CheckForQIX() && !CheckForSBN()) |
2636 | 0 | { |
2637 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
2638 | 0 | "Layer %s has no spatial index, DROP SPATIAL INDEX failed.", |
2639 | 0 | m_poFeatureDefn->GetName()); |
2640 | 0 | return OGRERR_FAILURE; |
2641 | 0 | } |
2642 | | |
2643 | 0 | const bool bHadQIX = m_hQIX != nullptr; |
2644 | |
|
2645 | 0 | SHPCloseDiskTree(m_hQIX); |
2646 | 0 | m_hQIX = nullptr; |
2647 | 0 | m_bCheckedForQIX = false; |
2648 | |
|
2649 | 0 | SBNCloseDiskTree(m_hSBN); |
2650 | 0 | m_hSBN = nullptr; |
2651 | 0 | m_bCheckedForSBN = false; |
2652 | |
|
2653 | 0 | if (bHadQIX) |
2654 | 0 | { |
2655 | 0 | const std::string osQIXFilename = |
2656 | 0 | CPLResetExtensionSafe(m_osFullName.c_str(), "qix"); |
2657 | 0 | CPLDebug("SHAPE", "Unlinking index file %s", osQIXFilename.c_str()); |
2658 | |
|
2659 | 0 | if (VSIUnlink(osQIXFilename.c_str()) != 0) |
2660 | 0 | { |
2661 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
2662 | 0 | "Failed to delete file %s.\n%s", osQIXFilename.c_str(), |
2663 | 0 | VSIStrerror(errno)); |
2664 | 0 | return OGRERR_FAILURE; |
2665 | 0 | } |
2666 | 0 | } |
2667 | | |
2668 | 0 | if (!m_bSbnSbxDeleted) |
2669 | 0 | { |
2670 | 0 | const char papszExt[2][4] = {"sbn", "sbx"}; |
2671 | 0 | for (int i = 0; i < 2; i++) |
2672 | 0 | { |
2673 | 0 | const std::string osIndexFilename = |
2674 | 0 | CPLResetExtensionSafe(m_osFullName.c_str(), papszExt[i]); |
2675 | 0 | CPLDebug("SHAPE", "Trying to unlink index file %s", |
2676 | 0 | osIndexFilename.c_str()); |
2677 | |
|
2678 | 0 | if (VSIUnlink(osIndexFilename.c_str()) != 0) |
2679 | 0 | { |
2680 | 0 | CPLDebug("SHAPE", "Failed to delete file %s.\n%s", |
2681 | 0 | osIndexFilename.c_str(), VSIStrerror(errno)); |
2682 | 0 | } |
2683 | 0 | } |
2684 | 0 | } |
2685 | 0 | m_bSbnSbxDeleted = true; |
2686 | |
|
2687 | 0 | ClearSpatialFIDs(); |
2688 | |
|
2689 | 0 | return OGRERR_NONE; |
2690 | 0 | } |
2691 | | |
2692 | | /************************************************************************/ |
2693 | | /* CreateSpatialIndex() */ |
2694 | | /************************************************************************/ |
2695 | | |
2696 | | OGRErr OGRShapeLayer::CreateSpatialIndex(int nMaxDepth) |
2697 | | |
2698 | 0 | { |
2699 | 0 | if (!StartUpdate("CreateSpatialIndex")) |
2700 | 0 | return OGRERR_FAILURE; |
2701 | | |
2702 | | /* -------------------------------------------------------------------- */ |
2703 | | /* If we have an existing spatial index, blow it away first. */ |
2704 | | /* -------------------------------------------------------------------- */ |
2705 | 0 | if (CheckForQIX()) |
2706 | 0 | DropSpatialIndex(); |
2707 | |
|
2708 | 0 | m_bCheckedForQIX = false; |
2709 | | |
2710 | | /* -------------------------------------------------------------------- */ |
2711 | | /* Build a quadtree structure for this file. */ |
2712 | | /* -------------------------------------------------------------------- */ |
2713 | 0 | OGRShapeLayer::SyncToDisk(); |
2714 | 0 | SHPTree *psTree = SHPCreateTree(m_hSHP, 2, nMaxDepth, nullptr, nullptr); |
2715 | |
|
2716 | 0 | if (nullptr == psTree) |
2717 | 0 | { |
2718 | | // TODO(mloskot): Is it better to return OGRERR_NOT_ENOUGH_MEMORY? |
2719 | 0 | CPLDebug("SHAPE", |
2720 | 0 | "Index creation failure. Likely, memory allocation error."); |
2721 | |
|
2722 | 0 | return OGRERR_FAILURE; |
2723 | 0 | } |
2724 | | |
2725 | | /* -------------------------------------------------------------------- */ |
2726 | | /* Trim unused nodes from the tree. */ |
2727 | | /* -------------------------------------------------------------------- */ |
2728 | 0 | SHPTreeTrimExtraNodes(psTree); |
2729 | | |
2730 | | /* -------------------------------------------------------------------- */ |
2731 | | /* Dump tree to .qix file. */ |
2732 | | /* -------------------------------------------------------------------- */ |
2733 | 0 | char *pszQIXFilename = |
2734 | 0 | CPLStrdup(CPLResetExtensionSafe(m_osFullName.c_str(), "qix").c_str()); |
2735 | |
|
2736 | 0 | CPLDebug("SHAPE", "Creating index file %s", pszQIXFilename); |
2737 | |
|
2738 | 0 | SHPWriteTree(psTree, pszQIXFilename); |
2739 | 0 | CPLFree(pszQIXFilename); |
2740 | | |
2741 | | /* -------------------------------------------------------------------- */ |
2742 | | /* cleanup */ |
2743 | | /* -------------------------------------------------------------------- */ |
2744 | 0 | SHPDestroyTree(psTree); |
2745 | |
|
2746 | 0 | CPL_IGNORE_RET_VAL(CheckForQIX()); |
2747 | |
|
2748 | 0 | return OGRERR_NONE; |
2749 | 0 | } |
2750 | | |
2751 | | /************************************************************************/ |
2752 | | /* CheckFileDeletion() */ |
2753 | | /************************************************************************/ |
2754 | | |
2755 | | static void CheckFileDeletion(const CPLString &osFilename) |
2756 | 0 | { |
2757 | | // On Windows, sometimes the file is still triansiently reported |
2758 | | // as existing although being deleted, which makes QGIS things that |
2759 | | // an issue arose. The following helps to reduce that risk. |
2760 | 0 | VSIStatBufL sStat; |
2761 | 0 | if (VSIStatL(osFilename, &sStat) == 0 && VSIStatL(osFilename, &sStat) == 0) |
2762 | 0 | { |
2763 | 0 | CPLDebug("Shape", |
2764 | 0 | "File %s is still reported as existing whereas " |
2765 | 0 | "it should have been deleted", |
2766 | 0 | osFilename.c_str()); |
2767 | 0 | } |
2768 | 0 | } |
2769 | | |
2770 | | /************************************************************************/ |
2771 | | /* ForceDeleteFile() */ |
2772 | | /************************************************************************/ |
2773 | | |
2774 | | static void ForceDeleteFile(const CPLString &osFilename) |
2775 | 0 | { |
2776 | 0 | if (VSIUnlink(osFilename) != 0) |
2777 | 0 | { |
2778 | | // In case of failure retry with a small delay (Windows specific) |
2779 | 0 | CPLSleep(0.1); |
2780 | 0 | if (VSIUnlink(osFilename) != 0) |
2781 | 0 | { |
2782 | 0 | CPLDebug("Shape", "Cannot delete %s : %s", osFilename.c_str(), |
2783 | 0 | VSIStrerror(errno)); |
2784 | 0 | } |
2785 | 0 | } |
2786 | 0 | CheckFileDeletion(osFilename); |
2787 | 0 | } |
2788 | | |
2789 | | /************************************************************************/ |
2790 | | /* Repack() */ |
2791 | | /* */ |
2792 | | /* Repack the shape and dbf file, dropping deleted records. */ |
2793 | | /* FIDs may change. */ |
2794 | | /************************************************************************/ |
2795 | | |
2796 | | OGRErr OGRShapeLayer::Repack() |
2797 | | |
2798 | 0 | { |
2799 | 0 | if (m_eNeedRepack == NO) |
2800 | 0 | { |
2801 | 0 | CPLDebug("Shape", "REPACK: nothing to do. Was done previously"); |
2802 | 0 | return OGRERR_NONE; |
2803 | 0 | } |
2804 | | |
2805 | 0 | if (!StartUpdate("Repack")) |
2806 | 0 | return OGRERR_FAILURE; |
2807 | | |
2808 | | /* -------------------------------------------------------------------- */ |
2809 | | /* Build a list of records to be dropped. */ |
2810 | | /* -------------------------------------------------------------------- */ |
2811 | 0 | std::vector<int> anRecordsToDelete; |
2812 | 0 | OGRErr eErr = OGRERR_NONE; |
2813 | |
|
2814 | 0 | CPLDebug("Shape", "REPACK: Checking if features have been deleted"); |
2815 | |
|
2816 | 0 | if (m_hDBF != nullptr) |
2817 | 0 | { |
2818 | 0 | try |
2819 | 0 | { |
2820 | 0 | for (int iShape = 0; iShape < m_nTotalShapeCount; iShape++) |
2821 | 0 | { |
2822 | 0 | if (DBFIsRecordDeleted(m_hDBF, iShape)) |
2823 | 0 | { |
2824 | 0 | anRecordsToDelete.push_back(iShape); |
2825 | 0 | } |
2826 | 0 | if (VSIFEofL(VSI_SHP_GetVSIL(m_hDBF->fp)) || |
2827 | 0 | VSIFErrorL(VSI_SHP_GetVSIL(m_hDBF->fp))) |
2828 | 0 | { |
2829 | 0 | return OGRERR_FAILURE; // I/O error. |
2830 | 0 | } |
2831 | 0 | } |
2832 | 0 | } |
2833 | 0 | catch (const std::bad_alloc &) |
2834 | 0 | { |
2835 | 0 | CPLError(CE_Failure, CPLE_OutOfMemory, "Out of memory in Repack()"); |
2836 | 0 | return OGRERR_FAILURE; |
2837 | 0 | } |
2838 | 0 | } |
2839 | | |
2840 | | /* -------------------------------------------------------------------- */ |
2841 | | /* If there are no records marked for deletion, we take no */ |
2842 | | /* action. */ |
2843 | | /* -------------------------------------------------------------------- */ |
2844 | 0 | if (anRecordsToDelete.empty() && !m_bSHPNeedsRepack) |
2845 | 0 | { |
2846 | 0 | CPLDebug("Shape", "REPACK: nothing to do"); |
2847 | 0 | return OGRERR_NONE; |
2848 | 0 | } |
2849 | | |
2850 | | /* -------------------------------------------------------------------- */ |
2851 | | /* Find existing filenames with exact case (see #3293). */ |
2852 | | /* -------------------------------------------------------------------- */ |
2853 | 0 | const CPLString osDirname(CPLGetPathSafe(m_osFullName.c_str())); |
2854 | 0 | const CPLString osBasename(CPLGetBasenameSafe(m_osFullName.c_str())); |
2855 | |
|
2856 | 0 | CPLString osDBFName; |
2857 | 0 | CPLString osSHPName; |
2858 | 0 | CPLString osSHXName; |
2859 | 0 | CPLString osCPGName; |
2860 | 0 | char **papszCandidates = VSIReadDir(osDirname); |
2861 | 0 | int i = 0; |
2862 | 0 | while (papszCandidates != nullptr && papszCandidates[i] != nullptr) |
2863 | 0 | { |
2864 | 0 | const CPLString osCandidateBasename = |
2865 | 0 | CPLGetBasenameSafe(papszCandidates[i]); |
2866 | 0 | const CPLString osCandidateExtension = |
2867 | 0 | CPLGetExtensionSafe(papszCandidates[i]); |
2868 | | #ifdef _WIN32 |
2869 | | // On Windows, as filenames are case insensitive, a shapefile layer can |
2870 | | // be made of foo.shp and FOO.DBF, so use case insensitive comparison. |
2871 | | if (EQUAL(osCandidateBasename, osBasename)) |
2872 | | #else |
2873 | 0 | if (osCandidateBasename.compare(osBasename) == 0) |
2874 | 0 | #endif |
2875 | 0 | { |
2876 | 0 | if (EQUAL(osCandidateExtension, "dbf")) |
2877 | 0 | osDBFName = |
2878 | 0 | CPLFormFilenameSafe(osDirname, papszCandidates[i], nullptr); |
2879 | 0 | else if (EQUAL(osCandidateExtension, "shp")) |
2880 | 0 | osSHPName = |
2881 | 0 | CPLFormFilenameSafe(osDirname, papszCandidates[i], nullptr); |
2882 | 0 | else if (EQUAL(osCandidateExtension, "shx")) |
2883 | 0 | osSHXName = |
2884 | 0 | CPLFormFilenameSafe(osDirname, papszCandidates[i], nullptr); |
2885 | 0 | else if (EQUAL(osCandidateExtension, "cpg")) |
2886 | 0 | osCPGName = |
2887 | 0 | CPLFormFilenameSafe(osDirname, papszCandidates[i], nullptr); |
2888 | 0 | } |
2889 | |
|
2890 | 0 | i++; |
2891 | 0 | } |
2892 | 0 | CSLDestroy(papszCandidates); |
2893 | 0 | papszCandidates = nullptr; |
2894 | |
|
2895 | 0 | if (m_hDBF != nullptr && osDBFName.empty()) |
2896 | 0 | { |
2897 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
2898 | 0 | "Cannot find the filename of the DBF file, but we managed to " |
2899 | 0 | "open it before !"); |
2900 | | // Should not happen, really. |
2901 | 0 | return OGRERR_FAILURE; |
2902 | 0 | } |
2903 | | |
2904 | 0 | if (m_hSHP != nullptr && osSHPName.empty()) |
2905 | 0 | { |
2906 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
2907 | 0 | "Cannot find the filename of the SHP file, but we managed to " |
2908 | 0 | "open it before !"); |
2909 | | // Should not happen, really. |
2910 | 0 | return OGRERR_FAILURE; |
2911 | 0 | } |
2912 | | |
2913 | 0 | if (m_hSHP != nullptr && osSHXName.empty()) |
2914 | 0 | { |
2915 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
2916 | 0 | "Cannot find the filename of the SHX file, but we managed to " |
2917 | 0 | "open it before !"); |
2918 | | // Should not happen, really. |
2919 | 0 | return OGRERR_FAILURE; |
2920 | 0 | } |
2921 | | |
2922 | | /* -------------------------------------------------------------------- */ |
2923 | | /* Cleanup any existing spatial index. It will become */ |
2924 | | /* meaningless when the fids change. */ |
2925 | | /* -------------------------------------------------------------------- */ |
2926 | 0 | if (CheckForQIX() || CheckForSBN()) |
2927 | 0 | DropSpatialIndex(); |
2928 | | |
2929 | | /* -------------------------------------------------------------------- */ |
2930 | | /* Create a new dbf file, matching the old. */ |
2931 | | /* -------------------------------------------------------------------- */ |
2932 | 0 | bool bMustReopenDBF = false; |
2933 | 0 | CPLString oTempFileDBF; |
2934 | 0 | const int nNewRecords = |
2935 | 0 | m_nTotalShapeCount - static_cast<int>(anRecordsToDelete.size()); |
2936 | |
|
2937 | 0 | if (m_hDBF != nullptr && !anRecordsToDelete.empty()) |
2938 | 0 | { |
2939 | 0 | CPLDebug("Shape", "REPACK: repacking .dbf"); |
2940 | 0 | bMustReopenDBF = true; |
2941 | |
|
2942 | 0 | oTempFileDBF = CPLFormFilenameSafe(osDirname, osBasename, nullptr); |
2943 | 0 | oTempFileDBF += "_packed.dbf"; |
2944 | |
|
2945 | 0 | DBFHandle hNewDBF = DBFCloneEmpty(m_hDBF, oTempFileDBF); |
2946 | 0 | if (hNewDBF == nullptr) |
2947 | 0 | { |
2948 | 0 | CPLError(CE_Failure, CPLE_OpenFailed, |
2949 | 0 | "Failed to create temp file %s.", oTempFileDBF.c_str()); |
2950 | 0 | return OGRERR_FAILURE; |
2951 | 0 | } |
2952 | | |
2953 | | // Delete temporary .cpg file if existing. |
2954 | 0 | if (!osCPGName.empty()) |
2955 | 0 | { |
2956 | 0 | CPLString oCPGTempFile = |
2957 | 0 | CPLFormFilenameSafe(osDirname, osBasename, nullptr); |
2958 | 0 | oCPGTempFile += "_packed.cpg"; |
2959 | 0 | ForceDeleteFile(oCPGTempFile); |
2960 | 0 | } |
2961 | | |
2962 | | /* -------------------------------------------------------------------- |
2963 | | */ |
2964 | | /* Copy over all records that are not deleted. */ |
2965 | | /* -------------------------------------------------------------------- |
2966 | | */ |
2967 | 0 | int iDestShape = 0; |
2968 | 0 | size_t iNextDeletedShape = 0; |
2969 | |
|
2970 | 0 | for (int iShape = 0; iShape < m_nTotalShapeCount && eErr == OGRERR_NONE; |
2971 | 0 | iShape++) |
2972 | 0 | { |
2973 | 0 | if (iNextDeletedShape < anRecordsToDelete.size() && |
2974 | 0 | anRecordsToDelete[iNextDeletedShape] == iShape) |
2975 | 0 | { |
2976 | 0 | iNextDeletedShape++; |
2977 | 0 | } |
2978 | 0 | else |
2979 | 0 | { |
2980 | 0 | void *pTuple = const_cast<char *>(DBFReadTuple(m_hDBF, iShape)); |
2981 | 0 | if (pTuple == nullptr || |
2982 | 0 | !DBFWriteTuple(hNewDBF, iDestShape++, pTuple)) |
2983 | 0 | { |
2984 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
2985 | 0 | "Error writing record %d in .dbf", iShape); |
2986 | 0 | eErr = OGRERR_FAILURE; |
2987 | 0 | } |
2988 | 0 | } |
2989 | 0 | } |
2990 | |
|
2991 | 0 | DBFClose(hNewDBF); |
2992 | |
|
2993 | 0 | if (eErr != OGRERR_NONE) |
2994 | 0 | { |
2995 | 0 | VSIUnlink(oTempFileDBF); |
2996 | 0 | return eErr; |
2997 | 0 | } |
2998 | 0 | } |
2999 | | |
3000 | | /* -------------------------------------------------------------------- */ |
3001 | | /* Now create a shapefile matching the old one. */ |
3002 | | /* -------------------------------------------------------------------- */ |
3003 | 0 | bool bMustReopenSHP = m_hSHP != nullptr; |
3004 | 0 | CPLString oTempFileSHP; |
3005 | 0 | CPLString oTempFileSHX; |
3006 | |
|
3007 | 0 | SHPInfo sSHPInfo; |
3008 | 0 | memset(&sSHPInfo, 0, sizeof(sSHPInfo)); |
3009 | 0 | unsigned int *panRecOffsetNew = nullptr; |
3010 | 0 | unsigned int *panRecSizeNew = nullptr; |
3011 | | |
3012 | | // On Windows, use the pack-in-place approach, ie copy the content of |
3013 | | // the _packed files on top of the existing opened files. This avoids |
3014 | | // many issues with files being locked, at the expense of more I/O |
3015 | 0 | const bool bPackInPlace = |
3016 | 0 | CPLTestBool(CPLGetConfigOption("OGR_SHAPE_PACK_IN_PLACE", |
3017 | | #ifdef _WIN32 |
3018 | | "YES" |
3019 | | #else |
3020 | 0 | "NO" |
3021 | 0 | #endif |
3022 | 0 | )); |
3023 | |
|
3024 | 0 | if (m_hSHP != nullptr) |
3025 | 0 | { |
3026 | 0 | CPLDebug("Shape", "REPACK: repacking .shp + .shx"); |
3027 | |
|
3028 | 0 | oTempFileSHP = CPLFormFilenameSafe(osDirname, osBasename, nullptr); |
3029 | 0 | oTempFileSHP += "_packed.shp"; |
3030 | 0 | oTempFileSHX = CPLFormFilenameSafe(osDirname, osBasename, nullptr); |
3031 | 0 | oTempFileSHX += "_packed.shx"; |
3032 | |
|
3033 | 0 | SHPHandle hNewSHP = SHPCreate(oTempFileSHP, m_hSHP->nShapeType); |
3034 | 0 | if (hNewSHP == nullptr) |
3035 | 0 | { |
3036 | 0 | if (!oTempFileDBF.empty()) |
3037 | 0 | VSIUnlink(oTempFileDBF); |
3038 | 0 | return OGRERR_FAILURE; |
3039 | 0 | } |
3040 | | |
3041 | | /* -------------------------------------------------------------------- |
3042 | | */ |
3043 | | /* Copy over all records that are not deleted. */ |
3044 | | /* -------------------------------------------------------------------- |
3045 | | */ |
3046 | 0 | size_t iNextDeletedShape = 0; |
3047 | |
|
3048 | 0 | for (int iShape = 0; iShape < m_nTotalShapeCount && eErr == OGRERR_NONE; |
3049 | 0 | iShape++) |
3050 | 0 | { |
3051 | 0 | if (iNextDeletedShape < anRecordsToDelete.size() && |
3052 | 0 | anRecordsToDelete[iNextDeletedShape] == iShape) |
3053 | 0 | { |
3054 | 0 | iNextDeletedShape++; |
3055 | 0 | } |
3056 | 0 | else |
3057 | 0 | { |
3058 | 0 | SHPObject *hObject = SHPReadObject(m_hSHP, iShape); |
3059 | 0 | if (hObject == nullptr || |
3060 | 0 | SHPWriteObject(hNewSHP, -1, hObject) == -1) |
3061 | 0 | { |
3062 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
3063 | 0 | "Error writing record %d in .shp", iShape); |
3064 | 0 | eErr = OGRERR_FAILURE; |
3065 | 0 | } |
3066 | |
|
3067 | 0 | if (hObject) |
3068 | 0 | SHPDestroyObject(hObject); |
3069 | 0 | } |
3070 | 0 | } |
3071 | |
|
3072 | 0 | if (bPackInPlace) |
3073 | 0 | { |
3074 | | // Backup information of the updated shape context so as to |
3075 | | // restore it later in the current shape context |
3076 | 0 | memcpy(&sSHPInfo, hNewSHP, sizeof(sSHPInfo)); |
3077 | | |
3078 | | // Use malloc like shapelib does |
3079 | 0 | panRecOffsetNew = reinterpret_cast<unsigned int *>( |
3080 | 0 | malloc(sizeof(unsigned int) * hNewSHP->nMaxRecords)); |
3081 | 0 | panRecSizeNew = reinterpret_cast<unsigned int *>( |
3082 | 0 | malloc(sizeof(unsigned int) * hNewSHP->nMaxRecords)); |
3083 | 0 | if (panRecOffsetNew == nullptr || panRecSizeNew == nullptr) |
3084 | 0 | { |
3085 | 0 | CPLError(CE_Failure, CPLE_OutOfMemory, |
3086 | 0 | "Cannot allocate panRecOffsetNew/panRecSizeNew"); |
3087 | 0 | eErr = OGRERR_FAILURE; |
3088 | 0 | } |
3089 | 0 | else |
3090 | 0 | { |
3091 | 0 | memcpy(panRecOffsetNew, hNewSHP->panRecOffset, |
3092 | 0 | sizeof(unsigned int) * hNewSHP->nRecords); |
3093 | 0 | memcpy(panRecSizeNew, hNewSHP->panRecSize, |
3094 | 0 | sizeof(unsigned int) * hNewSHP->nRecords); |
3095 | 0 | } |
3096 | 0 | } |
3097 | |
|
3098 | 0 | SHPClose(hNewSHP); |
3099 | |
|
3100 | 0 | if (eErr != OGRERR_NONE) |
3101 | 0 | { |
3102 | 0 | VSIUnlink(oTempFileSHP); |
3103 | 0 | VSIUnlink(oTempFileSHX); |
3104 | 0 | if (!oTempFileDBF.empty()) |
3105 | 0 | VSIUnlink(oTempFileDBF); |
3106 | 0 | free(panRecOffsetNew); |
3107 | 0 | free(panRecSizeNew); |
3108 | 0 | return eErr; |
3109 | 0 | } |
3110 | 0 | } |
3111 | | |
3112 | | // We could also use pack in place for Unix but this involves extra I/O |
3113 | | // w.r.t to the delete and rename approach |
3114 | | |
3115 | 0 | if (bPackInPlace) |
3116 | 0 | { |
3117 | 0 | if (m_hDBF != nullptr && !oTempFileDBF.empty()) |
3118 | 0 | { |
3119 | 0 | if (!OGRShapeDataSource::CopyInPlace(VSI_SHP_GetVSIL(m_hDBF->fp), |
3120 | 0 | oTempFileDBF)) |
3121 | 0 | { |
3122 | 0 | CPLError( |
3123 | 0 | CE_Failure, CPLE_FileIO, |
3124 | 0 | "An error occurred while copying the content of %s on top " |
3125 | 0 | "of %s. " |
3126 | 0 | "The non corrupted version is in the _packed.dbf, " |
3127 | 0 | "_packed.shp and _packed.shx files that you should rename " |
3128 | 0 | "on top of the main ones.", |
3129 | 0 | oTempFileDBF.c_str(), VSI_SHP_GetFilename(m_hDBF->fp)); |
3130 | 0 | free(panRecOffsetNew); |
3131 | 0 | free(panRecSizeNew); |
3132 | |
|
3133 | 0 | DBFClose(m_hDBF); |
3134 | 0 | m_hDBF = nullptr; |
3135 | 0 | if (m_hSHP != nullptr) |
3136 | 0 | { |
3137 | 0 | SHPClose(m_hSHP); |
3138 | 0 | m_hSHP = nullptr; |
3139 | 0 | } |
3140 | |
|
3141 | 0 | return OGRERR_FAILURE; |
3142 | 0 | } |
3143 | | |
3144 | | // Refresh current handle |
3145 | 0 | m_hDBF->nRecords = nNewRecords; |
3146 | 0 | } |
3147 | | |
3148 | 0 | if (m_hSHP != nullptr && !oTempFileSHP.empty()) |
3149 | 0 | { |
3150 | 0 | if (!OGRShapeDataSource::CopyInPlace(VSI_SHP_GetVSIL(m_hSHP->fpSHP), |
3151 | 0 | oTempFileSHP)) |
3152 | 0 | { |
3153 | 0 | CPLError( |
3154 | 0 | CE_Failure, CPLE_FileIO, |
3155 | 0 | "An error occurred while copying the content of %s on top " |
3156 | 0 | "of %s. " |
3157 | 0 | "The non corrupted version is in the _packed.dbf, " |
3158 | 0 | "_packed.shp and _packed.shx files that you should rename " |
3159 | 0 | "on top of the main ones.", |
3160 | 0 | oTempFileSHP.c_str(), VSI_SHP_GetFilename(m_hSHP->fpSHP)); |
3161 | 0 | free(panRecOffsetNew); |
3162 | 0 | free(panRecSizeNew); |
3163 | |
|
3164 | 0 | if (m_hDBF != nullptr) |
3165 | 0 | { |
3166 | 0 | DBFClose(m_hDBF); |
3167 | 0 | m_hDBF = nullptr; |
3168 | 0 | } |
3169 | 0 | SHPClose(m_hSHP); |
3170 | 0 | m_hSHP = nullptr; |
3171 | |
|
3172 | 0 | return OGRERR_FAILURE; |
3173 | 0 | } |
3174 | 0 | if (!OGRShapeDataSource::CopyInPlace(VSI_SHP_GetVSIL(m_hSHP->fpSHX), |
3175 | 0 | oTempFileSHX)) |
3176 | 0 | { |
3177 | 0 | CPLError( |
3178 | 0 | CE_Failure, CPLE_FileIO, |
3179 | 0 | "An error occurred while copying the content of %s on top " |
3180 | 0 | "of %s. " |
3181 | 0 | "The non corrupted version is in the _packed.dbf, " |
3182 | 0 | "_packed.shp and _packed.shx files that you should rename " |
3183 | 0 | "on top of the main ones.", |
3184 | 0 | oTempFileSHX.c_str(), VSI_SHP_GetFilename(m_hSHP->fpSHX)); |
3185 | 0 | free(panRecOffsetNew); |
3186 | 0 | free(panRecSizeNew); |
3187 | |
|
3188 | 0 | if (m_hDBF != nullptr) |
3189 | 0 | { |
3190 | 0 | DBFClose(m_hDBF); |
3191 | 0 | m_hDBF = nullptr; |
3192 | 0 | } |
3193 | 0 | SHPClose(m_hSHP); |
3194 | 0 | m_hSHP = nullptr; |
3195 | |
|
3196 | 0 | return OGRERR_FAILURE; |
3197 | 0 | } |
3198 | | |
3199 | | // Refresh current handle |
3200 | 0 | m_hSHP->nRecords = sSHPInfo.nRecords; |
3201 | 0 | m_hSHP->nMaxRecords = sSHPInfo.nMaxRecords; |
3202 | 0 | m_hSHP->nFileSize = sSHPInfo.nFileSize; |
3203 | 0 | CPLAssert(sizeof(sSHPInfo.adBoundsMin) == 4 * sizeof(double)); |
3204 | 0 | memcpy(m_hSHP->adBoundsMin, sSHPInfo.adBoundsMin, |
3205 | 0 | sizeof(sSHPInfo.adBoundsMin)); |
3206 | 0 | memcpy(m_hSHP->adBoundsMax, sSHPInfo.adBoundsMax, |
3207 | 0 | sizeof(sSHPInfo.adBoundsMax)); |
3208 | 0 | free(m_hSHP->panRecOffset); |
3209 | 0 | free(m_hSHP->panRecSize); |
3210 | 0 | m_hSHP->panRecOffset = panRecOffsetNew; |
3211 | 0 | m_hSHP->panRecSize = panRecSizeNew; |
3212 | 0 | } |
3213 | 0 | else |
3214 | 0 | { |
3215 | | // The free() are not really necessary but CSA doesn't realize it |
3216 | 0 | free(panRecOffsetNew); |
3217 | 0 | free(panRecSizeNew); |
3218 | 0 | } |
3219 | | |
3220 | | // Now that everything is successful, we can delete the temp files |
3221 | 0 | if (!oTempFileDBF.empty()) |
3222 | 0 | { |
3223 | 0 | ForceDeleteFile(oTempFileDBF); |
3224 | 0 | } |
3225 | 0 | if (!oTempFileSHP.empty()) |
3226 | 0 | { |
3227 | 0 | ForceDeleteFile(oTempFileSHP); |
3228 | 0 | ForceDeleteFile(oTempFileSHX); |
3229 | 0 | } |
3230 | 0 | } |
3231 | 0 | else |
3232 | 0 | { |
3233 | | /* -------------------------------------------------------------------- |
3234 | | */ |
3235 | | /* Cleanup the old .dbf, .shp, .shx and rename the new ones. */ |
3236 | | /* -------------------------------------------------------------------- |
3237 | | */ |
3238 | 0 | if (!oTempFileDBF.empty()) |
3239 | 0 | { |
3240 | 0 | DBFClose(m_hDBF); |
3241 | 0 | m_hDBF = nullptr; |
3242 | |
|
3243 | 0 | if (VSIUnlink(osDBFName) != 0) |
3244 | 0 | { |
3245 | 0 | CPLError(CE_Failure, CPLE_FileIO, |
3246 | 0 | "Failed to delete old DBF file: %s", |
3247 | 0 | VSIStrerror(errno)); |
3248 | |
|
3249 | 0 | m_hDBF = |
3250 | 0 | m_poDS->DS_DBFOpen(osDBFName, m_bUpdateAccess ? "r+" : "r"); |
3251 | |
|
3252 | 0 | VSIUnlink(oTempFileDBF); |
3253 | |
|
3254 | 0 | return OGRERR_FAILURE; |
3255 | 0 | } |
3256 | | |
3257 | 0 | if (VSIRename(oTempFileDBF, osDBFName) != 0) |
3258 | 0 | { |
3259 | 0 | CPLError(CE_Failure, CPLE_FileIO, |
3260 | 0 | "Can not rename new DBF file: %s", VSIStrerror(errno)); |
3261 | 0 | return OGRERR_FAILURE; |
3262 | 0 | } |
3263 | | |
3264 | 0 | CheckFileDeletion(oTempFileDBF); |
3265 | 0 | } |
3266 | | |
3267 | 0 | if (!oTempFileSHP.empty()) |
3268 | 0 | { |
3269 | 0 | SHPClose(m_hSHP); |
3270 | 0 | m_hSHP = nullptr; |
3271 | |
|
3272 | 0 | if (VSIUnlink(osSHPName) != 0) |
3273 | 0 | { |
3274 | 0 | CPLError(CE_Failure, CPLE_FileIO, |
3275 | 0 | "Can not delete old SHP file: %s", VSIStrerror(errno)); |
3276 | 0 | return OGRERR_FAILURE; |
3277 | 0 | } |
3278 | | |
3279 | 0 | if (VSIUnlink(osSHXName) != 0) |
3280 | 0 | { |
3281 | 0 | CPLError(CE_Failure, CPLE_FileIO, |
3282 | 0 | "Can not delete old SHX file: %s", VSIStrerror(errno)); |
3283 | 0 | return OGRERR_FAILURE; |
3284 | 0 | } |
3285 | | |
3286 | 0 | if (VSIRename(oTempFileSHP, osSHPName) != 0) |
3287 | 0 | { |
3288 | 0 | CPLError(CE_Failure, CPLE_FileIO, |
3289 | 0 | "Can not rename new SHP file: %s", VSIStrerror(errno)); |
3290 | 0 | return OGRERR_FAILURE; |
3291 | 0 | } |
3292 | | |
3293 | 0 | if (VSIRename(oTempFileSHX, osSHXName) != 0) |
3294 | 0 | { |
3295 | 0 | CPLError(CE_Failure, CPLE_FileIO, |
3296 | 0 | "Can not rename new SHX file: %s", VSIStrerror(errno)); |
3297 | 0 | return OGRERR_FAILURE; |
3298 | 0 | } |
3299 | | |
3300 | 0 | CheckFileDeletion(oTempFileSHP); |
3301 | 0 | CheckFileDeletion(oTempFileSHX); |
3302 | 0 | } |
3303 | | |
3304 | | /* -------------------------------------------------------------------- |
3305 | | */ |
3306 | | /* Reopen the shapefile */ |
3307 | | /* */ |
3308 | | /* We do not need to reimplement OGRShapeDataSource::OpenFile() here */ |
3309 | | /* with the fully featured error checking. */ |
3310 | | /* If all operations above succeeded, then all necessary files are */ |
3311 | | /* in the right place and accessible. */ |
3312 | | /* -------------------------------------------------------------------- |
3313 | | */ |
3314 | | |
3315 | 0 | const char *const pszAccess = m_bUpdateAccess ? "r+" : "r"; |
3316 | |
|
3317 | 0 | if (bMustReopenSHP) |
3318 | 0 | m_hSHP = m_poDS->DS_SHPOpen(osSHPName, pszAccess); |
3319 | 0 | if (bMustReopenDBF) |
3320 | 0 | m_hDBF = m_poDS->DS_DBFOpen(osDBFName, pszAccess); |
3321 | |
|
3322 | 0 | if ((bMustReopenSHP && nullptr == m_hSHP) || |
3323 | 0 | (bMustReopenDBF && nullptr == m_hDBF)) |
3324 | 0 | return OGRERR_FAILURE; |
3325 | 0 | } |
3326 | | |
3327 | | /* -------------------------------------------------------------------- */ |
3328 | | /* Update total shape count. */ |
3329 | | /* -------------------------------------------------------------------- */ |
3330 | 0 | if (m_hDBF != nullptr) |
3331 | 0 | m_nTotalShapeCount = m_hDBF->nRecords; |
3332 | 0 | m_bSHPNeedsRepack = false; |
3333 | 0 | m_eNeedRepack = NO; |
3334 | |
|
3335 | 0 | return OGRERR_NONE; |
3336 | 0 | } |
3337 | | |
3338 | | /************************************************************************/ |
3339 | | /* ResizeDBF() */ |
3340 | | /* */ |
3341 | | /* Autoshrink columns of the DBF file to their minimum */ |
3342 | | /* size, according to the existing data. */ |
3343 | | /************************************************************************/ |
3344 | | |
3345 | | OGRErr OGRShapeLayer::ResizeDBF() |
3346 | | |
3347 | 0 | { |
3348 | 0 | if (!StartUpdate("ResizeDBF")) |
3349 | 0 | return OGRERR_FAILURE; |
3350 | | |
3351 | 0 | if (m_hDBF == nullptr) |
3352 | 0 | { |
3353 | 0 | CPLError( |
3354 | 0 | CE_Failure, CPLE_NotSupported, |
3355 | 0 | "Attempt to RESIZE a shapefile with no .dbf file not supported."); |
3356 | 0 | return OGRERR_FAILURE; |
3357 | 0 | } |
3358 | | |
3359 | | /* Look which columns must be examined */ |
3360 | 0 | int *panColMap = static_cast<int *>( |
3361 | 0 | CPLMalloc(m_poFeatureDefn->GetFieldCount() * sizeof(int))); |
3362 | 0 | int *panBestWidth = static_cast<int *>( |
3363 | 0 | CPLMalloc(m_poFeatureDefn->GetFieldCount() * sizeof(int))); |
3364 | 0 | int nStringCols = 0; |
3365 | 0 | for (int i = 0; i < m_poFeatureDefn->GetFieldCount(); i++) |
3366 | 0 | { |
3367 | 0 | if (m_poFeatureDefn->GetFieldDefn(i)->GetType() == OFTString || |
3368 | 0 | m_poFeatureDefn->GetFieldDefn(i)->GetType() == OFTInteger || |
3369 | 0 | m_poFeatureDefn->GetFieldDefn(i)->GetType() == OFTInteger64) |
3370 | 0 | { |
3371 | 0 | panColMap[nStringCols] = i; |
3372 | 0 | panBestWidth[nStringCols] = 1; |
3373 | 0 | nStringCols++; |
3374 | 0 | } |
3375 | 0 | } |
3376 | |
|
3377 | 0 | if (nStringCols == 0) |
3378 | 0 | { |
3379 | | // Nothing to do. |
3380 | 0 | CPLFree(panColMap); |
3381 | 0 | CPLFree(panBestWidth); |
3382 | 0 | return OGRERR_NONE; |
3383 | 0 | } |
3384 | | |
3385 | 0 | CPLDebug("SHAPE", "Computing optimal column size..."); |
3386 | |
|
3387 | 0 | bool bAlreadyWarned = false; |
3388 | 0 | for (int i = 0; i < m_hDBF->nRecords; i++) |
3389 | 0 | { |
3390 | 0 | if (!DBFIsRecordDeleted(m_hDBF, i)) |
3391 | 0 | { |
3392 | 0 | for (int j = 0; j < nStringCols; j++) |
3393 | 0 | { |
3394 | 0 | if (DBFIsAttributeNULL(m_hDBF, i, panColMap[j])) |
3395 | 0 | continue; |
3396 | | |
3397 | 0 | const char *pszVal = |
3398 | 0 | DBFReadStringAttribute(m_hDBF, i, panColMap[j]); |
3399 | 0 | const int nLen = static_cast<int>(strlen(pszVal)); |
3400 | 0 | if (nLen > panBestWidth[j]) |
3401 | 0 | panBestWidth[j] = nLen; |
3402 | 0 | } |
3403 | 0 | } |
3404 | 0 | else if (!bAlreadyWarned) |
3405 | 0 | { |
3406 | 0 | bAlreadyWarned = true; |
3407 | 0 | CPLDebug( |
3408 | 0 | "SHAPE", |
3409 | 0 | "DBF file would also need a REPACK due to deleted records"); |
3410 | 0 | } |
3411 | 0 | } |
3412 | |
|
3413 | 0 | for (int j = 0; j < nStringCols; j++) |
3414 | 0 | { |
3415 | 0 | const int iField = panColMap[j]; |
3416 | 0 | OGRFieldDefn *const poFieldDefn = m_poFeatureDefn->GetFieldDefn(iField); |
3417 | |
|
3418 | 0 | const char chNativeType = DBFGetNativeFieldType(m_hDBF, iField); |
3419 | 0 | char szFieldName[XBASE_FLDNAME_LEN_READ + 1] = {}; |
3420 | 0 | int nOriWidth = 0; |
3421 | 0 | int nPrecision = 0; |
3422 | 0 | DBFGetFieldInfo(m_hDBF, iField, szFieldName, &nOriWidth, &nPrecision); |
3423 | |
|
3424 | 0 | if (panBestWidth[j] < nOriWidth) |
3425 | 0 | { |
3426 | 0 | CPLDebug("SHAPE", |
3427 | 0 | "Shrinking field %d (%s) from %d to %d characters", iField, |
3428 | 0 | poFieldDefn->GetNameRef(), nOriWidth, panBestWidth[j]); |
3429 | |
|
3430 | 0 | if (!DBFAlterFieldDefn(m_hDBF, iField, szFieldName, chNativeType, |
3431 | 0 | panBestWidth[j], nPrecision)) |
3432 | 0 | { |
3433 | 0 | CPLError( |
3434 | 0 | CE_Failure, CPLE_AppDefined, |
3435 | 0 | "Shrinking field %d (%s) from %d to %d characters failed", |
3436 | 0 | iField, poFieldDefn->GetNameRef(), nOriWidth, |
3437 | 0 | panBestWidth[j]); |
3438 | |
|
3439 | 0 | CPLFree(panColMap); |
3440 | 0 | CPLFree(panBestWidth); |
3441 | |
|
3442 | 0 | return OGRERR_FAILURE; |
3443 | 0 | } |
3444 | 0 | else |
3445 | 0 | { |
3446 | 0 | whileUnsealing(poFieldDefn)->SetWidth(panBestWidth[j]); |
3447 | 0 | } |
3448 | 0 | } |
3449 | 0 | } |
3450 | | |
3451 | 0 | TruncateDBF(); |
3452 | |
|
3453 | 0 | CPLFree(panColMap); |
3454 | 0 | CPLFree(panBestWidth); |
3455 | |
|
3456 | 0 | return OGRERR_NONE; |
3457 | 0 | } |
3458 | | |
3459 | | /************************************************************************/ |
3460 | | /* TruncateDBF() */ |
3461 | | /************************************************************************/ |
3462 | | |
3463 | | void OGRShapeLayer::TruncateDBF() |
3464 | 0 | { |
3465 | 0 | if (m_hDBF == nullptr) |
3466 | 0 | return; |
3467 | | |
3468 | 0 | m_hDBF->sHooks.FSeek(m_hDBF->fp, 0, SEEK_END); |
3469 | 0 | vsi_l_offset nOldSize = m_hDBF->sHooks.FTell(m_hDBF->fp); |
3470 | 0 | vsi_l_offset nNewSize = |
3471 | 0 | m_hDBF->nRecordLength * static_cast<SAOffset>(m_hDBF->nRecords) + |
3472 | 0 | m_hDBF->nHeaderLength; |
3473 | 0 | if (m_hDBF->bWriteEndOfFileChar) |
3474 | 0 | nNewSize++; |
3475 | 0 | if (nNewSize < nOldSize) |
3476 | 0 | { |
3477 | 0 | CPLDebug("SHAPE", |
3478 | 0 | "Truncating DBF file from " CPL_FRMT_GUIB " to " CPL_FRMT_GUIB |
3479 | 0 | " bytes", |
3480 | 0 | nOldSize, nNewSize); |
3481 | 0 | VSIFTruncateL(VSI_SHP_GetVSIL(m_hDBF->fp), nNewSize); |
3482 | 0 | } |
3483 | 0 | m_hDBF->sHooks.FSeek(m_hDBF->fp, 0, SEEK_SET); |
3484 | 0 | } |
3485 | | |
3486 | | /************************************************************************/ |
3487 | | /* RecomputeExtent() */ |
3488 | | /* */ |
3489 | | /* Force recomputation of the extent of the .SHP file */ |
3490 | | /************************************************************************/ |
3491 | | |
3492 | | OGRErr OGRShapeLayer::RecomputeExtent() |
3493 | 0 | { |
3494 | 0 | if (!StartUpdate("RecomputeExtent")) |
3495 | 0 | return OGRERR_FAILURE; |
3496 | | |
3497 | 0 | if (m_hSHP == nullptr) |
3498 | 0 | { |
3499 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
3500 | 0 | "The RECOMPUTE EXTENT operation is not permitted on a layer " |
3501 | 0 | "without .SHP file."); |
3502 | 0 | return OGRERR_FAILURE; |
3503 | 0 | } |
3504 | | |
3505 | 0 | double adBoundsMin[4] = {0.0, 0.0, 0.0, 0.0}; |
3506 | 0 | double adBoundsMax[4] = {0.0, 0.0, 0.0, 0.0}; |
3507 | |
|
3508 | 0 | bool bHasBeenInit = false; |
3509 | |
|
3510 | 0 | for (int iShape = 0; iShape < m_nTotalShapeCount; iShape++) |
3511 | 0 | { |
3512 | 0 | if (m_hDBF == nullptr || !DBFIsRecordDeleted(m_hDBF, iShape)) |
3513 | 0 | { |
3514 | 0 | SHPObject *psObject = SHPReadObject(m_hSHP, iShape); |
3515 | 0 | if (psObject != nullptr && psObject->nSHPType != SHPT_NULL && |
3516 | 0 | psObject->nVertices != 0) |
3517 | 0 | { |
3518 | 0 | if (!bHasBeenInit) |
3519 | 0 | { |
3520 | 0 | bHasBeenInit = true; |
3521 | 0 | adBoundsMin[0] = psObject->padfX[0]; |
3522 | 0 | adBoundsMax[0] = psObject->padfX[0]; |
3523 | 0 | adBoundsMin[1] = psObject->padfY[0]; |
3524 | 0 | adBoundsMax[1] = psObject->padfY[0]; |
3525 | 0 | if (psObject->padfZ) |
3526 | 0 | { |
3527 | 0 | adBoundsMin[2] = psObject->padfZ[0]; |
3528 | 0 | adBoundsMax[2] = psObject->padfZ[0]; |
3529 | 0 | } |
3530 | 0 | if (psObject->padfM) |
3531 | 0 | { |
3532 | 0 | adBoundsMin[3] = psObject->padfM[0]; |
3533 | 0 | adBoundsMax[3] = psObject->padfM[0]; |
3534 | 0 | } |
3535 | 0 | } |
3536 | |
|
3537 | 0 | for (int i = 0; i < psObject->nVertices; i++) |
3538 | 0 | { |
3539 | 0 | adBoundsMin[0] = |
3540 | 0 | std::min(adBoundsMin[0], psObject->padfX[i]); |
3541 | 0 | adBoundsMin[1] = |
3542 | 0 | std::min(adBoundsMin[1], psObject->padfY[i]); |
3543 | 0 | adBoundsMax[0] = |
3544 | 0 | std::max(adBoundsMax[0], psObject->padfX[i]); |
3545 | 0 | adBoundsMax[1] = |
3546 | 0 | std::max(adBoundsMax[1], psObject->padfY[i]); |
3547 | 0 | if (psObject->padfZ) |
3548 | 0 | { |
3549 | 0 | adBoundsMin[2] = |
3550 | 0 | std::min(adBoundsMin[2], psObject->padfZ[i]); |
3551 | 0 | adBoundsMax[2] = |
3552 | 0 | std::max(adBoundsMax[2], psObject->padfZ[i]); |
3553 | 0 | } |
3554 | 0 | if (psObject->padfM) |
3555 | 0 | { |
3556 | 0 | adBoundsMax[3] = |
3557 | 0 | std::max(adBoundsMax[3], psObject->padfM[i]); |
3558 | 0 | adBoundsMin[3] = |
3559 | 0 | std::min(adBoundsMin[3], psObject->padfM[i]); |
3560 | 0 | } |
3561 | 0 | } |
3562 | 0 | } |
3563 | 0 | SHPDestroyObject(psObject); |
3564 | 0 | } |
3565 | 0 | } |
3566 | |
|
3567 | 0 | if (memcmp(m_hSHP->adBoundsMin, adBoundsMin, 4 * sizeof(double)) != 0 || |
3568 | 0 | memcmp(m_hSHP->adBoundsMax, adBoundsMax, 4 * sizeof(double)) != 0) |
3569 | 0 | { |
3570 | 0 | m_bHeaderDirty = true; |
3571 | 0 | m_hSHP->bUpdated = TRUE; |
3572 | 0 | memcpy(m_hSHP->adBoundsMin, adBoundsMin, 4 * sizeof(double)); |
3573 | 0 | memcpy(m_hSHP->adBoundsMax, adBoundsMax, 4 * sizeof(double)); |
3574 | 0 | } |
3575 | |
|
3576 | 0 | return OGRERR_NONE; |
3577 | 0 | } |
3578 | | |
3579 | | /************************************************************************/ |
3580 | | /* TouchLayer() */ |
3581 | | /************************************************************************/ |
3582 | | |
3583 | | bool OGRShapeLayer::TouchLayer() |
3584 | 0 | { |
3585 | 0 | m_poDS->SetLastUsedLayer(this); |
3586 | |
|
3587 | 0 | if (m_eFileDescriptorsState == FD_OPENED) |
3588 | 0 | return true; |
3589 | 0 | if (m_eFileDescriptorsState == FD_CANNOT_REOPEN) |
3590 | 0 | return false; |
3591 | | |
3592 | 0 | return ReopenFileDescriptors(); |
3593 | 0 | } |
3594 | | |
3595 | | /************************************************************************/ |
3596 | | /* ReopenFileDescriptors() */ |
3597 | | /************************************************************************/ |
3598 | | |
3599 | | bool OGRShapeLayer::ReopenFileDescriptors() |
3600 | 0 | { |
3601 | 0 | CPLDebug("SHAPE", "ReopenFileDescriptors(%s)", m_osFullName.c_str()); |
3602 | |
|
3603 | 0 | const bool bRealUpdateAccess = |
3604 | 0 | m_bUpdateAccess && |
3605 | 0 | (!m_poDS->IsZip() || !m_poDS->GetTemporaryUnzipDir().empty()); |
3606 | |
|
3607 | 0 | if (m_bHSHPWasNonNULL) |
3608 | 0 | { |
3609 | 0 | m_hSHP = m_poDS->DS_SHPOpen(m_osFullName.c_str(), |
3610 | 0 | bRealUpdateAccess ? "r+" : "r"); |
3611 | |
|
3612 | 0 | if (m_hSHP == nullptr) |
3613 | 0 | { |
3614 | 0 | m_eFileDescriptorsState = FD_CANNOT_REOPEN; |
3615 | 0 | return false; |
3616 | 0 | } |
3617 | 0 | } |
3618 | | |
3619 | 0 | if (m_bHDBFWasNonNULL) |
3620 | 0 | { |
3621 | 0 | m_hDBF = m_poDS->DS_DBFOpen(m_osFullName.c_str(), |
3622 | 0 | bRealUpdateAccess ? "r+" : "r"); |
3623 | |
|
3624 | 0 | if (m_hDBF == nullptr) |
3625 | 0 | { |
3626 | 0 | CPLError( |
3627 | 0 | CE_Failure, CPLE_OpenFailed, "Cannot reopen %s", |
3628 | 0 | CPLResetExtensionSafe(m_osFullName.c_str(), "dbf").c_str()); |
3629 | 0 | m_eFileDescriptorsState = FD_CANNOT_REOPEN; |
3630 | 0 | return false; |
3631 | 0 | } |
3632 | 0 | } |
3633 | | |
3634 | 0 | m_eFileDescriptorsState = FD_OPENED; |
3635 | |
|
3636 | 0 | return true; |
3637 | 0 | } |
3638 | | |
3639 | | /************************************************************************/ |
3640 | | /* CloseUnderlyingLayer() */ |
3641 | | /************************************************************************/ |
3642 | | |
3643 | | void OGRShapeLayer::CloseUnderlyingLayer() |
3644 | 0 | { |
3645 | 0 | CPLDebug("SHAPE", "CloseUnderlyingLayer(%s)", m_osFullName.c_str()); |
3646 | |
|
3647 | 0 | if (m_hDBF != nullptr) |
3648 | 0 | DBFClose(m_hDBF); |
3649 | 0 | m_hDBF = nullptr; |
3650 | |
|
3651 | 0 | if (m_hSHP != nullptr) |
3652 | 0 | SHPClose(m_hSHP); |
3653 | 0 | m_hSHP = nullptr; |
3654 | | |
3655 | | // We close QIX and reset the check flag, so that CheckForQIX() |
3656 | | // will retry opening it if necessary when the layer is active again. |
3657 | 0 | if (m_hQIX != nullptr) |
3658 | 0 | SHPCloseDiskTree(m_hQIX); |
3659 | 0 | m_hQIX = nullptr; |
3660 | 0 | m_bCheckedForQIX = false; |
3661 | |
|
3662 | 0 | if (m_hSBN != nullptr) |
3663 | 0 | SBNCloseDiskTree(m_hSBN); |
3664 | 0 | m_hSBN = nullptr; |
3665 | 0 | m_bCheckedForSBN = false; |
3666 | |
|
3667 | 0 | m_eFileDescriptorsState = FD_CLOSED; |
3668 | 0 | } |
3669 | | |
3670 | | /************************************************************************/ |
3671 | | /* AddToFileList() */ |
3672 | | /************************************************************************/ |
3673 | | |
3674 | | void OGRShapeLayer::AddToFileList(CPLStringList &oFileList) |
3675 | 0 | { |
3676 | 0 | if (!TouchLayer()) |
3677 | 0 | return; |
3678 | | |
3679 | 0 | if (m_hSHP) |
3680 | 0 | { |
3681 | 0 | const char *pszSHPFilename = VSI_SHP_GetFilename(m_hSHP->fpSHP); |
3682 | 0 | oFileList.AddStringDirectly(VSIGetCanonicalFilename(pszSHPFilename)); |
3683 | 0 | const std::string osSHPExt = CPLGetExtensionSafe(pszSHPFilename); |
3684 | 0 | const std::string osSHXFilename = CPLResetExtensionSafe( |
3685 | 0 | pszSHPFilename, (osSHPExt[0] == 's') ? "shx" : "SHX"); |
3686 | 0 | oFileList.AddStringDirectly( |
3687 | 0 | VSIGetCanonicalFilename(osSHXFilename.c_str())); |
3688 | 0 | } |
3689 | |
|
3690 | 0 | if (m_hDBF) |
3691 | 0 | { |
3692 | 0 | const char *pszDBFFilename = VSI_SHP_GetFilename(m_hDBF->fp); |
3693 | 0 | oFileList.AddStringDirectly(VSIGetCanonicalFilename(pszDBFFilename)); |
3694 | 0 | if (m_hDBF->pszCodePage != nullptr && m_hDBF->iLanguageDriver == 0) |
3695 | 0 | { |
3696 | 0 | const std::string osDBFExt = CPLGetExtensionSafe(pszDBFFilename); |
3697 | 0 | const std::string osCPGFilename = CPLResetExtensionSafe( |
3698 | 0 | pszDBFFilename, (osDBFExt[0] == 'd') ? "cpg" : "CPG"); |
3699 | 0 | oFileList.AddStringDirectly( |
3700 | 0 | VSIGetCanonicalFilename(osCPGFilename.c_str())); |
3701 | 0 | } |
3702 | 0 | } |
3703 | |
|
3704 | 0 | if (m_hSHP) |
3705 | 0 | { |
3706 | 0 | if (GetSpatialRef() != nullptr) |
3707 | 0 | { |
3708 | 0 | OGRShapeGeomFieldDefn *poGeomFieldDefn = |
3709 | 0 | cpl::down_cast<OGRShapeGeomFieldDefn *>( |
3710 | 0 | GetLayerDefn()->GetGeomFieldDefn(0)); |
3711 | 0 | oFileList.AddStringDirectly( |
3712 | 0 | VSIGetCanonicalFilename(poGeomFieldDefn->GetPrjFilename())); |
3713 | 0 | } |
3714 | 0 | if (CheckForQIX()) |
3715 | 0 | { |
3716 | 0 | const std::string osQIXFilename = |
3717 | 0 | CPLResetExtensionSafe(m_osFullName.c_str(), "qix"); |
3718 | 0 | oFileList.AddStringDirectly( |
3719 | 0 | VSIGetCanonicalFilename(osQIXFilename.c_str())); |
3720 | 0 | } |
3721 | 0 | else if (CheckForSBN()) |
3722 | 0 | { |
3723 | 0 | const std::string osSBNFilename = |
3724 | 0 | CPLResetExtensionSafe(m_osFullName.c_str(), "sbn"); |
3725 | 0 | oFileList.AddStringDirectly( |
3726 | 0 | VSIGetCanonicalFilename(osSBNFilename.c_str())); |
3727 | 0 | const std::string osSBXFilename = |
3728 | 0 | CPLResetExtensionSafe(m_osFullName.c_str(), "sbx"); |
3729 | 0 | oFileList.AddStringDirectly( |
3730 | 0 | VSIGetCanonicalFilename(osSBXFilename.c_str())); |
3731 | 0 | } |
3732 | 0 | } |
3733 | 0 | } |
3734 | | |
3735 | | /************************************************************************/ |
3736 | | /* UpdateFollowingDeOrRecompression() */ |
3737 | | /************************************************************************/ |
3738 | | |
3739 | | void OGRShapeLayer::UpdateFollowingDeOrRecompression() |
3740 | 0 | { |
3741 | 0 | CPLAssert(m_poDS->IsZip()); |
3742 | 0 | CPLString osDSDir = m_poDS->GetTemporaryUnzipDir(); |
3743 | 0 | if (osDSDir.empty()) |
3744 | 0 | osDSDir = m_poDS->GetVSIZipPrefixeDir(); |
3745 | |
|
3746 | 0 | if (GetSpatialRef() != nullptr) |
3747 | 0 | { |
3748 | 0 | OGRShapeGeomFieldDefn *poGeomFieldDefn = |
3749 | 0 | cpl::down_cast<OGRShapeGeomFieldDefn *>( |
3750 | 0 | GetLayerDefn()->GetGeomFieldDefn(0)); |
3751 | 0 | poGeomFieldDefn->SetPrjFilename( |
3752 | 0 | CPLFormFilenameSafe( |
3753 | 0 | osDSDir.c_str(), |
3754 | 0 | CPLGetFilename(poGeomFieldDefn->GetPrjFilename().c_str()), |
3755 | 0 | nullptr) |
3756 | 0 | .c_str()); |
3757 | 0 | } |
3758 | |
|
3759 | 0 | m_osFullName = CPLFormFilenameSafe( |
3760 | 0 | osDSDir, CPLGetFilename(m_osFullName.c_str()), nullptr); |
3761 | 0 | CloseUnderlyingLayer(); |
3762 | 0 | } |
3763 | | |
3764 | | /************************************************************************/ |
3765 | | /* Rename() */ |
3766 | | /************************************************************************/ |
3767 | | |
3768 | | OGRErr OGRShapeLayer::Rename(const char *pszNewName) |
3769 | 0 | { |
3770 | 0 | if (!TestCapability(OLCRename)) |
3771 | 0 | return OGRERR_FAILURE; |
3772 | | |
3773 | 0 | if (m_poDS->GetLayerByName(pszNewName) != nullptr) |
3774 | 0 | { |
3775 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Layer %s already exists", |
3776 | 0 | pszNewName); |
3777 | 0 | return OGRERR_FAILURE; |
3778 | 0 | } |
3779 | | |
3780 | 0 | if (!m_poDS->UncompressIfNeeded()) |
3781 | 0 | return OGRERR_FAILURE; |
3782 | | |
3783 | 0 | CPLStringList oFileList; |
3784 | 0 | AddToFileList(oFileList); |
3785 | |
|
3786 | 0 | const std::string osDirname = CPLGetPathSafe(m_osFullName.c_str()); |
3787 | 0 | for (int i = 0; i < oFileList.size(); ++i) |
3788 | 0 | { |
3789 | 0 | const std::string osRenamedFile = |
3790 | 0 | CPLFormFilenameSafe(osDirname.c_str(), pszNewName, |
3791 | 0 | CPLGetExtensionSafe(oFileList[i]).c_str()); |
3792 | 0 | VSIStatBufL sStat; |
3793 | 0 | if (VSIStatL(osRenamedFile.c_str(), &sStat) == 0) |
3794 | 0 | { |
3795 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "File %s already exists", |
3796 | 0 | osRenamedFile.c_str()); |
3797 | 0 | return OGRERR_FAILURE; |
3798 | 0 | } |
3799 | 0 | } |
3800 | | |
3801 | 0 | CloseUnderlyingLayer(); |
3802 | |
|
3803 | 0 | for (int i = 0; i < oFileList.size(); ++i) |
3804 | 0 | { |
3805 | 0 | const std::string osRenamedFile = |
3806 | 0 | CPLFormFilenameSafe(osDirname.c_str(), pszNewName, |
3807 | 0 | CPLGetExtensionSafe(oFileList[i]).c_str()); |
3808 | 0 | if (VSIRename(oFileList[i], osRenamedFile.c_str()) != 0) |
3809 | 0 | { |
3810 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Cannot rename %s to %s", |
3811 | 0 | oFileList[i], osRenamedFile.c_str()); |
3812 | 0 | return OGRERR_FAILURE; |
3813 | 0 | } |
3814 | 0 | } |
3815 | | |
3816 | 0 | if (GetSpatialRef() != nullptr) |
3817 | 0 | { |
3818 | 0 | OGRShapeGeomFieldDefn *poGeomFieldDefn = |
3819 | 0 | cpl::down_cast<OGRShapeGeomFieldDefn *>( |
3820 | 0 | GetLayerDefn()->GetGeomFieldDefn(0)); |
3821 | 0 | poGeomFieldDefn->SetPrjFilename( |
3822 | 0 | CPLFormFilenameSafe( |
3823 | 0 | osDirname.c_str(), pszNewName, |
3824 | 0 | CPLGetExtensionSafe(poGeomFieldDefn->GetPrjFilename().c_str()) |
3825 | 0 | .c_str()) |
3826 | 0 | .c_str()); |
3827 | 0 | } |
3828 | |
|
3829 | 0 | m_osFullName = |
3830 | 0 | CPLFormFilenameSafe(osDirname.c_str(), pszNewName, |
3831 | 0 | CPLGetExtensionSafe(m_osFullName.c_str()).c_str()); |
3832 | |
|
3833 | 0 | if (!ReopenFileDescriptors()) |
3834 | 0 | return OGRERR_FAILURE; |
3835 | | |
3836 | 0 | SetDescription(pszNewName); |
3837 | 0 | whileUnsealing(m_poFeatureDefn)->SetName(pszNewName); |
3838 | |
|
3839 | 0 | return OGRERR_NONE; |
3840 | 0 | } |
3841 | | |
3842 | | /************************************************************************/ |
3843 | | /* GetDataset() */ |
3844 | | /************************************************************************/ |
3845 | | |
3846 | | GDALDataset *OGRShapeLayer::GetDataset() |
3847 | 0 | { |
3848 | 0 | return m_poDS; |
3849 | 0 | } |
3850 | | |
3851 | | /************************************************************************/ |
3852 | | /* GetNextArrowArray() */ |
3853 | | /************************************************************************/ |
3854 | | |
3855 | | // Specialized implementation restricted to situations where only retrieving |
3856 | | // of FID values is asked (without filters) |
3857 | | // In other cases, fall back to generic implementation. |
3858 | | int OGRShapeLayer::GetNextArrowArray(struct ArrowArrayStream *stream, |
3859 | | struct ArrowArray *out_array) |
3860 | 0 | { |
3861 | 0 | m_bLastGetNextArrowArrayUsedOptimizedCodePath = false; |
3862 | 0 | if (!TouchLayer()) |
3863 | 0 | { |
3864 | 0 | memset(out_array, 0, sizeof(*out_array)); |
3865 | 0 | return EIO; |
3866 | 0 | } |
3867 | | |
3868 | 0 | if (!m_hDBF || m_poAttrQuery != nullptr || m_poFilterGeom != nullptr) |
3869 | 0 | { |
3870 | 0 | return OGRLayer::GetNextArrowArray(stream, out_array); |
3871 | 0 | } |
3872 | | |
3873 | | // If any field is not ignored, use generic implementation |
3874 | 0 | const int nFieldCount = m_poFeatureDefn->GetFieldCount(); |
3875 | 0 | for (int i = 0; i < nFieldCount; ++i) |
3876 | 0 | { |
3877 | 0 | if (!m_poFeatureDefn->GetFieldDefn(i)->IsIgnored()) |
3878 | 0 | return OGRLayer::GetNextArrowArray(stream, out_array); |
3879 | 0 | } |
3880 | 0 | if (GetGeomType() != wkbNone && |
3881 | 0 | !m_poFeatureDefn->GetGeomFieldDefn(0)->IsIgnored()) |
3882 | 0 | return OGRLayer::GetNextArrowArray(stream, out_array); |
3883 | | |
3884 | 0 | OGRArrowArrayHelper sHelper(m_poDS, m_poFeatureDefn, |
3885 | 0 | m_aosArrowArrayStreamOptions, out_array); |
3886 | 0 | if (out_array->release == nullptr) |
3887 | 0 | { |
3888 | 0 | return ENOMEM; |
3889 | 0 | } |
3890 | | |
3891 | 0 | if (!sHelper.m_bIncludeFID) |
3892 | 0 | { |
3893 | 0 | out_array->release(out_array); |
3894 | 0 | return OGRLayer::GetNextArrowArray(stream, out_array); |
3895 | 0 | } |
3896 | | |
3897 | 0 | m_bLastGetNextArrowArrayUsedOptimizedCodePath = true; |
3898 | 0 | int nCount = 0; |
3899 | 0 | while (m_iNextShapeId < m_nTotalShapeCount) |
3900 | 0 | { |
3901 | 0 | const bool bIsDeleted = |
3902 | 0 | CPL_TO_BOOL(DBFIsRecordDeleted(m_hDBF, m_iNextShapeId)); |
3903 | 0 | if (bIsDeleted) |
3904 | 0 | { |
3905 | 0 | ++m_iNextShapeId; |
3906 | 0 | continue; |
3907 | 0 | } |
3908 | 0 | if (VSIFEofL(VSI_SHP_GetVSIL(m_hDBF->fp)) || |
3909 | 0 | VSIFErrorL(VSI_SHP_GetVSIL(m_hDBF->fp))) |
3910 | 0 | { |
3911 | 0 | out_array->release(out_array); |
3912 | 0 | memset(out_array, 0, sizeof(*out_array)); |
3913 | 0 | return EIO; |
3914 | 0 | } |
3915 | 0 | sHelper.m_panFIDValues[nCount] = m_iNextShapeId; |
3916 | 0 | ++m_iNextShapeId; |
3917 | 0 | ++nCount; |
3918 | 0 | if (nCount == sHelper.m_nMaxBatchSize) |
3919 | 0 | break; |
3920 | 0 | } |
3921 | 0 | sHelper.Shrink(nCount); |
3922 | 0 | if (nCount == 0) |
3923 | 0 | { |
3924 | 0 | out_array->release(out_array); |
3925 | 0 | memset(out_array, 0, sizeof(*out_array)); |
3926 | 0 | } |
3927 | 0 | return 0; |
3928 | 0 | } |
3929 | | |
3930 | | /************************************************************************/ |
3931 | | /* GetMetadataItem() */ |
3932 | | /************************************************************************/ |
3933 | | |
3934 | | const char *OGRShapeLayer::GetMetadataItem(const char *pszName, |
3935 | | const char *pszDomain) |
3936 | 0 | { |
3937 | 0 | if (pszName && pszDomain && EQUAL(pszDomain, "__DEBUG__") && |
3938 | 0 | EQUAL(pszName, "LAST_GET_NEXT_ARROW_ARRAY_USED_OPTIMIZED_CODE_PATH")) |
3939 | 0 | { |
3940 | 0 | return m_bLastGetNextArrowArrayUsedOptimizedCodePath ? "YES" : "NO"; |
3941 | 0 | } |
3942 | 0 | return OGRLayer::GetMetadataItem(pszName, pszDomain); |
3943 | 0 | } |