AddresseToGPS.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package data;
  2. import org.json.JSONArray;
  3. import org.json.JSONObject;
  4. public class AddresseToGPS {
  5. private String adresse;
  6. public static final String NOMINATUM = "https://nominatim.openstreetmap.org/search/";
  7. public static final String FORMAT = "json";
  8. public static final int LIMIT = 3;
  9. /**
  10. * Lieu est un groupement d'un Nom a afficher et d'une coordonnée GPS.
  11. */
  12. public class Lieu{
  13. public final String DisplayName;
  14. public final Coordinates coordonees;
  15. public Lieu(String displayName, Coordinates coordonees) {
  16. super();
  17. DisplayName = displayName;
  18. this.coordonees = coordonees;
  19. }
  20. public Lieu(JSONObject jo) {
  21. super();
  22. DisplayName = jo.getString("display_name");
  23. this.coordonees = new Coordinates(jo.getDouble("lat"), jo.getDouble("lon"));
  24. }
  25. public String toString() {
  26. return DisplayName+" : "+coordonees;
  27. }
  28. }
  29. public AddresseToGPS(String adresse) {
  30. this.adresse = adresse;
  31. }
  32. /**
  33. * @return l'adresse
  34. */
  35. public String getAdresse() {
  36. return adresse;
  37. }
  38. /**
  39. * Récupere l'URL pour la requète a Nominatum pour {@link AddresseToGPS#adresse}
  40. * @return
  41. */
  42. public String getAddressToQuerry() {
  43. String s= NOMINATUM+adresse+"?format="+FORMAT+"&limit="+LIMIT;
  44. s=s.replaceAll("\\s", "%20");
  45. return s;
  46. }
  47. /**
  48. * Récupère les coordonnée du lieu correspondant à l'adresse
  49. * @return Coordonnée du lieu
  50. * @throws ErrorAddressException si plusieurs addresses sont possibles.
  51. */
  52. public Coordinates getCoordinates() throws ErrorAddressException {
  53. Lieu l = getLieu();
  54. return l.coordonees;
  55. }
  56. /**
  57. * Récupère les coordonnée du lieu correspondant à l'adresse parmis plusieurs
  58. * @param choice identifiant du choix que l'ont fait parmis les multiples possibilitées de lieu
  59. * @return
  60. */
  61. public Coordinates getCoordinatesWithChoiceForced(int choice) {
  62. Lieu l = getLieuWithChoiceForced(choice);
  63. return l.coordonees;
  64. }
  65. /**
  66. * Effectue la requète pour récuperer le lieu et le renvoie
  67. * @return Lieu correspondant ou null si il n'y en a pas
  68. * @throws ErrorAddressException si il y a plusieurs lieux possibles
  69. */
  70. public Lieu getLieu() throws ErrorAddressException {
  71. String get = ISPDAO.getInstance().executeGet(getAddressToQuerry());
  72. JSONArray ja = new JSONArray(get);
  73. if(ja.length()<1) {
  74. return null;
  75. }else if(ja.length()>1) {
  76. int len = ja.length();
  77. Lieu[] l = new Lieu[len];
  78. for(int i=0;i<len;++i) {
  79. l[i]=new Lieu(ja.getJSONObject(i));
  80. }
  81. throw new ErrorAddressException(l, adresse);
  82. }else {
  83. Lieu l = new Lieu(ja.getJSONObject(0));
  84. return l;
  85. }
  86. }
  87. /**
  88. * Même chose que {@link AddresseToGPS#getLieu()} mais avec le choix forcé sur l'identifiant
  89. * @param choice identifiant du choix
  90. * @return Lieu choisi parmis la liste ou null si aucun n'existe
  91. */
  92. public Lieu getLieuWithChoiceForced(int choice) {
  93. String get = ISPDAO.getInstance().executeGet(getAddressToQuerry());
  94. JSONArray ja = new JSONArray(get);
  95. JSONObject jo = ja.getJSONObject(choice);
  96. if(jo == null) {
  97. return null;
  98. }
  99. Lieu l = new Lieu(jo);
  100. return l;
  101. }
  102. /**
  103. * Récupere un tableau de Lieu correspondant a l'adresse indiquée.
  104. * @return
  105. */
  106. public Lieu[] getAllLieu() {
  107. String get = ISPDAO.getInstance().executeGet(getAddressToQuerry());
  108. JSONArray ja = new JSONArray(get);
  109. if(ja.length()<1) {
  110. return null;
  111. }else {
  112. int len = ja.length();
  113. Lieu[] l = new Lieu[len];
  114. for(int i=0;i<len;++i) {
  115. l[i]=new Lieu(ja.getJSONObject(i));
  116. }
  117. return l;
  118. }
  119. }
  120. }