Skip to main content
Version: 🚧 Alpha 🚧

Defining export files

The Save Statement​

The save statement writes data to a file. The available formats depend on the data type and installed extensions; common examples include text, csv, shp, json, kml, asc, geotiff, and image. The statement is used in model behavior such as an init block, a reflex, an action, or a user command.

Facets​

  • attributes, optional, expects any type in [map, list] - Allows to specify the attributes of a shape file or GeoJson file where agents are saved. Can be expressed as a list of string or as a literal map. When expressed as a list, each value should represent the name of an attribute of the shape or agent. The keys of the map are the names of the attributes that will be present in the file, the values are whatever expressions needed to define their value.
  • crs, optional, expects any type - the name of the projection, e.g. crs:"EPSG:4326" or its EPSG id, e.g. crs:4326. Here a list of the CRS codes (and EPSG id)
  • data, optional, expects any type - the data that will be saved to the file
  • header, optional, expects bool - an expression that evaluates to a boolean, specifying whether the save will write a header if the file does not exist
  • rewrite, optional, expects bool - a boolean expression specifying whether to erase the file if it exists or append data at the end of it. Only applicable to "text" or "csv" files. Default is true
  • to, optional, expects string - an expression that evaluates to a path or file. Omit it when the data expression already provides the file to save
  • format, optional, a string representing the format of the output file (e.g. shp, asc, geotiff, png, text, csv). If the file extension is non ambiguous in facet 'to:', this format does not need to be specified. However, in many cases, it can be useful to do it (for instance, when saving a string to a .pgw file, it is always better to clearly indicate that the expected format is 'text').
  • separator, optional, expects string - the single character between CSV columns. If omitted, GAMA uses the CSV separator preference.
  • encoding, optional, expects string - the character encoding for CSV output and dataframe JSON output.
  • no_data, optional, expects a number - the no-data marker for GeoTIFF raster output.
  • async, optional, expects bool - queues image encoding and writing in the background for GamaImage values; evaluating the image remains synchronous.

Usages​

  • Its simple syntax is:
save data to: output_file format: a_type_file;
  • To save data in a text file:
save (string(cycle) + "->" + name + ":" + location) to: "save_data.txt" format: "text";
  • To save the values of some attributes of the current agent in csv file:
save [name, location, host] to: "save_data.csv" format: "csv";
  • To save the geometries of all the agents of a species into a shapefile or a geojson (with optional attributes):
save species_of(self) to: "save_shapefile.shp" format: "shp" attributes: ["nameAgent"::name, "locationAgent"::location] crs: "EPSG:4326";
save species_of(self) to: "save_shapefile.geojson" format: "json" attributes: ["nameAgent"::name, "locationAgent"::location] ;
  • To save a grid into a geotiff or a asc file (the value considered will be the "grid_value" attribute of the cell):
save cell to:"../results/grid.tif" format:"geotiff";
save cell to:"../results/grid.asc" format:"asc";
  • To save a grid into an image file:
save cell to:"../results/grid.png" format:"image";

Saving results across experiments​

The save statement belongs in model behavior, not in an experiment definition. To collect values from multiple simulations, use the output options supported by the experiment type; see batch experiments.

Autosave​

Image files can be exported also through the autosave facet of the display, as explained in this previous part.