Tuesday, June 15, 2010

Merging Shapefiles with JavaScript

GeoScript aims to provide a framework for scripting common data processing tasks with GeoTools and the Java Topology Suite. Much in the same way that scripting languages like Python can leverage the powerful OGR/GDAL libraries, GeoScript offers similar functionality from languages that run on the JVM (currently Groovy, JavaScript, Python, or Scala).

In a recent post on his blog, Darren Cope wrote about merging a directory of shapefiles, a common data processing task. Here I'll demonstrate how to do the same thing with the JavaScript version of GeoScript.

This example uses the workspace and layer modules. I'm working with a directory containing shapefiles of US Census block groups for each state, merging them into a single shapefile for the country.


// import modules
var workspace = require("geoscript/workspace");
var layer = require("geoscript/layer");

// create workspaces from existing directories
var source = new workspace.Directory("path/to/source_dir");
var target = new workspace.Directory("path/to/target_dir");

// iterate through layers in source workspace
var country;
source.names.forEach(function(name) {
// create state layer from existing shapefile
var state = source.get(name);
// create country layer first time through
if (!country) {
country = new layer.Layer({
schema: state.schema.clone({name: "country"})
});
// this creates the new shapefile on disk
target.add(country);
}
// iterate through source features to add each to target
state.features.forEach(function(feature) {
country.add(feature);
});
});


While it is hard to beat the terseness of the ogr2ogr example, the JavaScript version of GeoScript provides syntax that I hope is familiar to a larger population of scripters.

No comments:

Post a Comment

Introducing GeoScript

GeoScript adds geo capabilities to dynamic scripting languages such as JavaScript, Python, Scala and Groovy.