Changeset 971:5a9501d50eb0


Ignore:
Timestamp:
02/19/09 11:02:45 (18 months ago)
Author:
Howard Butler <hobu.inc@…>
Branch:
default
Convert:
svn:1766ff46-f334-0410-ab20-d63176f87757/trunk@1049
Message:

don't leak, better error checking

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/lassrs.cpp

    r969 r971  
    7171    // FIXME: this needs to be cleaned up 
    7272     
    73     // if (m_gtiff) 
    74     // { 
    75     //     GTIFFree(m_gtiff); 
    76     //     m_gtiff = NULL; 
    77     // } 
    78     // if (m_tiff) 
    79     // { 
    80     //     ST_Destroy(m_tiff); 
    81     //     m_tiff = NULL; 
    82     // } 
     73    if (m_gtiff) 
     74    { 
     75        GTIFFree(m_gtiff); 
     76        m_gtiff = NULL; 
     77    } 
     78    if (m_tiff) 
     79    { 
     80        ST_Destroy(m_tiff); 
     81        m_tiff = NULL; 
     82    } 
    8383 
    8484 
     
    108108    std::string const uid("LASF_Projection"); 
    109109     
     110    // Wipe out any existing VLRs that might exist on the LASSRS 
    110111    m_vlrs.clear(); 
     112     
     113    // We only copy VLR records from the list which are related to GeoTIFF keys. 
     114    // They must have an id of "LASF_Projection" and a record id that's related. 
    111115    for (i = vlrs.begin(); i != vlrs.end(); ++i) 
    112116    { 
     117        //GTIFF_GEOKEYDIRECTORY == 34735 
    113118        if (uid == (*i).GetUserId(true).c_str() && 34735 == (*i).GetRecordId()) { 
    114119            m_vlrs.push_back(*i); 
    115120        } 
     121         
     122        // GTIFF_DOUBLEPARAMS == 34736 
    116123        if (uid == (*i).GetUserId(true).c_str() && 34736 == (*i).GetRecordId()) { 
    117124            m_vlrs.push_back(*i); 
    118125        } 
     126         
     127        // GTIFF_ASCIIPARAMS == 34737 
    119128        if (uid == (*i).GetUserId(true).c_str() && 34737 == (*i).GetRecordId()) { 
    120129            m_vlrs.push_back(*i); 
     
    151160    int atype =0; 
    152161     
    153     if (!m_tiff) throw std::invalid_argument("m_tiff was null"); 
    154     if (!m_gtiff) throw std::invalid_argument("m_gtiff was null"); 
     162    if (!m_tiff) throw std::invalid_argument("m_tiff was null, cannot reset VLRs without m_tiff"); 
     163    if (!m_gtiff) throw std::invalid_argument("m_gtiff was null, cannot reset VLRs without m_gtiff"); 
    155164 
    156165    //GTIFF_GEOKEYDIRECTORY == 34735 
     
    184193    } 
    185194 
     195    // GTIFF_DOUBLEPARAMS == 34736 
     196    ret = ST_GetKey(m_tiff, 34736, &dcount, &dtype, (void**)&ddata); 
     197    if (ret) 
     198    {     
     199        LASVLR record; 
     200        int i = 0; 
     201        record.SetRecordId(34736); 
     202        record.SetUserId("LASF_Projection"); 
     203        std::vector<uint8_t> data; 
     204 
     205        // Doubles are 8 bytes in length 
     206        uint16_t length = 8*dcount; 
     207        record.SetRecordLength(length); 
     208         
     209        // Copy the data into the data vector 
     210        for (i=0; i<dcount;i++) 
     211        { 
     212            dvalue = ddata[i]; 
     213             
     214            uint8_t* v =  reinterpret_cast<uint8_t*>(&dvalue); 
     215             
     216            data.push_back(v[0]); 
     217            data.push_back(v[1]); 
     218            data.push_back(v[2]); 
     219            data.push_back(v[3]); 
     220            data.push_back(v[4]); 
     221            data.push_back(v[5]); 
     222            data.push_back(v[6]); 
     223            data.push_back(v[7]);    
     224        }         
     225        record.SetData(data); 
     226        m_vlrs.push_back(record); 
     227    } 
     228     
    186229    // GTIFF_ASCIIPARAMS == 34737 
    187230    ret = ST_GetKey(m_tiff, 34737, &acount, &atype, (void**)&adata); 
     
    193236         record.SetUserId("LASF_Projection"); 
    194237         std::vector<uint8_t> data; 
    195               
     238 
     239         // whoa.  If the returned count was 0, it is because there  
     240         // was a bug in libgeotiff that existed before r1531 where it  
     241         // didn't calculate the string length for an ASCII geotiff tag. 
     242         // We need to throw an exception in this case because we're 
     243         // screwed, and if we don't we'll end up writing bad GeoTIFF keys. 
     244         if (!acount) { 
     245             throw std::runtime_error("GeoTIFF ASCII key with no returned size. "  
     246                                      "Upgrade your libgeotiff to a version greater " 
     247                                      "than r1531 (libgeotiff 1.2.6)"); 
     248                     } 
    196249         uint32_t length = acount; 
    197250         record.SetRecordLength(length); 
     
    217270    } 
    218271 
    219  
    220     // GTIFF_DOUBLEPARAMS == 34736 
    221     ret = ST_GetKey(m_tiff, 34736, &dcount, &dtype, (void**)&ddata); 
    222     if (ret) 
    223     {     
    224         LASVLR record; 
    225         int i = 0; 
    226         record.SetRecordId(34736); 
    227         record.SetUserId("LASF_Projection"); 
    228         std::vector<uint8_t> data; 
    229  
    230         // Doubles are 8 bytes in length 
    231         uint16_t length = 8*dcount; 
    232         record.SetRecordLength(length); 
    233          
    234         // Copy the data into the data vector 
    235         for (i=0; i<dcount;i++) 
    236         { 
    237             dvalue = ddata[i]; 
    238              
    239             uint8_t* v =  reinterpret_cast<uint8_t*>(&dvalue); 
    240              
    241             data.push_back(v[0]); 
    242             data.push_back(v[1]); 
    243             data.push_back(v[2]); 
    244             data.push_back(v[3]); 
    245             data.push_back(v[4]); 
    246             data.push_back(v[5]); 
    247             data.push_back(v[6]); 
    248             data.push_back(v[7]);    
    249         }         
    250         record.SetData(data); 
    251         m_vlrs.push_back(record); 
    252     } 
    253272     
    254273#endif // ndef HAVE_LIBGEOTIFF 
     
    259278 
    260279#ifdef HAVE_LIBGEOTIFF 
    261     // if we're already set, don't overwrite 
    262     // FIXME this is a bug. 
    263     // if (m_gtiff) return m_gtiff; 
     280    // If we already have m_gtiff and m_tiff, that is because we have  
     281    // already called GetGTIF once before.  VLRs ultimately drive how the  
     282    // LASSRS is defined, not the GeoTIFF keys.   
     283    if (m_tiff) {ST_Destroy(m_tiff); m_tiff = NULL;} 
     284    if (m_gtiff) {GTIFFree(m_gtiff); m_gtiff = NULL; } 
    264285     
    265286    m_tiff = ST_Create(); 
    266287    std::string const uid("LASF_Projection"); 
    267288     
     289    // Nothing is going to happen here if we don't have any VLRs describing 
     290    // SRS information on the LASSRS.   
    268291    for (uint16_t i = 0; i < m_vlrs.size(); ++i) 
    269292    { 
     
    298321    return NULL; 
    299322} 
    300  
    301323 
    302324/// Fetch the SRS as WKT 
     
    336358    } 
    337359#else 
    338     ; 
     360     
     361    throw std::runtime_error("GDAL is not available, LASSRS could not be set from WKT"); 
    339362 
    340363#endif 
     
    362385    std::string tmp(proj4); 
    363386    std::free(proj4); 
    364  
     387     
     388    delete poSRS; 
     389     
    365390    return tmp;     
    366391 
     
    403428     
    404429    poSRS->exportToWkt(&poWKT); 
     430    delete poSRS; 
     431     
    405432    std::string tmp(poWKT); 
    406433    std::free(poWKT); 
Note: See TracChangeset for help on using the changeset viewer.