Converting just a few?
Use a website. Like this one: http://www.nearby.org.uk/conversions.cgi
Converting a big batch?
Say you have a text file (CSV maybe) with 1.7 million entries (for example, all the postcodes in the UK from the Ordnance Survey Code Point data, which is now free and downloadable here).
You want to convert them all but don't fancy doing them one by one because you have a life.
To do this, I've used Python and Mapnik. You'll need some (very) basic programming skills to do this example. I'm in Linux (Ubuntu to be precise), so you might have to adapt this slightly if you're using something else.
You'll need Python and Mapnik installed. In Ubuntu, I installed the following packages:
You might not need all of them, but I have them installed. Then, download a little example I put together here. Don't worry, even if you don't know Python. It's really simple.
Decompress it into a folder, then in a command line type:
python en_to_latlng.py input.txt
The input.txt file is just an example to get you started. Notice it just dumps the output to the console. To save it to a file:
python en_to_latlng.py input.txt > output.txt
The "greater than" symbol just pushes all the output into output.txt using output redirection.
Doing it in Java?
Yes, I like Java too. I used GeoTools to do this conversion in Java. If you use Maven, it's fantastically easy to add the dependencies. You'll need to add 3 things to your pom.xml:
- Property which defines which version you're using
- The dependencies themselves
- The repository (from which to download the geotools JARs and POMs)
<!-- include WarpAffine to prevent ClassLoader error -->
<name>Open Source Geospatial Foundation Repository</name>
As for the code itself, it's pretty straightforward:
CRSAuthorityFactory crsFac =
CoordinateReferenceSystem wgs84crs = crsFac.createCoordinateReferenceSystem("4326");
CoordinateReferenceSystem osgbCrs = crsFac.createCoordinateReferenceSystem("27700");
CoordinateOperation op = new DefaultCoordinateOperationFactory().createOperation(osgbCrs, wgs84crs);
DirectPosition eastNorth = new GeneralDirectPosition(easting, northing);
DirectPosition latLng = op.getMathTransform().transform(eastNorth, eastNorth);
System.out.println("lat: " + latLng.getOrdinate(0));
System.out.println("lng: " + latLng.getOrdinate(1));