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 240 33.8 %
Date: 2025-07-08 21:33:46 Functions: 8 20 40.0 %

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

Generated by: LCOV version 1.14