1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
|
/******************************************************************************
*
* Project: OpenGIS Simple Features Reference Implementation
* Purpose: Program to generate a UMN MapServer compatible tile index for a
* set of OGR data sources.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 2002, Frank Warmerdam
* Copyright (c) 2007-2010, Even Rouault <even dot rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_port.h"
#include <cassert>
#include <vector>
#include "cpl_conv.h"
#include "cpl_string.h"
#include "gdal_version.h"
#include "ogr_api.h"
#include "ogrsf_frmts.h"
#include "commonutils.h"
typedef enum
{
FORMAT_AUTO,
FORMAT_WKT,
FORMAT_EPSG,
FORMAT_PROJ
} SrcSRSFormat;
/************************************************************************/
/* Usage() */
/************************************************************************/
static void Usage()
{
printf(
"Usage: ogrtindex [-lnum n]... [-lname name]... [-f output_format]\n"
" [-write_absolute_path] [-skip_different_projection]\n"
" [-t_srs target_srs]\n"
" [-src_srs_name field_name] [-src_srs_format "
"[AUTO|WKT|EPSG|PROJ]\n"
" [-accept_different_schemas]\n"
" output_dataset src_dataset...\n");
printf("\n");
printf(" -lnum n: Add layer number 'n' from each source file\n"
" in the tile index.\n");
printf(" -lname name: Add the layer named 'name' from each source file\n"
" in the tile index.\n");
printf(" -f output_format: Select an output format name.\n");
printf(" -tileindex field_name: The name to use for the dataset name.\n"
" Defaults to LOCATION.\n");
printf(
" -write_absolute_path: Filenames are written with absolute paths.\n");
printf(
" -skip_different_projection: Only layers with same projection ref \n"
" as layers already inserted in the tileindex will be "
"inserted.\n");
printf(" -accept_different_schemas: by default ogrtindex checks that all "
"layers inserted\n"
" into the index have the same "
"attribute schemas. If you\n"
" specify this option, this test will "
"be disabled. Be aware that\n"
" resulting index may be incompatible "
"with MapServer!\n");
printf(" - If -t_srs is specified, geometries of input files will be "
"transformed to the desired\n"
" target coordinate reference system.\n"
" Note that using this option generates files that are NOT "
"compatible with MapServer < 7.2.\n"
" - Simple rectangular polygons are generated in the same "
"coordinate reference system\n"
" as the vectors, or in target reference system if the -t_srs "
"option is used.\n");
printf("\n");
printf("If no -lnum or -lname arguments are given it is assumed that\n"
"all layers in source datasets should be added to the tile index\n"
"as independent records.\n");
exit(1);
}
/************************************************************************/
/* main() */
/************************************************************************/
MAIN_START(nArgc, papszArgv)
{
// Check strict compilation and runtime library version as we use C++ API.
if (!GDAL_CHECK_VERSION(papszArgv[0]))
exit(1);
/* -------------------------------------------------------------------- */
/* Register format(s). */
/* -------------------------------------------------------------------- */
OGRRegisterAll();
/* -------------------------------------------------------------------- */
/* Processing command line arguments. */
/* -------------------------------------------------------------------- */
int nFirstSourceDataset = -1;
bool bLayersWildcarded = true;
const char *pszFormat = nullptr;
const char *pszTileIndexField = "LOCATION";
const char *pszOutputName = nullptr;
bool write_absolute_path = false;
bool skip_different_projection = false;
char *current_path = nullptr;
bool accept_different_schemas = false;
bool bFirstWarningForNonMatchingAttributes = true;
const char *pszTargetSRS = "";
bool bSetTargetSRS = false;
const char *pszSrcSRSName = nullptr;
int i_SrcSRSName = -1;
bool bSrcSRSFormatSpecified = false;
SrcSRSFormat eSrcSRSFormat = FORMAT_AUTO;
size_t nMaxFieldSize = 254;
for (int iArg = 1; iArg < nArgc; iArg++)
{
if (EQUAL(papszArgv[iArg], "--utility_version"))
{
printf("%s was compiled against GDAL %s and "
"is running against GDAL %s\n",
papszArgv[0], GDAL_RELEASE_NAME,
GDALVersionInfo("RELEASE_NAME"));
return 0;
}
else if (iArg < nArgc - 1 && (EQUAL(papszArgv[iArg], "-f") ||
EQUAL(papszArgv[iArg], "-of")))
{
pszFormat = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "-write_absolute_path"))
{
write_absolute_path = true;
}
else if (EQUAL(papszArgv[iArg], "-skip_different_projection"))
{
skip_different_projection = true;
}
else if (EQUAL(papszArgv[iArg], "-accept_different_schemas"))
{
accept_different_schemas = true;
}
else if (iArg < nArgc - 1 && EQUAL(papszArgv[iArg], "-tileindex"))
{
pszTileIndexField = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "-lnum") ||
EQUAL(papszArgv[iArg], "-lname"))
{
iArg++;
bLayersWildcarded = false;
}
else if (iArg < nArgc - 1 && strcmp(papszArgv[iArg], "-t_srs") == 0)
{
pszTargetSRS = papszArgv[++iArg];
bSetTargetSRS = true;
}
else if (iArg < nArgc - 1 &&
strcmp(papszArgv[iArg], "-src_srs_name") == 0)
{
pszSrcSRSName = papszArgv[++iArg];
}
else if (iArg < nArgc - 1 &&
strcmp(papszArgv[iArg], "-src_srs_format") == 0)
{
bSrcSRSFormatSpecified = true;
const char *pszSRSFormat = papszArgv[++iArg];
if (EQUAL(pszSRSFormat, "AUTO"))
eSrcSRSFormat = FORMAT_AUTO;
else if (EQUAL(pszSRSFormat, "WKT"))
eSrcSRSFormat = FORMAT_WKT;
else if (EQUAL(pszSRSFormat, "EPSG"))
eSrcSRSFormat = FORMAT_EPSG;
else if (EQUAL(pszSRSFormat, "PROJ"))
eSrcSRSFormat = FORMAT_PROJ;
}
else if (papszArgv[iArg][0] == '-')
{
Usage();
}
else if (pszOutputName == nullptr)
{
pszOutputName = papszArgv[iArg];
}
else if (nFirstSourceDataset == -1)
{
nFirstSourceDataset = iArg;
}
}
if (pszOutputName == nullptr || nFirstSourceDataset == -1)
Usage();
if (bSrcSRSFormatSpecified && pszSrcSRSName == nullptr)
{
fprintf(stderr,
"-src_srs_name must be specified when -src_srs_format is "
"specified.\n");
Usage();
}
/* -------------------------------------------------------------------- */
/* Create and validate target SRS if given. */
/* -------------------------------------------------------------------- */
OGRSpatialReference *poTargetSRS = nullptr;
if (bSetTargetSRS)
{
if (skip_different_projection)
{
fprintf(stderr,
"Warning : -skip_different_projection does not apply "
"when -t_srs is requested.\n");
}
poTargetSRS = new OGRSpatialReference();
poTargetSRS->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
// coverity[tainted_data]
if (poTargetSRS->SetFromUserInput(pszTargetSRS) != CE_None)
{
delete poTargetSRS;
fprintf(stderr, "Invalid target SRS `%s'.\n", pszTargetSRS);
exit(1);
}
}
/* -------------------------------------------------------------------- */
/* Try to open as an existing dataset for update access. */
/* -------------------------------------------------------------------- */
GDALDataset *poDstDS =
reinterpret_cast<GDALDataset *>(OGROpen(pszOutputName, TRUE, nullptr));
/* -------------------------------------------------------------------- */
/* If that failed, find the driver so we can create the tile index.*/
/* -------------------------------------------------------------------- */
OGRLayer *poDstLayer = nullptr;
if (poDstDS == nullptr)
{
CPLString osFormat;
if (pszFormat == nullptr)
{
const std::vector<CPLString> aoDrivers =
GetOutputDriversFor(pszOutputName, GDAL_OF_VECTOR);
if (aoDrivers.empty())
{
CPLError(CE_Failure, CPLE_AppDefined,
"Cannot guess driver for %s", pszOutputName);
exit(10);
}
else
{
if (aoDrivers.size() > 1)
{
CPLError(CE_Warning, CPLE_AppDefined,
"Several drivers matching %s extension. Using %s",
CPLGetExtension(pszOutputName),
aoDrivers[0].c_str());
}
osFormat = aoDrivers[0];
}
}
else
{
osFormat = pszFormat;
}
if (!EQUAL(osFormat, "ESRI Shapefile"))
nMaxFieldSize = 0;
GDALDriverH hDriver = GDALGetDriverByName(osFormat.c_str());
if (hDriver == nullptr)
{
GDALDriverManager *poDM = GetGDALDriverManager();
for (int iDriver = 0; iDriver < poDM->GetDriverCount(); iDriver++)
{
fprintf(stderr, "Unable to find driver `%s'.\n",
osFormat.c_str());
fprintf(stderr, "The following drivers are available:\n");
GDALDriver *poIter = poDM->GetDriver(iDriver);
char **papszDriverMD = poIter->GetMetadata();
if (CPLTestBool(CSLFetchNameValueDef(
papszDriverMD, GDAL_DCAP_VECTOR, "FALSE")) &&
CPLTestBool(CSLFetchNameValueDef(
papszDriverMD, GDAL_DCAP_CREATE, "FALSE")))
{
fprintf(stderr, " -> `%s'\n", poIter->GetDescription());
}
}
exit(1);
}
if (!CPLTestBool(CSLFetchNameValueDef(GDALGetMetadata(hDriver, nullptr),
GDAL_DCAP_CREATE, "FALSE")))
{
fprintf(stderr,
"%s driver does not support data source creation.\n",
osFormat.c_str());
exit(1);
}
/* --------------------------------------------------------------------
*/
/* Now create it. */
/* --------------------------------------------------------------------
*/
poDstDS = reinterpret_cast<GDALDataset *>(
GDALCreate(hDriver, pszOutputName, 0, 0, 0, GDT_Unknown, nullptr));
if (poDstDS == nullptr)
{
fprintf(stderr, "%s driver failed to create %s\n", osFormat.c_str(),
pszOutputName);
exit(1);
}
if (poDstDS->GetLayerCount() == 0)
{
if (nFirstSourceDataset < nArgc &&
papszArgv[nFirstSourceDataset][0] == '-')
{
nFirstSourceDataset++;
}
OGRSpatialReference *poSrcSpatialRef = nullptr;
if (bSetTargetSRS)
{
// Fetches the SRS from target SRS (if set), or from the SRS of
// the first layer and use it when creating the
// tileindex layer.
poSrcSpatialRef = poTargetSRS->Clone();
}
else if (nFirstSourceDataset < nArgc)
{
GDALDataset *poDS = reinterpret_cast<GDALDataset *>(
OGROpen(papszArgv[nFirstSourceDataset], FALSE, nullptr));
if (poDS != nullptr)
{
for (int iLayer = 0; iLayer < poDS->GetLayerCount();
iLayer++)
{
bool bRequested = bLayersWildcarded;
OGRLayer *poLayer = poDS->GetLayer(iLayer);
for (int iArg = 1; iArg < nArgc && !bRequested; iArg++)
{
if (EQUAL(papszArgv[iArg], "-lnum") &&
atoi(papszArgv[iArg + 1]) == iLayer)
bRequested = true;
else if (EQUAL(papszArgv[iArg], "-lname") &&
EQUAL(papszArgv[iArg + 1],
poLayer->GetLayerDefn()->GetName()))
bRequested = true;
}
if (!bRequested)
continue;
if (poLayer->GetSpatialRef())
poSrcSpatialRef = poLayer->GetSpatialRef()->Clone();
break;
}
}
GDALClose(poDS);
}
poDstLayer = poDstDS->CreateLayer("tileindex", poSrcSpatialRef);
OGRFieldDefn oLocation(pszTileIndexField, OFTString);
oLocation.SetWidth(200);
poDstLayer->CreateField(&oLocation);
if (pszSrcSRSName != nullptr)
{
OGRFieldDefn oSrcSRSNameField(pszSrcSRSName, OFTString);
poDstLayer->CreateField(&oSrcSRSNameField);
}
if (poSrcSpatialRef)
poSrcSpatialRef->Release();
}
}
/* -------------------------------------------------------------------- */
/* Identify target layer and field. */
/* -------------------------------------------------------------------- */
poDstLayer = poDstDS->GetLayer(0);
if (poDstLayer == nullptr)
{
fprintf(stderr, "Can't find any layer in output tileindex!\n");
exit(1);
}
const int iTileIndexField =
poDstLayer->GetLayerDefn()->GetFieldIndex(pszTileIndexField);
if (iTileIndexField == -1)
{
fprintf(stderr, "Can't find %s field in tile index dataset.\n",
pszTileIndexField);
exit(1);
}
if (pszSrcSRSName != nullptr)
i_SrcSRSName = poDstLayer->GetLayerDefn()->GetFieldIndex(pszSrcSRSName);
OGRFeatureDefn *poFeatureDefn = nullptr;
// Load in memory existing file names in SHP.
char **existingLayersTab = nullptr;
OGRSpatialReference *alreadyExistingSpatialRef = nullptr;
bool alreadyExistingSpatialRefValid = false;
const int nExistingLayers = static_cast<int>(poDstLayer->GetFeatureCount());
if (nExistingLayers)
{
existingLayersTab =
static_cast<char **>(CPLMalloc(nExistingLayers * sizeof(char *)));
for (int i = 0; i < nExistingLayers; i++)
{
OGRFeature *feature = poDstLayer->GetNextFeature();
existingLayersTab[i] =
CPLStrdup(feature->GetFieldAsString(iTileIndexField));
if (i == 0)
{
char *filename = CPLStrdup(existingLayersTab[i]);
// j used after for.
int j = static_cast<int>(strlen(filename)) - 1;
for (; j >= 0; j--)
{
if (filename[j] == ',')
break;
}
GDALDataset *poDS = nullptr;
if (j >= 0)
{
const int iLayer = atoi(filename + j + 1);
filename[j] = 0;
poDS = reinterpret_cast<GDALDataset *>(
OGROpen(filename, FALSE, nullptr));
if (poDS != nullptr)
{
OGRLayer *poLayer = poDS->GetLayer(iLayer);
if (poLayer)
{
alreadyExistingSpatialRefValid = true;
alreadyExistingSpatialRef =
poLayer->GetSpatialRef()
? poLayer->GetSpatialRef()->Clone()
: nullptr;
if (poFeatureDefn == nullptr)
poFeatureDefn =
poLayer->GetLayerDefn()->Clone();
}
GDALClose(poDS);
}
}
}
}
}
if (write_absolute_path)
{
current_path = CPLGetCurrentDir();
if (current_path == nullptr)
{
fprintf(stderr,
"This system does not support the CPLGetCurrentDir call. "
"The option -write_absolute_path will have no effect\n");
write_absolute_path = false;
}
}
/* ==================================================================== */
/* Process each input datasource in turn. */
/* ==================================================================== */
for (; nFirstSourceDataset < nArgc; nFirstSourceDataset++)
{
if (papszArgv[nFirstSourceDataset][0] == '-')
{
nFirstSourceDataset++;
continue;
}
char *fileNameToWrite = nullptr;
VSIStatBuf sStatBuf;
if (write_absolute_path &&
CPLIsFilenameRelative(papszArgv[nFirstSourceDataset]) &&
VSIStat(papszArgv[nFirstSourceDataset], &sStatBuf) == 0)
{
fileNameToWrite = CPLStrdup(CPLProjectRelativeFilename(
current_path, papszArgv[nFirstSourceDataset]));
}
else
{
fileNameToWrite = CPLStrdup(papszArgv[nFirstSourceDataset]);
}
GDALDataset *poDS = reinterpret_cast<GDALDataset *>(
OGROpen(papszArgv[nFirstSourceDataset], FALSE, nullptr));
if (poDS == nullptr)
{
fprintf(stderr, "Failed to open dataset %s, skipping.\n",
papszArgv[nFirstSourceDataset]);
CPLFree(fileNameToWrite);
continue;
}
/* --------------------------------------------------------------------
*/
/* Check all layers, and see if they match requests. */
/* --------------------------------------------------------------------
*/
for (int iLayer = 0; iLayer < poDS->GetLayerCount(); iLayer++)
{
bool bRequested = bLayersWildcarded;
OGRLayer *poLayer = poDS->GetLayer(iLayer);
for (int iArg = 1; iArg < nArgc && !bRequested; iArg++)
{
if (EQUAL(papszArgv[iArg], "-lnum") &&
atoi(papszArgv[iArg + 1]) == iLayer)
bRequested = true;
else if (EQUAL(papszArgv[iArg], "-lname") &&
EQUAL(papszArgv[iArg + 1],
poLayer->GetLayerDefn()->GetName()))
bRequested = true;
}
if (!bRequested)
continue;
// Checks that the layer is not already in tileindex.
int i = 0; // Used after for.
for (; i < nExistingLayers; i++)
{
// TODO(schwehr): Move this off of the stack.
char szLocation[5000] = {};
snprintf(szLocation, sizeof(szLocation), "%s,%d",
fileNameToWrite, iLayer);
if (EQUAL(szLocation, existingLayersTab[i]))
{
fprintf(stderr,
"Layer %d of %s is already in tileindex. "
"Skipping it.\n",
iLayer, papszArgv[nFirstSourceDataset]);
break;
}
}
if (i != nExistingLayers)
{
continue;
}
OGRSpatialReference *spatialRef = poLayer->GetSpatialRef();
// If not set target srs, test that the current file uses same
// projection as others.
if (!bSetTargetSRS)
{
if (alreadyExistingSpatialRefValid)
{
if ((spatialRef != nullptr &&
alreadyExistingSpatialRef != nullptr &&
spatialRef->IsSame(alreadyExistingSpatialRef) ==
FALSE) ||
((spatialRef != nullptr) !=
(alreadyExistingSpatialRef != nullptr)))
{
fprintf(
stderr,
"Warning : layer %d of %s is not using the same "
"projection system as other files in the "
"tileindex. This may cause problems when using it "
"in MapServer for example.%s\n",
iLayer, papszArgv[nFirstSourceDataset],
skip_different_projection ? " Skipping it" : "");
if (skip_different_projection)
{
continue;
}
}
}
else
{
alreadyExistingSpatialRefValid = true;
alreadyExistingSpatialRef =
spatialRef ? spatialRef->Clone() : nullptr;
}
}
/* --------------------------------------------------------------------
*/
/* Check if all layers in dataset have the same attributes
* schema. */
/* --------------------------------------------------------------------
*/
if (poFeatureDefn == nullptr)
{
poFeatureDefn = poLayer->GetLayerDefn()->Clone();
}
else if (!accept_different_schemas)
{
OGRFeatureDefn *poFeatureDefnCur = poLayer->GetLayerDefn();
assert(nullptr != poFeatureDefnCur);
const int fieldCount = poFeatureDefnCur->GetFieldCount();
if (fieldCount != poFeatureDefn->GetFieldCount())
{
fprintf(stderr,
"Number of attributes of layer %s of %s "
"does not match ... skipping it.\n",
poLayer->GetLayerDefn()->GetName(),
papszArgv[nFirstSourceDataset]);
if (bFirstWarningForNonMatchingAttributes)
{
fprintf(
stderr,
"Note : you can override this "
"behavior with -accept_different_schemas option\n"
"but this may result in a tileindex incompatible "
"with MapServer\n");
bFirstWarningForNonMatchingAttributes = false;
}
continue;
}
bool bSkip = false;
for (int fn = 0; fn < poFeatureDefnCur->GetFieldCount(); fn++)
{
OGRFieldDefn *poField = poFeatureDefn->GetFieldDefn(fn);
OGRFieldDefn *poFieldCur =
poFeatureDefnCur->GetFieldDefn(fn);
// XXX - Should those pointers be checked against NULL?
assert(nullptr != poField);
assert(nullptr != poFieldCur);
if (poField->GetType() != poFieldCur->GetType() ||
poField->GetWidth() != poFieldCur->GetWidth() ||
poField->GetPrecision() != poFieldCur->GetPrecision() ||
!EQUAL(poField->GetNameRef(), poFieldCur->GetNameRef()))
{
fprintf(stderr,
"Schema of attributes of layer %s of %s "
"does not match. Skipping it.\n",
poLayer->GetLayerDefn()->GetName(),
papszArgv[nFirstSourceDataset]);
if (bFirstWarningForNonMatchingAttributes)
{
fprintf(
stderr,
"Note : you can override this "
"behavior with -accept_different_schemas "
"option,\nbut this may result in a tileindex "
"incompatible with MapServer\n");
bFirstWarningForNonMatchingAttributes = false;
}
bSkip = true;
break;
}
}
if (bSkip)
continue;
}
/* --------------------------------------------------------------------
*/
/* Get layer extents, and create a corresponding polygon */
/* geometry. */
/* --------------------------------------------------------------------
*/
OGREnvelope sExtents;
if (poLayer->GetExtent(&sExtents, TRUE) != OGRERR_NONE)
{
fprintf(stderr,
"GetExtent() failed on layer %s of %s, skipping.\n",
poLayer->GetLayerDefn()->GetName(),
papszArgv[nFirstSourceDataset]);
continue;
}
OGRLinearRing oRing;
oRing.addPoint(sExtents.MinX, sExtents.MinY);
oRing.addPoint(sExtents.MinX, sExtents.MaxY);
oRing.addPoint(sExtents.MaxX, sExtents.MaxY);
oRing.addPoint(sExtents.MaxX, sExtents.MinY);
oRing.addPoint(sExtents.MinX, sExtents.MinY);
OGRPolygon oRegion;
oRegion.addRing(&oRing);
// If set target srs, do the forward transformation of all points.
if (bSetTargetSRS && spatialRef != nullptr)
{
OGRCoordinateTransformation *poCT = nullptr;
if (!spatialRef->IsSame(poTargetSRS))
{
poCT = OGRCreateCoordinateTransformation(spatialRef,
poTargetSRS);
if (poCT == nullptr ||
oRegion.transform(poCT) == OGRERR_FAILURE)
{
char *pszSourceWKT = nullptr;
spatialRef->exportToWkt(&pszSourceWKT);
fprintf(
stderr,
"Warning : unable to transform points from source "
"SRS `%s' to target SRS `%s'\n"
"for file `%s' - file skipped\n",
pszSourceWKT, pszTargetSRS,
papszArgv[nFirstSourceDataset]);
CPLFree(pszSourceWKT);
delete poCT;
continue;
}
delete poCT;
}
}
/* --------------------------------------------------------------------
*/
/* Add layer to tileindex. */
/* --------------------------------------------------------------------
*/
OGRFeature oTileFeat(poDstLayer->GetLayerDefn());
// TODO(schwehr): Move this off of the stack.
char szLocation[5000] = {};
snprintf(szLocation, sizeof(szLocation), "%s,%d", fileNameToWrite,
iLayer);
oTileFeat.SetGeometry(&oRegion);
oTileFeat.SetField(iTileIndexField, szLocation);
if (i_SrcSRSName >= 0 && spatialRef != nullptr)
{
const char *pszAuthorityCode =
spatialRef->GetAuthorityCode(nullptr);
const char *pszAuthorityName =
spatialRef->GetAuthorityName(nullptr);
char *pszWKT = nullptr;
spatialRef->exportToWkt(&pszWKT);
if (eSrcSRSFormat == FORMAT_AUTO)
{
if (pszAuthorityName != nullptr &&
pszAuthorityCode != nullptr)
{
oTileFeat.SetField(i_SrcSRSName,
CPLSPrintf("%s:%s", pszAuthorityName,
pszAuthorityCode));
}
else if (nMaxFieldSize == 0 ||
strlen(pszWKT) <= nMaxFieldSize)
{
oTileFeat.SetField(i_SrcSRSName, pszWKT);
}
else
{
char *pszProj4 = nullptr;
if (spatialRef->exportToProj4(&pszProj4) == OGRERR_NONE)
{
oTileFeat.SetField(i_SrcSRSName, pszProj4);
CPLFree(pszProj4);
}
else
{
oTileFeat.SetField(i_SrcSRSName, pszWKT);
}
}
}
else if (eSrcSRSFormat == FORMAT_WKT)
{
if (nMaxFieldSize == 0 || strlen(pszWKT) <= nMaxFieldSize)
{
oTileFeat.SetField(i_SrcSRSName, pszWKT);
}
else
{
fprintf(
stderr,
"Cannot write WKT for file %s as it is too long!\n",
fileNameToWrite);
}
}
else if (eSrcSRSFormat == FORMAT_PROJ)
{
char *pszProj4 = nullptr;
if (spatialRef->exportToProj4(&pszProj4) == OGRERR_NONE)
{
oTileFeat.SetField(i_SrcSRSName, pszProj4);
CPLFree(pszProj4);
}
}
else if (eSrcSRSFormat == FORMAT_EPSG)
{
if (pszAuthorityName != nullptr &&
pszAuthorityCode != nullptr)
oTileFeat.SetField(i_SrcSRSName,
CPLSPrintf("%s:%s", pszAuthorityName,
pszAuthorityCode));
}
CPLFree(pszWKT);
}
if (poDstLayer->CreateFeature(&oTileFeat) != OGRERR_NONE)
{
fprintf(stderr, "Failed to create feature on tile index. "
"Terminating.");
GDALClose(poDstDS);
exit(1);
}
}
/* --------------------------------------------------------------------
*/
/* Cleanup this data source. */
/* --------------------------------------------------------------------
*/
CPLFree(fileNameToWrite);
GDALClose(poDS);
}
/* -------------------------------------------------------------------- */
/* Close tile index and clear buffers. */
/* -------------------------------------------------------------------- */
GDALClose(poDstDS);
OGRFeatureDefn::DestroyFeatureDefn(poFeatureDefn);
if (alreadyExistingSpatialRef != nullptr)
alreadyExistingSpatialRef->Release();
delete poTargetSRS;
CPLFree(current_path);
if (nExistingLayers)
{
for (int i = 0; i < nExistingLayers; i++)
{
CPLFree(existingLayersTab[i]);
}
CPLFree(existingLayersTab);
}
OGRCleanupAll();
return 0;
}
MAIN_END
|