USGS

Isis 3.0 Application Source Code Reference

Home

autoseed.cpp

Go to the documentation of this file.
00001 #include "Isis.h"
00002 
00003 #include <map>
00004 #include <sstream>
00005 
00006 #include "geos/util/GEOSException.h"
00007 #include "Process.h"
00008 #include "Pvl.h"
00009 #include "SerialNumberList.h"
00010 #include "ImageOverlapSet.h"
00011 #include "ImageOverlap.h"
00012 #include "ControlNet.h"
00013 #include "UniversalGroundMap.h"
00014 #include "PolygonSeederFactory.h"
00015 #include "PolygonSeeder.h"
00016 #include "PolygonTools.h"
00017 #include "ID.h"
00018 #include "iException.h"
00019 #include "CameraFactory.h"
00020 
00021 using namespace std;
00022 using namespace Isis;
00023 
00024 void IsisMain() {
00025 
00026   UserInterface &ui = Application::GetUserInterface();
00027   SerialNumberList serialNumbers(ui.GetFilename("FROMLIST"));
00028 
00029   // Get the AutoSeed PVL internalized
00030   Pvl seedDef(ui.GetFilename("SEEDDEF"));
00031 
00032   // Grab the labels from the first filename in the SerialNumberList to get
00033   // some info
00034   Pvl cubeLab(serialNumbers.Filename(0));
00035 
00036   // Construct a Projection for converting between Lon/Lat and X/Y
00037   // This is used inside the seeding algorithms.
00038   // Note: Should this be an option to include this in the program?
00039   PvlGroup inst = cubeLab.FindGroup("Instrument", Pvl::Traverse);
00040   string target = inst["TargetName"];
00041   PvlGroup radii = Projection::TargetRadii(target);
00042   Isis::Pvl maplab;
00043   maplab.AddGroup(Isis::PvlGroup("Mapping"));
00044   Isis::PvlGroup &mapGroup = maplab.FindGroup("Mapping");
00045   mapGroup += Isis::PvlKeyword("EquatorialRadius",(string)radii["EquatorialRadius"]);
00046   mapGroup += Isis::PvlKeyword("PolarRadius",(string)radii["PolarRadius"]);
00047   mapGroup += Isis::PvlKeyword("LatitudeType","Planetocentric");
00048   mapGroup += Isis::PvlKeyword("LongitudeDirection","PositiveEast");
00049   mapGroup += Isis::PvlKeyword("LongitudeDomain",360);
00050   mapGroup += Isis::PvlKeyword("CenterLatitude",0);
00051   mapGroup += Isis::PvlKeyword("CenterLongitude",0);
00052   mapGroup += Isis::PvlKeyword("ProjectionName","Sinusoidal");
00053   PolygonSeeder *seeder = PolygonSeederFactory::Create(seedDef);
00054   Projection *proj = Isis::ProjectionFactory::Create(maplab);
00055 
00056   // Create the control net to store the points in.
00057   ControlNet cnet;
00058   cnet.SetType (ControlNet::ImageToGround);
00059   cnet.SetTarget (target);
00060   string networkId = ui.GetString("NETWORKID");
00061   cnet.SetNetworkId(networkId);
00062   cnet.SetUserName(Isis::Application::UserName());
00063   cnet.SetDescription(ui.GetString("DESCRIPTION"));
00064 
00065   // Set up an automatic id generator for the point ids
00066   ID pointId = ID(ui.GetString("POINTID"));
00067 
00068   // Find all the overlaps between the images in the FROMLIST
00069   // The overlap polygon coordinates are in Lon/Lat order
00070   ImageOverlapSet overlaps;
00071   overlaps.ReadImageOverlaps(ui.GetFilename("OVERLAPLIST"));
00072 
00073   // Create a Universal Ground Map (UGM) for each image in the list
00074   int stats_noOverlap = 0;
00075   int stats_tolerance = 0;
00076 
00077   map<std::string, UniversalGroundMap*> gMaps;
00078   for (int sn=0; sn<serialNumbers.Size(); ++sn) {
00079     // Create the UGM for the cube associated with this SN
00080     Pvl lab = Pvl(serialNumbers.Filename(sn));
00081     gMaps.insert(std::pair<std::string, UniversalGroundMap*>
00082                  (serialNumbers.SerialNumber(sn), new UniversalGroundMap(lab)));
00083   }
00084 
00085   stringstream errors (stringstream::in | stringstream::out);
00086   int errorNum = 0;
00087 
00088   // Process each overlap area
00089   //   Seed measurments into it
00090   //   Store the measurments in the control network
00091 
00092   bool previousControlNet = ui.WasEntered("CNET");
00093 
00094   vector< geos::geom::Point *> points;
00095   if ( previousControlNet ) {
00096 
00097     ControlNet precnet( ui.GetFilename("CNET") );
00098 
00099     Progress progress;
00100     progress.SetText("Calculating Provided Control Net");
00101     progress.SetMaximumSteps(precnet.Size());
00102     progress.CheckStatus();
00103     
00104     for ( int i = 0 ; i < precnet.Size(); i ++ ) {
00105       ControlPoint cp = precnet[i];
00106       ControlMeasure cm = cp[0];
00107       if( cp.HasReference() ) {
00108         cm = cp[cp.ReferenceIndex()];
00109       }
00110 
00111       string c = serialNumbers.Filename( cm.CubeSerialNumber() );
00112       Pvl cubepvl( c );
00113       Camera *cam = CameraFactory::Create( cubepvl );
00114       cam->SetImage( cm.Sample(), cm.Line() );
00115 
00116       points.push_back( Isis::globalFactory.createPoint(
00117         geos::geom::Coordinate(cam->UniversalLongitude(),cam->UniversalLatitude()) ) );
00118 
00119       delete cam;
00120       cam = NULL;
00121 
00122       progress.CheckStatus();
00123     }
00124 
00125   }
00126 
00127   Progress progress;
00128   progress.SetText("Seeding Points");
00129   progress.SetMaximumSteps(overlaps.Size());
00130   progress.CheckStatus();
00131 
00132   for (int ov=0; ov<overlaps.Size(); ++ov) {
00133     if (overlaps[ov]->Size() == 1) {
00134       stats_noOverlap++;
00135       progress.CheckStatus();
00136       continue;
00137     }
00138 
00139     // Checks if this overlap was already seeded
00140     if ( previousControlNet ) {
00141 
00142       // Grabs the Multipolygon's Envelope for Lat/Lon comparison
00143       const geos::geom::MultiPolygon *lonLatPoly = overlaps[ov]->Polygon();
00144 
00145       bool overlapSeeded = false;
00146       for( unsigned int j = 0; j < lonLatPoly->getNumGeometries()  &&  !overlapSeeded; j ++ ) {
00147         const geos::geom::Geometry *lonLatGeom = lonLatPoly->getGeometryN( j );
00148 
00149         // Checks if Control Point is in the MultiPolygon using Lon/Lat
00150         for ( unsigned int i = 0 ; i < points.size()  &&  !overlapSeeded; i ++ ) {
00151           if ( lonLatGeom->contains(points[i]) ) overlapSeeded = true;
00152         }
00153       }
00154 
00155       if( overlapSeeded ) continue;
00156     }
00157 
00158     // Seed this overlap with points
00159     const geos::geom::MultiPolygon *mp = overlaps[ov]->Polygon();
00160     std::vector<geos::geom::Point*> seed;
00161 
00162     try {
00163       seed = seeder->Seed(mp, proj);
00164     }
00165     catch (iException &e) {
00166 
00167       if ( ui.WasEntered("ERRORS") ) {
00168 
00169         if ( errorNum > 0 ) {
00170           errors << endl;
00171         }
00172         errorNum ++;
00173 
00174         errors << e.PvlErrors().Group(0).FindKeyword("Message")[0];
00175         for (int serNum = 0; serNum < overlaps[ov]->Size(); serNum++) {
00176           if ( serNum == 0 ) {
00177             errors << ": ";
00178           }
00179           else {
00180             errors << ", ";
00181           }
00182           errors << (*overlaps[ov])[serNum];
00183         }
00184       }
00185 
00186       e.Clear();
00187       continue;
00188     }
00189 
00190     // No points were seeded in this polygon, so collect some stats and move on
00191     if (seed.size() == 0) {
00192       stats_tolerance++;
00193       progress.CheckStatus();
00194       continue;
00195     }
00196 
00197     //   Create a control point for each seeded point in this overlap
00198     for (unsigned int point=0; point<seed.size(); ++point) {
00199 
00200       ControlPoint control;
00201       control.SetId (pointId.Next());
00202       control.SetType (ControlPoint::Tie);
00203 
00204       // Create a measurment at this point for each image in the overlap area
00205       for (int sn=0; sn<overlaps[ov]->Size(); ++sn) {
00206 
00207         // Get the line/sample of the lat/lon for this cube
00208         UniversalGroundMap *gmap = gMaps[(*overlaps[ov])[sn]];
00209         gmap->SetUniversalGround(seed[point]->getY(), seed[point]->getX());
00210 
00211         // Put the line/samp into a measurment
00212         ControlMeasure measurment;
00213         measurment.SetCoordinate (gmap->Sample(), gmap->Line(),
00214                                   ControlMeasure::Estimated);
00215         measurment.SetType(ControlMeasure::Estimated);
00216         measurment.SetCubeSerialNumber((*(overlaps[ov]))[sn]);
00217         measurment.SetDateTime();
00218         measurment.SetChooserName("Application autoseed");
00219         if (sn == 0) measurment.SetReference(true);
00220         control.Add(measurment);
00221       }
00222 
00223       cnet.Add(control);
00224       delete seed[point];
00225 
00226     } // End of create control points loop
00227 
00228     progress.CheckStatus();
00229   } // End of seeding loop
00230 
00231   // All done with the UGMs so delete them
00232   for (unsigned int sn=0; sn<gMaps.size(); ++sn) {
00233     UniversalGroundMap *gmap = gMaps[serialNumbers.SerialNumber(sn)];
00234     delete gmap;
00235   }
00236 
00237   for ( unsigned int i = 0 ; i < points.size(); i ++ ) {
00238     delete points[i];
00239     points[i] = NULL;
00240   }
00241 
00242   gMaps.clear();
00243 
00244   // Write the control network out
00245   cnet.Write(ui.GetFilename("TO"));
00246 
00247   //Log the ERRORS file
00248   if( ui.WasEntered("ERRORS") ) {
00249     string errorname = ui.GetFilename("ERRORS");
00250     std::ofstream errorsfile;
00251     errorsfile.open( errorname.c_str() );
00252     errorsfile << errors.str();
00253     errorsfile.close();
00254   }
00255 
00256   // create SeedDef group and add to print.prt
00257   PvlGroup pluginInfo = seeder->PluginParameters("SeedDefinition");
00258   Application::Log(pluginInfo);
00259 
00260   delete proj;
00261   proj = NULL;
00262 
00263   delete seeder;
00264   seeder = NULL;
00265 
00266 }
00267