Changeset 696:a96d4d202f0d


Ignore:
Timestamp:
05/26/08 20:53:43 (2 years ago)
Author:
Mateusz Loskot <mateusz@…>
Branch:
default
Convert:
svn:1766ff46-f334-0410-ab20-d63176f87757/trunk@764
Message:

las2ogr: implemented arguments parsing, usage message, listing of supported formats (Ticket #32).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • apps/las2ogr.cpp

    r685 r696  
    2020// ogr 
    2121#include <ogr_api.h> 
     22#include <ogrsf_frmts.h> 
    2223//std 
    2324#include <cassert> 
     
    7980}; 
    8081 
    81 OGRFieldDefnH create_field(const char* name, OGRFieldType type, int width, int precision ) { 
    82  
     82OGRFieldDefnH create_field(const char* name, OGRFieldType type, int width, int precision) 
     83{ 
    8384    OGRFieldDefnH fld; 
    8485    fld= OGR_Fld_Create(name, type); 
     
    152153void report_ogr_formats() 
    153154{ 
    154  
    155     std::cout << "Supported target formats:"; 
     155    std::cerr << "Supported OGR formats:"; 
    156156 
    157157    for (int i = 0; i < OGRGetDriverCount(); ++i) 
     
    160160        assert(0 != drv); 
    161161 
    162         if (OGR_Dr_TestCapability(drv,"ODrCCreateDataSource")) 
    163         { 
    164             std::cout << "\n - " << OGR_Dr_GetName(drv); 
    165         } 
    166     } 
    167  
    168     std::cout << std::endl; 
     162        if (OGR_Dr_TestCapability(drv, ODrCCreateDataSource)) 
     163        { 
     164            std::cerr << "\n - " << OGR_Dr_GetName(drv); 
     165        } 
     166    } 
     167 
     168    std::cerr << "\nMore details at http://gdal.org/ogr/ogr_formats.html" << std::endl; 
     169} 
     170 
     171void usage() 
     172{ 
     173    std::cerr << "Usage: las2ogr OPTIONS\nOptions are:\n" 
     174        << "\t-h print this message\n" 
     175        << "\t-i <infile>\tinput ASPRS LAS file\n" 
     176        << "\t-o <outfile>\toutput file\n" 
     177        << "\t-f <format>\tOGR format for output file\n" 
     178        << "\t-formats\tlist supported OGR formats\n";        
    169179} 
    170180 
    171181int main(int argc, char* argv[]) 
    172182{ 
    173     // TODO: 
    174     // - rename las2shp to las2ogr with ESRI Shapefile as default output format 
    175     // - input params parsing 
    176     // - usage info 
    177     // - progress info 
    178  
    179     OGRRegisterAll(); 
     183    int rc = 0; 
    180184 
    181185    try 
    182186    { 
    183         // TODO: input params should come from argv 
    184         //std::string lasname("d:\\data\\lidar\\LDR030828_213450_0.LAS"); 
    185         std::string lasname("D:\\data\\lidar\\gilmer\\000001.las"); 
    186         std::string ogrname("cloud.shp"); 
     187        OGRRegisterAll(); 
     188 
     189        // Parse command-line options 
     190        std::string in_file; 
     191        std::string out_file; 
     192        std::string out_frmt; 
     193        { 
     194            int on_arg = 1; 
     195            while (on_arg < argc) 
     196            { 
     197                std::string arg(argv[on_arg]); 
     198                if (arg == "-h") 
     199                { 
     200                    usage(); 
     201                    return 0; 
     202                } 
     203                else if (arg == "-formats") 
     204                { 
     205                    report_ogr_formats(); 
     206                    return 0; 
     207                } 
     208                else if (arg == "-i" && (on_arg + 1 < argc)) 
     209                {    
     210                    ++on_arg; 
     211                    assert(on_arg < argc); 
     212                    in_file = argv[on_arg]; 
     213                } 
     214                else if (arg == "-o" && (on_arg + 1 < argc)) 
     215                { 
     216                    ++on_arg; 
     217                    assert(on_arg < argc); 
     218                    out_file = argv[on_arg]; 
     219                    out_frmt = "ESRI Shapefile"; // default output format 
     220                } 
     221                else if (arg == "-f" && (on_arg + 1 < argc)) 
     222                { 
     223                    ++on_arg; 
     224                    assert(on_arg < argc); 
     225                    out_frmt = argv[on_arg]; 
     226                } 
     227                else 
     228                { 
     229                    throw std::runtime_error(std::string("unrecognized parameter: ") + arg); 
     230                } 
     231                ++on_arg; 
     232            } 
     233 
     234            if (in_file.empty() || out_file.empty() || out_frmt.empty()) 
     235            { 
     236                throw std::runtime_error("missing input paremeters"); 
     237            } 
     238        } 
    187239 
    188240        // 
    189241        // Source 
    190242        // 
    191         std::cout << "Source:" << "\n - dataset: " << lasname << std::endl; 
     243        std::cout << "Source:" << "\n - dataset: " << in_file << std::endl; 
    192244 
    193245        std::ifstream ifs; 
    194         if (!liblas::Open(ifs, lasname.c_str())) 
    195         { 
    196             throw std::runtime_error(std::string("Can not open ") + lasname); 
     246        if (!liblas::Open(ifs, in_file.c_str())) 
     247        { 
     248            throw std::runtime_error(std::string("Can not open \'") + in_file + "\'"); 
    197249        } 
    198250        liblas::LASReader reader(ifs); 
     
    201253        // Target 
    202254        // 
    203         std::string const drvname("ESRI Shapefile"); 
    204         std::string const lyrname(ogrname.substr(0, ogrname.find_last_of('.'))); 
     255        std::string const lyrname(out_file.substr(0, out_file.find_last_of('.'))); 
    205256 
    206257        std::cout << "Target:"  
    207                   << "\n - format: " << drvname 
    208                   << "\n - dataset: " << ogrname 
    209                   << "\n - layer: " << lyrname 
    210                   << std::endl; 
    211          
    212         OGRSFDriverH drv = OGRGetDriverByName(drvname.c_str()); 
     258            << "\n - format: " << out_frmt 
     259            << "\n - dataset: " << out_file 
     260            << "\n - layer: " << lyrname 
     261            << std::endl; 
     262 
     263        OGRSFDriverH drv = OGRGetDriverByName(out_frmt.c_str()); 
    213264        if (0 == drv) 
    214265        { 
    215             throw std::runtime_error(drvname + " driver not available"); 
    216         } 
    217   
    218         ogr_wrapper<OGRDataSourceH> ds(OGR_Dr_CreateDataSource(drv, ogrname.c_str(), 0), 
    219                                        OGR_DS_Destroy); 
     266            throw std::runtime_error(out_frmt + " driver not available"); 
     267        } 
     268 
     269        ogr_wrapper<OGRDataSourceH> ds(OGR_Dr_CreateDataSource(drv, out_file.c_str(), 0), OGR_DS_Destroy); 
    220270        if (0 == ds) 
    221271        { 
    222             throw std::runtime_error(ogrname + " datasource  cration failed"); 
    223         } 
    224  
    225         OGRLayerH lyr = 0; 
    226         lyr = OGR_DS_CreateLayer(ds, lyrname.c_str(), 0, wkbPoint25D, 0); 
     272            throw std::runtime_error(out_file + " datasource  cration failed"); 
     273        } 
     274 
     275        OGRLayerH lyr = OGR_DS_CreateLayer(ds, lyrname.c_str(), 0, wkbPoint25D, 0); 
    227276        if (0 == lyr) 
    228277        { 
    229             throw std::runtime_error(ogrname + " layer  cration failed"); 
    230         } 
    231         
     278            throw std::runtime_error(out_file + " layer  cration failed"); 
     279        } 
     280 
    232281        // Prepare layer schema 
    233282        create_layer_def(lyr); 
     
    238287        std::cout << "Translating " << reader.GetHeader().GetPointRecordsCount() << " points..."; 
    239288 
    240         ogr_wrapper<OGRFeatureH> feat(OGR_F_Create(OGR_L_GetLayerDefn(lyr)), 
    241                                       OGR_F_Destroy); 
     289        ogr_wrapper<OGRFeatureH> feat(OGR_F_Create(OGR_L_GetLayerDefn(lyr)), OGR_F_Destroy); 
    242290 
    243291        while (reader.ReadNextPoint()) 
     
    252300            OGR_F_SetFieldDouble(feat, 5, p.GetTime()); 
    253301 
    254             ogr_wrapper<OGRGeometryH> geom(OGR_G_CreateGeometry(wkbPoint25D), 
    255                                            OGR_G_DestroyGeometry); 
     302            ogr_wrapper<OGRGeometryH> geom(OGR_G_CreateGeometry(wkbPoint25D), OGR_G_DestroyGeometry); 
    256303            OGR_G_SetPoint(geom, 0, p.GetX(), p.GetY(), p.GetZ()); 
    257304            if (OGRERR_NONE != OGR_F_SetGeometry(feat, geom)) 
     
    271318    { 
    272319        std::cerr << "Error: " << e.what() << std::endl; 
     320        rc = -1; 
    273321    } 
    274322    catch (...) 
    275323    { 
    276324        std::cerr << "Unknown error\n"; 
    277     } 
    278  
    279     return 0; 
    280 } 
    281  
     325        rc = -1; 
     326    } 
     327 
     328    return rc; 
     329} 
     330 
Note: See TracChangeset for help on using the changeset viewer.