object_ptr interpreter::make_text (parameters &command) { TRACE ('f', command); string first = shift(command); string font = ""; double size = 12.0; //Default size is 12 for text //If conversion succeeds, then next string on list is the fontname, //otherwise the string that couldn't be converted to a double is //the fontname. try{ size = from_string<double>(first); font = shift(command); } catch (runtime_error &error){ font = first; } //At this point, the rest of the words are text to be printed. string textdata{}; parameters::const_iterator itor = command.begin(); parameters::const_iterator end = command.end(); --end; for(; itor != end; ++itor){ textdata += *itor + ' '; }textdata += *itor;//remove trailing space return make_shared<text> (font, points(size), textdata); }
datasource_ptr datasource_cache::create(const parameters& params, bool bind) { boost::optional<std::string> type = params.get<std::string>("type"); if ( ! type) { throw config_error(std::string("Could not create datasource. Required ") + "parameter 'type' is missing"); } #ifdef MAPNIK_THREADSAFE mutex::scoped_lock lock(mutex_); #endif datasource_ptr ds; std::map<std::string,boost::shared_ptr<PluginInfo> >::iterator itr=plugins_.find(*type); if ( itr == plugins_.end() ) { throw config_error(std::string("Could not create datasource. No plugin ") + "found for type '" + * type + "' (searched in: " + plugin_directories() + ")"); } if ( ! itr->second->handle()) { throw std::runtime_error(std::string("Cannot load library: ") + lt_dlerror()); } // http://www.mr-edd.co.uk/blog/supressing_gcc_warnings #ifdef __GNUC__ __extension__ #endif create_ds* create_datasource = reinterpret_cast<create_ds*>(lt_dlsym(itr->second->handle(), "create")); if (! create_datasource) { throw std::runtime_error(std::string("Cannot load symbols: ") + lt_dlerror()); } #ifdef MAPNIK_LOG MAPNIK_LOG_DEBUG(datasource_cache) << "datasource_cache: Size=" << params.size(); parameters::const_iterator i = params.begin(); for (; i != params.end(); ++i) { MAPNIK_LOG_DEBUG(datasource_cache) << "datasource_cache: -- " << i->first << "=" << i->second; } #endif ds = datasource_ptr(create_datasource(params, bind), datasource_deleter()); MAPNIK_LOG_DEBUG(datasource_cache) << "datasource_cache: Datasource=" << ds << " type=" << type; return ds; }
static return_type extract(parameters const& params, std::string const& name, boost::optional<T> const& default_opt_value) { boost::optional<T> result(default_opt_value); parameters::const_iterator itr = params.find(name); if (itr != params.end()) { util::apply_visitor(value_extractor_visitor<T>(result),itr->second); } return result; }
static boost::python::tuple getstate(const parameters& p) { using namespace boost::python; dict d; parameters::const_iterator pos=p.begin(); while(pos!=p.end()) { d[pos->first] = pos->second; ++pos; } return boost::python::make_tuple(d); }
boost::python::list list_params(parameters& p) { boost::python::list l; parameters::const_iterator pos=p.begin(); while(pos!=p.end()) { boost::python::list vals; pickle_value serializer( vals ); mapnik::value_holder val = pos->second; boost::apply_visitor( serializer, val ); l.append(boost::python::make_tuple(pos->first,vals[0])); ++pos; } return l; }
boost::python::dict dict_params(parameters& p) { boost::python::dict d; parameters::const_iterator pos=p.begin(); while(pos!=p.end()) { boost::python::list vals; pickle_value serializer( vals ); mapnik::value_holder val = pos->second; boost::apply_visitor( serializer, val ); d[pos->first] = vals[0]; ++pos; } return d; }
chained_datasource::chained_datasource(const parameters& params, bool _bind) : datasource(params) { // Iterate over params and populate m_source_params // from all parameters that start with "src_" for (parameters::const_iterator iter=params.begin(); iter!=params.end(); iter++) { const std::string &name(iter->first); if (!boost::starts_with(name, "src_")) continue; const std::string sub_name(name.substr(4)); source_params_[sub_name]=iter->second; } if (_bind) { bind(); } }
static boost::python::tuple getstate(const parameters& p) { using namespace boost::python; dict d; parameters::const_iterator pos=p.begin(); while(pos!=p.end()) { boost::python::list vals; pickle_value serializer( vals ); mapnik::value_holder val = pos->second; boost::apply_visitor( serializer, val ); d[pos->first] = vals[0]; ++pos; } return boost::python::make_tuple(d); }
matches im_utility::diff(const std::string &im_file1, const std::string &im_file2, const parameters ¶ms, key_points *im1_kps, key_points *im2_kps) { using namespace cv; matches mt; std::vector<KeyPoint> im1_key_points, im2_key_points; std::vector<DMatch> matches; std::vector<DMatch> good_matches; double min_hessian = 400.0; if (params.find(key_hessian_threshold) != params.end()) min_hessian = params.at(key_hessian_threshold); double sigma = 2.0; if (params.find(key_match_threshold) != params.end()) sigma = params.at(key_match_threshold); int speedup = speedup_default; if (params.find(key_speedup) != params.end()) speedup = params.at(key_speedup); //auto info = getBuildInformation(); if (speedup == speedup_use_cuda) { using namespace gpu; DeviceInfo dev; auto name = dev.name(); GpuMat img1, img2; // upload data img1.upload(imread(im_file1, CV_LOAD_IMAGE_GRAYSCALE)); img2.upload(imread(im_file2, CV_LOAD_IMAGE_GRAYSCALE)); // detect keypoints & computing descriptors SURF_GPU surf; GpuMat kps_gpu1, kps_gpu2; GpuMat desc_gpu1, desc_gpu2; surf(img1, GpuMat(), kps_gpu1, desc_gpu1); surf(img2, GpuMat(), kps_gpu2, desc_gpu2); // matching descriptors BFMatcher_GPU matcher_gpu(NORM_L2); GpuMat trainIdx, distance; matcher_gpu.matchSingle(desc_gpu1, desc_gpu2, trainIdx, distance); // download results std::vector<float> desc1, desc2; surf.downloadKeypoints(kps_gpu1, im1_key_points); surf.downloadKeypoints(kps_gpu2, im2_key_points); surf.downloadDescriptors(desc_gpu1, desc1); surf.downloadDescriptors(desc_gpu2, desc2); BFMatcher_GPU::matchDownload(trainIdx, distance, matches); good_matches = matches; } else if (speedup == speedup_use_ocl) { using namespace ocl; DevicesInfo devs; getOpenCLDevices(devs, CVCL_DEVICE_TYPE_GPU); int dev = 0; if (params.find(key_ocl_dev) != params.end()) dev = params.at(key_ocl_dev); if (dev < 0 || dev >= devs.size()) dev = 0; setDevice(devs[dev]); SURF_OCL surf(800.0); BFMatcher_OCL matcher(NORM_L2); oclMat desc1, desc2; oclMat im1, im2; im1 = imread(im_file1, CV_LOAD_IMAGE_GRAYSCALE); im2 = imread(im_file2, CV_LOAD_IMAGE_GRAYSCALE); surf(im1, oclMat(), im1_key_points, desc1); surf(im2, oclMat(), im2_key_points, desc2); matcher.match(desc1, desc2, matches); double max_dist = 0; double min_dist = 100; for (int i = 0; i < desc1.rows; i++) { double dist = matches[i].distance; if (dist < min_dist) min_dist = dist; if (dist > max_dist) max_dist = dist; } for (int i = 0; i < desc1.rows; i++) { if (matches[i].distance <= max(sigma * min_dist, 0.02)) { good_matches.push_back(matches[i]); } } } else { auto get_file_desc = [](const std::string &filename, double min_hessian, std::vector<cv::KeyPoint> *out_key_points) { using namespace cv; Mat desc; Mat img = imread(filename, CV_LOAD_IMAGE_GRAYSCALE); if (!img.data) return desc; SurfFeatureDetector detecter(min_hessian); std::vector<KeyPoint> key_points; detecter.detect(img, key_points); if (out_key_points) *out_key_points = key_points; if (key_points.empty()) { std::cout << "no feature detected: " << filename << std::endl; return desc; } SurfDescriptorExtractor extractor; extractor.compute(img, key_points, desc); return desc; }; auto im1_desc = get_file_desc(im_file1, min_hessian, &im1_key_points); auto im2_desc = get_file_desc(im_file2, min_hessian, &im2_key_points); FlannBasedMatcher matcher; matcher.match(im1_desc, im2_desc, matches); double max_dist = 0; double min_dist = 100; for (int i = 0; i < im1_desc.rows; i++) { double dist = matches[i].distance; if (dist < min_dist) min_dist = dist; if (dist > max_dist) max_dist = dist; } for (int i = 0; i < im1_desc.rows; i++) { if (matches[i].distance <= max(sigma * min_dist, 0.02)) { good_matches.push_back(matches[i]); } } } auto conv_keypoint = [](const std::vector<KeyPoint> &kps) { key_points out; std::transform(kps.begin(), kps.end(), std::back_inserter(out), [](const KeyPoint &kp){ //return key_point{ kp.pt.x, kp.pt.y, kp.size, kp.angle }; key_point p; p.x = kp.pt.x; p.y = kp.pt.y; p.size = kp.size; p.angle = kp.angle; return p; }); return out; }; auto kps1 = conv_keypoint(im1_key_points); auto kps2 = conv_keypoint(im2_key_points); if (im1_kps) *im1_kps = kps1; if (im2_kps) *im2_kps = kps2; std::transform(good_matches.begin(), good_matches.end(), std::back_inserter(mt), [&](const DMatch &m) { //return match{ kps1[m.queryIdx] , kps2[m.trainIdx] , m.distance }; match ma; ma.pt1 = kps1[m.queryIdx]; ma.pt2 = kps2[m.trainIdx]; ma.distance = m.distance; return ma; }); return mt; }
datasource_ptr datasource_cache::create(parameters const& params) { boost::optional<std::string> type = params.get<std::string>("type"); if ( ! type) { throw config_error(std::string("Could not create datasource. Required ") + "parameter 'type' is missing"); } datasource_ptr ds; #ifdef MAPNIK_STATIC_PLUGINS // return if it's created, raise otherwise ds = create_static_datasource(params); if (ds) { return ds; } #endif #ifdef MAPNIK_THREADSAFE mapnik::scoped_lock lock(mutex_); #endif std::map<std::string,std::shared_ptr<PluginInfo> >::iterator itr=plugins_.find(*type); if (itr == plugins_.end()) { std::string s("Could not create datasource for type: '"); s += *type + "'"; if (plugin_directories_.empty()) { s += " (no datasource plugin directories have been successfully registered)"; } else { s += " (searched for datasource plugins in '" + plugin_directories() + "')"; } throw config_error(s); } if (! itr->second->valid()) { throw std::runtime_error(std::string("Cannot load library: ") + itr->second->get_error()); } // http://www.mr-edd.co.uk/blog/supressing_gcc_warnings #ifdef __GNUC__ __extension__ #endif create_ds* create_datasource = reinterpret_cast<create_ds*>(itr->second->get_symbol("create")); if (! create_datasource) { throw std::runtime_error(std::string("Cannot load symbols: ") + itr->second->get_error()); } ds = datasource_ptr(create_datasource(params), datasource_deleter()); #ifdef MAPNIK_LOG MAPNIK_LOG_DEBUG(datasource_cache) << "datasource_cache: Datasource=" << ds << " type=" << type; MAPNIK_LOG_DEBUG(datasource_cache) << "datasource_cache: Size=" << params.size(); parameters::const_iterator i = params.begin(); for (; i != params.end(); ++i) { MAPNIK_LOG_DEBUG(datasource_cache) << "datasource_cache: -- " << i->first << "=" << i->second; } #endif return ds; }