occupancy_grid.py
Go to the documentation of this file.
00001 import sys
00002 
00003 from .registry import converts_from_numpy, converts_to_numpy
00004 from nav_msgs.msg import OccupancyGrid, MapMetaData
00005 
00006 import numpy as np
00007 from numpy.lib.stride_tricks import as_strided
00008 
00009 @converts_to_numpy(OccupancyGrid)
00010 def occupancygrid_to_numpy(msg):
00011         data = np.asarray(msg.data, dtype=np.int8).reshape(msg.info.height, msg.info.width)
00012 
00013         return np.ma.array(data, mask=data==-1, fill_value=-1)
00014 
00015 
00016 @converts_from_numpy(OccupancyGrid)
00017 def numpy_to_occupancy_grid(arr, info=None):
00018         if not len(arr.shape) == 2:
00019                 raise TypeError('Array must be 2D')
00020         if not arr.dtype == np.int8:
00021                 raise TypeError('Array must be of int8s')
00022 
00023         grid = OccupancyGrid()
00024         if isinstance(arr, np.ma.MaskedArray):
00025                 # We assume that the masked value are already -1, for speed
00026                 arr = arr.data
00027         grid.data = arr.ravel()
00028         grid.info = info or MapMetaData()
00029         grid.info.height = arr.shape[0]
00030         grid.info.width = arr.shape[1]
00031 
00032         return grid


ros_numpy
Author(s): Eric Wieser
autogenerated on Tue Mar 28 2017 04:16:38