def get_array_coordinates(self, line): if line[0] == 'P': coord_geo1_row = float(line[2]) coord_geo1_col = float(line[1]) coord_geo2_row = float(line[2]) coord_geo2_col = float(line[1]) if line[0] == 'E': coord_geo1_row = float(line[2]) coord_geo1_col = self.region.east coord_geo2_row = float(line[1]) coord_geo2_col = self.region.east if line[0] == 'W': coord_geo1_row = float(line[2]) coord_geo1_col = self.region.west coord_geo2_row = float(line[1]) coord_geo2_col = self.region.west if line[0] == 'N': coord_geo1_row = self.region.north coord_geo1_col = float(line[1]) coord_geo2_row = self.region.north coord_geo2_col = float(line[2]) if line[0] == 'S': coord_geo1_row = self.region.south coord_geo1_col = float(line[1]) coord_geo2_row = self.region.south coord_geo2_col = float(line[2]) assert is_number(coord_geo1_row) assert is_number(coord_geo1_col) assert is_number(coord_geo2_row) assert is_number(coord_geo2_col) # crop coordinates to fit in region coord_geo1_row = max(coord_geo1_row, self.region.south) coord_geo1_col = max(coord_geo1_col, self.region.west) coord_geo2_row = min(coord_geo2_row, self.region.north) coord_geo2_col = min(coord_geo2_col, self.region.east) bc_len_row = coord_geo2_row - coord_geo1_row bc_len_col = coord_geo2_col - coord_geo1_col if bc_len_row < 0 or bc_len_col < 0: self.msgr.fatal( 'Incoherent coordinates \n {}'.format(line)) # transform into array coordinates coord_arr_1 = utils.coor2pixel( (coord_geo1_col, coord_geo1_row), self.region) coord_arr_2 = utils.coor2pixel( (coord_geo2_col, coord_geo2_row), self.region) return (coord_arr_1, coord_arr_2)
def get_start_end_index(bbox_list): """Convert a Bounding Box to a list of the index of column start, end, row start and end :param bbox_list: a list of BBox object to convert :type bbox_list: list of BBox object """ ss_list = [] reg = Region() for bbox in bbox_list: r_start, c_start = coor2pixel((bbox.west, bbox.north), reg) r_end, c_end = coor2pixel((bbox.east, bbox.south), reg) ss_list.append((int(r_start), int(r_end), int(c_start), int(c_end))) return ss_list
def get_value(self, point, region=None): """This method returns the pixel value of a given pair of coordinates: :param point: pair of coordinates in tuple object or class object with coords() method """ # Check for tuple if type(point) != type([]) and type(point) != type(()): point = point.coords() if not region: region = Region() row, col = utils.coor2pixel(point, region) if col < 0 or col > region.cols or row < 0 or row > region.rows: return None line = self.get_row(int(row)) return line[int(col)]
def get_value(self, point, region=None): """This method returns the pixel value of a given pair of coordinates: :param point: pair of coordinates in tuple object or class object with coords() method :param region: The region that should be used for sampling, default is the current computational region that can be set with set_region() or set_region_from_rast() """ # Check for tuple if type(point) != type([]) and type(point) != type(()): point = point.coords() # If no region was set, use the current computational region if not region: region = Region() libraster.Rast_get_window(region.byref()) row, col = utils.coor2pixel(point, region) if col < 0 or col > region.cols or row < 0 or row > region.rows: return None line = self.get_row(int(row)) return line[int(col)]
def coor2pixel(self, coor): """convert coordinates easting and northing to pixel row and column """ return gutils.coor2pixel(coor, self.region)
#!/usr/bin/env python from grass.pygrass.raster import RasterRow from grass.pygrass.vector import Vector from grass.pygrass.gis.region import Region from grass.pygrass.utils import coor2pixel from grass.pygrass.modules import Module rast = 'dmt@PERMANENT' Module('g.region', raster=rast) region = Region() dmt = RasterRow(rast) dmt.open('r') obce = Vector('obce_bod') obce.open('r') for o in obce: x, y = coor2pixel(o.coords(), region) value = dmt[int(x)][int(y)] print (u'{:40}: {:.0f}'.format(o.attrs['nazev'], value)) obce.close() dmt.close()
#!/usr/bin/env python3 from grass.pygrass.raster import RasterRow from grass.pygrass.vector import Vector from grass.pygrass.gis.region import Region from grass.pygrass.utils import coor2pixel name = 'dmt@PERMANENT' reg = Region() reg.from_rast(name) reg.set_current() dmt = RasterRow(name) dmt.open('r') obce = Vector('obce_bod') obce.open('r') for o in obce: x, y = coor2pixel(o.coords(), region) value = dmt[int(x)][int(y)] print ('{:40}: {:.0f}'.format(o.attrs['nazev'], value)) obce.close() dmt.close()