Great Expectations

I had partial success with my mapping project this weekend.

I had originally planned to map each state with little popup boxes showing details on the numbers for each state. It turns out that I greatly underestimated both my skills with JSON files and the Google APIv3 for maps.

But let me back up. I did have success mapping multiple points on a map. I also successfully mapped a point on a map with a detail window on click (click on the marker to see the details.) However, when I tried to map multiple points on a map with detail boxes, that’s where the train went off the rails.

Apparently, the Google Maps API has issues with looping through a long list of details to set markers and info boxes. It only pulls the last info box in the list. There are a nice bunch of tutorials on how to fix the issue, BUT since I am not as well schooled in javascript and calling JSON information, I could not get it to work.. although I did try. My attempt to get it to work is avail here and frankly, I think it’s a big old mess. For one, I’m not sure how to call the separate JSON file. [The tutorial I was using is here. I have his book and his examples were working well for me up until this point, which makes me think it’s a js/json problem.]

For reference, here is my JSON data.

var json = [
{
"state": "Virginia",
"lat": 37.768002,
"lng": -78.205704,
"detail": "In Virginia, 1254 men were executed between 1620 and 2002; 88 women were executed in this same time frame."
},
{
"state": "West Virginia",
"lat": 38.467999,
"lng": -80.969597,
"detail": "In West Virginia, 150 men were executed between 1620 and 2002; 4 women were executed in this same time frame.."
},
{
"state": "Maryland",
"lat": 39.072399,
"lng": -76.790199,
"detail": "In Maryland, 293 men were executed between 1620 and 2002; 11 women were executed in this same time frame.."
}
]

 

I’ll continue to try to debug and hack at it to get it to work, but I’m finding the online documentation only partially helpful. One big problem is that I’m not totally confident that I know how to code the start and end to each file. So when I find code snippets of “solutions” to common problems, I’m not entirely sure where to put the code. Since the order is really important, I’m sure I have some things out of order.

Full code examples would be more helpful so that I can see it in context, but I know this is a learning curve problem, and that it will diminish with time. These “help” blog posts are written for experienced coders and right now I’m a hack at best. So for the time being, I’m embracing my hack status and putting this to the side as a problem for another day.

Getting Ready to Map

I started the evening ready to tutorialize myself in Google APIs and mapping examples. I read two paragraphs and realized that I’d better do some prep work on my own data before I continue.

I also realized that I can organize myself better if I set some goals for my coding work. I have one goal this week: Successfully map all the executions in the database with fancy little popup boxes showing the details for each state. That’s it.

Once I get that to work, for my tutorial in two weeks, I’ll drill down into Virginia, Maryland and DC to show more detailed local information. But, don’t let me get ahead of myself.

On the prepping side, every tutorial I looked at pleaded with the reader NOT to geocode the latitude and longitude information on the server, but to do it in the source database whenever possible.  Since I only have 51 states to deal with and the information is static, adding in lat/long for each execution in the database wasn’t going to require that much brain damage. I created a new table of state lat/long information and populated the appropriate general state lat/long information. [Like everything I do in phpMyAdmin, I did one state by hand and looked at the SQL commands to replicate for the other 50 states. (After 8 weeks, I am beginning to realize that I learn by trial and error–useful, but incredibly frustrating.)] Per Google’s request, I created my lat and long fields in the new table as FLOATS with the size of (10,6), which will allow me to store 6 digits after the decimal, plus up to 4 digits before the decimal, e.g. -123.456789 degrees. I then joined this new table to my main executions table with a new foreign key between my original executions table and the id from the statelatlong table.

I then needed to make sure I actually understood how to JOIN these tables, so I walked through a tutorial (on Wikipedia nobles), replaced their fields with my own and TA DA! it worked. For reference, all the information in the results, save lat and long, are located in my original table. The lat long coordinates are stored in the new table. (The code I used is here on my github.)

Now I feel like I’m ready to actually start trying to get my maps to work… the results of which, I’ll describe in a new post. Once I get there.

Averting Distraction and Thinking Ahead

Sometimes you aren’t as productive as you’d like to be. Fred warned us that this class might do that, so I’m taking it as part of the territory. This week I ignored the urge to scrap my entire site and rebuild it using the fancy new tools in my toolkit. I admit that redesigning websites seems to be brain candy to me. Make it pretty. Make it blue. Make it different. I’d like to re-engineer my information architecture so that I group pages more thoughtfully. I would like to use includes so that I don’t need to update every single page any time I make a change to the footer, but getting them to work is going to take more time than I have right now. I’d like to add in a Creative Commons license. I resisted all of this; it can all wait until after the semester is over.

So instead I thought ahead to the Mapping tutorial I have to do in a couple of weeks. I realized I need to get re-acquainted with the Google API and that’s where I stopped. Suffice it to say, I’m looking forward to tonight’s web scraping tutorial and this weekend I’ll spend more time on the API, then I’ll design my tutorial.

Ebbs and flows of productivity, right?

Getting Answers from Your Data

My assignment this week was was to run the queries I “designed” last week. (*I am, of course, using the term “design” very loosely, as last week was full of hand wringing and general PHP consternation.)

This week, I ran 4 total queries. I am pretty happy with how they turned out:

  1. Total numbers of executions of women with details and counts by race and crime.
  2. Counts of executions of women by state.
  3. Details of executions of women in Virginia with details and counts by race and crime.
  4. Counts of executions of women by year.

I had hoped not only to pull results, but also to display them in a way that made sense. The two biggest challenges proved to be pulling details from each execution into a table and displaying the count for individual variables in the state, year, race and crime fields. Because of the nature of what I was trying to learn, I found my PHP code remained relatively simple, whereas my SQL queries got increasingly more complicated.

Tables

I pulled some code from Jeri and from an online tutorial, but when I ran the php, I was creating an individual table for each row of results. By removing the HTML for the table and header rows from the PHP and using generic html tags, I was able to create one large table.

[sourcecode language=”css”]

<p><h2>Details</h2></p>
<p>Details of all the executions of women, in chronological order.</p>
<p><table border=’1′ cellpadding=’10’>
<tr><th>First Name</th><th>Last Name</th><th>Crime</th><th>Method</th><th>State</th><th>Year</th></tr>

<?php

/* Requery # of women total */
$result = mysql_query("SELECT * FROM executions WHERE sex=’F’ ORDER BY executions.year ASC ", $connection);
if (!$result) {
die("Database query failed: ");
}

/* Organize results of Query into a table */
while($row = mysql_fetch_array($result)) {

// set up a row for each record
echo "<tr>";
echo "<td>" . $row[‘first’] . "</td>";
echo "<td>" . $row[‘last’] . "</td>";
echo "<td>" . $row[‘crime’]. "</td>";
echo "<td>" . $row[‘method’]. "</td>";
echo "<td>" . $row[‘state’]. "</td>";
echo "<td>" . $row[‘year’]. "</td>";
echo "</tr>";

}

?>

</table></p>

[/sourcecode]

Counts and Group By

The other queries were created with complex mysql queries. A fun little function in PHPMyAdmin “show PHP” allowed me to see how the query worked. From there I was able to figure out how to phrase the query and the order in which it should appear. To be honest, this was a long process of trial and error, but once I got the syntax, it is easy to cut and paste and adapt for all of my complex queries.

Thanks to Laura for showing me how to phrase my search, I was able to do some Googling to find good examples of successful Count and Grouped queries.

[sourcecode language=”css”]
<?php
/* Query # of women by race in Virginia */
$result = mysql_query("SELECT crime, count(crime) AS crimeCount FROM executions WHERE sex=’F’ AND state=’VA’ GROUP BY crime ORDER BY crimeCount DESC", $connection);
if (!$result) {
die("Database query failed: ");
}

/* Answer */
while($row = mysql_fetch_array($result)){
echo ($row[‘crime’]) . ": " . ($row[‘crimeCount’]) . "<br />";

}
?>
[/sourcecode]

I was surprised by several of the results that came back. I’m now really looking forward to visualizing these results!

Asking Questions of Your Data

Now that my data is normalized and I have a good idea of what is contained in my database, it’s time to start asking questions of my data. What do I want to know? Which queries are only for me? Which would I like to embed into a web page? While they don’t seem like tough questions, it is beginning to dawn on me that my issues with PHP may be more than just a learning curve. It isn’t so much the learning of the language (although, I am struggling with that), it’s also the fact that I don’t sit down and think about what I want it to do BEFORE I start coding.

I dive right in, copy examples and start changing variables and functions without even thinking about what I want it to do, let alone what this code snippet was programmed to do. The maddening part of PHP is that it’s so personal and subjective–each coder has written into the code their own logic and language. What is mine?

<crickets>

Honestly, I don’t know. I do know the questions I want to ask.

  1. How many women were executed in the United States?
  2. For what crimes? (Cluster the crimes)
  3. In what states? (Cluster both the state numbers AND the crimes by state)
  4. Are there any moments in time where there were an abnormally large group of women executed?
  5. Are there any states in which more women were executed over others?
  6. What of Virginia? What happened here?
  7. What overall trends can I see?
  8. What is missing?

Some of these questions are really specific and easy to both code and show. Others are not. All of this is to say that I’m still working on figuring this out. I admit that this “paralysis by analysis” affected my productivity this week. My output wasn’t what I had been hoping. Still, this is all part of the process, right? Right?

PHP Tango

Oh PHP. How you vex me.

Our assignment this week was to play with PHP, specifically to create a working data entry form using HTML, PHP and MySQL. I can report that, with very generous help from my Clio class, I did all of that.

My simple, but working form is here.
My simple, but functional PHP code is here.

I’m still not sure I totally understand WHY it works. I am told this is natural and to be expected. That with time and increased familiarity, my comprehension will improve. I look forward to that day. I will continue to plug away at it if you promise me you won’t judge or mock me for doing a happy dance every time I can actually make something work.

Deal?