AddresseToGPS.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. public Lieu[] getAllLieu() {
  68. String get = ISPDAO.getInstance().executeGet(getAddressToQuerry());
  69. JSONArray ja = new JSONArray(get);
  70. if(ja.length()<1) {
  71. return null;
  72. }else {
  73. int len = ja.length();
  74. Lieu[] l = new Lieu[len];
  75. for(int i=0;i<len;++i) {
  76. l[i]=new Lieu(ja.getJSONObject(i));
  77. }
  78. return l;
  79. }
  80. }
  81. }