data.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """
  2. Manipulate data in mapsets in GRASS GIS Spatial Database
  3. (C) 2020 by the GRASS Development Team
  4. This program is free software under the GNU General Public
  5. License (>=v2). Read the file COPYING that comes with GRASS
  6. for details.
  7. .. sectionauthor:: Vaclav Petras <wenzeslaus gmail com>
  8. """
  9. import grass.script as gs
  10. def map_exists(name, element, mapset=None, env=None):
  11. """Check is map is present in the mapset given in the environment
  12. :param name: Name of the map
  13. :param element: Data type ('raster', 'raster_3d', and 'vector')
  14. :param env: Environment created by function grass.script.create_environment
  15. :param mapset: Mapset name, "." for current mapset only,
  16. None for all mapsets in the search path
  17. """
  18. # change type to element used by find file
  19. if element == "raster":
  20. element = "cell"
  21. elif element == "raster_3d":
  22. element = "grid3"
  23. # g.findfile returns non-zero when file was not found
  24. # se we ignore return code and just focus on stdout
  25. process = gs.start_command(
  26. "g.findfile",
  27. flags="n",
  28. element=element,
  29. file=name,
  30. mapset=mapset,
  31. stdout=gs.PIPE,
  32. stderr=gs.PIPE,
  33. env=env,
  34. )
  35. output, unused_errors = process.communicate()
  36. info = gs.parse_key_val(output, sep="=")
  37. # file is the key questioned in grass.script.core find_file()
  38. # return code should be equivalent to checking the output
  39. if info["file"]:
  40. return True
  41. return False