LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/cad - gdalcaddataset.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 81 238 34.0 %
Date: 2026-06-19 21:24:00 Functions: 8 18 44.4 %

          Line data    Source code
       1             : /*******************************************************************************
       2             :  *  Project: OGR CAD Driver
       3             :  *  Purpose: Implements driver based on libopencad
       4             :  *  Author: Alexandr Borzykh, mush3d at gmail.com
       5             :  *  Author: Dmitry Baryshnikov, polimax@mail.ru
       6             :  *  Language: C++
       7             :  *******************************************************************************
       8             :  *  The MIT License (MIT)
       9             :  *
      10             :  *  Copyright (c) 2016 Alexandr Borzykh
      11             :  *  Copyright (c) 2016, NextGIS <info@nextgis.com>
      12             :  *
      13             :  * SPDX-License-Identifier: MIT
      14             :  *******************************************************************************/
      15             : #include "cpl_conv.h"
      16             : #include "gdal_pam.h"
      17             : #include "gdal_proxy.h"
      18             : #include "ogr_cad.h"
      19             : #include "vsilfileio.h"
      20             : 
      21             : class CADWrapperRasterBand : public GDALProxyRasterBand
      22             : {
      23             :     GDALRasterBand *poBaseBand;
      24             : 
      25             :   protected:
      26             :     virtual GDALRasterBand *
      27             :     RefUnderlyingRasterBand(bool /* bForceOpen */) const override;
      28             : 
      29             :   public:
      30           0 :     explicit CADWrapperRasterBand(GDALRasterBand *poBaseBandIn)
      31           0 :         : poBaseBand(poBaseBandIn)
      32             :     {
      33           0 :         eDataType = poBaseBand->GetRasterDataType();
      34           0 :         poBaseBand->GetBlockSize(&nBlockXSize, &nBlockYSize);
      35           0 :     }
      36             : };
      37             : 
      38             : GDALRasterBand *
      39           0 : CADWrapperRasterBand::RefUnderlyingRasterBand(bool /* bForceOpen */) const
      40             : {
      41           0 :     return poBaseBand;
      42             : }
      43             : 
      44           9 : GDALCADDataset::GDALCADDataset()
      45             :     : poCADFile(nullptr), papoLayers(nullptr), nLayers(0), poRasterDS(nullptr),
      46           9 :       poSpatialReference(nullptr)
      47             : {
      48           9 : }
      49             : 
      50          18 : GDALCADDataset::~GDALCADDataset()
      51             : {
      52           9 :     if (poRasterDS != nullptr)
      53             :     {
      54           0 :         GDALClose(poRasterDS);
      55           0 :         poRasterDS = nullptr;
      56             :     }
      57             : 
      58          19 :     for (int i = 0; i < nLayers; i++)
      59          10 :         delete papoLayers[i];
      60           9 :     CPLFree(papoLayers);
      61             : 
      62           9 :     if (poSpatialReference)
      63           8 :         poSpatialReference->Release();
      64             : 
      65           9 :     if (poCADFile)
      66           8 :         delete poCADFile;
      67          18 : }
      68             : 
      69           0 : void GDALCADDataset::FillTransform(CADImage *pImage, double dfUnits)
      70             : {
      71           0 :     CADImage::ResolutionUnit eResUnits = pImage->getResolutionUnits();
      72           0 :     double dfMultiply = 1.0;
      73             : 
      74           0 :     switch (eResUnits)  // 0 == none, 2 == centimeters, 5 == inches;
      75             :     {
      76           0 :         case CADImage::ResolutionUnit::CENTIMETER:
      77           0 :             dfMultiply = 100.0 / dfUnits;  // Meters to linear units
      78           0 :             break;
      79           0 :         case CADImage::ResolutionUnit::INCH:
      80           0 :             dfMultiply = 0.0254 / dfUnits;
      81           0 :             break;
      82           0 :         case CADImage::ResolutionUnit::NONE:
      83             :         default:
      84           0 :             dfMultiply = 1.0;
      85             :     }
      86             : 
      87           0 :     CADVector oSizePt = pImage->getImageSizeInPx();
      88           0 :     CADVector oInsPt = pImage->getVertInsertionPoint();
      89           0 :     CADVector oSizeUnitsPt = pImage->getPixelSizeInACADUnits();
      90           0 :     m_gt[0] = oInsPt.getX();
      91           0 :     m_gt[3] = oInsPt.getY() + oSizePt.getY() * oSizeUnitsPt.getX() * dfMultiply;
      92           0 :     m_gt[2] = 0.0;
      93           0 :     m_gt[4] = 0.0;
      94             : 
      95           0 :     m_gt[1] = oSizeUnitsPt.getX() * dfMultiply;
      96           0 :     m_gt[5] = -oSizeUnitsPt.getY() * dfMultiply;
      97           0 : }
      98             : 
      99           9 : int GDALCADDataset::Open(GDALOpenInfo *poOpenInfo, CADFileIO *pFileIO,
     100             :                          long nSubRasterLayer, long nSubRasterFID)
     101             : {
     102           9 :     osCADFilename = pFileIO->GetFilePath();
     103           9 :     SetDescription(poOpenInfo->pszFilename);
     104             : 
     105             :     const char *papszReadOptions =
     106           9 :         CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "MODE", "READ_FAST");
     107          18 :     const char *papszReadUnsupportedGeoms = CSLFetchNameValueDef(
     108           9 :         poOpenInfo->papszOpenOptions, "ADD_UNSUPPORTED_GEOMETRIES_DATA", "NO");
     109             : 
     110           9 :     enum CADFile::OpenOptions openOpts = CADFile::READ_FAST;
     111           9 :     bool bReadUnsupportedGeometries = false;
     112           9 :     if (EQUAL(papszReadOptions, "READ_ALL"))
     113             :     {
     114           0 :         openOpts = CADFile::READ_ALL;
     115             :     }
     116           9 :     else if (EQUAL(papszReadOptions, "READ_FASTEST"))
     117             :     {
     118           0 :         openOpts = CADFile::READ_FASTEST;
     119             :     }
     120             : 
     121           9 :     if (EQUAL(papszReadUnsupportedGeoms, "YES"))
     122             :     {
     123           0 :         bReadUnsupportedGeometries = true;
     124             :     }
     125             : 
     126           9 :     poCADFile = OpenCADFile(pFileIO, openOpts, bReadUnsupportedGeometries);
     127             : 
     128           9 :     if (GetLastErrorCode() == CADErrorCodes::UNSUPPORTED_VERSION)
     129             :     {
     130           1 :         CPLError(CE_Failure, CPLE_NotSupported,
     131             :                  "libopencad %s does not support this version of CAD file.\n"
     132             :                  "Supported formats are:\n%s",
     133             :                  GetVersionString(), GetCADFormats());
     134           1 :         return FALSE;
     135             :     }
     136             : 
     137           8 :     if (GetLastErrorCode() != CADErrorCodes::SUCCESS)
     138             :     {
     139           0 :         CPLError(CE_Failure, CPLE_NotSupported,
     140             :                  "libopencad %s does not support this version of CAD "
     141             :                  "file.\nSupported formats: %s",
     142             :                  GetVersionString(), GetCADFormats());
     143           0 :         return FALSE;
     144             :     }
     145             : 
     146           8 :     const OGRSpatialReference *poSpatialRef = GetSpatialRef();
     147           8 :     int nRasters = 1;
     148             : 
     149           8 :     if (nSubRasterLayer != -1 && nSubRasterFID != -1)
     150             :     {
     151             :         // Indicates that subdataset from CAD layer number nSubRasterLayer and
     152             :         // FID nSubRasterFID is request
     153           0 :         nRasters = 2;
     154             :     }
     155             :     else
     156             :     {
     157             :         // Fill metadata
     158           8 :         const CADHeader &header = poCADFile->getHeader();
     159         576 :         for (size_t i = 0; i < header.getSize(); ++i)
     160             :         {
     161         568 :             short nCode = header.getCode(static_cast<int>(i));
     162        1136 :             const CADVariant &oVal = header.getValue(nCode);
     163         568 :             GDALDataset::SetMetadataItem(header.getValueName(nCode),
     164         568 :                                          oVal.getString().c_str());
     165             :         }
     166             : 
     167             :         // Reading content of .prj file, or extracting it from CAD if not
     168             :         // present
     169           8 :         nLayers = 0;
     170             :         // FIXME: We allocate extra memory, do we need more strict policy here?
     171           8 :         papoLayers = static_cast<OGRCADLayer **>(
     172           8 :             CPLMalloc(sizeof(OGRCADLayer *) * poCADFile->GetLayersCount()));
     173             : 
     174           8 :         int nEncoding = GetCadEncoding();
     175          18 :         for (size_t i = 0; i < poCADFile->GetLayersCount(); ++i)
     176             :         {
     177          10 :             CADLayer &oLayer = poCADFile->GetLayer(i);
     178          20 :             if (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR &&
     179          10 :                 oLayer.getGeometryCount() > 0)
     180             :             {
     181             :                 OGRSpatialReference *poSRS =
     182          10 :                     poSpatialRef ? poSpatialRef->Clone() : nullptr;
     183          10 :                 papoLayers[nLayers++] =
     184          10 :                     new OGRCADLayer(this, oLayer, poSRS, nEncoding);
     185          10 :                 if (poSRS)
     186          10 :                     poSRS->Release();
     187             :             }
     188             : 
     189          10 :             if (poOpenInfo->nOpenFlags & GDAL_OF_RASTER)
     190             :             {
     191          10 :                 for (size_t j = 0; j < oLayer.getImageCount(); ++j)
     192             :                 {
     193           0 :                     nSubRasterLayer = static_cast<long>(i);
     194           0 :                     nSubRasterFID = static_cast<long>(j);
     195           0 :                     GDALDataset::SetMetadataItem(
     196             :                         CPLSPrintf("SUBDATASET_%d_NAME", nRasters),
     197             :                         CPLSPrintf("CAD:%s:%ld:%ld", osCADFilename.c_str(),
     198             :                                    nSubRasterLayer, nSubRasterFID),
     199             :                         GDAL_MDD_SUBDATASETS);
     200           0 :                     GDALDataset::SetMetadataItem(
     201             :                         CPLSPrintf("SUBDATASET_%d_DESC", nRasters),
     202           0 :                         CPLSPrintf("%s - %ld", oLayer.getName().c_str(),
     203             :                                    nSubRasterFID),
     204             :                         GDAL_MDD_SUBDATASETS);
     205           0 :                     nRasters++;
     206             :                 }
     207             :             }
     208             :         }
     209             :         // If nRasters == 2 we have the only one raster in CAD file
     210             :     }
     211             : 
     212             :     // The only one raster layer in dataset is present or subdataset is request
     213           8 :     if (nRasters == 2)
     214             :     {
     215           0 :         CADLayer &oLayer = poCADFile->GetLayer(nSubRasterLayer);
     216           0 :         CADImage *pImage = oLayer.getImage(nSubRasterFID);
     217           0 :         if (pImage)
     218             :         {
     219             :             // TODO: Add support clipping region in neatline
     220           0 :             CPLString osImgFilename = pImage->getFilePath();
     221           0 :             CPLString osImgPath = CPLGetPathSafe(osImgFilename);
     222           0 :             if (osImgPath.empty())
     223             :             {
     224             :                 osImgFilename =
     225           0 :                     CPLFormFilenameSafe(CPLGetPathSafe(osCADFilename).c_str(),
     226           0 :                                         osImgFilename, nullptr);
     227             :             }
     228             : 
     229           0 :             if (!CPLCheckForFile(const_cast<char *>(osImgFilename.c_str()),
     230             :                                  nullptr))
     231           0 :                 return poOpenInfo->nOpenFlags & GDAL_OF_VECTOR;
     232             : 
     233           0 :             poRasterDS = GDALDataset::Open(
     234             :                 osImgFilename,
     235           0 :                 poOpenInfo->eAccess | GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR);
     236           0 :             if (poRasterDS == nullptr)
     237             :             {
     238           0 :                 delete pImage;
     239           0 :                 return poOpenInfo->nOpenFlags & GDAL_OF_VECTOR;
     240             :             }
     241           0 :             if (poRasterDS->GetRasterCount() == 0)
     242             :             {
     243           0 :                 delete pImage;
     244           0 :                 GDALClose(poRasterDS);
     245           0 :                 return poOpenInfo->nOpenFlags & GDAL_OF_VECTOR;
     246             :             }
     247             : 
     248           0 :             if (poRasterDS->GetGeoTransform(m_gt) != CE_None)
     249             :             {
     250             :                 // The external world file have priority
     251           0 :                 double dfUnits = 1.0;
     252           0 :                 if (nullptr != poSpatialRef)
     253           0 :                     dfUnits = poSpatialRef->GetLinearUnits();
     254           0 :                 FillTransform(pImage, dfUnits);
     255             :             }
     256           0 :             delete pImage;
     257             : 
     258           0 :             nRasterXSize = poRasterDS->GetRasterXSize();
     259           0 :             nRasterYSize = poRasterDS->GetRasterYSize();
     260           0 :             if (!GDALCheckDatasetDimensions(nRasterXSize, nRasterYSize))
     261             :             {
     262           0 :                 GDALClose(poRasterDS);
     263           0 :                 return poOpenInfo->nOpenFlags & GDAL_OF_VECTOR;
     264             :             }
     265             : 
     266           0 :             for (int iBand = 1; iBand <= poRasterDS->GetRasterCount(); iBand++)
     267           0 :                 SetBand(iBand, new CADWrapperRasterBand(
     268           0 :                                    poRasterDS->GetRasterBand(iBand)));
     269             : 
     270           0 :             char **papszDomainList = poRasterDS->GetMetadataDomainList();
     271           0 :             while (papszDomainList)
     272             :             {
     273           0 :                 CSLConstList papszMetadata = GetMetadata(*papszDomainList);
     274             :                 CSLConstList papszRasterMetadata =
     275           0 :                     poRasterDS->GetMetadata(*papszDomainList);
     276           0 :                 if (nullptr == papszMetadata)
     277           0 :                     SetMetadata(papszRasterMetadata, *papszDomainList);
     278             :                 else
     279             :                 {
     280           0 :                     char **papszMD = CSLMerge(CSLDuplicate(papszMetadata),
     281             :                                               papszRasterMetadata);
     282           0 :                     SetMetadata(papszMD, *papszDomainList);
     283           0 :                     CSLDestroy(papszMD);
     284             :                 }
     285           0 :                 papszDomainList++;
     286             :             }
     287             :         }
     288             :     }
     289             : 
     290           8 :     return TRUE;
     291             : }
     292             : 
     293          10 : const OGRLayer *GDALCADDataset::GetLayer(int iLayer) const
     294             : {
     295          10 :     if (iLayer < 0 || iLayer >= nLayers)
     296           0 :         return nullptr;
     297             :     else
     298          10 :         return papoLayers[iLayer];
     299             : }
     300             : 
     301           0 : int GDALCADDataset::TestCapability(const char *pszCap) const
     302             : {
     303           0 :     if (EQUAL(pszCap, ODsCCreateLayer) || EQUAL(pszCap, ODsCDeleteLayer))
     304           0 :         return FALSE;
     305           0 :     else if (EQUAL(pszCap, ODsCCurveGeometries))
     306           0 :         return TRUE;
     307           0 :     else if (EQUAL(pszCap, ODsCMeasuredGeometries))
     308           0 :         return TRUE;
     309           0 :     else if (EQUAL(pszCap, ODsCZGeometries))
     310           0 :         return TRUE;
     311           0 :     return FALSE;
     312             : }
     313             : 
     314           0 : char **GDALCADDataset::GetFileList()
     315             : {
     316           0 :     char **papszFileList = GDALDataset::GetFileList();
     317             : 
     318             :     /* duplicated papszFileList = CSLAddString( papszFileList, osCADFilename
     319             :      * );*/
     320           0 :     const std::string osPRJFilename = GetPrjFilePath();
     321           0 :     if (!osPRJFilename.empty())
     322           0 :         papszFileList = CSLAddString(papszFileList, osPRJFilename.c_str());
     323             : 
     324           0 :     for (size_t i = 0; i < poCADFile->GetLayersCount(); ++i)
     325             :     {
     326           0 :         CADLayer &oLayer = poCADFile->GetLayer(i);
     327           0 :         for (size_t j = 0; j < oLayer.getImageCount(); ++j)
     328             :         {
     329           0 :             CADImage *pImage = oLayer.getImage(j);
     330           0 :             if (pImage)
     331             :             {
     332           0 :                 CPLString osImgFilename = pImage->getFilePath();
     333           0 :                 if (CPLCheckForFile(const_cast<char *>(osImgFilename.c_str()),
     334           0 :                                     nullptr) == TRUE)
     335           0 :                     papszFileList = CSLAddString(papszFileList, osImgFilename);
     336             :             }
     337             :         }
     338             :     }
     339             : 
     340           0 :     if (nullptr != poRasterDS)
     341             :     {
     342           0 :         papszFileList = CSLMerge(papszFileList, poRasterDS->GetFileList());
     343             :     }
     344           0 :     return papszFileList;
     345             : }
     346             : 
     347           8 : int GDALCADDataset::GetCadEncoding() const
     348             : {
     349           8 :     if (poCADFile == nullptr)
     350           0 :         return 0;
     351           8 :     const CADHeader &header = poCADFile->getHeader();
     352             :     return static_cast<int>(
     353           8 :         header.getValue(CADHeader::DWGCODEPAGE, 0).getDecimal());
     354             : }
     355             : 
     356           8 : const OGRSpatialReference *GDALCADDataset::GetSpatialRef() const
     357             : {
     358           8 :     if (poSpatialReference)
     359           0 :         return poSpatialReference;
     360             : 
     361           8 :     if (poCADFile != nullptr)
     362             :     {
     363          16 :         CPLString sESRISpatRef;
     364           8 :         poSpatialReference = new OGRSpatialReference();
     365           8 :         poSpatialReference->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
     366             : 
     367          16 :         CADDictionary oNOD = poCADFile->GetNOD();
     368          24 :         CPLString sESRISpatRefData = oNOD.getRecordByName("ESRI_PRJ");
     369           8 :         if (!sESRISpatRefData.empty())
     370             :         {
     371             :             sESRISpatRef =
     372           0 :                 sESRISpatRefData.substr(sESRISpatRefData.find("GEO"));
     373             :         }
     374             : 
     375           8 :         if (!sESRISpatRef.empty())
     376             :         {
     377           0 :             char **papszPRJData = nullptr;
     378           0 :             papszPRJData = CSLAddString(papszPRJData, sESRISpatRef);
     379           0 :             if (poSpatialReference->importFromESRI(papszPRJData) != OGRERR_NONE)
     380             :             {
     381           0 :                 CPLError(CE_Warning, CPLE_AppDefined,
     382             :                          "Failed to parse PRJ section, ignoring.");
     383           0 :                 delete poSpatialReference;
     384           0 :                 poSpatialReference = nullptr;
     385             :             }
     386             : 
     387           0 :             CSLDestroy(papszPRJData);
     388             :         }
     389             :         else
     390             :         {
     391          16 :             const std::string osPRJFilename = GetPrjFilePath();
     392           8 :             if (!osPRJFilename.empty())  // check if path exists
     393             :             {
     394           0 :                 CPLPushErrorHandler(CPLQuietErrorHandler);
     395           0 :                 char **papszPRJData = CSLLoad(osPRJFilename.c_str());
     396           0 :                 CPLPopErrorHandler();
     397             : 
     398           0 :                 if (poSpatialReference->importFromESRI(papszPRJData) !=
     399             :                     OGRERR_NONE)
     400             :                 {
     401           0 :                     CPLError(CE_Warning, CPLE_AppDefined,
     402             :                              "Failed to parse PRJ file, ignoring.");
     403           0 :                     delete poSpatialReference;
     404           0 :                     poSpatialReference = nullptr;
     405             :                 }
     406             : 
     407           0 :                 if (papszPRJData)
     408           0 :                     CSLDestroy(papszPRJData);
     409             :             }
     410             :         }
     411             :     }
     412             : 
     413           8 :     return poSpatialReference;
     414             : }
     415             : 
     416           8 : const std::string GDALCADDataset::GetPrjFilePath() const
     417             : {
     418          16 :     std::string osPRJFilename = CPLResetExtensionSafe(osCADFilename, "prj");
     419           8 :     if (CPLCheckForFile(osPRJFilename.data(), nullptr) == TRUE)
     420           0 :         return osPRJFilename;
     421             : 
     422           8 :     osPRJFilename = CPLResetExtensionSafe(osCADFilename, "PRJ");
     423           8 :     if (CPLCheckForFile(osPRJFilename.data(), nullptr) == TRUE)
     424           0 :         return osPRJFilename;
     425             : 
     426           8 :     return std::string();
     427             : }
     428             : 
     429           0 : CPLErr GDALCADDataset::GetGeoTransform(GDALGeoTransform &gt) const
     430             : {
     431           0 :     gt = m_gt;
     432           0 :     return CE_None;
     433             : }
     434             : 
     435           0 : int GDALCADDataset::GetGCPCount()
     436             : {
     437           0 :     if (nullptr == poRasterDS)
     438           0 :         return 0;
     439           0 :     return poRasterDS->GetGCPCount();
     440             : }
     441             : 
     442           0 : const OGRSpatialReference *GDALCADDataset::GetGCPSpatialRef() const
     443             : {
     444           0 :     if (nullptr == poRasterDS)
     445           0 :         return nullptr;
     446           0 :     return poRasterDS->GetGCPSpatialRef();
     447             : }
     448             : 
     449           0 : const GDAL_GCP *GDALCADDataset::GetGCPs()
     450             : {
     451           0 :     if (nullptr == poRasterDS)
     452           0 :         return nullptr;
     453           0 :     return poRasterDS->GetGCPs();
     454             : }
     455             : 
     456           0 : int GDALCADDataset::CloseDependentDatasets()
     457             : {
     458           0 :     int bRet = GDALDataset::CloseDependentDatasets();
     459           0 :     if (poRasterDS != nullptr)
     460             :     {
     461           0 :         GDALClose(poRasterDS);
     462           0 :         poRasterDS = nullptr;
     463           0 :         bRet = TRUE;
     464             :     }
     465           0 :     return bRet;
     466             : }

Generated by: LCOV version 1.14