Last active
December 30, 2015 11:19
-
-
Save fiskurgit/7822233 to your computer and use it in GitHub Desktop.
Quickly hacked together Java app to read a CSV file and convert old OS Easting and Northing values to Latitude and Longitude. Dependencies: JCoord: http://www.jstott.me.uk/jcoord
OpenCSV: http://opencsv.sourceforge.net/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fiskur.osgb2latlon; | |
import au.com.bytecode.opencsv.CSVReader; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import uk.me.jstott.jcoord.LatLng; | |
import uk.me.jstott.jcoord.OSRef; | |
/** | |
* | |
* @author jonathan.fisher | |
*/ | |
public class osgb2latlon { | |
private static final String COMMA = ","; | |
private static final String NEW_LINE = "\n"; | |
private static CSVReader mReader = null; | |
private static FileWriter mWriter = null; | |
/** | |
* Usage: java -jar osgb2latlon.jar somecsvfile.csv | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
if(args == null || args.length == 0){ | |
System.out.println("No CSV file"); | |
return; | |
} | |
String filename = args[0]; | |
if(!filename.toLowerCase().endsWith(".csv")){ | |
System.out.println("Invalid file format"); | |
return; | |
} | |
File csvFile = new File(filename); | |
if(!csvFile.exists()){ | |
System.out.println("File does not exist"); | |
return; | |
} | |
String outFilename = filename.substring(0, filename.lastIndexOf(".")) + "_converted.csv"; | |
File outFile = new File(outFilename); | |
if(outFile.exists()){ | |
outFile.delete(); | |
} | |
try { | |
mReader = new CSVReader(new FileReader(csvFile)); | |
mWriter = new FileWriter(outFile); | |
String[] colNames = mReader.readNext(); | |
int numCols = colNames.length; | |
String[] newColNames = new String[numCols]; | |
int eastingColIndex = 0; | |
int northingColIndex = 0; | |
boolean foundEasting = false; | |
boolean foundNorthing = false; | |
for(int i = 0 ; i < numCols ; i++){ | |
String colName = colNames[i]; | |
newColNames[i] = colName; | |
if(colName.toLowerCase().equals("easting")){ | |
eastingColIndex = i; | |
foundEasting = true; | |
newColNames[i] = "latitude"; | |
} | |
if(colName.toLowerCase().equals("northing")){ | |
northingColIndex = i; | |
foundNorthing = true; | |
newColNames[i] = "longitude"; | |
} | |
} | |
if(!foundEasting || !foundNorthing){ | |
System.out.println("CSV does not contain Easting and Northing columns"); | |
return; | |
} | |
writeCSVLine(newColNames); | |
String [] rowArray; | |
while ((rowArray = mReader.readNext()) != null) { | |
String easting = rowArray[eastingColIndex]; | |
String northing = rowArray[northingColIndex]; | |
OSRef osRef = new OSRef(Double.parseDouble(easting), Double.parseDouble(northing)); | |
LatLng latLon = osRef.toLatLng(); | |
latLon.toWGS84();//See http://hannahfry.co.uk/2012/02/01/converting-british-national-grid-to-latitude-and-longitude-ii/ | |
rowArray[eastingColIndex] = "" + latLon.getLat(); | |
rowArray[northingColIndex] = "" + latLon.getLng(); | |
writeCSVLine(rowArray); | |
} | |
} catch (FileNotFoundException ex) { | |
System.out.println(ex.toString()); | |
System.exit(-1); | |
} catch (IOException ex) { | |
System.out.println(ex.toString()); | |
System.exit(-1); | |
} finally{ | |
try { | |
if(mReader != null){ | |
mReader.close(); | |
} | |
if(mWriter != null){ | |
mWriter.close(); | |
} | |
} catch (IOException ex) { | |
System.out.println(ex.toString()); | |
} | |
} | |
} | |
private static void writeCSVLine(String[] data){ | |
try { | |
for(String field : data){ | |
mWriter.write(field + COMMA); | |
} | |
mWriter.append(NEW_LINE); | |
} catch (IOException ex) { | |
System.out.println(ex.toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment