Database Queries Using GIS: The ESRI REST API Without Geography

6 Jan

When people think of GIS they think of maps. But GIS is about data and if we think about it as data it is not special. In this post, I will show you how to use your GIS data as a database and nothing more. I am going to build a website that displays a table of all the public art in Albuquerque with the artist, title and a link to an image of the piece.

First, go to the Albuquerque Public Data website and select Public Art. You will see a directory listing. Choose the last option: PublicArtREST. You will then see a bunch of the ArcServer information for the service. This URL could be used to load the data in to a map for display, but we are only interested in the tabular data at this time. On the bottom of the page you will see the supported operations. We are going to use the query operation. Click the query link and you will see a form. In the where box, type 1=1 and then click the button query (GET). a list of features will show up under the form. See the geographic data available as (x.y) coordinates? In the outfields box, type a *. Then set the return geometry option to False. Now press Query (GET) again. Now you have all the data from the database (there are x,y fields only because they are an actual column in the table. This is not the geometry from ESRI. You can tell because the projection changed – it is now in wkid:4326 and the ESRI version is in wkid:3857).

Now you know where the data lives and how to get it without the geographic portion. The next step is to grab it using AJAX. The key line in the AJAX query will be the parameters:

var params = “where=1=1&outfields=*&f=json”;

You used the first two parameters in the form. The 1=1 returns all the records(ArcServer limits the number of results so a large dataset may only return between 1000-2500 unless it is set for more). The outfield=* returns all the fields for each feature. The last parameter “f=JSON” is returning the JSON data and not the HTML underneath the form. This makes it easy to parse in JavaScript.

The complete code will create two DIV elements: one for the count of public art pieces; and one for the table of results. It will make an AJAX post request, count the records and write out the number, then create a table of the results by sticking each line in to an array then dumping it at the end. The result is below with the complete code after the image.

Table of Public Art with Link to Image

Table of Public Art with Link to Image

<html>
<head>
<title>PUBLIC ART</title>
</head>
<body>
<div id=”count”></div>
<div id=”data”></div>
<script>
var lines=[“<table border=’1′><tr><td>TITLE</td><td>ARTIST</td><td>IMAGE URL</td></tr>”];
var params = “where=1=1&outfields=*&f=json”;
var url = “http://coagisweb.cabq.gov/arcgis/rest/services/public/PublicArt/MapServer/0/query&#8221;;
http=new XMLHttpRequest();
http.open(“POST”, url, true);
http.setRequestHeader(“Content-type”, “application/x-www-form-urlencoded”);
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
theGeom= JSON.parse(http.responseText);
var key, count = 0;
for(key in theGeom.features) {
if(theGeom.features.hasOwnProperty(key)) {
count++;
}}
document.getElementById(“count”).innerHTML=”<h3>There are “+count+” pieces of Public Art in Albuquerque</h3>”;

for(i=0;i<count;i++){

lines.push(“<tr><td>”+theGeom.features[i].attributes.TITLE+”</td><td>”+theGeom.features[i].attributes.ARTIST+”</td><td><a href='”+theGeom.features[i].attributes.IMAGE_URL+”‘>Image</a></td></tr>”);}

lines.push(“</table>”);

document.getElementById(“data”).innerHTML=lines.join(” “);

}}

http.send(params);
</script>
</body>
</html>

Now you know how to use a GIS service to retrieve data without a map. You can refine your query to only pieces by a certain artist, or create a table that lists artists and the number of pieces they have in the collection.

Happy Hacking.

Advertisements

2 Responses to “Database Queries Using GIS: The ESRI REST API Without Geography”

  1. dineshramitc February 7, 2015 at 9:03 am #

    Reblogged this on Dinesh Ram Kali..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: