0%

gtran: a promised, consistent, user-friendly GeoJson conversion package

Do you still feel inconvenient when converting GeoJson to other data formats in Node.js?

GeoJson is one of simplest geographic data format. If we search for data conversion packages at npm.org, there will be a long list packages: shapefile for shapefile, tokml for kml, geojson2dcsv for csv, osm-and-geojson for osm package, geojson2 for multiple formats, org2org and gdal as data solutions… However, I still felt very inconvenient when I developed a GeoJson handling module a few months ago.

Why inconvenient?

From my point of view, there are a few problems:

  • Most packages are designed for one specific data format.

  • Each package is written in a style different from the other.

  • Some packages require external libraries,like gdal, which aren’t npm-able.

  • Most package aren’t asynchronous or promised. (maybe just a personal problem)

As a result, in order to make them work together, one will need to learn how to use these diverse packages, customize code to handle their diverse input/output, and write some comments for the future project maintainer.

Well, it’s not very fun.

Gtran: the solution

The programmer’s way to handle unhappiness is to write something that make him happy :) What I want is a package with following features:

  • Able to work with multiple formats

  • Consistent and simple: functions and their input/output

  • Completely npm-able

  • Asynchronous

And here comes gtran.

What does gtran do?

gtran wraps several existing npm packages to provide

  • from/to conversion of multiple formats: .shp, .kml, .kmz, .csv, and TopoJson.

  • consistent functions: from[format name]() and to[format name]()

  • simple input/output: geojson or file name

  • asynchronous ability with your favorite promise library (bluebird, promise, Q, or native).

  • minimal installation: each format support is provide by a child package gtran-xxx.

It could be installed with npm install gtran.

How could it be used?

A complete use guide could be found at the GitHub repo and here is a small use case:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var gtran = require('gtran');

# Specify the promise library if necessary
gtran.setPromiseLib(require('bluebird'));

# Read shapefile
gtran.fromShp('source.shp')
.then(function(object) {
var geojson = object;
});

# Save geojson into shapefile
gtran.toShp(geojson, 'point.shp')
.then(function(fileNames) {
console.log('SHP files have been saved at:' + fileNames.toString());
});

# Read csv file
# Assume the test.csv has two columns: latitude and longitude
gtran.fromCSV('source.csv', {
mapping: { x: 'longitude', y: 'latitude' }
})
.then(function(object) {
var geojson = object;
});

# Save geojson into a csv file
gtran.toCSV(geojson, 'point.csv')
.then(function(fileName) {
console.log('CSV file has been saved at:' + fileName);
});

Hope you enjoy it. If you find a bug or want a new feature, just create an issue and I will appreciate it :-)