osgEarth 2.1.1

/home/cube/sources/osgearth/src/applications/osgearth_featureinfo/osgearth_featureinfo.cpp

Go to the documentation of this file.
00001 /* -*-c++-*- */
00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
00003 * Copyright 2008-2010 Pelican Mapping
00004 * http://osgearth.org
00005 *
00006 * osgEarth is free software; you can redistribute it and/or modify
00007 * it under the terms of the GNU Lesser General Public License as published by
00008 * the Free Software Foundation; either version 2 of the License, or
00009 * (at your option) any later version.
00010 *
00011 * This program is distributed in the hope that it will be useful,
00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 * GNU Lesser General Public License for more details.
00015 *
00016 * You should have received a copy of the GNU Lesser General Public License
00017 * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018 */
00019 
00020 #include <osg/Notify>
00021 
00022 #include <osgEarthDrivers/feature_ogr/OGRFeatureOptions>
00023 
00024 using namespace osgEarth::Features;
00025 using namespace osgEarth::Drivers;
00026 using namespace osgEarth::Symbology;
00027 
00028 #include <osgEarthFeatures/GeometryUtils>
00029 
00030 std::string attributeTypeToString( AttributeType type )
00031 {
00032     switch (type)
00033     {
00034     case ATTRTYPE_BOOL: return "Boolean";
00035     case ATTRTYPE_DOUBLE: return "Double";
00036     case ATTRTYPE_INT: return "Integer";
00037     case ATTRTYPE_STRING: return "String";
00038     default:  return "Unspecified";
00039     }
00040 }
00041 
00042 std::string indent = "    ";
00043 
00044 void printStats(FeatureSource* features)
00045 {
00046     std::cout << "Feature Count:  " << features->getFeatureCount() << std::endl;
00047     std::cout << "Geometry Type:  " << osgEarth::Symbology::Geometry::toString( features->getGeometryType() ) << std::endl;
00048 
00049     //Print the schema
00050     const FeatureSchema schema = features->getSchema();
00051     std::cout << "Schema:" << std::endl;
00052     for (FeatureSchema::const_iterator itr = schema.begin(); itr != schema.end(); ++itr)
00053     {
00054         std::cout << indent << itr->first << ": " << attributeTypeToString(itr->second) << std::endl;
00055     }
00056     std::cout << std::endl;
00057 }
00058 
00059 void printFeature( Feature* feature )
00060 {
00061     std::cout << "FID: " << feature->getFID() << std::endl;
00062     for (AttributeTable::const_iterator itr = feature->getAttrs().begin(); itr != feature->getAttrs().end(); ++itr)
00063     {
00064         std::cout 
00065             << indent 
00066             << itr->first << "=" << itr->second.getString() << " ("
00067             << (itr->second.first == ATTRTYPE_INT?    "integer" :
00068                 itr->second.first == ATTRTYPE_DOUBLE? "double" :
00069                 itr->second.first == ATTRTYPE_BOOL?   "bool" :
00070                 "string")
00071             << ")" << std::endl;
00072     }
00073 
00074     //Print out the geometry
00075     Geometry* geom = feature->getGeometry();
00076     if (geom)
00077     {
00078         std::cout << indent << geometryToWkt( geom ) << std::endl;
00079     }
00080     std::cout << std::endl;
00081 }
00082 
00083 void printAllFeatures(FeatureSource* features)
00084 {
00085     osg::ref_ptr< FeatureCursor > cursor = features->createFeatureCursor();
00086     while (cursor->hasMore())
00087     {
00088         osg::ref_ptr< Feature > feature = cursor->nextFeature();
00089         printFeature( feature.get() );
00090     }
00091 }
00092 
00093 int
00094 usage( const std::string& msg )
00095 {
00096     if ( !msg.empty() )
00097     {
00098         std::cout << msg << std::endl;
00099     }
00100 
00101     std::cout
00102         << std::endl
00103         << "USAGE: osgearth_featureinfo [options] filename" << std::endl
00104         << std::endl
00105         << "    --printfeatures                   ; Prints all features in the source" << std::endl
00106         << "    --delete fid                      ; Deletes the given FID from the source." << std::endl
00107         << "    --fid fid                         ; Displays the given FID." << std::endl
00108         << std::endl;
00109 
00110     return -1;
00111 }
00112 
00113 int main(int argc, char** argv)
00114 {
00115     osg::ArgumentParser arguments(&argc,argv);
00116 
00117     if (argc < 2)
00118     {
00119         return usage("");
00120     }
00121 
00122 
00123     std::vector< FeatureID > toDelete;
00124     int fid;
00125     while (arguments.read("--delete", fid))
00126     {
00127         toDelete.push_back( fid );
00128     }
00129 
00130     std::vector< FeatureID > fids;
00131     while (arguments.read("--fid", fid))
00132     {
00133         fids.push_back( fid );
00134     }
00135     
00136     bool printFeatures = false;
00137     if (arguments.read("--printfeatures" )) printFeatures = true;
00138 
00139     std::string filename;
00140 
00141     //Get the first argument that is not an option
00142     for(int pos=1;pos<arguments.argc();++pos)
00143     {
00144         if (!arguments.isOption(pos))
00145         {
00146             filename  = arguments[ pos ];
00147         }
00148     }
00149 
00150     if (filename.empty())
00151     {
00152         return usage( "Please provide a filename" );
00153     }
00154 
00155     bool write = toDelete.size() > 0;
00156 
00157 
00158 
00159     //Open the feature source
00160     OGRFeatureOptions featureOpt;
00161     featureOpt.url() = filename;
00162     featureOpt.openWrite() = write;
00163 
00164     osg::ref_ptr< FeatureSource > features = FeatureSourceFactory::create( featureOpt );
00165     features->initialize("");
00166     features->getFeatureProfile();
00167 
00168     //Delete any features if requested
00169     if (toDelete.size() > 0)
00170     {
00171         for (unsigned int i = 0; i < toDelete.size(); ++i)
00172         {
00173             FeatureID fid = toDelete[i];
00174             std::cout << "Deleting Feature " << fid << std::endl;
00175             features->deleteFeature( fid );
00176         }
00177     }
00178     else if (fids.size() > 0)
00179     {
00180         //Print out any specific FIDs
00181         for (unsigned int i = 0; i < fids.size(); ++i)
00182         {
00183             FeatureID fid = fids[i];
00184             osg::ref_ptr< Feature > feature = features->getFeature( fid );
00185             if (feature.valid())
00186             {
00187                 printFeature( feature.get() );
00188             }
00189             else
00190             {
00191                 std::cout << "Couldn't get feature " << fid << std::endl;
00192             }
00193         }
00194     }
00195     else
00196     {
00197         //Print out feature info
00198         printStats( features.get() );
00199 
00200         if (printFeatures)
00201         {
00202             printAllFeatures( features.get() );
00203         }
00204     }
00205 
00206     return 0;
00207 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines