Thursday, July 16, 2015
address to coordinates transformation through openstreetmap
http://nominatim.openstreetmap.org/search/gb/birmingham/pilkington%20avenue/135?format=xml&polygon=1&addressdetails=1
Search through the above url and the response is in xml format.
I have written a java app to convert address to coordinates
here is my code:
package javaconnection;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class JavaConnection
{
public static void main(String[] args) throws MalformedURLException, IOException, SAXException, ParserConfigurationException, Exception
{
String roadnumber = "135";
String AvenuneName = "pilkington";
String avenue = "avenue";
String Country = "birmingham";
String BaseURL = "http://nominatim.openstreetmap.org/search/";
String uri =
BaseURL + roadnumber + "%20" + AvenuneName + "%20"
+ avenue + "," + "%20" + Country
+ "?format=xml&point=1&addressdetails=1";
URL url = new URL(uri);
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");
InputStream xml = connection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xml);
prettyPrint(doc);
}
public static final void prettyPrint(Document xml) throws Exception
{
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
Writer out = new StringWriter();
tf.transform(new DOMSource(xml), new StreamResult(out));
for (String coordinates : out.toString().split(" "))
{
if(coordinates.contains("lon="))
System.out.println(coordinates);
if(coordinates.contains("lat="))
System.out.println(coordinates);
}
// System.out.println(out.toString());
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment