Permalink
Browse files

tools: Finish working rpc_mapproject

This is a debug utility to help me determine quality differences
between gdal's and my math.
  • Loading branch information...
1 parent bc563b8 commit bd1138f5d1afbda9dd6c01fa77eb873629e2cfe8 Zack Moratto committed May 17, 2012
Showing with 214 additions and 29 deletions.
  1. +1 −0 .gitignore
  2. +31 −29 configure.ac
  3. +6 −0 src/asp/Tools/Makefile.am
  4. +176 −0 src/asp/Tools/rpc_mapproject.cc
View
@@ -104,6 +104,7 @@ src/asp/Tools/reconstruct
src/asp/Tools/results
src/asp/Tools/rmax2cahvor
src/asp/Tools/rmax_adjust
+src/asp/Tools/rpc_mapproject
src/asp/Tools/stereo
src/asp/Tools/stereo_corr
src/asp/Tools/stereo_fltr
View
@@ -389,27 +389,28 @@ AX_MODULE(SESSIONS, [src/asp/Sessions], [libaspSessions.la], yes, [CORE], [XERCE
AX_MODULE(MPI, [src/asp/MPI], [], no, [VW_BUNDLEADJUSTMENT ISISIO CORE], [BOOST_SERIALIZATION BOOST_MPI OPEN_MPI VW_ALL])
# Tools
-AX_APP(STEREO, [src/asp/Tools], yes, [SESSIONS])
-AX_APP(ORTHOPROJECT, [src/asp/Tools], yes, [SESSIONS])
+AX_APP(ALIGNDEM, [src/asp/Tools], no, [VW_MOSAIC VW_CARTOGRAPHY VW_INTEREST_POINT CORE])
AX_APP(BUNDLEADJUST, [src/asp/Tools], no, [VW_BUNDLEADJUSTMENT SESSIONS])
-AX_APP(ORBITVIZ, [src/asp/Tools], yes, [SESSIONS])
-AX_APP(DISPARITYDEBUG, [src/asp/Tools], yes, [VW_STEREO CORE])
-AX_APP(POINT2MESH, [src/asp/Tools], yes, [OPENSCENEGRAPH VW CORE])
-AX_APP(POINT2DEM, [src/asp/Tools], yes, [CORE])
-AX_APP(RMAX2CAHVOR, [src/asp/Tools], no, [SESSIONS])
-AX_APP(RMAXADJUST, [src/asp/Tools], no, [VW_BUNDLEADJUSTMENT SESSIONS])
AX_APP(BUNDLEVIS, [src/asp/Tools], no, [OPENSCENEGRAPH VW_BUNDLEADJUSTMENT CORE])
-AX_APP(ISISADJUST, [src/asp/Tools], yes, [VW_BUNDLEADJUSTMENT ISISIO CORE])
-AX_APP(RESULTS, [src/asp/Tools], no, [CORE ISISIO])
-AX_APP(RECONSTRUCT, [src/asp/Tools], no, [VW VW_PHOTOMETRY VW_CARTOGRAPHY ])
-AX_APP(ALIGNDEM, [src/asp/Tools], no, [VW_MOSAIC VW_CARTOGRAPHY VW_INTEREST_POINT CORE])
-AX_APP(GEODIFF, [src/asp/Tools], no, [BOOST VW VW_CARTOGRAPHY CORE])
AX_APP(DEMPROFILE, [src/asp/Tools], no, [VW VW_CARTOGRAPHY CORE])
-AX_APP(PLATEORTHOPROJECT,[src/asp/Tools], no, [VW_PLATE CORE SESSIONS ISISIO])
+AX_APP(DISPARITYDEBUG, [src/asp/Tools], yes, [VW_STEREO CORE])
+AX_APP(GEODIFF, [src/asp/Tools], no, [BOOST VW VW_CARTOGRAPHY CORE])
AX_APP(HSVMERGE, [src/asp/Tools], yes, [VW VW_CARTOGRAPHY CORE])
+AX_APP(ISISADJUST, [src/asp/Tools], yes, [VW_BUNDLEADJUSTMENT ISISIO CORE])
AX_APP(ISISADJUSTCAMERAERR, [src/asp/Tools], no, [VW_BUNDLEADJUSTMENT CORE ISISIO])
AX_APP(ISISADJUSTCNETCLIP, [src/asp/Tools], no,[VW_BUNDLEADJUSTMENT CORE ISISIO])
AX_APP(MER2CAMERA, [src/asp/Tools], yes, [VW VW_CAMERA CORE])
+AX_APP(ORBITVIZ, [src/asp/Tools], yes, [SESSIONS])
+AX_APP(ORTHOPROJECT, [src/asp/Tools], yes, [SESSIONS])
+AX_APP(PLATEORTHOPROJECT,[src/asp/Tools], no, [VW_PLATE CORE SESSIONS ISISIO])
+AX_APP(POINT2DEM, [src/asp/Tools], yes, [CORE])
+AX_APP(POINT2MESH, [src/asp/Tools], yes, [OPENSCENEGRAPH VW CORE])
+AX_APP(RECONSTRUCT, [src/asp/Tools], no, [VW VW_PHOTOMETRY VW_CARTOGRAPHY ])
+AX_APP(RESULTS, [src/asp/Tools], no, [CORE ISISIO])
+AX_APP(RMAX2CAHVOR, [src/asp/Tools], no, [SESSIONS])
+AX_APP(RMAXADJUST, [src/asp/Tools], no, [VW_BUNDLEADJUSTMENT SESSIONS])
+AX_APP(RPCMAPPROJECT, [src/asp/Tools], yes, [VW VW_CAMERA VW_CARTOGRAPHY CORE SESSIONS])
+AX_APP(STEREO, [src/asp/Tools], yes, [SESSIONS])
# Toolkits (like module, but doesn't build a library)
AX_MODULE(CONTROLNETTK, [src/asp/ControlNetTK], [], no, [VW_BUNDLEADJUSTMENT CORE ISISIO], [BOOST])
@@ -423,28 +424,29 @@ AM_CONDITIONAL(MAKE_MODULE_SESSIONS, [test "$MAKE_MODULE_SESSIONS" = "yes"])
AM_CONDITIONAL(MAKE_MODULE_MPI, [test "$MAKE_MODULE_MPI" = "yes"])
AM_CONDITIONAL(MAKE_MODULE_CONTROLNETTK, [test "$MAKE_MODULE_CONTROLNETTK" = "yes"])
-AM_CONDITIONAL(MAKE_APP_STEREO, [test "$MAKE_APP_STEREO" = "yes"])
-AM_CONDITIONAL(MAKE_APP_STEREOGUI, [test "$MAKE_APP_STEREOGUI" = "yes"])
-AM_CONDITIONAL(MAKE_APP_ORTHOPROJECT, [test "$MAKE_APP_ORTHOPROJECT" = "yes"])
+AM_CONDITIONAL(MAKE_APP_ALIGNDEM, [test "$MAKE_APP_ALIGNDEM" = "yes"])
AM_CONDITIONAL(MAKE_APP_BUNDLEADJUST, [test "$MAKE_APP_BUNDLEADJUST" = "yes"])
-AM_CONDITIONAL(MAKE_APP_ORBITVIZ, [test "$MAKE_APP_ORBITVIZ" = "yes"])
-AM_CONDITIONAL(MAKE_APP_DISPARITYDEBUG, [test "$MAKE_APP_DISPARITYDEBUG" = "yes"])
-AM_CONDITIONAL(MAKE_APP_POINT2MESH, [test "$MAKE_APP_POINT2MESH" = "yes"])
-AM_CONDITIONAL(MAKE_APP_POINT2DEM, [test "$MAKE_APP_POINT2DEM" = "yes"])
-AM_CONDITIONAL(MAKE_APP_RMAX2CAHVOR, [test "$MAKE_APP_RMAX2CAHVOR" = "yes"])
-AM_CONDITIONAL(MAKE_APP_RMAXADJUST, [test "$MAKE_APP_RMAXADJUST" = "yes"])
AM_CONDITIONAL(MAKE_APP_BUNDLEVIS, [test "$MAKE_APP_BUNDLEVIS" = "yes"])
-AM_CONDITIONAL(MAKE_APP_ISISADJUST, [test "$MAKE_APP_ISISADJUST" = "yes"])
-AM_CONDITIONAL(MAKE_APP_RESULTS, [test "$MAKE_APP_RESULTS" = "yes"])
-AM_CONDITIONAL(MAKE_APP_RECONSTRUCT, [test "$MAKE_APP_RECONSTRUCT" = "yes"])
-AM_CONDITIONAL(MAKE_APP_ALIGNDEM, [test "$MAKE_APP_ALIGNDEM" = "yes"])
-AM_CONDITIONAL(MAKE_APP_GEODIFF, [test "$MAKE_APP_GEODIFF" = "yes"])
AM_CONDITIONAL(MAKE_APP_DEMPROFILE, [test "$MAKE_APP_DEMPROFILE" = "yes"])
-AM_CONDITIONAL(MAKE_APP_PLATEORTHOPROJECT, [test "$MAKE_APP_PLATEORTHOPROJECT" = "yes"])
+AM_CONDITIONAL(MAKE_APP_DISPARITYDEBUG, [test "$MAKE_APP_DISPARITYDEBUG" = "yes"])
+AM_CONDITIONAL(MAKE_APP_GEODIFF, [test "$MAKE_APP_GEODIFF" = "yes"])
AM_CONDITIONAL(MAKE_APP_HSVMERGE, [test "$MAKE_APP_HSVMERGE" = "yes"])
+AM_CONDITIONAL(MAKE_APP_ISISADJUST, [test "$MAKE_APP_ISISADJUST" = "yes"])
AM_CONDITIONAL(MAKE_APP_ISISADJUSTCAMERAERR, [test "$MAKE_APP_ISISADJUSTCAMERAERR" = "yes"])
AM_CONDITIONAL(MAKE_APP_ISISADJUSTCNETCLIP, [test "$MAKE_APP_ISISADJUSTCNETCLIP" = "yes"])
AM_CONDITIONAL(MAKE_APP_MER2CAMERA, [test "$MAKE_APP_MER2CAMERA" = "yes"])
+AM_CONDITIONAL(MAKE_APP_ORBITVIZ, [test "$MAKE_APP_ORBITVIZ" = "yes"])
+AM_CONDITIONAL(MAKE_APP_ORTHOPROJECT, [test "$MAKE_APP_ORTHOPROJECT" = "yes"])
+AM_CONDITIONAL(MAKE_APP_PLATEORTHOPROJECT, [test "$MAKE_APP_PLATEORTHOPROJECT" = "yes"])
+AM_CONDITIONAL(MAKE_APP_POINT2DEM, [test "$MAKE_APP_POINT2DEM" = "yes"])
+AM_CONDITIONAL(MAKE_APP_POINT2MESH, [test "$MAKE_APP_POINT2MESH" = "yes"])
+AM_CONDITIONAL(MAKE_APP_RECONSTRUCT, [test "$MAKE_APP_RECONSTRUCT" = "yes"])
+AM_CONDITIONAL(MAKE_APP_RESULTS, [test "$MAKE_APP_RESULTS" = "yes"])
+AM_CONDITIONAL(MAKE_APP_RMAX2CAHVOR, [test "$MAKE_APP_RMAX2CAHVOR" = "yes"])
+AM_CONDITIONAL(MAKE_APP_RMAXADJUST, [test "$MAKE_APP_RMAXADJUST" = "yes"])
+AM_CONDITIONAL(MAKE_APP_RPCMAPPROJECT, [test "$MAKE_APP_RPCMAPPROJECT" = "yes"])
+AM_CONDITIONAL(MAKE_APP_STEREO, [test "$MAKE_APP_STEREO" = "yes"])
+AM_CONDITIONAL(MAKE_APP_STEREOGUI, [test "$MAKE_APP_STEREOGUI" = "yes"])
##################################################
# final processing
@@ -158,6 +158,12 @@ if MAKE_APP_MER2CAMERA
mer2camera_LDADD = $(APP_MER2CAMERA_LIBS)
endif
+if MAKE_APP_RPCMAPPROJECT
+ bin_PROGRAMS += rpc_mapproject
+ rpc_mapproject_SOURCES = rpc_mapproject.cc
+ rpc_mapproject_LDADD = $(APP_RPCMAPPROJECT_LIBS)
+endif
+
# Scripts
##############################################################################
@@ -0,0 +1,176 @@
+// __BEGIN_LICENSE__
+// Copyright (c) 2009-2012, United States Government as represented by the
+// Administrator of the National Aeronautics and Space Administration. All
+// rights reserved.
+//
+// The NGT platform is licensed under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance with the
+// License. You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+// __END_LICENSE__
+
+#include <vw/Core.h>
+#include <vw/FileIO.h>
+#include <vw/Image.h>
+#include <vw/Cartography.h>
+using namespace vw;
+
+#include <asp/Core/Macros.h>
+#include <asp/Core/Common.h>
+#include <asp/Sessions/DG/StereoSessionDG.h>
+#include <asp/Sessions/DG/XML.h>
+#include <asp/Sessions/RPC/RPCMapTransform.h>
+namespace po = boost::program_options;
+namespace fs = boost::filesystem;
+
+#include "ogr_spatialref.h"
+
+struct Options : asp::BaseOptions {
+ // Input
+ std::string dem_file, image_file, camera_model_file, output_file;
+
+ // Settings
+ std::string target_srs_string;
+ float target_resolution;
+ double nodata_value;
+};
+
+void handle_arguments( int argc, char *argv[], Options& opt ) {
+ po::options_description general_options("");
+ general_options.add_options()
+ ("nodata-value", po::value(&opt.nodata_value), "Nodata value to use on output.")
+ ("t_srs", po::value(&opt.target_srs_string)->required(), "Target spatial reference set. This mimicks the gdal option.")
+ ("tr", po::value(&opt.target_resolution)->default_value(0), "Set output file resolution (in target georeferenced units per pixel)");
+
+ general_options.add( asp::BaseOptionsDescription(opt) );
+
+ po::options_description positional("");
+ positional.add_options()
+ ("dem", po::value(&opt.dem_file)->required())
+ ("camera-image", po::value(&opt.image_file)->required())
+ ("camera-model", po::value(&opt.camera_model_file)->required())
+ ("output-file", po::value(&opt.output_file));
+
+ po::positional_options_description positional_desc;
+ positional_desc.add("dem", 1);
+ positional_desc.add("camera-image",1);
+ positional_desc.add("camera-model",1);
+ positional_desc.add("output-file",1);
+
+ std::string usage("[options] <dem> <camera-image> <camera-model> <output>");
+ po::variables_map vm =
+ asp::check_command_line( argc, argv, opt, general_options,
+ positional, positional_desc, usage );
+
+ if ( opt.output_file.empty() )
+ opt.output_file = fs::basename( opt.image_file ) + "_rpcmapped.tif";
+}
+
+int main( int argc, char* argv[] ) {
+
+ Options opt;
+ try {
+ handle_arguments( argc, argv, opt );
+
+ // Load DEM
+ boost::shared_ptr<DiskImageResource>
+ dem_rsrc( DiskImageResource::open( opt.dem_file ) );
+ cartography::GeoReference dem_georef;
+ read_georeference( dem_georef, opt.dem_file );
+
+ // Read projection. Work out output bounding box in points using
+ // original camera model.
+ cartography::GeoReference target_georef;
+ boost::replace_first(opt.target_srs_string,
+ "IAU2000:","DICT:IAU2000.wkt,");
+ VW_OUT(DebugMessage,"asp") << "Asking GDAL to decypher: \""
+ << opt.target_srs_string << "\"\n";
+ OGRSpatialReference gdal_spatial_ref;
+ if (gdal_spatial_ref.SetFromUserInput( opt.target_srs_string.c_str() ))
+ vw_throw( ArgumentErr() << "Failed to parse: \"" << opt.target_srs_string << "\"." );
+ char *wkt = NULL;
+ gdal_spatial_ref.exportToWkt( &wkt );
+ std::string wkt_string(wkt);
+ delete[] wkt;
+
+ target_georef.set_wkt( wkt_string );
+
+ // Work out target resolution and image size
+ asp::StereoSessionDG session;
+ boost::shared_ptr<camera::CameraModel>
+ camera_model( session.camera_model( opt.image_file, opt.camera_model_file ) );
+ Vector2i image_size = asp::file_image_size( opt.image_file );
+ BBox2 point_bounds =
+ opt.target_resolution <= 0 ?
+ camera_bbox( target_georef, camera_model,
+ image_size.x(), image_size.y(),
+ opt.target_resolution ) :
+ camera_bbox( target_georef, camera_model,
+ image_size.x(), image_size.y() );
+
+ {
+ Matrix3x3 transform = math::identity_matrix<3>();
+ transform(0,0) = opt.target_resolution;
+ transform(1,1) = -opt.target_resolution;
+ transform(0,2) = point_bounds.min().x();
+ transform(1,2) = point_bounds.max().y();
+ if ( target_georef.pixel_interpretation() ==
+ cartography::GeoReference::PixelAsArea ) {
+ transform(0,2) -= 0.5 * opt.target_resolution;
+ transform(1,2) += 0.5 * opt.target_resolution;
+ }
+ target_georef.set_transform( transform );
+ }
+ vw_out() << "Output Georeference:\n" << target_georef << std::endl;
+
+ BBox2i target_image_size =
+ target_georef.point_to_pixel_bbox( point_bounds );
+ vw_out() << "Creating output file that is " << target_image_size << ".\n";
+
+ // Load RPC
+ asp::RPCXML xml;
+ xml.read_from_file( opt.camera_model_file );
+
+ boost::shared_ptr<DiskImageResource>
+ src_rsrc( DiskImageResource::open( opt.image_file ) );
+
+ // Raster output image
+ if ( src_rsrc->has_nodata_read() ) {
+ asp::block_write_gdal_image( opt.output_file,
+ apply_mask(transform( create_mask( DiskImageView<float>( src_rsrc), src_rsrc->nodata_read() ),
+ asp::RPCMapTransform( *xml.rpc_ptr(),
+ target_georef,
+ dem_georef,
+ dem_rsrc ),
+ target_image_size.width(),
+ target_image_size.height(),
+ ValueEdgeExtension<PixelMask<float> >( PixelMask<float>() ),
+ BicubicInterpolation() ),
+ src_rsrc->nodata_read() ),
+ target_georef, opt,
+ TerminalProgressCallback("","") );
+ } else {
+ asp::block_write_gdal_image(opt.output_file,
+ transform( DiskImageView<float>( src_rsrc ),
+ asp::RPCMapTransform( *xml.rpc_ptr(),
+ target_georef,
+ dem_georef,
+ dem_rsrc ),
+ target_image_size.width(),
+ target_image_size.height(),
+ ZeroEdgeExtension(),
+ BicubicInterpolation() ),
+ target_georef, opt,
+ TerminalProgressCallback("","") );
+ }
+
+ } ASP_STANDARD_CATCHES;
+
+ return 0;
+}

0 comments on commit bd1138f

Please sign in to comment.