def __init__(self): """Create a Playground with all the relevant info by GRASS""" self.layers = dict() self.grassmapnames = dict() self.region = grass.region() if self.region["ewres"] != self.region["nsres"]: raise error.DataError(Grassland.ME, "Only square raster cells make sense.")
def setmaps(cost, output): """ Set the user maps in place """ if cost: if -1 == cost.find("@"): cost = cost + "@" + grass.gisenv()["MAPSET"] # set cost/penalty layer world.playground.setgrasslayer("COST", cost, True) else: raise error.DataError("r.agent.rand:", "The cost map is mandatory.") if output: if -1 == output.find("@"): output = output + "@" + grass.gisenv()["MAPSET"] world.playground.setgrasslayer("RESULT", output, True) else: raise error.DataError("r.agent.rand:", "The output map is mandatory.")
def move(self, agent, position=None): """ Set agent to a new position @param agent to be moved @param list coordinates of the new position or none for a random one """ position = self.findposition(position) if not position: raise error.DataError("r.agent::libagent.world.World.move()", "invalid position") agent.setposition(position)
def setmaps(site, cost, wastecosts, inphero, outphero, wastephero): """ Set the user maps in place """ if site: if -1 == site.find('@'): site = site + '@' + grass.gisenv()['MAPSET'] # set sitemap and site list world.sites = world.playground.parsevectorlayer( anthill.Anthill.SITE, site, -1, True) if not world.sites: raise error.DataError("r.agent.aco:", "There were no sites in" + site) else: raise error.DataError("r.agent.aco:", "The site map is mandatory.") if cost: if -1 == cost.find('@'): cost = cost + '@' + grass.gisenv()['MAPSET'] # set cost/penalty layer world.playground.setgrasslayer(anthill.Anthill.COST, cost, True) world.overwritepenalty = wastecosts else: raise error.DataError("r.agent.aco:", "The cost map is mandatory.") if outphero: if -1 == outphero.find('@'): outphero = outphero + '@' + grass.gisenv()['MAPSET'] else: raise error.DataError("r.agent.aco:", "The output map is mandatory.") if inphero == outphero: if not wastephero: raise error.DataError("aco:", "May not overwrite the output map.") if inphero: if -1 == inphero.find('@'): inphero = inphero + '@' + grass.gisenv()['MAPSET'] world.playground.setgrasslayer(anthill.Anthill.RESULT, inphero, True) world.playground.grassmapnames[anthill.Anthill.RESULT] = outphero #TODO hopefully not really needed - workaround for broken(?) garray world.playground.createlayer("copy", outphero, True) world.playground.grassmapnames["copy"] = outphero world.overwritepheormone = wastephero
def writelayer(self, layername, grassmapname=False, force=False): """ Write out a given layer to a GRASS map file @param string name of the layer to be exported @param string optional name of the GRASS map file to be written @param boolean optional, whether an existing file may be overwritten """ if not grassmapname: if layername in self.grassmapnames: grassmapname = self.grassmapnames[layername] else: raise error.DataError(Grassland.ME, "Grass Map name is empty.") if layername in self.layers: if grassmapname in grass.list_strings("rast"): if force: force = "force" else: raise error.DataError(Grassland.ME, "Grass map already exists.") self.layers[layername].write(grassmapname, overwrite=force) else: raise error.DataError(Grassland.ME, "Layer is not in list.")
def setgrasslayer(self, layername, grassmapname, force=False): """ Put an existing map from GRASS to the layer collection @param string name of the layer @param string name of an existing GRASS map layer @param boolean optional, whether to overwrite values if key exists """ # fill the new grass array with the contents from the map (must exist) if grassmapname in grass.list_strings("rast"): layer = garray.array(grassmapname) self.grassmapnames[layername] = grassmapname self.setlayer(layername, layer, force) else: raise error.DataError(Grassland.ME, "Grass Map was missing: " + grassmapname)
def bear(self, timetolive, position=None, agenttype=None): """ Set a new agent into the world @param int number of cycles the agent has to live @param list coordinates to put the agent on, none for a random position @param agenttype the type of agent to be spawned @return agent the newly created agent """ position = self.findposition(position) if not position: raise error.DataError("r.agent::libagent.world.World.bear()", "invalid position") agent = self.agenttype(timetolive, self, position) self.agents.append(agent) return agent
def raise_data(context, message): raise error.DataError(context, message)