Create a gist now

Instantly share code, notes, and snippets.

package com.ianturton.w3w;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
public class W3WApi {
// to fetch position of words use
// https://api.what3words.com/w3w?key=YOURAPIKEY&string=word1.word2.word3
// to fetch words for position use
// https://api.what3words.com/position?key=YOURAPIKEY&position=lat,long
final static private GeometryFactory geomFac = new GeometryFactory();
private String key;
public W3WApi() throws FileNotFoundException, IOException {
this(".w3wkey");
}
public W3WApi(String keyFile) throws FileNotFoundException, IOException {
String home = System.getenv("HOME");
File keyfile = new File(home, keyFile);
BufferedReader bufferedReader = new BufferedReader(new FileReader(keyfile));
key = bufferedReader.readLine();
bufferedReader.close();
}
public String getWords(Point p) {
ArrayList<String> ret = new ArrayList<>();
try {
URL fetch = new URL("https://api.what3words.com/position?key=" + key + "&position=" + p.getX()+","+p.getY());
InputStreamReader reader = new InputStreamReader(fetch.openStream());
JSONParser parser = new JSONParser();
JSONObject out = (JSONObject) parser.parse(reader);
JSONArray w = (JSONArray) out.get("words");
for(int i=0;i<w.size();i++) {
ret.add((String) w.get(i));
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuilder sb = new StringBuilder();
String sep = "";
String separator=".";
for(String s: ret) {
sb.append(sep).append(s);
sep = separator;
}
return sb.toString();
}
public Point getPosition(String w1, String w2, String w3) {
return getPosition(w1 + "." + w2 + "." + w3);
}
public Point getPosition(String words) {
Coordinate coord = new Coordinate(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
try {
URL fetch = new URL("https://api.what3words.com/w3w?key=" + key + "&string=" + words);
InputStreamReader reader = new InputStreamReader(fetch.openStream());
JSONParser parser = new JSONParser();
JSONObject out = (JSONObject) parser.parse(reader);
JSONArray p = (JSONArray) out.get("position");
coord = new Coordinate(((Double) p.get(0)).doubleValue(),
((Double) p.get(1)).doubleValue());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Point ret = geomFac.createPoint(coord);
return ret;
}
public static void main(String[] args) throws FileNotFoundException, IOException {
W3WApi test = new W3WApi();
Point p1 = test.getPosition("daring", "lion", "race");
System.out.println(p1);
Point p2 = test.getPosition("glee", "elder", "gazed");
System.out.println(p2);
String w1 = test.getWords(p1);
System.out.println(w1);
String w2 = test.getWords(p2);
System.out.println(w2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment