Python Canvec Extractor

The Government of Canada provides a nifty dataset called CanVec which contains all the features you’d find on a standard NTS map in Shapefile format. The data is provided as zip archives, each corresponding to a map sheet in the NTS system.

The trouble arises when you want to extract a specific feature type across several sheets; you have to navigate a deep folder hierarchy, opening, searching and extracting files as you go.

I made this little Python script to make the task easier. It navigates the CanVec directory (in whatever form it exists on your drive), finds the files that match your search criterion and extracts them to a temporary folder. Then it creates an SQL file containing the instructions to create and populate a table in a PostGIS database with the data. This last step requires that shp2pgsql exist on your PATH.

Here’s the invocation:

python canvec.py FO_1030009 canvec contours_m output.sql ./canvec ./tmp

This tells canvec.py to find files whose names match the string FO_1030009 (metric contours). It searches for archives in ./canvec and saves temporary files in ./tmp. Then it outputs a file called output.sql which will create and populate a table called contours_m in the canvec schema.

Check it out on GitHub.

Posted in GIS, Maps, Python | Tagged , , , , , | Leave a comment

Subclassing DOM Elements in JavaScript

Ok, you actually can’t do that. But you can do something that feels the same.

The trick is to create a factory method, initialize the variables and functions that your subclass requires as local to the method and tie them to the DOM element instance using one or more closures. Using variables local to the factory function makes them “private” to the extent that such a concept exists in JavaScript, and you can use Object.defineProperty to allow”public” access to your new methods and properties.

For an example, I’ll create a subclass of the HTMLImageElement, and give it a setter called image. The setter receives an instance of a custom Image object that contains a src property and a title property. When the Image instance is set on the HTMLImageElement subclass instance, the source and alt text of the image are set implicitly.

First, the Image object.

/**
* Represents an Image.
* @param src The source URL of the image file.
* @param title The title of the image.
*/
function Image(src, title){
  this.title = title;
  this.src = src;
}
Image.prototype = new Object();

Now the “subclass.”

/**
* Creates a "subclass" of HTMLImageElement.
*/
function newImageElement(){

  // Create a regular img element.
  var img = document.createElement("img");

  // A variable to hold the Image instance.
  var _image = null;

  // Create a property. The get and set closures will keep
  // the _image variable in scope. The setter sets the properties
  // on the img element.
  Object.defineProperty(img, "image", {
    get : function(){
      return _image;
    },
    set : function(value){
      _image = value;
      this.src = value.src;
      this.alt = value.title;
    }
  });

  // Return the modified img element.
  return img;
}

You can do the same with methods:

/**
* Creates a "subclass" of HTMLImageElement.
*/
function newImageElement(){

  // Create a regular img element.
  var img = document.createElement("img");

  // The body of the setImage function
  var _setImage = function(value){
    _image = value;
    this.src = value.src;
    this.alt = value.title;
  }

  // The body of the getImage function
  var _getImage = function(){
    return _image;
  }

  // Create the public setImage property.
  Object.defineProperty(img, "setImage", {
    value : _setImage
  });

  // Create the public getImage property.
  Object.defineProperty(img, "getImage", {
    value : _getImage
  });

  // Return the modified img element.
  return img;
}

To use the factory method, do the following:

  // Create an img element.
  var img = newImageElement();

  // Create the image object.
  var image = new Image("http://farm6.static.flickr.com/5187/5774436039_28352f0d8f.jpg", "A cyclist.");

  // Set the image on the img.
  img.image = image;

  // Append the img on the document's body.
  document.body.appendChild(img);

Simple! Here’s a working example (view source to see the code.)

Posted in DOM, JavaScript | Leave a comment

JavaScript, Here I Come!

Well, my short, but very productive career as a Flash/FLEX/ActionScript programmer (here, and here) is probably coming to an end. Flash and its associated technologies are still pretty amazing, as far as I’m concerned but… let’s just say I took the deprecation of the Google Maps Flash API as a sign. My interests and Adobe’s have diverged.

That’s OK, though, because I’m going to dive into JavaScript and post about my experiences here. It can certainly be a frustrating transition — from strongly-typed languages like ActionScript and Java (where the IDE saves you the bother of having to remember anything) to JavaScript — but for the foreseeable future it’s the language of choice on the client and I’m prepared to embrace it.

Stay tuned.

My friend Dave has some interesting thoughts on the future of Flash/Flex.

Posted in Uncategorized | Leave a comment

Real-time Weather with Flash and Java

1-Wire Weather Station

1-Wire Weather Station

Update: The weather station is down… It withstood a couple of rough winters, but it wasn’t meant to be a real weather instrument, just a test-bed for the one-wire sensors. Perhaps I’ll resurrect it one day, but for now it’s offline.

This is a real-time weather station I built using an AAG electronica instrument and an EEE 701 for the server.

I coded the server in Java using Apache’s Mina library for non-blocking sockets. The client is FLEX, and uses Socket (rather than HTTP). The webcam service consists essentially of a Java class that reads the video4linux data as raw RGB and saves it to a JPEG which is then streamed over the socket along with all the other data (I built the server to be pluggable, so each of these little add-ons is a plugin that shares the stream with the other plugins). The archival data lives in MySQL.

Posted in ActionScript, FLEX, Java, Weather | Leave a comment

Flex Earthquake Map

Shows the world’s earthquakes during the last 24 hours. Blue markers are small magnitude quakes, red markers are large magnitude.

Data updates every 30 seconds and comes from here: http://earthquake.usgs.gov/earthquakes/catalogs/

Continue reading

Posted in ActionScript, FLEX, GIS, Maps | Leave a comment

AppendToGatewayUrl

If you’re using BlazeDS and seeing an error involving a callback called “AppendToGatewayUrl” this might help.

AppendToGatewayUrl modifies the gateway of a NetConnection object with the session id (in this case “jsessionid”) of the current session. If you reconnect with this value appended to the gateway URL, your session can be identified, even on clients that do not use cookies.

There are two ways to handle this:

  • set NetConnection.client to an object that implements the AppendToGatewayUrl function, or;
  • subclass NetConnection to implement it (since NetConnection.client points to the NetConnection instance by default, you can not reassign the client field if you want this method to work).

There’s one caveat to both approaches, though. It has been my experience that if you call NetConnection.connect while you are receiving a response to bundled remote methods, the data may be lost or a “Client.Data.UnderFlow” error thrown. This occurs because AppendToGatewayUrl gets called after the first result, and the immediate call to NetConnection.connect aborts the rest of the transfer.

Here’s a sample implementation of option A:

public class Sample{

  private var conn : NetConnection;

  public function Sample(){
    conn = new NetConnection();
    conn .client = this;
    conn .connect('http://localhost/gateway');
  }

  public function AppendToGatewayUrl(extra:String):void{
    conn .connect(conn.uri + extra);
  }

  public function load():void{
    conn .call('service.load',new Responder(loadResult,error));
  }

  private function loadResult(value:*):void{
    trace('value',value);
  }

  private function error(err:*):void{
    trace('error',err.message);
  }

}

To get around the problem with bundled calls, you could save the session id in a field and reconnect before the next call.

Posted in ActionScript, BlazeDS, Flash, FLEX | Comments Off

Why Does Flash Delete your Timeline Code?

If you’ve tried to load one swf into another and found that the loaded clip’s timeline code has disappeared, you’re not alone. Fortunately, the explanation is simple.

Usually what happens is, you’ve tried to cast the reference to the loaded swf to its document type, like so:

var page : MyDocumentClass = loader.content as MyDocumentClass;
page.doSomeMethod();

If doSomeMethod, for example, plays an animation on the timeline that you have governed with “stop” commands, or that calls methods in the document class, you’ll find that it doesn’t work.

Why? Because you’ve embedded the class definition in the parent movie, and it doesn’t know about the timeline code. Think about it: adding timeline code to a movie effectively makes that movie a subclass of its document class. When you embed a reference to the document class in the parent, the version associated with the child movie (with the timeline code) isn’t used.

An associated problem is that you’ve defeated some of the key reasons for modularizing your sites – to make updates easier, and load code only when it’s needed, not before. Embedding the document classes of your loaded movies defeats the former – because you have to recompile the parent as well as the child when you update the child, and the latter – because you’re loading all the child classes in the parent.

How to solve the problem, then? use interfaces. Make all of your document classes implement a single interface and make absolutely sure that no references to the document classes exist anywhere in the parent movie.

For example, here’s a child movie class:

public class MyChild extends MovieClip implements IChild{
  public function transitionIn():void{
    gotoAndPlay('somewhere');
  }
}

As you can see, MyChild implements IChild, an interface:

public interface IChild extends IEventDispatcher{
   function transitionIn():void;
}

Now, in your parent movie, you’ll use something like this:

private function movieLoaded(evt:Event):void{
  var child : IChild = loader.content as IChild;
  child.transitionIn();
}

Notice that you’re calling a method on the interface, not the document class. In fact, the parent movie should be completely agnostic where the doc classes are concerned. The interface is enough.

Posted in ActionScript, Flash | Comments Off

Update: Using PixelBender to Crunch Numbers

I didn’t realize it, but there’s a bug in PixelBender for Flash when using image4 input. In my case, I have x-y coordinates, and I was trying to operate on two at a time using image4. Can’t do it (yet). The kernel craps out after 60 elements.

Of course, you’re not allowed to output 2- or 1-element pixels, so you have to use image3 for input. What to do? It’s simple really: Say you have a Vector of numbers representing pairs (coordinates, in my case). The first pixel with contain x,y,x and the second y,x,y and so on. All you have to do is check the current pixel’s x-coordinate (assuming the height of your PixelBender input is set to 1) and do the calculations in one or the other order.

Note that in PixelBender, the x,y of the pixel (as reported by outCoord()) is the midpoint of the pixel, so x=0.5,1.5,2.5 and so on. To determine which order to take the calculations in, you just need to know whether x is even or odd.

Continue reading

Posted in ActionScript, Flash, FLEX | Comments Off

Using Pixel Bender for Math in Flash/FLEX

Pixel Bender has applications other than manipulating images in Flash/FLEX. While working on some map projection experiments, I decided to look for a way to offload some of the computations to avoid freezing up the display or simply taking too long. Turns out, Pixel Bender not only does math several times faster than Flash, it can also be run asynchronously!

Continue reading

Posted in ActionScript, Flash, FLEX | 2 Comments

A GPX Parser in ActionScript 3

GPX is the standard XML format used to store routes, tracks, waypoints and other data for GPS units.

I’m working on a parser in AS3 that will load a GPX file and expose all the information inside as ActionScript objects. See a test using Google Maps below.

Click “Load” to load the file from my server (this is a file containing a short bike ride around Victoria, BC), or click “Browse…” to load your own GPX file.

I haven’t tested this very extensively, so if your file has features that mine doesn’t, it may fail. If it does, please leave a comment! I’ll be uploading this to Google Code as soon as I do some more tests.

Continue reading

Posted in ActionScript, GIS, GPS, Maps | 6 Comments