| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Project: Parquet Translator |
| 4 |
* Purpose: Implements OGRParquetDriver. |
| 5 |
* Author: Even Rouault, <even.rouault at spatialys.com> |
| 6 |
* |
| 7 |
****************************************************************************** |
| 8 |
* Copyright (c) 2022, Planet Labs |
| 9 |
* |
| 10 |
* SPDX-License-Identifier: MIT |
| 11 |
****************************************************************************/ |
| 12 |
|
| 13 |
#ifdef STANDALONE |
| 14 |
#include "gdal_version.h" |
| 15 |
#else |
| 16 |
#undef DO_NOT_DEFINE_GDAL_DATE_NAME |
| 17 |
#include "gdal_version_full/gdal_version.h" |
| 18 |
#endif |
| 19 |
|
| 20 |
#include "ogr_parquet.h" |
| 21 |
|
| 22 |
#include "../arrow_common/ograrrowwriterlayer.hpp" |
| 23 |
|
| 24 |
#include "ogr_wkb.h" |
| 25 |
|
| 26 |
#include <cassert> |
| 27 |
#include <utility> |
| 28 |
|
| 29 |
/************************************************************************/ |
| 30 |
/* OGRParquetWriterLayer() */ |
| 31 |
/************************************************************************/ |
| 32 |
|
| 33 |
OGRParquetWriterLayer::OGRParquetWriterLayer( |
| 34 |
OGRParquetWriterDataset *poDataset, arrow::MemoryPool *poMemoryPool, |
| 35 |
const std::shared_ptr<arrow::io::OutputStream> &poOutputStream, |
| 36 |
const char *pszLayerName) |
| 37 |
: OGRArrowWriterLayer(poMemoryPool, poOutputStream, pszLayerName), |
| 38 |
m_poDataset(poDataset) |
| 39 |
{ |
| 40 |
m_bWriteFieldArrowExtensionName = CPLTestBool( |
| 41 |
CPLGetConfigOption("OGR_PARQUET_WRITE_ARROW_EXTENSION_NAME", "NO")); |
| 42 |
} |
| 43 |
|
| 44 |
/************************************************************************/ |
| 45 |
/* Close() */ |
| 46 |
/************************************************************************/ |
| 47 |
|
| 48 |
bool OGRParquetWriterLayer::Close() |
| 49 |
{ |
| 50 |
if (m_poTmpGPKGLayer) |
| 51 |
{ |
| 52 |
if (!CopyTmpGpkgLayerToFinalFile()) |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
if (m_bInitializationOK) |
| 57 |
{ |
| 58 |
if (!FinalizeWriting()) |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
/************************************************************************/ |
| 66 |
/* CopyTmpGpkgLayerToFinalFile() */ |
| 67 |
/************************************************************************/ |
| 68 |
|
| 69 |
bool OGRParquetWriterLayer::CopyTmpGpkgLayerToFinalFile() |
| 70 |
{ |
| 71 |
if (!m_poTmpGPKGLayer) |
| 72 |
{ |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
CPLDebug("PARQUET", "CopyTmpGpkgLayerToFinalFile(): start..."); |
| 77 |
|
| 78 |
VSIUnlink(m_poTmpGPKG->GetDescription()); |
| 79 |
|
| 80 |
OGRFeature oFeat(m_poFeatureDefn); |
| 81 |
|
| 82 |
// Interval in terms of features between 2 debug progress report messages |
| 83 |
constexpr int PROGRESS_FC_INTERVAL = 100 * 1000; |
| 84 |
|
| 85 |
// First, write features without geometries |
| 86 |
{ |
| 87 |
auto poTmpLayer = std::unique_ptr<OGRLayer>(m_poTmpGPKG->ExecuteSQL( |
| 88 |
"SELECT serialized_feature FROM tmp WHERE fid NOT IN (SELECT id " |
| 89 |
"FROM rtree_tmp_geom)", |
| 90 |
nullptr, nullptr)); |
| 91 |
if (!poTmpLayer) |
| 92 |
return false; |
| 93 |
for (const auto &poSrcFeature : poTmpLayer.get()) |
| 94 |
{ |
| 95 |
int nBytesFeature = 0; |
| 96 |
const GByte *pabyFeatureData = |
| 97 |
poSrcFeature->GetFieldAsBinary(0, &nBytesFeature); |
| 98 |
if (!oFeat.DeserializeFromBinary(pabyFeatureData, nBytesFeature)) |
| 99 |
{ |
| 100 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 101 |
"Cannot deserialize feature"); |
| 102 |
return false; |
| 103 |
} |
| 104 |
if (OGRArrowWriterLayer::ICreateFeature(&oFeat) != OGRERR_NONE) |
| 105 |
{ |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
if ((m_nFeatureCount % PROGRESS_FC_INTERVAL) == 0) |
| 110 |
{ |
| 111 |
CPLDebugProgress( |
| 112 |
"PARQUET", |
| 113 |
"CopyTmpGpkgLayerToFinalFile(): %.02f%% progress", |
| 114 |
100.0 * double(m_nFeatureCount) / |
| 115 |
double(m_nTmpFeatureCount)); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
if (!FlushFeatures()) |
| 120 |
{ |
| 121 |
return false; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// Now walk through the GPKG RTree for features with geometries |
| 126 |
// Cf https://github.com/sqlite/sqlite/blob/master/ext/rtree/rtree.c |
| 127 |
// for the description of the content of the rtree _node table |
| 128 |
std::vector<std::pair<int64_t, int>> aNodeNoDepthPair; |
| 129 |
int nTreeDepth = 0; |
| 130 |
// Queue the root node |
| 131 |
aNodeNoDepthPair.emplace_back( |
| 132 |
std::make_pair(/* nodeNo = */ 1, /* depth = */ 0)); |
| 133 |
int nCountWrittenFeaturesSinceLastFlush = 0; |
| 134 |
while (!aNodeNoDepthPair.empty()) |
| 135 |
{ |
| 136 |
const auto &oLastPair = aNodeNoDepthPair.back(); |
| 137 |
const int64_t nNodeNo = oLastPair.first; |
| 138 |
const int nCurDepth = oLastPair.second; |
| 139 |
//CPLDebug("PARQUET", "Reading nodeNode=%d, curDepth=%d", int(nNodeNo), nCurDepth); |
| 140 |
aNodeNoDepthPair.pop_back(); |
| 141 |
|
| 142 |
auto poRTreeLayer = std::unique_ptr<OGRLayer>(m_poTmpGPKG->ExecuteSQL( |
| 143 |
CPLSPrintf("SELECT data FROM rtree_tmp_geom_node WHERE nodeno " |
| 144 |
"= " CPL_FRMT_GIB, |
| 145 |
static_cast<GIntBig>(nNodeNo)), |
| 146 |
nullptr, nullptr)); |
| 147 |
if (!poRTreeLayer) |
| 148 |
{ |
| 149 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 150 |
"Cannot read node " CPL_FRMT_GIB, |
| 151 |
static_cast<GIntBig>(nNodeNo)); |
| 152 |
return false; |
| 153 |
} |
| 154 |
const auto poRTreeFeature = |
| 155 |
std::unique_ptr<const OGRFeature>(poRTreeLayer->GetNextFeature()); |
| 156 |
if (!poRTreeFeature) |
| 157 |
{ |
| 158 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 159 |
"Cannot read node " CPL_FRMT_GIB, |
| 160 |
static_cast<GIntBig>(nNodeNo)); |
| 161 |
return false; |
| 162 |
} |
| 163 |
|
| 164 |
int nNodeBytes = 0; |
| 165 |
const GByte *pabyNodeData = |
| 166 |
poRTreeFeature->GetFieldAsBinary(0, &nNodeBytes); |
| 167 |
constexpr int BLOB_HEADER_SIZE = 4; |
| 168 |
if (nNodeBytes < BLOB_HEADER_SIZE) |
| 169 |
{ |
| 170 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 171 |
"Not enough bytes when reading node " CPL_FRMT_GIB, |
| 172 |
static_cast<GIntBig>(nNodeNo)); |
| 173 |
return false; |
| 174 |
} |
| 175 |
if (nNodeNo == 1) |
| 176 |
{ |
| 177 |
// Get the RTree depth from the root node |
| 178 |
nTreeDepth = (pabyNodeData[0] << 8) | pabyNodeData[1]; |
| 179 |
//CPLDebug("PARQUET", "nTreeDepth = %d", nTreeDepth); |
| 180 |
} |
| 181 |
|
| 182 |
const int nCellCount = (pabyNodeData[2] << 8) | pabyNodeData[3]; |
| 183 |
constexpr int SIZEOF_CELL = 24; // int64_t + 4 float |
| 184 |
if (nNodeBytes < BLOB_HEADER_SIZE + SIZEOF_CELL * nCellCount) |
| 185 |
{ |
| 186 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 187 |
"Not enough bytes when reading node " CPL_FRMT_GIB, |
| 188 |
static_cast<GIntBig>(nNodeNo)); |
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
size_t nOffset = BLOB_HEADER_SIZE; |
| 193 |
if (nCurDepth == nTreeDepth) |
| 194 |
{ |
| 195 |
// Leaf node: it references feature IDs. |
| 196 |
|
| 197 |
// If we are about to go above m_nRowGroupSize, flush past |
| 198 |
// features now, to improve the spatial compacity of the row group. |
| 199 |
if (m_nRowGroupSize > nCellCount && |
| 200 |
nCountWrittenFeaturesSinceLastFlush + nCellCount > |
| 201 |
m_nRowGroupSize) |
| 202 |
{ |
| 203 |
nCountWrittenFeaturesSinceLastFlush = 0; |
| 204 |
if (!FlushFeatures()) |
| 205 |
{ |
| 206 |
return false; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
// nCellCount shouldn't be over 51 normally, but even 65535 |
| 211 |
// would be fine... |
| 212 |
assert(nCellCount <= 65535); |
| 213 |
for (int i = 0; i < nCellCount; ++i) |
| 214 |
{ |
| 215 |
int64_t nFID; |
| 216 |
memcpy(&nFID, pabyNodeData + nOffset, sizeof(int64_t)); |
| 217 |
CPL_MSBPTR64(&nFID); |
| 218 |
|
| 219 |
const auto poSrcFeature = std::unique_ptr<const OGRFeature>( |
| 220 |
m_poTmpGPKGLayer->GetFeature(nFID)); |
| 221 |
if (!poSrcFeature) |
| 222 |
{ |
| 223 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 224 |
"Cannot get feature " CPL_FRMT_GIB, |
| 225 |
static_cast<GIntBig>(nFID)); |
| 226 |
return false; |
| 227 |
} |
| 228 |
|
| 229 |
int nBytesFeature = 0; |
| 230 |
const GByte *pabyFeatureData = |
| 231 |
poSrcFeature->GetFieldAsBinary(0, &nBytesFeature); |
| 232 |
if (!oFeat.DeserializeFromBinary(pabyFeatureData, |
| 233 |
nBytesFeature)) |
| 234 |
{ |
| 235 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 236 |
"Cannot deserialize feature"); |
| 237 |
return false; |
| 238 |
} |
| 239 |
if (OGRArrowWriterLayer::ICreateFeature(&oFeat) != OGRERR_NONE) |
| 240 |
{ |
| 241 |
return false; |
| 242 |
} |
| 243 |
|
| 244 |
nOffset += SIZEOF_CELL; |
| 245 |
|
| 246 |
++nCountWrittenFeaturesSinceLastFlush; |
| 247 |
|
| 248 |
if ((m_nFeatureCount % PROGRESS_FC_INTERVAL) == 0 || |
| 249 |
m_nFeatureCount == m_nTmpFeatureCount / 2) |
| 250 |
{ |
| 251 |
CPLDebugProgress( |
| 252 |
"PARQUET", |
| 253 |
"CopyTmpGpkgLayerToFinalFile(): %.02f%% progress", |
| 254 |
100.0 * double(m_nFeatureCount) / |
| 255 |
double(m_nTmpFeatureCount)); |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
else |
| 260 |
{ |
| 261 |
// Non-leaf node: it references child nodes. |
| 262 |
|
| 263 |
// nCellCount shouldn't be over 51 normally, but even 65535 |
| 264 |
// would be fine... |
| 265 |
assert(nCellCount <= 65535); |
| 266 |
for (int i = 0; i < nCellCount; ++i) |
| 267 |
{ |
| 268 |
int64_t nNode; |
| 269 |
memcpy(&nNode, pabyNodeData + nOffset, sizeof(int64_t)); |
| 270 |
CPL_MSBPTR64(&nNode); |
| 271 |
aNodeNoDepthPair.emplace_back( |
| 272 |
std::make_pair(nNode, nCurDepth + 1)); |
| 273 |
nOffset += SIZEOF_CELL; |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
CPLDebug("PARQUET", |
| 279 |
"CopyTmpGpkgLayerToFinalFile(): 100%%, successfully finished"); |
| 280 |
return true; |
| 281 |
} |
| 282 |
|
| 283 |
/************************************************************************/ |
| 284 |
/* IsSupportedGeometryType() */ |
| 285 |
/************************************************************************/ |
| 286 |
|
| 287 |
bool OGRParquetWriterLayer::IsSupportedGeometryType( |
| 288 |
OGRwkbGeometryType eGType) const |
| 289 |
{ |
| 290 |
const auto eFlattenType = wkbFlatten(eGType); |
| 291 |
if (!OGR_GT_HasM(eGType) && eFlattenType <= wkbGeometryCollection) |
| 292 |
{ |
| 293 |
return true; |
| 294 |
} |
| 295 |
|
| 296 |
const auto osConfigOptionName = |
| 297 |
"OGR_" + GetDriverUCName() + "_ALLOW_ALL_DIMS"; |
| 298 |
if (CPLTestBool(CPLGetConfigOption(osConfigOptionName.c_str(), "NO"))) |
| 299 |
{ |
| 300 |
return true; |
| 301 |
} |
| 302 |
|
| 303 |
CPLError(CE_Failure, CPLE_NotSupported, |
| 304 |
"Only 2D and Z geometry types are supported (unless the " |
| 305 |
"%s configuration option is set to YES)", |
| 306 |
osConfigOptionName.c_str()); |
| 307 |
return false; |
| 308 |
} |
| 309 |
|
| 310 |
/************************************************************************/ |
| 311 |
/* SetOptions() */ |
| 312 |
/************************************************************************/ |
| 313 |
|
| 314 |
bool OGRParquetWriterLayer::SetOptions( |
| 315 |
const OGRGeomFieldDefn *poSrcGeomFieldDefn, CSLConstList papszOptions) |
| 316 |
{ |
| 317 |
m_aosCreationOptions = papszOptions; |
| 318 |
|
| 319 |
const char *pszWriteCoveringBBox = CSLFetchNameValueDef( |
| 320 |
papszOptions, "WRITE_COVERING_BBOX", |
| 321 |
CPLGetConfigOption("OGR_PARQUET_WRITE_COVERING_BBOX", nullptr)); |
| 322 |
m_bWriteBBoxStruct = |
| 323 |
pszWriteCoveringBBox == nullptr || CPLTestBool(pszWriteCoveringBBox); |
| 324 |
|
| 325 |
m_oBBoxStructFieldName = |
| 326 |
CSLFetchNameValueDef(papszOptions, "COVERING_BBOX_NAME", ""); |
| 327 |
|
| 328 |
if (CPLTestBool(CSLFetchNameValueDef(papszOptions, "SORT_BY_BBOX", "NO"))) |
| 329 |
{ |
| 330 |
const std::string osTmpGPKG(std::string(m_poDataset->GetDescription()) + |
| 331 |
".tmp.gpkg"); |
| 332 |
auto poGPKGDrv = GetGDALDriverManager()->GetDriverByName("GPKG"); |
| 333 |
if (!poGPKGDrv) |
| 334 |
{ |
| 335 |
CPLError( |
| 336 |
CE_Failure, CPLE_AppDefined, |
| 337 |
"Driver GPKG required for SORT_BY_BBOX layer creation option"); |
| 338 |
return false; |
| 339 |
} |
| 340 |
m_poTmpGPKG.reset(poGPKGDrv->Create(osTmpGPKG.c_str(), 0, 0, 0, |
| 341 |
GDT_Unknown, nullptr)); |
| 342 |
if (!m_poTmpGPKG) |
| 343 |
return false; |
| 344 |
m_poTmpGPKG->MarkSuppressOnClose(); |
| 345 |
m_poTmpGPKGLayer = m_poTmpGPKG->CreateLayer("tmp"); |
| 346 |
if (!m_poTmpGPKGLayer || |
| 347 |
// Serialized feature |
| 348 |
m_poTmpGPKGLayer->CreateField( |
| 349 |
std::make_unique<OGRFieldDefn>("serialized_feature", OFTBinary) |
| 350 |
.get()) != OGRERR_NONE || |
| 351 |
// FlushCache is needed to avoid SQLite3 errors on empty layers |
| 352 |
m_poTmpGPKG->FlushCache() != CE_None || |
| 353 |
m_poTmpGPKGLayer->StartTransaction() != OGRERR_NONE) |
| 354 |
{ |
| 355 |
return false; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
const char *pszGeomEncoding = |
| 360 |
CSLFetchNameValue(papszOptions, "GEOMETRY_ENCODING"); |
| 361 |
m_eGeomEncoding = OGRArrowGeomEncoding::WKB; |
| 362 |
if (pszGeomEncoding) |
| 363 |
{ |
| 364 |
if (EQUAL(pszGeomEncoding, "WKB")) |
| 365 |
m_eGeomEncoding = OGRArrowGeomEncoding::WKB; |
| 366 |
else if (EQUAL(pszGeomEncoding, "WKT")) |
| 367 |
m_eGeomEncoding = OGRArrowGeomEncoding::WKT; |
| 368 |
else if (EQUAL(pszGeomEncoding, "GEOARROW_INTERLEAVED")) |
| 369 |
{ |
| 370 |
CPLErrorOnce( |
| 371 |
CE_Warning, CPLE_AppDefined, |
| 372 |
"Use of GEOMETRY_ENCODING=GEOARROW_INTERLEAVED is not " |
| 373 |
"recommended. " |
| 374 |
"GeoParquet 1.1 uses GEOMETRY_ENCODING=GEOARROW (struct) " |
| 375 |
"instead."); |
| 376 |
m_eGeomEncoding = OGRArrowGeomEncoding::GEOARROW_FSL_GENERIC; |
| 377 |
} |
| 378 |
else if (EQUAL(pszGeomEncoding, "GEOARROW") || |
| 379 |
EQUAL(pszGeomEncoding, "GEOARROW_STRUCT")) |
| 380 |
m_eGeomEncoding = OGRArrowGeomEncoding::GEOARROW_STRUCT_GENERIC; |
| 381 |
else |
| 382 |
{ |
| 383 |
CPLError(CE_Failure, CPLE_NotSupported, |
| 384 |
"Unsupported GEOMETRY_ENCODING = %s", pszGeomEncoding); |
| 385 |
return false; |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
const char *pszCoordPrecision = |
| 390 |
CSLFetchNameValue(papszOptions, "COORDINATE_PRECISION"); |
| 391 |
if (pszCoordPrecision) |
| 392 |
m_nWKTCoordinatePrecision = atoi(pszCoordPrecision); |
| 393 |
|
| 394 |
m_bForceCounterClockwiseOrientation = |
| 395 |
EQUAL(CSLFetchNameValueDef(papszOptions, "POLYGON_ORIENTATION", |
| 396 |
"COUNTERCLOCKWISE"), |
| 397 |
"COUNTERCLOCKWISE"); |
| 398 |
|
| 399 |
const auto eGType = |
| 400 |
poSrcGeomFieldDefn ? poSrcGeomFieldDefn->GetType() : wkbNone; |
| 401 |
if (poSrcGeomFieldDefn && eGType != wkbNone) |
| 402 |
{ |
| 403 |
if (!IsSupportedGeometryType(eGType)) |
| 404 |
{ |
| 405 |
return false; |
| 406 |
} |
| 407 |
|
| 408 |
m_poFeatureDefn->SetGeomType(eGType); |
| 409 |
auto eGeomEncoding = m_eGeomEncoding; |
| 410 |
if (eGeomEncoding == OGRArrowGeomEncoding::GEOARROW_FSL_GENERIC || |
| 411 |
eGeomEncoding == OGRArrowGeomEncoding::GEOARROW_STRUCT_GENERIC) |
| 412 |
{ |
| 413 |
const auto eEncodingType = eGeomEncoding; |
| 414 |
eGeomEncoding = GetPreciseArrowGeomEncoding(eEncodingType, eGType); |
| 415 |
if (eGeomEncoding == eEncodingType) |
| 416 |
return false; |
| 417 |
} |
| 418 |
m_aeGeomEncoding.push_back(eGeomEncoding); |
| 419 |
|
| 420 |
std::string osGeometryName; |
| 421 |
const char *pszGeometryName = |
| 422 |
CSLFetchNameValue(papszOptions, "GEOMETRY_NAME"); |
| 423 |
if (pszGeometryName) |
| 424 |
osGeometryName = pszGeometryName; |
| 425 |
else if (poSrcGeomFieldDefn->GetNameRef()[0]) |
| 426 |
osGeometryName = poSrcGeomFieldDefn->GetNameRef(); |
| 427 |
else |
| 428 |
osGeometryName = "geometry"; |
| 429 |
m_poFeatureDefn->GetGeomFieldDefn(0)->SetName(osGeometryName.c_str()); |
| 430 |
|
| 431 |
const auto poSpatialRef = poSrcGeomFieldDefn->GetSpatialRef(); |
| 432 |
if (poSpatialRef) |
| 433 |
{ |
| 434 |
auto poSRS = poSpatialRef->Clone(); |
| 435 |
m_poFeatureDefn->GetGeomFieldDefn(0)->SetSpatialRef(poSRS); |
| 436 |
poSRS->Release(); |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
m_osFIDColumn = CSLFetchNameValueDef(papszOptions, "FID", ""); |
| 441 |
|
| 442 |
const char *pszCompression = CSLFetchNameValue(papszOptions, "COMPRESSION"); |
| 443 |
if (pszCompression == nullptr) |
| 444 |
{ |
| 445 |
auto oResult = arrow::util::Codec::GetCompressionType("snappy"); |
| 446 |
if (oResult.ok() && arrow::util::Codec::IsAvailable(*oResult)) |
| 447 |
{ |
| 448 |
pszCompression = "SNAPPY"; |
| 449 |
} |
| 450 |
else |
| 451 |
{ |
| 452 |
pszCompression = "NONE"; |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
if (EQUAL(pszCompression, "NONE")) |
| 457 |
pszCompression = "UNCOMPRESSED"; |
| 458 |
auto oResult = arrow::util::Codec::GetCompressionType( |
| 459 |
CPLString(pszCompression).tolower()); |
| 460 |
if (!oResult.ok()) |
| 461 |
{ |
| 462 |
CPLError(CE_Failure, CPLE_NotSupported, |
| 463 |
"Unrecognized compression method: %s", pszCompression); |
| 464 |
return false; |
| 465 |
} |
| 466 |
m_eCompression = *oResult; |
| 467 |
if (!arrow::util::Codec::IsAvailable(m_eCompression)) |
| 468 |
{ |
| 469 |
CPLError(CE_Failure, CPLE_NotSupported, |
| 470 |
"Compression method %s is known, but libarrow has not " |
| 471 |
"been built with support for it", |
| 472 |
pszCompression); |
| 473 |
return false; |
| 474 |
} |
| 475 |
m_oWriterPropertiesBuilder.compression(m_eCompression); |
| 476 |
|
| 477 |
const char *pszCompressionLevel = |
| 478 |
CSLFetchNameValue(papszOptions, "COMPRESSION_LEVEL"); |
| 479 |
if (pszCompressionLevel) |
| 480 |
{ |
| 481 |
const int nCompressionLevel = atoi(pszCompressionLevel); |
| 482 |
if (nCompressionLevel != DEFAULT_COMPRESSION_LEVEL) |
| 483 |
m_oWriterPropertiesBuilder.compression_level(nCompressionLevel); |
| 484 |
} |
| 485 |
else if (EQUAL(pszCompression, "ZSTD")) |
| 486 |
m_oWriterPropertiesBuilder.compression_level( |
| 487 |
OGR_PARQUET_ZSTD_DEFAULT_COMPRESSION_LEVEL); |
| 488 |
|
| 489 |
const std::string osCreator = |
| 490 |
CSLFetchNameValueDef(papszOptions, "CREATOR", ""); |
| 491 |
if (!osCreator.empty()) |
| 492 |
m_oWriterPropertiesBuilder.created_by(osCreator); |
| 493 |
else |
| 494 |
m_oWriterPropertiesBuilder.created_by("GDAL " GDAL_RELEASE_NAME |
| 495 |
", using " CREATED_BY_VERSION); |
| 496 |
|
| 497 |
// Undocumented option. Not clear it is useful besides unit test purposes |
| 498 |
if (!CPLTestBool(CSLFetchNameValueDef(papszOptions, "STATISTICS", "YES"))) |
| 499 |
m_oWriterPropertiesBuilder.disable_statistics(); |
| 500 |
|
| 501 |
#if PARQUET_VERSION_MAJOR >= 12 |
| 502 |
// Undocumented option. Not clear it is useful to disable it. |
| 503 |
if (CPLTestBool(CSLFetchNameValueDef(papszOptions, "PAGE_INDEX", "YES"))) |
| 504 |
m_oWriterPropertiesBuilder.enable_write_page_index(); |
| 505 |
#endif |
| 506 |
|
| 507 |
const char *pszWriteGeo = |
| 508 |
CPLGetConfigOption("OGR_PARQUET_WRITE_GEO", nullptr); |
| 509 |
m_bWriteGeoMetadata = pszWriteGeo == nullptr || CPLTestBool(pszWriteGeo); |
| 510 |
|
| 511 |
if (m_eGeomEncoding == OGRArrowGeomEncoding::WKB && eGType != wkbNone) |
| 512 |
{ |
| 513 |
#if ARROW_VERSION_MAJOR >= 21 |
| 514 |
const char *pszUseParquetGeoTypes = |
| 515 |
CSLFetchNameValueDef(papszOptions, "USE_PARQUET_GEO_TYPES", "NO"); |
| 516 |
if (EQUAL(pszUseParquetGeoTypes, "ONLY")) |
| 517 |
{ |
| 518 |
m_bUseArrowWKBExtension = true; |
| 519 |
if (pszWriteGeo == nullptr) |
| 520 |
m_bWriteGeoMetadata = false; |
| 521 |
if (pszWriteCoveringBBox == nullptr) |
| 522 |
m_bWriteBBoxStruct = false; |
| 523 |
} |
| 524 |
else |
| 525 |
{ |
| 526 |
m_bUseArrowWKBExtension = CPLTestBool(pszUseParquetGeoTypes); |
| 527 |
} |
| 528 |
#else |
| 529 |
m_oWriterPropertiesBuilder.disable_statistics( |
| 530 |
parquet::schema::ColumnPath::FromDotString( |
| 531 |
m_poFeatureDefn->GetGeomFieldDefn(0)->GetNameRef())); |
| 532 |
#endif |
| 533 |
} |
| 534 |
|
| 535 |
const char *pszRowGroupSize = |
| 536 |
CSLFetchNameValue(papszOptions, "ROW_GROUP_SIZE"); |
| 537 |
if (pszRowGroupSize) |
| 538 |
{ |
| 539 |
auto nRowGroupSize = static_cast<int64_t>(atoll(pszRowGroupSize)); |
| 540 |
if (nRowGroupSize > 0) |
| 541 |
{ |
| 542 |
if (nRowGroupSize > INT_MAX) |
| 543 |
nRowGroupSize = INT_MAX; |
| 544 |
m_nRowGroupSize = nRowGroupSize; |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
m_bEdgesSpherical = EQUAL( |
| 549 |
CSLFetchNameValueDef(papszOptions, "EDGES", "PLANAR"), "SPHERICAL"); |
| 550 |
|
| 551 |
m_bInitializationOK = true; |
| 552 |
return true; |
| 553 |
} |
| 554 |
|
| 555 |
/************************************************************************/ |
| 556 |
/* CloseFileWriter() */ |
| 557 |
/************************************************************************/ |
| 558 |
|
| 559 |
bool OGRParquetWriterLayer::CloseFileWriter() |
| 560 |
{ |
| 561 |
auto status = m_poFileWriter->Close(); |
| 562 |
if (!status.ok()) |
| 563 |
{ |
| 564 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 565 |
"FileWriter::Close() failed with %s", |
| 566 |
status.message().c_str()); |
| 567 |
} |
| 568 |
return status.ok(); |
| 569 |
} |
| 570 |
|
| 571 |
/************************************************************************/ |
| 572 |
/* GetGeoMetadata() */ |
| 573 |
/************************************************************************/ |
| 574 |
|
| 575 |
std::string OGRParquetWriterLayer::GetGeoMetadata() const |
| 576 |
{ |
| 577 |
// Just for unit testing purposes |
| 578 |
const char *pszGeoMetadata = |
| 579 |
CPLGetConfigOption("OGR_PARQUET_GEO_METADATA", nullptr); |
| 580 |
if (pszGeoMetadata) |
| 581 |
return pszGeoMetadata; |
| 582 |
|
| 583 |
if (m_poFeatureDefn->GetGeomFieldCount() != 0 && m_bWriteGeoMetadata) |
| 584 |
{ |
| 585 |
CPLJSONObject oRoot; |
| 586 |
oRoot.Add("version", "1.1.0"); |
| 587 |
oRoot.Add("primary_column", |
| 588 |
m_poFeatureDefn->GetGeomFieldDefn(0)->GetNameRef()); |
| 589 |
CPLJSONObject oColumns; |
| 590 |
oRoot.Add("columns", oColumns); |
| 591 |
for (int i = 0; i < m_poFeatureDefn->GetGeomFieldCount(); ++i) |
| 592 |
{ |
| 593 |
const auto poGeomFieldDefn = m_poFeatureDefn->GetGeomFieldDefn(i); |
| 594 |
CPLJSONObject oColumn; |
| 595 |
oColumns.Add(poGeomFieldDefn->GetNameRef(), oColumn); |
| 596 |
oColumn.Add("encoding", |
| 597 |
GetGeomEncodingAsString(m_aeGeomEncoding[i], true)); |
| 598 |
|
| 599 |
if (CPLTestBool(CPLGetConfigOption("OGR_PARQUET_WRITE_CRS", "YES"))) |
| 600 |
{ |
| 601 |
const auto poSRS = poGeomFieldDefn->GetSpatialRef(); |
| 602 |
if (poSRS) |
| 603 |
{ |
| 604 |
OGRSpatialReference oSRSIdentified(IdentifyCRS(poSRS)); |
| 605 |
|
| 606 |
const char *pszAuthName = oSRSIdentified.GetAuthorityName(); |
| 607 |
const char *pszAuthCode = oSRSIdentified.GetAuthorityCode(); |
| 608 |
|
| 609 |
bool bOmitCRS = false; |
| 610 |
if (pszAuthName != nullptr && pszAuthCode != nullptr && |
| 611 |
((EQUAL(pszAuthName, "EPSG") && |
| 612 |
EQUAL(pszAuthCode, "4326")) || |
| 613 |
(EQUAL(pszAuthName, "OGC") && |
| 614 |
EQUAL(pszAuthCode, "CRS84")))) |
| 615 |
{ |
| 616 |
// To make things less confusing for non-geo-aware |
| 617 |
// consumers, omit EPSG:4326 / OGC:CRS84 CRS by default |
| 618 |
bOmitCRS = CPLTestBool(CPLGetConfigOption( |
| 619 |
"OGR_PARQUET_CRS_OMIT_IF_WGS84", "YES")); |
| 620 |
} |
| 621 |
|
| 622 |
if (bOmitCRS) |
| 623 |
{ |
| 624 |
// do nothing |
| 625 |
} |
| 626 |
else if (EQUAL(CPLGetConfigOption( |
| 627 |
"OGR_PARQUET_CRS_ENCODING", "PROJJSON"), |
| 628 |
"PROJJSON")) |
| 629 |
{ |
| 630 |
// CRS encoded as PROJJSON for GeoParquet >= 0.4.0 |
| 631 |
char *pszPROJJSON = nullptr; |
| 632 |
oSRSIdentified.exportToPROJJSON(&pszPROJJSON, nullptr); |
| 633 |
CPLJSONDocument oCRSDoc; |
| 634 |
CPL_IGNORE_RET_VAL(oCRSDoc.LoadMemory(pszPROJJSON)); |
| 635 |
CPLFree(pszPROJJSON); |
| 636 |
CPLJSONObject oCRSRoot = oCRSDoc.GetRoot(); |
| 637 |
RemoveIDFromMemberOfEnsembles(oCRSRoot); |
| 638 |
oColumn.Add("crs", oCRSRoot); |
| 639 |
} |
| 640 |
else |
| 641 |
{ |
| 642 |
// WKT was used in GeoParquet <= 0.3.0 |
| 643 |
const char *const apszOptions[] = { |
| 644 |
"FORMAT=WKT2_2019", "MULTILINE=NO", nullptr}; |
| 645 |
char *pszWKT = nullptr; |
| 646 |
oSRSIdentified.exportToWkt(&pszWKT, apszOptions); |
| 647 |
if (pszWKT) |
| 648 |
oColumn.Add("crs", pszWKT); |
| 649 |
CPLFree(pszWKT); |
| 650 |
} |
| 651 |
|
| 652 |
const double dfCoordEpoch = poSRS->GetCoordinateEpoch(); |
| 653 |
if (dfCoordEpoch > 0) |
| 654 |
oColumn.Add("epoch", dfCoordEpoch); |
| 655 |
} |
| 656 |
else |
| 657 |
{ |
| 658 |
oColumn.AddNull("crs"); |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
if (m_bEdgesSpherical) |
| 663 |
{ |
| 664 |
oColumn.Add("edges", "spherical"); |
| 665 |
} |
| 666 |
|
| 667 |
if (m_aoEnvelopes[i].IsInit() && |
| 668 |
CPLTestBool( |
| 669 |
CPLGetConfigOption("OGR_PARQUET_WRITE_BBOX", "YES"))) |
| 670 |
{ |
| 671 |
bool bHasZ = false; |
| 672 |
for (const auto eGeomType : m_oSetWrittenGeometryTypes[i]) |
| 673 |
{ |
| 674 |
bHasZ = CPL_TO_BOOL(OGR_GT_HasZ(eGeomType)); |
| 675 |
if (bHasZ) |
| 676 |
break; |
| 677 |
} |
| 678 |
CPLJSONArray oBBOX; |
| 679 |
oBBOX.Add(m_aoEnvelopes[i].MinX); |
| 680 |
oBBOX.Add(m_aoEnvelopes[i].MinY); |
| 681 |
if (bHasZ) |
| 682 |
oBBOX.Add(m_aoEnvelopes[i].MinZ); |
| 683 |
oBBOX.Add(m_aoEnvelopes[i].MaxX); |
| 684 |
oBBOX.Add(m_aoEnvelopes[i].MaxY); |
| 685 |
if (bHasZ) |
| 686 |
oBBOX.Add(m_aoEnvelopes[i].MaxZ); |
| 687 |
oColumn.Add("bbox", oBBOX); |
| 688 |
} |
| 689 |
|
| 690 |
// Bounding box column definition |
| 691 |
if (m_bWriteBBoxStruct && |
| 692 |
CPLTestBool(CPLGetConfigOption( |
| 693 |
"OGR_PARQUET_WRITE_COVERING_BBOX_IN_METADATA", "YES"))) |
| 694 |
{ |
| 695 |
CPLJSONObject oCovering; |
| 696 |
oColumn.Add("covering", oCovering); |
| 697 |
CPLJSONObject oBBOX; |
| 698 |
oCovering.Add("bbox", oBBOX); |
| 699 |
const auto AddComponent = |
| 700 |
[this, i, &oBBOX](const char *pszComponent) |
| 701 |
{ |
| 702 |
CPLJSONArray oArray; |
| 703 |
oArray.Add(m_apoFieldsBBOX[i]->name()); |
| 704 |
oArray.Add(pszComponent); |
| 705 |
oBBOX.Add(pszComponent, oArray); |
| 706 |
}; |
| 707 |
AddComponent("xmin"); |
| 708 |
AddComponent("ymin"); |
| 709 |
AddComponent("xmax"); |
| 710 |
AddComponent("ymax"); |
| 711 |
} |
| 712 |
|
| 713 |
const auto GetStringGeometryType = [](OGRwkbGeometryType eType) |
| 714 |
{ |
| 715 |
const auto eFlattenType = wkbFlatten(eType); |
| 716 |
std::string osType = "Unknown"; |
| 717 |
if (wkbPoint == eFlattenType) |
| 718 |
osType = "Point"; |
| 719 |
else if (wkbLineString == eFlattenType) |
| 720 |
osType = "LineString"; |
| 721 |
else if (wkbPolygon == eFlattenType) |
| 722 |
osType = "Polygon"; |
| 723 |
else if (wkbMultiPoint == eFlattenType) |
| 724 |
osType = "MultiPoint"; |
| 725 |
else if (wkbMultiLineString == eFlattenType) |
| 726 |
osType = "MultiLineString"; |
| 727 |
else if (wkbMultiPolygon == eFlattenType) |
| 728 |
osType = "MultiPolygon"; |
| 729 |
else if (wkbGeometryCollection == eFlattenType) |
| 730 |
osType = "GeometryCollection"; |
| 731 |
if (osType != "Unknown") |
| 732 |
{ |
| 733 |
// M and ZM not supported officially currently, but it |
| 734 |
// doesn't hurt to anticipate |
| 735 |
if (OGR_GT_HasZ(eType) && OGR_GT_HasM(eType)) |
| 736 |
osType += " ZM"; |
| 737 |
else if (OGR_GT_HasZ(eType)) |
| 738 |
osType += " Z"; |
| 739 |
else if (OGR_GT_HasM(eType)) |
| 740 |
osType += " M"; |
| 741 |
} |
| 742 |
return osType; |
| 743 |
}; |
| 744 |
|
| 745 |
if (m_bForceCounterClockwiseOrientation) |
| 746 |
oColumn.Add("orientation", "counterclockwise"); |
| 747 |
|
| 748 |
CPLJSONArray oArray; |
| 749 |
for (const auto eType : m_oSetWrittenGeometryTypes[i]) |
| 750 |
{ |
| 751 |
oArray.Add(GetStringGeometryType(eType)); |
| 752 |
} |
| 753 |
oColumn.Add("geometry_types", oArray); |
| 754 |
} |
| 755 |
|
| 756 |
return oRoot.Format(CPLJSONObject::PrettyFormat::Plain); |
| 757 |
} |
| 758 |
return std::string(); |
| 759 |
} |
| 760 |
|
| 761 |
/************************************************************************/ |
| 762 |
/* PerformStepsBeforeFinalFlushGroup() */ |
| 763 |
/************************************************************************/ |
| 764 |
|
| 765 |
void OGRParquetWriterLayer::PerformStepsBeforeFinalFlushGroup() |
| 766 |
{ |
| 767 |
if (m_poKeyValueMetadata) |
| 768 |
{ |
| 769 |
std::string osGeoMetadata = GetGeoMetadata(); |
| 770 |
auto poTmpSchema = m_poSchema; |
| 771 |
if (!osGeoMetadata.empty()) |
| 772 |
{ |
| 773 |
// HACK: it would be good for Arrow to provide a clean way to alter |
| 774 |
// key value metadata before finalizing. |
| 775 |
// We need to write metadata at end to write the bounding box. |
| 776 |
const_cast<arrow::KeyValueMetadata *>(m_poKeyValueMetadata.get()) |
| 777 |
->Append("geo", osGeoMetadata); |
| 778 |
|
| 779 |
auto kvMetadata = poTmpSchema->metadata() |
| 780 |
? poTmpSchema->metadata()->Copy() |
| 781 |
: std::make_shared<arrow::KeyValueMetadata>(); |
| 782 |
kvMetadata->Append("geo", std::move(osGeoMetadata)); |
| 783 |
poTmpSchema = poTmpSchema->WithMetadata(kvMetadata); |
| 784 |
} |
| 785 |
|
| 786 |
if (CPLTestBool( |
| 787 |
CPLGetConfigOption("OGR_PARQUET_WRITE_ARROW_SCHEMA", "YES"))) |
| 788 |
{ |
| 789 |
auto status = |
| 790 |
::arrow::ipc::SerializeSchema(*poTmpSchema, m_poMemoryPool); |
| 791 |
if (status.ok()) |
| 792 |
{ |
| 793 |
// The serialized schema is not UTF-8, which is required for |
| 794 |
// Thrift |
| 795 |
const std::string schema_as_string = (*status)->ToString(); |
| 796 |
std::string schema_base64 = |
| 797 |
::arrow::util::base64_encode(schema_as_string); |
| 798 |
static const std::string kArrowSchemaKey = "ARROW:schema"; |
| 799 |
const_cast<arrow::KeyValueMetadata *>( |
| 800 |
m_poKeyValueMetadata.get()) |
| 801 |
->Append(kArrowSchemaKey, std::move(schema_base64)); |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
// Put GDAL metadata into a gdal:metadata domain |
| 806 |
CPLJSONObject oMultiMetadata; |
| 807 |
bool bHasMultiMetadata = false; |
| 808 |
auto &l_oMDMD = oMDMD.GetDomainList() && *(oMDMD.GetDomainList()) |
| 809 |
? oMDMD |
| 810 |
: m_poDataset->GetMultiDomainMetadata(); |
| 811 |
for (CSLConstList papszDomainIter = l_oMDMD.GetDomainList(); |
| 812 |
papszDomainIter && *papszDomainIter; ++papszDomainIter) |
| 813 |
{ |
| 814 |
const char *pszDomain = *papszDomainIter; |
| 815 |
CSLConstList papszMD = l_oMDMD.GetMetadata(pszDomain); |
| 816 |
if (STARTS_WITH(pszDomain, "json:") && papszMD && papszMD[0]) |
| 817 |
{ |
| 818 |
CPLJSONDocument oDoc; |
| 819 |
if (oDoc.LoadMemory(papszMD[0])) |
| 820 |
{ |
| 821 |
bHasMultiMetadata = true; |
| 822 |
oMultiMetadata.Add(pszDomain, oDoc.GetRoot()); |
| 823 |
continue; |
| 824 |
} |
| 825 |
} |
| 826 |
else if (STARTS_WITH(pszDomain, "xml:") && papszMD && papszMD[0]) |
| 827 |
{ |
| 828 |
bHasMultiMetadata = true; |
| 829 |
oMultiMetadata.Add(pszDomain, papszMD[0]); |
| 830 |
continue; |
| 831 |
} |
| 832 |
CPLJSONObject oMetadata; |
| 833 |
bool bHasMetadata = false; |
| 834 |
for (CSLConstList papszMDIter = papszMD; |
| 835 |
papszMDIter && *papszMDIter; ++papszMDIter) |
| 836 |
{ |
| 837 |
char *pszKey = nullptr; |
| 838 |
const char *pszValue = CPLParseNameValue(*papszMDIter, &pszKey); |
| 839 |
if (pszKey && pszValue) |
| 840 |
{ |
| 841 |
bHasMetadata = true; |
| 842 |
bHasMultiMetadata = true; |
| 843 |
oMetadata.Add(pszKey, pszValue); |
| 844 |
} |
| 845 |
CPLFree(pszKey); |
| 846 |
} |
| 847 |
if (bHasMetadata) |
| 848 |
oMultiMetadata.Add(pszDomain, oMetadata); |
| 849 |
} |
| 850 |
if (bHasMultiMetadata) |
| 851 |
{ |
| 852 |
const_cast<arrow::KeyValueMetadata *>(m_poKeyValueMetadata.get()) |
| 853 |
->Append( |
| 854 |
"gdal:metadata", |
| 855 |
oMultiMetadata.Format(CPLJSONObject::PrettyFormat::Plain)); |
| 856 |
} |
| 857 |
|
| 858 |
if (!m_aosCreationOptions.empty()) |
| 859 |
{ |
| 860 |
CPLJSONObject oCreationOptions; |
| 861 |
bool bEmpty = true; |
| 862 |
for (const auto &[key, value] : |
| 863 |
cpl::IterateNameValue(m_aosCreationOptions)) |
| 864 |
{ |
| 865 |
if (!EQUAL(key, "FID") && !EQUAL(key, "GEOMETRY_NAME") && |
| 866 |
!EQUAL(key, "EDGES")) |
| 867 |
{ |
| 868 |
bEmpty = false; |
| 869 |
oCreationOptions.Add(key, value); |
| 870 |
} |
| 871 |
} |
| 872 |
if (!bEmpty) |
| 873 |
{ |
| 874 |
const_cast<arrow::KeyValueMetadata *>( |
| 875 |
m_poKeyValueMetadata.get()) |
| 876 |
->Append("gdal:creation-options", |
| 877 |
oCreationOptions.Format( |
| 878 |
CPLJSONObject::PrettyFormat::Plain)); |
| 879 |
} |
| 880 |
} |
| 881 |
} |
| 882 |
} |
| 883 |
|
| 884 |
/************************************************************************/ |
| 885 |
/* Open() */ |
| 886 |
/************************************************************************/ |
| 887 |
|
| 888 |
// Same as parquet::arrow::FileWriter::Open(), except we also |
| 889 |
// return KeyValueMetadata |
| 890 |
static arrow::Status |
| 891 |
Open(const ::arrow::Schema &schema, ::arrow::MemoryPool *pool, |
| 892 |
std::shared_ptr<::arrow::io::OutputStream> sink, |
| 893 |
std::shared_ptr<parquet::WriterProperties> properties, |
| 894 |
std::shared_ptr<parquet::ArrowWriterProperties> arrow_properties, |
| 895 |
std::unique_ptr<parquet::arrow::FileWriter> *writer, |
| 896 |
std::shared_ptr<const arrow::KeyValueMetadata> *outMetadata) |
| 897 |
{ |
| 898 |
std::shared_ptr<parquet::SchemaDescriptor> parquet_schema; |
| 899 |
RETURN_NOT_OK(parquet::arrow::ToParquetSchema( |
| 900 |
&schema, *properties, *arrow_properties, &parquet_schema)); |
| 901 |
|
| 902 |
auto schema_node = std::static_pointer_cast<parquet::schema::GroupNode>( |
| 903 |
parquet_schema->schema_root()); |
| 904 |
|
| 905 |
auto metadata = schema.metadata() |
| 906 |
? schema.metadata()->Copy() |
| 907 |
: std::make_shared<arrow::KeyValueMetadata>(); |
| 908 |
*outMetadata = metadata; |
| 909 |
|
| 910 |
std::unique_ptr<parquet::ParquetFileWriter> base_writer; |
| 911 |
PARQUET_CATCH_NOT_OK(base_writer = parquet::ParquetFileWriter::Open( |
| 912 |
std::move(sink), std::move(schema_node), |
| 913 |
std::move(properties), metadata)); |
| 914 |
|
| 915 |
auto schema_ptr = std::make_shared<::arrow::Schema>(schema); |
| 916 |
return parquet::arrow::FileWriter::Make( |
| 917 |
pool, std::move(base_writer), std::move(schema_ptr), |
| 918 |
std::move(arrow_properties), writer); |
| 919 |
} |
| 920 |
|
| 921 |
/************************************************************************/ |
| 922 |
/* CreateSchema() */ |
| 923 |
/************************************************************************/ |
| 924 |
|
| 925 |
void OGRParquetWriterLayer::CreateSchema() |
| 926 |
{ |
| 927 |
CreateSchemaCommon(); |
| 928 |
} |
| 929 |
|
| 930 |
/************************************************************************/ |
| 931 |
/* CreateGeomField() */ |
| 932 |
/************************************************************************/ |
| 933 |
|
| 934 |
OGRErr OGRParquetWriterLayer::CreateGeomField(const OGRGeomFieldDefn *poField, |
| 935 |
int bApproxOK) |
| 936 |
{ |
| 937 |
OGRErr eErr = OGRArrowWriterLayer::CreateGeomField(poField, bApproxOK); |
| 938 |
if (eErr == OGRERR_NONE && |
| 939 |
m_aeGeomEncoding.back() == OGRArrowGeomEncoding::WKB |
| 940 |
#if ARROW_VERSION_MAJOR < 21 |
| 941 |
// Geostatistics in Arrow 21 do not support geographic type for now |
| 942 |
&& m_bEdgesSpherical |
| 943 |
#endif |
| 944 |
) |
| 945 |
{ |
| 946 |
m_oWriterPropertiesBuilder.disable_statistics( |
| 947 |
parquet::schema::ColumnPath::FromDotString( |
| 948 |
m_poFeatureDefn |
| 949 |
->GetGeomFieldDefn(m_poFeatureDefn->GetGeomFieldCount() - 1) |
| 950 |
->GetNameRef())); |
| 951 |
} |
| 952 |
return eErr; |
| 953 |
} |
| 954 |
|
| 955 |
/************************************************************************/ |
| 956 |
/* CreateWriter() */ |
| 957 |
/************************************************************************/ |
| 958 |
|
| 959 |
void OGRParquetWriterLayer::CreateWriter() |
| 960 |
{ |
| 961 |
CPLAssert(m_poFileWriter == nullptr); |
| 962 |
|
| 963 |
if (m_poSchema == nullptr) |
| 964 |
{ |
| 965 |
CreateSchema(); |
| 966 |
} |
| 967 |
else |
| 968 |
{ |
| 969 |
FinalizeSchema(); |
| 970 |
} |
| 971 |
|
| 972 |
auto arrowWriterProperties = |
| 973 |
parquet::ArrowWriterProperties::Builder().store_schema()->build(); |
| 974 |
CPL_IGNORE_RET_VAL(Open(*m_poSchema, m_poMemoryPool, m_poOutputStream, |
| 975 |
m_oWriterPropertiesBuilder.build(), |
| 976 |
std::move(arrowWriterProperties), &m_poFileWriter, |
| 977 |
&m_poKeyValueMetadata)); |
| 978 |
} |
| 979 |
|
| 980 |
/************************************************************************/ |
| 981 |
/* ICreateFeature() */ |
| 982 |
/************************************************************************/ |
| 983 |
|
| 984 |
OGRErr OGRParquetWriterLayer::ICreateFeature(OGRFeature *poFeature) |
| 985 |
{ |
| 986 |
// If not using SORT_BY_BBOX=YES layer creation option, we can directly |
| 987 |
// write features to the final Parquet file |
| 988 |
if (!m_poTmpGPKGLayer) |
| 989 |
return OGRArrowWriterLayer::ICreateFeature(poFeature); |
| 990 |
|
| 991 |
// SORT_BY_BBOX=YES case: we write for now a serialized version of poFeature |
| 992 |
// in a temporary GeoPackage file. |
| 993 |
|
| 994 |
GIntBig nFID = poFeature->GetFID(); |
| 995 |
if (!m_osFIDColumn.empty() && nFID == OGRNullFID) |
| 996 |
{ |
| 997 |
nFID = m_nTmpFeatureCount; |
| 998 |
poFeature->SetFID(nFID); |
| 999 |
} |
| 1000 |
++m_nTmpFeatureCount; |
| 1001 |
|
| 1002 |
std::vector<GByte> abyBuffer; |
| 1003 |
// Serialize the source feature as a single array of bytes to preserve it |
| 1004 |
// fully |
| 1005 |
if (!poFeature->SerializeToBinary(abyBuffer)) |
| 1006 |
{ |
| 1007 |
return OGRERR_FAILURE; |
| 1008 |
} |
| 1009 |
|
| 1010 |
// SQLite3 limitation: a row must fit in slightly less than 1 GB. |
| 1011 |
constexpr int SOME_MARGIN = 128; |
| 1012 |
if (abyBuffer.size() > 1024 * 1024 * 1024 - SOME_MARGIN) |
| 1013 |
{ |
| 1014 |
CPLError(CE_Failure, CPLE_NotSupported, |
| 1015 |
"Features larger than 1 GB are not supported"); |
| 1016 |
return OGRERR_FAILURE; |
| 1017 |
} |
| 1018 |
|
| 1019 |
OGRFeature oFeat(m_poTmpGPKGLayer->GetLayerDefn()); |
| 1020 |
oFeat.SetFID(nFID); |
| 1021 |
oFeat.SetField(0, static_cast<int>(abyBuffer.size()), abyBuffer.data()); |
| 1022 |
const auto poSrcGeom = poFeature->GetGeometryRef(); |
| 1023 |
if (poSrcGeom && !poSrcGeom->IsEmpty()) |
| 1024 |
{ |
| 1025 |
// For the purpose of building an RTree, just use the bounding box of |
| 1026 |
// the geometry as the geometry. |
| 1027 |
OGREnvelope sEnvelope; |
| 1028 |
poSrcGeom->getEnvelope(&sEnvelope); |
| 1029 |
auto poPoly = std::make_unique<OGRPolygon>(); |
| 1030 |
auto poLR = std::make_unique<OGRLinearRing>(); |
| 1031 |
poLR->addPoint(sEnvelope.MinX, sEnvelope.MinY); |
| 1032 |
poLR->addPoint(sEnvelope.MinX, sEnvelope.MaxY); |
| 1033 |
poLR->addPoint(sEnvelope.MaxX, sEnvelope.MaxY); |
| 1034 |
poLR->addPoint(sEnvelope.MaxX, sEnvelope.MinY); |
| 1035 |
poLR->addPoint(sEnvelope.MinX, sEnvelope.MinY); |
| 1036 |
poPoly->addRingDirectly(poLR.release()); |
| 1037 |
oFeat.SetGeometryDirectly(poPoly.release()); |
| 1038 |
} |
| 1039 |
return m_poTmpGPKGLayer->CreateFeature(&oFeat); |
| 1040 |
} |
| 1041 |
|
| 1042 |
/************************************************************************/ |
| 1043 |
/* FlushGroup() */ |
| 1044 |
/************************************************************************/ |
| 1045 |
|
| 1046 |
bool OGRParquetWriterLayer::FlushGroup() |
| 1047 |
{ |
| 1048 |
#if PARQUET_VERSION_MAJOR >= 20 |
| 1049 |
auto status = m_poFileWriter->NewRowGroup(); |
| 1050 |
#else |
| 1051 |
auto status = m_poFileWriter->NewRowGroup(m_apoBuilders[0]->length()); |
| 1052 |
#endif |
| 1053 |
if (!status.ok()) |
| 1054 |
{ |
| 1055 |
CPLError(CE_Failure, CPLE_AppDefined, "NewRowGroup() failed with %s", |
| 1056 |
status.message().c_str()); |
| 1057 |
ClearArrayBuilers(); |
| 1058 |
return false; |
| 1059 |
} |
| 1060 |
|
| 1061 |
auto ret = WriteArrays( |
| 1062 |
[this](const std::shared_ptr<arrow::Field> &field, |
| 1063 |
const std::shared_ptr<arrow::Array> &array) |
| 1064 |
{ |
| 1065 |
auto l_status = m_poFileWriter->WriteColumnChunk(*array); |
| 1066 |
if (!l_status.ok()) |
| 1067 |
{ |
| 1068 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 1069 |
"WriteColumnChunk() failed for field %s: %s", |
| 1070 |
field->name().c_str(), l_status.message().c_str()); |
| 1071 |
return false; |
| 1072 |
} |
| 1073 |
return true; |
| 1074 |
}); |
| 1075 |
|
| 1076 |
ClearArrayBuilers(); |
| 1077 |
return ret; |
| 1078 |
} |
| 1079 |
|
| 1080 |
/************************************************************************/ |
| 1081 |
/* FixupWKBGeometryBeforeWriting() */ |
| 1082 |
/************************************************************************/ |
| 1083 |
|
| 1084 |
void OGRParquetWriterLayer::FixupWKBGeometryBeforeWriting(GByte *pabyWkb, |
| 1085 |
size_t nLen) |
| 1086 |
{ |
| 1087 |
if (!m_bForceCounterClockwiseOrientation) |
| 1088 |
return; |
| 1089 |
|
| 1090 |
OGRWKBFixupCounterClockWiseExternalRing(pabyWkb, nLen); |
| 1091 |
} |
| 1092 |
|
| 1093 |
/************************************************************************/ |
| 1094 |
/* FixupGeometryBeforeWriting() */ |
| 1095 |
/************************************************************************/ |
| 1096 |
|
| 1097 |
void OGRParquetWriterLayer::FixupGeometryBeforeWriting(OGRGeometry *poGeom) |
| 1098 |
{ |
| 1099 |
if (!m_bForceCounterClockwiseOrientation) |
| 1100 |
return; |
| 1101 |
|
| 1102 |
const auto eFlattenType = wkbFlatten(poGeom->getGeometryType()); |
| 1103 |
// Polygon rings MUST follow the right-hand rule for orientation |
| 1104 |
// (counterclockwise external rings, clockwise internal rings) |
| 1105 |
if (eFlattenType == wkbPolygon) |
| 1106 |
{ |
| 1107 |
bool bFirstRing = true; |
| 1108 |
for (auto poRing : poGeom->toPolygon()) |
| 1109 |
{ |
| 1110 |
if ((bFirstRing && poRing->isClockwise()) || |
| 1111 |
(!bFirstRing && !poRing->isClockwise())) |
| 1112 |
{ |
| 1113 |
poRing->reversePoints(); |
| 1114 |
} |
| 1115 |
bFirstRing = false; |
| 1116 |
} |
| 1117 |
} |
| 1118 |
else if (eFlattenType == wkbMultiPolygon || |
| 1119 |
eFlattenType == wkbGeometryCollection) |
| 1120 |
{ |
| 1121 |
for (auto poSubGeom : poGeom->toGeometryCollection()) |
| 1122 |
{ |
| 1123 |
FixupGeometryBeforeWriting(poSubGeom); |
| 1124 |
} |
| 1125 |
} |
| 1126 |
} |
| 1127 |
|
| 1128 |
/************************************************************************/ |
| 1129 |
/* WriteArrowBatch() */ |
| 1130 |
/************************************************************************/ |
| 1131 |
|
| 1132 |
#if PARQUET_VERSION_MAJOR > 10 |
| 1133 |
inline bool |
| 1134 |
OGRParquetWriterLayer::WriteArrowBatch(const struct ArrowSchema *schema, |
| 1135 |
struct ArrowArray *array, |
| 1136 |
CSLConstList papszOptions) |
| 1137 |
{ |
| 1138 |
if (m_poTmpGPKGLayer) |
| 1139 |
{ |
| 1140 |
// When using SORT_BY_BBOX=YES option, we can't directly write the |
| 1141 |
// input array, because we need to sort features. Hence we fallback |
| 1142 |
// to the OGRLayer base implementation, which will ultimately call |
| 1143 |
// OGRParquetWriterLayer::ICreateFeature() |
| 1144 |
return OGRLayer::WriteArrowBatch(schema, array, papszOptions); |
| 1145 |
} |
| 1146 |
|
| 1147 |
return WriteArrowBatchInternal( |
| 1148 |
schema, array, papszOptions, |
| 1149 |
[this](const std::shared_ptr<arrow::RecordBatch> &poBatch) |
| 1150 |
{ |
| 1151 |
auto status = m_poFileWriter->NewBufferedRowGroup(); |
| 1152 |
if (!status.ok()) |
| 1153 |
{ |
| 1154 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 1155 |
"NewBufferedRowGroup() failed with %s", |
| 1156 |
status.message().c_str()); |
| 1157 |
return false; |
| 1158 |
} |
| 1159 |
|
| 1160 |
status = m_poFileWriter->WriteRecordBatch(*poBatch); |
| 1161 |
if (!status.ok()) |
| 1162 |
{ |
| 1163 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 1164 |
"WriteRecordBatch() failed: %s", |
| 1165 |
status.message().c_str()); |
| 1166 |
return false; |
| 1167 |
} |
| 1168 |
|
| 1169 |
return true; |
| 1170 |
}); |
| 1171 |
} |
| 1172 |
#endif |
| 1173 |
|
| 1174 |
/************************************************************************/ |
| 1175 |
/* TestCapability() */ |
| 1176 |
/************************************************************************/ |
| 1177 |
|
| 1178 |
inline int OGRParquetWriterLayer::TestCapability(const char *pszCap) const |
| 1179 |
{ |
| 1180 |
#if PARQUET_VERSION_MAJOR <= 10 |
| 1181 |
if (EQUAL(pszCap, OLCFastWriteArrowBatch)) |
| 1182 |
return false; |
| 1183 |
#endif |
| 1184 |
|
| 1185 |
if (m_poTmpGPKGLayer && EQUAL(pszCap, OLCFastWriteArrowBatch)) |
| 1186 |
{ |
| 1187 |
// When using SORT_BY_BBOX=YES option, we can't directly write the |
| 1188 |
// input array, because we need to sort features. So this is not |
| 1189 |
// fast |
| 1190 |
return false; |
| 1191 |
} |
| 1192 |
|
| 1193 |
return OGRArrowWriterLayer::TestCapability(pszCap); |
| 1194 |
} |
| 1195 |
|
| 1196 |
/************************************************************************/ |
| 1197 |
/* CreateFieldFromArrowSchema() */ |
| 1198 |
/************************************************************************/ |
| 1199 |
|
| 1200 |
#if PARQUET_VERSION_MAJOR > 10 |
| 1201 |
bool OGRParquetWriterLayer::CreateFieldFromArrowSchema( |
| 1202 |
const struct ArrowSchema *schema, CSLConstList papszOptions) |
| 1203 |
{ |
| 1204 |
if (m_poTmpGPKGLayer) |
| 1205 |
{ |
| 1206 |
// When using SORT_BY_BBOX=YES option, we can't directly write the |
| 1207 |
// input array, because we need to sort features. But this process |
| 1208 |
// only supports the base Arrow types supported by |
| 1209 |
// OGRLayer::WriteArrowBatch() |
| 1210 |
return OGRLayer::CreateFieldFromArrowSchema(schema, papszOptions); |
| 1211 |
} |
| 1212 |
|
| 1213 |
return OGRArrowWriterLayer::CreateFieldFromArrowSchema(schema, |
| 1214 |
papszOptions); |
| 1215 |
} |
| 1216 |
#endif |
| 1217 |
|
| 1218 |
/************************************************************************/ |
| 1219 |
/* IsArrowSchemaSupported() */ |
| 1220 |
/************************************************************************/ |
| 1221 |
|
| 1222 |
#if PARQUET_VERSION_MAJOR > 10 |
| 1223 |
bool OGRParquetWriterLayer::IsArrowSchemaSupported( |
| 1224 |
const struct ArrowSchema *schema, CSLConstList papszOptions, |
| 1225 |
std::string &osErrorMsg) const |
| 1226 |
{ |
| 1227 |
if (m_poTmpGPKGLayer) |
| 1228 |
{ |
| 1229 |
// When using SORT_BY_BBOX=YES option, we can't directly write the |
| 1230 |
// input array, because we need to sort features. But this process |
| 1231 |
// only supports the base Arrow types supported by |
| 1232 |
// OGRLayer::WriteArrowBatch() |
| 1233 |
return OGRLayer::IsArrowSchemaSupported(schema, papszOptions, |
| 1234 |
osErrorMsg); |
| 1235 |
} |
| 1236 |
|
| 1237 |
if (schema->format[0] == 'e' && schema->format[1] == 0) |
| 1238 |
{ |
| 1239 |
osErrorMsg = "float16 not supported"; |
| 1240 |
return false; |
| 1241 |
} |
| 1242 |
if (schema->format[0] == 'v' && schema->format[1] == 'u') |
| 1243 |
{ |
| 1244 |
osErrorMsg = "StringView not supported"; |
| 1245 |
return false; |
| 1246 |
} |
| 1247 |
if (schema->format[0] == 'v' && schema->format[1] == 'z') |
| 1248 |
{ |
| 1249 |
osErrorMsg = "BinaryView not supported"; |
| 1250 |
return false; |
| 1251 |
} |
| 1252 |
if (schema->format[0] == '+' && schema->format[1] == 'v') |
| 1253 |
{ |
| 1254 |
if (schema->format[2] == 'l') |
| 1255 |
{ |
| 1256 |
osErrorMsg = "ListView not supported"; |
| 1257 |
return false; |
| 1258 |
} |
| 1259 |
else if (schema->format[2] == 'L') |
| 1260 |
{ |
| 1261 |
osErrorMsg = "LargeListView not supported"; |
| 1262 |
return false; |
| 1263 |
} |
| 1264 |
} |
| 1265 |
for (int64_t i = 0; i < schema->n_children; ++i) |
| 1266 |
{ |
| 1267 |
if (!IsArrowSchemaSupported(schema->children[i], papszOptions, |
| 1268 |
osErrorMsg)) |
| 1269 |
{ |
| 1270 |
return false; |
| 1271 |
} |
| 1272 |
} |
| 1273 |
return true; |
| 1274 |
} |
| 1275 |
#endif |
| 1276 |
|
| 1277 |
/************************************************************************/ |
| 1278 |
/* SetMetadata() */ |
| 1279 |
/************************************************************************/ |
| 1280 |
|
| 1281 |
CPLErr OGRParquetWriterLayer::SetMetadata(CSLConstList papszMetadata, |
| 1282 |
const char *pszDomain) |
| 1283 |
{ |
| 1284 |
if (!pszDomain || !EQUAL(pszDomain, "SHAPEFILE")) |
| 1285 |
{ |
| 1286 |
return OGRLayer::SetMetadata(papszMetadata, pszDomain); |
| 1287 |
} |
| 1288 |
return CE_None; |
| 1289 |
} |
| 1290 |
|
| 1291 |
/************************************************************************/ |
| 1292 |
/* GetDataset() */ |
| 1293 |
/************************************************************************/ |
| 1294 |
|
| 1295 |
GDALDataset *OGRParquetWriterLayer::GetDataset() |
| 1296 |
{ |
| 1297 |
return m_poDataset; |
| 1298 |
} |