Skip to content

Commit 532ca5a

Browse files
committed
Add vrt parsing node
1 parent 6c08932 commit 532ca5a

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

grid_map_geo/include/grid_map_geo/grid_map_geo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ class GridMapGeo {
107107
*/
108108
bool initializeFromGeotiff(const std::string& path);
109109

110+
bool initializeFromVrt(const std::string& path);
111+
110112
/**
111113
* @brief Load a color layer from a geotiff file (orthomosaic)
112114
*

grid_map_geo/src/grid_map_geo.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,89 @@ bool GridMapGeo::initializeFromGeotiff(const std::string &path) {
141141
return true;
142142
}
143143

144+
bool GridMapGeo::initializeFromVrt(const std::string &path) {
145+
GDALAllRegister();
146+
GDALDataset *dataset = (GDALDataset *)GDALOpen(path.c_str(), GA_ReadOnly);
147+
if (!dataset) {
148+
std::cout << "Failed to open" << std::endl;
149+
return false;
150+
}
151+
std::cout << std::endl << "Loading GeoTIFF file for gridmap" << std::endl;
152+
153+
double originX, originY, pixelSizeX, pixelSizeY;
154+
double geoTransform[6];
155+
if (dataset->GetGeoTransform(geoTransform) == CE_None) {
156+
originX = geoTransform[0];
157+
originY = geoTransform[3];
158+
pixelSizeX = geoTransform[1];
159+
pixelSizeY = geoTransform[5];
160+
} else {
161+
std::cout << "Failed read geotransform" << std::endl;
162+
return false;
163+
}
164+
165+
const char *pszProjection = dataset->GetProjectionRef();
166+
std::cout << std::endl << "Wkt ProjectionRef: " << pszProjection << std::endl;
167+
168+
const OGRSpatialReference *spatial_ref = dataset->GetSpatialRef();
169+
std::string name_coordinate = spatial_ref->GetAttrValue("geogcs");
170+
std::string epsg_code = spatial_ref->GetAttrValue("AUTHORITY", 1);
171+
// Get image metadata
172+
unsigned width = dataset->GetRasterXSize();
173+
unsigned height = dataset->GetRasterYSize();
174+
double resolution = pixelSizeX;
175+
std::cout << "Width: " << width << " Height: " << height << " Resolution: " << resolution << std::endl;
176+
177+
// pixelSizeY is negative because the origin of the image is the north-east corner and positive
178+
// Y pixel coordinates go towards the south
179+
const double lengthX = resolution * width;
180+
const double lengthY = resolution * height;
181+
grid_map::Length length(lengthX, lengthY);
182+
183+
double mapcenter_e = originX + pixelSizeX * width * 0.5;
184+
double mapcenter_n = originY + pixelSizeY * height * 0.5;
185+
maporigin_.espg = static_cast<ESPG>(std::stoi(epsg_code));
186+
maporigin_.position = Eigen::Vector3d(mapcenter_e, mapcenter_n, 0.0);
187+
188+
Eigen::Vector2d position{Eigen::Vector2d::Zero()};
189+
190+
grid_map_.setGeometry(length, resolution, position);
191+
/// TODO: Use TF for geocoordinates
192+
grid_map_.setFrameId(frame_id_);
193+
grid_map_.add("elevation");
194+
GDALRasterBand *elevationBand = dataset->GetRasterBand(1);
195+
196+
std::vector<float> data(width * height, 0.0f);
197+
elevationBand->RasterIO(GF_Read, 0, 0, width, height, &data[0], width, height, GDT_Float32, 0, 0);
198+
199+
grid_map::Matrix &layer_elevation = grid_map_["elevation"];
200+
for (grid_map::GridMapIterator iterator(grid_map_); !iterator.isPastEnd(); ++iterator) {
201+
const grid_map::Index gridMapIndex = *iterator;
202+
// TODO: This may be wrong if the pixelSizeY > 0
203+
int x = width - 1 - gridMapIndex(0);
204+
int y = gridMapIndex(1);
205+
206+
layer_elevation(x, y) = data[gridMapIndex(0) + width * gridMapIndex(1)];
207+
}
208+
209+
static tf2_ros::StaticTransformBroadcaster static_broadcaster;
210+
geometry_msgs::TransformStamped static_transformStamped;
211+
212+
static_transformStamped.header.stamp = ros::Time::now();
213+
static_transformStamped.header.frame_id = name_coordinate;
214+
static_transformStamped.child_frame_id = frame_id_;
215+
static_transformStamped.transform.translation.x = mapcenter_e;
216+
static_transformStamped.transform.translation.y = mapcenter_n;
217+
static_transformStamped.transform.translation.z = 0.0;
218+
static_transformStamped.transform.rotation.x = 0.0;
219+
static_transformStamped.transform.rotation.y = 0.0;
220+
static_transformStamped.transform.rotation.z = 0.0;
221+
static_transformStamped.transform.rotation.w = 1.0;
222+
static_broadcaster.sendTransform(static_transformStamped);
223+
224+
return true;
225+
}
226+
144227
bool GridMapGeo::addColorFromGeotiff(const std::string &path) {
145228
GDALAllRegister();
146229
GDALDataset *dataset = (GDALDataset *)GDALOpen(path.c_str(), GA_ReadOnly);

0 commit comments

Comments
 (0)