Jayant's Blog

Wednesday, August 13, 2008

Using Php with ArcGIS Server 9.3 REST (v0.03)

So here's another sample. I have a pre-populated JSON file (addresses.txt) with a bunch of addresses to be geocoded.

{
  "addresses": [
    { "name":"ESRI", "street":"380 New York St", "city":"Redlands", "state":"CA", "zip":92373 },
    { "name":"Google", "street":"1600 Amphitheatre Parkway", "city":"Mountain View", "state":"CA", "zip":94043 },
    { "name":"Yahoo", "street":"701 First Avenue", "city":"Sunnyvale", "state":"CA", "zip":94089 },
    { "name":"Microsoft", "street":"One Microsoft Way", "city":"Redmond", "state":"WA", "zip":98052 }
  ]
}


This code shows how to load up the address list from the JSON file. I tried naming the file as .json, but am guessing since my json mime-type has not been set correctly, it wasn't loaded properly.

<?php
$url = "addresses.txt";
$response = file_get_contents($url);
$json = json_decode($response);
?>

Next is the code to take the address, build the request and use the geocoding service on sample server to geocode the addresses.

<?php
$geocodeUrl = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" . "Locators/ESRI_Geocode_USA/GeocodeServer/" . "findAddressCandidates?f=json";
foreach($json->addresses as $address) {
  $url = str_replace(" ", "+", $geocodeUrl . "&Address=" . $address->street . "&City=" . $address->city . "&State=" . $address->state . "&Zip=" . $address->zip);

  $response = file_get_contents($url);
  $candidates = json_decode($response);
?>

Next filter the address candidates and display only those candidates who's score is 100. We simply print these.

<?php
  $html = "Address candidate(s) for (" . $address->name . "\n<ul>\n";
  foreach($candidates->candidates as $candidate) {
    if ($candidate->score == 100) {
      $html .= "<li>" . $candidate->address . " (" . $candidate->location->x . ", " . $candidate->location->y . ")</li>";
    }
  }
  $html .= "</ul>\n";
  echo $html;
}
?>

Labels: ,

Saturday, August 09, 2008

Using Php with ArcGIS Server 9.3 REST (v0.02)

One of the powers of using GIS is that in addition to tabular data you also have access to spatial data as part of each record. So in the following Php code, I will query to find 'Los Angeles' and simple display the result with the coordinates of the city.

<?php
$url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services" . "/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0/query" . "?f=json&where=CITY_NAME='Los+Angeles'";
$response = file_get_contents($url);
$json = json_decode($response);

$la = $json->features[0];
echo $la->attributes->CITY_NAME . " (" . $la->geometry->x . ", " . $la->geometry->y . ")";
?>

Labels: ,

Using Php with ArcGIS Server 9.3 REST

The latest release of ArcGIS Server now supports REST access to the services. The best thing, it returns JSON. And at Php 5.2, there is support for JSON. So how simple is it to request an process a response?

Well, first use file_get_contents to call the REST endpoint. Next decode the JSON response as an object using json_decode. So here's a really simple sample to get a map image. Although I can't really see why you would use this rather than using the ArcGIS JavaScript API.

<?php
$url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services" . "/Specialty/ESRI_StateCityHighway_USA/MapServer/export?f=json";
$response = file_get_contents($url);
$json = json_decode($response);

echo "<img src='" . $json->href . "' width='" . $json->width . "' height='" . $json->height . "' />";
?>

Labels: ,

Saturday, August 02, 2008

The ArcGIS JavaScript API on dojotoolkit.org

The Dojo Toolkit website has added a section in their spotlight page about the ArcGIS JavaScript API page. Check it out http://dojotoolkit.org/spotlight. Click on the ESRI link to see the details page. Dylan also added a blog entry on the website as well ESRI Launches Dojo-based GIS JavaScript API.

Thanks to Dylan for spending the time putting this page up despite his busy schedule.

Labels: , ,