Forum

Welcome Guest 

Show/Hide Header

Welcome Guest, posting in this forum requires registration.





Pages: [1]
Author Topic: Bing Maps REST Geocoding
admin
Administrator
Posts: 4
Post Bing Maps REST Geocoding
on: June 19, 2011, 17:09

Last week I had an inquiry from a potential client for geocoding 10000 address in US.

This is nothing new or complicated for me but after 200 test geocoding records he sad that the accuracy of

geocoding which suggested is not enough. It was usual routable geocoding.
So after three days of hard work now I can honestly say that I geocoding up to 50000 records per day.

And,you know- the beauty with REST is that you simply need to build URL queries and handle the JSON or XML

responses. It’s quite beautiful actually and it’s essentially the way the WWW was architected, so the learning

curve will be fairly flat.

Below you will find the sources of knowledge and source code.

1.
http://www.bing.com/community/site_blogs/b/maps/archive/2010/06/07/rest-easy-bing-maps-has-you-covered.aspx

2.
http://msdn.microsoft.com/en-us/library/ff817004.aspx

3.
http://msdn.microsoft.com/en-us/library/ff701714.aspx

4.
http://msdn.microsoft.com/en-us/library/ff701737.aspx

5.
http://msdn.microsoft.com/en-us/library/ff701732.aspx

6.
http://msdn.microsoft.com/en-us/library/ff701736.aspx

Sourse Code:

The XML Feed:

<GeocodeFeed>
 <GeocodeEntity Id="001"
    xmlns="http://schemas.microsoft.com/search/local/2010/5/geocode">
 <GeocodeRequest Culture="en-US">
  <Address AddressLine="291 Broadway" AdminDistrict="NY" Locality="New York"
    PostalCode="10007" />
 </GeocodeRequest>
 </GeocodeEntity>
 <GeocodeEntity Id="002"
    xmlns="http://schemas.microsoft.com/search/local/2010/5/geocode">
 <GeocodeRequest Culture="en-US">
  <Address AddressLine="599 Broadway" AdminDistrict="NY" Locality="New York" />
  <ConfidenceFilter MinimumConfidence="High" xmlns="" />
 </GeocodeRequest>
 </GeocodeEntity>
 <GeocodeEntity Id="003"
   xmlns="http://schemas.microsoft.com/search/local/2010/5/geocode">
 <GeocodeRequest Culture="en-US" Query="Empire State Building" />
 </GeocodeEntity>
</GeocodeFeed>

The Code:

<html>
 <head>
 <title>Using the Bing Spatial Data Geocode Dataflow API</title>
 </head>
 <body>
 <?php

$key = "Bing Maps Key";
$url = "http://spatial.virtualearth.net/REST/v1/Dataflows/Geocode?

description=MyJob&input=xml&output=xml&key=".$key;

// STEP 1 - Create a geocode job

// Get the contents of an XML data file
$myfile = "geocodeFeed.xml";
$data = file_get_contents($myfile);

// Call custom function to generate an HTTP request and get back an HTTP response
$response = do_post_request($url, $data);

// This function constructs and sends an HTTP request with a provided URL and data, and returns an HTTP 

response object
// This function uses the php_http extension
function do_post_request($url, $data, $optional_headers = null) {
 $request = new HttpRequest($url, HttpRequest::METH_POST);
 $request->setBody($data);
 $request->setContentType("application/xml");
 $response = $request->send();
 return $response->getBody();
}

// Convert the response body into an XML element so we can extract data
$responseBody = new SimpleXMLElement($response);

// Get data (such as job id, status description, and job status) from the response
$statusDescription = $responseBody->StatusDescription;
$jobId = $responseBody->ResourceSets->ResourceSet->Resources->DataflowJob->Id;
$jobStatus = $responseBody->ResourceSets->ResourceSet->Resources->DataflowJob->Status;

echo "Job Created:<br>";
echo " Request Status: ".$statusDescription."<br>";
echo " Job ID: ".$jobId."<br>";
echo " Job Status: ".$jobStatus."<br><br>";

// STEP 2 - Get the status of geocode job(s)

// Call the API to determine the status of all geocode jobs associated with a Bing Maps key
echo "Checking status until complete...<br>";
while ($jobStatus != "Completed") {

 // Wait 5 seconds, then check the job’s status
 sleep(5);

 // Construct the URL to check the job status, including the jobId
 $checkUrl = "http://spatial.virtualearth.net/REST/v1/Dataflows/Geocode/".$jobId."?&output=xml&key=".$key;
 $checkResponse = file_get_contents($checkUrl);
 $responseBody = new SimpleXMLElement($checkResponse);

 // Get and print the description and current status of the job
 $jobDesc = $responseBody->ResourceSets->ResourceSet->Resources->DataflowJob->Description;
 $jobStatus = $responseBody->ResourceSets->ResourceSet->Resources->DataflowJob->Status;

 echo $jobDesc." - ".$jobStatus."<br>";
}

echo "checkUrl: " . $checkUrl . "<br>";

// STEP 3 - Obtain results from a successfully geocoded set of data

$successUrl="";

//Iterate through the links provided with the first geocode job and extract the 'succeeded' link
$Links = $responseBody->ResourceSets->ResourceSet->Resources->DataflowJob->Link;
foreach ($Links as $Link) {
 if ($Link['name'] == "succeeded")
 {
 $successUrl .= $Link;
 break;
 }
}

// Access the URL for the successful requests, and convert response to an XML element
$successUrl .= "?output=xml&key=".$key;
$successResponse = file_get_contents($successUrl);
$successResponseBody = new SimpleXMLElement($successResponse);

// Loop through the geocoded results and output addresses and lat/long coordinates
foreach ($successResponseBody->GeocodeEntity as $entity) {
 echo $entity->GeocodeResponse->Address['FormattedAddress'],"<br>";
 if (!$entity->GeocodeResponse->RooftopLocation) {
 echo $entity->GeocodeResponse->InterpolatedLocation['Longitude'].", ";
 echo $entity->GeocodeResponse->InterpolatedLocation['Latitude']."<br>";
 }
 else {
 echo $entity->GeocodeResponse->RooftopLocation['Longitude'].", ";
 echo $entity->GeocodeResponse->RooftopLocation['Latitude']."<br>";
 }
}

?>

 </body>
</html>

The Output:

Job Created:
Request Status: Created
Job ID: 15702ecbc3724f3eb4a67dd48962b20e
Job Status: Pending

Checking status until complete…
MyJob – Pending
MyJob – Pending
MyJob – Pending
MyJob – Pending
MyJob – Pending
MyJob – Pending
MyJob – Pending
MyJob – Pending
MyJob – Completed
checkUrl: http://spatial.virtualearth.net/REST/v1/Dataflows/Geocode/15702ecbc3724f3eb4a67dd48962b20e?

&output=xml&key=AlFh4_XMKnLnvK1XEP2Dny75N88a0UNtl9krISHp0WTe7WSYsNak6KjndXolidaT
291 Broadway, New York, NY 10007-1814
-74.006134, 40.715
599 Broadway, New York, NY 10012-3235
-73.99702, 40.725111
Empire State Building, NY
-73.98581, 40.748435

Pages: [1]
Mingle Forum by cartpauj
Version: 1.0.31.3 ; Page loaded in: 0.025 seconds.

28/08/2010 | Comments Closed