Archive for the 'Technology' Category

Geotools, WFS-T Update Request

Friday, August 3rd, 2007

A pet project of mine has flung me into the exciting though less-than-firm territory of web-backed geographical information systems. Since I don’t have the thousands of dollars it costs to get a commercial server like those provided by ESRI, I’ve had to check out the open-source alternatives. And there are some out there. I’m using GeoServer, and it works great! I can send all the web-feature service transactions (WFS-T) in XML I want and it works every time. Not bad if you want to make a GoogleMap of your house on your own—so long as you’re content to hard-code everything by hand. Should you want to jazz things up a bit (i.e., make minimally useful dynamic maps), like me, then you have to do a little more work. Actually, you need to do a lot more work.

GeoServer is built on top of a gargantuan set of Java libraries, collectively packaged under the name GeoTools. Now I appreciate that this thing exists, all two hundred and fifty megs of source code and all the functionality that comes with it. However, navigating the mountain of documentation for this thing is, at least for me, a little daunting. It took me a few days (and some serious help from my friend Matt) to figure out how to write a simple update transaction using their API. (Compare that to the forty-two seconds it takes me to type up the XML.)

Since other people might want to know what they have to do update an attribute field using WFS with GeoTools, and since I couldn’t easily find out how to do it elsewhere, I’ve decided to post a short snippet of code right here on my blog. That’s right: my charity knows no bounds.

In this example I’m going to update the value of all the features (polygons, lines, points, whatever) that match a simple filter. Here I’m going to change the value of propertyToUpdate to updatedValue using a filter to get all the features with the attribute called constraintProperty with a value of constraintValue. I’ve marked them in red, so that it’s as easy as possible to customize this example to fit your needs. Let’s start with the XML that the Open Geospatial Consortium standards expects to see.

<wfs:Transaction service=”WFS” version=”1.0.0″
     xmlns:myns=”http://www.domain.com/ns
     xmlns:ogc=”http://www.opengis.net/ogc”
     xmlns:wfs=”http://www.opengis.net/wfs”>
  <wfs:Update typeName=”myns:LayerToUpdate“>
    <wfs:Property>
        <wfs:Name>propertyToUpdate</wfs:Name>
        <wfs:Value>updatedValue</wfs:Value>
    </wfs:Property>
    <ogc:Filter>
        <ogc:PropertyIsEqualTo>
            <ogc:PropertyName>constraintProperty</ogc:PropertyName>
            <ogc:Literal>constraintValue</ogc:Literal>
        </ogc:PropertyIsEqualTo>
    </ogc:Filter>
  </wfs:Update>
</wfs:Transaction>

Now let’s rock out the Java.

Like I said, GeoTools is mammoth. To make life easy, we’re going to import a whole bunch of classes for this example. So many, in fact, that their number really warrants my displaying them here in their own list. What’s more, the names of some of classes (like Filter) show up in more than one package, and you need to keep track of which is used where. So keep an eye out for things from org.opengis.

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

import org.geotools.data.DataStore;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.FeatureStore;
import org.geotools.data.Transaction;
import org.geotools.data.wfs.WFSDataStoreFactory;
import org.geotools.feature.AttributeType;
import org.geotools.feature.FeatureType;
import org.geotools.filter.FilterFactoryFinder;
import org.geotools.xml.XMLSAXHandler;

import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory;
import org.opengis.filter.expression.Expression;

In our constructor we’ll set up a connection to the WFS server using a URL. If you’re tinkering with GeoServer, then that URL you’re looking for probably looks something like http://localhost:8080/geoserver/wfs. Since we know that we’ll want to filter our responses, it’s not a terrible idea to make a filter factory now and save it for later. In GeoTools everything is made using a factory. For filters, we need to make a factory using the new keyword, though. Here goes.

public class WFSUpdater {

    private DataStore wfs;
    private FilterFactory filterFactory;

    public WFSUpdater(String url) {
      try {
        URL endPoint = new URL(url);

        XMLSAXHandler.setLogLevel(Level.OFF); // turns off logging for XML parsing.

        // Parameters to connect to the WFS server, namely the URL.
        // You could have others, say if you had to authenticate your connection with a username and password.

        Map params = new HashMap();
        params.put(WFSDataStoreFactory.URL.key, endPoint);

        wfs = (new WFSDataStoreFactory()).createNewDataStore(params);
        filterFactory = FilterFactoryFinder.createFilterFactory();

      } catch (MalformedURLException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

Now that we have a connection, it’s time to make the transaction. As a first timer to GeoTools, I found it difficult to crawl through the documentation. Lots of their classes and methods have been deprecated with no clear hints about what to use instead. I had a hard time finding the right constructors. In what follows everything is hard-wired into the code, but it shouldn’t be all that bad to tweak things so that it works the way you like.

public void updateProperty(String propertyToUpdate, String updatedValue) {
    // This is the layer name on the server.
    String layer = “myns:LayerToUpdate“;
    Transaction update = new DefaultTransaction(”update”); // The handle/ID of this transaction is called “update.” It’s required.

    try {
      // Make the filter.
      Expression property = filterFactory.property(”constraintProperty“);
      Expression value = filterFactory.literal(”constraintValue“);
      Filter filter = filterFactory.equals(property, value); // This is an org.opengis.filter.Filter.

      FeatureStore features = (FeatureStore) wfs.getFeatureSource(layer);

      // Set the transaction. Otherwise, we can’t commit our changes later on.
      features.setTransaction(update);

      // Fetch the property from the FeatureType schema so that we can update it with the new value.
      FeatureType schema = features.getSchema();
      AttributeType atrributeToUpdate = schema.getAttributeType(propertyToUpdate);

      features.modifyFeatures(atrributeToUpdate, updatedValue, (org.geotools.filter.Filter) filter); // There’s that casting again.

      // Record the modifications.
      update.commit();

      } catch (IOException e) {
        e.printStackTrace();
      }
    }
}

Anyway, I hope this saves some people the hassle of tearing through the Javadocs for GeoTools. Also, if there’s a better way to do what I did, please let me know. Happy GIS-ing.

Technorati Tags: , , , , , , , , , , , , ,

Genetics by the Poolside

Sunday, July 8th, 2007

Happy Independence Day! To celebrate our nation’s founding, my family and I often hit up the Cape. This year was no exception, there’s little to report. The weather has been spotty: a little rain here, a few showers there, but nothing substantial. Someone was playing bagpipes the other night. And I witnessed a gruesome car accident a few feet from my balcony during the fireworks spectacular. A mass of people immediately sprang up to help the man, direct traffic, and call 911 repeatedly until emergency vehicles could make their way here. I was genuinely impressed by the response, professional and make-shift alike. Within seconds the response team had the guy off to the hospital in no time flat. I think it prudent not to speculate on the cyclist’s health. I don’t want to jinx anything, you know.

And since it’s vacation time, I’m here, at the kitchen table, on my laptop, implementing genetic algorithms. Maybe later I’ll describe what I did. Maybe if I do, someone will be able to tell me if my results make any sense. Whether or not my programs reproduce the classical results isn’t really the point, though. Look at the evolution of strategies for playing the iterated prisoner’s dilemma: they make perfect modern art tile mosaics! I bet someone’d love to have this pattern on their pool floor or garden wall. (Don’t be alarmed that they don’t appear all that related. Each row in black represents the fittest individual from one of a number of independent runs. That is, they probably never had the chance to meet each other.) Imagine the graphic tastefully obscured by flowering vines. (Click on it for a larger image.)

I can see an upside-down raccoon in it. What can you find?

Crab Canon

Thursday, April 5th, 2007

This week we had to create a sound collage for my computational media class. I didn’t set aside a lot of time to work on it, so it became something of a last-minute project. Today I spent most of my day meeting with bioinformatics folk to discuss herberia and taxa and distributed architectures. I may end up working in a CS research group playing with this (or other) stuff. Anyway, by the time I got home, I only had a few hours to start and finish my project. Luckily, I’ve been toying around with MIDI on my own. So I took a line from J.S. Bach and tried to reconstruct part of his crab canon. (This amounts to reversing, compositing, and normalizing a small bit of data.)

Here’s what I started with.

Two hours later, here’s what I ended up with. True it’s not precisely a crab canon—I prefixed a short introduction before the canon starts proper. But if you played from then on backwards, it would sound exactly the same. That’s right: I one-upped Bach. He thought he was writing a musical palindrome. Unfortunately, he couldn’t reverse the attack and decay of each note. He needed me to come along and help him out with the minor, technical details. There’s no shame in that.

You can even download my project in spiffy MP3 format if you like. I’m just that sort of guy. Giving, courteous, clean.

Crab Canon.mp3

Technorati Tags:, , , , , , ,

I’ve Landed.

Tuesday, March 13th, 2007

Today I was reverse-stalking the links pointing to my site when I discovered that Planet 02138 uses my RSS feed to fill their content. To be honest, I’m a little touched. At first I thought it might be associated with that magazine I don’t read with the same zip code. Fortunately, it’s not. If I were to guess, it’s another service offered by the kind folks at the Berkman Center.

Anyway, here’s how the Planet explains itself:

Planet 02138 is a collection of Harvard blogs. It is a sample of opinions and ramblings by Harvard students, faculty, and alumni.

From what I saw, they nailed it head-on.

You can make your own feed reader with the software from Planet Planet. Gosh, that’s fun to say.

Trolling their blogroll inspired me to update my own. Sure, my RSS reader knows what I’m currently reading—somehow my blog got left behind, though. After all, how are you going to know what I’m [likely to be] reading?

Technorati Tags:, , , , , , ,

Grassy Field

Sunday, March 11th, 2007

Since all I do these days is post my school projects to my blog, here’s another one for you. This week we had to create a collage. The requirements were pretty bare: at least five instances of the picture, one rotation, one rescaling, and at least one color modification. Try to spot each of the requirements in the final product below. (Maybe you’ve seen the original image before.) I had planned on using longer strips than the squares I ended up implementing, but I got lazy. The checkered effect is a little busy for my tastes; hopefully it’ll make the grade.

I tried for freakin’ ever to get the sky to soft clip to the hill top. I was able to adapt the intermediate image technique described in this article to create a tacky sun (not shown for art’s sake), but not for much more. Instead, I used the built-in, jagged setClip() method native to the java.awt.Graphics2D class. In case you were wondering, the clip was made with about six straight lines. I hate spline fitting, and try never to use curves—especially if line segments will do just fine. File that little tidbit away, it could be useful someday.

But convolutions rock. I’ve always thought so. Ever since I started using them to do signal processing in astronomy class. Our professor made us do a lot of convolutions using a visual calculus that really changed the way I thought about calculation in general. Drawing it out refined my sense of geometric interaction and avoided a lot of messy integrals. Here’s to qualitative methods: hurrah!

A field in collage

Technorati Tags:, , , , , ,

Books

Saturday, January 20th, 2007

I’ve spent part of tonight cataloguing my books. This is only the beginning. I need to differentiate among books I own, I’ve read, I’d like to own, and books I’d like to read. Also, I needed to figure out a work-around for the Library Thing blog widget since the Law School server doesn’t allow me to execute scripts. (First pass hack shown below.)

The State of Grafitti: Yuppie as Mascot

Sunday, December 31st, 2006

About a year ago, I was at the Park Street station on my way back to Cambridge. As I waited for the train to come, I did what I always do when I’m waiting without a book: I paced the end of the platform. Rather than slowly pass my foot over the knobs of the textured, yellow safety strip,—a favorite pastime of mine—I kept to the flat brick on a well-defined route that visits the supporting columns which dwell nearest to the tunnel’s opening.

Normally I’m not struck by public graffiti, but every once in a while something unexpected crops up. This time one of my columns read: “Kill all yuppies.”

I was very excited by this message. No, I’m not in favor of killing all the yuppies. That suggestion’d put me too close at risk. There’s a very good chance, indeed, that I’m a yuppie. So, no. Please be kind to the yuppies. But here’s what’s different. Normally the graffiti that I’ve encountered are either some sort of tag—you know, a personal statement of existence and potential ownership, “Kilroy was here” or “AlL St*R” or something along those lines—or alternatively they are some commitment of love or hate (often accompanied by a slur or two). You seen them, something like “Joe is a fag” or “I love Tiffany.” Anyway, all of these examples are personally directed. They don’t extend beyond an individual. Sometimes I’ll find one that condemns a whole group of people, like my yuppies example, scrawled on a public alleyway. But those even those are gang-related or race-related. Yuppies represent something new.

Whoever wrote it got my attention because his hatred was not race-directed. It points to a larger social movement. The new segregation, if it is really new, will be intellect. And these upwardly mobile persons are central enough to earn the distinguished role of spokesperson. But what exactly are yuppies mascots of? Well, that sort of brings me to some more recent graffiti.

The Ashmont train station is undergoing some pretty hefty repairs. Officials have suspended the Mattapan High Speed Line service for a year, and the train station is hidden from plain sight by several, several ton mounds of dirt. Like most other forms of transportation in the city, the Ashmont station is going underground. It’ll take some time before things are back in order. For now, there are lots of make-shift wooden structures to take the places of the bus depot and station entrance. And that means there’s plenty of board space for community art—I mean graffiti.

The last time I was at Ashmont I noticed some of the newer pieces as I walked by one of the wooden panels. This time a website caught my eye. I haven’t seen many hypertext tags outside of the internet, but there it was: a link to someone’s myspace page. Kilroy has entered a new age and he’s updated his message. Now the statement is “I am not here, I’m here. Come find me.” It’s a revolution. Personalization on the web is at an all-time high, and movers in the field want more of it. Collaborative filtering, social navigation, blogs! They’re all in style, and they don’t look like they’re going to go away any time soon. I can’t say I mind it, either. In fact, I want to be more a part of it.

This is not the same technological revolution that your slightly older brother talked about only decades ago. No, the paradigm is different: we can read the writing on the wall. Literally. Before technology brought with it an increased level of impersonality. The assembly-line metaphor bled into everything—it’s still around, of course. Don’t worry, the transactional framework driven by the glory of mass manipulation of raw goods to form an endless supply of identical product is still very much alive. And people are still applying manufacturing-inspired methods completely out of context. And the effect is still very isolating. But lo! the very same push to maximize profit that once aimed to cut time and kill interpersonal relationships has turned a corner. Personalization is the new rage.

But will personalization help build bridges among people; won’t it keep us even more securely glued to our seats in front of our computers? I’m afraid that it can. Technologically-backed social ventures, like AOL Instant Messenger and other chat programs, have made it easier for the quiet kids to remain quiet and alone. Chat tools give the user the appearance that they’re interacting with other people. But some researchers suggest that the analogy is only that: apparent. The real satisfaction one gains from honest-to-goodness, face-to-face conversation is so much greater than its virtual manifestation that it’s almost silly to make the comparison. So, what’s going on?

The invitational nature of MySpace is different than AIM. A person’s page is like his home. Each click to that site is really a visit. That’s why it makes the news so often. Sometimes the visits aren’t just virtual. And everyone uses it: college kids, little kids, married couples. The range of demographics represented by MySpace’s users is enormous. Unlike Friendster, which originally withheld a user’s access to a stranger’s page by default, MySpace let everyone see everyone else from the get-go. Friendster was a place for people who were already friends. MySpace, I believe, was built to get people to go to and listen to new bands in concert. The idea that you’d actually meet strangers was the founding idea. Now it’s just a place find others you’d like to bone à la Craig’s List’s personals but less so. But the idea that you might meet the person attached to the website is still very much there. Isn’t that exactly what that graffiti from Ashmont Station was all about? The internet takes all the scariness out of meeting a stranger, because you don’t physically meet, and the meeting is still completely anonymous. (There’s a trade-off, though. The relationships that form are even more tenuous than those so-called and ever important “weak bonds.” Online relationships tend to be superficial and sometimes socially damaging. Like I said before, they permit the loners to find each other and stay alone. Even those of us who aren’t loners end up as loners the longer we stay online rather than outside.)

So we’ve found a cause for our mascots. Like the term itself, today’s yuppies herald the dawn of a new form of impersonalization: isolation through personalization. Technology is poised to use what it knows about you and your preferences to make a friendlier, easier experience. In the process, you get to interact with others—real or not. The interaction is deep enough to convince you that you’ve done something meaningful. You’ve made a friend or learned a new fact. (Wikipedia is a blessing and curse.) But have you really; can you rely on your friend or apply your fact?

Your iPod list has exactly the music you want to hear. And so now, people go through life not listening to each other but to themselves, plugged into a clean, white box whose world revolves around the most important person—its only person: its master is me. Time Magazine got it wrong. The person of the year is not You; it’s me. This is the society recorded in graffiti today.

Technorati Tags:, , , , , , , , , , , , , , , , , , , , , ,

Back to the Fun Stuff

Friday, November 17th, 2006

I’ve started doing what I like to do again. That’s right. I’ve decided that it’s been long enough. Why don’t I have a tag-driven, database-backed website up and running yet? I’ve been meaning to collect funny and meaningful quotes—Lord knows I run across so many good ones everday. But for some reason I have forced myself to sit down and set up the site. Rather than using some out-of-the-box, I’m going to take this as an opportunity to learn some skills. I’ve taken out three books that may or may not prove useful: two on data mining, one on interaction design. Because of the hype, I should probably get one that covers AJAX, too.

I’ll let you know where and when you can see what I’ve cooked up.

I consider this a living exercise in computer science and philosophy. I’m fascinated by these folksonomy things and what it means for information architecture. I think that these user-authored category systems are going to propel the semantic web in the immediate future. But then again, I’m not sure really I know what I’m talking about. Please don’t trust me. I need to do some more reading and thinking about distributed cognition. Please let me know about your insights on these subjects.

The bloodgood shed all its foliage. The core stem is green. I keep tricking myself into finding what are not new buds. Still, it looks healthy. And it’s beautiful, what with its sterile dignity. Maybe I’ll go purchase some clunky, black frames to match it.

Technorati Tags:, , , , , , ,

The Writing on the Wall

Saturday, August 12th, 2006

I have some spare time in between my summer job and my fall classes. So I’ve spent the past five days learning to program in PHP and MySQL. My focus has been on the development of so-called large scale web applications. Luckily my dad has agreed to accommodate my unemployment, taken me back in, and even found me a room in the apartment so that I no longer have to sleep on the couch in the living room. In process of learning good organization and coding practices, I came across the idea of templates. And then I realized why graffiti never became an art form, despite its introduction into high-class New York galleries in the early 90s. Stephanie, this post is dedicated to you.

Templates are pretty natural and, nowadays, pretty common. Anyone who has used Microsoft Word or Excel has probably seen a template, some have maybe even used one. They’re empty containers which you can fill in with your own information to produce a finished product without too much effort. What they do, though, is subtle. I hadn’t realized just how subtle they are until last night. Templates allow you to separate content from presentation. This is important. The same thing works in programming, except in web development it’s a little bit more complicated.

A web application has three parts: the content, the presentation—both of which you, the user, see—and the business logic—the code which does the actual heavy-lifting in the silently backroom in the dark. It’s a good idea to keep these guys as far away from each other as possible. You enter the content, more or less, in HTML. Fortunately and unfortunately, the paragraph tag < p > is blind to your content. You wouldn’t format the address in a letter the same way you format a recipe, for example. HTML, however, can’t distinguish between the two. It treats everything similarly. Luckily, that’s where another standard, Cascading Style Sheets (CSS), comes in.

CSS allows you to tell the browser exactly how you’d like a certain type of text to look. You can control placement, font face, font weight, behavior in response to events (like when the mouse cursor hovers over a link), and more! This is the presentation part of it all.

A clever little package, aptly named Smarty, lets you keep your PHP scripts from mingling with your HTML and your CSS. That way you can redesign the look of your pages without having to update the guts which control the functionality, too. Your copy editors and content managers stay happy, too, because they aren’t effected, either, and can continue doing what they like to do best: write content.

And all this got me thinking about my friend Stephanie and her undergraduate honors thesis. It straddled the divide between literature and art history; she wrote about the rise and subsequently fall of graffiti in the art world. She argues that the art world rejected graffiti, actually, a particular type called Writing—writers would never call their Writing graffiti, so why should we?—because it was written and people got caught up in trying to read it. And that makes sense. Try to take in the artistic value of the following:

Do not read this.

You can’t. If you know how to read in English, then you read and processed the above statement, even though I intended it as a purely visual object. Writing is a little bit more subversive. A single author didn’t always tag with one name, and often the script was so stylized that it was impossible to read in the popular sense of the word. Yet other Writers had no problem identifying authorship. The trick is, they were able to distinguish between the presentation and content of a Writer’s tag. Critiques strove to find meaning in the words the Writers presented—meaning that was never there. And the style discovered its author, not the name. Writers had deconstructed the written word, extracting only a visual idiom while leaving the word’s referent alone to fend for itself fully detatched from its referrer.

Few people in the academia of literature, it seems, study the effects typography and layout have on a written work for better or for worse. Perhaps it is more important to distinguish the two when investigating Islamic writing (and its calligraphy) or medieval, illuminated texts. Too bad, though, that presentation has been relegated to the design world. Everyone interacts with layout. It affects so much of what we do everyday.

ImagineifIhadalternatedcolorratherthanusedspacestobreakbetweenwords?

What are the implications of my scheme?

I don’t even want to mention what a meta-language like XML might mean to literature academes. At least not now.

Paddington Puzzles

Tuesday, July 11th, 2006

Mentioned last time, it’s worth seeing:

Paddington Bear Puzzle

Seeing as I’m here, and you’re probably not, I just opened a Flickr account so that we may share my experiences more fully. You can even subscribe to its RSS feed. I will try to update it frequently enough to make things interesting.

Protected by AkismetBlog with WordPress