gama-platform / gama

Main repository for developing the 2024+ versions of GAMA
https://gama-platform.org
GNU General Public License v3.0
12 stars 5 forks source link

Creating grids (or species) from files specified in json file #137

Open jpmullercir opened 3 months ago

jpmullercir commented 3 months ago

Describe the bug I would like the model to be entirely specified by a json file (including the files used to create grids and species.

To Reproduce A test project is provided with some attempt to make it work correctly...but it doesn't

Expected behavior A clear and concise description of what you expected to happen:

1) load the parameter file 2) create the grids and species depending on some files provided in the parameter file 3) run it

Screenshots gama project included Test.zip

Desktop (please complete the following information):

applicationXMI=org.eclipse.ui.workbench/LegacyIDE.e4xmi
ds.delayed.keepInstances=true
ds.delayed.keepInstances.default=true
eclipse.application=msi.gama.application.GamaApplication
eclipse.buildId=${build.id}
eclipse.commands=-os
macosx
-ws
cocoa
-arch
x86_64
-showsplash
-launcher
/Applications/Developer/Gama/Gama.app/Contents/MacOS/Gama
-name
Gama
--launcher.library
/Applications/Developer/Gama/Gama.app/Contents/MacOS//../Eclipse/plugins/org.eclipse.equinox.launcher.cocoa.macosx.x86_64_1.2.700.v20221108-1024/eclipse_11801.so
-startup
/Applications/Developer/Gama/Gama.app/Contents/MacOS//../Eclipse/plugins/org.eclipse.equinox.launcher_1.6.500.v20230717-2134.jar
--launcher.overrideVmargs
-data
@noDefault
-vm
/Applications/Developer/Gama/Gama.app/Contents/jdk/Contents/Home/bin/../lib/server/libjvm.dylib
eclipse.home.location=file:/Applications/Developer/Gama/Gama.app/Contents/Eclipse/
eclipse.launcher=/Applications/Developer/Gama/Gama.app/Contents/MacOS/Gama
eclipse.launcher.name=Gama
eclipse.log.level=ERROR
eclipse.p2.data.area=@config.dir/../p2
eclipse.p2.profile=DefaultProfile
eclipse.product=msi.gama.application.product
eclipse.startTime=1711102205932
eclipse.stateSaveDelayInterval=30000
eclipse.vm=/Applications/Developer/Gama/Gama.app/Contents/jdk/Contents/Home/bin/../lib/server/libjvm.dylib
eclipse.vmargs=-server
-XX:+UseG1GC
-XX:+UseStringDeduplication
-Dorg.eclipse.swt.graphics.Resource.reportNonDisposed=false
-Xms4096m
-Xmx4096m
-Xss128m
-Xmn1024m
-XX:+UseAdaptiveSizePolicy
-XX:+OptimizeStringConcat
-Dosgi.locking=none
-Dosgi.checkConfiguration=false
-Declipse.log.level=ERROR
-Dorg.eclipse.ecf.provider.filetransfer.retrieve.retryAttempts=10
-Dorg.eclipse.ecf.provider.filetransfer.retrieve.closeTimeout=6000
-Dorg.eclipse.ecf.provider.filetransfer.retrieve.readTimeout=6000
-Denable_logging=true
-Denable_debug=false
-Duse_global_preference_store=true
-Dswt.autoScale=exact
-Dread_only=false
-Duse_old_tabs=true
-Duse_legacy_drawers=false
-Duse_delayed_resize=false
--add-exports=java.base/java.lang=ALL-UNNAMED
--add-exports=java.desktop/sun.awt=ALL-UNNAMED
--add-exports=java.desktop/sun.java2d=ALL-UNNAMED
--add-exports=java.desktop/sun.awt.image=ALL-UNNAMED
--add-exports=java.base/java.math=ALL-UNNAMED
--add-exports=java.base/sun.nio.ch=ALL-UNNAMED
--add-opens=java.base/java.lang=ALL-UNNAMED
--add-opens=java.base/jdk.internal.loader=ALL-UNNAMED
--add-opens=java.base/java.math=ALL-UNNAMED
--add-opens=java.base/java.util=ALL-UNNAMED
--add-opens=java.base/java.util.concurrent=ALL-UNNAMED
--add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED
--add-opens=java.base/java.net=ALL-UNNAMED
--add-opens=java.base/java.security=ALL-UNNAMED
--add-opens=java.desktop/java.awt=ALL-UNNAMED
--add-opens=java.base/java.io=ALL-UNNAMED
--add-opens=java.base/java.time=ALL-UNNAMED
--add-opens=java.base/java.util.concurrent.locks=ALL-UNNAMED
--add-opens=java.base/java.text=ALL-UNNAMED
--add-opens=java.base/java.lang.ref=ALL-UNNAMED
--add-opens=java.sql/java.sql=ALL-UNNAMED
-XstartOnFirstThread
-Dorg.eclipse.swt.internal.carbon.smallFonts
-Djava.class.path=/Applications/Developer/Gama/Gama.app/Contents/MacOS//../Eclipse/plugins/org.eclipse.equinox.launcher_1.6.500.v20230717-2134.jar
enable_debug=false
enable_logging=true
equinox.init.uuid=true
file.encoding=UTF-8
file.separator=/
ftp.nonProxyHosts=local|*.local|169.254/16|*.169.254/16
gosh.args=--nointeractive
java.class.path=/Applications/Developer/Gama/Gama.app/Contents/MacOS//../Eclipse/plugins/org.eclipse.equinox.launcher_1.6.500.v20230717-2134.jar
java.class.version=61.0
java.home=/Applications/Developer/Gama/Gama.app/Contents/jdk/Contents/Home
java.io.tmpdir=/var/folders/s5/4fr_2cgx7t35nkz01g62nw4h0000gn/T/
java.library.path=/Users/jpmuller/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
java.runtime.name=OpenJDK Runtime Environment
java.runtime.version=17.0.8.1+1
java.specification.name=Java Platform API Specification
java.specification.vendor=Oracle Corporation
java.specification.version=17
java.vendor=Eclipse Adoptium
java.vendor.url=https://adoptium.net/
java.vendor.url.bug=https://github.com/adoptium/adoptium-support/issues
java.vendor.version=Temurin-17.0.8.1+1
java.version=17.0.8.1
java.version.date=2023-08-24
java.vm.compressedOopsMode=Zero based
java.vm.info=mixed mode, sharing
java.vm.name=OpenJDK 64-Bit Server VM
java.vm.specification.name=Java Virtual Machine Specification
java.vm.specification.vendor=Oracle Corporation
java.vm.specification.version=17
java.vm.vendor=Eclipse Adoptium
java.vm.version=17.0.8.1+1
jdk.debug=release
jnlp.jogamp.tmp.cache.root=jln4910247250656430717
line.separator=native.encoding=UTF-8
lesquoyb commented 3 months ago

Hello, I think what you want to do is already possible, but you have to take into account the order of creation of elements in gama. grids are created before the init block of the global is executed, thus you have to get your file paths directly at declaration. In addition you will need to safeguard the use of maxSlope in the aspects because it's going to be set to the correct value after. Which will give us something like this for Test v1:

/**
* Name: Test
* Based on the internal empty template. 
* Author: jpmuller
* Tags: 
*/

model Test

global {
  // GLOBAL VARIABLES

  // simulation control
  string parameter_name <- "../includes/test/parameters.json";
  file parameterFile    <- json_file(parameter_name); 
  map<string,unknown> parameters <- parameterFile.contents;
  file bounds <- file(parameters["site"]);
  file slopes <- file(parameters["pente"]);
  geometry bounds_shape <- envelope(bounds); // the bounds of the whole system
  int patch_length <- 100;
  float slopeMax <- 0.0;
  bool first <- true;

    reflex step {
        if first{
          ask Patch overlapping bounds_shape {
                inSite <- true;
                // slope
                list<Slope> slopelist <- Slope overlapping self;
                float localSlopeMax <- 0.0;
                if !empty(slopelist) {
                    slope         <- slopelist mean_of (each.grid_value);
                    localSlopeMax <- slopelist max_of  (each.grid_value);
                }
                slopeMax <- max(slopeMax,localSlopeMax);
          }
          first <- false;
          write sample(slopeMax);
        }
        write "step";
    }
}

// the patch species
grid Patch width: shape.width / patch_length height: shape.height / patch_length neighbors: 8 {
    bool inSite <- false;
    float slope;

    aspect default {
        if inSite {
            color <- #black;
        } else {
            color <- #white;
        }
    }
    aspect slope {
        if inSite {
            float depth <- slopeMax > 0 ? grid_value / slopeMax :0;
            draw shape color: rgb(0,0,int(255 * depth));
        }
    }
}

grid Slope file: slopes {
    init {
        color <- #white;
    }

    // DISPLAY
    aspect default {

        float depth <- slopeMax > 0 ? grid_value / slopeMax :0;
        draw shape color: rgb(255 * depth,255 * depth,255 * depth);
    }

}

experiment Test {
    output {
                // the patch grid for system viewing
        display Test type: java2D {
            species Slope           aspect: default refresh: false;
            species Patch           aspect: default refresh: true;
        }

    }
}

note that there seems to be a projection problem in your model as there's no overlap between your file GrandCoyer.shp and Pente_GD_Coyer.tif but that's probably related to the previous issue you posted