java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ext/Geo.java
author František Kučera <franta-hg@frantovo.cz>
Sun Jun 20 14:46:47 2010 +0200 (2010-06-20)
changeset 145 0efefbf5f8b6
parent 121 cd43c349f39b
child 164 e146e2e3b80b
permissions -rw-r--r--
Formátování kódu, důsledné používání tabulátorů, drobné úpravy, StringBuilder
     1 package cz.frantovo.nekurak.ext;
     2 
     3 import java.io.UnsupportedEncodingException;
     4 import java.net.URLEncoder;
     5 import java.util.logging.Level;
     6 import java.util.logging.Logger;
     7 import javax.xml.parsers.DocumentBuilder;
     8 import javax.xml.parsers.DocumentBuilderFactory;
     9 import org.w3c.dom.Document;
    10 import org.w3c.dom.Node;
    11 import org.w3c.dom.NodeList;
    12 
    13 /**
    14  * TODO: přesunout jinam a převést na EJB.
    15  * @author fiki
    16  */
    17 public class Geo {
    18 
    19 	private static final Logger log = Logger.getLogger(Geo.class.getSimpleName());
    20 
    21 	/**
    22 	 * Převede poštovní adresu na zeměpisné souřadnice.
    23 	 * @param adresa
    24 	 * @return souřadnice k dané adrese nebo null, v případě chyby.
    25 	 */
    26 	public Souradnice getSouradnice(String adresa) {
    27 		try {
    28 
    29 			/**
    30 			 * TODO: naprosto zprasené → předělat →
    31 			 * http://code.google.com/intl/cs/apis/maps/documentation/geocoding/#XMLParsing
    32 			 */
    33 			DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    34 			Document d = db.parse(sestavURL(adresa));
    35 			NodeList mista = d.getElementsByTagName("location");
    36 			Node misto = mista.item(0);
    37 			NodeList potomci = misto.getChildNodes();
    38 			String delka = null;
    39 			String sirka = null;
    40 			for (int i = 0; i < potomci.getLength(); i++) {
    41 				Node p = potomci.item(i);
    42 				if ("lat".equals(p.getNodeName())) {
    43 					sirka = p.getTextContent();
    44 				}
    45 				if ("lng".equals(p.getNodeName())) {
    46 					delka = p.getTextContent();
    47 				}
    48 			}
    49 
    50 			if (sirka == null || delka == null) {
    51 				log.log(Level.WARNING, "Selhalo zjišťování souřadnic – šířka nebo délka jsou null – pro adresu: " + adresa);
    52 				return null;
    53 			} else {
    54 				return new Souradnice(Double.parseDouble(sirka), Double.parseDouble(delka));
    55 			}
    56 		} catch (Exception e) {
    57 			log.log(Level.WARNING, "Selhalo zjišťování souřadnic pro adresu: " + adresa, e);
    58 			return null;
    59 		}
    60 	}
    61 
    62 	private static String sestavURL(String adresa) throws UnsupportedEncodingException {
    63 		return "http://maps.google.com/maps/api/geocode/xml?sensor=false&address=" + URLEncoder.encode(adresa, "UTF-8");
    64 	}
    65 
    66 	public class Souradnice {
    67 
    68 		private double sirka;
    69 		private double delka;
    70 
    71 		@Override
    72 		public String toString() {
    73 			return "šířka = " + sirka + "; délka = " + delka + ";";
    74 		}
    75 
    76 		@Override
    77 		public boolean equals(Object o) {
    78 			if (o instanceof Souradnice) {
    79 				Souradnice s = (Souradnice) o;
    80 				return s.sirka == sirka && s.delka == delka;
    81 			} else {
    82 				return false;
    83 			}
    84 		}
    85 
    86 		@Override
    87 		public int hashCode() {
    88 			int hash = 5;
    89 			hash = 79 * hash + (int) (Double.doubleToLongBits(this.sirka) ^ (Double.doubleToLongBits(this.sirka) >>> 32));
    90 			hash = 79 * hash + (int) (Double.doubleToLongBits(this.delka) ^ (Double.doubleToLongBits(this.delka) >>> 32));
    91 			return hash;
    92 		}
    93 
    94 		public Souradnice(double sirka, double delka) {
    95 			this.sirka = sirka;
    96 			this.delka = delka;
    97 		}
    98 
    99 		public String getLoc() {
   100 			return "Loc: " + sirka + ", " + delka;
   101 		}
   102 
   103 		public double getDelka() {
   104 			return delka;
   105 		}
   106 
   107 		public double getSirka() {
   108 			return sirka;
   109 		}
   110 
   111 		public void setDelka(double delka) {
   112 			this.delka = delka;
   113 		}
   114 
   115 		public void setSirka(double sirka) {
   116 			this.sirka = sirka;
   117 		}
   118 	}
   119 }