AddresseToGPS.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package data;
  2. import org.json.JSONArray;
  3. import org.json.JSONObject;
  4. import main.Bot;
  5. public class AddresseToGPS {
  6. private String adresse;
  7. public static final String NOMINATUM = "https://nominatim.openstreetmap.org/search/";
  8. public static final String FORMAT = "json";
  9. public static final int LIMIT = 3;
  10. private Bot bot;
  11. public class Lieu{
  12. public final String DisplayName;
  13. public final Coordinates coordonees;
  14. public Lieu(String displayName, Coordinates coordonees) {
  15. super();
  16. DisplayName = displayName;
  17. this.coordonees = coordonees;
  18. }
  19. public Lieu(JSONObject jo) {
  20. super();
  21. DisplayName = jo.getString("display_name");
  22. this.coordonees = new Coordinates(jo.getDouble("lat"), jo.getDouble("lon"));
  23. }
  24. }
  25. public AddresseToGPS(String adresse, Bot b) {
  26. this.adresse = adresse;
  27. this.bot = b;
  28. }
  29. public String getAddressToQuerry() {
  30. return NOMINATUM+adresse+"?format="+FORMAT+"&limit="+LIMIT;
  31. }
  32. public Coordinates getCoordinates() throws MultiplePossibleAddressException {
  33. Lieu l = getLieu();
  34. return l.coordonees;
  35. }
  36. public Coordinates getCoordinatesWithChoiceForced(int choice) {
  37. Lieu l = getLieuWithChoiceForced(choice);
  38. return l.coordonees;
  39. }
  40. public Lieu getLieu() throws MultiplePossibleAddressException {
  41. String get = ISPDAO.getInstance().executeGet(getAddressToQuerry());
  42. JSONArray ja = new JSONArray(get);
  43. if(ja.length()<1) {
  44. return null;
  45. }else if(ja.length()>1) {
  46. int len = ja.length();
  47. Lieu[] l = new Lieu[len];
  48. for(int i=0;i<len;++i) {
  49. l[i]=new Lieu(ja.getJSONObject(i));
  50. }
  51. throw new MultiplePossibleAddressException(l, adresse);
  52. }else {
  53. Lieu l = new Lieu(ja.getJSONObject(0));
  54. return l;
  55. }
  56. }
  57. public Lieu getLieuWithChoiceForced(int choice) {
  58. String get = ISPDAO.getInstance().executeGet(getAddressToQuerry());
  59. JSONArray ja = new JSONArray(get);
  60. JSONObject jo = ja.getJSONObject(choice);
  61. if(jo == null) {
  62. return null;
  63. }
  64. Lieu l = new Lieu(jo);
  65. return l;
  66. }
  67. }