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