package scripts; import java.util.*; import java.io.*; import java.nio.file.*; import java.nio.charset.StandardCharsets; import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.lookup.StringLookup; import org.apache.commons.text.StringSubstitutor; import fr.ifremer.isisfish.entities.*; import fr.ifremer.isisfish.map.GeoTools; /** * some neccessary reusable helper functions, also some default values * * Auteur: PHAN Tuan Anh, 2022 */ public final class PTAtoolbox { /** choisir la région pour modifier */ public final static String REGION_NAME = "playground"; /** mettre le dossier ici */ public final static String DATA_FOLDER = "C:/Users/taphan/Documents/code_R_ISIS/isis-r-scripts-for-westmed2022/output/for_region"; public final static String SEPARATOR = " @ "; // separateur des champs public final static String TRAWLER_GEARS = "OTB|OTM|OTT"; // regex public final static String PORT_FRA = "fr(CMT|CST|GPV|GST|XMA|XST)"; // regex public final static String PORT_ESP = "es(BAR|GAR|LEI|PMI|RSA|SCR|SPO|TAR|VMG)"; // regex public final static String VESSEL_LENGTH = "VL(0612|1218|1824|2440)"; // regex public final static String AUTO_CMT = "this entity is automatically generated by script, no info available here"; /** read text file as list, text is a vector of values separated by line break */ public static List readListTxt(String fileName) throws IOException { return Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8); } /** chercher si la zone existe, sinon la créer */ public static Zone traiterZone(ZoneDAO myZoneDAO, String zoneName) { Zone res = myZoneDAO.findByName(zoneName); if (res != null) { System.out.println(" > zone existed: " + zoneName); } else { System.out.println(" > zone create: " + zoneName); res = myZoneDAO.create(); res.setComment(AUTO_CMT); res.setName(zoneName); res.update(); } return res; } /** lire shapefile de carré, puis affecter aux zones */ public static void affecterShp(FisheryRegion myRegion, List listCells, Zone myZone, File shpFile) { Collection cellsFromFile = GeoTools.getCellFromShapefile(myRegion, listCells, shpFile); myZone.setCell(new ArrayList<>(cellsFromFile)); //convert Collection to List myZone.update(); } /** * Interpolating environment variables in string * this code is shared online: https://stackoverflow.com/a/74611963/10805680 */ public final static StringSubstitutor expandEnvirVar = new StringSubstitutor(new EnvLookUp()); private static class EnvLookUp implements StringLookup { @Override public String lookup(String key) { String value = System.getenv(key); if (StringUtils.isBlank(value)) { throw new IllegalArgumentException("key " + key + " is not found in the env variables"); } return value; } } }