Commit 74a41e8f authored by Matija Obreza's avatar Matija Obreza
Browse files

CSV reader and writer

parent b6ea5d7f
package org.genesys.geotools;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
public class LandOrSeaCLI {
// 2000 meters
private static final int ALLOWED_DISTANCE_FROM_COAST = 2000;
public static void main(String arg[]) throws Exception {
LandOrSeaServiceImpl landOrSeaService = new LandOrSeaServiceImpl();
landOrSeaService.afterPropertiesSet();
if (arg.length == 0) {
LandOrSeaServiceImpl landOrSeaService = new LandOrSeaServiceImpl();
landOrSeaService.afterPropertiesSet();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Pattern pattern = Pattern.compile("^(\\-?\\d*\\.?\\d*)[ \\t,]+(\\-?\\d*\\.?\\d*)$");
Pattern pattern = Pattern.compile("^(\\-?\\d*\\.?\\d*)[ \\t,]+(\\-?\\d*\\.?\\d*)$");
System.err.println("Expects input rows in format: Latitude\tLongitude");
System.err.println("Enter 'q' to quit.");
System.out.println("Latitude\tLongitude\tResult");
String input = null;
do {
input = br.readLine();
System.err.println("Expects input rows in format: Latitude\tLongitude");
System.err.println("Enter 'q' to quit.");
System.out.println("Latitude\tLongitude\tResult");
String input = null;
do {
input = br.readLine();
if (input == null || "q".equals(input))
break;
if (input ==null || "q".equals(input))
break;
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
float latitude = Float.parseFloat(matcher.group(1).replace(",", "."));
float longitude = Float.parseFloat(matcher.group(2).replace(",", "."));
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
float latitude = Float.parseFloat(matcher.group(1).replace(",", "."));
float longitude = Float.parseFloat(matcher.group(2).replace(",", "."));
if (landOrSeaService.isOnLand(longitude, latitude, ALLOWED_DISTANCE_FROM_COAST)) {
System.out.println(latitude + ", " + longitude + ", Land");
} else {
System.out.println(latitude + ", " + longitude + ", Water");
}
if (landOrSeaService.isOnLand(longitude, latitude, 200)) {
System.out.println(latitude + ", " + longitude + ", Land");
} else {
System.out.println(latitude + ", " + longitude + ", Water");
System.err.println("Invalid format: " + input);
}
} while (input != null && !"q".equals(input));
} else {
doCSV(arg);
}
}
private static void doCSV(String[] arg) throws IOException {
int columnLatitude = 4;
int columnLongitude = 3;
} else {
System.err.println("Invalid format: " + input);
CSVWriter writer = new CSVWriter(new OutputStreamWriter(System.out), ',', '"', '\\', "\n");
CSVReader reader = new CSVReader(new InputStreamReader(System.in), ',', '"', '\\', 0, false);
LandOrSeaServiceImpl landOrSeaService = new LandOrSeaServiceImpl();
landOrSeaService.afterPropertiesSet();
String[] nextLine;
int lineCount = 0;
while ((nextLine = reader.readNext()) != null) {
lineCount++;
if (lineCount % 10000 == 0) {
System.err.println("FYI, " + lineCount + " entries have been processed.");
}
String[] writeLine = Arrays.copyOf(nextLine, nextLine.length + 1);
try {
float latitude = Float.parseFloat(nextLine[columnLatitude].trim());
float longitude = Float.parseFloat(nextLine[columnLongitude].trim());
try {
writeLine[writeLine.length - 1] = landOrSeaService.classifyLocation(longitude, latitude,
ALLOWED_DISTANCE_FROM_COAST);
} catch (Exception e) {
writeLine[writeLine.length - 1] = e.getMessage();
}
} catch (NumberFormatException e) {
writeLine[writeLine.length - 1] = e.getMessage();
}
} while (input != null && !"q".equals(input));
writer.writeNext(writeLine);
}
writer.close();
reader.close();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment