title: How to feed Point Cloud from a file into a grid_map in ROS
body: <p>I started working on a project that turns <code>3d point clouds</code> from a file into <code>grid_map</code>. After successfully having set all my project in <code>CMake</code> I was trying to prepare a small example. I am already able to publish a <code>grid_map</code> from this <a href="https://github.com/ANYbotics/grid_map/blob/master/grid_map_demos/src/simple_demo_node.cpp" rel="nofollow noreferrer">tutorial</a> as it is possible to see from the print screen below:</p>
  
  <p><img src="https://i.imgur.com/w2r8sZy.png" alt="grid_map"></p>
  
  <p>Also from <a href="http://answers.ros.org/question/259154/is-it-possible-to-convert-mesh-or-point-cloud-to-grid-map/" rel="nofollow noreferrer">this source</a> it seems to be possible to feed a <code>grid_map</code> with some <code>point clouds</code> that I have in a file on my Desktop. 
  However I am finding this process a little bit difficult mostly because I am still not confident with <code>grid_map</code> as I started working with it recently. </p>
  
  <p>Below I am putting the code I am using to try to feed <code>grid_map</code> with a <code>point cloud</code> file I have on my Desktop:</p>
  
  <pre><code>#include &lt;ros/ros.h&gt;
  #include &lt;grid_map_ros/grid_map_ros.hpp&gt;
  #include &lt;Eigen/Eigen&gt;
  #include &lt;grid_map_msgs/GridMap.h&gt;
  #include &lt;string&gt;
  #include &lt;cstring&gt;
  #include &lt;cmath&gt;
  #include &lt;iostream&gt;
  #include &lt;sensor_msgs/PointCloud.h&gt;
  #include &lt;sensor_msgs/PointCloud2.h&gt;
  #include &lt;pcl/io/pcd_io.h&gt;
  #include &lt;pcl/point_types.h&gt;
  
  using namespace grid_map;
  
  class Point_CLoud_Reader
  {
  public:
      Point_CLoud_Reader();
      void pointCloudCallback(const sensor_msgs::PointCloudConstPtr&amp; msgIn);
      void readPCloud();
      void writeToGridMap();
      pcl::PointCloud&lt;pcl::PointXYZ&gt; cloud;
  
  private:
      ros::NodeHandle _node;
      ros::Publisher pCloudPub;
      ros::Subscriber pCloudSub;
      std::string _pointTopic;
  };
  
  
  Point_CLoud_Reader::Point_CLoud_Reader()
  {
      _node.param&lt;std::string&gt;("pointcloud_topic", _pointTopic, "detections");
      ROS_INFO("%s subscribing to topic %s ", ros::this_node::getName().c_str(), _pointTopic.c_str());
      pCloudPub = _node.advertise&lt;sensor_msgs::PointCloud&gt;("/point_cloud", 100, &amp;Point_CLoud_Reader::pointCloudCallback, this);
  }
  
  void Point_CLoud_Reader::pointCloudCallback(const sensor_msgs::PointCloudConstPtr &amp;msgIn)
  {
      sensor_msgs::PointCloud msgPointCloud = *msgIn;
      //msgPointCloud.points = cloud; // &lt;-- Error Here
      pCloudPub.publish(msgPointCloud);
  }
  
  void Point_CLoud_Reader::readPCloud()
  {
      pcl::PointCloud&lt;pcl::PointXYZ&gt;::Ptr cloud(new pcl::PointCloud&lt;pcl::PointXYZ&gt;);
      if(pcl::io::loadPCDFile&lt;pcl::PointXYZ&gt; ("/home/to/Desktop/wigglesbank_20cm.pcd", *cloud) == -1) // load point cloud file
      {
          PCL_ERROR("Could not read the file /home/to/Desktop/wigglesbank_20cm.pcd \n");
          return;
      }
      std::cout&lt;&lt;"Loaded"&lt;&lt;cloud-&gt;width * cloud-&gt;height
               &lt;&lt;"data points from /home/to/Desktop/wigglesbank_20cm.pcd with the following fields: "
               &lt;&lt;std::endl;
  
      for(size_t i = 0; i &lt; cloud-&gt;points.size(); ++i)
          std::cout &lt;&lt; "    " &lt;&lt; cloud-&gt;points[i].x
                    &lt;&lt; " "    &lt;&lt; cloud-&gt;points[i].y
                    &lt;&lt; " "    &lt;&lt; cloud-&gt;points[i].z &lt;&lt; std::endl;
  }
  
  int main(int argc, char** argv)
  {
          // initialize node and publisher
      ros::init(argc, argv, "grid_map_test");
      ros::NodeHandle nh;
      ros::Publisher pub = nh.advertise&lt;grid_map_msgs::GridMap&gt;("grid_map", 1, true);
      ros::Publisher pCloudPub= nh.advertise&lt;sensor_msgs::PointCloud&gt;("point_cloud", 1, true);
      pcl::PointCloud&lt;pcl::PointXYZ&gt; cloud;
  
      // create grid map
      GridMap map({"elevation"});
      map.setFrameId("map");
      map.setGeometry(Length(1.2, 2.0), 0.03);
      ROS_INFO("Created map with size %f x %f m (%i x %i cells).",
               map.getLength().x(), map.getLength().y(),
               map.getSize()(0), map.getSize()(1));
  
      // work with grid-map in a loop
      ros::Rate rate(30.0);
      while (nh.ok()) {
          // add data to grid-map and point cloud from file just read
          ros::Time time = ros::Time::now();
          for(GridMapIterator it(map); !it.isPastEnd(); ++it)
              for(int i = 0; i &lt; cloud.points.size(); i++)
              {
                  Point_CLoud_Reader readCloud;
                  readCloud.readPCloud();   // &lt;-- Not reading point clouds
                  Position position;
                  map.getPosition(*it, position);
                  map.at("elevation", *it) = -0.04 + 0.2 * std::tan(3.0 * time.toSec() + 5.0 * position.y()) * position.x();
              }
  
          // publish a grid map and point cloud from file just read
          map.setTimestamp(time.toNSec());
          grid_map_msgs::GridMap msg;
          sensor_msgs::PointCloud cloud;
          GridMapRosConverter::toMessage(map, msg);
          //GridMapRosConverter::toPointCloud(msg,cloud); &lt;-- Error Here
          pub.publish(msg);
          pCloudPub.publish(cloud);
          ROS_INFO_THROTTLE(1.0, "Grid map (timestamp %f) published.", msg.info.header.stamp.toSec());
          // wait for next cycle
          rate.sleep();
      }
          return 0;
  }
  </code></pre>
  
  <p>Thanks for pointing in the right direction and shedding light on this issue</p>