|
| 1 | +package fr.ign.validator.cnig.command; |
| 2 | + |
| 3 | +import java.io.BufferedWriter; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileWriter; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import org.apache.commons.cli.CommandLine; |
| 11 | +import org.apache.commons.cli.Option; |
| 12 | +import org.apache.commons.cli.Options; |
| 13 | +import org.apache.commons.cli.ParseException; |
| 14 | +import org.apache.commons.io.FilenameUtils; |
| 15 | +import org.apache.logging.log4j.LogManager; |
| 16 | +import org.apache.logging.log4j.Logger; |
| 17 | +import org.apache.logging.log4j.Marker; |
| 18 | +import org.apache.logging.log4j.MarkerManager; |
| 19 | + |
| 20 | +import fr.ign.validator.cnig.process.DocumentGeometryProcess; |
| 21 | +import fr.ign.validator.command.AbstractCommand; |
| 22 | + |
| 23 | +/** |
| 24 | + * Generates Document Geomtry from csv files |
| 25 | + * |
| 26 | + * @author DDarras |
| 27 | + * |
| 28 | + */ |
| 29 | +public class DocumentGeometryCommand extends AbstractCommand { |
| 30 | + |
| 31 | + public static final String NAME = "document_geometry"; |
| 32 | + |
| 33 | + public static final Logger log = LogManager.getRootLogger(); |
| 34 | + public static final Marker MARKER = MarkerManager.getMarker("DocumentGeometryCommand"); |
| 35 | + public static final String COMMA_DELIMITER = ","; |
| 36 | + |
| 37 | + /** |
| 38 | + * Input files |
| 39 | + */ |
| 40 | + private List<File> inputFiles = new ArrayList<File>(); |
| 41 | + |
| 42 | + /** |
| 43 | + * Geometry columns names |
| 44 | + */ |
| 45 | + private List<String> geometryColumnNames = new ArrayList<String>(); |
| 46 | + |
| 47 | + /** |
| 48 | + * Output file |
| 49 | + */ |
| 50 | + private File outputFile; |
| 51 | + |
| 52 | + @Override |
| 53 | + public String getName() { |
| 54 | + return NAME; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public String getDescription() { |
| 59 | + return "Generates Document Geometry from csv files"; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Life cycle of command |
| 64 | + */ |
| 65 | + @Override |
| 66 | + public void execute() throws Exception { |
| 67 | + File targetFile = new File(outputFile.getAbsolutePath()); |
| 68 | + log.info(MARKER, "Processing ..."); |
| 69 | + try { |
| 70 | + process(inputFiles, targetFile); |
| 71 | + } catch (Exception e) { |
| 72 | + log.error(MARKER, "Failure during processing"); |
| 73 | + e.printStackTrace(System.err); |
| 74 | + } |
| 75 | + log.info(MARKER, "complete"); |
| 76 | + // dans gpu-site, lancement commande + lecture WKT |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Processing data |
| 81 | + * |
| 82 | + * @param inputFiles |
| 83 | + * @param targetFile |
| 84 | + * @throws Exception |
| 85 | + */ |
| 86 | + private void process(List<File> inputFiles, File targetFile) throws Exception { |
| 87 | + // Core processing |
| 88 | + DocumentGeometryProcess documentGeometryProcess = new DocumentGeometryProcess(inputFiles, geometryColumnNames); |
| 89 | + log.info(MARKER, "Detecting Geometries ..."); |
| 90 | + documentGeometryProcess.detectGeometries(); |
| 91 | + |
| 92 | + log.info(MARKER, "Union from detected Geometries ..."); |
| 93 | + String union = documentGeometryProcess.union(); |
| 94 | + |
| 95 | + // Writing to file |
| 96 | + log.info(MARKER, "Writing Union Geometry to file ..."); |
| 97 | + BufferedWriter bufferedWriter = getWriter(targetFile); |
| 98 | + bufferedWriter.write(union); |
| 99 | + bufferedWriter.close(); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * File Writer helper |
| 104 | + * |
| 105 | + * @param targetFile |
| 106 | + * @return |
| 107 | + * @throws IOException |
| 108 | + */ |
| 109 | + private BufferedWriter getWriter(File targetFile) throws IOException { |
| 110 | + if (targetFile.exists()) { |
| 111 | + targetFile.delete(); |
| 112 | + } |
| 113 | + return new BufferedWriter(new FileWriter(targetFile)); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * auxilliary options for commnad |
| 118 | + */ |
| 119 | + @Override |
| 120 | + protected void buildCustomOptions(Options options) { |
| 121 | + // input |
| 122 | + { |
| 123 | + Option option = new Option( |
| 124 | + "i", "input", true, |
| 125 | + "Input files (csv). Different inputs should be separated by ','" |
| 126 | + ); |
| 127 | + option.setRequired(true); |
| 128 | + option.setType(String.class); |
| 129 | + options.addOption(option); |
| 130 | + } |
| 131 | + { |
| 132 | + Option option = new Option( |
| 133 | + "g", "geometries", true, |
| 134 | + "Input names of geometry columns names. Different names should be separated by ','. " + |
| 135 | + "'geom' and 'geometry' are included by default" |
| 136 | + ); |
| 137 | + option.setRequired(false); |
| 138 | + option.setType(String.class); |
| 139 | + options.addOption(option); |
| 140 | + } |
| 141 | + |
| 142 | + // output |
| 143 | + { |
| 144 | + Option option = new Option("o", "output", true, "Output file"); |
| 145 | + option.setRequired(true); |
| 146 | + option.setType(File.class); |
| 147 | + options.addOption(option); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Parses command input from command line |
| 153 | + */ |
| 154 | + @Override |
| 155 | + protected void parseCustomOptions(CommandLine commandLine) throws ParseException { |
| 156 | + String inputString = (String) commandLine.getParsedOptionValue("input"); |
| 157 | + String geometryColumnNameString = (String) commandLine.getParsedOptionValue("geometries"); |
| 158 | + this.outputFile = (File) commandLine.getParsedOptionValue("output"); |
| 159 | + |
| 160 | + this.inputFiles = parseFileOption(inputString); |
| 161 | + this.geometryColumnNames = parseGeometryOption(geometryColumnNameString, commandLine.hasOption("geometries")); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Asserts that Files input is conform |
| 166 | + * |
| 167 | + * @param inputString |
| 168 | + * @return |
| 169 | + * @throws ParseException |
| 170 | + */ |
| 171 | + public List<File> parseFileOption(String inputString) throws ParseException { |
| 172 | + List<File> inputFiles = new ArrayList<File>(); |
| 173 | + if (inputString.isEmpty()) { |
| 174 | + throw new ParseException("Input is empty"); |
| 175 | + } |
| 176 | + String[] potentialStrings = inputString.split(", *"); |
| 177 | + for (String potentialString : potentialStrings) { |
| 178 | + File potentialFile = new File(potentialString); |
| 179 | + if (!potentialFile.isFile()) { |
| 180 | + throw new ParseException("'" + potentialString + "' cannot be found"); |
| 181 | + } |
| 182 | + if (!FilenameUtils.getExtension(potentialString).equals("csv")) { |
| 183 | + throw new ParseException("'" + potentialString + "' must be a CSV file"); |
| 184 | + } |
| 185 | + inputFiles.add(potentialFile); |
| 186 | + } |
| 187 | + return inputFiles; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Asserts that geometry input is conform |
| 192 | + * |
| 193 | + * @param geometryColumnNameString |
| 194 | + * @param hasOption |
| 195 | + * @return |
| 196 | + */ |
| 197 | + public List<String> parseGeometryOption(String geometryColumnNameString, boolean hasOption) { |
| 198 | + List<String> geometryColumnNames = new ArrayList<String>(); |
| 199 | + geometryColumnNames.add("geom"); |
| 200 | + geometryColumnNames.add("geometry"); |
| 201 | + if (hasOption) { |
| 202 | + String[] names = geometryColumnNameString.split(", *"); |
| 203 | + for (String geometryName : names) { |
| 204 | + geometryColumnNames.add(geometryName); |
| 205 | + } |
| 206 | + } |
| 207 | + return geometryColumnNames; |
| 208 | + } |
| 209 | + |
| 210 | +} |
0 commit comments