Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

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.

Monday, May 10, 2010

Calculating Centroids with JavaScript



In a previous post, Jared described how to use the Groovy GeoScript to create a new Shapefile based on centroids of features in an existing Shapefile. Today, I'll do the same using the JavaScript implementation.

One significant difference from the other implementations is that there is only one layer type in the JavaScript flavor of GeoScript. To create a layer representing a Shapefile, I'll create a directory workspace and get the layer from that.

In this example, I'll also use the clone methods to create a new layer schema and new features. These clone methods allow for new objects to be created with modified properties. This is useful in creating the schema for the centroids layer, because we want the same fields as the states layer with the exception of the geometry field. In this case, we're creating a layer with point geometries instead of multi-polygon geometries.

Without further ado, here is the code:

// import Directory and Layer constructors
var Directory = require("geoscript/workspace").Directory;
var Layer = require("geoscript/layer").Layer;

// create a directory workspace from an existing directory on disk
var dir = new Directory("data/shapefiles");

// create a layer based on an existing shapefile in the directory
var states = dir.get("states");

// create a new schema with a Point geometry type instead of MultiPolygon
var schema = states.schema.clone({
// give the schema a new name
name: "centroids",
fields: [
// overwrite existing field named "the_geom"
{name: "the_geom", type: "Point", projection: "EPSG:4326"}
]
});

// create a new temporary layer with the new schema
var centroids = new Layer({
schema: schema
});

// add the layer to existing workspace (this creates a new shapefile on disk)
dir.add(centroids);

// iterate through the state features to create features with state centroids
states.features.forEach(function(state) {
var centroid = state.clone({
schema: schema,
values: {
// overwrite the geometry value with the state centroid
the_geom: state.geometry.centroid
}
});
// add the new feature to the layer (this writes to the shapefile)
centroids.add(centroid);
});


The GeoScript viewer module exports a draw method. You can use this to render a layer, or arrays of geometies or features.


// use the viewer module to draw collections of features or geometries
var draw = require("geoscript/viewer").draw;
var geometries = [];

// add all state geometries to the array
states.features.forEach(function(state) {
geometries.push(state.geometry);
});

// add buffered centroids so they will be visible
centroids.features.forEach(function(centroid) {
geometries.push(centroid.geometry.buffer(0.5));
});

// draw all geometries
draw(geometries);


You'll find this example in the examples directory of the repository. See the JavaScript API documentation for more detail on using GeoScript.

Introducing GeoScript

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