def testGetFQPatchIDForCoordinates(self): fqPatchIDs = [ (rhessystypes.FQPatchID(patchID=self.inPatchID, \ zoneID=self.inZoneID, hillID=self.inHillID)) ] coords = self.grassdatalookup.getCoordinatesForFQPatchIDs(fqPatchIDs, self.patchMap, self.zoneMap, self.hillslopeMap) self.assertTrue( len(coords) == 1 ) keys = coords.keys() coordPair = coords[keys[0]][0] self.assertTrue( abs(coordPair.easting - self.easting) < ZERO ) self.assertTrue( abs(coordPair.northing - self.northing) < ZERO ) coordinate = rhessystypes.getCoordinatePair(coordPair.easting, coordPair.northing) (patchID, zoneID, hillID) = \ self.grassdatalookup.getFQPatchIDForCoordinates(coordinate, \ self.patchMap, self.zoneMap, self.hillslopeMap) self.assertTrue( self.inPatchID == patchID ) self.assertTrue( self.inZoneID == zoneID ) self.assertTrue( self.inHillID == hillID )
def _getCentroidCoordinatesForPatches(self, coordDict): """ @brief return a list of patch centroid coordinates for each list of patch sub-cell coordinates stored in a dictionary. @param coordDict Dictionary mapping fully qualified patch ID to a list of patch sub-cell coordindates @return List of coordinates representing the centroid of each cell """ coords = list() keys = coordDict.keys() for key in keys: coordList = coordDict[key] numCoords = len(coordList) easting = northing = 0.0 if numCoords > 0: for c in coordList: easting += c.easting northing += c.northing easting = easting / numCoords northing = northing / numCoords coords.append(rhessystypes.getCoordinatePair(easting, northing)) return coords
def getCoordinatesForFQPatchIDs(self, fqPatchIDs, patchMap, zoneMap, hillslopeMap): """ @brief Get the geographic coordinates for a list of patches identified by their fully qualified patch ID. The fully qualified patch ID is the combination of the patchID, zoneID, and hillslopeID. @param List of rhessystypes.FQPatchID objects representing a fully qualified patch ID @param grassConfig GRASSConfig specifying the GRASS mapset that contains the patch, zone, and hillslope maps @param patchMap String representing the name of the patch map @param zoneMap String representing the name of the zone map @param hillslopeMap String representing the name of the hillslope map @return Dict mapping rhessystypes.FQPatchID to the list of rhessysweb.types.CoordinatePair objects representing the raster pixels that make up each patch in the input list """ coords = {} # Set up GRASS environment self.grass_lowlevel.G_gisinit('') # Get the window so we can conver row,col to easting,northing window = self.grass_lowlevel.Cell_head() self.grass_lowlevel.G_get_window(byref(window)) numRows = self.grass_lowlevel.G_window_rows() numCols = self.grass_lowlevel.G_window_cols() # Open patch map patch = self.grass_lowlevel.G_find_cell2(patchMap, '') patch = c_char_p(patch).value patchType = self.grass_lowlevel.G_raster_map_type(patchMap, patch) if patchType == 0: ptype = POINTER(c_int) elif patchType == 1: ptype = POINTER(c_float) elif patchType == 2: ptype = POINTER(c_double) patchFd = self.grass_lowlevel.G_open_cell_old(patchMap, patch) patchRast = self.grass_lowlevel.G_allocate_raster_buf(patchType) patchRast = cast(c_void_p(patchRast), ptype) # Open zone map zone = self.grass_lowlevel.G_find_cell2(zoneMap, '') zone = c_char_p(zone).value zoneType = self.grass_lowlevel.G_raster_map_type(zoneMap, zone) if zoneType == 0: ptype = POINTER(c_int) elif zoneType == 1: ptype = POINTER(c_float) elif zoneType == 2: ptype = POINTER(c_double) zoneFd = self.grass_lowlevel.G_open_cell_old(zoneMap, zone) zoneRast = self.grass_lowlevel.G_allocate_raster_buf(zoneType) zoneRast = cast(c_void_p(zoneRast), ptype) # Open hill map hill = self.grass_lowlevel.G_find_cell2(hillslopeMap, '') hill = c_char_p(hill).value hillType = self.grass_lowlevel.G_raster_map_type(hillslopeMap, hill) if hillType == 0: ptype = POINTER(c_int) elif hillType == 1: ptype = POINTER(c_float) elif hillType == 2: ptype = POINTER(c_double) hillFd = self.grass_lowlevel.G_open_cell_old(hillslopeMap, hill) hillRast = self.grass_lowlevel.G_allocate_raster_buf(hillType) hillRast = cast(c_void_p(hillRast), ptype) # print( "getCoordinatesForFQPatchIDs: patchIDs: %s" % (fqPatchIDs,) ) for row in range(numRows): # Get current row for each dataset self.grass_lowlevel.G_get_raster_row(patchFd, patchRast, row, patchType) self.grass_lowlevel.G_get_raster_row(zoneFd, zoneRast, row, zoneType) self.grass_lowlevel.G_get_raster_row(hillFd, hillRast, row, hillType) for col in range(numCols): for fqPatchID in fqPatchIDs: if patchRast[col] == fqPatchID.patchID and \ zoneRast[col] == fqPatchID.zoneID and \ hillRast[col] == fqPatchID.hillID: # Match found, get its coordinates easting = self.grass_lowlevel.G_col_to_easting(col + 0.5, byref(window)) northing = self.grass_lowlevel.G_row_to_northing(row + 0.5, byref(window)) try: coordList = coords[fqPatchID] except KeyError: coordList = [] coords[fqPatchID] = coordList coordList.append(rhessystypes.getCoordinatePair(easting, northing)) # print( "row: %d, col: %d; patch: %d, %d, %d; coordList: %s" % \ # (row, col, fqPatchID.patchID, fqPatchID.zoneID, fqPatchID.hillID, coordList) ) # Clean up self.grass_lowlevel.G_close_cell(patchFd) self.grass_lowlevel.G_free(patchRast) self.grass_lowlevel.G_close_cell(zoneFd) self.grass_lowlevel.G_free(zoneRast) self.grass_lowlevel.G_close_cell(hillFd) self.grass_lowlevel.G_free(hillRast) # os.environ['GIS_LOCK'] = '' # os.unlink(os.environ['GISRC']) returnCoords = OrderedDict() for fqPatchID in fqPatchIDs: returnCoords[fqPatchID] = coords[fqPatchID] return returnCoords
(args.grassdbase,) ) grassdbase = os.path.abspath(args.grassdbase) grassConfig = GRASSConfig(gisbase=gisbase, dbase=grassdbase, location=args.location, mapset=args.mapset) t_srs = getSpatialReferenceForGRASSDataset(grassConfig) s_srs = osr.SpatialReference() s_srs.ImportFromEPSG(4326) crx = osr.CoordinateTransformation(s_srs, t_srs) sys.stdout.write("lat%slon%seasting%snorthing%spatchID%szoneID%shillID\n" % (SEP, SEP, SEP, SEP, SEP, SEP) ) f = open(args.coordinates, 'r') f.next() # skip first line for line in f: line = line.strip() values = line.split(',') lat = float(values[0]) lon = float(values[1]) # Transform coordinates from WGS84 to the coordinate system of the # GRASS mapset (x, y, z) = crx.TransformPoint( lon, lat ) # Lookup patchID for transformed coordinates coord = rhessystypes.getCoordinatePair(x, y) id = getFQPatchIDForCoordinates(coord, grassConfig, args.patchmap, args.zonemap, args.hillmap) sys.stdout.write("%f%s%f%s%f%s%f%s%d%s%d%s%d\n" % (lat, SEP, lon, SEP, coord.easting, SEP, coord.northing, SEP, id.patchID, SEP, id.zoneID, SEP, id.hillID) )